crngt.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2019-2022 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. #include "crypto/context.h"
  25. typedef struct crng_test_global_st {
  26. unsigned char crngt_prev[EVP_MAX_MD_SIZE];
  27. EVP_MD *md;
  28. int preloaded;
  29. CRYPTO_RWLOCK *lock;
  30. } CRNG_TEST_GLOBAL;
  31. static int crngt_get_entropy(PROV_CTX *provctx, const EVP_MD *digest,
  32. unsigned char *buf, unsigned char *md,
  33. unsigned int *md_size)
  34. {
  35. int r;
  36. size_t n;
  37. unsigned char *p;
  38. n = ossl_prov_get_entropy(provctx, &p, 0, CRNGT_BUFSIZ, CRNGT_BUFSIZ);
  39. if (n == CRNGT_BUFSIZ) {
  40. r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, digest, NULL);
  41. if (r != 0)
  42. memcpy(buf, p, CRNGT_BUFSIZ);
  43. ossl_prov_cleanup_entropy(provctx, p, n);
  44. return r != 0;
  45. }
  46. if (n != 0)
  47. ossl_prov_cleanup_entropy(provctx, p, n);
  48. return 0;
  49. }
  50. void ossl_rand_crng_ctx_free(void *vcrngt_glob)
  51. {
  52. CRNG_TEST_GLOBAL *crngt_glob = vcrngt_glob;
  53. CRYPTO_THREAD_lock_free(crngt_glob->lock);
  54. EVP_MD_free(crngt_glob->md);
  55. OPENSSL_free(crngt_glob);
  56. }
  57. void *ossl_rand_crng_ctx_new(OSSL_LIB_CTX *ctx)
  58. {
  59. CRNG_TEST_GLOBAL *crngt_glob = OPENSSL_zalloc(sizeof(*crngt_glob));
  60. if (crngt_glob == NULL)
  61. return NULL;
  62. if ((crngt_glob->md = EVP_MD_fetch(ctx, "SHA256", "")) == NULL) {
  63. OPENSSL_free(crngt_glob);
  64. return NULL;
  65. }
  66. if ((crngt_glob->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  67. EVP_MD_free(crngt_glob->md);
  68. OPENSSL_free(crngt_glob);
  69. return NULL;
  70. }
  71. return crngt_glob;
  72. }
  73. static int prov_crngt_compare_previous(const unsigned char *prev,
  74. const unsigned char *cur,
  75. size_t sz)
  76. {
  77. const int res = memcmp(prev, cur, sz) != 0;
  78. if (!res)
  79. ossl_set_error_state(OSSL_SELF_TEST_TYPE_CRNG);
  80. return res;
  81. }
  82. size_t ossl_crngt_get_entropy(PROV_DRBG *drbg,
  83. unsigned char **pout,
  84. int entropy, size_t min_len, size_t max_len,
  85. int prediction_resistance)
  86. {
  87. unsigned char md[EVP_MAX_MD_SIZE];
  88. unsigned char buf[CRNGT_BUFSIZ];
  89. unsigned char *ent, *entp, *entbuf;
  90. unsigned int sz;
  91. size_t bytes_needed;
  92. size_t r = 0, s, t;
  93. int crng_test_pass = 1;
  94. OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx);
  95. CRNG_TEST_GLOBAL *crngt_glob
  96. = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_RAND_CRNGT_INDEX);
  97. OSSL_CALLBACK *stcb = NULL;
  98. void *stcbarg = NULL;
  99. OSSL_SELF_TEST *st = NULL;
  100. if (crngt_glob == NULL)
  101. return 0;
  102. if (!CRYPTO_THREAD_write_lock(crngt_glob->lock))
  103. return 0;
  104. if (!crngt_glob->preloaded) {
  105. if (!crngt_get_entropy(drbg->provctx, crngt_glob->md, buf,
  106. crngt_glob->crngt_prev, NULL)) {
  107. OPENSSL_cleanse(buf, sizeof(buf));
  108. goto unlock_return;
  109. }
  110. crngt_glob->preloaded = 1;
  111. }
  112. /*
  113. * Calculate how many bytes of seed material we require, rounded up
  114. * to the nearest byte. If the entropy is of less than full quality,
  115. * the amount required should be scaled up appropriately here.
  116. */
  117. bytes_needed = (entropy + 7) / 8;
  118. if (bytes_needed < min_len)
  119. bytes_needed = min_len;
  120. if (bytes_needed > max_len)
  121. goto unlock_return;
  122. entp = ent = OPENSSL_secure_malloc(bytes_needed);
  123. if (ent == NULL)
  124. goto unlock_return;
  125. OSSL_SELF_TEST_get_callback(libctx, &stcb, &stcbarg);
  126. if (stcb != NULL) {
  127. st = OSSL_SELF_TEST_new(stcb, stcbarg);
  128. if (st == NULL)
  129. goto err;
  130. OSSL_SELF_TEST_onbegin(st, OSSL_SELF_TEST_TYPE_CRNG,
  131. OSSL_SELF_TEST_DESC_RNG);
  132. }
  133. for (t = bytes_needed; t > 0;) {
  134. /* Care needs to be taken to avoid overrunning the buffer */
  135. s = t >= CRNGT_BUFSIZ ? CRNGT_BUFSIZ : t;
  136. entbuf = t >= CRNGT_BUFSIZ ? entp : buf;
  137. if (!crngt_get_entropy(drbg->provctx, crngt_glob->md, entbuf, md, &sz))
  138. goto err;
  139. if (t < CRNGT_BUFSIZ)
  140. memcpy(entp, buf, t);
  141. /* Force a failure here if the callback returns 1 */
  142. if (OSSL_SELF_TEST_oncorrupt_byte(st, md))
  143. memcpy(md, crngt_glob->crngt_prev, sz);
  144. if (!prov_crngt_compare_previous(crngt_glob->crngt_prev, md, sz)) {
  145. crng_test_pass = 0;
  146. goto err;
  147. }
  148. /* Update for next block */
  149. memcpy(crngt_glob->crngt_prev, md, sz);
  150. entp += s;
  151. t -= s;
  152. }
  153. r = bytes_needed;
  154. *pout = ent;
  155. ent = NULL;
  156. err:
  157. OSSL_SELF_TEST_onend(st, crng_test_pass);
  158. OSSL_SELF_TEST_free(st);
  159. OPENSSL_secure_clear_free(ent, bytes_needed);
  160. unlock_return:
  161. CRYPTO_THREAD_unlock(crngt_glob->lock);
  162. return r;
  163. }
  164. void ossl_crngt_cleanup_entropy(ossl_unused PROV_DRBG *drbg,
  165. unsigned char *out, size_t outlen)
  166. {
  167. OPENSSL_secure_clear_free(out, outlen);
  168. }