seed_src.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/rand.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/e_os2.h>
  13. #include <openssl/params.h>
  14. #include <openssl/core_names.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/err.h>
  17. #include <openssl/randerr.h>
  18. #include <openssl/proverr.h>
  19. #include "prov/implementations.h"
  20. #include "prov/provider_ctx.h"
  21. #include "crypto/rand.h"
  22. #include "crypto/rand_pool.h"
  23. static OSSL_FUNC_rand_newctx_fn seed_src_new;
  24. static OSSL_FUNC_rand_freectx_fn seed_src_free;
  25. static OSSL_FUNC_rand_instantiate_fn seed_src_instantiate;
  26. static OSSL_FUNC_rand_uninstantiate_fn seed_src_uninstantiate;
  27. static OSSL_FUNC_rand_generate_fn seed_src_generate;
  28. static OSSL_FUNC_rand_reseed_fn seed_src_reseed;
  29. static OSSL_FUNC_rand_gettable_ctx_params_fn seed_src_gettable_ctx_params;
  30. static OSSL_FUNC_rand_get_ctx_params_fn seed_src_get_ctx_params;
  31. static OSSL_FUNC_rand_verify_zeroization_fn seed_src_verify_zeroization;
  32. static OSSL_FUNC_rand_enable_locking_fn seed_src_enable_locking;
  33. static OSSL_FUNC_rand_lock_fn seed_src_lock;
  34. static OSSL_FUNC_rand_unlock_fn seed_src_unlock;
  35. static OSSL_FUNC_rand_get_seed_fn seed_get_seed;
  36. static OSSL_FUNC_rand_clear_seed_fn seed_clear_seed;
  37. typedef struct {
  38. void *provctx;
  39. int state;
  40. } PROV_SEED_SRC;
  41. static void *seed_src_new(void *provctx, void *parent,
  42. const OSSL_DISPATCH *parent_dispatch)
  43. {
  44. PROV_SEED_SRC *s;
  45. if (parent != NULL) {
  46. ERR_raise(ERR_LIB_PROV, PROV_R_SEED_SOURCES_MUST_NOT_HAVE_A_PARENT);
  47. return NULL;
  48. }
  49. s = OPENSSL_zalloc(sizeof(*s));
  50. if (s == NULL) {
  51. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  52. return NULL;
  53. }
  54. s->provctx = provctx;
  55. s->state = EVP_RAND_STATE_UNINITIALISED;
  56. return s;
  57. }
  58. static void seed_src_free(void *vseed)
  59. {
  60. OPENSSL_free(vseed);
  61. }
  62. static int seed_src_instantiate(void *vseed, unsigned int strength,
  63. int prediction_resistance,
  64. const unsigned char *pstr, size_t pstr_len,
  65. ossl_unused const OSSL_PARAM params[])
  66. {
  67. PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
  68. s->state = EVP_RAND_STATE_READY;
  69. return 1;
  70. }
  71. static int seed_src_uninstantiate(void *vseed)
  72. {
  73. PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
  74. s->state = EVP_RAND_STATE_UNINITIALISED;
  75. return 1;
  76. }
  77. static int seed_src_generate(void *vseed, unsigned char *out, size_t outlen,
  78. unsigned int strength,
  79. ossl_unused int prediction_resistance,
  80. ossl_unused const unsigned char *adin,
  81. ossl_unused size_t adin_len)
  82. {
  83. PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
  84. size_t entropy_available;
  85. RAND_POOL *pool;
  86. if (s->state != EVP_RAND_STATE_READY) {
  87. ERR_raise(ERR_LIB_PROV,
  88. s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
  89. : PROV_R_NOT_INSTANTIATED);
  90. return 0;
  91. }
  92. pool = ossl_rand_pool_new(strength, 1, outlen, outlen);
  93. if (pool == NULL) {
  94. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  95. return 0;
  96. }
  97. /* Get entropy by polling system entropy sources. */
  98. entropy_available = ossl_pool_acquire_entropy(pool);
  99. if (entropy_available > 0)
  100. memcpy(out, ossl_rand_pool_buffer(pool), ossl_rand_pool_length(pool));
  101. ossl_rand_pool_free(pool);
  102. return entropy_available > 0;
  103. }
  104. static int seed_src_reseed(void *vseed,
  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. PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
  112. if (s->state != EVP_RAND_STATE_READY) {
  113. ERR_raise(ERR_LIB_PROV,
  114. s->state == EVP_RAND_STATE_ERROR ? PROV_R_IN_ERROR_STATE
  115. : PROV_R_NOT_INSTANTIATED);
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. static int seed_src_get_ctx_params(void *vseed, OSSL_PARAM params[])
  121. {
  122. PROV_SEED_SRC *s = (PROV_SEED_SRC *)vseed;
  123. OSSL_PARAM *p;
  124. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
  125. if (p != NULL && !OSSL_PARAM_set_int(p, s->state))
  126. return 0;
  127. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
  128. if (p != NULL && !OSSL_PARAM_set_int(p, 1024))
  129. return 0;
  130. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  131. if (p != NULL && !OSSL_PARAM_set_size_t(p, 128))
  132. return 0;
  133. return 1;
  134. }
  135. static const OSSL_PARAM *seed_src_gettable_ctx_params(ossl_unused void *vseed,
  136. ossl_unused void *provctx)
  137. {
  138. static const OSSL_PARAM known_gettable_ctx_params[] = {
  139. OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
  140. OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
  141. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
  142. OSSL_PARAM_END
  143. };
  144. return known_gettable_ctx_params;
  145. }
  146. static int seed_src_verify_zeroization(ossl_unused void *vseed)
  147. {
  148. return 1;
  149. }
  150. static size_t seed_get_seed(void *vseed, unsigned char **pout,
  151. int entropy, size_t min_len, size_t max_len,
  152. int prediction_resistance,
  153. const unsigned char *adin, size_t adin_len)
  154. {
  155. size_t bytes_needed;
  156. unsigned char *p;
  157. /*
  158. * Figure out how many bytes we need.
  159. * This assumes that the seed sources provide eight bits of entropy
  160. * per byte. For lower quality sources, the formula will need to be
  161. * different.
  162. */
  163. bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0;
  164. if (bytes_needed < min_len)
  165. bytes_needed = min_len;
  166. if (bytes_needed > max_len) {
  167. ERR_raise(ERR_LIB_PROV, PROV_R_ENTROPY_SOURCE_STRENGTH_TOO_WEAK);
  168. return 0;
  169. }
  170. p = OPENSSL_secure_malloc(bytes_needed);
  171. if (p == NULL) {
  172. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  173. return 0;
  174. }
  175. if (seed_src_generate(vseed, p, bytes_needed, 0, prediction_resistance,
  176. adin, adin_len) != 0) {
  177. *pout = p;
  178. return bytes_needed;
  179. }
  180. OPENSSL_secure_clear_free(p, bytes_needed);
  181. return 0;
  182. }
  183. static void seed_clear_seed(ossl_unused void *vdrbg,
  184. unsigned char *out, size_t outlen)
  185. {
  186. OPENSSL_secure_clear_free(out, outlen);
  187. }
  188. static int seed_src_enable_locking(ossl_unused void *vseed)
  189. {
  190. return 1;
  191. }
  192. int seed_src_lock(ossl_unused void *vctx)
  193. {
  194. return 1;
  195. }
  196. void seed_src_unlock(ossl_unused void *vctx)
  197. {
  198. }
  199. const OSSL_DISPATCH ossl_seed_src_functions[] = {
  200. { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))seed_src_new },
  201. { OSSL_FUNC_RAND_FREECTX, (void(*)(void))seed_src_free },
  202. { OSSL_FUNC_RAND_INSTANTIATE,
  203. (void(*)(void))seed_src_instantiate },
  204. { OSSL_FUNC_RAND_UNINSTANTIATE,
  205. (void(*)(void))seed_src_uninstantiate },
  206. { OSSL_FUNC_RAND_GENERATE, (void(*)(void))seed_src_generate },
  207. { OSSL_FUNC_RAND_RESEED, (void(*)(void))seed_src_reseed },
  208. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))seed_src_enable_locking },
  209. { OSSL_FUNC_RAND_LOCK, (void(*)(void))seed_src_lock },
  210. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))seed_src_unlock },
  211. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  212. (void(*)(void))seed_src_gettable_ctx_params },
  213. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))seed_src_get_ctx_params },
  214. { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
  215. (void(*)(void))seed_src_verify_zeroization },
  216. { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))seed_get_seed },
  217. { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))seed_clear_seed },
  218. { 0, NULL }
  219. };