]> git.cryptolib.org Git - avr-crypto-lib.git/blob - prng.c
documentation upgrade; most docu still missing but a first step is done
[avr-crypto-lib.git] / prng.c
1 /**
2  * \file                prng.c
3  * \author              Daniel Otte
4  * \date                17.05.2006
5  * \par License:
6  *      GPL
7  * \brief       This file contains an implementaition of a pseudo-random-number generator.
8  * 
9  * Extension 1:
10  *      rndCore is expanded to 512 bits for more security.
11  *
12  * \verbatim
13  *                      ####################################################################################
14  *                      #                                                                                  #
15  *                      #         +---------------------------+                                            #
16  *                      #         |                           |                                            #
17  *                      #         V                           |                                            #
18  *                      #      (concat)                       |                                            #
19  *  +---------------+   #    o---------o             (xor)+---------+      o---------o       o---------o   #    +--------------+
20  *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block |
21  *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |   o---------o   #    +--------------+
22  *                      #                                 (xor) (xor)                    |                 #
23  *                      #                                   ^     ^                      |                 #
24  *                      #                                    \   /                       |                 #
25  *                      #                                   (offset)---------------------+                 #
26  *                      #                                                                                  #
27  *                      ####################################################################################
28  * \endverbatim
29  */
30
31  /* \verbatim
32  *                      ####################################################################################
33  *                      #                                                                                                                                                      #
34  *                                          #             +---------------------------+                                                                                    #
35  *                                          #             |                                           |                                                                                    #
36  *                                          #             V                                           |                                                                                    #
37  *                      #      (concat)                           |                                                                                        #
38  *  +---------------+   #    o---------o             (xor)+---------+      o---------o       o---------o   #    +--------------+
39  *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+-> | sha-256 | -----> | random Block |
40  *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |   o---------o   #    +--------------+
41  *                                              #                                                     (xor)     (xor)                                    |                                 #
42  *                                              #                                                           ^     ^                                              |                                 #
43  *                                              #                                                            \   /                                               |                                 #
44  *                                              #                                                           (offset)---------------------+                                 #
45  *                                              #                                                                                                                                                      #
46  *                                              ####################################################################################
47  * \endverbatim
48  */
49
50 #include <stdint.h>
51 #include <string.h>
52 #include "sha256.h"
53
54 /**
55  * \brief secret entropy pool. 
56  * This is the core of the random which is generated
57  */
58 uint32_t rndCore[16]; 
59
60 /*************************************************************************/
61
62 /**
63  * \brief This function adds entropy to the central entropy pool
64  * 
65  * @param length This ist the length of the random data in BITS. 
66  * @param data This is the random data which should be added to the entropy pool
67 */
68 /* idea is: hash the message and add it via xor to rndCore
69  *
70  * length in bits 
71  * 
72  * we simply first "hash" rndCore, then entropy.
73  */
74 void addEntropy(unsigned length, void* data){
75         sha256_ctx_t s;
76         static uint8_t offset=0; /* selects if higher or lower half gets updated */
77         sha256_init(&s);
78         sha256_nextBlock(&s, rndCore);
79         while (length>=512){
80                 sha256_nextBlock(&s, data);
81                 data += 512/8;
82                 length -= 512;  
83         }
84         sha256_lastBlock(&s, data, length);
85         uint8_t i;
86         for (i=0; i<8; ++i){
87                 rndCore[i+offset] ^= s.h[i];
88         }
89         offset ^= 8; /* hehe */
90 }
91
92 /*************************************************************************/
93 /**
94  * \brief This function fills a given buffer with 32 random bytes
95  * @param b Pointer to buffer wich is to fill
96  */
97 void getRandomBlock(uint32_t *b){
98         sha256_ctx_t s;
99         uint8_t offset=8;
100         
101         sha256_init(&s);
102         sha256_lastBlock(&s, rndCore, 512); /* remeber the byte order! */
103         uint8_t i;
104         for (i=0; i<8; ++i){
105                 rndCore[i+offset] ^= s.h[i];
106         }
107         offset ^= 8; /* hehe */
108         memcpy(b, s.h, 32); /* back up first hash in b */
109         sha256_init(&s);
110         sha256_lastBlock(&s, b, 256);
111         memcpy(b, s.h, 32);
112 }
113
114 /*************************************************************************/
115  
116 /**
117  * \brief This function simply returns a random byte
118  * @return a random byte
119  */
120 uint8_t getRandomByte(void){
121         static uint8_t block[32];
122         static uint8_t i=32;
123         
124         if (i==32){
125                 getRandomBlock((void*)block);
126                 i=0;
127         }       
128         return block[i++];
129 }
130  
131