1*4724848cSchristos /*
2*4724848cSchristos * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #include <string.h>
11*4724848cSchristos #include "internal/nelem.h"
12*4724848cSchristos #include <openssl/crypto.h>
13*4724848cSchristos #include <openssl/err.h>
14*4724848cSchristos #include <openssl/rand.h>
15*4724848cSchristos #include <openssl/obj_mac.h>
16*4724848cSchristos #include <openssl/evp.h>
17*4724848cSchristos #include <openssl/aes.h>
18*4724848cSchristos #include "../crypto/rand/rand_local.h"
19*4724848cSchristos #include "../include/crypto/rand.h"
20*4724848cSchristos
21*4724848cSchristos #if defined(_WIN32)
22*4724848cSchristos # include <windows.h>
23*4724848cSchristos #endif
24*4724848cSchristos
25*4724848cSchristos
26*4724848cSchristos #if defined(OPENSSL_SYS_UNIX)
27*4724848cSchristos # include <sys/types.h>
28*4724848cSchristos # include <sys/wait.h>
29*4724848cSchristos # include <unistd.h>
30*4724848cSchristos #endif
31*4724848cSchristos
32*4724848cSchristos #include "testutil.h"
33*4724848cSchristos #include "drbgtest.h"
34*4724848cSchristos
35*4724848cSchristos typedef struct drbg_selftest_data_st {
36*4724848cSchristos int post;
37*4724848cSchristos int nid;
38*4724848cSchristos unsigned int flags;
39*4724848cSchristos
40*4724848cSchristos /* KAT data for no PR */
41*4724848cSchristos const unsigned char *entropy;
42*4724848cSchristos size_t entropylen;
43*4724848cSchristos const unsigned char *nonce;
44*4724848cSchristos size_t noncelen;
45*4724848cSchristos const unsigned char *pers;
46*4724848cSchristos size_t perslen;
47*4724848cSchristos const unsigned char *adin;
48*4724848cSchristos size_t adinlen;
49*4724848cSchristos const unsigned char *entropyreseed;
50*4724848cSchristos size_t entropyreseedlen;
51*4724848cSchristos const unsigned char *adinreseed;
52*4724848cSchristos size_t adinreseedlen;
53*4724848cSchristos const unsigned char *adin2;
54*4724848cSchristos size_t adin2len;
55*4724848cSchristos const unsigned char *expected;
56*4724848cSchristos size_t exlen;
57*4724848cSchristos const unsigned char *kat2;
58*4724848cSchristos size_t kat2len;
59*4724848cSchristos
60*4724848cSchristos /* KAT data for PR */
61*4724848cSchristos const unsigned char *entropy_pr;
62*4724848cSchristos size_t entropylen_pr;
63*4724848cSchristos const unsigned char *nonce_pr;
64*4724848cSchristos size_t noncelen_pr;
65*4724848cSchristos const unsigned char *pers_pr;
66*4724848cSchristos size_t perslen_pr;
67*4724848cSchristos const unsigned char *adin_pr;
68*4724848cSchristos size_t adinlen_pr;
69*4724848cSchristos const unsigned char *entropypr_pr;
70*4724848cSchristos size_t entropyprlen_pr;
71*4724848cSchristos const unsigned char *ading_pr;
72*4724848cSchristos size_t adinglen_pr;
73*4724848cSchristos const unsigned char *entropyg_pr;
74*4724848cSchristos size_t entropyglen_pr;
75*4724848cSchristos const unsigned char *kat_pr;
76*4724848cSchristos size_t katlen_pr;
77*4724848cSchristos const unsigned char *kat2_pr;
78*4724848cSchristos size_t kat2len_pr;
79*4724848cSchristos } DRBG_SELFTEST_DATA;
80*4724848cSchristos
81*4724848cSchristos #define make_drbg_test_data(nid, flag, pr, post) {\
82*4724848cSchristos post, nid, flag, \
83*4724848cSchristos pr##_entropyinput, sizeof(pr##_entropyinput), \
84*4724848cSchristos pr##_nonce, sizeof(pr##_nonce), \
85*4724848cSchristos pr##_personalizationstring, sizeof(pr##_personalizationstring), \
86*4724848cSchristos pr##_additionalinput, sizeof(pr##_additionalinput), \
87*4724848cSchristos pr##_entropyinputreseed, sizeof(pr##_entropyinputreseed), \
88*4724848cSchristos pr##_additionalinputreseed, sizeof(pr##_additionalinputreseed), \
89*4724848cSchristos pr##_additionalinput2, sizeof(pr##_additionalinput2), \
90*4724848cSchristos pr##_int_returnedbits, sizeof(pr##_int_returnedbits), \
91*4724848cSchristos pr##_returnedbits, sizeof(pr##_returnedbits), \
92*4724848cSchristos pr##_pr_entropyinput, sizeof(pr##_pr_entropyinput), \
93*4724848cSchristos pr##_pr_nonce, sizeof(pr##_pr_nonce), \
94*4724848cSchristos pr##_pr_personalizationstring, sizeof(pr##_pr_personalizationstring), \
95*4724848cSchristos pr##_pr_additionalinput, sizeof(pr##_pr_additionalinput), \
96*4724848cSchristos pr##_pr_entropyinputpr, sizeof(pr##_pr_entropyinputpr), \
97*4724848cSchristos pr##_pr_additionalinput2, sizeof(pr##_pr_additionalinput2), \
98*4724848cSchristos pr##_pr_entropyinputpr2, sizeof(pr##_pr_entropyinputpr2), \
99*4724848cSchristos pr##_pr_int_returnedbits, sizeof(pr##_pr_int_returnedbits), \
100*4724848cSchristos pr##_pr_returnedbits, sizeof(pr##_pr_returnedbits) \
101*4724848cSchristos }
102*4724848cSchristos
103*4724848cSchristos #define make_drbg_test_data_use_df(nid, pr, p) \
104*4724848cSchristos make_drbg_test_data(nid, 0, pr, p)
105*4724848cSchristos
106*4724848cSchristos #define make_drbg_test_data_no_df(nid, pr, p) \
107*4724848cSchristos make_drbg_test_data(nid, RAND_DRBG_FLAG_CTR_NO_DF, pr, p)
108*4724848cSchristos
109*4724848cSchristos static DRBG_SELFTEST_DATA drbg_test[] = {
110*4724848cSchristos make_drbg_test_data_no_df (NID_aes_128_ctr, aes_128_no_df, 0),
111*4724848cSchristos make_drbg_test_data_no_df (NID_aes_192_ctr, aes_192_no_df, 0),
112*4724848cSchristos make_drbg_test_data_no_df (NID_aes_256_ctr, aes_256_no_df, 1),
113*4724848cSchristos make_drbg_test_data_use_df(NID_aes_128_ctr, aes_128_use_df, 0),
114*4724848cSchristos make_drbg_test_data_use_df(NID_aes_192_ctr, aes_192_use_df, 0),
115*4724848cSchristos make_drbg_test_data_use_df(NID_aes_256_ctr, aes_256_use_df, 1),
116*4724848cSchristos };
117*4724848cSchristos
118*4724848cSchristos static int app_data_index;
119*4724848cSchristos
120*4724848cSchristos /*
121*4724848cSchristos * Test context data, attached as EXDATA to the RAND_DRBG
122*4724848cSchristos */
123*4724848cSchristos typedef struct test_ctx_st {
124*4724848cSchristos const unsigned char *entropy;
125*4724848cSchristos size_t entropylen;
126*4724848cSchristos int entropycnt;
127*4724848cSchristos const unsigned char *nonce;
128*4724848cSchristos size_t noncelen;
129*4724848cSchristos int noncecnt;
130*4724848cSchristos } TEST_CTX;
131*4724848cSchristos
kat_entropy(RAND_DRBG * drbg,unsigned char ** pout,int entropy,size_t min_len,size_t max_len,int prediction_resistance)132*4724848cSchristos static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
133*4724848cSchristos int entropy, size_t min_len, size_t max_len,
134*4724848cSchristos int prediction_resistance)
135*4724848cSchristos {
136*4724848cSchristos TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
137*4724848cSchristos
138*4724848cSchristos t->entropycnt++;
139*4724848cSchristos *pout = (unsigned char *)t->entropy;
140*4724848cSchristos return t->entropylen;
141*4724848cSchristos }
142*4724848cSchristos
kat_nonce(RAND_DRBG * drbg,unsigned char ** pout,int entropy,size_t min_len,size_t max_len)143*4724848cSchristos static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
144*4724848cSchristos int entropy, size_t min_len, size_t max_len)
145*4724848cSchristos {
146*4724848cSchristos TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
147*4724848cSchristos
148*4724848cSchristos t->noncecnt++;
149*4724848cSchristos *pout = (unsigned char *)t->nonce;
150*4724848cSchristos return t->noncelen;
151*4724848cSchristos }
152*4724848cSchristos
uninstantiate(RAND_DRBG * drbg)153*4724848cSchristos static int uninstantiate(RAND_DRBG *drbg)
154*4724848cSchristos {
155*4724848cSchristos int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
156*4724848cSchristos
157*4724848cSchristos ERR_clear_error();
158*4724848cSchristos return ret;
159*4724848cSchristos }
160*4724848cSchristos
161*4724848cSchristos /*
162*4724848cSchristos * Do a single KAT test. Return 0 on failure.
163*4724848cSchristos */
single_kat(DRBG_SELFTEST_DATA * td)164*4724848cSchristos static int single_kat(DRBG_SELFTEST_DATA *td)
165*4724848cSchristos {
166*4724848cSchristos RAND_DRBG *drbg = NULL;
167*4724848cSchristos TEST_CTX t;
168*4724848cSchristos int failures = 0;
169*4724848cSchristos unsigned char buff[1024];
170*4724848cSchristos
171*4724848cSchristos /*
172*4724848cSchristos * Test without PR: Instantiate DRBG with test entropy, nonce and
173*4724848cSchristos * personalisation string.
174*4724848cSchristos */
175*4724848cSchristos if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
176*4724848cSchristos return 0;
177*4724848cSchristos if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
178*4724848cSchristos kat_nonce, NULL))) {
179*4724848cSchristos failures++;
180*4724848cSchristos goto err;
181*4724848cSchristos }
182*4724848cSchristos memset(&t, 0, sizeof(t));
183*4724848cSchristos t.entropy = td->entropy;
184*4724848cSchristos t.entropylen = td->entropylen;
185*4724848cSchristos t.nonce = td->nonce;
186*4724848cSchristos t.noncelen = td->noncelen;
187*4724848cSchristos RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
188*4724848cSchristos
189*4724848cSchristos if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
190*4724848cSchristos || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
191*4724848cSchristos td->adin, td->adinlen))
192*4724848cSchristos || !TEST_mem_eq(td->expected, td->exlen, buff, td->exlen))
193*4724848cSchristos failures++;
194*4724848cSchristos
195*4724848cSchristos /* Reseed DRBG with test entropy and additional input */
196*4724848cSchristos t.entropy = td->entropyreseed;
197*4724848cSchristos t.entropylen = td->entropyreseedlen;
198*4724848cSchristos if (!TEST_true(RAND_DRBG_reseed(drbg, td->adinreseed, td->adinreseedlen, 0)
199*4724848cSchristos || !TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len, 0,
200*4724848cSchristos td->adin2, td->adin2len))
201*4724848cSchristos || !TEST_mem_eq(td->kat2, td->kat2len, buff, td->kat2len)))
202*4724848cSchristos failures++;
203*4724848cSchristos uninstantiate(drbg);
204*4724848cSchristos
205*4724848cSchristos /*
206*4724848cSchristos * Now test with PR: Instantiate DRBG with test entropy, nonce and
207*4724848cSchristos * personalisation string.
208*4724848cSchristos */
209*4724848cSchristos if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
210*4724848cSchristos || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
211*4724848cSchristos kat_nonce, NULL)))
212*4724848cSchristos failures++;
213*4724848cSchristos RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
214*4724848cSchristos t.entropy = td->entropy_pr;
215*4724848cSchristos t.entropylen = td->entropylen_pr;
216*4724848cSchristos t.nonce = td->nonce_pr;
217*4724848cSchristos t.noncelen = td->noncelen_pr;
218*4724848cSchristos t.entropycnt = 0;
219*4724848cSchristos t.noncecnt = 0;
220*4724848cSchristos if (!TEST_true(RAND_DRBG_instantiate(drbg, td->pers_pr, td->perslen_pr)))
221*4724848cSchristos failures++;
222*4724848cSchristos
223*4724848cSchristos /*
224*4724848cSchristos * Now generate with PR: we need to supply entropy as this will
225*4724848cSchristos * perform a reseed operation.
226*4724848cSchristos */
227*4724848cSchristos t.entropy = td->entropypr_pr;
228*4724848cSchristos t.entropylen = td->entropyprlen_pr;
229*4724848cSchristos if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->katlen_pr, 1,
230*4724848cSchristos td->adin_pr, td->adinlen_pr))
231*4724848cSchristos || !TEST_mem_eq(td->kat_pr, td->katlen_pr, buff, td->katlen_pr))
232*4724848cSchristos failures++;
233*4724848cSchristos
234*4724848cSchristos /*
235*4724848cSchristos * Now generate again with PR: supply new entropy again.
236*4724848cSchristos */
237*4724848cSchristos t.entropy = td->entropyg_pr;
238*4724848cSchristos t.entropylen = td->entropyglen_pr;
239*4724848cSchristos
240*4724848cSchristos if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->kat2len_pr, 1,
241*4724848cSchristos td->ading_pr, td->adinglen_pr))
242*4724848cSchristos || !TEST_mem_eq(td->kat2_pr, td->kat2len_pr,
243*4724848cSchristos buff, td->kat2len_pr))
244*4724848cSchristos failures++;
245*4724848cSchristos
246*4724848cSchristos err:
247*4724848cSchristos uninstantiate(drbg);
248*4724848cSchristos RAND_DRBG_free(drbg);
249*4724848cSchristos return failures == 0;
250*4724848cSchristos }
251*4724848cSchristos
252*4724848cSchristos /*
253*4724848cSchristos * Initialise a DRBG based on selftest data
254*4724848cSchristos */
init(RAND_DRBG * drbg,DRBG_SELFTEST_DATA * td,TEST_CTX * t)255*4724848cSchristos static int init(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td, TEST_CTX *t)
256*4724848cSchristos {
257*4724848cSchristos if (!TEST_true(RAND_DRBG_set(drbg, td->nid, td->flags))
258*4724848cSchristos || !TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
259*4724848cSchristos kat_nonce, NULL)))
260*4724848cSchristos return 0;
261*4724848cSchristos RAND_DRBG_set_ex_data(drbg, app_data_index, t);
262*4724848cSchristos t->entropy = td->entropy;
263*4724848cSchristos t->entropylen = td->entropylen;
264*4724848cSchristos t->nonce = td->nonce;
265*4724848cSchristos t->noncelen = td->noncelen;
266*4724848cSchristos t->entropycnt = 0;
267*4724848cSchristos t->noncecnt = 0;
268*4724848cSchristos return 1;
269*4724848cSchristos }
270*4724848cSchristos
271*4724848cSchristos /*
272*4724848cSchristos * Initialise and instantiate DRBG based on selftest data
273*4724848cSchristos */
instantiate(RAND_DRBG * drbg,DRBG_SELFTEST_DATA * td,TEST_CTX * t)274*4724848cSchristos static int instantiate(RAND_DRBG *drbg, DRBG_SELFTEST_DATA *td,
275*4724848cSchristos TEST_CTX *t)
276*4724848cSchristos {
277*4724848cSchristos if (!TEST_true(init(drbg, td, t))
278*4724848cSchristos || !TEST_true(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
279*4724848cSchristos return 0;
280*4724848cSchristos return 1;
281*4724848cSchristos }
282*4724848cSchristos
283*4724848cSchristos /*
284*4724848cSchristos * Perform extensive error checking as required by SP800-90.
285*4724848cSchristos * Induce several failure modes and check an error condition is set.
286*4724848cSchristos */
error_check(DRBG_SELFTEST_DATA * td)287*4724848cSchristos static int error_check(DRBG_SELFTEST_DATA *td)
288*4724848cSchristos {
289*4724848cSchristos RAND_DRBG *drbg = NULL;
290*4724848cSchristos TEST_CTX t;
291*4724848cSchristos unsigned char buff[1024];
292*4724848cSchristos unsigned int reseed_counter_tmp;
293*4724848cSchristos int ret = 0;
294*4724848cSchristos
295*4724848cSchristos if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
296*4724848cSchristos goto err;
297*4724848cSchristos
298*4724848cSchristos /*
299*4724848cSchristos * Personalisation string tests
300*4724848cSchristos */
301*4724848cSchristos
302*4724848cSchristos /* Test detection of too large personalisation string */
303*4724848cSchristos if (!init(drbg, td, &t)
304*4724848cSchristos || !TEST_false(RAND_DRBG_instantiate(drbg, td->pers, drbg->max_perslen + 1)))
305*4724848cSchristos goto err;
306*4724848cSchristos
307*4724848cSchristos /*
308*4724848cSchristos * Entropy source tests
309*4724848cSchristos */
310*4724848cSchristos
311*4724848cSchristos /* Test entropy source failure detection: i.e. returns no data */
312*4724848cSchristos t.entropylen = 0;
313*4724848cSchristos if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen)))
314*4724848cSchristos goto err;
315*4724848cSchristos
316*4724848cSchristos /* Try to generate output from uninstantiated DRBG */
317*4724848cSchristos if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
318*4724848cSchristos td->adin, td->adinlen))
319*4724848cSchristos || !uninstantiate(drbg))
320*4724848cSchristos goto err;
321*4724848cSchristos
322*4724848cSchristos /* Test insufficient entropy */
323*4724848cSchristos if (!init(drbg, td, &t))
324*4724848cSchristos goto err;
325*4724848cSchristos t.entropylen = drbg->min_entropylen - 1;
326*4724848cSchristos if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
327*4724848cSchristos || !uninstantiate(drbg))
328*4724848cSchristos goto err;
329*4724848cSchristos
330*4724848cSchristos /* Test too much entropy */
331*4724848cSchristos if (!init(drbg, td, &t))
332*4724848cSchristos goto err;
333*4724848cSchristos t.entropylen = drbg->max_entropylen + 1;
334*4724848cSchristos if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
335*4724848cSchristos || !uninstantiate(drbg))
336*4724848cSchristos goto err;
337*4724848cSchristos
338*4724848cSchristos /*
339*4724848cSchristos * Nonce tests
340*4724848cSchristos */
341*4724848cSchristos
342*4724848cSchristos /* Test too small nonce */
343*4724848cSchristos if (drbg->min_noncelen) {
344*4724848cSchristos if (!init(drbg, td, &t))
345*4724848cSchristos goto err;
346*4724848cSchristos t.noncelen = drbg->min_noncelen - 1;
347*4724848cSchristos if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
348*4724848cSchristos || !uninstantiate(drbg))
349*4724848cSchristos goto err;
350*4724848cSchristos }
351*4724848cSchristos
352*4724848cSchristos /* Test too large nonce */
353*4724848cSchristos if (drbg->max_noncelen) {
354*4724848cSchristos if (!init(drbg, td, &t))
355*4724848cSchristos goto err;
356*4724848cSchristos t.noncelen = drbg->max_noncelen + 1;
357*4724848cSchristos if (!TEST_false(RAND_DRBG_instantiate(drbg, td->pers, td->perslen))
358*4724848cSchristos || !uninstantiate(drbg))
359*4724848cSchristos goto err;
360*4724848cSchristos }
361*4724848cSchristos
362*4724848cSchristos /* Instantiate with valid data, Check generation is now OK */
363*4724848cSchristos if (!instantiate(drbg, td, &t)
364*4724848cSchristos || !TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
365*4724848cSchristos td->adin, td->adinlen)))
366*4724848cSchristos goto err;
367*4724848cSchristos
368*4724848cSchristos /* Request too much data for one request */
369*4724848cSchristos if (!TEST_false(RAND_DRBG_generate(drbg, buff, drbg->max_request + 1, 0,
370*4724848cSchristos td->adin, td->adinlen)))
371*4724848cSchristos goto err;
372*4724848cSchristos
373*4724848cSchristos /* Try too large additional input */
374*4724848cSchristos if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
375*4724848cSchristos td->adin, drbg->max_adinlen + 1)))
376*4724848cSchristos goto err;
377*4724848cSchristos
378*4724848cSchristos /*
379*4724848cSchristos * Check prediction resistance request fails if entropy source
380*4724848cSchristos * failure.
381*4724848cSchristos */
382*4724848cSchristos t.entropylen = 0;
383*4724848cSchristos if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
384*4724848cSchristos td->adin, td->adinlen))
385*4724848cSchristos || !uninstantiate(drbg))
386*4724848cSchristos goto err;
387*4724848cSchristos
388*4724848cSchristos /* Instantiate again with valid data */
389*4724848cSchristos if (!instantiate(drbg, td, &t))
390*4724848cSchristos goto err;
391*4724848cSchristos reseed_counter_tmp = drbg->generate_counter;
392*4724848cSchristos drbg->generate_counter = drbg->reseed_interval;
393*4724848cSchristos
394*4724848cSchristos /* Generate output and check entropy has been requested for reseed */
395*4724848cSchristos t.entropycnt = 0;
396*4724848cSchristos if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
397*4724848cSchristos td->adin, td->adinlen))
398*4724848cSchristos || !TEST_int_eq(t.entropycnt, 1)
399*4724848cSchristos || !TEST_int_eq(drbg->generate_counter, reseed_counter_tmp + 1)
400*4724848cSchristos || !uninstantiate(drbg))
401*4724848cSchristos goto err;
402*4724848cSchristos
403*4724848cSchristos /*
404*4724848cSchristos * Check prediction resistance request fails if entropy source
405*4724848cSchristos * failure.
406*4724848cSchristos */
407*4724848cSchristos t.entropylen = 0;
408*4724848cSchristos if (!TEST_false(RAND_DRBG_generate(drbg, buff, td->exlen, 1,
409*4724848cSchristos td->adin, td->adinlen))
410*4724848cSchristos || !uninstantiate(drbg))
411*4724848cSchristos goto err;
412*4724848cSchristos
413*4724848cSchristos /* Test reseed counter works */
414*4724848cSchristos if (!instantiate(drbg, td, &t))
415*4724848cSchristos goto err;
416*4724848cSchristos reseed_counter_tmp = drbg->generate_counter;
417*4724848cSchristos drbg->generate_counter = drbg->reseed_interval;
418*4724848cSchristos
419*4724848cSchristos /* Generate output and check entropy has been requested for reseed */
420*4724848cSchristos t.entropycnt = 0;
421*4724848cSchristos if (!TEST_true(RAND_DRBG_generate(drbg, buff, td->exlen, 0,
422*4724848cSchristos td->adin, td->adinlen))
423*4724848cSchristos || !TEST_int_eq(t.entropycnt, 1)
424*4724848cSchristos || !TEST_int_eq(drbg->generate_counter, reseed_counter_tmp + 1)
425*4724848cSchristos || !uninstantiate(drbg))
426*4724848cSchristos goto err;
427*4724848cSchristos
428*4724848cSchristos /*
429*4724848cSchristos * Explicit reseed tests
430*4724848cSchristos */
431*4724848cSchristos
432*4724848cSchristos /* Test explicit reseed with too large additional input */
433*4724848cSchristos if (!instantiate(drbg, td, &t)
434*4724848cSchristos || !TEST_false(RAND_DRBG_reseed(drbg, td->adin, drbg->max_adinlen + 1, 0)))
435*4724848cSchristos goto err;
436*4724848cSchristos
437*4724848cSchristos /* Test explicit reseed with entropy source failure */
438*4724848cSchristos t.entropylen = 0;
439*4724848cSchristos if (!TEST_false(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0))
440*4724848cSchristos || !uninstantiate(drbg))
441*4724848cSchristos goto err;
442*4724848cSchristos
443*4724848cSchristos /* Test explicit reseed with too much entropy */
444*4724848cSchristos if (!instantiate(drbg, td, &t))
445*4724848cSchristos goto err;
446*4724848cSchristos t.entropylen = drbg->max_entropylen + 1;
447*4724848cSchristos if (!TEST_false(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0))
448*4724848cSchristos || !uninstantiate(drbg))
449*4724848cSchristos goto err;
450*4724848cSchristos
451*4724848cSchristos /* Test explicit reseed with too little entropy */
452*4724848cSchristos if (!instantiate(drbg, td, &t))
453*4724848cSchristos goto err;
454*4724848cSchristos t.entropylen = drbg->min_entropylen - 1;
455*4724848cSchristos if (!TEST_false(RAND_DRBG_reseed(drbg, td->adin, td->adinlen, 0))
456*4724848cSchristos || !uninstantiate(drbg))
457*4724848cSchristos goto err;
458*4724848cSchristos
459*4724848cSchristos ret = 1;
460*4724848cSchristos
461*4724848cSchristos err:
462*4724848cSchristos uninstantiate(drbg);
463*4724848cSchristos RAND_DRBG_free(drbg);
464*4724848cSchristos return ret;
465*4724848cSchristos }
466*4724848cSchristos
test_kats(int i)467*4724848cSchristos static int test_kats(int i)
468*4724848cSchristos {
469*4724848cSchristos DRBG_SELFTEST_DATA *td = &drbg_test[i];
470*4724848cSchristos int rv = 0;
471*4724848cSchristos
472*4724848cSchristos if (!single_kat(td))
473*4724848cSchristos goto err;
474*4724848cSchristos rv = 1;
475*4724848cSchristos
476*4724848cSchristos err:
477*4724848cSchristos return rv;
478*4724848cSchristos }
479*4724848cSchristos
test_error_checks(int i)480*4724848cSchristos static int test_error_checks(int i)
481*4724848cSchristos {
482*4724848cSchristos DRBG_SELFTEST_DATA *td = &drbg_test[i];
483*4724848cSchristos int rv = 0;
484*4724848cSchristos
485*4724848cSchristos if (!error_check(td))
486*4724848cSchristos goto err;
487*4724848cSchristos rv = 1;
488*4724848cSchristos
489*4724848cSchristos err:
490*4724848cSchristos return rv;
491*4724848cSchristos }
492*4724848cSchristos
493*4724848cSchristos /*
494*4724848cSchristos * Hook context data, attached as EXDATA to the RAND_DRBG
495*4724848cSchristos */
496*4724848cSchristos typedef struct hook_ctx_st {
497*4724848cSchristos RAND_DRBG *drbg;
498*4724848cSchristos /*
499*4724848cSchristos * Currently, all DRBGs use the same get_entropy() callback.
500*4724848cSchristos * The tests however, don't assume this and store
501*4724848cSchristos * the original callback for every DRBG separately.
502*4724848cSchristos */
503*4724848cSchristos RAND_DRBG_get_entropy_fn get_entropy;
504*4724848cSchristos /* forces a failure of the get_entropy() call if nonzero */
505*4724848cSchristos int fail;
506*4724848cSchristos /* counts successful reseeds */
507*4724848cSchristos int reseed_count;
508*4724848cSchristos } HOOK_CTX;
509*4724848cSchristos
510*4724848cSchristos static HOOK_CTX master_ctx, public_ctx, private_ctx;
511*4724848cSchristos
get_hook_ctx(RAND_DRBG * drbg)512*4724848cSchristos static HOOK_CTX *get_hook_ctx(RAND_DRBG *drbg)
513*4724848cSchristos {
514*4724848cSchristos return (HOOK_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
515*4724848cSchristos }
516*4724848cSchristos
517*4724848cSchristos /* Intercepts and counts calls to the get_entropy() callback */
get_entropy_hook(RAND_DRBG * drbg,unsigned char ** pout,int entropy,size_t min_len,size_t max_len,int prediction_resistance)518*4724848cSchristos static size_t get_entropy_hook(RAND_DRBG *drbg, unsigned char **pout,
519*4724848cSchristos int entropy, size_t min_len, size_t max_len,
520*4724848cSchristos int prediction_resistance)
521*4724848cSchristos {
522*4724848cSchristos size_t ret;
523*4724848cSchristos HOOK_CTX *ctx = get_hook_ctx(drbg);
524*4724848cSchristos
525*4724848cSchristos if (ctx->fail != 0)
526*4724848cSchristos return 0;
527*4724848cSchristos
528*4724848cSchristos ret = ctx->get_entropy(drbg, pout, entropy, min_len, max_len,
529*4724848cSchristos prediction_resistance);
530*4724848cSchristos
531*4724848cSchristos if (ret != 0)
532*4724848cSchristos ctx->reseed_count++;
533*4724848cSchristos return ret;
534*4724848cSchristos }
535*4724848cSchristos
536*4724848cSchristos /* Installs a hook for the get_entropy() callback of the given drbg */
hook_drbg(RAND_DRBG * drbg,HOOK_CTX * ctx)537*4724848cSchristos static void hook_drbg(RAND_DRBG *drbg, HOOK_CTX *ctx)
538*4724848cSchristos {
539*4724848cSchristos memset(ctx, 0, sizeof(*ctx));
540*4724848cSchristos ctx->drbg = drbg;
541*4724848cSchristos ctx->get_entropy = drbg->get_entropy;
542*4724848cSchristos drbg->get_entropy = get_entropy_hook;
543*4724848cSchristos RAND_DRBG_set_ex_data(drbg, app_data_index, ctx);
544*4724848cSchristos }
545*4724848cSchristos
546*4724848cSchristos /* Installs the hook for the get_entropy() callback of the given drbg */
unhook_drbg(RAND_DRBG * drbg)547*4724848cSchristos static void unhook_drbg(RAND_DRBG *drbg)
548*4724848cSchristos {
549*4724848cSchristos HOOK_CTX *ctx = get_hook_ctx(drbg);
550*4724848cSchristos
551*4724848cSchristos drbg->get_entropy = ctx->get_entropy;
552*4724848cSchristos CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
553*4724848cSchristos }
554*4724848cSchristos
555*4724848cSchristos /* Resets the given hook context */
reset_hook_ctx(HOOK_CTX * ctx)556*4724848cSchristos static void reset_hook_ctx(HOOK_CTX *ctx)
557*4724848cSchristos {
558*4724848cSchristos ctx->fail = 0;
559*4724848cSchristos ctx->reseed_count = 0;
560*4724848cSchristos }
561*4724848cSchristos
562*4724848cSchristos /* Resets all drbg hook contexts */
reset_drbg_hook_ctx(void)563*4724848cSchristos static void reset_drbg_hook_ctx(void)
564*4724848cSchristos {
565*4724848cSchristos reset_hook_ctx(&master_ctx);
566*4724848cSchristos reset_hook_ctx(&public_ctx);
567*4724848cSchristos reset_hook_ctx(&private_ctx);
568*4724848cSchristos }
569*4724848cSchristos
570*4724848cSchristos /*
571*4724848cSchristos * Generates random output using RAND_bytes() and RAND_priv_bytes()
572*4724848cSchristos * and checks whether the three shared DRBGs were reseeded as
573*4724848cSchristos * expected.
574*4724848cSchristos *
575*4724848cSchristos * |expect_success|: expected outcome (as reported by RAND_status())
576*4724848cSchristos * |master|, |public|, |private|: pointers to the three shared DRBGs
577*4724848cSchristos * |expect_xxx_reseed| =
578*4724848cSchristos * 1: it is expected that the specified DRBG is reseeded
579*4724848cSchristos * 0: it is expected that the specified DRBG is not reseeded
580*4724848cSchristos * -1: don't check whether the specified DRBG was reseeded or not
581*4724848cSchristos * |reseed_time|: if nonzero, used instead of time(NULL) to set the
582*4724848cSchristos * |before_reseed| time.
583*4724848cSchristos */
test_drbg_reseed(int expect_success,RAND_DRBG * master,RAND_DRBG * public,RAND_DRBG * private,int expect_master_reseed,int expect_public_reseed,int expect_private_reseed,time_t reseed_time)584*4724848cSchristos static int test_drbg_reseed(int expect_success,
585*4724848cSchristos RAND_DRBG *master,
586*4724848cSchristos RAND_DRBG *public,
587*4724848cSchristos RAND_DRBG *private,
588*4724848cSchristos int expect_master_reseed,
589*4724848cSchristos int expect_public_reseed,
590*4724848cSchristos int expect_private_reseed,
591*4724848cSchristos time_t reseed_time
592*4724848cSchristos )
593*4724848cSchristos {
594*4724848cSchristos unsigned char buf[32];
595*4724848cSchristos time_t before_reseed, after_reseed;
596*4724848cSchristos int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR);
597*4724848cSchristos
598*4724848cSchristos /*
599*4724848cSchristos * step 1: check preconditions
600*4724848cSchristos */
601*4724848cSchristos
602*4724848cSchristos /* Test whether seed propagation is enabled */
603*4724848cSchristos if (!TEST_int_ne(master->reseed_counter, 0)
604*4724848cSchristos || !TEST_int_ne(public->reseed_counter, 0)
605*4724848cSchristos || !TEST_int_ne(private->reseed_counter, 0))
606*4724848cSchristos return 0;
607*4724848cSchristos
608*4724848cSchristos /* Check whether the master DRBG's reseed counter is the largest one */
609*4724848cSchristos if (!TEST_int_le(public->reseed_counter, master->reseed_counter)
610*4724848cSchristos || !TEST_int_le(private->reseed_counter, master->reseed_counter))
611*4724848cSchristos return 0;
612*4724848cSchristos
613*4724848cSchristos /*
614*4724848cSchristos * step 2: generate random output
615*4724848cSchristos */
616*4724848cSchristos
617*4724848cSchristos if (reseed_time == 0)
618*4724848cSchristos reseed_time = time(NULL);
619*4724848cSchristos
620*4724848cSchristos /* Generate random output from the public and private DRBG */
621*4724848cSchristos before_reseed = expect_master_reseed == 1 ? reseed_time : 0;
622*4724848cSchristos if (!TEST_int_eq(RAND_bytes(buf, sizeof(buf)), expect_success)
623*4724848cSchristos || !TEST_int_eq(RAND_priv_bytes(buf, sizeof(buf)), expect_success))
624*4724848cSchristos return 0;
625*4724848cSchristos after_reseed = time(NULL);
626*4724848cSchristos
627*4724848cSchristos
628*4724848cSchristos /*
629*4724848cSchristos * step 3: check postconditions
630*4724848cSchristos */
631*4724848cSchristos
632*4724848cSchristos /* Test whether reseeding succeeded as expected */
633*4724848cSchristos if (!TEST_int_eq(master->state, expected_state)
634*4724848cSchristos || !TEST_int_eq(public->state, expected_state)
635*4724848cSchristos || !TEST_int_eq(private->state, expected_state))
636*4724848cSchristos return 0;
637*4724848cSchristos
638*4724848cSchristos if (expect_master_reseed >= 0) {
639*4724848cSchristos /* Test whether master DRBG was reseeded as expected */
640*4724848cSchristos if (!TEST_int_eq(master_ctx.reseed_count, expect_master_reseed))
641*4724848cSchristos return 0;
642*4724848cSchristos }
643*4724848cSchristos
644*4724848cSchristos if (expect_public_reseed >= 0) {
645*4724848cSchristos /* Test whether public DRBG was reseeded as expected */
646*4724848cSchristos if (!TEST_int_eq(public_ctx.reseed_count, expect_public_reseed))
647*4724848cSchristos return 0;
648*4724848cSchristos }
649*4724848cSchristos
650*4724848cSchristos if (expect_private_reseed >= 0) {
651*4724848cSchristos /* Test whether public DRBG was reseeded as expected */
652*4724848cSchristos if (!TEST_int_eq(private_ctx.reseed_count, expect_private_reseed))
653*4724848cSchristos return 0;
654*4724848cSchristos }
655*4724848cSchristos
656*4724848cSchristos if (expect_success == 1) {
657*4724848cSchristos /* Test whether all three reseed counters are synchronized */
658*4724848cSchristos if (!TEST_int_eq(public->reseed_counter, master->reseed_counter)
659*4724848cSchristos || !TEST_int_eq(private->reseed_counter, master->reseed_counter))
660*4724848cSchristos return 0;
661*4724848cSchristos
662*4724848cSchristos /* Test whether reseed time of master DRBG is set correctly */
663*4724848cSchristos if (!TEST_time_t_le(before_reseed, master->reseed_time)
664*4724848cSchristos || !TEST_time_t_le(master->reseed_time, after_reseed))
665*4724848cSchristos return 0;
666*4724848cSchristos
667*4724848cSchristos /* Test whether reseed times of child DRBGs are synchronized with master */
668*4724848cSchristos if (!TEST_time_t_ge(public->reseed_time, master->reseed_time)
669*4724848cSchristos || !TEST_time_t_ge(private->reseed_time, master->reseed_time))
670*4724848cSchristos return 0;
671*4724848cSchristos } else {
672*4724848cSchristos ERR_clear_error();
673*4724848cSchristos }
674*4724848cSchristos
675*4724848cSchristos return 1;
676*4724848cSchristos }
677*4724848cSchristos
678*4724848cSchristos
679*4724848cSchristos #if defined(OPENSSL_SYS_UNIX)
680*4724848cSchristos /*
681*4724848cSchristos * Test whether master, public and private DRBG are reseeded after
682*4724848cSchristos * forking the process.
683*4724848cSchristos */
test_drbg_reseed_after_fork(RAND_DRBG * master,RAND_DRBG * public,RAND_DRBG * private)684*4724848cSchristos static int test_drbg_reseed_after_fork(RAND_DRBG *master,
685*4724848cSchristos RAND_DRBG *public,
686*4724848cSchristos RAND_DRBG *private)
687*4724848cSchristos {
688*4724848cSchristos pid_t pid;
689*4724848cSchristos int status=0;
690*4724848cSchristos
691*4724848cSchristos pid = fork();
692*4724848cSchristos if (!TEST_int_ge(pid, 0))
693*4724848cSchristos return 0;
694*4724848cSchristos
695*4724848cSchristos if (pid > 0) {
696*4724848cSchristos /* I'm the parent; wait for the child and check its exit code */
697*4724848cSchristos return TEST_int_eq(waitpid(pid, &status, 0), pid) && TEST_int_eq(status, 0);
698*4724848cSchristos }
699*4724848cSchristos
700*4724848cSchristos /* I'm the child; check whether all three DRBGs reseed. */
701*4724848cSchristos if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
702*4724848cSchristos status = 1;
703*4724848cSchristos
704*4724848cSchristos /* Remove hooks */
705*4724848cSchristos unhook_drbg(master);
706*4724848cSchristos unhook_drbg(public);
707*4724848cSchristos unhook_drbg(private);
708*4724848cSchristos exit(status);
709*4724848cSchristos }
710*4724848cSchristos #endif
711*4724848cSchristos
712*4724848cSchristos /*
713*4724848cSchristos * Test whether the default rand_method (RAND_OpenSSL()) is
714*4724848cSchristos * setup correctly, in particular whether reseeding works
715*4724848cSchristos * as designed.
716*4724848cSchristos */
test_rand_drbg_reseed(void)717*4724848cSchristos static int test_rand_drbg_reseed(void)
718*4724848cSchristos {
719*4724848cSchristos RAND_DRBG *master, *public, *private;
720*4724848cSchristos unsigned char rand_add_buf[256];
721*4724848cSchristos int rv=0;
722*4724848cSchristos time_t before_reseed;
723*4724848cSchristos
724*4724848cSchristos /* Check whether RAND_OpenSSL() is the default method */
725*4724848cSchristos if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL()))
726*4724848cSchristos return 0;
727*4724848cSchristos
728*4724848cSchristos /* All three DRBGs should be non-null */
729*4724848cSchristos if (!TEST_ptr(master = RAND_DRBG_get0_master())
730*4724848cSchristos || !TEST_ptr(public = RAND_DRBG_get0_public())
731*4724848cSchristos || !TEST_ptr(private = RAND_DRBG_get0_private()))
732*4724848cSchristos return 0;
733*4724848cSchristos
734*4724848cSchristos /* There should be three distinct DRBGs, two of them chained to master */
735*4724848cSchristos if (!TEST_ptr_ne(public, private)
736*4724848cSchristos || !TEST_ptr_ne(public, master)
737*4724848cSchristos || !TEST_ptr_ne(private, master)
738*4724848cSchristos || !TEST_ptr_eq(public->parent, master)
739*4724848cSchristos || !TEST_ptr_eq(private->parent, master))
740*4724848cSchristos return 0;
741*4724848cSchristos
742*4724848cSchristos /* uninstantiate the three global DRBGs */
743*4724848cSchristos RAND_DRBG_uninstantiate(private);
744*4724848cSchristos RAND_DRBG_uninstantiate(public);
745*4724848cSchristos RAND_DRBG_uninstantiate(master);
746*4724848cSchristos
747*4724848cSchristos
748*4724848cSchristos /* Install hooks for the following tests */
749*4724848cSchristos hook_drbg(master, &master_ctx);
750*4724848cSchristos hook_drbg(public, &public_ctx);
751*4724848cSchristos hook_drbg(private, &private_ctx);
752*4724848cSchristos
753*4724848cSchristos
754*4724848cSchristos /*
755*4724848cSchristos * Test initial seeding of shared DRBGs
756*4724848cSchristos */
757*4724848cSchristos if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1, 0)))
758*4724848cSchristos goto error;
759*4724848cSchristos reset_drbg_hook_ctx();
760*4724848cSchristos
761*4724848cSchristos
762*4724848cSchristos /*
763*4724848cSchristos * Test initial state of shared DRBGs
764*4724848cSchristos */
765*4724848cSchristos if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 0, 0)))
766*4724848cSchristos goto error;
767*4724848cSchristos reset_drbg_hook_ctx();
768*4724848cSchristos
769*4724848cSchristos /*
770*4724848cSchristos * Test whether the public and private DRBG are both reseeded when their
771*4724848cSchristos * reseed counters differ from the master's reseed counter.
772*4724848cSchristos */
773*4724848cSchristos master->reseed_counter++;
774*4724848cSchristos if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 1, 0)))
775*4724848cSchristos goto error;
776*4724848cSchristos reset_drbg_hook_ctx();
777*4724848cSchristos
778*4724848cSchristos /*
779*4724848cSchristos * Test whether the public DRBG is reseeded when its reseed counter differs
780*4724848cSchristos * from the master's reseed counter.
781*4724848cSchristos */
782*4724848cSchristos master->reseed_counter++;
783*4724848cSchristos private->reseed_counter++;
784*4724848cSchristos if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 1, 0, 0)))
785*4724848cSchristos goto error;
786*4724848cSchristos reset_drbg_hook_ctx();
787*4724848cSchristos
788*4724848cSchristos /*
789*4724848cSchristos * Test whether the private DRBG is reseeded when its reseed counter differs
790*4724848cSchristos * from the master's reseed counter.
791*4724848cSchristos */
792*4724848cSchristos master->reseed_counter++;
793*4724848cSchristos public->reseed_counter++;
794*4724848cSchristos if (!TEST_true(test_drbg_reseed(1, master, public, private, 0, 0, 1, 0)))
795*4724848cSchristos goto error;
796*4724848cSchristos reset_drbg_hook_ctx();
797*4724848cSchristos
798*4724848cSchristos #if defined(OPENSSL_SYS_UNIX)
799*4724848cSchristos if (!TEST_true(test_drbg_reseed_after_fork(master, public, private)))
800*4724848cSchristos goto error;
801*4724848cSchristos #endif
802*4724848cSchristos
803*4724848cSchristos /* fill 'randomness' buffer with some arbitrary data */
804*4724848cSchristos memset(rand_add_buf, 'r', sizeof(rand_add_buf));
805*4724848cSchristos
806*4724848cSchristos /*
807*4724848cSchristos * Test whether all three DRBGs are reseeded by RAND_add().
808*4724848cSchristos * The before_reseed time has to be measured here and passed into the
809*4724848cSchristos * test_drbg_reseed() test, because the master DRBG gets already reseeded
810*4724848cSchristos * in RAND_add(), whence the check for the condition
811*4724848cSchristos * before_reseed <= master->reseed_time will fail if the time value happens
812*4724848cSchristos * to increase between the RAND_add() and the test_drbg_reseed() call.
813*4724848cSchristos */
814*4724848cSchristos before_reseed = time(NULL);
815*4724848cSchristos RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
816*4724848cSchristos if (!TEST_true(test_drbg_reseed(1, master, public, private, 1, 1, 1,
817*4724848cSchristos before_reseed)))
818*4724848cSchristos goto error;
819*4724848cSchristos reset_drbg_hook_ctx();
820*4724848cSchristos
821*4724848cSchristos
822*4724848cSchristos /*
823*4724848cSchristos * Test whether none of the DRBGs is reseed if the master fails to reseed
824*4724848cSchristos */
825*4724848cSchristos master_ctx.fail = 1;
826*4724848cSchristos master->reseed_counter++;
827*4724848cSchristos RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf));
828*4724848cSchristos if (!TEST_true(test_drbg_reseed(0, master, public, private, 0, 0, 0, 0)))
829*4724848cSchristos goto error;
830*4724848cSchristos reset_drbg_hook_ctx();
831*4724848cSchristos
832*4724848cSchristos rv = 1;
833*4724848cSchristos
834*4724848cSchristos error:
835*4724848cSchristos /* Remove hooks */
836*4724848cSchristos unhook_drbg(master);
837*4724848cSchristos unhook_drbg(public);
838*4724848cSchristos unhook_drbg(private);
839*4724848cSchristos
840*4724848cSchristos return rv;
841*4724848cSchristos }
842*4724848cSchristos
843*4724848cSchristos #if defined(OPENSSL_THREADS)
844*4724848cSchristos static int multi_thread_rand_bytes_succeeded = 1;
845*4724848cSchristos static int multi_thread_rand_priv_bytes_succeeded = 1;
846*4724848cSchristos
run_multi_thread_test(void)847*4724848cSchristos static void run_multi_thread_test(void)
848*4724848cSchristos {
849*4724848cSchristos unsigned char buf[256];
850*4724848cSchristos time_t start = time(NULL);
851*4724848cSchristos RAND_DRBG *public = NULL, *private = NULL;
852*4724848cSchristos
853*4724848cSchristos if (!TEST_ptr(public = RAND_DRBG_get0_public())
854*4724848cSchristos || !TEST_ptr(private = RAND_DRBG_get0_private())) {
855*4724848cSchristos multi_thread_rand_bytes_succeeded = 0;
856*4724848cSchristos return;
857*4724848cSchristos }
858*4724848cSchristos RAND_DRBG_set_reseed_time_interval(private, 1);
859*4724848cSchristos RAND_DRBG_set_reseed_time_interval(public, 1);
860*4724848cSchristos
861*4724848cSchristos do {
862*4724848cSchristos if (RAND_bytes(buf, sizeof(buf)) <= 0)
863*4724848cSchristos multi_thread_rand_bytes_succeeded = 0;
864*4724848cSchristos if (RAND_priv_bytes(buf, sizeof(buf)) <= 0)
865*4724848cSchristos multi_thread_rand_priv_bytes_succeeded = 0;
866*4724848cSchristos }
867*4724848cSchristos while(time(NULL) - start < 5);
868*4724848cSchristos }
869*4724848cSchristos
870*4724848cSchristos # if defined(OPENSSL_SYS_WINDOWS)
871*4724848cSchristos
872*4724848cSchristos typedef HANDLE thread_t;
873*4724848cSchristos
thread_run(LPVOID arg)874*4724848cSchristos static DWORD WINAPI thread_run(LPVOID arg)
875*4724848cSchristos {
876*4724848cSchristos run_multi_thread_test();
877*4724848cSchristos /*
878*4724848cSchristos * Because we're linking with a static library, we must stop each
879*4724848cSchristos * thread explicitly, or so says OPENSSL_thread_stop(3)
880*4724848cSchristos */
881*4724848cSchristos OPENSSL_thread_stop();
882*4724848cSchristos return 0;
883*4724848cSchristos }
884*4724848cSchristos
run_thread(thread_t * t)885*4724848cSchristos static int run_thread(thread_t *t)
886*4724848cSchristos {
887*4724848cSchristos *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL);
888*4724848cSchristos return *t != NULL;
889*4724848cSchristos }
890*4724848cSchristos
wait_for_thread(thread_t thread)891*4724848cSchristos static int wait_for_thread(thread_t thread)
892*4724848cSchristos {
893*4724848cSchristos return WaitForSingleObject(thread, INFINITE) == 0;
894*4724848cSchristos }
895*4724848cSchristos
896*4724848cSchristos # else
897*4724848cSchristos
898*4724848cSchristos typedef pthread_t thread_t;
899*4724848cSchristos
thread_run(void * arg)900*4724848cSchristos static void *thread_run(void *arg)
901*4724848cSchristos {
902*4724848cSchristos run_multi_thread_test();
903*4724848cSchristos /*
904*4724848cSchristos * Because we're linking with a static library, we must stop each
905*4724848cSchristos * thread explicitly, or so says OPENSSL_thread_stop(3)
906*4724848cSchristos */
907*4724848cSchristos OPENSSL_thread_stop();
908*4724848cSchristos return NULL;
909*4724848cSchristos }
910*4724848cSchristos
run_thread(thread_t * t)911*4724848cSchristos static int run_thread(thread_t *t)
912*4724848cSchristos {
913*4724848cSchristos return pthread_create(t, NULL, thread_run, NULL) == 0;
914*4724848cSchristos }
915*4724848cSchristos
wait_for_thread(thread_t thread)916*4724848cSchristos static int wait_for_thread(thread_t thread)
917*4724848cSchristos {
918*4724848cSchristos return pthread_join(thread, NULL) == 0;
919*4724848cSchristos }
920*4724848cSchristos
921*4724848cSchristos # endif
922*4724848cSchristos
923*4724848cSchristos /*
924*4724848cSchristos * The main thread will also run the test, so we'll have THREADS+1 parallel
925*4724848cSchristos * tests running
926*4724848cSchristos */
927*4724848cSchristos # define THREADS 3
928*4724848cSchristos
test_multi_thread(void)929*4724848cSchristos static int test_multi_thread(void)
930*4724848cSchristos {
931*4724848cSchristos thread_t t[THREADS];
932*4724848cSchristos int i;
933*4724848cSchristos
934*4724848cSchristos for (i = 0; i < THREADS; i++)
935*4724848cSchristos run_thread(&t[i]);
936*4724848cSchristos run_multi_thread_test();
937*4724848cSchristos for (i = 0; i < THREADS; i++)
938*4724848cSchristos wait_for_thread(t[i]);
939*4724848cSchristos
940*4724848cSchristos if (!TEST_true(multi_thread_rand_bytes_succeeded))
941*4724848cSchristos return 0;
942*4724848cSchristos if (!TEST_true(multi_thread_rand_priv_bytes_succeeded))
943*4724848cSchristos return 0;
944*4724848cSchristos
945*4724848cSchristos return 1;
946*4724848cSchristos }
947*4724848cSchristos #endif
948*4724848cSchristos
949*4724848cSchristos /*
950*4724848cSchristos * Test that instantiation with RAND_seed() works as expected
951*4724848cSchristos *
952*4724848cSchristos * If no os entropy source is available then RAND_seed(buffer, bufsize)
953*4724848cSchristos * is expected to succeed if and only if the buffer length is at least
954*4724848cSchristos * rand_drbg_seedlen(master) bytes.
955*4724848cSchristos *
956*4724848cSchristos * If an os entropy source is available then RAND_seed(buffer, bufsize)
957*4724848cSchristos * is expected to succeed always.
958*4724848cSchristos */
test_rand_seed(void)959*4724848cSchristos static int test_rand_seed(void)
960*4724848cSchristos {
961*4724848cSchristos RAND_DRBG *master = NULL;
962*4724848cSchristos unsigned char rand_buf[256];
963*4724848cSchristos size_t rand_buflen;
964*4724848cSchristos size_t required_seed_buflen = 0;
965*4724848cSchristos
966*4724848cSchristos if (!TEST_ptr(master = RAND_DRBG_get0_master()))
967*4724848cSchristos return 0;
968*4724848cSchristos
969*4724848cSchristos #ifdef OPENSSL_RAND_SEED_NONE
970*4724848cSchristos required_seed_buflen = rand_drbg_seedlen(master);
971*4724848cSchristos #endif
972*4724848cSchristos
973*4724848cSchristos memset(rand_buf, 0xCD, sizeof(rand_buf));
974*4724848cSchristos
975*4724848cSchristos for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
976*4724848cSchristos RAND_DRBG_uninstantiate(master);
977*4724848cSchristos RAND_seed(rand_buf, rand_buflen);
978*4724848cSchristos
979*4724848cSchristos if (!TEST_int_eq(RAND_status(),
980*4724848cSchristos (rand_buflen >= required_seed_buflen)))
981*4724848cSchristos return 0;
982*4724848cSchristos }
983*4724848cSchristos
984*4724848cSchristos return 1;
985*4724848cSchristos }
986*4724848cSchristos
987*4724848cSchristos /*
988*4724848cSchristos * Test that adding additional data with RAND_add() works as expected
989*4724848cSchristos * when the master DRBG is instantiated (and below its reseed limit).
990*4724848cSchristos *
991*4724848cSchristos * This should succeed regardless of whether an os entropy source is
992*4724848cSchristos * available or not.
993*4724848cSchristos */
test_rand_add(void)994*4724848cSchristos static int test_rand_add(void)
995*4724848cSchristos {
996*4724848cSchristos unsigned char rand_buf[256];
997*4724848cSchristos size_t rand_buflen;
998*4724848cSchristos
999*4724848cSchristos memset(rand_buf, 0xCD, sizeof(rand_buf));
1000*4724848cSchristos
1001*4724848cSchristos /* make sure it's instantiated */
1002*4724848cSchristos RAND_seed(rand_buf, sizeof(rand_buf));
1003*4724848cSchristos if (!TEST_true(RAND_status()))
1004*4724848cSchristos return 0;
1005*4724848cSchristos
1006*4724848cSchristos for ( rand_buflen = 256 ; rand_buflen > 0 ; --rand_buflen ) {
1007*4724848cSchristos RAND_add(rand_buf, rand_buflen, 0.0);
1008*4724848cSchristos if (!TEST_true(RAND_status()))
1009*4724848cSchristos return 0;
1010*4724848cSchristos }
1011*4724848cSchristos
1012*4724848cSchristos return 1;
1013*4724848cSchristos }
1014*4724848cSchristos
setup_tests(void)1015*4724848cSchristos int setup_tests(void)
1016*4724848cSchristos {
1017*4724848cSchristos app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
1018*4724848cSchristos
1019*4724848cSchristos ADD_ALL_TESTS(test_kats, OSSL_NELEM(drbg_test));
1020*4724848cSchristos ADD_ALL_TESTS(test_error_checks, OSSL_NELEM(drbg_test));
1021*4724848cSchristos ADD_TEST(test_rand_drbg_reseed);
1022*4724848cSchristos ADD_TEST(test_rand_seed);
1023*4724848cSchristos ADD_TEST(test_rand_add);
1024*4724848cSchristos #if defined(OPENSSL_THREADS)
1025*4724848cSchristos ADD_TEST(test_multi_thread);
1026*4724848cSchristos #endif
1027*4724848cSchristos return 1;
1028*4724848cSchristos }
1029