| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
/* |
|
2
|
|
|
|
|
|
|
* Copyright (c) 2016 Thomas Pornin |
|
3
|
|
|
|
|
|
|
* |
|
4
|
|
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
|
5
|
|
|
|
|
|
|
* a copy of this software and associated documentation files (the |
|
6
|
|
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including |
|
7
|
|
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
|
8
|
|
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
|
9
|
|
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
|
10
|
|
|
|
|
|
|
* the following conditions: |
|
11
|
|
|
|
|
|
|
* |
|
12
|
|
|
|
|
|
|
* The above copyright notice and this permission notice shall be |
|
13
|
|
|
|
|
|
|
* included in all copies or substantial portions of the Software. |
|
14
|
|
|
|
|
|
|
* |
|
15
|
|
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
16
|
|
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
17
|
|
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
18
|
|
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
|
19
|
|
|
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
|
20
|
|
|
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
21
|
|
|
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22
|
|
|
|
|
|
|
* SOFTWARE. |
|
23
|
|
|
|
|
|
|
*/ |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
#include "inner.h" |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
/* see bearssl.h */ |
|
28
|
|
|
|
|
|
|
void |
|
29
|
6
|
|
|
|
|
|
br_hmac_drbg_init(br_hmac_drbg_context *ctx, |
|
30
|
|
|
|
|
|
|
const br_hash_class *digest_class, const void *seed, size_t len) |
|
31
|
|
|
|
|
|
|
{ |
|
32
|
|
|
|
|
|
|
size_t hlen; |
|
33
|
|
|
|
|
|
|
|
|
34
|
6
|
|
|
|
|
|
ctx->vtable = &br_hmac_drbg_vtable; |
|
35
|
6
|
|
|
|
|
|
hlen = br_digest_size(digest_class); |
|
36
|
6
|
|
|
|
|
|
memset(ctx->K, 0x00, hlen); |
|
37
|
6
|
|
|
|
|
|
memset(ctx->V, 0x01, hlen); |
|
38
|
6
|
|
|
|
|
|
ctx->digest_class = digest_class; |
|
39
|
6
|
|
|
|
|
|
br_hmac_drbg_update(ctx, seed, len); |
|
40
|
6
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
/* see bearssl.h */ |
|
43
|
|
|
|
|
|
|
void |
|
44
|
606
|
|
|
|
|
|
br_hmac_drbg_generate(br_hmac_drbg_context *ctx, void *out, size_t len) |
|
45
|
|
|
|
|
|
|
{ |
|
46
|
|
|
|
|
|
|
const br_hash_class *dig; |
|
47
|
|
|
|
|
|
|
br_hmac_key_context kc; |
|
48
|
|
|
|
|
|
|
br_hmac_context hc; |
|
49
|
|
|
|
|
|
|
size_t hlen; |
|
50
|
|
|
|
|
|
|
unsigned char *buf; |
|
51
|
|
|
|
|
|
|
unsigned char x; |
|
52
|
|
|
|
|
|
|
|
|
53
|
606
|
|
|
|
|
|
dig = ctx->digest_class; |
|
54
|
606
|
|
|
|
|
|
hlen = br_digest_size(dig); |
|
55
|
606
|
|
|
|
|
|
br_hmac_key_init(&kc, dig, ctx->K, hlen); |
|
56
|
606
|
|
|
|
|
|
buf = out; |
|
57
|
2400
|
100
|
|
|
|
|
while (len > 0) { |
|
58
|
|
|
|
|
|
|
size_t clen; |
|
59
|
|
|
|
|
|
|
|
|
60
|
1794
|
|
|
|
|
|
br_hmac_init(&hc, &kc, 0); |
|
61
|
1794
|
|
|
|
|
|
br_hmac_update(&hc, ctx->V, hlen); |
|
62
|
1794
|
|
|
|
|
|
br_hmac_out(&hc, ctx->V); |
|
63
|
1794
|
|
|
|
|
|
clen = hlen; |
|
64
|
1794
|
100
|
|
|
|
|
if (clen > len) { |
|
65
|
598
|
|
|
|
|
|
clen = len; |
|
66
|
|
|
|
|
|
|
} |
|
67
|
1794
|
|
|
|
|
|
memcpy(buf, ctx->V, clen); |
|
68
|
1794
|
|
|
|
|
|
buf += clen; |
|
69
|
1794
|
|
|
|
|
|
len -= clen; |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
/* |
|
73
|
|
|
|
|
|
|
* To prepare the state for the next request, we should call |
|
74
|
|
|
|
|
|
|
* br_hmac_drbg_update() with an empty additional seed. However, |
|
75
|
|
|
|
|
|
|
* we already have an initialized HMAC context with the right |
|
76
|
|
|
|
|
|
|
* initial key, and we don't want to push another one on the |
|
77
|
|
|
|
|
|
|
* stack, so we inline that update() call here. |
|
78
|
|
|
|
|
|
|
*/ |
|
79
|
606
|
|
|
|
|
|
br_hmac_init(&hc, &kc, 0); |
|
80
|
606
|
|
|
|
|
|
br_hmac_update(&hc, ctx->V, hlen); |
|
81
|
606
|
|
|
|
|
|
x = 0x00; |
|
82
|
606
|
|
|
|
|
|
br_hmac_update(&hc, &x, 1); |
|
83
|
606
|
|
|
|
|
|
br_hmac_out(&hc, ctx->K); |
|
84
|
606
|
|
|
|
|
|
br_hmac_key_init(&kc, dig, ctx->K, hlen); |
|
85
|
606
|
|
|
|
|
|
br_hmac_init(&hc, &kc, 0); |
|
86
|
606
|
|
|
|
|
|
br_hmac_update(&hc, ctx->V, hlen); |
|
87
|
606
|
|
|
|
|
|
br_hmac_out(&hc, ctx->V); |
|
88
|
606
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
/* see bearssl.h */ |
|
91
|
|
|
|
|
|
|
void |
|
92
|
11
|
|
|
|
|
|
br_hmac_drbg_update(br_hmac_drbg_context *ctx, const void *seed, size_t len) |
|
93
|
|
|
|
|
|
|
{ |
|
94
|
|
|
|
|
|
|
const br_hash_class *dig; |
|
95
|
|
|
|
|
|
|
br_hmac_key_context kc; |
|
96
|
|
|
|
|
|
|
br_hmac_context hc; |
|
97
|
|
|
|
|
|
|
size_t hlen; |
|
98
|
|
|
|
|
|
|
unsigned char x; |
|
99
|
|
|
|
|
|
|
|
|
100
|
11
|
|
|
|
|
|
dig = ctx->digest_class; |
|
101
|
11
|
|
|
|
|
|
hlen = br_digest_size(dig); |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
/* |
|
104
|
|
|
|
|
|
|
* 1. K = HMAC(K, V || 0x00 || seed) |
|
105
|
|
|
|
|
|
|
*/ |
|
106
|
11
|
|
|
|
|
|
br_hmac_key_init(&kc, dig, ctx->K, hlen); |
|
107
|
11
|
|
|
|
|
|
br_hmac_init(&hc, &kc, 0); |
|
108
|
11
|
|
|
|
|
|
br_hmac_update(&hc, ctx->V, hlen); |
|
109
|
11
|
|
|
|
|
|
x = 0x00; |
|
110
|
11
|
|
|
|
|
|
br_hmac_update(&hc, &x, 1); |
|
111
|
11
|
|
|
|
|
|
br_hmac_update(&hc, seed, len); |
|
112
|
11
|
|
|
|
|
|
br_hmac_out(&hc, ctx->K); |
|
113
|
11
|
|
|
|
|
|
br_hmac_key_init(&kc, dig, ctx->K, hlen); |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
/* |
|
116
|
|
|
|
|
|
|
* 2. V = HMAC(K, V) |
|
117
|
|
|
|
|
|
|
*/ |
|
118
|
11
|
|
|
|
|
|
br_hmac_init(&hc, &kc, 0); |
|
119
|
11
|
|
|
|
|
|
br_hmac_update(&hc, ctx->V, hlen); |
|
120
|
11
|
|
|
|
|
|
br_hmac_out(&hc, ctx->V); |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
/* |
|
123
|
|
|
|
|
|
|
* 3. If the additional seed is empty, then stop here. |
|
124
|
|
|
|
|
|
|
*/ |
|
125
|
11
|
100
|
|
|
|
|
if (len == 0) { |
|
126
|
3
|
|
|
|
|
|
return; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
/* |
|
130
|
|
|
|
|
|
|
* 4. K = HMAC(K, V || 0x01 || seed) |
|
131
|
|
|
|
|
|
|
*/ |
|
132
|
8
|
|
|
|
|
|
br_hmac_init(&hc, &kc, 0); |
|
133
|
8
|
|
|
|
|
|
br_hmac_update(&hc, ctx->V, hlen); |
|
134
|
8
|
|
|
|
|
|
x = 0x01; |
|
135
|
8
|
|
|
|
|
|
br_hmac_update(&hc, &x, 1); |
|
136
|
8
|
|
|
|
|
|
br_hmac_update(&hc, seed, len); |
|
137
|
8
|
|
|
|
|
|
br_hmac_out(&hc, ctx->K); |
|
138
|
8
|
|
|
|
|
|
br_hmac_key_init(&kc, dig, ctx->K, hlen); |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
/* |
|
141
|
|
|
|
|
|
|
* 5. V = HMAC(K, V) |
|
142
|
|
|
|
|
|
|
*/ |
|
143
|
8
|
|
|
|
|
|
br_hmac_init(&hc, &kc, 0); |
|
144
|
8
|
|
|
|
|
|
br_hmac_update(&hc, ctx->V, hlen); |
|
145
|
8
|
|
|
|
|
|
br_hmac_out(&hc, ctx->V); |
|
146
|
|
|
|
|
|
|
} |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
/* see bearssl.h */ |
|
149
|
|
|
|
|
|
|
const br_prng_class br_hmac_drbg_vtable = { |
|
150
|
|
|
|
|
|
|
sizeof(br_hmac_drbg_context), |
|
151
|
|
|
|
|
|
|
(void (*)(const br_prng_class **, const void *, const void *, size_t)) |
|
152
|
|
|
|
|
|
|
&br_hmac_drbg_init, |
|
153
|
|
|
|
|
|
|
(void (*)(const br_prng_class **, void *, size_t)) |
|
154
|
|
|
|
|
|
|
&br_hmac_drbg_generate, |
|
155
|
|
|
|
|
|
|
(void (*)(const br_prng_class **, const void *, size_t)) |
|
156
|
|
|
|
|
|
|
&br_hmac_drbg_update |
|
157
|
|
|
|
|
|
|
}; |