1
0

rand_test.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2021-2025 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 "crypto/rand.h"
  14. #include "testutil.h"
  15. static int test_rand(void)
  16. {
  17. EVP_RAND_CTX *privctx;
  18. OSSL_PARAM params[2], *p = params;
  19. unsigned char entropy1[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
  20. unsigned char entropy2[] = { 0xff, 0xfe, 0xfd };
  21. unsigned char nonce[] = { 0x00, 0x01, 0x02, 0x03, 0x04 };
  22. unsigned char outbuf[3];
  23. *p++ = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  24. entropy1, sizeof(entropy1));
  25. *p = OSSL_PARAM_construct_end();
  26. if (!TEST_ptr(privctx = RAND_get0_private(NULL))
  27. || !TEST_true(EVP_RAND_CTX_set_params(privctx, params))
  28. || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
  29. || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy1, sizeof(outbuf))
  30. || !TEST_int_le(RAND_priv_bytes(outbuf, sizeof(outbuf) + 1), 0)
  31. || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
  32. || !TEST_mem_eq(outbuf, sizeof(outbuf),
  33. entropy1 + sizeof(outbuf), sizeof(outbuf)))
  34. return 0;
  35. *params = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY,
  36. entropy2, sizeof(entropy2));
  37. if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params))
  38. || !TEST_int_gt(RAND_priv_bytes(outbuf, sizeof(outbuf)), 0)
  39. || !TEST_mem_eq(outbuf, sizeof(outbuf), entropy2, sizeof(outbuf)))
  40. return 0;
  41. *params = OSSL_PARAM_construct_octet_string(OSSL_RAND_PARAM_TEST_NONCE,
  42. nonce, sizeof(nonce));
  43. if (!TEST_true(EVP_RAND_CTX_set_params(privctx, params))
  44. || !TEST_true(EVP_RAND_nonce(privctx, outbuf, sizeof(outbuf)))
  45. || !TEST_mem_eq(outbuf, sizeof(outbuf), nonce, sizeof(outbuf)))
  46. return 0;
  47. return 1;
  48. }
  49. static int test_rand_uniform(void)
  50. {
  51. uint32_t x, i, j;
  52. int err = 0, res = 0;
  53. OSSL_LIB_CTX *ctx;
  54. if (!test_get_libctx(&ctx, NULL, NULL, NULL, NULL))
  55. goto err;
  56. for (i = 1; i < 100; i += 13) {
  57. x = ossl_rand_uniform_uint32(ctx, i, &err);
  58. if (!TEST_int_eq(err, 0)
  59. || !TEST_uint_ge(x, 0)
  60. || !TEST_uint_lt(x, i))
  61. return 0;
  62. }
  63. for (i = 1; i < 100; i += 17)
  64. for (j = i + 1; j < 150; j += 11) {
  65. x = ossl_rand_range_uint32(ctx, i, j, &err);
  66. if (!TEST_int_eq(err, 0)
  67. || !TEST_uint_ge(x, i)
  68. || !TEST_uint_lt(x, j))
  69. return 0;
  70. }
  71. res = 1;
  72. err:
  73. OSSL_LIB_CTX_free(ctx);
  74. return res;
  75. }
  76. int setup_tests(void)
  77. {
  78. if (!TEST_true(RAND_set_DRBG_type(NULL, "TEST-RAND", NULL, NULL, NULL)))
  79. return 0;
  80. ADD_TEST(test_rand);
  81. ADD_TEST(test_rand_uniform);
  82. return 1;
  83. }