4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Yarrow implementation
34 * \author Giovanni Bajo <rasky@develer.com>
39 #include <sec/hash/sha1.h>
40 #include <cfg/macros.h>
43 #define CONFIG_YARROW_GENERATOR_GATE 10
45 static void yarrow_generate(PRNG *ctx_, uint8_t *data, size_t len)
47 YarrowContext *ctx = (YarrowContext *)ctx_;
48 BlockCipher *cipher = 0;
54 if (ctx->lastidx == sizeof(ctx->last))
58 cipher = AES128_stackinit();
59 ASSERT(sizeof(ctx->counter) == cipher_block_len(cipher));
60 ASSERT(sizeof(ctx->curkey) == cipher_key_len(cipher));
62 cipher_set_key(cipher, ctx->curkey);
63 cipher_ctr_begin(cipher, ctx->counter);
66 cipher_ctr_step(cipher, ctx->last);
68 if (ctx->curkey_gencount == CONFIG_YARROW_GENERATOR_GATE)
70 ASSERT(cipher_block_len(cipher) == cipher_key_len(cipher));
71 cipher_set_key(cipher, ctx->last);
72 ctx->curkey_gencount = 0;
77 ctx->curkey_gencount++;
80 int n = MIN(len, 16U-ctx->lastidx);
81 memcpy(data, ctx->last+ctx->lastidx, n);
88 static void yarrow_reseed(PRNG *ctx_, const uint8_t *seed)
90 YarrowContext *ctx = (YarrowContext *)ctx_;
91 Hash *h = SHA1_stackinit();
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));
98 // Reset the counter for the sequence
99 memset(ctx->counter, 0, sizeof(ctx->counter));
103 /*********************************************************************/
105 void yarrow_init(YarrowContext *ctx)
107 ctx->prng.reseed = yarrow_reseed;
108 ctx->prng.generate = yarrow_generate;
109 ctx->prng.seed_len = 16;
110 ctx->prng.seeded = 0;
112 ctx->lastidx = sizeof(ctx->last);
113 ctx->curkey_gencount = 0;
114 memset(ctx->curkey, 0, sizeof(ctx->curkey));
116 ASSERT(sizeof(ctx->counter) == sizeof(ctx->last));