]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
prng improvement
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Tue, 26 Jun 2007 04:10:46 +0000 (04:10 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Tue, 26 Jun 2007 04:10:46 +0000 (04:10 +0000)
prng.c
prng.h

diff --git a/prng.c b/prng.c
index 078ed0df24bce281ea6abd72fa228e2564841786..7a2cdda37d2743b15da3fd54ed9200947c62fddc 100644 (file)
--- a/prng.c
+++ b/prng.c
  *     rndCore is expanded to 512 bits for more security.
  *
  * \verbatim
- *                      ####################################################################################
- *                      #                                                                                  #
- *                      #         +---------------------------+                                            #
- *                      #         |                           |                                            #
- *                      #         V                           |                                            #
- *                      #      (concat)                       |                                            #
- *  +---------------+   #    o---------o             (xor)+---------+      o---------o       o---------o   #    +--------------+
- *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block |
- *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |   o---------o   #    +--------------+
- *                      #                                 (xor) (xor)                    |                 #
- *                      #                                   ^     ^                      |                 #
- *                      #                                    \   /                       |                 #
- *                      #                                   (offset)---------------------+                 #
- *                      #                                                                                  #
- *                      ####################################################################################
+ *                      ################################################################################################
+ *                      #                                                                                              #
+ *                      #         +---------------------------+                                                        #
+ *                      #         |                           |                             +---+                      #
+ *                      #         V                           |                             |   |                      #
+ *                      #      (concat)                       |                             |   V                      #
+ *  +---------------+   #    o---------o             (xor)+---------+      o---------o      | o----o     o---------o   #    +--------------+
+ *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
+ *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |    o----o     o---------o   #    +--------------+
+ *                      #                                 (xor) (xor)                    |                             #
+ *                      #                                   ^     ^                      |                             #
+ *                      #                                    \   /                       |                             #
+ *                      #                                   (offset)---------------------+                             #
+ *                      #                                                                                              #
+ *                      ################################################################################################
  * \endverbatim
  */
 
  /* \verbatim
- *                      ####################################################################################
- *                      #                                                                                                                                                     #
- *                                         #             +---------------------------+                                                                                    #
- *                                         #             |                                           |                                                                                    #
- *                                         #             V                                           |                                                                                    #
- *                      #      (concat)                                  |                                                                                        #
- *  +---------------+   #    o---------o             (xor)+---------+      o---------o       o---------o   #    +--------------+
- *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block |
- *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |   o---------o   #    +--------------+
- *                                             #                                                     (xor)     (xor)                                    |                                 #
- *                                             #                                                           ^     ^                                              |                                 #
- *                                             #                                                            \   /                                               |                                 #
- *                                             #                                                           (offset)---------------------+                                 #
- *                                             #                                                                                                                                                      #
- *                                             ####################################################################################
+ *                      ################################################################################################
+ *                      #                                                                                              #
+ *                      #         +---------------------------+                                                        #
+ *                      #         |                           |                             +---+                      #
+ *                      #         V                           |                             |   |                      #
+ *                      #      (concat)                       |                             |   V                      #
+ *  +---------------+   #    o---------o             (xor)+---------+      o---------o      | o----o     o---------o   #    +--------------+
+ *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
+ *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |    o----o     o---------o   #    +--------------+
+ *                      #                                 (xor) (xor)                    |                             #
+ *                      #                                   ^     ^                      |                             #
+ *                      #                                    \   /                       |                             #
+ *                      #                                   (offset)---------------------+                             #
+ *                      #                                                                                              #
+ *                      ################################################################################################
  * \endverbatim
  */
 
 #include <stdint.h>
 #include <string.h>
 #include "sha256.h"
+#include "prng.h"
 
 /**
  * \brief secret entropy pool. 
@@ -78,7 +79,7 @@ void addEntropy(unsigned length, void* data){
        sha256_nextBlock(&s, rndCore);
        while (length>=512){
                sha256_nextBlock(&s, data);
-               data += 512/8;
+               data = (uint8_t*)data+ 512/8;
                length -= 512;  
        }
        sha256_lastBlock(&s, data, length);
@@ -106,6 +107,7 @@ void getRandomBlock(uint32_t *b){
        }
        offset ^= 8; /* hehe */
        memcpy(b, s.h, 32); /* back up first hash in b */
+       ((uint8_t*)b)[*b&31]++;         /* the important increment step */
        sha256_init(&s);
        sha256_lastBlock(&s, b, 256);
        memcpy(b, s.h, 32);
@@ -127,5 +129,24 @@ uint8_t getRandomByte(void){
        }       
        return block[i++];
 }
+
+/*************************************************************************/
+/**
+ * \brief This function fills the given bock with length random bytes
+ * @return a random byte
+ */
+void fillBlockRandom(void* block, unsigned length){
+       while(length>RANDOMBLOCK_SIZE){
+               getRandomBlock(block);
+               block += RANDOMBLOCK_SIZE;
+               length -= RANDOMBLOCK_SIZE;
+       }
+       while(length){
+               *((uint8_t*)block) = getRandomByte();
+               ++block; --length;
+       }
+}
  
  
diff --git a/prng.h b/prng.h
index 5266368ebe1312244b4d6db9c2b72ffddaabfd8b..b2d31357d47d7fe89d86b4d7e9c53b51339842d9 100644 (file)
--- a/prng.h
+++ b/prng.h
 /*
  * length in bits 
  */
+#define RANDOMBLOCK_SIZE 32 /* bytes */
 void addEntropy(unsigned length, void* data); 
 void getRandomBlock(uint32_t* b);
 /* this does some simple buffering */
 uint8_t getRandomByte(void);
+
+void fillBlockRandom(void* block, unsigned length);
 
 #endif /*PRNG_H_*/