Path: blob/main/crypto/openssl/providers/implementations/rands/drbg_ctr.c
106751 views
/*1* Copyright 2011-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89#include <stdlib.h>10#include <string.h>11#include <openssl/crypto.h>12#include <openssl/err.h>13#include <openssl/rand.h>14#include <openssl/aes.h>15#include <openssl/proverr.h>16#include "crypto/modes.h"17#include "internal/thread_once.h"18#include "prov/implementations.h"19#include "prov/providercommon.h"20#include "prov/provider_ctx.h"21#include "drbg_local.h"22#include "crypto/evp.h"23#include "crypto/evp/evp_local.h"24#include "internal/provider.h"25#include "internal/common.h"2627static OSSL_FUNC_rand_newctx_fn drbg_ctr_new_wrapper;28static OSSL_FUNC_rand_freectx_fn drbg_ctr_free;29static OSSL_FUNC_rand_instantiate_fn drbg_ctr_instantiate_wrapper;30static OSSL_FUNC_rand_uninstantiate_fn drbg_ctr_uninstantiate_wrapper;31static OSSL_FUNC_rand_generate_fn drbg_ctr_generate_wrapper;32static OSSL_FUNC_rand_reseed_fn drbg_ctr_reseed_wrapper;33static OSSL_FUNC_rand_settable_ctx_params_fn drbg_ctr_settable_ctx_params;34static OSSL_FUNC_rand_set_ctx_params_fn drbg_ctr_set_ctx_params;35static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_ctr_gettable_ctx_params;36static OSSL_FUNC_rand_get_ctx_params_fn drbg_ctr_get_ctx_params;37static OSSL_FUNC_rand_verify_zeroization_fn drbg_ctr_verify_zeroization;3839static int drbg_ctr_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[]);4041/*42* The state of a DRBG AES-CTR.43*/44typedef struct rand_drbg_ctr_st {45EVP_CIPHER_CTX *ctx_ecb;46EVP_CIPHER_CTX *ctx_ctr;47EVP_CIPHER_CTX *ctx_df;48EVP_CIPHER *cipher_ecb;49EVP_CIPHER *cipher_ctr;50size_t keylen;51int use_df;52unsigned char K[32];53unsigned char V[16];54/* Temporary block storage used by ctr_df */55unsigned char bltmp[16];56size_t bltmp_pos;57unsigned char KX[48];58} PROV_DRBG_CTR;5960/*61* Implementation of NIST SP 800-90A CTR DRBG.62*/63static void inc_128(PROV_DRBG_CTR *ctr)64{65unsigned char *p = &ctr->V[0];66u32 n = 16, c = 1;6768do {69--n;70c += p[n];71p[n] = (u8)c;72c >>= 8;73} while (n);74}7576static void ctr_XOR(PROV_DRBG_CTR *ctr, const unsigned char *in, size_t inlen)77{78size_t i, n;7980if (in == NULL || inlen == 0)81return;8283/*84* Any zero padding will have no effect on the result as we85* are XORing. So just process however much input we have.86*/87n = inlen < ctr->keylen ? inlen : ctr->keylen;88if (!ossl_assert(n <= sizeof(ctr->K)))89return;90for (i = 0; i < n; i++)91ctr->K[i] ^= in[i];92if (inlen <= ctr->keylen)93return;9495n = inlen - ctr->keylen;96if (n > 16) {97/* Should never happen */98n = 16;99}100for (i = 0; i < n; i++)101ctr->V[i] ^= in[i + ctr->keylen];102}103104/*105* Process a complete block using BCC algorithm of SP 800-90A 10.3.3106*/107__owur static int ctr_BCC_block(PROV_DRBG_CTR *ctr, unsigned char *out,108const unsigned char *in, int len)109{110int i, outlen = AES_BLOCK_SIZE;111112for (i = 0; i < len; i++)113out[i] ^= in[i];114115if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, len)116|| outlen != len)117return 0;118return 1;119}120121/*122* Handle several BCC operations for as much data as we need for K and X123*/124__owur static int ctr_BCC_blocks(PROV_DRBG_CTR *ctr, const unsigned char *in)125{126unsigned char in_tmp[48];127unsigned char num_of_blk = 2;128129memcpy(in_tmp, in, 16);130memcpy(in_tmp + 16, in, 16);131if (ctr->keylen != 16) {132memcpy(in_tmp + 32, in, 16);133num_of_blk = 3;134}135return ctr_BCC_block(ctr, ctr->KX, in_tmp, AES_BLOCK_SIZE * num_of_blk);136}137138/*139* Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:140* see 10.3.1 stage 7.141*/142__owur static int ctr_BCC_init(PROV_DRBG_CTR *ctr)143{144unsigned char bltmp[48] = { 0 };145unsigned char num_of_blk;146147memset(ctr->KX, 0, 48);148num_of_blk = ctr->keylen == 16 ? 2 : 3;149bltmp[(AES_BLOCK_SIZE * 1) + 3] = 1;150bltmp[(AES_BLOCK_SIZE * 2) + 3] = 2;151return ctr_BCC_block(ctr, ctr->KX, bltmp, num_of_blk * AES_BLOCK_SIZE);152}153154/*155* Process several blocks into BCC algorithm, some possibly partial156*/157__owur static int ctr_BCC_update(PROV_DRBG_CTR *ctr,158const unsigned char *in, size_t inlen)159{160if (in == NULL || inlen == 0)161return 1;162163/* If we have partial block handle it first */164if (ctr->bltmp_pos) {165size_t left = 16 - ctr->bltmp_pos;166167/* If we now have a complete block process it */168if (inlen >= left) {169memcpy(ctr->bltmp + ctr->bltmp_pos, in, left);170if (!ctr_BCC_blocks(ctr, ctr->bltmp))171return 0;172ctr->bltmp_pos = 0;173inlen -= left;174in += left;175}176}177178/* Process zero or more complete blocks */179for (; inlen >= 16; in += 16, inlen -= 16) {180if (!ctr_BCC_blocks(ctr, in))181return 0;182}183184/* Copy any remaining partial block to the temporary buffer */185if (inlen > 0) {186memcpy(ctr->bltmp + ctr->bltmp_pos, in, inlen);187ctr->bltmp_pos += inlen;188}189return 1;190}191192__owur static int ctr_BCC_final(PROV_DRBG_CTR *ctr)193{194if (ctr->bltmp_pos) {195memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos);196if (!ctr_BCC_blocks(ctr, ctr->bltmp))197return 0;198}199return 1;200}201202__owur static int ctr_df(PROV_DRBG_CTR *ctr,203const unsigned char *in1, size_t in1len,204const unsigned char *in2, size_t in2len,205const unsigned char *in3, size_t in3len)206{207static unsigned char c80 = 0x80;208size_t inlen;209unsigned char *p = ctr->bltmp;210int outlen = AES_BLOCK_SIZE;211212if (!ctr_BCC_init(ctr))213return 0;214if (in1 == NULL)215in1len = 0;216if (in2 == NULL)217in2len = 0;218if (in3 == NULL)219in3len = 0;220inlen = in1len + in2len + in3len;221/* Initialise L||N in temporary block */222*p++ = (inlen >> 24) & 0xff;223*p++ = (inlen >> 16) & 0xff;224*p++ = (inlen >> 8) & 0xff;225*p++ = inlen & 0xff;226227/* NB keylen is at most 32 bytes */228*p++ = 0;229*p++ = 0;230*p++ = 0;231*p = (unsigned char)((ctr->keylen + 16) & 0xff);232ctr->bltmp_pos = 8;233if (!ctr_BCC_update(ctr, in1, in1len)234|| !ctr_BCC_update(ctr, in2, in2len)235|| !ctr_BCC_update(ctr, in3, in3len)236|| !ctr_BCC_update(ctr, &c80, 1)237|| !ctr_BCC_final(ctr))238return 0;239/* Set up key K */240if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->KX, NULL, -1))241return 0;242/* X follows key K */243if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX, &outlen, ctr->KX + ctr->keylen,244AES_BLOCK_SIZE)245|| outlen != AES_BLOCK_SIZE)246return 0;247if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 16, &outlen, ctr->KX,248AES_BLOCK_SIZE)249|| outlen != AES_BLOCK_SIZE)250return 0;251if (ctr->keylen != 16)252if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 32, &outlen,253ctr->KX + 16, AES_BLOCK_SIZE)254|| outlen != AES_BLOCK_SIZE)255return 0;256return 1;257}258259/*260* NB the no-df Update in SP800-90A specifies a constant input length261* of seedlen, however other uses of this algorithm pad the input with262* zeroes if necessary and have up to two parameters XORed together,263* so we handle both cases in this function instead.264*/265__owur static int ctr_update(PROV_DRBG *drbg,266const unsigned char *in1, size_t in1len,267const unsigned char *in2, size_t in2len,268const unsigned char *nonce, size_t noncelen)269{270PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;271int outlen = AES_BLOCK_SIZE;272unsigned char V_tmp[48], out[48];273unsigned char len;274275/* correct key is already set up. */276memcpy(V_tmp, ctr->V, 16);277inc_128(ctr);278memcpy(V_tmp + 16, ctr->V, 16);279if (ctr->keylen == 16) {280len = 32;281} else {282inc_128(ctr);283memcpy(V_tmp + 32, ctr->V, 16);284len = 48;285}286if (!EVP_CipherUpdate(ctr->ctx_ecb, out, &outlen, V_tmp, len)287|| outlen != len)288return 0;289memcpy(ctr->K, out, ctr->keylen);290memcpy(ctr->V, out + ctr->keylen, 16);291292if (ctr->use_df) {293/* If no input reuse existing derived value */294if (in1 != NULL || nonce != NULL || in2 != NULL)295if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))296return 0;297/* If this a reuse input in1len != 0 */298if (in1len)299ctr_XOR(ctr, ctr->KX, drbg->seedlen);300} else {301ctr_XOR(ctr, in1, in1len);302ctr_XOR(ctr, in2, in2len);303}304305if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1)306|| !EVP_CipherInit_ex(ctr->ctx_ctr, NULL, NULL, ctr->K, NULL, -1))307return 0;308return 1;309}310311static int drbg_ctr_instantiate(PROV_DRBG *drbg,312const unsigned char *entropy, size_t entropylen,313const unsigned char *nonce, size_t noncelen,314const unsigned char *pers, size_t perslen)315{316PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;317318if (entropy == NULL)319return 0;320321memset(ctr->K, 0, sizeof(ctr->K));322memset(ctr->V, 0, sizeof(ctr->V));323if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1))324return 0;325326inc_128(ctr);327if (!ctr_update(drbg, entropy, entropylen, pers, perslen, nonce, noncelen))328return 0;329return 1;330}331332static int drbg_ctr_instantiate_wrapper(void *vdrbg, unsigned int strength,333int prediction_resistance,334const unsigned char *pstr,335size_t pstr_len,336const OSSL_PARAM params[])337{338PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;339int ret = 0;340341if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))342return 0;343344if (!ossl_prov_is_running()345|| !drbg_ctr_set_ctx_params_locked(drbg, params))346goto err;347ret = ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,348pstr, pstr_len);349err:350if (drbg->lock != NULL)351CRYPTO_THREAD_unlock(drbg->lock);352return ret;353}354355static int drbg_ctr_reseed(PROV_DRBG *drbg,356const unsigned char *entropy, size_t entropylen,357const unsigned char *adin, size_t adinlen)358{359PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;360361if (entropy == NULL)362return 0;363364inc_128(ctr);365if (!ctr_update(drbg, entropy, entropylen, adin, adinlen, NULL, 0))366return 0;367return 1;368}369370static int drbg_ctr_reseed_wrapper(void *vdrbg, int prediction_resistance,371const unsigned char *ent, size_t ent_len,372const unsigned char *adin, size_t adin_len)373{374PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;375376return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,377adin, adin_len);378}379380static void ctr96_inc(unsigned char *counter)381{382u32 n = 12, c = 1;383384do {385--n;386c += counter[n];387counter[n] = (u8)c;388c >>= 8;389} while (n);390}391392static int drbg_ctr_generate(PROV_DRBG *drbg,393unsigned char *out, size_t outlen,394const unsigned char *adin, size_t adinlen)395{396PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;397unsigned int ctr32, blocks;398int outl, buflen;399400if (adin != NULL && adinlen != 0) {401inc_128(ctr);402403if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))404return 0;405/* This means we reuse derived value */406if (ctr->use_df) {407adin = NULL;408adinlen = 1;409}410} else {411adinlen = 0;412}413414inc_128(ctr);415416if (outlen == 0) {417inc_128(ctr);418419if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))420return 0;421return 1;422}423424memset(out, 0, outlen);425426do {427if (!EVP_CipherInit_ex(ctr->ctx_ctr,428NULL, NULL, NULL, ctr->V, -1))429return 0;430431/*-432* outlen has type size_t while EVP_CipherUpdate takes an433* int argument and thus cannot be guaranteed to process more434* than 2^31-1 bytes at a time. We process such huge generate435* requests in 2^30 byte chunks, which is the greatest multiple436* of AES block size lower than or equal to 2^31-1.437*/438buflen = outlen > (1U << 30) ? (1U << 30) : outlen;439blocks = (buflen + 15) / 16;440441ctr32 = GETU32(ctr->V + 12) + blocks;442if (ctr32 < blocks) {443/* 32-bit counter overflow into V. */444if (ctr32 != 0) {445blocks -= ctr32;446buflen = blocks * 16;447ctr32 = 0;448}449ctr96_inc(ctr->V);450}451PUTU32(ctr->V + 12, ctr32);452453if (!EVP_CipherUpdate(ctr->ctx_ctr, out, &outl, out, buflen)454|| outl != buflen)455return 0;456457out += buflen;458outlen -= buflen;459} while (outlen);460461if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))462return 0;463return 1;464}465466static int drbg_ctr_generate_wrapper(void *vdrbg, unsigned char *out, size_t outlen,467unsigned int strength, int prediction_resistance,468const unsigned char *adin, size_t adin_len)469{470PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;471472return ossl_prov_drbg_generate(drbg, out, outlen, strength,473prediction_resistance, adin, adin_len);474}475476static int drbg_ctr_uninstantiate(PROV_DRBG *drbg)477{478PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;479480OPENSSL_cleanse(ctr->K, sizeof(ctr->K));481OPENSSL_cleanse(ctr->V, sizeof(ctr->V));482OPENSSL_cleanse(ctr->bltmp, sizeof(ctr->bltmp));483OPENSSL_cleanse(ctr->KX, sizeof(ctr->KX));484ctr->bltmp_pos = 0;485return ossl_prov_drbg_uninstantiate(drbg);486}487488static int drbg_ctr_uninstantiate_wrapper(void *vdrbg)489{490PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;491int ret;492493if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))494return 0;495496ret = drbg_ctr_uninstantiate(drbg);497498if (drbg->lock != NULL)499CRYPTO_THREAD_unlock(drbg->lock);500501return ret;502}503504static int drbg_ctr_verify_zeroization(void *vdrbg)505{506PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;507PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;508int ret = 0;509510if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))511return 0;512513PROV_DRBG_VERIFY_ZEROIZATION(ctr->K);514PROV_DRBG_VERIFY_ZEROIZATION(ctr->V);515PROV_DRBG_VERIFY_ZEROIZATION(ctr->bltmp);516PROV_DRBG_VERIFY_ZEROIZATION(ctr->KX);517if (ctr->bltmp_pos != 0)518goto err;519520ret = 1;521err:522if (drbg->lock != NULL)523CRYPTO_THREAD_unlock(drbg->lock);524return ret;525}526527static int drbg_ctr_init_lengths(PROV_DRBG *drbg)528{529PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;530int res = 1;531532/* Maximum number of bits per request = 2^19 = 2^16 bytes */533drbg->max_request = 1 << 16;534if (ctr->use_df) {535drbg->min_entropylen = 0;536drbg->max_entropylen = DRBG_MAX_LENGTH;537drbg->min_noncelen = 0;538drbg->max_noncelen = DRBG_MAX_LENGTH;539drbg->max_perslen = DRBG_MAX_LENGTH;540drbg->max_adinlen = DRBG_MAX_LENGTH;541542if (ctr->keylen > 0) {543drbg->min_entropylen = ctr->keylen;544drbg->min_noncelen = drbg->min_entropylen / 2;545}546} else {547const size_t len = ctr->keylen > 0 ? drbg->seedlen : DRBG_MAX_LENGTH;548549drbg->min_entropylen = len;550drbg->max_entropylen = len;551/* Nonce not used */552drbg->min_noncelen = 0;553drbg->max_noncelen = 0;554drbg->max_perslen = len;555drbg->max_adinlen = len;556}557return res;558}559560static int drbg_ctr_init(PROV_DRBG *drbg)561{562PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;563size_t keylen;564565if (ctr->cipher_ctr == NULL) {566ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);567return 0;568}569ctr->keylen = keylen = EVP_CIPHER_get_key_length(ctr->cipher_ctr);570if (ctr->ctx_ecb == NULL)571ctr->ctx_ecb = EVP_CIPHER_CTX_new();572if (ctr->ctx_ctr == NULL)573ctr->ctx_ctr = EVP_CIPHER_CTX_new();574if (ctr->ctx_ecb == NULL || ctr->ctx_ctr == NULL) {575ERR_raise(ERR_LIB_PROV, ERR_R_EVP_LIB);576goto err;577}578579if (!EVP_CipherInit_ex(ctr->ctx_ecb,580ctr->cipher_ecb, NULL, NULL, NULL, 1)581|| !EVP_CipherInit_ex(ctr->ctx_ctr,582ctr->cipher_ctr, NULL, NULL, NULL, 1)) {583ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_INITIALISE_CIPHERS);584goto err;585}586587drbg->strength = keylen * 8;588drbg->seedlen = keylen + 16;589590if (ctr->use_df) {591/* df initialisation */592static const unsigned char df_key[32] = {5930x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,5940x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,5950x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,5960x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f597};598599if (ctr->ctx_df == NULL)600ctr->ctx_df = EVP_CIPHER_CTX_new();601if (ctr->ctx_df == NULL) {602ERR_raise(ERR_LIB_PROV, ERR_R_EVP_LIB);603goto err;604}605/* Set key schedule for df_key */606if (!EVP_CipherInit_ex(ctr->ctx_df,607ctr->cipher_ecb, NULL, df_key, NULL, 1)) {608ERR_raise(ERR_LIB_PROV, PROV_R_DERIVATION_FUNCTION_INIT_FAILED);609goto err;610}611}612return drbg_ctr_init_lengths(drbg);613614err:615EVP_CIPHER_CTX_free(ctr->ctx_ecb);616EVP_CIPHER_CTX_free(ctr->ctx_ctr);617ctr->ctx_ecb = ctr->ctx_ctr = NULL;618return 0;619}620621static int drbg_ctr_new(PROV_DRBG *drbg)622{623PROV_DRBG_CTR *ctr;624625ctr = OPENSSL_secure_zalloc(sizeof(*ctr));626if (ctr == NULL)627return 0;628629ctr->use_df = 1;630drbg->data = ctr;631OSSL_FIPS_IND_INIT(drbg)632return drbg_ctr_init_lengths(drbg);633}634635static void *drbg_ctr_new_wrapper(void *provctx, void *parent,636const OSSL_DISPATCH *parent_dispatch)637{638return ossl_rand_drbg_new(provctx, parent, parent_dispatch,639&drbg_ctr_new, &drbg_ctr_free,640&drbg_ctr_instantiate, &drbg_ctr_uninstantiate,641&drbg_ctr_reseed, &drbg_ctr_generate);642}643644static void drbg_ctr_free(void *vdrbg)645{646PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;647PROV_DRBG_CTR *ctr;648649if (drbg != NULL && (ctr = (PROV_DRBG_CTR *)drbg->data) != NULL) {650EVP_CIPHER_CTX_free(ctr->ctx_ecb);651EVP_CIPHER_CTX_free(ctr->ctx_ctr);652EVP_CIPHER_CTX_free(ctr->ctx_df);653EVP_CIPHER_free(ctr->cipher_ecb);654EVP_CIPHER_free(ctr->cipher_ctr);655656OPENSSL_secure_clear_free(ctr, sizeof(*ctr));657}658ossl_rand_drbg_free(drbg);659}660661static int drbg_ctr_get_ctx_params(void *vdrbg, OSSL_PARAM params[])662{663PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;664PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;665OSSL_PARAM *p;666int ret = 0, complete = 0;667668if (!ossl_drbg_get_ctx_params_no_lock(drbg, params, &complete))669return 0;670671if (complete)672return 1;673674if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))675return 0;676677p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_USE_DF);678if (p != NULL && !OSSL_PARAM_set_int(p, ctr->use_df))679goto err;680681p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_CIPHER);682if (p != NULL) {683if (ctr->cipher_ctr == NULL684|| !OSSL_PARAM_set_utf8_string(p,685EVP_CIPHER_get0_name(ctr->cipher_ctr)))686goto err;687}688689ret = ossl_drbg_get_ctx_params(drbg, params);690err:691if (drbg->lock != NULL)692CRYPTO_THREAD_unlock(drbg->lock);693694return ret;695}696697static const OSSL_PARAM *drbg_ctr_gettable_ctx_params(ossl_unused void *vctx,698ossl_unused void *provctx)699{700static const OSSL_PARAM known_gettable_ctx_params[] = {701OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_CIPHER, NULL, 0),702OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),703OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,704OSSL_FIPS_IND_GETTABLE_CTX_PARAM()705OSSL_PARAM_END706};707return known_gettable_ctx_params;708}709710static int drbg_ctr_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[])711{712PROV_DRBG *ctx = (PROV_DRBG *)vctx;713PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)ctx->data;714OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);715OSSL_PROVIDER *prov = NULL;716const OSSL_PARAM *p;717char *ecb;718const char *propquery = NULL;719int i, cipher_init = 0;720721if ((p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_USE_DF)) != NULL722&& OSSL_PARAM_get_int(p, &i)) {723/* FIPS errors out in the drbg_ctr_init() call later */724ctr->use_df = i != 0;725cipher_init = 1;726}727728if ((p = OSSL_PARAM_locate_const(params,729OSSL_DRBG_PARAM_PROPERTIES))730!= NULL) {731if (p->data_type != OSSL_PARAM_UTF8_STRING)732return 0;733propquery = (const char *)p->data;734}735736if ((p = OSSL_PARAM_locate_const(params,737OSSL_PROV_PARAM_CORE_PROV_NAME))738!= NULL) {739if (p->data_type != OSSL_PARAM_UTF8_STRING)740return 0;741if ((prov = ossl_provider_find(libctx,742(const char *)p->data, 1))743== NULL)744return 0;745}746747if ((p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_CIPHER)) != NULL) {748const char *base = (const char *)p->data;749size_t ctr_str_len = sizeof("CTR") - 1;750size_t ecb_str_len = sizeof("ECB") - 1;751752if (p->data_type != OSSL_PARAM_UTF8_STRING753|| p->data_size < ctr_str_len) {754ossl_provider_free(prov);755return 0;756}757if (OPENSSL_strcasecmp("CTR", base + p->data_size - ctr_str_len) != 0) {758ERR_raise(ERR_LIB_PROV, PROV_R_REQUIRE_CTR_MODE_CIPHER);759ossl_provider_free(prov);760return 0;761}762if ((ecb = OPENSSL_strndup(base, p->data_size)) == NULL) {763ossl_provider_free(prov);764return 0;765}766strcpy(ecb + p->data_size - ecb_str_len, "ECB");767EVP_CIPHER_free(ctr->cipher_ecb);768EVP_CIPHER_free(ctr->cipher_ctr);769/*770* Try to fetch algorithms from our own provider code, fallback771* to generic fetch only if that fails772*/773(void)ERR_set_mark();774ctr->cipher_ctr = evp_cipher_fetch_from_prov(prov, base, NULL);775if (ctr->cipher_ctr == NULL) {776(void)ERR_pop_to_mark();777ctr->cipher_ctr = EVP_CIPHER_fetch(libctx, base, propquery);778} else {779(void)ERR_clear_last_mark();780}781(void)ERR_set_mark();782ctr->cipher_ecb = evp_cipher_fetch_from_prov(prov, ecb, NULL);783if (ctr->cipher_ecb == NULL) {784(void)ERR_pop_to_mark();785ctr->cipher_ecb = EVP_CIPHER_fetch(libctx, ecb, propquery);786} else {787(void)ERR_clear_last_mark();788}789OPENSSL_free(ecb);790if (ctr->cipher_ctr == NULL || ctr->cipher_ecb == NULL) {791ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_FIND_CIPHERS);792ossl_provider_free(prov);793return 0;794}795cipher_init = 1;796}797ossl_provider_free(prov);798799if (cipher_init && !drbg_ctr_init(ctx))800return 0;801802return ossl_drbg_set_ctx_params(ctx, params);803}804805static int drbg_ctr_set_ctx_params(void *vctx, const OSSL_PARAM params[])806{807PROV_DRBG *drbg = (PROV_DRBG *)vctx;808int ret;809810if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))811return 0;812813ret = drbg_ctr_set_ctx_params_locked(vctx, params);814815if (drbg->lock != NULL)816CRYPTO_THREAD_unlock(drbg->lock);817818return ret;819}820821static const OSSL_PARAM *drbg_ctr_settable_ctx_params(ossl_unused void *vctx,822ossl_unused void *provctx)823{824static const OSSL_PARAM known_settable_ctx_params[] = {825OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),826OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_CIPHER, NULL, 0),827OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),828OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,829OSSL_PARAM_END830};831return known_settable_ctx_params;832}833834const OSSL_DISPATCH ossl_drbg_ctr_functions[] = {835{ OSSL_FUNC_RAND_NEWCTX, (void (*)(void))drbg_ctr_new_wrapper },836{ OSSL_FUNC_RAND_FREECTX, (void (*)(void))drbg_ctr_free },837{ OSSL_FUNC_RAND_INSTANTIATE,838(void (*)(void))drbg_ctr_instantiate_wrapper },839{ OSSL_FUNC_RAND_UNINSTANTIATE,840(void (*)(void))drbg_ctr_uninstantiate_wrapper },841{ OSSL_FUNC_RAND_GENERATE, (void (*)(void))drbg_ctr_generate_wrapper },842{ OSSL_FUNC_RAND_RESEED, (void (*)(void))drbg_ctr_reseed_wrapper },843{ OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))ossl_drbg_enable_locking },844{ OSSL_FUNC_RAND_LOCK, (void (*)(void))ossl_drbg_lock },845{ OSSL_FUNC_RAND_UNLOCK, (void (*)(void))ossl_drbg_unlock },846{ OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,847(void (*)(void))drbg_ctr_settable_ctx_params },848{ OSSL_FUNC_RAND_SET_CTX_PARAMS, (void (*)(void))drbg_ctr_set_ctx_params },849{ OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,850(void (*)(void))drbg_ctr_gettable_ctx_params },851{ OSSL_FUNC_RAND_GET_CTX_PARAMS, (void (*)(void))drbg_ctr_get_ctx_params },852{ OSSL_FUNC_RAND_VERIFY_ZEROIZATION,853(void (*)(void))drbg_ctr_verify_zeroization },854{ OSSL_FUNC_RAND_GET_SEED, (void (*)(void))ossl_drbg_get_seed },855{ OSSL_FUNC_RAND_CLEAR_SEED, (void (*)(void))ossl_drbg_clear_seed },856OSSL_DISPATCH_END857};858859860