1 1.1 christos /* 2 1.1 christos * Copyright 2011-2024 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * 4 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 5 1.1 christos * this file except in compliance with the License. You can obtain a copy 6 1.1 christos * in the file LICENSE in the source distribution or at 7 1.1 christos * https://www.openssl.org/source/license.html 8 1.1 christos */ 9 1.1 christos 10 1.1 christos /* We need to use some deprecated APIs */ 11 1.1 christos #define OPENSSL_SUPPRESS_DEPRECATED 12 1.1 christos 13 1.1 christos #include <string.h> 14 1.1 christos #include "internal/nelem.h" 15 1.1 christos #include <openssl/crypto.h> 16 1.1 christos #include <openssl/err.h> 17 1.1 christos #include <openssl/rand.h> 18 1.1 christos #include <openssl/obj_mac.h> 19 1.1 christos #include <openssl/evp.h> 20 1.1 christos #include <openssl/aes.h> 21 1.1 christos #include "../crypto/rand/rand_local.h" 22 1.1 christos #include "../include/crypto/rand.h" 23 1.1 christos #include "../include/crypto/evp.h" 24 1.1 christos #include "../providers/implementations/rands/drbg_local.h" 25 1.1 christos #include "../crypto/evp/evp_local.h" 26 1.1 christos 27 1.1 christos #if defined(_WIN32) 28 1.1.1.2 christos #include <windows.h> 29 1.1 christos #endif 30 1.1 christos 31 1.1 christos #if defined(OPENSSL_SYS_UNIX) 32 1.1.1.2 christos #include <sys/types.h> 33 1.1.1.2 christos #include <sys/wait.h> 34 1.1.1.2 christos #include <unistd.h> 35 1.1 christos #endif 36 1.1 christos 37 1.1 christos #include "testutil.h" 38 1.1 christos 39 1.1 christos /* 40 1.1 christos * DRBG generate wrappers 41 1.1 christos */ 42 1.1 christos static int gen_bytes(EVP_RAND_CTX *drbg, unsigned char *buf, int num) 43 1.1 christos { 44 1.1 christos #ifndef OPENSSL_NO_DEPRECATED_3_0 45 1.1 christos const RAND_METHOD *meth = RAND_get_rand_method(); 46 1.1 christos 47 1.1 christos if (meth != NULL && meth != RAND_OpenSSL()) { 48 1.1 christos if (meth->bytes != NULL) 49 1.1 christos return meth->bytes(buf, num); 50 1.1 christos return -1; 51 1.1 christos } 52 1.1 christos #endif 53 1.1 christos 54 1.1 christos if (drbg != NULL) 55 1.1 christos return EVP_RAND_generate(drbg, buf, num, 0, 0, NULL, 0); 56 1.1 christos return 0; 57 1.1 christos } 58 1.1 christos 59 1.1 christos static int rand_bytes(unsigned char *buf, int num) 60 1.1 christos { 61 1.1 christos return gen_bytes(RAND_get0_public(NULL), buf, num); 62 1.1 christos } 63 1.1 christos 64 1.1 christos static int rand_priv_bytes(unsigned char *buf, int num) 65 1.1 christos { 66 1.1 christos return gen_bytes(RAND_get0_private(NULL), buf, num); 67 1.1 christos } 68 1.1 christos 69 1.1 christos /* size of random output generated in test_drbg_reseed() */ 70 1.1 christos #define RANDOM_SIZE 16 71 1.1 christos 72 1.1 christos /* 73 1.1 christos * DRBG query functions 74 1.1 christos */ 75 1.1 christos static int state(EVP_RAND_CTX *drbg) 76 1.1 christos { 77 1.1 christos return EVP_RAND_get_state(drbg); 78 1.1 christos } 79 1.1 christos 80 1.1 christos static unsigned int query_rand_uint(EVP_RAND_CTX *drbg, const char *name) 81 1.1 christos { 82 1.1 christos OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; 83 1.1 christos unsigned int n; 84 1.1 christos 85 1.1 christos *params = OSSL_PARAM_construct_uint(name, &n); 86 1.1 christos if (EVP_RAND_CTX_get_params(drbg, params)) 87 1.1 christos return n; 88 1.1 christos return 0; 89 1.1 christos } 90 1.1 christos 91 1.1.1.2 christos #define DRBG_UINT(name) \ 92 1.1.1.2 christos static unsigned int name(EVP_RAND_CTX *drbg) \ 93 1.1.1.2 christos { \ 94 1.1.1.2 christos return query_rand_uint(drbg, #name); \ 95 1.1 christos } 96 1.1 christos DRBG_UINT(reseed_counter) 97 1.1 christos 98 1.1 christos static PROV_DRBG *prov_rand(EVP_RAND_CTX *drbg) 99 1.1 christos { 100 1.1 christos return (PROV_DRBG *)drbg->algctx; 101 1.1 christos } 102 1.1 christos 103 1.1 christos static void set_reseed_counter(EVP_RAND_CTX *drbg, unsigned int n) 104 1.1 christos { 105 1.1 christos PROV_DRBG *p = prov_rand(drbg); 106 1.1 christos 107 1.1 christos p->reseed_counter = n; 108 1.1 christos } 109 1.1 christos 110 1.1 christos static void inc_reseed_counter(EVP_RAND_CTX *drbg) 111 1.1 christos { 112 1.1 christos set_reseed_counter(drbg, reseed_counter(drbg) + 1); 113 1.1 christos } 114 1.1 christos 115 1.1 christos static time_t reseed_time(EVP_RAND_CTX *drbg) 116 1.1 christos { 117 1.1 christos OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; 118 1.1 christos time_t t; 119 1.1 christos 120 1.1 christos *params = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME, &t); 121 1.1 christos if (EVP_RAND_CTX_get_params(drbg, params)) 122 1.1 christos return t; 123 1.1 christos return 0; 124 1.1 christos } 125 1.1 christos 126 1.1 christos /* 127 1.1 christos * When building the FIPS module, it isn't possible to disable the continuous 128 1.1 christos * RNG tests. Tests that require this are skipped and this means a detection 129 1.1 christos * mechanism for the FIPS provider being in use. 130 1.1 christos */ 131 1.1 christos static int using_fips_rng(void) 132 1.1 christos { 133 1.1 christos EVP_RAND_CTX *primary = RAND_get0_primary(NULL); 134 1.1 christos const OSSL_PROVIDER *prov; 135 1.1 christos const char *name; 136 1.1 christos 137 1.1 christos if (!TEST_ptr(primary)) 138 1.1 christos return 0; 139 1.1 christos 140 1.1 christos prov = EVP_RAND_get0_provider(EVP_RAND_CTX_get0_rand(primary)); 141 1.1 christos if (!TEST_ptr(prov)) 142 1.1 christos return 0; 143 1.1 christos name = OSSL_PROVIDER_get0_name(prov); 144 1.1 christos return strstr(name, "FIPS Provider") != NULL; 145 1.1 christos } 146 1.1 christos 147 1.1.1.2 christos /* 148 1.1 christos * Disable CRNG testing if it is enabled. 149 1.1 christos * This stub remains to indicate the calling locations where it is necessary. 150 1.1 christos * Once the RNG infrastructure is able to disable these tests, it should be 151 1.1 christos * reconstituted. 152 1.1 christos */ 153 1.1 christos static int disable_crngt(EVP_RAND_CTX *drbg) 154 1.1 christos { 155 1.1 christos return 1; 156 1.1 christos } 157 1.1 christos 158 1.1 christos /* 159 1.1 christos * Generates random output using rand_bytes() and rand_priv_bytes() 160 1.1 christos * and checks whether the three shared DRBGs were reseeded as 161 1.1 christos * expected. 162 1.1 christos * 163 1.1 christos * |expect_success|: expected outcome (as reported by RAND_status()) 164 1.1 christos * |primary|, |public|, |private|: pointers to the three shared DRBGs 165 1.1 christos * |public_random|, |private_random|: generated random output 166 1.1 christos * |expect_xxx_reseed| = 167 1.1 christos * 1: it is expected that the specified DRBG is reseeded 168 1.1 christos * 0: it is expected that the specified DRBG is not reseeded 169 1.1 christos * -1: don't check whether the specified DRBG was reseeded or not 170 1.1 christos * |reseed_when|: if nonzero, used instead of time(NULL) to set the 171 1.1 christos * |before_reseed| time. 172 1.1 christos */ 173 1.1 christos static int test_drbg_reseed(int expect_success, 174 1.1.1.2 christos EVP_RAND_CTX *primary, 175 1.1.1.2 christos EVP_RAND_CTX *public, 176 1.1.1.2 christos EVP_RAND_CTX *private, 177 1.1.1.2 christos unsigned char *public_random, 178 1.1.1.2 christos unsigned char *private_random, 179 1.1.1.2 christos int expect_primary_reseed, 180 1.1.1.2 christos int expect_public_reseed, 181 1.1.1.2 christos int expect_private_reseed, 182 1.1.1.2 christos time_t reseed_when) 183 1.1 christos { 184 1.1 christos time_t before_reseed, after_reseed; 185 1.1 christos int expected_state = (expect_success ? DRBG_READY : DRBG_ERROR); 186 1.1 christos unsigned int primary_reseed, public_reseed, private_reseed; 187 1.1 christos unsigned char dummy[RANDOM_SIZE]; 188 1.1 christos 189 1.1 christos if (public_random == NULL) 190 1.1 christos public_random = dummy; 191 1.1 christos 192 1.1 christos if (private_random == NULL) 193 1.1 christos private_random = dummy; 194 1.1 christos 195 1.1 christos /* 196 1.1 christos * step 1: check preconditions 197 1.1 christos */ 198 1.1 christos 199 1.1 christos /* Test whether seed propagation is enabled */ 200 1.1 christos if (!TEST_int_ne(primary_reseed = reseed_counter(primary), 0) 201 1.1 christos || !TEST_int_ne(public_reseed = reseed_counter(public), 0) 202 1.1 christos || !TEST_int_ne(private_reseed = reseed_counter(private), 0)) 203 1.1 christos return 0; 204 1.1 christos 205 1.1 christos /* 206 1.1 christos * step 2: generate random output 207 1.1 christos */ 208 1.1 christos 209 1.1 christos if (reseed_when == 0) 210 1.1 christos reseed_when = time(NULL); 211 1.1 christos 212 1.1 christos /* Generate random output from the public and private DRBG */ 213 1.1 christos before_reseed = expect_primary_reseed == 1 ? reseed_when : 0; 214 1.1.1.2 christos if (!TEST_int_eq(rand_bytes((unsigned char *)public_random, 215 1.1.1.2 christos RANDOM_SIZE), 216 1.1.1.2 christos expect_success) 217 1.1.1.2 christos || !TEST_int_eq(rand_priv_bytes((unsigned char *)private_random, 218 1.1.1.2 christos RANDOM_SIZE), 219 1.1.1.2 christos expect_success)) 220 1.1 christos return 0; 221 1.1 christos after_reseed = time(NULL); 222 1.1 christos 223 1.1 christos /* 224 1.1 christos * step 3: check postconditions 225 1.1 christos */ 226 1.1 christos 227 1.1 christos /* Test whether reseeding succeeded as expected */ 228 1.1 christos if (!TEST_int_eq(state(primary), expected_state) 229 1.1 christos || !TEST_int_eq(state(public), expected_state) 230 1.1 christos || !TEST_int_eq(state(private), expected_state)) 231 1.1 christos return 0; 232 1.1 christos 233 1.1 christos if (expect_primary_reseed >= 0) { 234 1.1 christos /* Test whether primary DRBG was reseeded as expected */ 235 1.1 christos if (!TEST_int_ge(reseed_counter(primary), primary_reseed)) 236 1.1 christos return 0; 237 1.1 christos } 238 1.1 christos 239 1.1 christos if (expect_public_reseed >= 0) { 240 1.1 christos /* Test whether public DRBG was reseeded as expected */ 241 1.1 christos if (!TEST_int_ge(reseed_counter(public), public_reseed) 242 1.1.1.2 christos || !TEST_uint_ge(reseed_counter(public), 243 1.1.1.2 christos reseed_counter(primary))) 244 1.1 christos return 0; 245 1.1 christos } 246 1.1 christos 247 1.1 christos if (expect_private_reseed >= 0) { 248 1.1 christos /* Test whether public DRBG was reseeded as expected */ 249 1.1 christos if (!TEST_int_ge(reseed_counter(private), private_reseed) 250 1.1.1.2 christos || !TEST_uint_ge(reseed_counter(private), 251 1.1.1.2 christos reseed_counter(primary))) 252 1.1 christos return 0; 253 1.1 christos } 254 1.1 christos 255 1.1 christos if (expect_success == 1) { 256 1.1 christos /* Test whether reseed time of primary DRBG is set correctly */ 257 1.1 christos if (!TEST_time_t_le(before_reseed, reseed_time(primary)) 258 1.1 christos || !TEST_time_t_le(reseed_time(primary), after_reseed)) 259 1.1 christos return 0; 260 1.1 christos 261 1.1 christos /* Test whether reseed times of child DRBGs are synchronized with primary */ 262 1.1 christos if (!TEST_time_t_ge(reseed_time(public), reseed_time(primary)) 263 1.1 christos || !TEST_time_t_ge(reseed_time(private), reseed_time(primary))) 264 1.1 christos return 0; 265 1.1 christos } else { 266 1.1 christos ERR_clear_error(); 267 1.1 christos } 268 1.1 christos 269 1.1 christos return 1; 270 1.1 christos } 271 1.1 christos 272 1.1 christos #if defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_RAND_SEED_EGD) 273 1.1 christos /* number of children to fork */ 274 1.1 christos #define DRBG_FORK_COUNT 9 275 1.1 christos /* two results per child, two for the parent */ 276 1.1 christos #define DRBG_FORK_RESULT_COUNT (2 * (DRBG_FORK_COUNT + 1)) 277 1.1 christos 278 1.1 christos typedef struct drbg_fork_result_st { 279 1.1 christos 280 1.1 christos unsigned char random[RANDOM_SIZE]; /* random output */ 281 1.1 christos 282 1.1.1.2 christos int pindex; /* process index (0: parent, 1,2,3...: children)*/ 283 1.1.1.2 christos pid_t pid; /* process id */ 284 1.1.1.2 christos int private; /* true if the private drbg was used */ 285 1.1.1.2 christos char name[10]; /* 'parent' resp. 'child 1', 'child 2', ... */ 286 1.1 christos } drbg_fork_result; 287 1.1 christos 288 1.1 christos /* 289 1.1 christos * Sort the drbg_fork_result entries in lexicographical order 290 1.1 christos * 291 1.1 christos * This simplifies finding duplicate random output and makes 292 1.1 christos * the printout in case of an error more readable. 293 1.1 christos */ 294 1.1 christos static int compare_drbg_fork_result(const void *left, const void *right) 295 1.1 christos { 296 1.1 christos int result; 297 1.1 christos const drbg_fork_result *l = left; 298 1.1 christos const drbg_fork_result *r = right; 299 1.1 christos 300 1.1 christos /* separate public and private results */ 301 1.1 christos result = l->private - r->private; 302 1.1 christos 303 1.1 christos if (result == 0) 304 1.1 christos result = memcmp(l->random, r->random, RANDOM_SIZE); 305 1.1 christos 306 1.1 christos if (result == 0) 307 1.1 christos result = l->pindex - r->pindex; 308 1.1 christos 309 1.1 christos return result; 310 1.1 christos } 311 1.1 christos 312 1.1 christos /* 313 1.1 christos * Sort two-byte chunks of random data 314 1.1 christos * 315 1.1 christos * Used for finding collisions in two-byte chunks 316 1.1 christos */ 317 1.1 christos static int compare_rand_chunk(const void *left, const void *right) 318 1.1 christos { 319 1.1 christos return memcmp(left, right, 2); 320 1.1 christos } 321 1.1 christos 322 1.1 christos /* 323 1.1 christos * Test whether primary, public and private DRBG are reseeded 324 1.1 christos * in the child after forking the process. Collect the random 325 1.1 christos * output of the public and private DRBG and send it back to 326 1.1 christos * the parent process. 327 1.1 christos */ 328 1.1 christos static int test_drbg_reseed_in_child(EVP_RAND_CTX *primary, 329 1.1.1.2 christos EVP_RAND_CTX *public, 330 1.1.1.2 christos EVP_RAND_CTX *private, 331 1.1.1.2 christos drbg_fork_result result[2]) 332 1.1 christos { 333 1.1 christos int rv = 0, status; 334 1.1 christos int fd[2]; 335 1.1 christos pid_t pid; 336 1.1 christos unsigned char random[2 * RANDOM_SIZE]; 337 1.1 christos 338 1.1 christos if (!TEST_int_ge(pipe(fd), 0)) 339 1.1 christos return 0; 340 1.1 christos 341 1.1 christos if (!TEST_int_ge(pid = fork(), 0)) { 342 1.1 christos close(fd[0]); 343 1.1 christos close(fd[1]); 344 1.1 christos return 0; 345 1.1 christos } else if (pid > 0) { 346 1.1 christos 347 1.1 christos /* I'm the parent; close the write end */ 348 1.1 christos close(fd[1]); 349 1.1 christos 350 1.1 christos /* wait for children to terminate and collect their random output */ 351 1.1 christos if (TEST_int_eq(waitpid(pid, &status, 0), pid) 352 1.1 christos && TEST_int_eq(status, 0) 353 1.1 christos && TEST_true(read(fd[0], &random[0], sizeof(random)) 354 1.1.1.2 christos == sizeof(random))) { 355 1.1 christos 356 1.1 christos /* random output of public drbg */ 357 1.1 christos result[0].pid = pid; 358 1.1 christos result[0].private = 0; 359 1.1 christos memcpy(result[0].random, &random[0], RANDOM_SIZE); 360 1.1 christos 361 1.1 christos /* random output of private drbg */ 362 1.1 christos result[1].pid = pid; 363 1.1 christos result[1].private = 1; 364 1.1 christos memcpy(result[1].random, &random[RANDOM_SIZE], RANDOM_SIZE); 365 1.1 christos 366 1.1 christos rv = 1; 367 1.1 christos } 368 1.1 christos 369 1.1 christos /* close the read end */ 370 1.1 christos close(fd[0]); 371 1.1 christos 372 1.1 christos return rv; 373 1.1 christos 374 1.1 christos } else { 375 1.1 christos 376 1.1 christos /* I'm the child; close the read end */ 377 1.1 christos close(fd[0]); 378 1.1 christos 379 1.1 christos /* check whether all three DRBGs reseed and send output to parent */ 380 1.1 christos if (TEST_true(test_drbg_reseed(1, primary, public, private, 381 1.1.1.2 christos &random[0], &random[RANDOM_SIZE], 382 1.1.1.2 christos 1, 1, 1, 0)) 383 1.1 christos && TEST_true(write(fd[1], random, sizeof(random)) 384 1.1.1.2 christos == sizeof(random))) { 385 1.1 christos 386 1.1 christos rv = 1; 387 1.1 christos } 388 1.1 christos 389 1.1 christos /* close the write end */ 390 1.1 christos close(fd[1]); 391 1.1 christos 392 1.1 christos /* convert boolean to exit code */ 393 1.1 christos exit(rv == 0); 394 1.1 christos } 395 1.1 christos } 396 1.1 christos 397 1.1 christos static int test_rand_reseed_on_fork(EVP_RAND_CTX *primary, 398 1.1.1.2 christos EVP_RAND_CTX *public, 399 1.1.1.2 christos EVP_RAND_CTX *private) 400 1.1 christos { 401 1.1 christos unsigned int i; 402 1.1 christos pid_t pid = getpid(); 403 1.1 christos int verbose = (getenv("V") != NULL); 404 1.1 christos int success = 1; 405 1.1.1.2 christos int duplicate[2] = { 0, 0 }; 406 1.1 christos unsigned char random[2 * RANDOM_SIZE]; 407 1.1 christos unsigned char sample[DRBG_FORK_RESULT_COUNT * RANDOM_SIZE]; 408 1.1 christos unsigned char *psample = &sample[0]; 409 1.1 christos drbg_fork_result result[DRBG_FORK_RESULT_COUNT]; 410 1.1 christos drbg_fork_result *presult = &result[2]; 411 1.1 christos 412 1.1.1.2 christos memset(&result, 0, sizeof(result)); 413 1.1 christos 414 1.1.1.2 christos for (i = 1; i <= DRBG_FORK_COUNT; ++i) { 415 1.1 christos 416 1.1 christos presult[0].pindex = presult[1].pindex = i; 417 1.1 christos 418 1.1 christos BIO_snprintf(presult[0].name, sizeof(presult[0].name), "child %d", i); 419 1.1 christos strcpy(presult[1].name, presult[0].name); 420 1.1 christos 421 1.1 christos /* collect the random output of the children */ 422 1.1 christos if (!TEST_true(test_drbg_reseed_in_child(primary, 423 1.1.1.2 christos public, 424 1.1.1.2 christos private, 425 1.1.1.2 christos presult))) 426 1.1 christos return 0; 427 1.1 christos 428 1.1 christos presult += 2; 429 1.1 christos } 430 1.1 christos 431 1.1 christos /* collect the random output of the parent */ 432 1.1 christos if (!TEST_true(test_drbg_reseed(1, 433 1.1.1.2 christos primary, public, private, 434 1.1.1.2 christos &random[0], &random[RANDOM_SIZE], 435 1.1.1.2 christos 0, 0, 0, 0))) 436 1.1 christos return 0; 437 1.1 christos 438 1.1 christos strcpy(result[0].name, "parent"); 439 1.1 christos strcpy(result[1].name, "parent"); 440 1.1 christos 441 1.1 christos /* output of public drbg */ 442 1.1 christos result[0].pid = pid; 443 1.1 christos result[0].private = 0; 444 1.1 christos memcpy(result[0].random, &random[0], RANDOM_SIZE); 445 1.1 christos 446 1.1 christos /* output of private drbg */ 447 1.1 christos result[1].pid = pid; 448 1.1 christos result[1].private = 1; 449 1.1 christos memcpy(result[1].random, &random[RANDOM_SIZE], RANDOM_SIZE); 450 1.1 christos 451 1.1 christos /* collect all sampled random data in a single buffer */ 452 1.1.1.2 christos for (i = 0; i < DRBG_FORK_RESULT_COUNT; ++i) { 453 1.1 christos memcpy(psample, &result[i].random[0], RANDOM_SIZE); 454 1.1 christos psample += RANDOM_SIZE; 455 1.1 christos } 456 1.1 christos 457 1.1 christos /* sort the results... */ 458 1.1 christos qsort(result, DRBG_FORK_RESULT_COUNT, sizeof(drbg_fork_result), 459 1.1.1.2 christos compare_drbg_fork_result); 460 1.1 christos 461 1.1 christos /* ...and count duplicate prefixes by looking at the first byte only */ 462 1.1.1.2 christos for (i = 1; i < DRBG_FORK_RESULT_COUNT; ++i) { 463 1.1.1.2 christos if (result[i].random[0] == result[i - 1].random[0]) { 464 1.1 christos /* count public and private duplicates separately */ 465 1.1 christos ++duplicate[result[i].private]; 466 1.1 christos } 467 1.1 christos } 468 1.1 christos 469 1.1 christos if (duplicate[0] >= DRBG_FORK_COUNT - 1) { 470 1.1 christos /* just too many duplicates to be a coincidence */ 471 1.1 christos TEST_note("ERROR: %d duplicate prefixes in public random output", duplicate[0]); 472 1.1 christos success = 0; 473 1.1 christos } 474 1.1 christos 475 1.1 christos if (duplicate[1] >= DRBG_FORK_COUNT - 1) { 476 1.1 christos /* just too many duplicates to be a coincidence */ 477 1.1 christos TEST_note("ERROR: %d duplicate prefixes in private random output", duplicate[1]); 478 1.1 christos success = 0; 479 1.1 christos } 480 1.1 christos 481 1.1 christos duplicate[0] = 0; 482 1.1 christos 483 1.1 christos /* sort the two-byte chunks... */ 484 1.1.1.2 christos qsort(sample, sizeof(sample) / 2, 2, compare_rand_chunk); 485 1.1 christos 486 1.1 christos /* ...and count duplicate chunks */ 487 1.1.1.2 christos for (i = 2, psample = sample + 2; i < sizeof(sample); i += 2, psample += 2) { 488 1.1 christos if (compare_rand_chunk(psample - 2, psample) == 0) 489 1.1 christos ++duplicate[0]; 490 1.1 christos } 491 1.1 christos 492 1.1 christos if (duplicate[0] >= DRBG_FORK_COUNT - 1) { 493 1.1 christos /* just too many duplicates to be a coincidence */ 494 1.1 christos TEST_note("ERROR: %d duplicate chunks in random output", duplicate[0]); 495 1.1 christos success = 0; 496 1.1 christos } 497 1.1 christos 498 1.1 christos if (verbose || !success) { 499 1.1 christos 500 1.1.1.2 christos for (i = 0; i < DRBG_FORK_RESULT_COUNT; ++i) { 501 1.1 christos char *rand_hex = OPENSSL_buf2hexstr(result[i].random, RANDOM_SIZE); 502 1.1 christos 503 1.1 christos TEST_note(" random: %s, pid: %d (%s, %s)", 504 1.1.1.2 christos rand_hex, 505 1.1.1.2 christos result[i].pid, 506 1.1.1.2 christos result[i].name, 507 1.1.1.2 christos result[i].private ? "private" : "public"); 508 1.1 christos 509 1.1 christos OPENSSL_free(rand_hex); 510 1.1 christos } 511 1.1 christos } 512 1.1 christos 513 1.1 christos return success; 514 1.1 christos } 515 1.1 christos 516 1.1 christos static int test_rand_fork_safety(int i) 517 1.1 christos { 518 1.1 christos int success = 1; 519 1.1 christos unsigned char random[1]; 520 1.1 christos EVP_RAND_CTX *primary, *public, *private; 521 1.1 christos 522 1.1 christos /* All three DRBGs should be non-null */ 523 1.1 christos if (!TEST_ptr(primary = RAND_get0_primary(NULL)) 524 1.1 christos || !TEST_ptr(public = RAND_get0_public(NULL)) 525 1.1 christos || !TEST_ptr(private = RAND_get0_private(NULL))) 526 1.1 christos return 0; 527 1.1 christos 528 1.1 christos /* run the actual test */ 529 1.1 christos if (!TEST_true(test_rand_reseed_on_fork(primary, public, private))) 530 1.1 christos success = 0; 531 1.1 christos 532 1.1 christos /* request a single byte from each of the DRBGs before the next run */ 533 1.1 christos if (!TEST_int_gt(RAND_bytes(random, 1), 0) || !TEST_int_gt(RAND_priv_bytes(random, 1), 0)) 534 1.1 christos success = 0; 535 1.1 christos 536 1.1 christos return success; 537 1.1 christos } 538 1.1 christos #endif 539 1.1 christos 540 1.1 christos /* 541 1.1 christos * Test whether the default rand_method (RAND_OpenSSL()) is 542 1.1 christos * setup correctly, in particular whether reseeding works 543 1.1 christos * as designed. 544 1.1 christos */ 545 1.1 christos static int test_rand_reseed(void) 546 1.1 christos { 547 1.1 christos EVP_RAND_CTX *primary, *public, *private; 548 1.1 christos unsigned char rand_add_buf[256]; 549 1.1 christos int rv = 0; 550 1.1 christos time_t before_reseed; 551 1.1 christos 552 1.1 christos if (using_fips_rng()) 553 1.1 christos return TEST_skip("CRNGT cannot be disabled"); 554 1.1 christos 555 1.1 christos #ifndef OPENSSL_NO_DEPRECATED_3_0 556 1.1 christos /* Check whether RAND_OpenSSL() is the default method */ 557 1.1 christos if (!TEST_ptr_eq(RAND_get_rand_method(), RAND_OpenSSL())) 558 1.1 christos return 0; 559 1.1 christos #endif 560 1.1 christos 561 1.1 christos /* All three DRBGs should be non-null */ 562 1.1 christos if (!TEST_ptr(primary = RAND_get0_primary(NULL)) 563 1.1 christos || !TEST_ptr(public = RAND_get0_public(NULL)) 564 1.1 christos || !TEST_ptr(private = RAND_get0_private(NULL))) 565 1.1 christos return 0; 566 1.1 christos 567 1.1 christos /* There should be three distinct DRBGs, two of them chained to primary */ 568 1.1 christos if (!TEST_ptr_ne(public, private) 569 1.1 christos || !TEST_ptr_ne(public, primary) 570 1.1 christos || !TEST_ptr_ne(private, primary) 571 1.1 christos || !TEST_ptr_eq(prov_rand(public)->parent, prov_rand(primary)) 572 1.1 christos || !TEST_ptr_eq(prov_rand(private)->parent, prov_rand(primary))) 573 1.1 christos return 0; 574 1.1 christos 575 1.1 christos /* Disable CRNG testing for the primary DRBG */ 576 1.1 christos if (!TEST_true(disable_crngt(primary))) 577 1.1 christos return 0; 578 1.1 christos 579 1.1 christos /* uninstantiate the three global DRBGs */ 580 1.1 christos EVP_RAND_uninstantiate(primary); 581 1.1 christos EVP_RAND_uninstantiate(private); 582 1.1 christos EVP_RAND_uninstantiate(public); 583 1.1 christos 584 1.1 christos /* 585 1.1 christos * Test initial seeding of shared DRBGs 586 1.1 christos */ 587 1.1 christos if (!TEST_true(test_drbg_reseed(1, 588 1.1.1.2 christos primary, public, private, 589 1.1.1.2 christos NULL, NULL, 590 1.1.1.2 christos 1, 1, 1, 0))) 591 1.1 christos goto error; 592 1.1 christos 593 1.1 christos /* 594 1.1 christos * Test initial state of shared DRBGs 595 1.1 christos */ 596 1.1 christos if (!TEST_true(test_drbg_reseed(1, 597 1.1.1.2 christos primary, public, private, 598 1.1.1.2 christos NULL, NULL, 599 1.1.1.2 christos 0, 0, 0, 0))) 600 1.1 christos goto error; 601 1.1 christos 602 1.1 christos /* 603 1.1 christos * Test whether the public and private DRBG are both reseeded when their 604 1.1 christos * reseed counters differ from the primary's reseed counter. 605 1.1 christos */ 606 1.1 christos inc_reseed_counter(primary); 607 1.1 christos if (!TEST_true(test_drbg_reseed(1, 608 1.1.1.2 christos primary, public, private, 609 1.1.1.2 christos NULL, NULL, 610 1.1.1.2 christos 0, 1, 1, 0))) 611 1.1 christos goto error; 612 1.1 christos 613 1.1 christos /* 614 1.1 christos * Test whether the public DRBG is reseeded when its reseed counter differs 615 1.1 christos * from the primary's reseed counter. 616 1.1 christos */ 617 1.1 christos inc_reseed_counter(primary); 618 1.1 christos inc_reseed_counter(private); 619 1.1 christos if (!TEST_true(test_drbg_reseed(1, 620 1.1.1.2 christos primary, public, private, 621 1.1.1.2 christos NULL, NULL, 622 1.1.1.2 christos 0, 1, 0, 0))) 623 1.1 christos goto error; 624 1.1 christos 625 1.1 christos /* 626 1.1 christos * Test whether the private DRBG is reseeded when its reseed counter differs 627 1.1 christos * from the primary's reseed counter. 628 1.1 christos */ 629 1.1 christos inc_reseed_counter(primary); 630 1.1 christos inc_reseed_counter(public); 631 1.1 christos if (!TEST_true(test_drbg_reseed(1, 632 1.1.1.2 christos primary, public, private, 633 1.1.1.2 christos NULL, NULL, 634 1.1.1.2 christos 0, 0, 1, 0))) 635 1.1 christos goto error; 636 1.1 christos 637 1.1 christos /* fill 'randomness' buffer with some arbitrary data */ 638 1.1 christos memset(rand_add_buf, 'r', sizeof(rand_add_buf)); 639 1.1 christos 640 1.1 christos /* 641 1.1 christos * Test whether all three DRBGs are reseeded by RAND_add(). 642 1.1 christos * The before_reseed time has to be measured here and passed into the 643 1.1 christos * test_drbg_reseed() test, because the primary DRBG gets already reseeded 644 1.1 christos * in RAND_add(), whence the check for the condition 645 1.1 christos * before_reseed <= reseed_time(primary) will fail if the time value happens 646 1.1 christos * to increase between the RAND_add() and the test_drbg_reseed() call. 647 1.1 christos */ 648 1.1 christos before_reseed = time(NULL); 649 1.1 christos RAND_add(rand_add_buf, sizeof(rand_add_buf), sizeof(rand_add_buf)); 650 1.1 christos if (!TEST_true(test_drbg_reseed(1, 651 1.1.1.2 christos primary, public, private, 652 1.1.1.2 christos NULL, NULL, 653 1.1.1.2 christos 1, 1, 1, 654 1.1.1.2 christos before_reseed))) 655 1.1 christos goto error; 656 1.1 christos 657 1.1 christos rv = 1; 658 1.1 christos 659 1.1 christos error: 660 1.1.1.2 christos return rv; 661 1.1 christos } 662 1.1 christos 663 1.1 christos #if defined(OPENSSL_THREADS) 664 1.1 christos static int multi_thread_rand_bytes_succeeded = 1; 665 1.1 christos static int multi_thread_rand_priv_bytes_succeeded = 1; 666 1.1 christos 667 1.1 christos static int set_reseed_time_interval(EVP_RAND_CTX *drbg, int t) 668 1.1 christos { 669 1.1 christos OSSL_PARAM params[2]; 670 1.1 christos 671 1.1 christos params[0] = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, 672 1.1.1.2 christos &t); 673 1.1 christos params[1] = OSSL_PARAM_construct_end(); 674 1.1 christos return EVP_RAND_CTX_set_params(drbg, params); 675 1.1 christos } 676 1.1 christos 677 1.1 christos static void run_multi_thread_test(void) 678 1.1 christos { 679 1.1 christos unsigned char buf[256]; 680 1.1 christos time_t start = time(NULL); 681 1.1 christos EVP_RAND_CTX *public = NULL, *private = NULL; 682 1.1 christos 683 1.1 christos if (!TEST_ptr(public = RAND_get0_public(NULL)) 684 1.1.1.2 christos || !TEST_ptr(private = RAND_get0_private(NULL)) 685 1.1.1.2 christos || !TEST_true(set_reseed_time_interval(private, 1)) 686 1.1.1.2 christos || !TEST_true(set_reseed_time_interval(public, 1))) { 687 1.1 christos multi_thread_rand_bytes_succeeded = 0; 688 1.1 christos return; 689 1.1 christos } 690 1.1 christos 691 1.1 christos do { 692 1.1 christos if (rand_bytes(buf, sizeof(buf)) <= 0) 693 1.1 christos multi_thread_rand_bytes_succeeded = 0; 694 1.1 christos if (rand_priv_bytes(buf, sizeof(buf)) <= 0) 695 1.1 christos multi_thread_rand_priv_bytes_succeeded = 0; 696 1.1.1.2 christos } while (time(NULL) - start < 5); 697 1.1 christos } 698 1.1 christos 699 1.1.1.2 christos #if defined(OPENSSL_SYS_WINDOWS) 700 1.1 christos 701 1.1 christos typedef HANDLE thread_t; 702 1.1 christos 703 1.1 christos static DWORD WINAPI thread_run(LPVOID arg) 704 1.1 christos { 705 1.1 christos run_multi_thread_test(); 706 1.1 christos /* 707 1.1 christos * Because we're linking with a static library, we must stop each 708 1.1 christos * thread explicitly, or so says OPENSSL_thread_stop(3) 709 1.1 christos */ 710 1.1 christos OPENSSL_thread_stop(); 711 1.1 christos return 0; 712 1.1 christos } 713 1.1 christos 714 1.1 christos static int run_thread(thread_t *t) 715 1.1 christos { 716 1.1 christos *t = CreateThread(NULL, 0, thread_run, NULL, 0, NULL); 717 1.1 christos return *t != NULL; 718 1.1 christos } 719 1.1 christos 720 1.1 christos static int wait_for_thread(thread_t thread) 721 1.1 christos { 722 1.1 christos return WaitForSingleObject(thread, INFINITE) == 0; 723 1.1 christos } 724 1.1 christos 725 1.1.1.2 christos #else 726 1.1 christos 727 1.1 christos typedef pthread_t thread_t; 728 1.1 christos 729 1.1 christos static void *thread_run(void *arg) 730 1.1 christos { 731 1.1 christos run_multi_thread_test(); 732 1.1 christos /* 733 1.1 christos * Because we're linking with a static library, we must stop each 734 1.1 christos * thread explicitly, or so says OPENSSL_thread_stop(3) 735 1.1 christos */ 736 1.1 christos OPENSSL_thread_stop(); 737 1.1 christos return NULL; 738 1.1 christos } 739 1.1 christos 740 1.1 christos static int run_thread(thread_t *t) 741 1.1 christos { 742 1.1 christos return pthread_create(t, NULL, thread_run, NULL) == 0; 743 1.1 christos } 744 1.1 christos 745 1.1 christos static int wait_for_thread(thread_t thread) 746 1.1 christos { 747 1.1 christos return pthread_join(thread, NULL) == 0; 748 1.1 christos } 749 1.1 christos 750 1.1.1.2 christos #endif 751 1.1 christos 752 1.1 christos /* 753 1.1 christos * The main thread will also run the test, so we'll have THREADS+1 parallel 754 1.1 christos * tests running 755 1.1 christos */ 756 1.1.1.2 christos #define THREADS 3 757 1.1 christos 758 1.1 christos static int test_multi_thread(void) 759 1.1 christos { 760 1.1 christos thread_t t[THREADS]; 761 1.1 christos int i; 762 1.1 christos 763 1.1 christos for (i = 0; i < THREADS; i++) 764 1.1 christos run_thread(&t[i]); 765 1.1 christos run_multi_thread_test(); 766 1.1 christos for (i = 0; i < THREADS; i++) 767 1.1 christos wait_for_thread(t[i]); 768 1.1 christos 769 1.1 christos if (!TEST_true(multi_thread_rand_bytes_succeeded)) 770 1.1 christos return 0; 771 1.1 christos if (!TEST_true(multi_thread_rand_priv_bytes_succeeded)) 772 1.1 christos return 0; 773 1.1 christos 774 1.1 christos return 1; 775 1.1 christos } 776 1.1 christos #endif 777 1.1 christos 778 1.1 christos static EVP_RAND_CTX *new_drbg(EVP_RAND_CTX *parent) 779 1.1 christos { 780 1.1 christos OSSL_PARAM params[2]; 781 1.1 christos EVP_RAND *rand = NULL; 782 1.1 christos EVP_RAND_CTX *drbg = NULL; 783 1.1 christos 784 1.1 christos params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER, 785 1.1.1.2 christos "AES-256-CTR", 0); 786 1.1 christos params[1] = OSSL_PARAM_construct_end(); 787 1.1 christos 788 1.1 christos if (!TEST_ptr(rand = EVP_RAND_fetch(NULL, "CTR-DRBG", NULL)) 789 1.1.1.2 christos || !TEST_ptr(drbg = EVP_RAND_CTX_new(rand, parent)) 790 1.1.1.2 christos || !TEST_true(EVP_RAND_CTX_set_params(drbg, params))) { 791 1.1 christos EVP_RAND_CTX_free(drbg); 792 1.1 christos drbg = NULL; 793 1.1 christos } 794 1.1 christos EVP_RAND_free(rand); 795 1.1 christos return drbg; 796 1.1 christos } 797 1.1 christos 798 1.1 christos static int test_rand_prediction_resistance(void) 799 1.1 christos { 800 1.1 christos EVP_RAND_CTX *x = NULL, *y = NULL, *z = NULL; 801 1.1 christos unsigned char buf1[51], buf2[sizeof(buf1)]; 802 1.1 christos int ret = 0, xreseed, yreseed, zreseed; 803 1.1 christos 804 1.1 christos if (using_fips_rng()) 805 1.1 christos return TEST_skip("CRNGT cannot be disabled"); 806 1.1 christos 807 1.1 christos /* Initialise a three long DRBG chain */ 808 1.1 christos if (!TEST_ptr(x = new_drbg(NULL)) 809 1.1 christos || !TEST_true(disable_crngt(x)) 810 1.1 christos || !TEST_true(EVP_RAND_instantiate(x, 0, 0, NULL, 0, NULL)) 811 1.1 christos || !TEST_ptr(y = new_drbg(x)) 812 1.1 christos || !TEST_true(EVP_RAND_instantiate(y, 0, 0, NULL, 0, NULL)) 813 1.1 christos || !TEST_ptr(z = new_drbg(y)) 814 1.1 christos || !TEST_true(EVP_RAND_instantiate(z, 0, 0, NULL, 0, NULL))) 815 1.1 christos goto err; 816 1.1 christos 817 1.1 christos /* 818 1.1 christos * During a normal reseed, only the last DRBG in the chain should 819 1.1 christos * be reseeded. 820 1.1 christos */ 821 1.1 christos inc_reseed_counter(y); 822 1.1 christos xreseed = reseed_counter(x); 823 1.1 christos yreseed = reseed_counter(y); 824 1.1 christos zreseed = reseed_counter(z); 825 1.1 christos if (!TEST_true(EVP_RAND_reseed(z, 0, NULL, 0, NULL, 0)) 826 1.1 christos || !TEST_int_eq(reseed_counter(x), xreseed) 827 1.1 christos || !TEST_int_eq(reseed_counter(y), yreseed) 828 1.1 christos || !TEST_int_gt(reseed_counter(z), zreseed)) 829 1.1 christos goto err; 830 1.1 christos 831 1.1 christos /* 832 1.1 christos * When prediction resistance is requested, the request should be 833 1.1 christos * propagated to the primary, so that the entire DRBG chain reseeds. 834 1.1 christos */ 835 1.1 christos zreseed = reseed_counter(z); 836 1.1 christos if (!TEST_true(EVP_RAND_reseed(z, 1, NULL, 0, NULL, 0)) 837 1.1 christos || !TEST_int_gt(reseed_counter(x), xreseed) 838 1.1 christos || !TEST_int_gt(reseed_counter(y), yreseed) 839 1.1 christos || !TEST_int_gt(reseed_counter(z), zreseed)) 840 1.1 christos goto err; 841 1.1 christos 842 1.1 christos /* 843 1.1 christos * During a normal generate, only the last DRBG should be reseed */ 844 1.1 christos inc_reseed_counter(y); 845 1.1 christos xreseed = reseed_counter(x); 846 1.1 christos yreseed = reseed_counter(y); 847 1.1 christos zreseed = reseed_counter(z); 848 1.1 christos if (!TEST_true(EVP_RAND_generate(z, buf1, sizeof(buf1), 0, 0, NULL, 0)) 849 1.1 christos || !TEST_int_eq(reseed_counter(x), xreseed) 850 1.1 christos || !TEST_int_eq(reseed_counter(y), yreseed) 851 1.1 christos || !TEST_int_gt(reseed_counter(z), zreseed)) 852 1.1 christos goto err; 853 1.1 christos 854 1.1 christos /* 855 1.1 christos * When a prediction resistant generate is requested, the request 856 1.1 christos * should be propagated to the primary, reseeding the entire DRBG chain. 857 1.1 christos */ 858 1.1 christos zreseed = reseed_counter(z); 859 1.1 christos if (!TEST_true(EVP_RAND_generate(z, buf2, sizeof(buf2), 0, 1, NULL, 0)) 860 1.1 christos || !TEST_int_gt(reseed_counter(x), xreseed) 861 1.1 christos || !TEST_int_gt(reseed_counter(y), yreseed) 862 1.1 christos || !TEST_int_gt(reseed_counter(z), zreseed) 863 1.1 christos || !TEST_mem_ne(buf1, sizeof(buf1), buf2, sizeof(buf2))) 864 1.1 christos goto err; 865 1.1 christos 866 1.1 christos /* Verify that a normal reseed still only reseeds the last DRBG */ 867 1.1 christos inc_reseed_counter(y); 868 1.1 christos xreseed = reseed_counter(x); 869 1.1 christos yreseed = reseed_counter(y); 870 1.1 christos zreseed = reseed_counter(z); 871 1.1 christos if (!TEST_true(EVP_RAND_reseed(z, 0, NULL, 0, NULL, 0)) 872 1.1 christos || !TEST_int_eq(reseed_counter(x), xreseed) 873 1.1 christos || !TEST_int_eq(reseed_counter(y), yreseed) 874 1.1 christos || !TEST_int_gt(reseed_counter(z), zreseed)) 875 1.1 christos goto err; 876 1.1 christos 877 1.1 christos ret = 1; 878 1.1 christos err: 879 1.1 christos EVP_RAND_CTX_free(z); 880 1.1 christos EVP_RAND_CTX_free(y); 881 1.1 christos EVP_RAND_CTX_free(x); 882 1.1 christos return ret; 883 1.1 christos } 884 1.1 christos 885 1.1 christos int setup_tests(void) 886 1.1 christos { 887 1.1 christos ADD_TEST(test_rand_reseed); 888 1.1 christos #if defined(OPENSSL_SYS_UNIX) && !defined(OPENSSL_RAND_SEED_EGD) 889 1.1 christos ADD_ALL_TESTS(test_rand_fork_safety, RANDOM_SIZE); 890 1.1 christos #endif 891 1.1 christos ADD_TEST(test_rand_prediction_resistance); 892 1.1 christos #if defined(OPENSSL_THREADS) 893 1.1 christos ADD_TEST(test_multi_thread); 894 1.1 christos #endif 895 1.1 christos return 1; 896 1.1 christos } 897