test_rng.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright 2020 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 "prov/providercommon.h"
  14. #include "prov/provider_ctx.h"
  15. #include "prov/provider_util.h"
  16. #include "prov/implementations.h"
  17. #include "drbg_local.h"
  18. static OSSL_FUNC_rand_newctx_fn test_rng_new_wrapper;
  19. static OSSL_FUNC_rand_freectx_fn test_rng_free;
  20. static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate_wrapper;
  21. static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate_wrapper;
  22. static OSSL_FUNC_rand_generate_fn test_rng_generate_wrapper;
  23. static OSSL_FUNC_rand_reseed_fn test_rng_reseed_wrapper;
  24. static OSSL_FUNC_rand_nonce_fn test_rng_nonce;
  25. static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params;
  26. static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params;
  27. static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params;
  28. static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params;
  29. static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization;
  30. typedef struct {
  31. unsigned char *entropy, *nonce;
  32. size_t entropy_len, entropy_pos, nonce_len;
  33. unsigned int strength;
  34. } PROV_TEST_RNG;
  35. static int test_rng_new(PROV_DRBG *ctx)
  36. {
  37. PROV_TEST_RNG *t;
  38. t = OPENSSL_zalloc(sizeof(*t));
  39. if (t == NULL)
  40. return 0;
  41. ctx->data = t;
  42. ctx->seedlen = INT_MAX;
  43. ctx->max_entropylen = INT_MAX;
  44. ctx->max_noncelen = INT_MAX;
  45. ctx->max_perslen = INT_MAX;
  46. ctx->max_adinlen = INT_MAX;
  47. ctx->max_request = INT_MAX;
  48. return 1;
  49. }
  50. static void test_rng_free(void *vdrbg)
  51. {
  52. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  53. PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
  54. OPENSSL_free(t->entropy);
  55. OPENSSL_free(t->nonce);
  56. OPENSSL_free(drbg->data);
  57. prov_rand_drbg_free(drbg);
  58. }
  59. static int test_rng_instantiate(PROV_DRBG *drbg,
  60. const unsigned char *ent, size_t ent_len,
  61. const unsigned char *nonce, size_t nonce_len,
  62. const unsigned char *pstr, size_t pstr_len)
  63. {
  64. PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
  65. if (ent != NULL && (ent_len < drbg->min_entropylen
  66. || ent_len >= drbg->max_entropylen))
  67. return 0;
  68. if (nonce != NULL && (nonce_len < drbg->min_noncelen
  69. || nonce_len >= drbg->max_noncelen))
  70. return 0;
  71. if (pstr != NULL && pstr_len >= drbg->max_perslen)
  72. return 0;
  73. t->entropy_pos = 0;
  74. return 1;
  75. }
  76. static int test_rng_instantiate_wrapper(void *vdrbg, unsigned int strength,
  77. int prediction_resistance,
  78. const unsigned char *pstr,
  79. size_t pstr_len)
  80. {
  81. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  82. if (pstr != NULL && pstr_len >= drbg->max_perslen)
  83. return 0;
  84. return PROV_DRBG_instantiate(drbg, strength, prediction_resistance,
  85. pstr, pstr_len);
  86. }
  87. static int test_rng_uninstantiate(PROV_DRBG *drbg)
  88. {
  89. PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
  90. t->entropy_pos = 0;
  91. return PROV_DRBG_uninstantiate(drbg);
  92. }
  93. static int test_rng_uninstantiate_wrapper(void *vdrbg)
  94. {
  95. return test_rng_uninstantiate((PROV_DRBG *)vdrbg);
  96. }
  97. static int test_rng_generate(PROV_DRBG *drbg,
  98. unsigned char *out, size_t outlen,
  99. const unsigned char *adin, size_t adin_len)
  100. {
  101. PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
  102. size_t i;
  103. if (t->entropy == NULL || (adin != NULL && adin_len >= drbg->max_adinlen))
  104. return 0;
  105. for (i = 0; i < outlen; i++) {
  106. out[i] = t->entropy[t->entropy_pos++];
  107. if (t->entropy_pos >= t->entropy_len)
  108. break;
  109. }
  110. return 1;
  111. }
  112. static int test_rng_generate_wrapper
  113. (void *vdrbg, unsigned char *out, size_t outlen,
  114. unsigned int strength, int prediction_resistance,
  115. const unsigned char *adin, size_t adin_len)
  116. {
  117. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  118. if (strength > drbg->strength)
  119. return 0;
  120. return test_rng_generate(drbg, out, outlen, adin, adin_len);
  121. }
  122. static int test_rng_reseed(PROV_DRBG *drbg,
  123. const unsigned char *ent, size_t ent_len,
  124. const unsigned char *adin, size_t adin_len)
  125. {
  126. if (ent != NULL && (ent_len < drbg->min_entropylen
  127. || ent_len >= drbg->max_entropylen))
  128. return 0;
  129. if (adin != NULL && adin_len >= drbg->max_adinlen)
  130. return 0;
  131. return 1;
  132. }
  133. static int test_rng_reseed_wrapper(void *vdrbg, int prediction_resistance,
  134. const unsigned char *ent, size_t ent_len,
  135. const unsigned char *adin, size_t adin_len)
  136. {
  137. return test_rng_reseed((PROV_DRBG *)vdrbg, ent, ent_len, adin, adin_len);
  138. }
  139. static size_t test_rng_nonce(void *vdrbg, unsigned char *out,
  140. unsigned int strength, size_t min_noncelen,
  141. size_t max_noncelen)
  142. {
  143. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  144. PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
  145. if (t->nonce == NULL
  146. || strength > drbg->strength
  147. || min_noncelen > t->nonce_len
  148. || max_noncelen < t->nonce_len)
  149. return 0;
  150. if (out != NULL)
  151. memcpy(out, t->nonce, t->nonce_len);
  152. return t->nonce_len;
  153. }
  154. static int test_rng_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
  155. {
  156. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  157. return drbg_get_ctx_params(drbg, params);
  158. }
  159. static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *provctx)
  160. {
  161. static const OSSL_PARAM known_gettable_ctx_params[] = {
  162. OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
  163. OSSL_PARAM_END
  164. };
  165. return known_gettable_ctx_params;
  166. }
  167. static int set_size_t(const OSSL_PARAM *params, const char *name,
  168. size_t *val)
  169. {
  170. const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, name);
  171. return p == NULL || OSSL_PARAM_get_size_t(p, val);
  172. }
  173. static int test_rng_set_ctx_params(void *vdrbg, const OSSL_PARAM params[])
  174. {
  175. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  176. PROV_TEST_RNG *t = (PROV_TEST_RNG *)drbg->data;
  177. const OSSL_PARAM *p;
  178. void *ptr = NULL;
  179. size_t size = 0;
  180. unsigned int uint;
  181. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
  182. if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->strength))
  183. return 0;
  184. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
  185. if (p != NULL) {
  186. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  187. return 0;
  188. OPENSSL_free(t->entropy);
  189. t->entropy = ptr;
  190. t->entropy_len = size;
  191. t->entropy_pos = 0;
  192. ptr = NULL;
  193. }
  194. p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
  195. if (p != NULL) {
  196. if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
  197. return 0;
  198. OPENSSL_free(t->nonce);
  199. t->nonce = ptr;
  200. t->nonce_len = size;
  201. }
  202. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_CTR);
  203. if (p != NULL) {
  204. if (!OSSL_PARAM_get_uint(p, &uint))
  205. return 0;
  206. tsan_store(&drbg->reseed_counter, uint);
  207. }
  208. p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME);
  209. if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time))
  210. return 0;
  211. if (!set_size_t(params, OSSL_DRBG_PARAM_MAX_REQUEST, &drbg->max_request)
  212. || !set_size_t(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN,
  213. &drbg->min_entropylen)
  214. || !set_size_t(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN,
  215. &drbg->max_entropylen)
  216. || !set_size_t(params, OSSL_DRBG_PARAM_MIN_NONCELEN,
  217. &drbg->min_noncelen)
  218. || !set_size_t(params, OSSL_DRBG_PARAM_MAX_NONCELEN,
  219. &drbg->max_noncelen)
  220. || !set_size_t(params, OSSL_DRBG_PARAM_MAX_PERSLEN,
  221. &drbg->max_perslen)
  222. || !set_size_t(params, OSSL_DRBG_PARAM_MAX_ADINLEN,
  223. &drbg->max_adinlen))
  224. return 0;
  225. return drbg_set_ctx_params(drbg, params);
  226. }
  227. static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *provctx)
  228. {
  229. static const OSSL_PARAM known_settable_ctx_params[] = {
  230. OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
  231. OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
  232. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
  233. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_REQUEST, NULL),
  234. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_ENTROPYLEN, NULL),
  235. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ENTROPYLEN, NULL),
  236. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MIN_NONCELEN, NULL),
  237. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_NONCELEN, NULL),
  238. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_PERSLEN, NULL),
  239. OSSL_PARAM_size_t(OSSL_DRBG_PARAM_MAX_ADINLEN, NULL),
  240. OSSL_PARAM_uint(OSSL_DRBG_PARAM_RESEED_CTR, NULL),
  241. OSSL_PARAM_time_t(OSSL_DRBG_PARAM_RESEED_TIME, NULL),
  242. OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
  243. OSSL_PARAM_END
  244. };
  245. return known_settable_ctx_params;
  246. }
  247. static int test_rng_verify_zeroization(void *vdrbg)
  248. {
  249. return 1;
  250. }
  251. static void *test_rng_new_wrapper(void *provctx, void *parent,
  252. const OSSL_DISPATCH *parent_dispatch)
  253. {
  254. return prov_rand_drbg_new(provctx, parent, parent_dispatch,
  255. &test_rng_new, &test_rng_instantiate,
  256. &test_rng_uninstantiate, &test_rng_reseed,
  257. &test_rng_generate);
  258. }
  259. const OSSL_DISPATCH test_rng_functions[] = {
  260. { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new_wrapper },
  261. { OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
  262. { OSSL_FUNC_RAND_INSTANTIATE,
  263. (void(*)(void))test_rng_instantiate_wrapper },
  264. { OSSL_FUNC_RAND_UNINSTANTIATE,
  265. (void(*)(void))test_rng_uninstantiate_wrapper },
  266. { OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate_wrapper },
  267. { OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed_wrapper },
  268. { OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
  269. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))drbg_enable_locking },
  270. { OSSL_FUNC_RAND_LOCK, (void(*)(void))drbg_lock },
  271. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))drbg_unlock },
  272. { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
  273. (void(*)(void))test_rng_settable_ctx_params },
  274. { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
  275. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  276. (void(*)(void))test_rng_gettable_ctx_params },
  277. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
  278. { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
  279. (void(*)(void))test_rng_verify_zeroization },
  280. { 0, NULL }
  281. };