Use bitspender->get_bytes() method in ntru_trits

This commit is contained in:
Andreas Steffen
2014-12-12 12:00:20 +01:00
parent 83447555a6
commit 881cffb826
+13 -25
View File
@@ -16,7 +16,7 @@
#include "ntru_trits.h"
#include "ntru_convert.h"
#include <crypto/mgf1/mgf1.h>
#include <crypto/mgf1/mgf1_bitspender.h>
#include <utils/debug.h>
#include <utils/test.h>
@@ -70,18 +70,15 @@ METHOD(ntru_trits_t, destroy, void,
ntru_trits_t *ntru_trits_create(size_t len, hash_algorithm_t alg, chunk_t seed)
{
private_ntru_trits_t *this;
uint8_t octets[HASH_SIZE_SHA512], buf[5], *trits;
size_t hash_len, octet_count = 0, trits_needed, i;
mgf1_t *mgf1;
uint8_t octet, buf[5], *trits;
size_t trits_needed;
mgf1_bitspender_t *bitspender;
DBG2(DBG_LIB, "mgf1 based on %N is seeded with %u octets",
hash_algorithm_short_names, alg, seed.len);
mgf1 = mgf1_create(alg, seed, TRUE);
if (!mgf1)
bitspender = mgf1_bitspender_create(alg, seed, TRUE);
if (!bitspender)
{
return NULL;
}
i = hash_len = mgf1->get_hash_size(mgf1);
INIT(this,
.public = {
@@ -98,21 +95,15 @@ ntru_trits_t *ntru_trits_create(size_t len, hash_algorithm_t alg, chunk_t seed)
while (trits_needed > 0)
{
if (i == hash_len)
if (!bitspender->get_byte(bitspender, &octet))
{
/* get another block from MGF1 */
if (!mgf1->get_mask(mgf1, hash_len, octets))
{
mgf1->destroy(mgf1);
destroy(this);
return NULL;
}
octet_count += hash_len;
i = 0;
bitspender->destroy(bitspender);
destroy(this);
return NULL;
}
if (octets[i] < 243) /* 243 = 3^5 */
if (octet < 243) /* 243 = 3^5 */
{
ntru_octet_2_trits(octets[i], (trits_needed < 5) ? buf : trits);
ntru_octet_2_trits(octet, (trits_needed < 5) ? buf : trits);
if (trits_needed < 5)
{
memcpy(trits, buf, trits_needed);
@@ -121,11 +112,8 @@ ntru_trits_t *ntru_trits_create(size_t len, hash_algorithm_t alg, chunk_t seed)
trits += 5;
trits_needed -= 5;
}
i++;
}
DBG2(DBG_LIB, "mgf1 generated %u octets to extract %u trits",
octet_count, len);
mgf1->destroy(mgf1);
bitspender->destroy(bitspender);
return &this->public;
}