crngt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2019-2020 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 "prov/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. RAND_POOL *crngt_pool;
  27. } CRNG_TEST_GLOBAL;
  28. static int crngt_get_entropy(OPENSSL_CTX *ctx, RAND_POOL *pool,
  29. unsigned char *buf, unsigned char *md,
  30. unsigned int *md_size)
  31. {
  32. int r;
  33. size_t n;
  34. unsigned char *p;
  35. EVP_MD *fmd;
  36. if (pool == NULL)
  37. return 0;
  38. n = prov_pool_acquire_entropy(pool);
  39. if (n >= CRNGT_BUFSIZ) {
  40. fmd = EVP_MD_fetch(ctx, "SHA256", "");
  41. if (fmd == NULL)
  42. return 0;
  43. p = rand_pool_detach(pool);
  44. r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, fmd, NULL);
  45. if (r != 0)
  46. memcpy(buf, p, CRNGT_BUFSIZ);
  47. rand_pool_reattach(pool, p);
  48. EVP_MD_free(fmd);
  49. return r;
  50. }
  51. return 0;
  52. }
  53. static void rand_crng_ossl_ctx_free(void *vcrngt_glob)
  54. {
  55. CRNG_TEST_GLOBAL *crngt_glob = vcrngt_glob;
  56. rand_pool_free(crngt_glob->crngt_pool);
  57. OPENSSL_free(crngt_glob);
  58. }
  59. static void *rand_crng_ossl_ctx_new(OPENSSL_CTX *ctx)
  60. {
  61. unsigned char buf[CRNGT_BUFSIZ];
  62. CRNG_TEST_GLOBAL *crngt_glob = OPENSSL_zalloc(sizeof(*crngt_glob));
  63. if (crngt_glob == NULL)
  64. return NULL;
  65. if ((crngt_glob->crngt_pool
  66. = rand_pool_new(0, 1, CRNGT_BUFSIZ, CRNGT_BUFSIZ)) == NULL) {
  67. OPENSSL_free(crngt_glob);
  68. return NULL;
  69. }
  70. if (crngt_get_entropy(ctx, crngt_glob->crngt_pool, buf,
  71. crngt_glob->crngt_prev, NULL)) {
  72. OPENSSL_cleanse(buf, sizeof(buf));
  73. return crngt_glob;
  74. }
  75. rand_pool_free(crngt_glob->crngt_pool);
  76. OPENSSL_free(crngt_glob);
  77. return NULL;
  78. }
  79. static const OPENSSL_CTX_METHOD rand_crng_ossl_ctx_method = {
  80. rand_crng_ossl_ctx_new,
  81. rand_crng_ossl_ctx_free,
  82. };
  83. static int prov_crngt_compare_previous(const unsigned char *prev,
  84. const unsigned char *cur,
  85. size_t sz)
  86. {
  87. const int res = memcmp(prev, cur, sz) != 0;
  88. if (!res)
  89. ossl_set_error_state(OSSL_SELF_TEST_TYPE_CRNG);
  90. return res;
  91. }
  92. size_t prov_crngt_get_entropy(PROV_DRBG *drbg,
  93. unsigned char **pout,
  94. int entropy, size_t min_len, size_t max_len,
  95. int prediction_resistance)
  96. {
  97. unsigned char buf[CRNGT_BUFSIZ], md[EVP_MAX_MD_SIZE];
  98. unsigned int sz;
  99. RAND_POOL *pool;
  100. size_t q, r = 0, s, t = 0;
  101. int attempts = 3;
  102. OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(drbg->provctx);
  103. CRNG_TEST_GLOBAL *crngt_glob
  104. = openssl_ctx_get_data(libctx, OPENSSL_CTX_RAND_CRNGT_INDEX,
  105. &rand_crng_ossl_ctx_method);
  106. if (crngt_glob == NULL)
  107. return 0;
  108. if ((pool = rand_pool_new(entropy, 1, min_len, max_len)) == NULL)
  109. return 0;
  110. while ((q = rand_pool_bytes_needed(pool, 1)) > 0 && attempts-- > 0) {
  111. s = q > sizeof(buf) ? sizeof(buf) : q;
  112. if (!crngt_get_entropy(libctx, crngt_glob->crngt_pool, buf, md,
  113. &sz)
  114. || !prov_crngt_compare_previous(crngt_glob->crngt_prev, md, sz)
  115. || !rand_pool_add(pool, buf, s, s * 8))
  116. goto err;
  117. memcpy(crngt_glob->crngt_prev, md, sz);
  118. t += s;
  119. attempts++;
  120. }
  121. r = t;
  122. *pout = rand_pool_detach(pool);
  123. err:
  124. OPENSSL_cleanse(buf, sizeof(buf));
  125. rand_pool_free(pool);
  126. return r;
  127. }
  128. void prov_crngt_cleanup_entropy(PROV_DRBG *drbg,
  129. unsigned char *out, size_t outlen)
  130. {
  131. OPENSSL_secure_clear_free(out, outlen);
  132. }