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