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