xref: /freebsd/crypto/openssl/test/rand_test.c (revision e0c4386e)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the >License>).  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <openssl/evp.h>
11*e0c4386eSCy Schubert #include <openssl/rand.h>
12*e0c4386eSCy Schubert #include <openssl/bio.h>
13*e0c4386eSCy Schubert #include <openssl/core_names.h>
14*e0c4386eSCy Schubert #include "testutil.h"
15*e0c4386eSCy Schubert 
test_rand(void)16*e0c4386eSCy Schubert static int test_rand(void)
17*e0c4386eSCy Schubert {
18*e0c4386eSCy Schubert     EVP_RAND_CTX *privctx;
19*e0c4386eSCy Schubert     OSSL_PARAM params[2], *p = params;
20*e0c4386eSCy Schubert     unsigned char entropy1[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
21*e0c4386eSCy Schubert     unsigned char entropy2[] = { 0xff, 0xfe, 0xfd };
22*e0c4386eSCy Schubert     unsigned char outbuf[3];
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert     *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
25*e0c4386eSCy Schubert                                              entropy1, sizeof(entropy1));
26*e0c4386eSCy Schubert     *p = OSSL_PARAM_construct_end();
27*e0c4386eSCy Schubert 
28*e0c4386eSCy Schubert     if (!TEST_ptr(privctx = RAND_get0_private(NULL))
29*e0c4386eSCy Schubert             || !TEST_true(EVP_RAND_CTX_set_params(privctx, params))
30*e0c4386eSCy Schubert             || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
31*e0c4386eSCy Schubert             || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy1, sizeof(outbuf))
32*e0c4386eSCy Schubert             || !TEST_int_le(RAND_priv_bytes(outbuf, sizeof(outbuf) + 1), 0)
33*e0c4386eSCy Schubert             || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
34*e0c4386eSCy Schubert             || !TEST_mem_eq(outbuf, sizeof(outbuf),
35*e0c4386eSCy Schubert                             entropy1 + sizeof(outbuf), sizeof(outbuf)))
36*e0c4386eSCy Schubert         return 0;
37*e0c4386eSCy Schubert 
38*e0c4386eSCy Schubert     *params = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
39*e0c4386eSCy Schubert                                                 entropy2, sizeof(entropy2));
40*e0c4386eSCy Schubert     if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params))
41*e0c4386eSCy Schubert             || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
42*e0c4386eSCy Schubert             || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy2, sizeof(outbuf)))
43*e0c4386eSCy Schubert         return 0;
44*e0c4386eSCy Schubert     return 1;
45*e0c4386eSCy Schubert }
46*e0c4386eSCy Schubert 
setup_tests(void)47*e0c4386eSCy Schubert int setup_tests(void)
48*e0c4386eSCy Schubert {
49*e0c4386eSCy Schubert     if (!TEST_true(RAND_set_DRBG_type(NULL, "TEST-RAND", NULL, NULL, NULL)))
50*e0c4386eSCy Schubert         return 0;
51*e0c4386eSCy Schubert     ADD_TEST(test_rand);
52*e0c4386eSCy Schubert     return 1;
53*e0c4386eSCy Schubert }
54