fake_random.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. #include <string.h>
  11. #include <openssl/core_names.h>
  12. #include <openssl/rand.h>
  13. #include <openssl/provider.h>
  14. #include "../include/crypto/evp.h"
  15. #include "../../crypto/evp/evp_local.h"
  16. #include "../testutil.h"
  17. typedef struct {
  18. fake_random_generate_cb *cb;
  19. int state;
  20. const char *name;
  21. EVP_RAND_CTX *ctx;
  22. } FAKE_RAND;
  23. static OSSL_FUNC_rand_newctx_fn fake_rand_newctx;
  24. static OSSL_FUNC_rand_freectx_fn fake_rand_freectx;
  25. static OSSL_FUNC_rand_instantiate_fn fake_rand_instantiate;
  26. static OSSL_FUNC_rand_uninstantiate_fn fake_rand_uninstantiate;
  27. static OSSL_FUNC_rand_generate_fn fake_rand_generate;
  28. static OSSL_FUNC_rand_gettable_ctx_params_fn fake_rand_gettable_ctx_params;
  29. static OSSL_FUNC_rand_get_ctx_params_fn fake_rand_get_ctx_params;
  30. static OSSL_FUNC_rand_enable_locking_fn fake_rand_enable_locking;
  31. static void *fake_rand_newctx(void *provctx, void *parent,
  32. const OSSL_DISPATCH *parent_dispatch)
  33. {
  34. FAKE_RAND *r = OPENSSL_zalloc(sizeof(*r));
  35. if (r != NULL)
  36. r->state = EVP_RAND_STATE_UNINITIALISED;
  37. return r;
  38. }
  39. static void fake_rand_freectx(void *vrng)
  40. {
  41. OPENSSL_free(vrng);
  42. }
  43. static int fake_rand_instantiate(void *vrng, ossl_unused unsigned int strength,
  44. ossl_unused int prediction_resistance,
  45. ossl_unused const unsigned char *pstr,
  46. size_t pstr_len,
  47. ossl_unused const OSSL_PARAM params[])
  48. {
  49. FAKE_RAND *frng = (FAKE_RAND *)vrng;
  50. frng->state = EVP_RAND_STATE_READY;
  51. return 1;
  52. }
  53. static int fake_rand_uninstantiate(void *vrng)
  54. {
  55. FAKE_RAND *frng = (FAKE_RAND *)vrng;
  56. frng->state = EVP_RAND_STATE_UNINITIALISED;
  57. return 1;
  58. }
  59. static int fake_rand_generate(void *vrng, unsigned char *out, size_t outlen,
  60. unsigned int strength, int prediction_resistance,
  61. const unsigned char *adin, size_t adinlen)
  62. {
  63. FAKE_RAND *frng = (FAKE_RAND *)vrng;
  64. size_t l;
  65. uint32_t r;
  66. if (frng->cb != NULL)
  67. return (*frng->cb)(out, outlen, frng->name, frng->ctx);
  68. while (outlen > 0) {
  69. r = test_random();
  70. l = outlen < sizeof(r) ? outlen : sizeof(r);
  71. memcpy(out, &r, l);
  72. out += l;
  73. outlen -= l;
  74. }
  75. return 1;
  76. }
  77. static int fake_rand_enable_locking(void *vrng)
  78. {
  79. return 1;
  80. }
  81. static int fake_rand_get_ctx_params(ossl_unused void *vrng, OSSL_PARAM params[])
  82. {
  83. FAKE_RAND *frng = (FAKE_RAND *)vrng;
  84. OSSL_PARAM *p;
  85. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  86. if (p != NULL && !OSSL_PARAM_set_int(p, frng->state))
  87. return 0;
  88. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  89. if (p != NULL && !OSSL_PARAM_set_int(p, 256))
  90. return 0;
  91. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  92. if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX))
  93. return 0;
  94. return 1;
  95. }
  96. static const OSSL_PARAM *fake_rand_gettable_ctx_params(ossl_unused void *vrng,
  97. ossl_unused void *provctx)
  98. {
  99. static const OSSL_PARAM known_gettable_ctx_params[] = {
  100. OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
  101. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
  102. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
  103. OSSL_PARAM_END
  104. };
  105. return known_gettable_ctx_params;
  106. }
  107. static const OSSL_DISPATCH fake_rand_functions[] = {
  108. { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))fake_rand_newctx },
  109. { OSSL_FUNC_RAND_FREECTX, (void (*)(void))fake_rand_freectx },
  110. { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))fake_rand_instantiate },
  111. { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))fake_rand_uninstantiate },
  112. { OSSL_FUNC_RAND_GENERATE, (void (*)(void))fake_rand_generate },
  113. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))fake_rand_enable_locking },
  114. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  115. (void(*)(void))fake_rand_gettable_ctx_params },
  116. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))fake_rand_get_ctx_params },
  117. { 0, NULL }
  118. };
  119. static const OSSL_ALGORITHM fake_rand_rand[] = {
  120. { "FAKE", "provider=fake", fake_rand_functions },
  121. { NULL, NULL, NULL }
  122. };
  123. static const OSSL_ALGORITHM *fake_rand_query(void *provctx,
  124. int operation_id,
  125. int *no_cache)
  126. {
  127. *no_cache = 0;
  128. switch (operation_id) {
  129. case OSSL_OP_RAND:
  130. return fake_rand_rand;
  131. }
  132. return NULL;
  133. }
  134. /* Functions we provide to the core */
  135. static const OSSL_DISPATCH fake_rand_method[] = {
  136. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
  137. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rand_query },
  138. { 0, NULL }
  139. };
  140. static int fake_rand_provider_init(const OSSL_CORE_HANDLE *handle,
  141. const OSSL_DISPATCH *in,
  142. const OSSL_DISPATCH **out, void **provctx)
  143. {
  144. if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
  145. return 0;
  146. *out = fake_rand_method;
  147. return 1;
  148. }
  149. static int check_rng(EVP_RAND_CTX *rng, const char *name)
  150. {
  151. FAKE_RAND *f;
  152. if (!TEST_ptr(rng)) {
  153. TEST_info("random: %s", name);
  154. return 0;
  155. }
  156. f = rng->algctx;
  157. f->name = name;
  158. f->ctx = rng;
  159. return 1;
  160. }
  161. OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx)
  162. {
  163. OSSL_PROVIDER *p;
  164. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rand",
  165. fake_rand_provider_init))
  166. || !TEST_true(RAND_set_DRBG_type(libctx, "fake", NULL, NULL, NULL))
  167. || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rand", 1)))
  168. return NULL;
  169. /* Ensure that the fake rand is initialized. */
  170. if (!TEST_true(check_rng(RAND_get0_primary(libctx), "primary"))
  171. || !TEST_true(check_rng(RAND_get0_private(libctx), "private"))
  172. || !TEST_true(check_rng(RAND_get0_public(libctx), "public"))) {
  173. OSSL_PROVIDER_unload(p);
  174. return NULL;
  175. }
  176. return p;
  177. }
  178. void fake_rand_finish(OSSL_PROVIDER *p)
  179. {
  180. OSSL_PROVIDER_unload(p);
  181. }
  182. void fake_rand_set_callback(EVP_RAND_CTX *rng,
  183. int (*cb)(unsigned char *out, size_t outlen,
  184. const char *name, EVP_RAND_CTX *ctx))
  185. {
  186. if (rng != NULL)
  187. ((FAKE_RAND *)rng->algctx)->cb = cb;
  188. }
  189. void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
  190. int (*cb)(unsigned char *out,
  191. size_t outlen,
  192. const char *name,
  193. EVP_RAND_CTX *ctx))
  194. {
  195. fake_rand_set_callback(RAND_get0_private(libctx), cb);
  196. fake_rand_set_callback(RAND_get0_public(libctx), cb);
  197. }