]>
Commit | Line | Data |
---|---|---|
12fb8c3d | 1 | /* |
48e5119a | 2 | * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved. |
12fb8c3d RS |
3 | * |
4 | * Licensed under the OpenSSL license (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include <string.h> | |
176db6dc | 11 | #include "internal/nelem.h" |
12fb8c3d RS |
12 | #include <openssl/crypto.h> |
13 | #include <openssl/err.h> | |
14 | #include <openssl/rand.h> | |
15 | #include <openssl/obj_mac.h> | |
16 | #include <openssl/evp.h> | |
17 | #include <openssl/aes.h> | |
18 | #include "../crypto/rand/rand_lcl.h" | |
19 | ||
20 | #include "testutil.h" | |
21 | #include "drbgtest.h" | |
22 | ||
23 | typedef struct drbg_selftest_data_st { | |
24 | int post; | |
25 | int nid; | |
26 | unsigned int flags; | |
27 | ||
28 | /* KAT data for no PR */ | |
aa048aef DMSP |
29 | const unsigned char *entropy; |
30 | size_t entropylen; | |
12fb8c3d RS |
31 | const unsigned char *nonce; |
32 | size_t noncelen; | |
33 | const unsigned char *pers; | |
34 | size_t perslen; | |
35 | const unsigned char *adin; | |
36 | size_t adinlen; | |
aa048aef DMSP |
37 | const unsigned char *entropyreseed; |
38 | size_t entropyreseedlen; | |
12fb8c3d RS |
39 | const unsigned char *adinreseed; |
40 | size_t adinreseedlen; | |
41 | const unsigned char *adin2; | |
42 | size_t adin2len; | |
43 | const unsigned char *expected; | |
44 | size_t exlen; | |
45 | const unsigned char *kat2; | |
46 | size_t kat2len; | |
47 | ||
48 | /* KAT data for PR */ | |
aa048aef DMSP |
49 | const unsigned char *entropy_pr; |
50 | size_t entropylen_pr; | |
12fb8c3d RS |
51 | const unsigned char *nonce_pr; |
52 | size_t noncelen_pr; | |
53 | const unsigned char *pers_pr; | |
54 | size_t perslen_pr; | |
55 | const unsigned char *adin_pr; | |
56 | size_t adinlen_pr; | |
aa048aef DMSP |
57 | const unsigned char *entropypr_pr; |
58 | size_t entropyprlen_pr; | |
12fb8c3d RS |
59 | const unsigned char *ading_pr; |
60 | size_t adinglen_pr; | |
aa048aef DMSP |
61 | const unsigned char *entropyg_pr; |
62 | size_t entropyglen_pr; | |
12fb8c3d RS |
63 | const unsigned char *kat_pr; |
64 | size_t katlen_pr; | |
65 | const unsigned char *kat2_pr; | |
66 | size_t kat2len_pr; | |
67 | } DRBG_SELFTEST_DATA; | |
68 | ||
69 | #define make_drbg_test_data(nid, flag, pr, post) {\ | |
70 | post, nid, flag, \ | |
71 | pr##_entropyinput, sizeof(pr##_entropyinput), \ | |
72 | pr##_nonce, sizeof(pr##_nonce), \ | |
73 | pr##_personalizationstring, sizeof(pr##_personalizationstring), \ | |
74 | pr##_additionalinput, sizeof(pr##_additionalinput), \ | |
75 | pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \ | |
76 | pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \ | |
77 | pr##_additionalinput2, sizeof(pr##_additionalinput2), \ | |
78 | pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \ | |
79 | pr##_returnedbits, sizeof(pr##_returnedbits), \ | |
80 | pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \ | |
81 | pr##_pr_nonce, sizeof(pr##_pr_nonce), \ | |
82 | pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \ | |
83 | pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \ | |
84 | pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \ | |
85 | pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \ | |
86 | pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \ | |
87 | pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \ | |
88 | pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \ | |
89 | } | |
90 | ||
91 | #define make_drbg_test_data_df(nid, pr, p) \ | |
92 | make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_USE_DF, pr, p) | |
93 | ||
94 | static DRBG_SELFTEST_DATA drbg_test[] = { | |
12fb8c3d RS |
95 | make_drbg_test_data (NID_aes_128_ctr, 0, aes_128_no_df, 0), |
96 | make_drbg_test_data (NID_aes_192_ctr, 0, aes_192_no_df, 0), | |
97 | make_drbg_test_data (NID_aes_256_ctr, 0, aes_256_no_df, 1), | |
75e2c877 RS |
98 | make_drbg_test_data_df(NID_aes_128_ctr, aes_128_use_df, 0), |
99 | make_drbg_test_data_df(NID_aes_192_ctr, aes_192_use_df, 0), | |
100 | make_drbg_test_data_df(NID_aes_256_ctr, aes_256_use_df, 1), | |
12fb8c3d RS |
101 | }; |
102 | ||
103 | static int app_data_index; | |
104 | ||
105 | /* | |
75e2c877 | 106 | * Test context data, attached as EXDATA to the RAND_DRBG |
12fb8c3d RS |
107 | */ |
108 | typedef struct test_ctx_st { | |
aa048aef DMSP |
109 | const unsigned char *entropy; |
110 | size_t entropylen; | |
111 | int entropycnt; | |
12fb8c3d RS |
112 | const unsigned char *nonce; |
113 | size_t noncelen; | |
114 | int noncecnt; | |
115 | } TEST_CTX; | |
116 | ||
75e2c877 | 117 | static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout, |
12fb8c3d RS |
118 | int entropy, size_t min_len, size_t max_len) |
119 | { | |
75e2c877 | 120 | TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index); |
12fb8c3d | 121 | |
aa048aef DMSP |
122 | t->entropycnt++; |
123 | *pout = (unsigned char *)t->entropy; | |
124 | return t->entropylen; | |
12fb8c3d RS |
125 | } |
126 | ||
75e2c877 | 127 | static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout, |
12fb8c3d RS |
128 | int entropy, size_t min_len, size_t max_len) |
129 | { | |
75e2c877 | 130 | TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index); |
12fb8c3d RS |
131 | |
132 | t->noncecnt++; | |
133 | *pout = (unsigned char *)t->nonce; | |
134 | return t->noncelen; | |
135 | } | |
136 | ||
75e2c877 | 137 | static int uninstantiate(RAND_DRBG *drbg) |
12fb8c3d | 138 | { |
75e2c877 | 139 | int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg); |
12fb8c3d RS |
140 | |
141 | ERR_clear_error(); | |
142 | return ret; | |
143 | } | |
144 | ||
145 | /* | |
146 | * Do a single KAT test. Return 0 on failure. | |
147 | */ | |
148 | static int single_kat(DRBG_SELFTEST_DATA *td) | |
149 | { | |
75e2c877 | 150 | RAND_DRBG *drbg = NULL; |
12fb8c3d RS |
151 | TEST_CTX t; |
152 | int failures = 0; | |
153 | unsigned char buff[1024]; | |
154 | ||
155 | /* | |
156 | * Test without PR: Instantiate DRBG with test entropy, nonce and | |
157 | * personalisation string. | |
158 | */ | |
75e2c877 | 159 | if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL))) |
12fb8c3d | 160 | return 0; |
75e2c877 | 161 | if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL, |
12fb8c3d RS |
162 | kat_nonce, NULL))) { |
163 | failures++; | |
164 | goto err; | |
165 | } | |
166 | memset(&t, 0, sizeof(t)); | |
aa048aef DMSP |
167 | t.entropy = td->entropy; |
168 | t.entropylen = td->entropylen; | |
12fb8c3d RS |
169 | t.nonce = td->nonce; |
170 | t.noncelen = td->noncelen; | |
75e2c877 | 171 | RAND_DRBG_set_ex_data(drbg, app_data_index, &t); |
12fb8c3d | 172 | |
75e2c877 RS |
173 | if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)) |
174 | || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0, | |
12fb8c3d RS |
175 | td->adin, td->adinlen)) |
176 | || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen)) | |
177 | failures++; | |
178 | ||
179 | /* Reseed DRBG with test entropy and additional input */ | |
aa048aef DMSP |
180 | t.entropy = td->entropyreseed; |
181 | t.entropylen = td->entropyreseedlen; | |
75e2c877 RS |
182 | if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen) |
183 | || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0, | |
12fb8c3d RS |
184 | td->adin2, td->adin2len)) |
185 | || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len))) | |
186 | failures++; | |
75e2c877 | 187 | uninstantiate(drbg); |
12fb8c3d RS |
188 | |
189 | /* | |
190 | * Now test with PR: Instantiate DRBG with test entropy, nonce and | |
191 | * personalisation string. | |
192 | */ | |
75e2c877 RS |
193 | if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags)) |
194 | || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL, | |
12fb8c3d RS |
195 | kat_nonce, NULL))) |
196 | failures++; | |
75e2c877 | 197 | RAND_DRBG_set_ex_data(drbg, app_data_index, &t); |
aa048aef DMSP |
198 | t.entropy = td->entropy_pr; |
199 | t.entropylen = td->entropylen_pr; | |
12fb8c3d RS |
200 | t.nonce = td->nonce_pr; |
201 | t.noncelen = td->noncelen_pr; | |
aa048aef | 202 | t.entropycnt = 0; |
12fb8c3d | 203 | t.noncecnt = 0; |
75e2c877 | 204 | if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr))) |
12fb8c3d RS |
205 | failures++; |
206 | ||
207 | /* | |
208 | * Now generate with PR: we need to supply entropy as this will | |
209 | * perform a reseed operation. | |
210 | */ | |
aa048aef DMSP |
211 | t.entropy = td->entropypr_pr; |
212 | t.entropylen = td->entropyprlen_pr; | |
75e2c877 | 213 | if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1, |
12fb8c3d RS |
214 | td->adin_pr, td->adinlen_pr)) |
215 | || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr)) | |
216 | failures++; | |
217 | ||
218 | /* | |
219 | * Now generate again with PR: supply new entropy again. | |
220 | */ | |
aa048aef DMSP |
221 | t.entropy = td->entropyg_pr; |
222 | t.entropylen = td->entropyglen_pr; | |
12fb8c3d | 223 | |
75e2c877 | 224 | if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1, |
12fb8c3d RS |
225 | td->ading_pr, td->adinglen_pr)) |
226 | || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr, | |
227 | buff, td->kat2len_pr)) | |
228 | failures++; | |
229 | ||
230 | err: | |
75e2c877 RS |
231 | uninstantiate(drbg); |
232 | RAND_DRBG_free(drbg); | |
12fb8c3d RS |
233 | return failures == 0; |
234 | } | |
235 | ||
236 | /* | |
237 | * Initialise a DRBG based on selftest data | |
238 | */ | |
75e2c877 | 239 | static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t) |
12fb8c3d | 240 | { |
75e2c877 RS |
241 | if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags)) |
242 | || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL, | |
12fb8c3d RS |
243 | kat_nonce, NULL))) |
244 | return 0; | |
75e2c877 | 245 | RAND_DRBG_set_ex_data(drbg, app_data_index, t); |
aa048aef DMSP |
246 | t->entropy = td->entropy; |
247 | t->entropylen = td->entropylen; | |
12fb8c3d RS |
248 | t->nonce = td->nonce; |
249 | t->noncelen = td->noncelen; | |
aa048aef | 250 | t->entropycnt = 0; |
12fb8c3d RS |
251 | t->noncecnt = 0; |
252 | return 1; | |
253 | } | |
254 | ||
255 | /* | |
256 | * Initialise and instantiate DRBG based on selftest data | |
257 | */ | |
75e2c877 | 258 | static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, |
12fb8c3d RS |
259 | TEST_CTX *t) |
260 | { | |
75e2c877 RS |
261 | if (!TEST_true(init(drbg, td, t)) |
262 | || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))) | |
12fb8c3d RS |
263 | return 0; |
264 | return 1; | |
265 | } | |
266 | ||
267 | /* | |
268 | * Perform extensive error checking as required by SP800-90. | |
269 | * Induce several failure modes and check an error condition is set. | |
270 | */ | |
271 | static int error_check(DRBG_SELFTEST_DATA *td) | |
272 | { | |
75e2c877 RS |
273 | static char zero[sizeof(RAND_DRBG)]; |
274 | RAND_DRBG *drbg = NULL; | |
12fb8c3d RS |
275 | TEST_CTX t; |
276 | unsigned char buff[1024]; | |
a93ba405 | 277 | unsigned int generate_counter_tmp; |
12fb8c3d RS |
278 | int ret = 0; |
279 | ||
75e2c877 | 280 | if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL))) |
12fb8c3d RS |
281 | goto err; |
282 | ||
283 | /* | |
284 | * Personalisation string tests | |
285 | */ | |
286 | ||
287 | /* Test detection of too large personlisation string */ | |
75e2c877 | 288 | if (!init(drbg, td, &t) |
aa048aef | 289 | || RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1) > 0) |
12fb8c3d RS |
290 | goto err; |
291 | ||
292 | /* | |
293 | * Entropy source tests | |
294 | */ | |
295 | ||
296 | /* Test entropy source failure detecion: i.e. returns no data */ | |
aa048aef | 297 | t.entropylen = 0; |
75e2c877 | 298 | if (TEST_int_le(RAND_DRBG_instantiate(drbg, td->pers, td->perslen), 0)) |
12fb8c3d RS |
299 | goto err; |
300 | ||
301 | /* Try to generate output from uninstantiated DRBG */ | |
75e2c877 | 302 | if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0, |
12fb8c3d | 303 | td->adin, td->adinlen)) |
75e2c877 | 304 | || !uninstantiate(drbg)) |
12fb8c3d RS |
305 | goto err; |
306 | ||
307 | /* Test insufficient entropy */ | |
aa048aef | 308 | t.entropylen = drbg->min_entropylen - 1; |
75e2c877 RS |
309 | if (!init(drbg, td, &t) |
310 | || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0 | |
311 | || !uninstantiate(drbg)) | |
12fb8c3d RS |
312 | goto err; |
313 | ||
314 | /* Test too much entropy */ | |
aa048aef | 315 | t.entropylen = drbg->max_entropylen + 1; |
75e2c877 RS |
316 | if (!init(drbg, td, &t) |
317 | || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0 | |
318 | || !uninstantiate(drbg)) | |
12fb8c3d RS |
319 | goto err; |
320 | ||
321 | /* | |
322 | * Nonce tests | |
323 | */ | |
324 | ||
325 | /* Test too small nonce */ | |
aa048aef DMSP |
326 | if (drbg->min_noncelen) { |
327 | t.noncelen = drbg->min_noncelen - 1; | |
75e2c877 RS |
328 | if (!init(drbg, td, &t) |
329 | || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0 | |
330 | || !uninstantiate(drbg)) | |
12fb8c3d RS |
331 | goto err; |
332 | } | |
333 | ||
334 | /* Test too large nonce */ | |
aa048aef DMSP |
335 | if (drbg->max_noncelen) { |
336 | t.noncelen = drbg->max_noncelen + 1; | |
75e2c877 RS |
337 | if (!init(drbg, td, &t) |
338 | || RAND_DRBG_instantiate(drbg, td->pers, td->perslen) > 0 | |
339 | || !uninstantiate(drbg)) | |
12fb8c3d RS |
340 | goto err; |
341 | } | |
342 | ||
343 | /* Instantiate with valid data, Check generation is now OK */ | |
75e2c877 RS |
344 | if (!instantiate(drbg, td, &t) |
345 | || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0, | |
12fb8c3d RS |
346 | td->adin, td->adinlen))) |
347 | goto err; | |
348 | ||
349 | /* Request too much data for one request */ | |
75e2c877 | 350 | if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0, |
12fb8c3d RS |
351 | td->adin, td->adinlen))) |
352 | goto err; | |
353 | ||
354 | /* Try too large additional input */ | |
75e2c877 | 355 | if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0, |
aa048aef | 356 | td->adin, drbg->max_adinlen + 1))) |
12fb8c3d RS |
357 | goto err; |
358 | ||
359 | /* | |
360 | * Check prediction resistance request fails if entropy source | |
361 | * failure. | |
362 | */ | |
aa048aef | 363 | t.entropylen = 0; |
75e2c877 | 364 | if (TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1, |
12fb8c3d | 365 | td->adin, td->adinlen)) |
75e2c877 | 366 | || !uninstantiate(drbg)) |
12fb8c3d RS |
367 | goto err; |
368 | ||
4468b6ed | 369 | /* Instantiate again with valid data */ |
75e2c877 | 370 | if (!instantiate(drbg, td, &t)) |
12fb8c3d | 371 | goto err; |
a93ba405 DMSP |
372 | generate_counter_tmp = drbg->generate_counter; |
373 | drbg->generate_counter = drbg->reseed_interval; | |
12fb8c3d RS |
374 | |
375 | /* Generate output and check entropy has been requested for reseed */ | |
aa048aef | 376 | t.entropycnt = 0; |
75e2c877 | 377 | if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0, |
12fb8c3d | 378 | td->adin, td->adinlen)) |
aa048aef | 379 | || !TEST_int_eq(t.entropycnt, 1) |
a93ba405 | 380 | || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1) |
75e2c877 | 381 | || !uninstantiate(drbg)) |
12fb8c3d RS |
382 | goto err; |
383 | ||
384 | /* | |
385 | * Check prediction resistance request fails if entropy source | |
386 | * failure. | |
387 | */ | |
aa048aef | 388 | t.entropylen = 0; |
75e2c877 | 389 | if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1, |
12fb8c3d | 390 | td->adin, td->adinlen)) |
75e2c877 | 391 | || !uninstantiate(drbg)) |
12fb8c3d RS |
392 | goto err; |
393 | ||
394 | /* Test reseed counter works */ | |
75e2c877 | 395 | if (!instantiate(drbg, td, &t)) |
12fb8c3d | 396 | goto err; |
a93ba405 DMSP |
397 | generate_counter_tmp = drbg->generate_counter; |
398 | drbg->generate_counter = drbg->reseed_interval; | |
12fb8c3d RS |
399 | |
400 | /* Generate output and check entropy has been requested for reseed */ | |
aa048aef | 401 | t.entropycnt = 0; |
75e2c877 | 402 | if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0, |
12fb8c3d | 403 | td->adin, td->adinlen)) |
aa048aef | 404 | || !TEST_int_eq(t.entropycnt, 1) |
a93ba405 | 405 | || !TEST_int_eq(drbg->generate_counter, generate_counter_tmp + 1) |
75e2c877 | 406 | || !uninstantiate(drbg)) |
12fb8c3d RS |
407 | goto err; |
408 | ||
409 | /* | |
410 | * Explicit reseed tests | |
411 | */ | |
412 | ||
413 | /* Test explicit reseed with too large additional input */ | |
75e2c877 | 414 | if (!init(drbg, td, &t) |
aa048aef | 415 | || RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1) > 0) |
12fb8c3d RS |
416 | goto err; |
417 | ||
418 | /* Test explicit reseed with entropy source failure */ | |
aa048aef | 419 | t.entropylen = 0; |
75e2c877 RS |
420 | if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0) |
421 | || !uninstantiate(drbg)) | |
12fb8c3d RS |
422 | goto err; |
423 | ||
424 | /* Test explicit reseed with too much entropy */ | |
75e2c877 | 425 | if (!init(drbg, td, &t)) |
12fb8c3d | 426 | goto err; |
aa048aef | 427 | t.entropylen = drbg->max_entropylen + 1; |
75e2c877 RS |
428 | if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0) |
429 | || !uninstantiate(drbg)) | |
12fb8c3d RS |
430 | goto err; |
431 | ||
432 | /* Test explicit reseed with too little entropy */ | |
75e2c877 | 433 | if (!init(drbg, td, &t)) |
12fb8c3d | 434 | goto err; |
aa048aef | 435 | t.entropylen = drbg->min_entropylen - 1; |
75e2c877 RS |
436 | if (!TEST_int_le(RAND_DRBG_reseed(drbg, td->adin, td->adinlen), 0) |
437 | || !uninstantiate(drbg)) | |
12fb8c3d RS |
438 | goto err; |
439 | ||
440 | /* Standard says we have to check uninstantiate really zeroes */ | |
8212d505 | 441 | if (!TEST_mem_eq(zero, sizeof(drbg->data), &drbg->data, sizeof(drbg->data))) |
12fb8c3d RS |
442 | goto err; |
443 | ||
444 | ret = 1; | |
445 | ||
446 | err: | |
75e2c877 RS |
447 | uninstantiate(drbg); |
448 | RAND_DRBG_free(drbg); | |
12fb8c3d RS |
449 | return ret; |
450 | } | |
451 | ||
452 | static int test_kats(int i) | |
453 | { | |
454 | DRBG_SELFTEST_DATA *td = &drbg_test[i]; | |
455 | int rv = 0; | |
456 | ||
457 | if (!single_kat(td)) | |
458 | goto err; | |
459 | rv = 1; | |
460 | ||
461 | err: | |
462 | return rv; | |
463 | } | |
464 | ||
465 | static int test_error_checks(int i) | |
466 | { | |
467 | DRBG_SELFTEST_DATA *td = &drbg_test[i]; | |
468 | int rv = 0; | |
469 | ||
470 | if (error_check(td)) | |
471 | goto err; | |
472 | rv = 1; | |
473 | ||
474 | err: | |
475 | return rv; | |
476 | } | |
477 | ||
a93ba405 DMSP |
478 | /* |
479 | * Hook context data, attached as EXDATA to the RAND_DRBG | |
480 | */ | |
481 | typedef struct hook_ctx_st { | |
482 | RAND_DRBG *drbg; | |
483 | /* | |
484 | * Currently, all DRBGs use the same get_entropy() callback. | |
485 | * The tests however, don't assume this and store | |
486 | * the original callback for every DRBG separately. | |
487 | */ | |
488 | RAND_DRBG_get_entropy_fn get_entropy; | |
489 | /* forces a failure of the get_entropy() call if nonzero */ | |
490 | int fail; | |
491 | /* counts successful reseeds */ | |
492 | int reseed_count; | |
493 | } HOOK_CTX; | |
494 | ||
495 | static HOOK_CTX master_ctx, public_ctx, private_ctx; | |
496 | ||
497 | static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg) | |
498 | { | |
499 | return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index); | |
500 | } | |
501 | ||
502 | /* Intercepts and counts calls to the get_entropy() callback */ | |
503 | static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout, | |
504 | int entropy, size_t min_len, size_t max_len) | |
505 | { | |
506 | size_t ret; | |
507 | HOOK_CTX *ctx = get_hook_ctx(drbg); | |
508 | ||
509 | if (ctx->fail != 0) | |
510 | return 0; | |
511 | ||
512 | ret = ctx->get_entropy( | |
513 | drbg, pout, entropy, min_len, max_len); | |
514 | ||
515 | if (ret != 0) | |
516 | ctx->reseed_count++; | |
517 | return ret; | |
518 | } | |
519 | ||
520 | /* Installs a hook for the get_entropy() callback of the given drbg */ | |
521 | static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx) | |
522 | { | |
523 | memset(ctx, 0, sizeof(*ctx)); | |
524 | ctx->drbg = drbg; | |
525 | ctx->get_entropy = drbg->get_entropy; | |
526 | drbg->get_entropy = get_entropy_hook; | |
527 | RAND_DRBG_set_ex_data(drbg, app_data_index, ctx); | |
528 | } | |
529 | ||
530 | /* Installs the hook for the get_entropy() callback of the given drbg */ | |
531 | static void unhook_drbg(RAND_DRBG *drbg) | |
532 | { | |
533 | HOOK_CTX *ctx = get_hook_ctx(drbg); | |
534 | ||
535 | drbg->get_entropy = ctx->get_entropy; | |
536 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data); | |
537 | } | |
75e2c877 | 538 | |
a93ba405 DMSP |
539 | /* Resets the given hook context */ |
540 | static void reset_hook_ctx(HOOK_CTX *ctx) | |
75e2c877 | 541 | { |
a93ba405 DMSP |
542 | ctx->fail = 0; |
543 | ctx->reseed_count = 0; | |
544 | } | |
545 | ||
546 | /* Resets all drbg hook contexts */ | |
547 | static void reset_drbg_hook_ctx() | |
548 | { | |
549 | reset_hook_ctx(&master_ctx); | |
550 | reset_hook_ctx(&public_ctx); | |
551 | reset_hook_ctx(&private_ctx); | |
552 | } | |
553 | ||
554 | /* | |
555 | * Generates random output using RAND_bytes() and RAND_priv_bytes() | |
556 | * and checks whether the three shared DRBGs were reseeded as | |
557 | * expected. | |
558 | * | |
559 | * |expect_success|: expected outcome (as reported by RAND_status()) | |
560 | * |master|, |public|, |private|: pointers to the three shared DRBGs | |
561 | * |expect_xxx_reseed| = | |
562 | * 1: it is expected that the specified DRBG is reseeded | |
563 | * 0: it is expected that the specified DRBG is not reseeded | |
564 | * -1: don't check whether the specified DRBG was reseeded or not | |
565 | */ | |
566 | static int test_drbg_reseed(int expect_success, | |
567 | RAND_DRBG *master, | |
568 | RAND_DRBG *public, | |
569 | RAND_DRBG *private, | |
570 | int expect_master_reseed, | |
571 | int expect_public_reseed, | |
572 | int expect_private_reseed | |
573 | ) | |
574 | { | |
575 | unsigned char buf[32]; | |
08a65d96 | 576 | time_t before_reseed, after_reseed; |
a93ba405 DMSP |
577 | int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR); |
578 | ||
579 | /* | |
580 | * step 1: check preconditions | |
581 | */ | |
582 | ||
583 | /* Test whether seed propagation is enabled */ | |
584 | if (!TEST_int_ne(master->reseed_counter, 0) | |
585 | || !TEST_int_ne(public->reseed_counter, 0) | |
586 | || !TEST_int_ne(private->reseed_counter, 0)) | |
587 | return 0; | |
588 | ||
589 | /* Check whether the master DRBG's reseed counter is the largest one */ | |
590 | if (!TEST_int_le(public->reseed_counter, master->reseed_counter) | |
591 | || !TEST_int_le(private->reseed_counter, master->reseed_counter)) | |
592 | return 0; | |
593 | ||
594 | /* | |
595 | * step 2: generate random output | |
596 | */ | |
597 | ||
598 | /* Generate random output from the public and private DRBG */ | |
08a65d96 | 599 | before_reseed = expect_master_reseed == 1 ? time(NULL) : 0; |
a93ba405 DMSP |
600 | if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success) |
601 | || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success)) | |
602 | return 0; | |
08a65d96 | 603 | after_reseed = time(NULL); |
a93ba405 DMSP |
604 | |
605 | ||
606 | /* | |
607 | * step 3: check postconditions | |
608 | */ | |
75e2c877 | 609 | |
a93ba405 DMSP |
610 | /* Test whether reseeding succeeded as expected */ |
611 | if (!TEST_int_eq(master->state, expected_state) | |
612 | || !TEST_int_eq(public->state, expected_state) | |
613 | || !TEST_int_eq(private->state, expected_state)) | |
75e2c877 | 614 | return 0; |
a93ba405 DMSP |
615 | |
616 | if (expect_master_reseed >= 0) { | |
617 | /* Test whether master DRBG was reseeded as expected */ | |
618 | if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed)) | |
619 | return 0; | |
620 | } | |
621 | ||
622 | if (expect_public_reseed >= 0) { | |
623 | /* Test whether public DRBG was reseeded as expected */ | |
624 | if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed)) | |
625 | return 0; | |
626 | } | |
627 | ||
628 | if (expect_private_reseed >= 0) { | |
629 | /* Test whether public DRBG was reseeded as expected */ | |
630 | if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed)) | |
631 | return 0; | |
632 | } | |
633 | ||
634 | if (expect_success == 1) { | |
635 | /* Test whether all three reseed counters are synchronized */ | |
636 | if (!TEST_int_eq(public->reseed_counter, master->reseed_counter) | |
637 | || !TEST_int_eq(private->reseed_counter, master->reseed_counter)) | |
638 | return 0; | |
08a65d96 DMSP |
639 | |
640 | /* Test whether reseed time of master DRBG is set correctly */ | |
641 | if (!TEST_time_t_le(before_reseed, master->reseed_time) | |
642 | || !TEST_time_t_le(master->reseed_time, after_reseed)) | |
643 | return 0; | |
644 | ||
645 | /* Test whether reseed times of child DRBGs are synchronized with master */ | |
646 | if (!TEST_time_t_ge(public->reseed_time, master->reseed_time) | |
647 | || !TEST_time_t_ge(private->reseed_time, master->reseed_time)) | |
648 | return 0; | |
a93ba405 DMSP |
649 | } else { |
650 | ERR_clear_error(); | |
651 | } | |
652 | ||
75e2c877 RS |
653 | return 1; |
654 | } | |
655 | ||
a93ba405 DMSP |
656 | /* |
657 | * Test whether the default rand_method (RAND_OpenSSL()) is | |
658 | * setup correctly, in particular whether reseeding works | |
659 | * as designed. | |
660 | */ | |
661 | static int test_rand_reseed(void) | |
662 | { | |
663 | RAND_DRBG *master, *public, *private; | |
664 | unsigned char rand_add_buf[256]; | |
665 | int rv=0; | |
666 | ||
667 | /* Check whether RAND_OpenSSL() is the default method */ | |
668 | if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL())) | |
669 | return 0; | |
670 | ||
671 | /* All three DRBGs should be non-null */ | |
672 | if (!TEST_ptr(master = RAND_DRBG_get0_master()) | |
673 | || !TEST_ptr(public = RAND_DRBG_get0_public()) | |
674 | || !TEST_ptr(private = RAND_DRBG_get0_private())) | |
675 | return 0; | |
676 | ||
677 | /* There should be three distinct DRBGs, two of them chained to master */ | |
678 | if (!TEST_ptr_ne(public, private) | |
679 | || !TEST_ptr_ne(public, master) | |
680 | || !TEST_ptr_ne(private, master) | |
681 | || !TEST_ptr_eq(public->parent, master) | |
682 | || !TEST_ptr_eq(private->parent, master)) | |
683 | return 0; | |
684 | ||
685 | /* Install hooks for the following tests */ | |
686 | hook_drbg(master, &master_ctx); | |
687 | hook_drbg(public, &public_ctx); | |
688 | hook_drbg(private, &private_ctx); | |
689 | ||
690 | /* | |
691 | * Test initial state of shared DRBs | |
692 | */ | |
693 | if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0))) | |
694 | goto error; | |
695 | reset_drbg_hook_ctx(); | |
696 | ||
697 | /* | |
698 | * Test whether the public and private DRBG are both reseeded when their | |
699 | * reseed counters differ from the master's reseed counter. | |
700 | */ | |
701 | master->reseed_counter++; | |
702 | if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1))) | |
703 | goto error; | |
704 | reset_drbg_hook_ctx(); | |
705 | ||
706 | /* | |
707 | * Test whether the public DRBG is reseeded when its reseed counter differs | |
708 | * from the master's reseed counter. | |
709 | */ | |
710 | master->reseed_counter++; | |
711 | private->reseed_counter++; | |
712 | if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0))) | |
713 | goto error; | |
714 | reset_drbg_hook_ctx(); | |
715 | ||
716 | /* | |
717 | * Test whether the private DRBG is reseeded when its reseed counter differs | |
718 | * from the master's reseed counter. | |
719 | */ | |
720 | master->reseed_counter++; | |
721 | public->reseed_counter++; | |
722 | if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1))) | |
723 | goto error; | |
724 | reset_drbg_hook_ctx(); | |
725 | ||
726 | ||
727 | /* fill 'randomness' buffer with some arbitrary data */ | |
728 | memset(rand_add_buf, 'r', sizeof(rand_add_buf)); | |
729 | ||
730 | /* | |
731 | * Test whether all three DRBGs are reseeded by RAND_add() | |
732 | */ | |
733 | RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf)); | |
734 | if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1))) | |
735 | goto error; | |
736 | reset_drbg_hook_ctx(); | |
737 | ||
738 | ||
739 | /* | |
740 | * Test whether none of the DRBGs is reseed if the master fails to reseed | |
741 | */ | |
742 | master_ctx.fail = 1; | |
743 | master->reseed_counter++; | |
744 | RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf)); | |
745 | if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0))) | |
746 | goto error; | |
747 | reset_drbg_hook_ctx(); | |
748 | ||
749 | rv = 1; | |
750 | ||
751 | error: | |
752 | /* Remove hooks */ | |
753 | unhook_drbg(master); | |
754 | unhook_drbg(public); | |
755 | unhook_drbg(private); | |
756 | ||
757 | return rv; | |
758 | } | |
759 | ||
12fb8c3d | 760 | |
ad887416 | 761 | int setup_tests(void) |
12fb8c3d | 762 | { |
12fb8c3d RS |
763 | app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL); |
764 | ||
765 | ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test)); | |
766 | ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test)); | |
a93ba405 | 767 | ADD_TEST(test_rand_reseed); |
ad887416 | 768 | return 1; |
12fb8c3d | 769 | } |