3d9469fc85e1fb69b10d43c6513bb5c3481a9096
[bertos.git] / bertos / sec / prng / yarrow.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos 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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Yarrow implementation
34  * \author Giovanni Bajo <rasky@develer.com>
35  *
36  */
37
38 #include "yarrow.h"
39 #include <sec/hash/sha1.h>
40 #include <cfg/macros.h>
41 #include <string.h>
42
43 #define CONFIG_YARROW_GENERATOR_GATE             10
44
45 static void yarrow_generate(PRNG *ctx_, uint8_t *data, size_t len)
46 {
47         YarrowContext *ctx = (YarrowContext *)ctx_;
48         BlockCipher *cipher = 0;
49
50         ASSERT(len != 0);
51
52         do
53         {
54                 if (ctx->lastidx == sizeof(ctx->last))
55                 {
56                         if (!cipher)
57                         {
58                                 cipher = AES128_stackinit();
59                                 ASSERT(sizeof(ctx->counter) == cipher_block_len(cipher));
60                                 ASSERT(sizeof(ctx->curkey) == cipher_key_len(cipher));
61
62                                 cipher_set_key(cipher, ctx->curkey);
63                                 cipher_ctr_begin(cipher, ctx->counter);
64                         }
65
66                         cipher_ctr_step(cipher, ctx->last);
67
68                         if (ctx->curkey_gencount == CONFIG_YARROW_GENERATOR_GATE)
69                         {
70                                 ASSERT(cipher_block_len(cipher) == cipher_key_len(cipher));
71                                 cipher_set_key(cipher, ctx->last);
72                                 ctx->curkey_gencount = 0;
73                                 continue;
74                         }
75
76                         ctx->lastidx = 0;
77                         ctx->curkey_gencount++;
78                 }
79
80                 int n = MIN(len, 16U-ctx->lastidx);
81                 memcpy(data, ctx->last+ctx->lastidx, n);
82                 data += n;
83                 len -= n;
84                 ctx->lastidx += n;
85         } while (len);
86 }
87
88 static void yarrow_reseed(PRNG *ctx_, const uint8_t *seed)
89 {
90         YarrowContext *ctx = (YarrowContext *)ctx_;
91         Hash *h = SHA1_stackinit();
92
93         hash_begin(h);
94         hash_update(h, seed, ctx->prng.seed_len);
95         hash_update(h, ctx->curkey, sizeof(ctx->curkey));
96         memcpy(ctx->curkey, hash_final(h), sizeof(ctx->curkey));
97
98         // Reset the counter for the sequence
99         memset(ctx->counter, 0, sizeof(ctx->counter));
100 }
101
102
103 /*********************************************************************/
104
105 void yarrow_init(YarrowContext *ctx)
106 {
107         ctx->prng.reseed = yarrow_reseed;
108         ctx->prng.generate = yarrow_generate;
109         ctx->prng.seed_len = 16;
110         ctx->prng.seeded = 0;
111
112         ctx->lastidx = sizeof(ctx->last);
113         ctx->curkey_gencount = 0;
114         memset(ctx->curkey, 0, sizeof(ctx->curkey));
115
116         ASSERT(sizeof(ctx->counter) == sizeof(ctx->last));
117 }