1b077aed3SPierre Pronchery /*
2*44096ebdSEnji Cooper * Copyright 2011-2024 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery */
9b077aed3SPierre Pronchery
10b077aed3SPierre Pronchery #include <assert.h>
11b077aed3SPierre Pronchery #include <stdlib.h>
12b077aed3SPierre Pronchery #include <string.h>
13b077aed3SPierre Pronchery #include <openssl/sha.h>
14b077aed3SPierre Pronchery #include <openssl/crypto.h>
15b077aed3SPierre Pronchery #include <openssl/err.h>
16b077aed3SPierre Pronchery #include <openssl/rand.h>
17b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
18b077aed3SPierre Pronchery #include <openssl/proverr.h>
19b077aed3SPierre Pronchery #include "internal/thread_once.h"
20b077aed3SPierre Pronchery #include "prov/providercommon.h"
21b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
22b077aed3SPierre Pronchery #include "prov/provider_util.h"
23b077aed3SPierre Pronchery #include "prov/implementations.h"
24b077aed3SPierre Pronchery #include "drbg_local.h"
25b077aed3SPierre Pronchery
26b077aed3SPierre Pronchery static OSSL_FUNC_rand_newctx_fn drbg_hash_new_wrapper;
27b077aed3SPierre Pronchery static OSSL_FUNC_rand_freectx_fn drbg_hash_free;
28b077aed3SPierre Pronchery static OSSL_FUNC_rand_instantiate_fn drbg_hash_instantiate_wrapper;
29b077aed3SPierre Pronchery static OSSL_FUNC_rand_uninstantiate_fn drbg_hash_uninstantiate_wrapper;
30b077aed3SPierre Pronchery static OSSL_FUNC_rand_generate_fn drbg_hash_generate_wrapper;
31b077aed3SPierre Pronchery static OSSL_FUNC_rand_reseed_fn drbg_hash_reseed_wrapper;
32b077aed3SPierre Pronchery static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hash_settable_ctx_params;
33b077aed3SPierre Pronchery static OSSL_FUNC_rand_set_ctx_params_fn drbg_hash_set_ctx_params;
34b077aed3SPierre Pronchery static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hash_gettable_ctx_params;
35b077aed3SPierre Pronchery static OSSL_FUNC_rand_get_ctx_params_fn drbg_hash_get_ctx_params;
36b077aed3SPierre Pronchery static OSSL_FUNC_rand_verify_zeroization_fn drbg_hash_verify_zeroization;
37b077aed3SPierre Pronchery
38b077aed3SPierre Pronchery /* 888 bits from SP800-90Ar1 10.1 table 2 */
39b077aed3SPierre Pronchery #define HASH_PRNG_MAX_SEEDLEN (888/8)
40b077aed3SPierre Pronchery
41b077aed3SPierre Pronchery /* 440 bits from SP800-90Ar1 10.1 table 2 */
42b077aed3SPierre Pronchery #define HASH_PRNG_SMALL_SEEDLEN (440/8)
43b077aed3SPierre Pronchery
44b077aed3SPierre Pronchery /* Determine what seedlen to use based on the block length */
45b077aed3SPierre Pronchery #define MAX_BLOCKLEN_USING_SMALL_SEEDLEN (256/8)
46b077aed3SPierre Pronchery #define INBYTE_IGNORE ((unsigned char)0xFF)
47b077aed3SPierre Pronchery
48b077aed3SPierre Pronchery typedef struct rand_drbg_hash_st {
49b077aed3SPierre Pronchery PROV_DIGEST digest;
50b077aed3SPierre Pronchery EVP_MD_CTX *ctx;
51b077aed3SPierre Pronchery size_t blocklen;
52b077aed3SPierre Pronchery unsigned char V[HASH_PRNG_MAX_SEEDLEN];
53b077aed3SPierre Pronchery unsigned char C[HASH_PRNG_MAX_SEEDLEN];
54b077aed3SPierre Pronchery /* Temporary value storage: should always exceed max digest length */
55b077aed3SPierre Pronchery unsigned char vtmp[HASH_PRNG_MAX_SEEDLEN];
56b077aed3SPierre Pronchery } PROV_DRBG_HASH;
57b077aed3SPierre Pronchery
58b077aed3SPierre Pronchery /*
59b077aed3SPierre Pronchery * SP800-90Ar1 10.3.1 Derivation function using a Hash Function (Hash_df).
60b077aed3SPierre Pronchery * The input string used is composed of:
61b077aed3SPierre Pronchery * inbyte - An optional leading byte (ignore if equal to INBYTE_IGNORE)
62b077aed3SPierre Pronchery * in - input string 1 (A Non NULL value).
63b077aed3SPierre Pronchery * in2 - optional input string (Can be NULL).
64b077aed3SPierre Pronchery * in3 - optional input string (Can be NULL).
65b077aed3SPierre Pronchery * These are concatenated as part of the DigestUpdate process.
66b077aed3SPierre Pronchery */
hash_df(PROV_DRBG * drbg,unsigned char * out,const unsigned char inbyte,const unsigned char * in,size_t inlen,const unsigned char * in2,size_t in2len,const unsigned char * in3,size_t in3len)67b077aed3SPierre Pronchery static int hash_df(PROV_DRBG *drbg, unsigned char *out,
68b077aed3SPierre Pronchery const unsigned char inbyte,
69b077aed3SPierre Pronchery const unsigned char *in, size_t inlen,
70b077aed3SPierre Pronchery const unsigned char *in2, size_t in2len,
71b077aed3SPierre Pronchery const unsigned char *in3, size_t in3len)
72b077aed3SPierre Pronchery {
73b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
74b077aed3SPierre Pronchery EVP_MD_CTX *ctx = hash->ctx;
75b077aed3SPierre Pronchery unsigned char *vtmp = hash->vtmp;
76b077aed3SPierre Pronchery /* tmp = counter || num_bits_returned || [inbyte] */
77b077aed3SPierre Pronchery unsigned char tmp[1 + 4 + 1];
78b077aed3SPierre Pronchery int tmp_sz = 0;
79b077aed3SPierre Pronchery size_t outlen = drbg->seedlen;
80b077aed3SPierre Pronchery size_t num_bits_returned = outlen * 8;
81b077aed3SPierre Pronchery /*
82b077aed3SPierre Pronchery * No need to check outlen size here, as the standard only ever needs
83b077aed3SPierre Pronchery * seedlen bytes which is always less than the maximum permitted.
84b077aed3SPierre Pronchery */
85b077aed3SPierre Pronchery
86b077aed3SPierre Pronchery /* (Step 3) counter = 1 (tmp[0] is the 8 bit counter) */
87b077aed3SPierre Pronchery tmp[tmp_sz++] = 1;
88b077aed3SPierre Pronchery /* tmp[1..4] is the fixed 32 bit no_of_bits_to_return */
89b077aed3SPierre Pronchery tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 24) & 0xff);
90b077aed3SPierre Pronchery tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 16) & 0xff);
91b077aed3SPierre Pronchery tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 8) & 0xff);
92b077aed3SPierre Pronchery tmp[tmp_sz++] = (unsigned char)(num_bits_returned & 0xff);
93b077aed3SPierre Pronchery /* Tack the additional input byte onto the end of tmp if it exists */
94b077aed3SPierre Pronchery if (inbyte != INBYTE_IGNORE)
95b077aed3SPierre Pronchery tmp[tmp_sz++] = inbyte;
96b077aed3SPierre Pronchery
97b077aed3SPierre Pronchery /* (Step 4) */
98b077aed3SPierre Pronchery for (;;) {
99b077aed3SPierre Pronchery /*
100b077aed3SPierre Pronchery * (Step 4.1) out = out || Hash(tmp || in || [in2] || [in3])
101b077aed3SPierre Pronchery * (where tmp = counter || num_bits_returned || [inbyte])
102b077aed3SPierre Pronchery */
103b077aed3SPierre Pronchery if (!(EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
104b077aed3SPierre Pronchery && EVP_DigestUpdate(ctx, tmp, tmp_sz)
105b077aed3SPierre Pronchery && EVP_DigestUpdate(ctx, in, inlen)
106b077aed3SPierre Pronchery && (in2 == NULL || EVP_DigestUpdate(ctx, in2, in2len))
107b077aed3SPierre Pronchery && (in3 == NULL || EVP_DigestUpdate(ctx, in3, in3len))))
108b077aed3SPierre Pronchery return 0;
109b077aed3SPierre Pronchery
110b077aed3SPierre Pronchery if (outlen < hash->blocklen) {
111b077aed3SPierre Pronchery if (!EVP_DigestFinal(ctx, vtmp, NULL))
112b077aed3SPierre Pronchery return 0;
113b077aed3SPierre Pronchery memcpy(out, vtmp, outlen);
114b077aed3SPierre Pronchery OPENSSL_cleanse(vtmp, hash->blocklen);
115b077aed3SPierre Pronchery break;
116b077aed3SPierre Pronchery } else if(!EVP_DigestFinal(ctx, out, NULL)) {
117b077aed3SPierre Pronchery return 0;
118b077aed3SPierre Pronchery }
119b077aed3SPierre Pronchery
120b077aed3SPierre Pronchery outlen -= hash->blocklen;
121b077aed3SPierre Pronchery if (outlen == 0)
122b077aed3SPierre Pronchery break;
123b077aed3SPierre Pronchery /* (Step 4.2) counter++ */
124b077aed3SPierre Pronchery tmp[0]++;
125b077aed3SPierre Pronchery out += hash->blocklen;
126b077aed3SPierre Pronchery }
127b077aed3SPierre Pronchery return 1;
128b077aed3SPierre Pronchery }
129b077aed3SPierre Pronchery
130b077aed3SPierre Pronchery /* Helper function that just passes 2 input parameters to hash_df() */
hash_df1(PROV_DRBG * drbg,unsigned char * out,const unsigned char in_byte,const unsigned char * in1,size_t in1len)131b077aed3SPierre Pronchery static int hash_df1(PROV_DRBG *drbg, unsigned char *out,
132b077aed3SPierre Pronchery const unsigned char in_byte,
133b077aed3SPierre Pronchery const unsigned char *in1, size_t in1len)
134b077aed3SPierre Pronchery {
135b077aed3SPierre Pronchery return hash_df(drbg, out, in_byte, in1, in1len, NULL, 0, NULL, 0);
136b077aed3SPierre Pronchery }
137b077aed3SPierre Pronchery
138b077aed3SPierre Pronchery /*
139b077aed3SPierre Pronchery * Add 2 byte buffers together. The first elements in each buffer are the top
140b077aed3SPierre Pronchery * most bytes. The result is stored in the dst buffer.
141b077aed3SPierre Pronchery * The final carry is ignored i.e: dst = (dst + in) mod (2^seedlen_bits).
142b077aed3SPierre Pronchery * where dst size is drbg->seedlen, and inlen <= drbg->seedlen.
143b077aed3SPierre Pronchery */
add_bytes(PROV_DRBG * drbg,unsigned char * dst,unsigned char * in,size_t inlen)144b077aed3SPierre Pronchery static int add_bytes(PROV_DRBG *drbg, unsigned char *dst,
145b077aed3SPierre Pronchery unsigned char *in, size_t inlen)
146b077aed3SPierre Pronchery {
147b077aed3SPierre Pronchery size_t i;
148b077aed3SPierre Pronchery int result;
149b077aed3SPierre Pronchery const unsigned char *add;
150b077aed3SPierre Pronchery unsigned char carry = 0, *d;
151b077aed3SPierre Pronchery
152b077aed3SPierre Pronchery assert(drbg->seedlen >= 1 && inlen >= 1 && inlen <= drbg->seedlen);
153b077aed3SPierre Pronchery
154b077aed3SPierre Pronchery d = &dst[drbg->seedlen - 1];
155b077aed3SPierre Pronchery add = &in[inlen - 1];
156b077aed3SPierre Pronchery
157b077aed3SPierre Pronchery for (i = inlen; i > 0; i--, d--, add--) {
158b077aed3SPierre Pronchery result = *d + *add + carry;
159b077aed3SPierre Pronchery carry = (unsigned char)(result >> 8);
160b077aed3SPierre Pronchery *d = (unsigned char)(result & 0xff);
161b077aed3SPierre Pronchery }
162b077aed3SPierre Pronchery
163b077aed3SPierre Pronchery if (carry != 0) {
164b077aed3SPierre Pronchery /* Add the carry to the top of the dst if inlen is not the same size */
165b077aed3SPierre Pronchery for (i = drbg->seedlen - inlen; i > 0; --i, d--) {
166b077aed3SPierre Pronchery *d += 1; /* Carry can only be 1 */
167b077aed3SPierre Pronchery if (*d != 0) /* exit if carry doesnt propagate to the next byte */
168b077aed3SPierre Pronchery break;
169b077aed3SPierre Pronchery }
170b077aed3SPierre Pronchery }
171b077aed3SPierre Pronchery return 1;
172b077aed3SPierre Pronchery }
173b077aed3SPierre Pronchery
174b077aed3SPierre Pronchery /* V = (V + Hash(inbyte || V || [additional_input]) mod (2^seedlen) */
add_hash_to_v(PROV_DRBG * drbg,unsigned char inbyte,const unsigned char * adin,size_t adinlen)175b077aed3SPierre Pronchery static int add_hash_to_v(PROV_DRBG *drbg, unsigned char inbyte,
176b077aed3SPierre Pronchery const unsigned char *adin, size_t adinlen)
177b077aed3SPierre Pronchery {
178b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
179b077aed3SPierre Pronchery EVP_MD_CTX *ctx = hash->ctx;
180b077aed3SPierre Pronchery
181b077aed3SPierre Pronchery return EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
182b077aed3SPierre Pronchery && EVP_DigestUpdate(ctx, &inbyte, 1)
183b077aed3SPierre Pronchery && EVP_DigestUpdate(ctx, hash->V, drbg->seedlen)
184b077aed3SPierre Pronchery && (adin == NULL || EVP_DigestUpdate(ctx, adin, adinlen))
185b077aed3SPierre Pronchery && EVP_DigestFinal(ctx, hash->vtmp, NULL)
186b077aed3SPierre Pronchery && add_bytes(drbg, hash->V, hash->vtmp, hash->blocklen);
187b077aed3SPierre Pronchery }
188b077aed3SPierre Pronchery
189b077aed3SPierre Pronchery /*
190b077aed3SPierre Pronchery * The Hashgen() as listed in SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process.
191b077aed3SPierre Pronchery *
192b077aed3SPierre Pronchery * drbg contains the current value of V.
193b077aed3SPierre Pronchery * outlen is the requested number of bytes.
194b077aed3SPierre Pronchery * out is a buffer to return the generated bits.
195b077aed3SPierre Pronchery *
196b077aed3SPierre Pronchery * The algorithm to generate the bits is:
197b077aed3SPierre Pronchery * data = V
198b077aed3SPierre Pronchery * w = NULL
199b077aed3SPierre Pronchery * for (i = 1 to m) {
200b077aed3SPierre Pronchery * W = W || Hash(data)
201b077aed3SPierre Pronchery * data = (data + 1) mod (2^seedlen)
202b077aed3SPierre Pronchery * }
203b077aed3SPierre Pronchery * out = Leftmost(W, outlen)
204b077aed3SPierre Pronchery *
205b077aed3SPierre Pronchery * Returns zero if an error occurs otherwise it returns 1.
206b077aed3SPierre Pronchery */
hash_gen(PROV_DRBG * drbg,unsigned char * out,size_t outlen)207b077aed3SPierre Pronchery static int hash_gen(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
208b077aed3SPierre Pronchery {
209b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
210b077aed3SPierre Pronchery unsigned char one = 1;
211b077aed3SPierre Pronchery
212b077aed3SPierre Pronchery if (outlen == 0)
213b077aed3SPierre Pronchery return 1;
214b077aed3SPierre Pronchery memcpy(hash->vtmp, hash->V, drbg->seedlen);
215b077aed3SPierre Pronchery for(;;) {
216b077aed3SPierre Pronchery if (!EVP_DigestInit_ex(hash->ctx, ossl_prov_digest_md(&hash->digest),
217b077aed3SPierre Pronchery NULL)
218b077aed3SPierre Pronchery || !EVP_DigestUpdate(hash->ctx, hash->vtmp, drbg->seedlen))
219b077aed3SPierre Pronchery return 0;
220b077aed3SPierre Pronchery
221b077aed3SPierre Pronchery if (outlen < hash->blocklen) {
222b077aed3SPierre Pronchery if (!EVP_DigestFinal(hash->ctx, hash->vtmp, NULL))
223b077aed3SPierre Pronchery return 0;
224b077aed3SPierre Pronchery memcpy(out, hash->vtmp, outlen);
225b077aed3SPierre Pronchery return 1;
226b077aed3SPierre Pronchery } else {
227b077aed3SPierre Pronchery if (!EVP_DigestFinal(hash->ctx, out, NULL))
228b077aed3SPierre Pronchery return 0;
229b077aed3SPierre Pronchery outlen -= hash->blocklen;
230b077aed3SPierre Pronchery if (outlen == 0)
231b077aed3SPierre Pronchery break;
232b077aed3SPierre Pronchery out += hash->blocklen;
233b077aed3SPierre Pronchery }
234b077aed3SPierre Pronchery add_bytes(drbg, hash->vtmp, &one, 1);
235b077aed3SPierre Pronchery }
236b077aed3SPierre Pronchery return 1;
237b077aed3SPierre Pronchery }
238b077aed3SPierre Pronchery
239b077aed3SPierre Pronchery /*
240b077aed3SPierre Pronchery * SP800-90Ar1 10.1.1.2 Hash_DRBG_Instantiate_Process:
241b077aed3SPierre Pronchery *
242b077aed3SPierre Pronchery * ent is entropy input obtained from a randomness source of length ent_len.
243b077aed3SPierre Pronchery * nonce is a string of bytes of length nonce_len.
244b077aed3SPierre Pronchery * pstr is a personalization string received from an application. May be NULL.
245b077aed3SPierre Pronchery *
246b077aed3SPierre Pronchery * Returns zero if an error occurs otherwise it returns 1.
247b077aed3SPierre Pronchery */
drbg_hash_instantiate(PROV_DRBG * drbg,const unsigned char * ent,size_t ent_len,const unsigned char * nonce,size_t nonce_len,const unsigned char * pstr,size_t pstr_len)248b077aed3SPierre Pronchery static int drbg_hash_instantiate(PROV_DRBG *drbg,
249b077aed3SPierre Pronchery const unsigned char *ent, size_t ent_len,
250b077aed3SPierre Pronchery const unsigned char *nonce, size_t nonce_len,
251b077aed3SPierre Pronchery const unsigned char *pstr, size_t pstr_len)
252b077aed3SPierre Pronchery {
253b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
254b077aed3SPierre Pronchery
255b077aed3SPierre Pronchery EVP_MD_CTX_free(hash->ctx);
256b077aed3SPierre Pronchery hash->ctx = EVP_MD_CTX_new();
257b077aed3SPierre Pronchery
258b077aed3SPierre Pronchery /* (Step 1-3) V = Hash_df(entropy||nonce||pers, seedlen) */
259b077aed3SPierre Pronchery return hash->ctx != NULL
260b077aed3SPierre Pronchery && hash_df(drbg, hash->V, INBYTE_IGNORE,
261b077aed3SPierre Pronchery ent, ent_len, nonce, nonce_len, pstr, pstr_len)
262b077aed3SPierre Pronchery /* (Step 4) C = Hash_df(0x00||V, seedlen) */
263b077aed3SPierre Pronchery && hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
264b077aed3SPierre Pronchery }
265b077aed3SPierre Pronchery
drbg_hash_instantiate_wrapper(void * vdrbg,unsigned int strength,int prediction_resistance,const unsigned char * pstr,size_t pstr_len,const OSSL_PARAM params[])266b077aed3SPierre Pronchery static int drbg_hash_instantiate_wrapper(void *vdrbg, unsigned int strength,
267b077aed3SPierre Pronchery int prediction_resistance,
268b077aed3SPierre Pronchery const unsigned char *pstr,
269b077aed3SPierre Pronchery size_t pstr_len,
270b077aed3SPierre Pronchery const OSSL_PARAM params[])
271b077aed3SPierre Pronchery {
272b077aed3SPierre Pronchery PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
273b077aed3SPierre Pronchery
274b077aed3SPierre Pronchery if (!ossl_prov_is_running() || !drbg_hash_set_ctx_params(drbg, params))
275b077aed3SPierre Pronchery return 0;
276b077aed3SPierre Pronchery return ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
277b077aed3SPierre Pronchery pstr, pstr_len);
278b077aed3SPierre Pronchery }
279b077aed3SPierre Pronchery
280b077aed3SPierre Pronchery /*
281b077aed3SPierre Pronchery * SP800-90Ar1 10.1.1.3 Hash_DRBG_Reseed_Process:
282b077aed3SPierre Pronchery *
283b077aed3SPierre Pronchery * ent is entropy input bytes obtained from a randomness source.
284b077aed3SPierre Pronchery * addin is additional input received from an application. May be NULL.
285b077aed3SPierre Pronchery *
286b077aed3SPierre Pronchery * Returns zero if an error occurs otherwise it returns 1.
287b077aed3SPierre Pronchery */
drbg_hash_reseed(PROV_DRBG * drbg,const unsigned char * ent,size_t ent_len,const unsigned char * adin,size_t adin_len)288b077aed3SPierre Pronchery static int drbg_hash_reseed(PROV_DRBG *drbg,
289b077aed3SPierre Pronchery const unsigned char *ent, size_t ent_len,
290b077aed3SPierre Pronchery const unsigned char *adin, size_t adin_len)
291b077aed3SPierre Pronchery {
292b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
293b077aed3SPierre Pronchery
294b077aed3SPierre Pronchery /* (Step 1-2) V = Hash_df(0x01 || V || entropy_input || additional_input) */
295b077aed3SPierre Pronchery /* V about to be updated so use C as output instead */
296b077aed3SPierre Pronchery if (!hash_df(drbg, hash->C, 0x01, hash->V, drbg->seedlen, ent, ent_len,
297b077aed3SPierre Pronchery adin, adin_len))
298b077aed3SPierre Pronchery return 0;
299b077aed3SPierre Pronchery memcpy(hash->V, hash->C, drbg->seedlen);
300b077aed3SPierre Pronchery /* (Step 4) C = Hash_df(0x00||V, seedlen) */
301b077aed3SPierre Pronchery return hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
302b077aed3SPierre Pronchery }
303b077aed3SPierre Pronchery
drbg_hash_reseed_wrapper(void * vdrbg,int prediction_resistance,const unsigned char * ent,size_t ent_len,const unsigned char * adin,size_t adin_len)304b077aed3SPierre Pronchery static int drbg_hash_reseed_wrapper(void *vdrbg, int prediction_resistance,
305b077aed3SPierre Pronchery const unsigned char *ent, size_t ent_len,
306b077aed3SPierre Pronchery const unsigned char *adin, size_t adin_len)
307b077aed3SPierre Pronchery {
308b077aed3SPierre Pronchery PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
309b077aed3SPierre Pronchery
310b077aed3SPierre Pronchery return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
311b077aed3SPierre Pronchery adin, adin_len);
312b077aed3SPierre Pronchery }
313b077aed3SPierre Pronchery
314b077aed3SPierre Pronchery /*
315b077aed3SPierre Pronchery * SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process:
316b077aed3SPierre Pronchery *
317b077aed3SPierre Pronchery * Generates pseudo random bytes using the drbg.
318b077aed3SPierre Pronchery * out is a buffer to fill with outlen bytes of pseudo random data.
319b077aed3SPierre Pronchery * addin is additional input received from an application. May be NULL.
320b077aed3SPierre Pronchery *
321b077aed3SPierre Pronchery * Returns zero if an error occurs otherwise it returns 1.
322b077aed3SPierre Pronchery */
drbg_hash_generate(PROV_DRBG * drbg,unsigned char * out,size_t outlen,const unsigned char * adin,size_t adin_len)323b077aed3SPierre Pronchery static int drbg_hash_generate(PROV_DRBG *drbg,
324b077aed3SPierre Pronchery unsigned char *out, size_t outlen,
325b077aed3SPierre Pronchery const unsigned char *adin, size_t adin_len)
326b077aed3SPierre Pronchery {
327b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
328b077aed3SPierre Pronchery unsigned char counter[4];
329b077aed3SPierre Pronchery int reseed_counter = drbg->generate_counter;
330b077aed3SPierre Pronchery
331b077aed3SPierre Pronchery counter[0] = (unsigned char)((reseed_counter >> 24) & 0xff);
332b077aed3SPierre Pronchery counter[1] = (unsigned char)((reseed_counter >> 16) & 0xff);
333b077aed3SPierre Pronchery counter[2] = (unsigned char)((reseed_counter >> 8) & 0xff);
334b077aed3SPierre Pronchery counter[3] = (unsigned char)(reseed_counter & 0xff);
335b077aed3SPierre Pronchery
336b077aed3SPierre Pronchery return hash->ctx != NULL
337b077aed3SPierre Pronchery && (adin == NULL
338b077aed3SPierre Pronchery /* (Step 2) if adin != NULL then V = V + Hash(0x02||V||adin) */
339b077aed3SPierre Pronchery || adin_len == 0
340b077aed3SPierre Pronchery || add_hash_to_v(drbg, 0x02, adin, adin_len))
341b077aed3SPierre Pronchery /* (Step 3) Hashgen(outlen, V) */
342b077aed3SPierre Pronchery && hash_gen(drbg, out, outlen)
343b077aed3SPierre Pronchery /* (Step 4/5) H = V = (V + Hash(0x03||V) mod (2^seedlen_bits) */
344b077aed3SPierre Pronchery && add_hash_to_v(drbg, 0x03, NULL, 0)
345b077aed3SPierre Pronchery /* (Step 5) V = (V + H + C + reseed_counter) mod (2^seedlen_bits) */
346b077aed3SPierre Pronchery /* V = (V + C) mod (2^seedlen_bits) */
347b077aed3SPierre Pronchery && add_bytes(drbg, hash->V, hash->C, drbg->seedlen)
348b077aed3SPierre Pronchery /* V = (V + reseed_counter) mod (2^seedlen_bits) */
349b077aed3SPierre Pronchery && add_bytes(drbg, hash->V, counter, 4);
350b077aed3SPierre Pronchery }
351b077aed3SPierre Pronchery
drbg_hash_generate_wrapper(void * vdrbg,unsigned char * out,size_t outlen,unsigned int strength,int prediction_resistance,const unsigned char * adin,size_t adin_len)352b077aed3SPierre Pronchery static int drbg_hash_generate_wrapper
353b077aed3SPierre Pronchery (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength,
354b077aed3SPierre Pronchery int prediction_resistance, const unsigned char *adin, size_t adin_len)
355b077aed3SPierre Pronchery {
356b077aed3SPierre Pronchery PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
357b077aed3SPierre Pronchery
358b077aed3SPierre Pronchery return ossl_prov_drbg_generate(drbg, out, outlen, strength,
359b077aed3SPierre Pronchery prediction_resistance, adin, adin_len);
360b077aed3SPierre Pronchery }
361b077aed3SPierre Pronchery
drbg_hash_uninstantiate(PROV_DRBG * drbg)362b077aed3SPierre Pronchery static int drbg_hash_uninstantiate(PROV_DRBG *drbg)
363b077aed3SPierre Pronchery {
364b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
365b077aed3SPierre Pronchery
366b077aed3SPierre Pronchery OPENSSL_cleanse(hash->V, sizeof(hash->V));
367b077aed3SPierre Pronchery OPENSSL_cleanse(hash->C, sizeof(hash->C));
368b077aed3SPierre Pronchery OPENSSL_cleanse(hash->vtmp, sizeof(hash->vtmp));
369b077aed3SPierre Pronchery return ossl_prov_drbg_uninstantiate(drbg);
370b077aed3SPierre Pronchery }
371b077aed3SPierre Pronchery
drbg_hash_uninstantiate_wrapper(void * vdrbg)372b077aed3SPierre Pronchery static int drbg_hash_uninstantiate_wrapper(void *vdrbg)
373b077aed3SPierre Pronchery {
374b077aed3SPierre Pronchery return drbg_hash_uninstantiate((PROV_DRBG *)vdrbg);
375b077aed3SPierre Pronchery }
376b077aed3SPierre Pronchery
drbg_hash_verify_zeroization(void * vdrbg)377b077aed3SPierre Pronchery static int drbg_hash_verify_zeroization(void *vdrbg)
378b077aed3SPierre Pronchery {
379b077aed3SPierre Pronchery PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
380b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
381b077aed3SPierre Pronchery
382b077aed3SPierre Pronchery PROV_DRBG_VERYIFY_ZEROIZATION(hash->V);
383b077aed3SPierre Pronchery PROV_DRBG_VERYIFY_ZEROIZATION(hash->C);
384b077aed3SPierre Pronchery PROV_DRBG_VERYIFY_ZEROIZATION(hash->vtmp);
385b077aed3SPierre Pronchery return 1;
386b077aed3SPierre Pronchery }
387b077aed3SPierre Pronchery
drbg_hash_new(PROV_DRBG * ctx)388b077aed3SPierre Pronchery static int drbg_hash_new(PROV_DRBG *ctx)
389b077aed3SPierre Pronchery {
390b077aed3SPierre Pronchery PROV_DRBG_HASH *hash;
391b077aed3SPierre Pronchery
392b077aed3SPierre Pronchery hash = OPENSSL_secure_zalloc(sizeof(*hash));
393b077aed3SPierre Pronchery if (hash == NULL) {
394b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
395b077aed3SPierre Pronchery return 0;
396b077aed3SPierre Pronchery }
397b077aed3SPierre Pronchery
398b077aed3SPierre Pronchery ctx->data = hash;
399b077aed3SPierre Pronchery ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
400b077aed3SPierre Pronchery ctx->max_entropylen = DRBG_MAX_LENGTH;
401b077aed3SPierre Pronchery ctx->max_noncelen = DRBG_MAX_LENGTH;
402b077aed3SPierre Pronchery ctx->max_perslen = DRBG_MAX_LENGTH;
403b077aed3SPierre Pronchery ctx->max_adinlen = DRBG_MAX_LENGTH;
404b077aed3SPierre Pronchery
405b077aed3SPierre Pronchery /* Maximum number of bits per request = 2^19 = 2^16 bytes */
406b077aed3SPierre Pronchery ctx->max_request = 1 << 16;
407b077aed3SPierre Pronchery return 1;
408b077aed3SPierre Pronchery }
409b077aed3SPierre Pronchery
drbg_hash_new_wrapper(void * provctx,void * parent,const OSSL_DISPATCH * parent_dispatch)410b077aed3SPierre Pronchery static void *drbg_hash_new_wrapper(void *provctx, void *parent,
411b077aed3SPierre Pronchery const OSSL_DISPATCH *parent_dispatch)
412b077aed3SPierre Pronchery {
413*44096ebdSEnji Cooper return ossl_rand_drbg_new(provctx, parent, parent_dispatch,
414*44096ebdSEnji Cooper &drbg_hash_new, &drbg_hash_free,
415b077aed3SPierre Pronchery &drbg_hash_instantiate, &drbg_hash_uninstantiate,
416b077aed3SPierre Pronchery &drbg_hash_reseed, &drbg_hash_generate);
417b077aed3SPierre Pronchery }
418b077aed3SPierre Pronchery
drbg_hash_free(void * vdrbg)419b077aed3SPierre Pronchery static void drbg_hash_free(void *vdrbg)
420b077aed3SPierre Pronchery {
421b077aed3SPierre Pronchery PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
422b077aed3SPierre Pronchery PROV_DRBG_HASH *hash;
423b077aed3SPierre Pronchery
424b077aed3SPierre Pronchery if (drbg != NULL && (hash = (PROV_DRBG_HASH *)drbg->data) != NULL) {
425b077aed3SPierre Pronchery EVP_MD_CTX_free(hash->ctx);
426b077aed3SPierre Pronchery ossl_prov_digest_reset(&hash->digest);
427b077aed3SPierre Pronchery OPENSSL_secure_clear_free(hash, sizeof(*hash));
428b077aed3SPierre Pronchery }
429b077aed3SPierre Pronchery ossl_rand_drbg_free(drbg);
430b077aed3SPierre Pronchery }
431b077aed3SPierre Pronchery
drbg_hash_get_ctx_params(void * vdrbg,OSSL_PARAM params[])432b077aed3SPierre Pronchery static int drbg_hash_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
433b077aed3SPierre Pronchery {
434b077aed3SPierre Pronchery PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
435b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
436b077aed3SPierre Pronchery const EVP_MD *md;
437b077aed3SPierre Pronchery OSSL_PARAM *p;
438b077aed3SPierre Pronchery
439b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST);
440b077aed3SPierre Pronchery if (p != NULL) {
441b077aed3SPierre Pronchery md = ossl_prov_digest_md(&hash->digest);
442b077aed3SPierre Pronchery if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md)))
443b077aed3SPierre Pronchery return 0;
444b077aed3SPierre Pronchery }
445b077aed3SPierre Pronchery
446b077aed3SPierre Pronchery return ossl_drbg_get_ctx_params(drbg, params);
447b077aed3SPierre Pronchery }
448b077aed3SPierre Pronchery
drbg_hash_gettable_ctx_params(ossl_unused void * vctx,ossl_unused void * p_ctx)449b077aed3SPierre Pronchery static const OSSL_PARAM *drbg_hash_gettable_ctx_params(ossl_unused void *vctx,
450b077aed3SPierre Pronchery ossl_unused void *p_ctx)
451b077aed3SPierre Pronchery {
452b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = {
453b077aed3SPierre Pronchery OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
454b077aed3SPierre Pronchery OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
455b077aed3SPierre Pronchery OSSL_PARAM_END
456b077aed3SPierre Pronchery };
457b077aed3SPierre Pronchery return known_gettable_ctx_params;
458b077aed3SPierre Pronchery }
459b077aed3SPierre Pronchery
drbg_hash_set_ctx_params(void * vctx,const OSSL_PARAM params[])460b077aed3SPierre Pronchery static int drbg_hash_set_ctx_params(void *vctx, const OSSL_PARAM params[])
461b077aed3SPierre Pronchery {
462b077aed3SPierre Pronchery PROV_DRBG *ctx = (PROV_DRBG *)vctx;
463b077aed3SPierre Pronchery PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)ctx->data;
464b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
465b077aed3SPierre Pronchery const EVP_MD *md;
466b077aed3SPierre Pronchery
467b077aed3SPierre Pronchery if (!ossl_prov_digest_load_from_params(&hash->digest, params, libctx))
468b077aed3SPierre Pronchery return 0;
469b077aed3SPierre Pronchery
470b077aed3SPierre Pronchery md = ossl_prov_digest_md(&hash->digest);
471b077aed3SPierre Pronchery if (md != NULL) {
472b077aed3SPierre Pronchery if ((EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF) != 0) {
473b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
474b077aed3SPierre Pronchery return 0;
475b077aed3SPierre Pronchery }
476b077aed3SPierre Pronchery
477b077aed3SPierre Pronchery /* These are taken from SP 800-90 10.1 Table 2 */
478b077aed3SPierre Pronchery hash->blocklen = EVP_MD_get_size(md);
479b077aed3SPierre Pronchery /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
480b077aed3SPierre Pronchery ctx->strength = 64 * (hash->blocklen >> 3);
481b077aed3SPierre Pronchery if (ctx->strength > 256)
482b077aed3SPierre Pronchery ctx->strength = 256;
483b077aed3SPierre Pronchery if (hash->blocklen > MAX_BLOCKLEN_USING_SMALL_SEEDLEN)
484b077aed3SPierre Pronchery ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
485b077aed3SPierre Pronchery else
486b077aed3SPierre Pronchery ctx->seedlen = HASH_PRNG_SMALL_SEEDLEN;
487b077aed3SPierre Pronchery
488b077aed3SPierre Pronchery ctx->min_entropylen = ctx->strength / 8;
489b077aed3SPierre Pronchery ctx->min_noncelen = ctx->min_entropylen / 2;
490b077aed3SPierre Pronchery }
491b077aed3SPierre Pronchery
492b077aed3SPierre Pronchery return ossl_drbg_set_ctx_params(ctx, params);
493b077aed3SPierre Pronchery }
494b077aed3SPierre Pronchery
drbg_hash_settable_ctx_params(ossl_unused void * vctx,ossl_unused void * p_ctx)495b077aed3SPierre Pronchery static const OSSL_PARAM *drbg_hash_settable_ctx_params(ossl_unused void *vctx,
496b077aed3SPierre Pronchery ossl_unused void *p_ctx)
497b077aed3SPierre Pronchery {
498b077aed3SPierre Pronchery static const OSSL_PARAM known_settable_ctx_params[] = {
499b077aed3SPierre Pronchery OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
500b077aed3SPierre Pronchery OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
501b077aed3SPierre Pronchery OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
502b077aed3SPierre Pronchery OSSL_PARAM_END
503b077aed3SPierre Pronchery };
504b077aed3SPierre Pronchery return known_settable_ctx_params;
505b077aed3SPierre Pronchery }
506b077aed3SPierre Pronchery
507b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_drbg_hash_functions[] = {
508b077aed3SPierre Pronchery { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hash_new_wrapper },
509b077aed3SPierre Pronchery { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hash_free },
510b077aed3SPierre Pronchery { OSSL_FUNC_RAND_INSTANTIATE,
511b077aed3SPierre Pronchery (void(*)(void))drbg_hash_instantiate_wrapper },
512b077aed3SPierre Pronchery { OSSL_FUNC_RAND_UNINSTANTIATE,
513b077aed3SPierre Pronchery (void(*)(void))drbg_hash_uninstantiate_wrapper },
514b077aed3SPierre Pronchery { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hash_generate_wrapper },
515b077aed3SPierre Pronchery { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hash_reseed_wrapper },
516b077aed3SPierre Pronchery { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
517b077aed3SPierre Pronchery { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
518b077aed3SPierre Pronchery { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
519b077aed3SPierre Pronchery { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
520b077aed3SPierre Pronchery (void(*)(void))drbg_hash_settable_ctx_params },
521b077aed3SPierre Pronchery { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hash_set_ctx_params },
522b077aed3SPierre Pronchery { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
523b077aed3SPierre Pronchery (void(*)(void))drbg_hash_gettable_ctx_params },
524b077aed3SPierre Pronchery { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hash_get_ctx_params },
525b077aed3SPierre Pronchery { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
526b077aed3SPierre Pronchery (void(*)(void))drbg_hash_verify_zeroization },
527b077aed3SPierre Pronchery { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
528b077aed3SPierre Pronchery { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
529b077aed3SPierre Pronchery { 0, NULL }
530b077aed3SPierre Pronchery };
531