2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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
12 * Implementation of the FIPS 140-2 section 4.9.2 Conditional Tests.
16 #include <openssl/evp.h>
17 #include "crypto/rand.h"
18 #include "internal/thread_once.h"
19 #include "internal/cryptlib.h"
20 #include "crypto/rand_pool.h"
21 #include "drbg_local.h"
22 #include "crypto/rand_pool.h"
23 #include "seeding/seeding.h"
25 typedef struct crng_test_global_st {
26 unsigned char crngt_prev[EVP_MAX_MD_SIZE];
27 RAND_POOL *crngt_pool;
30 static int crngt_get_entropy(OPENSSL_CTX *ctx, RAND_POOL *pool,
31 unsigned char *buf, unsigned char *md,
32 unsigned int *md_size)
41 n = prov_pool_acquire_entropy(pool);
42 if (n >= CRNGT_BUFSIZ) {
43 EVP_MD *fmd = EVP_MD_fetch(ctx, "SHA256", "");
46 p = rand_pool_detach(pool);
47 r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, fmd, NULL);
49 memcpy(buf, p, CRNGT_BUFSIZ);
50 rand_pool_reattach(pool, p);
57 static void rand_crng_ossl_ctx_free(void *vcrngt_glob)
59 CRNG_TEST_GLOBAL *crngt_glob = vcrngt_glob;
61 rand_pool_free(crngt_glob->crngt_pool);
62 OPENSSL_free(crngt_glob);
65 static void *rand_crng_ossl_ctx_new(OPENSSL_CTX *ctx)
67 unsigned char buf[CRNGT_BUFSIZ];
68 CRNG_TEST_GLOBAL *crngt_glob = OPENSSL_zalloc(sizeof(*crngt_glob));
70 if (crngt_glob == NULL)
73 if ((crngt_glob->crngt_pool
74 = rand_pool_new(0, 1, CRNGT_BUFSIZ, CRNGT_BUFSIZ)) == NULL) {
75 OPENSSL_free(crngt_glob);
78 if (crngt_get_entropy(ctx, crngt_glob->crngt_pool, buf,
79 crngt_glob->crngt_prev, NULL)) {
80 OPENSSL_cleanse(buf, sizeof(buf));
83 rand_pool_free(crngt_glob->crngt_pool);
84 OPENSSL_free(crngt_glob);
88 static const OPENSSL_CTX_METHOD rand_crng_ossl_ctx_method = {
89 rand_crng_ossl_ctx_new,
90 rand_crng_ossl_ctx_free,
93 int rand_crngt_get_entropy_cb(OPENSSL_CTX *ctx,
97 unsigned int *md_size)
106 n = rand_pool_acquire_entropy(pool);
107 if (n >= CRNGT_BUFSIZ) {
108 EVP_MD *fmd = EVP_MD_fetch(ctx, "SHA256", "");
111 p = rand_pool_detach(pool);
112 r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, fmd, NULL);
114 memcpy(buf, p, CRNGT_BUFSIZ);
115 rand_pool_reattach(pool, p);
122 size_t rand_crngt_get_entropy(RAND_DRBG *drbg,
123 unsigned char **pout,
124 int entropy, size_t min_len, size_t max_len,
125 int prediction_resistance)
127 unsigned char buf[CRNGT_BUFSIZ], md[EVP_MAX_MD_SIZE];
130 size_t q, r = 0, s, t = 0;
132 CRNG_TEST_GLOBAL *crngt_glob
133 = openssl_ctx_get_data(drbg->libctx, OPENSSL_CTX_RAND_CRNGT_INDEX,
134 &rand_crng_ossl_ctx_method);
136 if (crngt_glob == NULL)
139 if ((pool = rand_pool_new(entropy, 1, min_len, max_len)) == NULL)
142 while ((q = rand_pool_bytes_needed(pool, 1)) > 0 && attempts-- > 0) {
143 s = q > sizeof(buf) ? sizeof(buf) : q;
144 if (!crngt_get_entropy(drbg->libctx, crngt_glob->crngt_pool, buf, md,
146 || memcmp(crngt_glob->crngt_prev, md, sz) == 0
147 || !rand_pool_add(pool, buf, s, s * 8))
149 memcpy(crngt_glob->crngt_prev, md, sz);
154 *pout = rand_pool_detach(pool);
156 OPENSSL_cleanse(buf, sizeof(buf));
157 rand_pool_free(pool);
161 void rand_crngt_cleanup_entropy(RAND_DRBG *drbg,
162 unsigned char *out, size_t outlen)
164 OPENSSL_secure_clear_free(out, outlen);
168 const OSSL_DISPATCH crngt_functions[] = {
169 { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))crngt_new },
170 { OSSL_FUNC_RAND_FREECTX, (void(*)(void))crngt_free },
171 { OSSL_FUNC_RAND_INSTANTIATE, (void(*)(void))crngt_instantiate },
172 { OSSL_FUNC_RAND_UNINSTANTIATE, (void(*)(void))crngt_uninstantiate },
173 { OSSL_FUNC_RAND_GENERATE, (void(*)(void))crngt_generate },
174 { OSSL_FUNC_RAND_RESEED, (void(*)(void))crngt_reseed },
175 { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))drbg_enable_locking },
176 { OSSL_FUNC_RAND_LOCK, (void(*)(void))drbg_lock },
177 { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))drbg_unlock },
178 { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
179 (void(*)(void))crngt_settable_ctx_params },
180 { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))crngt_set_ctx_params },
181 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
182 (void(*)(void))crngt_gettable_ctx_params },
183 { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))crngt_get_ctx_params },