crngt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /*
  11. * Implementation of the FIPS 140-2 section 4.9.2 Conditional Tests.
  12. */
  13. #include <string.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/params.h>
  17. #include <openssl/self_test.h>
  18. #include "prov/providercommon.h"
  19. #include "prov/provider_ctx.h"
  20. #include "internal/cryptlib.h"
  21. #include "crypto/rand_pool.h"
  22. #include "drbg_local.h"
  23. #include "prov/seeding.h"
  24. typedef struct crng_test_global_st {
  25. unsigned char crngt_prev[EVP_MAX_MD_SIZE];
  26. EVP_MD *md;
  27. int preloaded;
  28. CRYPTO_RWLOCK *lock;
  29. } CRNG_TEST_GLOBAL;
  30. static int crngt_get_entropy(PROV_CTX *provctx, const EVP_MD *digest,
  31. unsigned char *buf, unsigned char *md,
  32. unsigned int *md_size)
  33. {
  34. int r;
  35. size_t n;
  36. unsigned char *p;
  37. n = ossl_prov_get_entropy(provctx, &p, 0, CRNGT_BUFSIZ, CRNGT_BUFSIZ);
  38. if (n == CRNGT_BUFSIZ) {
  39. r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, digest, NULL);
  40. if (r != 0)
  41. memcpy(buf, p, CRNGT_BUFSIZ);
  42. ossl_prov_cleanup_entropy(provctx, p, n);
  43. return r != 0;
  44. }
  45. if (n != 0)
  46. ossl_prov_cleanup_entropy(provctx, p, n);
  47. return 0;
  48. }
  49. static void rand_crng_ossl_ctx_free(void *vcrngt_glob)
  50. {
  51. CRNG_TEST_GLOBAL *crngt_glob = vcrngt_glob;
  52. CRYPTO_THREAD_lock_free(crngt_glob->lock);
  53. EVP_MD_free(crngt_glob->md);
  54. OPENSSL_free(crngt_glob);
  55. }
  56. static void *rand_crng_ossl_ctx_new(OSSL_LIB_CTX *ctx)
  57. {
  58. CRNG_TEST_GLOBAL *crngt_glob = OPENSSL_zalloc(sizeof(*crngt_glob));
  59. if (crngt_glob == NULL)
  60. return NULL;
  61. if ((crngt_glob->md = EVP_MD_fetch(ctx, "SHA256", "")) == NULL) {
  62. OPENSSL_free(crngt_glob);
  63. return NULL;
  64. }
  65. if ((crngt_glob->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  66. EVP_MD_free(crngt_glob->md);
  67. OPENSSL_free(crngt_glob);
  68. return NULL;
  69. }
  70. return crngt_glob;
  71. }
  72. static const OSSL_LIB_CTX_METHOD rand_crng_ossl_ctx_method = {
  73. OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  74. rand_crng_ossl_ctx_new,
  75. rand_crng_ossl_ctx_free,
  76. };
  77. static int prov_crngt_compare_previous(const unsigned char *prev,
  78. const unsigned char *cur,
  79. size_t sz)
  80. {
  81. const int res = memcmp(prev, cur, sz) != 0;
  82. if (!res)
  83. ossl_set_error_state(OSSL_SELF_TEST_TYPE_CRNG);
  84. return res;
  85. }
  86. size_t ossl_crngt_get_entropy(PROV_DRBG *drbg,
  87. unsigned char **pout,
  88. int entropy, size_t min_len, size_t max_len,
  89. int prediction_resistance)
  90. {
  91. unsigned char md[EVP_MAX_MD_SIZE];
  92. unsigned char buf[CRNGT_BUFSIZ];
  93. unsigned char *ent, *entp, *entbuf;
  94. unsigned int sz;
  95. size_t bytes_needed;
  96. size_t r = 0, s, t;
  97. int crng_test_pass = 1;
  98. OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx);
  99. CRNG_TEST_GLOBAL *crngt_glob
  100. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_RAND_CRNGT_INDEX,
  101. &rand_crng_ossl_ctx_method);
  102. OSSL_CALLBACK *stcb = NULL;
  103. void *stcbarg = NULL;
  104. OSSL_SELF_TEST *st = NULL;
  105. if (crngt_glob == NULL)
  106. return 0;
  107. if (!CRYPTO_THREAD_write_lock(crngt_glob->lock))
  108. return 0;
  109. if (!crngt_glob->preloaded) {
  110. if (!crngt_get_entropy(drbg->provctx, crngt_glob->md, buf,
  111. crngt_glob->crngt_prev, NULL)) {
  112. OPENSSL_cleanse(buf, sizeof(buf));
  113. goto unlock_return;
  114. }
  115. crngt_glob->preloaded = 1;
  116. }
  117. /*
  118. * Calculate how many bytes of seed material we require, rounded up
  119. * to the nearest byte. If the entropy is of less than full quality,
  120. * the amount required should be scaled up appropriately here.
  121. */
  122. bytes_needed = (entropy + 7) / 8;
  123. if (bytes_needed < min_len)
  124. bytes_needed = min_len;
  125. if (bytes_needed > max_len)
  126. goto unlock_return;
  127. entp = ent = OPENSSL_secure_malloc(bytes_needed);
  128. if (ent == NULL)
  129. goto unlock_return;
  130. OSSL_SELF_TEST_get_callback(libctx, &stcb, &stcbarg);
  131. if (stcb != NULL) {
  132. st = OSSL_SELF_TEST_new(stcb, stcbarg);
  133. if (st == NULL)
  134. goto err;
  135. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_CRNG,
  136. OSSL_SELF_TEST_DESC_RNG);
  137. }
  138. for (t = bytes_needed; t > 0;) {
  139. /* Care needs to be taken to avoid overrunning the buffer */
  140. s = t >= CRNGT_BUFSIZ ? CRNGT_BUFSIZ : t;
  141. entbuf = t >= CRNGT_BUFSIZ ? entp : buf;
  142. if (!crngt_get_entropy(drbg->provctx, crngt_glob->md, entbuf, md, &sz))
  143. goto err;
  144. if (t < CRNGT_BUFSIZ)
  145. memcpy(entp, buf, t);
  146. /* Force a failure here if the callback returns 1 */
  147. if (OSSL_SELF_TEST_oncorrupt_byte(st, md))
  148. memcpy(md, crngt_glob->crngt_prev, sz);
  149. if (!prov_crngt_compare_previous(crngt_glob->crngt_prev, md, sz)) {
  150. crng_test_pass = 0;
  151. goto err;
  152. }
  153. /* Update for next block */
  154. memcpy(crngt_glob->crngt_prev, md, sz);
  155. entp += s;
  156. t -= s;
  157. }
  158. r = bytes_needed;
  159. *pout = ent;
  160. ent = NULL;
  161. err:
  162. OSSL_SELF_TEST_onend(st, crng_test_pass);
  163. OSSL_SELF_TEST_free(st);
  164. OPENSSL_secure_clear_free(ent, bytes_needed);
  165. unlock_return:
  166. CRYPTO_THREAD_unlock(crngt_glob->lock);
  167. return r;
  168. }
  169. void ossl_crngt_cleanup_entropy(ossl_unused PROV_DRBG *drbg,
  170. unsigned char *out, size_t outlen)
  171. {
  172. OPENSSL_secure_clear_free(out, outlen);
  173. }