test_rng.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright 2020-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 <string.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/e_os2.h>
  12. #include <openssl/params.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/err.h>
  16. #include <openssl/randerr.h>
  17. #include "prov/providercommon.h"
  18. #include "prov/provider_ctx.h"
  19. #include "prov/provider_util.h"
  20. #include "prov/implementations.h"
  21. static OSSL_FUNC_rand_newctx_fn test_rng_new;
  22. static OSSL_FUNC_rand_freectx_fn test_rng_free;
  23. static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate;
  24. static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate;
  25. static OSSL_FUNC_rand_generate_fn test_rng_generate;
  26. static OSSL_FUNC_rand_reseed_fn test_rng_reseed;
  27. static OSSL_FUNC_rand_nonce_fn test_rng_nonce;
  28. static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params;
  29. static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params;
  30. static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params;
  31. static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params;
  32. static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization;
  33. static OSSL_FUNC_rand_enable_locking_fn test_rng_enable_locking;
  34. static OSSL_FUNC_rand_lock_fn test_rng_lock;
  35. static OSSL_FUNC_rand_unlock_fn test_rng_unlock;
  36. static OSSL_FUNC_rand_get_seed_fn test_rng_get_seed;
  37. typedef struct {
  38. void *provctx;
  39. int state;
  40. unsigned int strength;
  41. size_t max_request;
  42. unsigned char *entropy, *nonce;
  43. size_t entropy_len, entropy_pos, nonce_len;
  44. CRYPTO_RWLOCK *lock;
  45. } PROV_TEST_RNG;
  46. static void *test_rng_new(void *provctx, void *parent,
  47. const OSSL_DISPATCH *parent_dispatch)
  48. {
  49. PROV_TEST_RNG *t;
  50. t = OPENSSL_zalloc(sizeof(*t));
  51. if (t == NULL)
  52. return NULL;
  53. t->max_request = INT_MAX;
  54. t->provctx = provctx;
  55. t->state = EVP_RAND_STATE_UNINITIALISED;
  56. return t;
  57. }
  58. static void test_rng_free(void *vtest)
  59. {
  60. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  61. if (t == NULL)
  62. return;
  63. OPENSSL_free(t->entropy);
  64. OPENSSL_free(t->nonce);
  65. CRYPTO_THREAD_lock_free(t->lock);
  66. OPENSSL_free(t);
  67. }
  68. static int test_rng_instantiate(void *vtest, unsigned int strength,
  69. int prediction_resistance,
  70. const unsigned char *pstr, size_t pstr_len,
  71. const OSSL_PARAM params[])
  72. {
  73. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  74. if (!test_rng_set_ctx_params(t, params) || strength > t->strength)
  75. return 0;
  76. t->state = EVP_RAND_STATE_READY;
  77. t->entropy_pos = 0;
  78. return 1;
  79. }
  80. static int test_rng_uninstantiate(void *vtest)
  81. {
  82. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  83. t->entropy_pos = 0;
  84. t->state = EVP_RAND_STATE_UNINITIALISED;
  85. return 1;
  86. }
  87. static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen,
  88. unsigned int strength, int prediction_resistance,
  89. const unsigned char *adin, size_t adin_len)
  90. {
  91. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  92. if (strength > t->strength || t->entropy_len - t->entropy_pos < outlen)
  93. return 0;
  94. memcpy(out, t->entropy + t->entropy_pos, outlen);
  95. t->entropy_pos += outlen;
  96. return 1;
  97. }
  98. static int test_rng_reseed(ossl_unused void *vtest,
  99. ossl_unused int prediction_resistance,
  100. ossl_unused const unsigned char *ent,
  101. ossl_unused size_t ent_len,
  102. ossl_unused const unsigned char *adin,
  103. ossl_unused size_t adin_len)
  104. {
  105. return 1;
  106. }
  107. static size_t test_rng_nonce(void *vtest, unsigned char *out,
  108. unsigned int strength,
  109. ossl_unused size_t min_noncelen,
  110. ossl_unused size_t max_noncelen)
  111. {
  112. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  113. if (t->nonce == NULL || strength > t->strength)
  114. return 0;
  115. if (out != NULL)
  116. memcpy(out, t->nonce, t->nonce_len);
  117. return t->nonce_len;
  118. }
  119. static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[])
  120. {
  121. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  122. OSSL_PARAM *p;
  123. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  124. if (p != NULL && !OSSL_PARAM_set_int(p, t->state))
  125. return 0;
  126. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  127. if (p != NULL && !OSSL_PARAM_set_int(p, t->strength))
  128. return 0;
  129. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  130. if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request))
  131. return 0;
  132. return 1;
  133. }
  134. static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *vtest,
  135. ossl_unused void *provctx)
  136. {
  137. static const OSSL_PARAM known_gettable_ctx_params[] = {
  138. OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
  139. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
  140. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
  141. OSSL_PARAM_END
  142. };
  143. return known_gettable_ctx_params;
  144. }
  145. static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[])
  146. {
  147. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  148. const OSSL_PARAM *p;
  149. void *ptr = NULL;
  150. size_t size = 0;
  151. if (params == NULL)
  152. return 1;
  153. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
  154. if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength))
  155. return 0;
  156. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
  157. if (p != NULL) {
  158. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  159. return 0;
  160. OPENSSL_free(t->entropy);
  161. t->entropy = ptr;
  162. t->entropy_len = size;
  163. t->entropy_pos = 0;
  164. ptr = NULL;
  165. }
  166. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
  167. if (p != NULL) {
  168. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  169. return 0;
  170. OPENSSL_free(t->nonce);
  171. t->nonce = ptr;
  172. t->nonce_len = size;
  173. }
  174. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST);
  175. if (p != NULL && !OSSL_PARAM_get_size_t(p, &t->max_request))
  176. return 0;
  177. return 1;
  178. }
  179. static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *vtest,
  180. ossl_unused void *provctx)
  181. {
  182. static const OSSL_PARAM known_settable_ctx_params[] = {
  183. OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
  184. OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
  185. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
  186. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
  187. OSSL_PARAM_END
  188. };
  189. return known_settable_ctx_params;
  190. }
  191. static int test_rng_verify_zeroization(ossl_unused void *vtest)
  192. {
  193. return 1;
  194. }
  195. static size_t test_rng_get_seed(void *vtest, unsigned char **pout,
  196. int entropy, size_t min_len, size_t max_len,
  197. ossl_unused int prediction_resistance,
  198. ossl_unused const unsigned char *adin,
  199. ossl_unused size_t adin_len)
  200. {
  201. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  202. *pout = t->entropy;
  203. return t->entropy_len > max_len ? max_len : t->entropy_len;
  204. }
  205. static int test_rng_enable_locking(void *vtest)
  206. {
  207. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  208. if (t != NULL && t->lock == NULL) {
  209. t->lock = CRYPTO_THREAD_lock_new();
  210. if (t->lock == NULL) {
  211. ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK);
  212. return 0;
  213. }
  214. }
  215. return 1;
  216. }
  217. static int test_rng_lock(void *vtest)
  218. {
  219. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  220. if (t == NULL || t->lock == NULL)
  221. return 1;
  222. return CRYPTO_THREAD_write_lock(t->lock);
  223. }
  224. static void test_rng_unlock(void *vtest)
  225. {
  226. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  227. if (t != NULL && t->lock != NULL)
  228. CRYPTO_THREAD_unlock(t->lock);
  229. }
  230. const OSSL_DISPATCH ossl_test_rng_functions[] = {
  231. { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new },
  232. { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
  233. { OSSL_FUNC_RAND_INSTANTIATE,
  234. (void(*)(void))test_rng_instantiate },
  235. { OSSL_FUNC_RAND_UNINSTANTIATE,
  236. (void(*)(void))test_rng_uninstantiate },
  237. { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate },
  238. { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed },
  239. { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
  240. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking },
  241. { OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock },
  242. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock },
  243. { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
  244. (void(*)(void))test_rng_settable_ctx_params },
  245. { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
  246. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  247. (void(*)(void))test_rng_gettable_ctx_params },
  248. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
  249. { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
  250. (void(*)(void))test_rng_verify_zeroization },
  251. { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))test_rng_get_seed },
  252. { 0, NULL }
  253. };