Select one of the symbols to view example projects that use it.
 
Outline
#include <tinycrypt/hmac_prng.h>
#include <tinycrypt/hmac.h>
#include <tinycrypt/constants.h>
#include <tinycrypt/utils.h>
MIN_SLEN
MAX_SLEN
MAX_PLEN
MAX_ALEN
MAX_GENS
MAX_OUT
update(TCHmacPrng_t, const uint8_t *, unsigned int, const uint8_t *, unsigned int)
tc_hmac_prng_init(TCHmacPrng_t, const uint8_t *, unsigned int)
tc_hmac_prng_reseed(TCHmacPrng_t, const uint8_t *, unsigned int, const uint8_t *, unsigned int)
tc_hmac_prng_generate(uint8_t *, unsigned int, TCHmacPrng_t)
Files
ESP-IDF
components
app_trace
app_update
bootloader_support
bt
common
controller
esp_ble_mesh
api
btc
common
include
tinycrypt
include
src
core
lib
models
v1.1
host
include
porting
cmock
console
cxx
driver
efuse
esp_adc
esp_app_format
esp_bootloader_format
esp_coex
esp_common
esp_driver_ana_cmpr
esp_driver_cam
esp_driver_dac
esp_driver_gpio
esp_driver_gptimer
esp_driver_i2c
esp_driver_i2s
esp_driver_jpeg
esp_driver_ledc
esp_driver_mcpwm
esp_driver_parlio
esp_driver_pcnt
esp_driver_rmt
esp_driver_sdio
esp_driver_sdm
esp_driver_sdmmc
esp_driver_sdspi
esp_driver_spi
esp_driver_tsens
esp_driver_uart
esp_driver_usb_serial_jtag
esp_eth
esp_event
esp_gdbstub
esp_hid
esp_http_client
esp_http_server
esp_https_ota
esp_https_server
esp_hw_support
esp_lcd
esp_local_ctrl
esp_mm
esp_netif
esp_partition
esp_phy
esp_pm
esp_psram
esp_ringbuf
esp_rom
esp_security
esp_system
esp_timer
esp_vfs_console
esp_wifi
esp-tls
espcoredump
hal
heap
http_parser
ieee802154
log
mqtt
newlib
nvs_flash
nvs_sec_provider
openthread
perfmon
protobuf-c
protocomm
pthread
rt
sdmmc
soc
spi_flash
spiffs
tcp_transport
ulp
unity
vfs
wear_levelling
wifi_provisioning
wpa_supplicant
xtensa
examples
lwIP
FreeRTOS
cJSON
mbedTLS
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/bt/esp_ble_mesh/common/tinycrypt/src/hmac_prng.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* hmac_prng.c - TinyCrypt implementation of HMAC-PRNG */ /* * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Intel Corporation nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. *//* ... */ #include <tinycrypt/hmac_prng.h> #include <tinycrypt/hmac.h> #include <tinycrypt/constants.h> #include <tinycrypt/utils.h> /* * min bytes in the seed string. * MIN_SLEN*8 must be at least the expected security level. *//* ... */ static const unsigned int MIN_SLEN = 32; /* * max bytes in the seed string; * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes). *//* ... */ static const unsigned int MAX_SLEN = UINT32_MAX; /* * max bytes in the personalization string; * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes). *//* ... */ static const unsigned int MAX_PLEN = UINT32_MAX; /* * max bytes in the additional_info string; * SP800-90A specifies a maximum of 2^35 bits (i.e., 2^32 bytes). *//* ... */ static const unsigned int MAX_ALEN = UINT32_MAX; /* * max number of generates between re-seeds; * TinyCrypt accepts up to (2^32 - 1) which is the maximal value of * a 32-bit unsigned int variable, while SP800-90A specifies a maximum of 2^48. *//* ... */ static const unsigned int MAX_GENS = UINT32_MAX; /* * maximum bytes per generate call; * SP800-90A specifies a maximum up to 2^19. *//* ... */ static const unsigned int MAX_OUT = (1 << 19); /* * Assumes: prng != NULL *//* ... */ static void update(TCHmacPrng_t prng, const uint8_t *data, unsigned int datalen, const uint8_t *additional_data, unsigned int additional_datalen) { const uint8_t separator0 = 0x00; const uint8_t separator1 = 0x01; /* configure the new prng key into the prng's instance of hmac */ tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key)); /* use current state, e and separator 0 to compute a new prng key: */ (void)tc_hmac_init(&prng->h); (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v)); (void)tc_hmac_update(&prng->h, &separator0, sizeof(separator0)); if (data && datalen) { (void)tc_hmac_update(&prng->h, data, datalen); }{...} if (additional_data && additional_datalen) { (void)tc_hmac_update(&prng->h, additional_data, additional_datalen); }{...} (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h); /* configure the new prng key into the prng's instance of hmac */ (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key)); /* use the new key to compute a new state variable v */ (void)tc_hmac_init(&prng->h); (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v)); (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h); if (data == 0 || datalen == 0) { return; }{...} /* configure the new prng key into the prng's instance of hmac */ tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key)); /* use current state, e and separator 1 to compute a new prng key: */ (void)tc_hmac_init(&prng->h); (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v)); (void)tc_hmac_update(&prng->h, &separator1, sizeof(separator1)); (void)tc_hmac_update(&prng->h, data, datalen); if (additional_data && additional_datalen) { (void)tc_hmac_update(&prng->h, additional_data, additional_datalen); }{...} (void)tc_hmac_final(prng->key, sizeof(prng->key), &prng->h); /* configure the new prng key into the prng's instance of hmac */ (void)tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key)); /* use the new key to compute a new state variable v */ (void)tc_hmac_init(&prng->h); (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v)); (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h); }{ ... } int tc_hmac_prng_init(TCHmacPrng_t prng, const uint8_t *personalization, unsigned int plen) { /* input sanity check: */ if (prng == (TCHmacPrng_t) 0 || personalization == (uint8_t *) 0 || plen > MAX_PLEN) { return TC_CRYPTO_FAIL; }{...} /* put the generator into a known state: */ _set(prng->key, 0x00, sizeof(prng->key)); _set(prng->v, 0x01, sizeof(prng->v)); update(prng, personalization, plen, 0, 0); /* force a reseed before allowing tc_hmac_prng_generate to succeed: */ prng->countdown = 0; return TC_CRYPTO_SUCCESS; }{ ... } int tc_hmac_prng_reseed(TCHmacPrng_t prng, const uint8_t *seed, unsigned int seedlen, const uint8_t *additional_input, unsigned int additionallen) { /* input sanity check: */ if (prng == (TCHmacPrng_t) 0 || seed == (const uint8_t *) 0 || seedlen < MIN_SLEN || seedlen > MAX_SLEN) { return TC_CRYPTO_FAIL; }{...} if (additional_input != (const uint8_t *) 0) { /* * Abort if additional_input is provided but has inappropriate * length *//* ... */ if (additionallen == 0 || additionallen > MAX_ALEN) { return TC_CRYPTO_FAIL; }{...} else { /* call update for the seed and additional_input */ update(prng, seed, seedlen, additional_input, additionallen); }{...} }{...} else { /* call update only for the seed */ update(prng, seed, seedlen, 0, 0); }{...} /* ... and enable hmac_prng_generate */ prng->countdown = MAX_GENS; return TC_CRYPTO_SUCCESS; }{ ... } int tc_hmac_prng_generate(uint8_t *out, unsigned int outlen, TCHmacPrng_t prng) { unsigned int bufferlen; /* input sanity check: */ if (out == (uint8_t *) 0 || prng == (TCHmacPrng_t) 0 || outlen == 0 || outlen > MAX_OUT) { return TC_CRYPTO_FAIL; }{...} else if (prng->countdown == 0) { return TC_HMAC_PRNG_RESEED_REQ; }{...} prng->countdown--; while (outlen != 0) { /* configure the new prng key into the prng's instance of hmac */ tc_hmac_set_key(&prng->h, prng->key, sizeof(prng->key)); /* operate HMAC in OFB mode to create "random" outputs */ (void)tc_hmac_init(&prng->h); (void)tc_hmac_update(&prng->h, prng->v, sizeof(prng->v)); (void)tc_hmac_final(prng->v, sizeof(prng->v), &prng->h); bufferlen = (TC_SHA256_DIGEST_SIZE > outlen) ? outlen : TC_SHA256_DIGEST_SIZE; (void)_copy(out, bufferlen, prng->v, bufferlen); out += bufferlen; outlen = (outlen > TC_SHA256_DIGEST_SIZE) ? (outlen - TC_SHA256_DIGEST_SIZE) : 0; }{...} /* block future PRNG compromises from revealing past state */ update(prng, 0, 0, 0, 0); return TC_CRYPTO_SUCCESS; }{ ... }
Details