]> git.cryptolib.org Git - avr-crypto-lib.git/blob - entropium.c
insereated GPLv3 stub
[avr-crypto-lib.git] / entropium.c
1 /* entropium.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * \file                entropium.c
21  * \author              Daniel Otte
22  * \date                17.05.2006
23  * \par License:
24  *      GPL
25  * \brief       This file contains an implementaition of a pseudo-random-number generator.
26  * 
27  * Extension 1:
28  *      rndCore is expanded to 512 bits for more security.
29  *
30  * \verbatim
31  *                      ################################################################################################
32  *                      #                                                                                              #
33  *                      #         +---------------------------+                                                        #
34  *                      #         |                           |                             +---+                      #
35  *                      #         V                           |                             |   |                      #
36  *                      #      (concat)                       |                             |   V                      #
37  *  +---------------+   #    o---------o             (xor)+---------+      o---------o      | o----o     o---------o   #    +--------------+
38  *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
39  *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |    o----o     o---------o   #    +--------------+
40  *                      #                                 (xor) (xor)                    |                             #
41  *                      #                                   ^     ^                      |                             #
42  *                      #                                    \   /                       |                             #
43  *                      #                                   (offset)---------------------+                             #
44  *                      #                                                                                              #
45  *                      ################################################################################################
46  * \endverbatim
47  */
48
49  /* \verbatim
50  *                      ################################################################################################
51  *                      #                                                                                              #
52  *                      #         +---------------------------+                                                        #
53  *                      #         |                           |                             +---+                      #
54  *                      #         V                           |                             |   |                      #
55  *                      #      (concat)                       |                             |   V                      #
56  *  +---------------+   #    o---------o             (xor)+---------+      o---------o      | o----o     o---------o   #    +--------------+
57  *  | entropy Block | -----> | sha-256 | --(offset)-<     | rndCore | ---> | sha-256 | --+--+-| +1 |---> | sha-256 | -----> | random Block |
58  *  +---------------+   #    o---------o             (xor)+---------+      o---------o   |    o----o     o---------o   #    +--------------+
59  *                      #                                 (xor) (xor)                    |                             #
60  *                      #                                   ^     ^                      |                             #
61  *                      #                                    \   /                       |                             #
62  *                      #                                   (offset)---------------------+                             #
63  *                      #                                                                                              #
64  *                      ################################################################################################
65  * \endverbatim
66  */
67
68 #include <stdint.h>
69 #include <string.h>
70 #include "sha256.h"
71 #include "entropium.h"
72
73 /**
74  * \brief secret entropy pool. 
75  * This is the core of the random which is generated
76  */
77 uint32_t rndCore[16]; 
78
79 /*************************************************************************/
80
81 /**
82  * \brief This function adds entropy to the central entropy pool
83  * 
84  * @param length This ist the length of the random data in BITS. 
85  * @param data This is the random data which should be added to the entropy pool
86 */
87 /* idea is: hash the message and add it via xor to rndCore
88  *
89  * length in bits 
90  * 
91  * we simply first "hash" rndCore, then entropy.
92  */
93 void entropium_addEntropy(unsigned length_b, const void* data){
94         sha256_ctx_t s;
95         static uint8_t offset=0; /* selects if higher or lower half gets updated */
96         sha256_init(&s);
97         sha256_nextBlock(&s, rndCore);
98         while (length_b>=512){
99                 sha256_nextBlock(&s, data);
100                 data = (uint8_t*)data+ 512/8;
101                 length_b -= 512;        
102         }
103         sha256_lastBlock(&s, data, length_b);
104         uint8_t i;
105         for (i=0; i<8; ++i){
106                 rndCore[i+offset] ^= s.h[i];
107         }
108         offset ^= 8; /* hehe */
109 }
110
111 /*************************************************************************/
112 /**
113  * \brief This function fills a given buffer with 32 random bytes
114  * @param b Pointer to buffer wich is to fill
115  */
116 void entropium_getRandomBlock(void *b){
117         sha256_ctx_t s;
118         uint8_t offset=8;
119         
120         sha256_init(&s);
121         sha256_lastBlock(&s, rndCore, 512); /* remeber the byte order! */
122         uint8_t i;
123         for (i=0; i<8; ++i){
124                 rndCore[i+offset] ^= s.h[i];
125         }
126         offset ^= 8; /* hehe */
127         memcpy(b, s.h, 32); /* back up first hash in b */
128         ((uint8_t*)b)[*((uint8_t*)b)&31]++;     /* the important increment step */
129         sha256_init(&s);
130         sha256_lastBlock(&s, b, 256);
131         memcpy(b, s.h, 32);
132 }
133
134 /*************************************************************************/
135  
136 /**
137  * \brief This function simply returns a random byte
138  * @return a random byte
139  */
140 uint8_t entropium_getRandomByte(void){
141         static uint8_t block[32];
142         static uint8_t i=32;
143         
144         if (i==32){
145                 entropium_getRandomBlock((void*)block);
146                 i=0;
147         }       
148         return block[i++];
149 }
150
151 /*************************************************************************/
152  
153 /**
154  * \brief This function fills the given bock with length random bytes
155  * @return a random byte
156  */
157  
158 void entropium_fillBlockRandom(void* block, unsigned length_B){
159         while(length_B>ENTROPIUM_RANDOMBLOCK_SIZE){
160                 entropium_getRandomBlock(block);
161                 block = (uint8_t*)block + ENTROPIUM_RANDOMBLOCK_SIZE;
162                 length_B -= ENTROPIUM_RANDOMBLOCK_SIZE;
163         }
164         while(length_B){
165                 *((uint8_t*)block) = entropium_getRandomByte();
166                 block= (uint8_t*)block +1; --length_B;
167         }
168 }
169  
170