rand_test.c 2.0 KB

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