2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
13 #include <openssl/sha.h>
14 #include <openssl/crypto.h>
15 #include <openssl/err.h>
16 #include <openssl/rand.h>
17 #include <openssl/core_numbers.h>
18 #include "internal/thread_once.h"
19 #include "prov/providercommon.h"
20 #include "prov/provider_ctx.h"
21 #include "prov/provider_util.h"
22 #include "prov/implementations.h"
23 #include "prov/providercommonerr.h"
24 #include "drbg_local.h"
26 static OSSL_OP_rand_newctx_fn drbg_hash_new_wrapper;
27 static OSSL_OP_rand_freectx_fn drbg_hash_free;
28 static OSSL_OP_rand_instantiate_fn drbg_hash_instantiate_wrapper;
29 static OSSL_OP_rand_uninstantiate_fn drbg_hash_uninstantiate_wrapper;
30 static OSSL_OP_rand_generate_fn drbg_hash_generate_wrapper;
31 static OSSL_OP_rand_reseed_fn drbg_hash_reseed_wrapper;
32 static OSSL_OP_rand_settable_ctx_params_fn drbg_hash_settable_ctx_params;
33 static OSSL_OP_rand_set_ctx_params_fn drbg_hash_set_ctx_params;
34 static OSSL_OP_rand_gettable_ctx_params_fn drbg_hash_gettable_ctx_params;
35 static OSSL_OP_rand_get_ctx_params_fn drbg_hash_get_ctx_params;
36 static OSSL_OP_rand_verify_zeroization_fn drbg_hash_verify_zeroization;
38 /* 888 bits from SP800-90Ar1 10.1 table 2 */
39 #define HASH_PRNG_MAX_SEEDLEN (888/8)
41 /* 440 bits from SP800-90Ar1 10.1 table 2 */
42 #define HASH_PRNG_SMALL_SEEDLEN (440/8)
44 /* Determine what seedlen to use based on the block length */
45 #define MAX_BLOCKLEN_USING_SMALL_SEEDLEN (256/8)
46 #define INBYTE_IGNORE ((unsigned char)0xFF)
48 typedef struct rand_drbg_hash_st {
52 unsigned char V[HASH_PRNG_MAX_SEEDLEN];
53 unsigned char C[HASH_PRNG_MAX_SEEDLEN];
54 /* Temporary value storage: should always exceed max digest length */
55 unsigned char vtmp[HASH_PRNG_MAX_SEEDLEN];
59 * SP800-90Ar1 10.3.1 Derivation function using a Hash Function (Hash_df).
60 * The input string used is composed of:
61 * inbyte - An optional leading byte (ignore if equal to INBYTE_IGNORE)
62 * in - input string 1 (A Non NULL value).
63 * in2 - optional input string (Can be NULL).
64 * in3 - optional input string (Can be NULL).
65 * These are concatenated as part of the DigestUpdate process.
67 static int hash_df(PROV_DRBG *drbg, unsigned char *out,
68 const unsigned char inbyte,
69 const unsigned char *in, size_t inlen,
70 const unsigned char *in2, size_t in2len,
71 const unsigned char *in3, size_t in3len)
73 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
74 EVP_MD_CTX *ctx = hash->ctx;
75 unsigned char *vtmp = hash->vtmp;
76 /* tmp = counter || num_bits_returned || [inbyte] */
77 unsigned char tmp[1 + 4 + 1];
79 size_t outlen = drbg->seedlen;
80 size_t num_bits_returned = outlen * 8;
82 * No need to check outlen size here, as the standard only ever needs
83 * seedlen bytes which is always less than the maximum permitted.
86 /* (Step 3) counter = 1 (tmp[0] is the 8 bit counter) */
88 /* tmp[1..4] is the fixed 32 bit no_of_bits_to_return */
89 tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 24) & 0xff);
90 tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 16) & 0xff);
91 tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 8) & 0xff);
92 tmp[tmp_sz++] = (unsigned char)(num_bits_returned & 0xff);
93 /* Tack the additional input byte onto the end of tmp if it exists */
94 if (inbyte != INBYTE_IGNORE)
95 tmp[tmp_sz++] = inbyte;
100 * (Step 4.1) out = out || Hash(tmp || in || [in2] || [in3])
101 * (where tmp = counter || num_bits_returned || [inbyte])
103 if (!(EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
104 && EVP_DigestUpdate(ctx, tmp, tmp_sz)
105 && EVP_DigestUpdate(ctx, in, inlen)
106 && (in2 == NULL || EVP_DigestUpdate(ctx, in2, in2len))
107 && (in3 == NULL || EVP_DigestUpdate(ctx, in3, in3len))))
110 if (outlen < hash->blocklen) {
111 if (!EVP_DigestFinal(ctx, vtmp, NULL))
113 memcpy(out, vtmp, outlen);
114 OPENSSL_cleanse(vtmp, hash->blocklen);
116 } else if(!EVP_DigestFinal(ctx, out, NULL)) {
120 outlen -= hash->blocklen;
123 /* (Step 4.2) counter++ */
125 out += hash->blocklen;
130 /* Helper function that just passes 2 input parameters to hash_df() */
131 static int hash_df1(PROV_DRBG *drbg, unsigned char *out,
132 const unsigned char in_byte,
133 const unsigned char *in1, size_t in1len)
135 return hash_df(drbg, out, in_byte, in1, in1len, NULL, 0, NULL, 0);
139 * Add 2 byte buffers together. The first elements in each buffer are the top
140 * most bytes. The result is stored in the dst buffer.
141 * The final carry is ignored i.e: dst = (dst + in) mod (2^seedlen_bits).
142 * where dst size is drbg->seedlen, and inlen <= drbg->seedlen.
144 static int add_bytes(PROV_DRBG *drbg, unsigned char *dst,
145 unsigned char *in, size_t inlen)
149 const unsigned char *add;
150 unsigned char carry = 0, *d;
152 assert(drbg->seedlen >= 1 && inlen >= 1 && inlen <= drbg->seedlen);
154 d = &dst[drbg->seedlen - 1];
155 add = &in[inlen - 1];
157 for (i = inlen; i > 0; i--, d--, add--) {
158 result = *d + *add + carry;
159 carry = (unsigned char)(result >> 8);
160 *d = (unsigned char)(result & 0xff);
164 /* Add the carry to the top of the dst if inlen is not the same size */
165 for (i = drbg->seedlen - inlen; i > 0; --i, d--) {
166 *d += 1; /* Carry can only be 1 */
167 if (*d != 0) /* exit if carry doesnt propagate to the next byte */
174 /* V = (V + Hash(inbyte || V || [additional_input]) mod (2^seedlen) */
175 static int add_hash_to_v(PROV_DRBG *drbg, unsigned char inbyte,
176 const unsigned char *adin, size_t adinlen)
178 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
179 EVP_MD_CTX *ctx = hash->ctx;
181 return EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL)
182 && EVP_DigestUpdate(ctx, &inbyte, 1)
183 && EVP_DigestUpdate(ctx, hash->V, drbg->seedlen)
184 && (adin == NULL || EVP_DigestUpdate(ctx, adin, adinlen))
185 && EVP_DigestFinal(ctx, hash->vtmp, NULL)
186 && add_bytes(drbg, hash->V, hash->vtmp, hash->blocklen);
190 * The Hashgen() as listed in SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process.
192 * drbg contains the current value of V.
193 * outlen is the requested number of bytes.
194 * out is a buffer to return the generated bits.
196 * The algorithm to generate the bits is:
200 * W = W || Hash(data)
201 * data = (data + 1) mod (2^seedlen)
203 * out = Leftmost(W, outlen)
205 * Returns zero if an error occurs otherwise it returns 1.
207 static int hash_gen(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
209 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
210 unsigned char one = 1;
214 memcpy(hash->vtmp, hash->V, drbg->seedlen);
216 if (!EVP_DigestInit_ex(hash->ctx, ossl_prov_digest_md(&hash->digest),
218 || !EVP_DigestUpdate(hash->ctx, hash->vtmp, drbg->seedlen))
221 if (outlen < hash->blocklen) {
222 if (!EVP_DigestFinal(hash->ctx, hash->vtmp, NULL))
224 memcpy(out, hash->vtmp, outlen);
227 if (!EVP_DigestFinal(hash->ctx, out, NULL))
229 outlen -= hash->blocklen;
232 out += hash->blocklen;
234 add_bytes(drbg, hash->vtmp, &one, 1);
240 * SP800-90Ar1 10.1.1.2 Hash_DRBG_Instantiate_Process:
242 * ent is entropy input obtained from a randomness source of length ent_len.
243 * nonce is a string of bytes of length nonce_len.
244 * pstr is a personalization string received from an application. May be NULL.
246 * Returns zero if an error occurs otherwise it returns 1.
248 static int drbg_hash_instantiate(PROV_DRBG *drbg,
249 const unsigned char *ent, size_t ent_len,
250 const unsigned char *nonce, size_t nonce_len,
251 const unsigned char *pstr, size_t pstr_len)
253 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
255 EVP_MD_CTX_free(hash->ctx);
256 hash->ctx = EVP_MD_CTX_new();
258 /* (Step 1-3) V = Hash_df(entropy||nonce||pers, seedlen) */
259 return hash->ctx != NULL
260 && hash_df(drbg, hash->V, INBYTE_IGNORE,
261 ent, ent_len, nonce, nonce_len, pstr, pstr_len)
262 /* (Step 4) C = Hash_df(0x00||V, seedlen) */
263 && hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
266 static int drbg_hash_instantiate_wrapper(void *vdrbg, unsigned int strength,
267 int prediction_resistance,
268 const unsigned char *pstr,
271 PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
273 return PROV_DRBG_instantiate(drbg, strength, prediction_resistance,
278 * SP800-90Ar1 10.1.1.3 Hash_DRBG_Reseed_Process:
280 * ent is entropy input bytes obtained from a randomness source.
281 * addin is additional input received from an application. May be NULL.
283 * Returns zero if an error occurs otherwise it returns 1.
285 static int drbg_hash_reseed(PROV_DRBG *drbg,
286 const unsigned char *ent, size_t ent_len,
287 const unsigned char *adin, size_t adin_len)
289 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
291 /* (Step 1-2) V = Hash_df(0x01 || V || entropy_input || additional_input) */
292 /* V about to be updated so use C as output instead */
293 if (!hash_df(drbg, hash->C, 0x01, hash->V, drbg->seedlen, ent, ent_len,
296 memcpy(hash->V, hash->C, drbg->seedlen);
297 /* (Step 4) C = Hash_df(0x00||V, seedlen) */
298 return hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen);
301 static int drbg_hash_reseed_wrapper(void *vdrbg, int prediction_resistance,
302 const unsigned char *ent, size_t ent_len,
303 const unsigned char *adin, size_t adin_len)
305 PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
307 return PROV_DRBG_reseed(drbg, prediction_resistance, ent, ent_len,
312 * SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process:
314 * Generates pseudo random bytes using the drbg.
315 * out is a buffer to fill with outlen bytes of pseudo random data.
316 * addin is additional input received from an application. May be NULL.
318 * Returns zero if an error occurs otherwise it returns 1.
320 static int drbg_hash_generate(PROV_DRBG *drbg,
321 unsigned char *out, size_t outlen,
322 const unsigned char *adin, size_t adin_len)
324 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
325 unsigned char counter[4];
326 int reseed_counter = drbg->reseed_gen_counter;
328 counter[0] = (unsigned char)((reseed_counter >> 24) & 0xff);
329 counter[1] = (unsigned char)((reseed_counter >> 16) & 0xff);
330 counter[2] = (unsigned char)((reseed_counter >> 8) & 0xff);
331 counter[3] = (unsigned char)(reseed_counter & 0xff);
333 return hash->ctx != NULL
335 /* (Step 2) if adin != NULL then V = V + Hash(0x02||V||adin) */
337 || add_hash_to_v(drbg, 0x02, adin, adin_len))
338 /* (Step 3) Hashgen(outlen, V) */
339 && hash_gen(drbg, out, outlen)
340 /* (Step 4/5) H = V = (V + Hash(0x03||V) mod (2^seedlen_bits) */
341 && add_hash_to_v(drbg, 0x03, NULL, 0)
342 /* (Step 5) V = (V + H + C + reseed_counter) mod (2^seedlen_bits) */
343 /* V = (V + C) mod (2^seedlen_bits) */
344 && add_bytes(drbg, hash->V, hash->C, drbg->seedlen)
345 /* V = (V + reseed_counter) mod (2^seedlen_bits) */
346 && add_bytes(drbg, hash->V, counter, 4);
349 static int drbg_hash_generate_wrapper
350 (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength,
351 int prediction_resistance, const unsigned char *adin, size_t adin_len)
353 PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
355 return PROV_DRBG_generate(drbg, out, outlen, strength,
356 prediction_resistance, adin, adin_len);
359 static int drbg_hash_uninstantiate(PROV_DRBG *drbg)
361 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
363 OPENSSL_cleanse(hash->V, sizeof(hash->V));
364 OPENSSL_cleanse(hash->C, sizeof(hash->C));
365 OPENSSL_cleanse(hash->vtmp, sizeof(hash->vtmp));
366 return PROV_DRBG_uninstantiate(drbg);
369 static int drbg_hash_uninstantiate_wrapper(void *vdrbg)
371 return drbg_hash_uninstantiate((PROV_DRBG *)vdrbg);
374 static int drbg_hash_verify_zeroization(void *vdrbg)
376 PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
377 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data;
379 PROV_DRBG_VERYIFY_ZEROIZATION(hash->V);
380 PROV_DRBG_VERYIFY_ZEROIZATION(hash->C);
381 PROV_DRBG_VERYIFY_ZEROIZATION(hash->vtmp);
385 static int drbg_hash_new(PROV_DRBG *ctx)
387 PROV_DRBG_HASH *hash;
389 hash = OPENSSL_secure_zalloc(sizeof(*hash));
391 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
396 ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
397 ctx->max_entropylen = DRBG_MAX_LENGTH;
398 ctx->max_noncelen = DRBG_MAX_LENGTH;
399 ctx->max_perslen = DRBG_MAX_LENGTH;
400 ctx->max_adinlen = DRBG_MAX_LENGTH;
402 /* Maximum number of bits per request = 2^19 = 2^16 bytes */
403 ctx->max_request = 1 << 16;
407 static void *drbg_hash_new_wrapper(void *provctx, void *parent,
408 const OSSL_DISPATCH *parent_dispatch)
410 return prov_rand_drbg_new(provctx, parent, parent_dispatch, &drbg_hash_new,
411 &drbg_hash_instantiate, &drbg_hash_uninstantiate,
412 &drbg_hash_reseed, &drbg_hash_generate);
415 static void drbg_hash_free(void *vdrbg)
417 PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
418 PROV_DRBG_HASH *hash;
420 if (drbg != NULL && (hash = (PROV_DRBG_HASH *)drbg->data) != NULL) {
421 EVP_MD_CTX_free(hash->ctx);
422 ossl_prov_digest_reset(&hash->digest);
423 OPENSSL_secure_clear_free(hash, sizeof(*hash));
425 prov_rand_drbg_free(drbg);
428 static int drbg_hash_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
430 PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
432 return drbg_get_ctx_params(drbg, params);
435 static const OSSL_PARAM *drbg_hash_gettable_ctx_params(void)
437 static const OSSL_PARAM known_gettable_ctx_params[] = {
438 OSSL_PARAM_DRBG_GETABLE_CTX_COMMON,
441 return known_gettable_ctx_params;
444 static int drbg_hash_set_ctx_params(void *vctx, const OSSL_PARAM params[])
446 PROV_DRBG *ctx = (PROV_DRBG *)vctx;
447 PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)ctx->data;
448 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
451 if (!ossl_prov_digest_load_from_params(&hash->digest, params, libctx))
454 md = ossl_prov_digest_md(&hash->digest);
456 if ((EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0) {
457 ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
461 /* These are taken from SP 800-90 10.1 Table 2 */
462 hash->blocklen = EVP_MD_size(md);
463 /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
464 ctx->strength = 64 * (hash->blocklen >> 3);
465 if (ctx->strength > 256)
467 if (hash->blocklen > MAX_BLOCKLEN_USING_SMALL_SEEDLEN)
468 ctx->seedlen = HASH_PRNG_MAX_SEEDLEN;
470 ctx->seedlen = HASH_PRNG_SMALL_SEEDLEN;
472 ctx->min_entropylen = ctx->strength / 8;
473 ctx->min_noncelen = ctx->min_entropylen / 2;
476 return drbg_set_ctx_params(ctx, params);
479 static const OSSL_PARAM *drbg_hash_settable_ctx_params(void)
481 static const OSSL_PARAM known_settable_ctx_params[] = {
482 OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
483 OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
484 OSSL_PARAM_DRBG_SETABLE_CTX_COMMON,
487 return known_settable_ctx_params;
490 const OSSL_DISPATCH drbg_hash_functions[] = {
491 { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hash_new_wrapper },
492 { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hash_free },
493 { OSSL_FUNC_RAND_INSTANTIATE,
494 (void(*)(void))drbg_hash_instantiate_wrapper },
495 { OSSL_FUNC_RAND_UNINSTANTIATE,
496 (void(*)(void))drbg_hash_uninstantiate_wrapper },
497 { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hash_generate_wrapper },
498 { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hash_reseed_wrapper },
499 { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))drbg_enable_locking },
500 { OSSL_FUNC_RAND_LOCK, (void(*)(void))drbg_lock },
501 { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))drbg_unlock },
502 { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
503 (void(*)(void))drbg_hash_settable_ctx_params },
504 { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hash_set_ctx_params },
505 { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
506 (void(*)(void))drbg_hash_gettable_ctx_params },
507 { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hash_get_ctx_params },
508 { OSSL_FUNC_RAND_SET_CALLBACKS, (void(*)(void))drbg_set_callbacks },
509 { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
510 (void(*)(void))drbg_hash_verify_zeroization },