Use bitspender->get_bytes() method in ntru_trits
authorAndreas Steffen <andreas.steffen@strongswan.org>
Thu, 11 Dec 2014 21:30:29 +0000 (22:30 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Fri, 12 Dec 2014 11:00:20 +0000 (12:00 +0100)
src/libstrongswan/plugins/ntru/ntru_trits.c

index 5d8fc59..57b3532 100644 (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;
-
-       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)
+       uint8_t octet, buf[5], *trits;
+       size_t trits_needed;
+       mgf1_bitspender_t *bitspender;
+
+       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;
 }