1 /* 2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (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 <openssl/evp.h> 11 #include <openssl/rand.h> 12 #include <openssl/bio.h> 13 #include <openssl/core_names.h> 14 #include "testutil.h" 15 16 static int test_rand(void) 17 { 18 EVP_RAND_CTX *privctx; 19 OSSL_PARAM params[2], *p = params; 20 unsigned char entropy1[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }; 21 unsigned char entropy2[] = { 0xff, 0xfe, 0xfd }; 22 unsigned char outbuf[3]; 23 24 *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, 25 entropy1, sizeof(entropy1)); 26 *p = OSSL_PARAM_construct_end(); 27 28 if (!TEST_ptr(privctx = RAND_get0_private(NULL)) 29 || !TEST_true(EVP_RAND_CTX_set_params(privctx, params)) 30 || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0) 31 || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy1, sizeof(outbuf)) 32 || !TEST_int_le(RAND_priv_bytes(outbuf, sizeof(outbuf) + 1), 0) 33 || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0) 34 || !TEST_mem_eq(outbuf, sizeof(outbuf), 35 entropy1 + sizeof(outbuf), sizeof(outbuf))) 36 return 0; 37 38 *params = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, 39 entropy2, sizeof(entropy2)); 40 if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params)) 41 || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0) 42 || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy2, sizeof(outbuf))) 43 return 0; 44 return 1; 45 } 46 47 int setup_tests(void) 48 { 49 if (!TEST_true(RAND_set_DRBG_type(NULL, "TEST-RAND", NULL, NULL, NULL))) 50 return 0; 51 ADD_TEST(test_rand); 52 return 1; 53 } 54