A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
9.5 KiB

  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. if (parent != NULL)
  51. return NULL;
  52. t = OPENSSL_zalloc(sizeof(*t));
  53. if (t == NULL)
  54. return NULL;
  55. t->max_request = INT_MAX;
  56. t->provctx = provctx;
  57. t->state = EVP_RAND_STATE_UNINITIALISED;
  58. return t;
  59. }
  60. static void test_rng_free(void *vtest)
  61. {
  62. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  63. if (t == NULL)
  64. return;
  65. OPENSSL_free(t->entropy);
  66. OPENSSL_free(t->nonce);
  67. CRYPTO_THREAD_lock_free(t->lock);
  68. OPENSSL_free(t);
  69. }
  70. static int test_rng_instantiate(void *vtest, unsigned int strength,
  71. int prediction_resistance,
  72. const unsigned char *pstr, size_t pstr_len,
  73. const OSSL_PARAM params[])
  74. {
  75. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  76. if (!test_rng_set_ctx_params(t, params) || strength > t->strength)
  77. return 0;
  78. t->state = EVP_RAND_STATE_READY;
  79. t->entropy_pos = 0;
  80. return 1;
  81. }
  82. static int test_rng_uninstantiate(void *vtest)
  83. {
  84. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  85. t->entropy_pos = 0;
  86. t->state = EVP_RAND_STATE_UNINITIALISED;
  87. return 1;
  88. }
  89. static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen,
  90. unsigned int strength, int prediction_resistance,
  91. const unsigned char *adin, size_t adin_len)
  92. {
  93. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  94. size_t i;
  95. if (strength > t->strength)
  96. return 0;
  97. for (i = 0; i < outlen; i++) {
  98. out[i] = t->entropy[t->entropy_pos++];
  99. if (t->entropy_pos >= t->entropy_len)
  100. break;
  101. }
  102. return 1;
  103. }
  104. static int test_rng_reseed(ossl_unused void *vtest,
  105. ossl_unused int prediction_resistance,
  106. ossl_unused const unsigned char *ent,
  107. ossl_unused size_t ent_len,
  108. ossl_unused const unsigned char *adin,
  109. ossl_unused size_t adin_len)
  110. {
  111. return 1;
  112. }
  113. static size_t test_rng_nonce(void *vtest, unsigned char *out,
  114. unsigned int strength,
  115. ossl_unused size_t min_noncelen,
  116. ossl_unused size_t max_noncelen)
  117. {
  118. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  119. if (t->nonce == NULL || strength > t->strength)
  120. return 0;
  121. if (out != NULL)
  122. memcpy(out, t->nonce, t->nonce_len);
  123. return t->nonce_len;
  124. }
  125. static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[])
  126. {
  127. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  128. OSSL_PARAM *p;
  129. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  130. if (p != NULL && !OSSL_PARAM_set_int(p, t->state))
  131. return 0;
  132. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  133. if (p != NULL && !OSSL_PARAM_set_int(p, t->strength))
  134. return 0;
  135. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  136. if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request))
  137. return 0;
  138. return 1;
  139. }
  140. static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *vtest,
  141. ossl_unused void *provctx)
  142. {
  143. static const OSSL_PARAM known_gettable_ctx_params[] = {
  144. OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
  145. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
  146. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
  147. OSSL_PARAM_END
  148. };
  149. return known_gettable_ctx_params;
  150. }
  151. static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[])
  152. {
  153. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  154. const OSSL_PARAM *p;
  155. void *ptr = NULL;
  156. size_t size = 0;
  157. if (params == NULL)
  158. return 1;
  159. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
  160. if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength))
  161. return 0;
  162. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
  163. if (p != NULL) {
  164. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  165. return 0;
  166. OPENSSL_free(t->entropy);
  167. t->entropy = ptr;
  168. t->entropy_len = size;
  169. t->entropy_pos = 0;
  170. ptr = NULL;
  171. }
  172. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
  173. if (p != NULL) {
  174. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  175. return 0;
  176. OPENSSL_free(t->nonce);
  177. t->nonce = ptr;
  178. t->nonce_len = size;
  179. }
  180. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST);
  181. if (p != NULL && !OSSL_PARAM_get_size_t(p, &t->max_request))
  182. return 0;
  183. return 1;
  184. }
  185. static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *vtest,
  186. ossl_unused void *provctx)
  187. {
  188. static const OSSL_PARAM known_settable_ctx_params[] = {
  189. OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
  190. OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
  191. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
  192. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
  193. OSSL_PARAM_END
  194. };
  195. return known_settable_ctx_params;
  196. }
  197. static int test_rng_verify_zeroization(ossl_unused void *vtest)
  198. {
  199. return 1;
  200. }
  201. static size_t test_rng_get_seed(void *vtest, unsigned char **pout,
  202. int entropy, size_t min_len, size_t max_len,
  203. ossl_unused int prediction_resistance,
  204. ossl_unused const unsigned char *adin,
  205. ossl_unused size_t adin_len)
  206. {
  207. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  208. *pout = t->entropy;
  209. return t->entropy_len > max_len ? max_len : t->entropy_len;
  210. }
  211. static int test_rng_enable_locking(void *vtest)
  212. {
  213. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  214. if (t != NULL && t->lock == NULL) {
  215. t->lock = CRYPTO_THREAD_lock_new();
  216. if (t->lock == NULL) {
  217. ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK);
  218. return 0;
  219. }
  220. }
  221. return 1;
  222. }
  223. static int test_rng_lock(void *vtest)
  224. {
  225. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  226. if (t == NULL || t->lock == NULL)
  227. return 1;
  228. return CRYPTO_THREAD_write_lock(t->lock);
  229. }
  230. static void test_rng_unlock(void *vtest)
  231. {
  232. PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
  233. if (t != NULL && t->lock != NULL)
  234. CRYPTO_THREAD_unlock(t->lock);
  235. }
  236. const OSSL_DISPATCH ossl_test_rng_functions[] = {
  237. { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new },
  238. { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
  239. { OSSL_FUNC_RAND_INSTANTIATE,
  240. (void(*)(void))test_rng_instantiate },
  241. { OSSL_FUNC_RAND_UNINSTANTIATE,
  242. (void(*)(void))test_rng_uninstantiate },
  243. { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate },
  244. { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed },
  245. { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
  246. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking },
  247. { OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock },
  248. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock },
  249. { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
  250. (void(*)(void))test_rng_settable_ctx_params },
  251. { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
  252. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  253. (void(*)(void))test_rng_gettable_ctx_params },
  254. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
  255. { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
  256. (void(*)(void))test_rng_verify_zeroization },
  257. { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))test_rng_get_seed },
  258. { 0, NULL }
  259. };