blob: ceac27f69d5ba79a08b19ef2a0accb09a5d2d27d [file] [log] [blame]
David Brownfecda2d2017-09-07 10:20:34 -06001/* hmac_prng.c - TinyCrypt implementation of HMAC-PRNG */
2
3/*
4 * Copyright (C) 2015 by Intel Corporation, All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * - Neither the name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <tinycrypt/hmac_prng.h>
34#include <tinycrypt/hmac.h>
35#include <tinycrypt/constants.h>
36#include <tinycrypt/utils.h>
37
38/*
39 * min bytes in the seed string.
40 * MIN_SLEN*8 must be at least the expected security level.
41 */
42static const uint32_t MIN_SLEN = 32;
43
44/*
45 * max bytes in the seed string;
46 * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
47 */
48static const uint32_t MAX_SLEN = UINT32_MAX;
49
50/*
51 * max bytes in the personalization string;
52 * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
53 */
54static const uint32_t MAX_PLEN = UINT32_MAX;
55
56/*
57 * max bytes in the additional_info string;
58 * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes).
59 */
60static const uint32_t MAX_ALEN = UINT32_MAX;
61
62/*
63 * max number of generates between re-seeds;
64 * TinyCrypt accepts up to (2^32 - 1) which is the maximal value of
65 * a uint32_t variable, while SP800-90A specifies a maximum of 2^48.
66 */
67static const uint32_t MAX_GENS = UINT32_MAX;
68
69/*
70 * maximum bytes per generate call;
71 * SP800-90A specifies a maximum up to 2^19.
72 */
73static const uint32_t MAX_OUT = (1 << 19);
74
75/*
76 * Assumes: prng != NULL, e != NULL, len >= 0.
77 */
78static void update(TCHmacPrng_t prng, const uint8_t *e, uint32_t len)
79{
80 const uint8_t separator0 = 0x00;
81 const uint8_t separator1 = 0x01;
82
83 /* use current state, e and separator 0 to compute a new prng key: */
84 (void)tc_hmac_init(&prng->h);
85 (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
86 (void)tc_hmac_update(&prng->h, &separator0, sizeof(separator0));
87 (void)tc_hmac_update(&prng->h, e, len);
88 (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
89 /* configure the new prng key into the prng's instance of hmac */
90 (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
91
92 /* use the new key to compute a new state variable v */
93 (void)tc_hmac_init(&prng->h);
94 (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
95 (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
96
97 /* use current state, e and separator 1 to compute a new prng key: */
98 (void)tc_hmac_init(&prng->h);
99 (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
100 (void)tc_hmac_update(&prng->h, &separator1, sizeof(separator1));
101 (void)tc_hmac_update(&prng->h, e, len);
102 (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h);
103 /* configure the new prng key into the prng's instance of hmac */
104 (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
105
106 /* use the new key to compute a new state variable v */
107 (void)tc_hmac_init(&prng->h);
108 (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
109 (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
110}
111
112int32_t tc_hmac_prng_init(TCHmacPrng_t prng,
113 const uint8_t *personalization,
114 uint32_t plen)
115{
116 /* input sanity check: */
117 if (prng == (TCHmacPrng_t) 0 ||
118 personalization == (uint8_t *) 0 ||
119 plen > MAX_PLEN) {
120 return TC_CRYPTO_FAIL;
121 }
122
123 /* put the generator into a known state: */
124 _set(prng->key, 0x00, sizeof(prng->key));
125 _set(prng->v, 0x01, sizeof(prng->v));
126 tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key));
127 /* update assumes SOME key has been configured into HMAC */
128
129 update(prng, personalization, plen);
130
131 /* force a reseed before allowing tc_hmac_prng_generate to succeed: */
132 prng->countdown = 0;
133
134 return TC_CRYPTO_SUCCESS;
135}
136
137int32_t tc_hmac_prng_reseed(TCHmacPrng_t prng,
138 const uint8_t *seed,
139 uint32_t seedlen,
140 const uint8_t *additional_input,
141 uint32_t additionallen)
142{
143 /* input sanity check: */
144 if (prng == (TCHmacPrng_t) 0 ||
145 seed == (const uint8_t *) 0 ||
146 seedlen < MIN_SLEN ||
147 seedlen > MAX_SLEN) {
148 return TC_CRYPTO_FAIL;
149 }
150
151 if (additional_input != (const uint8_t *) 0) {
152 /*
153 * Abort if additional_input is provided but has inappropriate
154 * length
155 */
156 if (additionallen == 0 ||
157 additionallen > MAX_ALEN) {
158 return TC_CRYPTO_FAIL;
159 } else {
160 /* call update for the seed and additional_input */
161 update(prng, seed, seedlen);
162 update(prng, additional_input, additionallen);
163 }
164 } else {
165 /* call update only for the seed */
166 update(prng, seed, seedlen);
167 }
168
169 /* ... and enable hmac_prng_generate */
170 prng->countdown = MAX_GENS;
171
172 return TC_CRYPTO_SUCCESS;
173}
174
175int32_t tc_hmac_prng_generate(uint8_t *out, uint32_t outlen, TCHmacPrng_t prng)
176{
177 uint32_t bufferlen;
178
179 /* input sanity check: */
180 if (out == (uint8_t *) 0 ||
181 prng == (TCHmacPrng_t) 0 ||
182 outlen == 0 ||
183 outlen > MAX_OUT) {
184 return TC_CRYPTO_FAIL;
185 } else if (prng->countdown == 0) {
186 return TC_HMAC_PRNG_RESEED_REQ;
187 }
188
189 prng->countdown--;
190
191 while (outlen != 0) {
192 /* operate HMAC in OFB mode to create "random" outputs */
193 (void)tc_hmac_init(&prng->h);
194 (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v));
195 (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h);
196
197 bufferlen = (TC_SHA256_DIGEST_SIZE > outlen) ?
198 outlen : TC_SHA256_DIGEST_SIZE;
199 (void)_copy(out, bufferlen, prng->v, bufferlen);
200
201 out += bufferlen;
202 outlen = (outlen > TC_SHA256_DIGEST_SIZE) ?
203 (outlen - TC_SHA256_DIGEST_SIZE) : 0;
204 }
205
206 /* block future PRNG compromises from revealing past state */
207 update(prng, prng->v, TC_SHA256_DIGEST_SIZE);
208
209 return TC_CRYPTO_SUCCESS;
210}