• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
test_rand(void)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 nonce[] = { 0x00, 0x01, 0x02, 0x03, 0x04 };
23     unsigned char outbuf[3];
24 
25     *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
26                                              entropy1, sizeof(entropy1));
27     *p = OSSL_PARAM_construct_end();
28 
29     if (!TEST_ptr(privctx = RAND_get0_private(NULL))
30             || !TEST_true(EVP_RAND_CTX_set_params(privctx, params))
31             || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
32             || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy1, sizeof(outbuf))
33             || !TEST_int_le(RAND_priv_bytes(outbuf, sizeof(outbuf) + 1), 0)
34             || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
35             || !TEST_mem_eq(outbuf, sizeof(outbuf),
36                             entropy1 + sizeof(outbuf), sizeof(outbuf)))
37         return 0;
38 
39     *params = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
40                                                 entropy2, sizeof(entropy2));
41     if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params))
42             || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
43             || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy2, sizeof(outbuf)))
44         return 0;
45 
46     *params = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
47                                                 nonce, sizeof(nonce));
48     if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params))
49             || !TEST_true(EVP_RAND_nonce(privctx, outbuf, sizeof(outbuf)))
50             || !TEST_mem_eq(outbuf, sizeof(outbuf), nonce, sizeof(outbuf)))
51         return 0;
52 
53     return 1;
54 }
55 
setup_tests(void)56 int setup_tests(void)
57 {
58     if (!TEST_true(RAND_set_DRBG_type(NULL, "TEST-RAND", NULL, NULL, NULL)))
59         return 0;
60     ADD_TEST(test_rand);
61     return 1;
62 }
63