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_trits.h"
#include "ntru_convert.h" #include "ntru_convert.h"
#include <crypto/mgf1/mgf1.h> #include <crypto/mgf1/mgf1_bitspender.h>
#include <utils/debug.h> #include <utils/debug.h>
#include <utils/test.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) ntru_trits_t *ntru_trits_create(size_t len, hash_algorithm_t alg, chunk_t seed)
{ {
private_ntru_trits_t *this; private_ntru_trits_t *this;
uint8_t octets[HASH_SIZE_SHA512], buf[5], *trits; uint8_t octet, buf[5], *trits;
size_t hash_len, octet_count = 0, trits_needed, i; size_t trits_needed;
mgf1_t *mgf1; mgf1_bitspender_t *bitspender;
DBG2(DBG_LIB, "mgf1 based on %N is seeded with %u octets", bitspender = mgf1_bitspender_create(alg, seed, TRUE);
hash_algorithm_short_names, alg, seed.len); if (!bitspender)
mgf1 = mgf1_create(alg, seed, TRUE);
if (!mgf1)
{ {
return NULL; return NULL;
} }
i = hash_len = mgf1->get_hash_size(mgf1);
INIT(this, INIT(this,
.public = { .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) while (trits_needed > 0)
{ {
if (i == hash_len) if (!bitspender->get_byte(bitspender, &octet))
{ {
/* get another block from MGF1 */ bitspender->destroy(bitspender);
if (!mgf1->get_mask(mgf1, hash_len, octets)) destroy(this);
{ return NULL;
mgf1->destroy(mgf1);
destroy(this);
return NULL;
}
octet_count += hash_len;
i = 0;
} }
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) if (trits_needed < 5)
{ {
memcpy(trits, buf, trits_needed); 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 += 5;
trits_needed -= 5; trits_needed -= 5;
} }
i++;
} }
DBG2(DBG_LIB, "mgf1 generated %u octets to extract %u trits", bitspender->destroy(bitspender);
octet_count, len);
mgf1->destroy(mgf1);
return &this->public; return &this->public;
} }