drbg_ctr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * Copyright 2011-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #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"
  26. static OSSL_FUNC_rand_newctx_fn drbg_ctr_new_wrapper;
  27. static OSSL_FUNC_rand_freectx_fn drbg_ctr_free;
  28. static OSSL_FUNC_rand_instantiate_fn drbg_ctr_instantiate_wrapper;
  29. static OSSL_FUNC_rand_uninstantiate_fn drbg_ctr_uninstantiate_wrapper;
  30. static OSSL_FUNC_rand_generate_fn drbg_ctr_generate_wrapper;
  31. static OSSL_FUNC_rand_reseed_fn drbg_ctr_reseed_wrapper;
  32. static OSSL_FUNC_rand_settable_ctx_params_fn drbg_ctr_settable_ctx_params;
  33. static OSSL_FUNC_rand_set_ctx_params_fn drbg_ctr_set_ctx_params;
  34. static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_ctr_gettable_ctx_params;
  35. static OSSL_FUNC_rand_get_ctx_params_fn drbg_ctr_get_ctx_params;
  36. static OSSL_FUNC_rand_verify_zeroization_fn drbg_ctr_verify_zeroization;
  37. static int drbg_ctr_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[]);
  38. /*
  39. * The state of a DRBG AES-CTR.
  40. */
  41. typedef struct rand_drbg_ctr_st {
  42. EVP_CIPHER_CTX *ctx_ecb;
  43. EVP_CIPHER_CTX *ctx_ctr;
  44. EVP_CIPHER_CTX *ctx_df;
  45. EVP_CIPHER *cipher_ecb;
  46. EVP_CIPHER *cipher_ctr;
  47. size_t keylen;
  48. int use_df;
  49. unsigned char K[32];
  50. unsigned char V[16];
  51. /* Temporary block storage used by ctr_df */
  52. unsigned char bltmp[16];
  53. size_t bltmp_pos;
  54. unsigned char KX[48];
  55. } PROV_DRBG_CTR;
  56. /*
  57. * Implementation of NIST SP 800-90A CTR DRBG.
  58. */
  59. static void inc_128(PROV_DRBG_CTR *ctr)
  60. {
  61. unsigned char *p = &ctr->V[0];
  62. u32 n = 16, c = 1;
  63. do {
  64. --n;
  65. c += p[n];
  66. p[n] = (u8)c;
  67. c >>= 8;
  68. } while (n);
  69. }
  70. static void ctr_XOR(PROV_DRBG_CTR *ctr, const unsigned char *in, size_t inlen)
  71. {
  72. size_t i, n;
  73. if (in == NULL || inlen == 0)
  74. return;
  75. /*
  76. * Any zero padding will have no effect on the result as we
  77. * are XORing. So just process however much input we have.
  78. */
  79. n = inlen < ctr->keylen ? inlen : ctr->keylen;
  80. if (!ossl_assert(n <= sizeof(ctr->K)))
  81. return;
  82. for (i = 0; i < n; i++)
  83. ctr->K[i] ^= in[i];
  84. if (inlen <= ctr->keylen)
  85. return;
  86. n = inlen - ctr->keylen;
  87. if (n > 16) {
  88. /* Should never happen */
  89. n = 16;
  90. }
  91. for (i = 0; i < n; i++)
  92. ctr->V[i] ^= in[i + ctr->keylen];
  93. }
  94. /*
  95. * Process a complete block using BCC algorithm of SP 800-90A 10.3.3
  96. */
  97. __owur static int ctr_BCC_block(PROV_DRBG_CTR *ctr, unsigned char *out,
  98. const unsigned char *in, int len)
  99. {
  100. int i, outlen = AES_BLOCK_SIZE;
  101. for (i = 0; i < len; i++)
  102. out[i] ^= in[i];
  103. if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, len)
  104. || outlen != len)
  105. return 0;
  106. return 1;
  107. }
  108. /*
  109. * Handle several BCC operations for as much data as we need for K and X
  110. */
  111. __owur static int ctr_BCC_blocks(PROV_DRBG_CTR *ctr, const unsigned char *in)
  112. {
  113. unsigned char in_tmp[48];
  114. unsigned char num_of_blk = 2;
  115. memcpy(in_tmp, in, 16);
  116. memcpy(in_tmp + 16, in, 16);
  117. if (ctr->keylen != 16) {
  118. memcpy(in_tmp + 32, in, 16);
  119. num_of_blk = 3;
  120. }
  121. return ctr_BCC_block(ctr, ctr->KX, in_tmp, AES_BLOCK_SIZE * num_of_blk);
  122. }
  123. /*
  124. * Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:
  125. * see 10.3.1 stage 7.
  126. */
  127. __owur static int ctr_BCC_init(PROV_DRBG_CTR *ctr)
  128. {
  129. unsigned char bltmp[48] = {0};
  130. unsigned char num_of_blk;
  131. memset(ctr->KX, 0, 48);
  132. num_of_blk = ctr->keylen == 16 ? 2 : 3;
  133. bltmp[(AES_BLOCK_SIZE * 1) + 3] = 1;
  134. bltmp[(AES_BLOCK_SIZE * 2) + 3] = 2;
  135. return ctr_BCC_block(ctr, ctr->KX, bltmp, num_of_blk * AES_BLOCK_SIZE);
  136. }
  137. /*
  138. * Process several blocks into BCC algorithm, some possibly partial
  139. */
  140. __owur static int ctr_BCC_update(PROV_DRBG_CTR *ctr,
  141. const unsigned char *in, size_t inlen)
  142. {
  143. if (in == NULL || inlen == 0)
  144. return 1;
  145. /* If we have partial block handle it first */
  146. if (ctr->bltmp_pos) {
  147. size_t left = 16 - ctr->bltmp_pos;
  148. /* If we now have a complete block process it */
  149. if (inlen >= left) {
  150. memcpy(ctr->bltmp + ctr->bltmp_pos, in, left);
  151. if (!ctr_BCC_blocks(ctr, ctr->bltmp))
  152. return 0;
  153. ctr->bltmp_pos = 0;
  154. inlen -= left;
  155. in += left;
  156. }
  157. }
  158. /* Process zero or more complete blocks */
  159. for (; inlen >= 16; in += 16, inlen -= 16) {
  160. if (!ctr_BCC_blocks(ctr, in))
  161. return 0;
  162. }
  163. /* Copy any remaining partial block to the temporary buffer */
  164. if (inlen > 0) {
  165. memcpy(ctr->bltmp + ctr->bltmp_pos, in, inlen);
  166. ctr->bltmp_pos += inlen;
  167. }
  168. return 1;
  169. }
  170. __owur static int ctr_BCC_final(PROV_DRBG_CTR *ctr)
  171. {
  172. if (ctr->bltmp_pos) {
  173. memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos);
  174. if (!ctr_BCC_blocks(ctr, ctr->bltmp))
  175. return 0;
  176. }
  177. return 1;
  178. }
  179. __owur static int ctr_df(PROV_DRBG_CTR *ctr,
  180. const unsigned char *in1, size_t in1len,
  181. const unsigned char *in2, size_t in2len,
  182. const unsigned char *in3, size_t in3len)
  183. {
  184. static unsigned char c80 = 0x80;
  185. size_t inlen;
  186. unsigned char *p = ctr->bltmp;
  187. int outlen = AES_BLOCK_SIZE;
  188. if (!ctr_BCC_init(ctr))
  189. return 0;
  190. if (in1 == NULL)
  191. in1len = 0;
  192. if (in2 == NULL)
  193. in2len = 0;
  194. if (in3 == NULL)
  195. in3len = 0;
  196. inlen = in1len + in2len + in3len;
  197. /* Initialise L||N in temporary block */
  198. *p++ = (inlen >> 24) & 0xff;
  199. *p++ = (inlen >> 16) & 0xff;
  200. *p++ = (inlen >> 8) & 0xff;
  201. *p++ = inlen & 0xff;
  202. /* NB keylen is at most 32 bytes */
  203. *p++ = 0;
  204. *p++ = 0;
  205. *p++ = 0;
  206. *p = (unsigned char)((ctr->keylen + 16) & 0xff);
  207. ctr->bltmp_pos = 8;
  208. if (!ctr_BCC_update(ctr, in1, in1len)
  209. || !ctr_BCC_update(ctr, in2, in2len)
  210. || !ctr_BCC_update(ctr, in3, in3len)
  211. || !ctr_BCC_update(ctr, &c80, 1)
  212. || !ctr_BCC_final(ctr))
  213. return 0;
  214. /* Set up key K */
  215. if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->KX, NULL, -1))
  216. return 0;
  217. /* X follows key K */
  218. if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX, &outlen, ctr->KX + ctr->keylen,
  219. AES_BLOCK_SIZE)
  220. || outlen != AES_BLOCK_SIZE)
  221. return 0;
  222. if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 16, &outlen, ctr->KX,
  223. AES_BLOCK_SIZE)
  224. || outlen != AES_BLOCK_SIZE)
  225. return 0;
  226. if (ctr->keylen != 16)
  227. if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 32, &outlen,
  228. ctr->KX + 16, AES_BLOCK_SIZE)
  229. || outlen != AES_BLOCK_SIZE)
  230. return 0;
  231. return 1;
  232. }
  233. /*
  234. * NB the no-df Update in SP800-90A specifies a constant input length
  235. * of seedlen, however other uses of this algorithm pad the input with
  236. * zeroes if necessary and have up to two parameters XORed together,
  237. * so we handle both cases in this function instead.
  238. */
  239. __owur static int ctr_update(PROV_DRBG *drbg,
  240. const unsigned char *in1, size_t in1len,
  241. const unsigned char *in2, size_t in2len,
  242. const unsigned char *nonce, size_t noncelen)
  243. {
  244. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  245. int outlen = AES_BLOCK_SIZE;
  246. unsigned char V_tmp[48], out[48];
  247. unsigned char len;
  248. /* correct key is already set up. */
  249. memcpy(V_tmp, ctr->V, 16);
  250. inc_128(ctr);
  251. memcpy(V_tmp + 16, ctr->V, 16);
  252. if (ctr->keylen == 16) {
  253. len = 32;
  254. } else {
  255. inc_128(ctr);
  256. memcpy(V_tmp + 32, ctr->V, 16);
  257. len = 48;
  258. }
  259. if (!EVP_CipherUpdate(ctr->ctx_ecb, out, &outlen, V_tmp, len)
  260. || outlen != len)
  261. return 0;
  262. memcpy(ctr->K, out, ctr->keylen);
  263. memcpy(ctr->V, out + ctr->keylen, 16);
  264. if (ctr->use_df) {
  265. /* If no input reuse existing derived value */
  266. if (in1 != NULL || nonce != NULL || in2 != NULL)
  267. if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))
  268. return 0;
  269. /* If this a reuse input in1len != 0 */
  270. if (in1len)
  271. ctr_XOR(ctr, ctr->KX, drbg->seedlen);
  272. } else {
  273. ctr_XOR(ctr, in1, in1len);
  274. ctr_XOR(ctr, in2, in2len);
  275. }
  276. if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1)
  277. || !EVP_CipherInit_ex(ctr->ctx_ctr, NULL, NULL, ctr->K, NULL, -1))
  278. return 0;
  279. return 1;
  280. }
  281. static int drbg_ctr_instantiate(PROV_DRBG *drbg,
  282. const unsigned char *entropy, size_t entropylen,
  283. const unsigned char *nonce, size_t noncelen,
  284. const unsigned char *pers, size_t perslen)
  285. {
  286. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  287. if (entropy == NULL)
  288. return 0;
  289. memset(ctr->K, 0, sizeof(ctr->K));
  290. memset(ctr->V, 0, sizeof(ctr->V));
  291. if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1))
  292. return 0;
  293. inc_128(ctr);
  294. if (!ctr_update(drbg, entropy, entropylen, pers, perslen, nonce, noncelen))
  295. return 0;
  296. return 1;
  297. }
  298. static int drbg_ctr_instantiate_wrapper(void *vdrbg, unsigned int strength,
  299. int prediction_resistance,
  300. const unsigned char *pstr,
  301. size_t pstr_len,
  302. const OSSL_PARAM params[])
  303. {
  304. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  305. int ret = 0;
  306. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  307. return 0;
  308. if (!ossl_prov_is_running()
  309. || !drbg_ctr_set_ctx_params_locked(drbg, params))
  310. goto err;
  311. ret = ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
  312. pstr, pstr_len);
  313. err:
  314. if (drbg->lock != NULL)
  315. CRYPTO_THREAD_unlock(drbg->lock);
  316. return ret;
  317. }
  318. static int drbg_ctr_reseed(PROV_DRBG *drbg,
  319. const unsigned char *entropy, size_t entropylen,
  320. const unsigned char *adin, size_t adinlen)
  321. {
  322. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  323. if (entropy == NULL)
  324. return 0;
  325. inc_128(ctr);
  326. if (!ctr_update(drbg, entropy, entropylen, adin, adinlen, NULL, 0))
  327. return 0;
  328. return 1;
  329. }
  330. static int drbg_ctr_reseed_wrapper(void *vdrbg, int prediction_resistance,
  331. const unsigned char *ent, size_t ent_len,
  332. const unsigned char *adin, size_t adin_len)
  333. {
  334. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  335. return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
  336. adin, adin_len);
  337. }
  338. static void ctr96_inc(unsigned char *counter)
  339. {
  340. u32 n = 12, c = 1;
  341. do {
  342. --n;
  343. c += counter[n];
  344. counter[n] = (u8)c;
  345. c >>= 8;
  346. } while (n);
  347. }
  348. static int drbg_ctr_generate(PROV_DRBG *drbg,
  349. unsigned char *out, size_t outlen,
  350. const unsigned char *adin, size_t adinlen)
  351. {
  352. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  353. unsigned int ctr32, blocks;
  354. int outl, buflen;
  355. if (adin != NULL && adinlen != 0) {
  356. inc_128(ctr);
  357. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  358. return 0;
  359. /* This means we reuse derived value */
  360. if (ctr->use_df) {
  361. adin = NULL;
  362. adinlen = 1;
  363. }
  364. } else {
  365. adinlen = 0;
  366. }
  367. inc_128(ctr);
  368. if (outlen == 0) {
  369. inc_128(ctr);
  370. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  371. return 0;
  372. return 1;
  373. }
  374. memset(out, 0, outlen);
  375. do {
  376. if (!EVP_CipherInit_ex(ctr->ctx_ctr,
  377. NULL, NULL, NULL, ctr->V, -1))
  378. return 0;
  379. /*-
  380. * outlen has type size_t while EVP_CipherUpdate takes an
  381. * int argument and thus cannot be guaranteed to process more
  382. * than 2^31-1 bytes at a time. We process such huge generate
  383. * requests in 2^30 byte chunks, which is the greatest multiple
  384. * of AES block size lower than or equal to 2^31-1.
  385. */
  386. buflen = outlen > (1U << 30) ? (1U << 30) : outlen;
  387. blocks = (buflen + 15) / 16;
  388. ctr32 = GETU32(ctr->V + 12) + blocks;
  389. if (ctr32 < blocks) {
  390. /* 32-bit counter overflow into V. */
  391. if (ctr32 != 0) {
  392. blocks -= ctr32;
  393. buflen = blocks * 16;
  394. ctr32 = 0;
  395. }
  396. ctr96_inc(ctr->V);
  397. }
  398. PUTU32(ctr->V + 12, ctr32);
  399. if (!EVP_CipherUpdate(ctr->ctx_ctr, out, &outl, out, buflen)
  400. || outl != buflen)
  401. return 0;
  402. out += buflen;
  403. outlen -= buflen;
  404. } while (outlen);
  405. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  406. return 0;
  407. return 1;
  408. }
  409. static int drbg_ctr_generate_wrapper
  410. (void *vdrbg, unsigned char *out, size_t outlen,
  411. unsigned int strength, int prediction_resistance,
  412. const unsigned char *adin, size_t adin_len)
  413. {
  414. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  415. return ossl_prov_drbg_generate(drbg, out, outlen, strength,
  416. prediction_resistance, adin, adin_len);
  417. }
  418. static int drbg_ctr_uninstantiate(PROV_DRBG *drbg)
  419. {
  420. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  421. OPENSSL_cleanse(ctr->K, sizeof(ctr->K));
  422. OPENSSL_cleanse(ctr->V, sizeof(ctr->V));
  423. OPENSSL_cleanse(ctr->bltmp, sizeof(ctr->bltmp));
  424. OPENSSL_cleanse(ctr->KX, sizeof(ctr->KX));
  425. ctr->bltmp_pos = 0;
  426. return ossl_prov_drbg_uninstantiate(drbg);
  427. }
  428. static int drbg_ctr_uninstantiate_wrapper(void *vdrbg)
  429. {
  430. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  431. int ret;
  432. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  433. return 0;
  434. ret = drbg_ctr_uninstantiate(drbg);
  435. if (drbg->lock != NULL)
  436. CRYPTO_THREAD_unlock(drbg->lock);
  437. return ret;
  438. }
  439. static int drbg_ctr_verify_zeroization(void *vdrbg)
  440. {
  441. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  442. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  443. int ret = 0;
  444. if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
  445. return 0;
  446. PROV_DRBG_VERIFY_ZEROIZATION(ctr->K);
  447. PROV_DRBG_VERIFY_ZEROIZATION(ctr->V);
  448. PROV_DRBG_VERIFY_ZEROIZATION(ctr->bltmp);
  449. PROV_DRBG_VERIFY_ZEROIZATION(ctr->KX);
  450. if (ctr->bltmp_pos != 0)
  451. goto err;
  452. ret = 1;
  453. err:
  454. if (drbg->lock != NULL)
  455. CRYPTO_THREAD_unlock(drbg->lock);
  456. return ret;
  457. }
  458. static int drbg_ctr_init_lengths(PROV_DRBG *drbg)
  459. {
  460. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  461. int res = 1;
  462. /* Maximum number of bits per request = 2^19 = 2^16 bytes */
  463. drbg->max_request = 1 << 16;
  464. if (ctr->use_df) {
  465. drbg->min_entropylen = 0;
  466. drbg->max_entropylen = DRBG_MAX_LENGTH;
  467. drbg->min_noncelen = 0;
  468. drbg->max_noncelen = DRBG_MAX_LENGTH;
  469. drbg->max_perslen = DRBG_MAX_LENGTH;
  470. drbg->max_adinlen = DRBG_MAX_LENGTH;
  471. if (ctr->keylen > 0) {
  472. drbg->min_entropylen = ctr->keylen;
  473. drbg->min_noncelen = drbg->min_entropylen / 2;
  474. }
  475. } else {
  476. const size_t len = ctr->keylen > 0 ? drbg->seedlen : DRBG_MAX_LENGTH;
  477. drbg->min_entropylen = len;
  478. drbg->max_entropylen = len;
  479. /* Nonce not used */
  480. drbg->min_noncelen = 0;
  481. drbg->max_noncelen = 0;
  482. drbg->max_perslen = len;
  483. drbg->max_adinlen = len;
  484. }
  485. return res;
  486. }
  487. static int drbg_ctr_init(PROV_DRBG *drbg)
  488. {
  489. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  490. size_t keylen;
  491. if (ctr->cipher_ctr == NULL) {
  492. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
  493. return 0;
  494. }
  495. ctr->keylen = keylen = EVP_CIPHER_get_key_length(ctr->cipher_ctr);
  496. if (ctr->ctx_ecb == NULL)
  497. ctr->ctx_ecb = EVP_CIPHER_CTX_new();
  498. if (ctr->ctx_ctr == NULL)
  499. ctr->ctx_ctr = EVP_CIPHER_CTX_new();
  500. if (ctr->ctx_ecb == NULL || ctr->ctx_ctr == NULL) {
  501. ERR_raise(ERR_LIB_PROV, ERR_R_EVP_LIB);
  502. goto err;
  503. }
  504. if (!EVP_CipherInit_ex(ctr->ctx_ecb,
  505. ctr->cipher_ecb, NULL, NULL, NULL, 1)
  506. || !EVP_CipherInit_ex(ctr->ctx_ctr,
  507. ctr->cipher_ctr, NULL, NULL, NULL, 1)) {
  508. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_INITIALISE_CIPHERS);
  509. goto err;
  510. }
  511. drbg->strength = keylen * 8;
  512. drbg->seedlen = keylen + 16;
  513. if (ctr->use_df) {
  514. /* df initialisation */
  515. static const unsigned char df_key[32] = {
  516. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  517. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  518. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  519. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  520. };
  521. if (ctr->ctx_df == NULL)
  522. ctr->ctx_df = EVP_CIPHER_CTX_new();
  523. if (ctr->ctx_df == NULL) {
  524. ERR_raise(ERR_LIB_PROV, ERR_R_EVP_LIB);
  525. goto err;
  526. }
  527. /* Set key schedule for df_key */
  528. if (!EVP_CipherInit_ex(ctr->ctx_df,
  529. ctr->cipher_ecb, NULL, df_key, NULL, 1)) {
  530. ERR_raise(ERR_LIB_PROV, PROV_R_DERIVATION_FUNCTION_INIT_FAILED);
  531. goto err;
  532. }
  533. }
  534. return drbg_ctr_init_lengths(drbg);
  535. err:
  536. EVP_CIPHER_CTX_free(ctr->ctx_ecb);
  537. EVP_CIPHER_CTX_free(ctr->ctx_ctr);
  538. ctr->ctx_ecb = ctr->ctx_ctr = NULL;
  539. return 0;
  540. }
  541. static int drbg_ctr_new(PROV_DRBG *drbg)
  542. {
  543. PROV_DRBG_CTR *ctr;
  544. ctr = OPENSSL_secure_zalloc(sizeof(*ctr));
  545. if (ctr == NULL)
  546. return 0;
  547. ctr->use_df = 1;
  548. drbg->data = ctr;
  549. OSSL_FIPS_IND_INIT(drbg)
  550. return drbg_ctr_init_lengths(drbg);
  551. }
  552. static void *drbg_ctr_new_wrapper(void *provctx, void *parent,
  553. const OSSL_DISPATCH *parent_dispatch)
  554. {
  555. return ossl_rand_drbg_new(provctx, parent, parent_dispatch,
  556. &drbg_ctr_new, &drbg_ctr_free,
  557. &drbg_ctr_instantiate, &drbg_ctr_uninstantiate,
  558. &drbg_ctr_reseed, &drbg_ctr_generate);
  559. }
  560. static void drbg_ctr_free(void *vdrbg)
  561. {
  562. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  563. PROV_DRBG_CTR *ctr;
  564. if (drbg != NULL && (ctr = (PROV_DRBG_CTR *)drbg->data) != NULL) {
  565. EVP_CIPHER_CTX_free(ctr->ctx_ecb);
  566. EVP_CIPHER_CTX_free(ctr->ctx_ctr);
  567. EVP_CIPHER_CTX_free(ctr->ctx_df);
  568. EVP_CIPHER_free(ctr->cipher_ecb);
  569. EVP_CIPHER_free(ctr->cipher_ctr);
  570. OPENSSL_secure_clear_free(ctr, sizeof(*ctr));
  571. }
  572. ossl_rand_drbg_free(drbg);
  573. }
  574. static int drbg_ctr_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
  575. {
  576. PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
  577. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
  578. OSSL_PARAM *p;
  579. int ret = 0, complete = 0;
  580. if (!ossl_drbg_get_ctx_params_no_lock(drbg, params, &complete))
  581. return 0;
  582. if (complete)
  583. return 1;
  584. if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))
  585. return 0;
  586. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_USE_DF);
  587. if (p != NULL && !OSSL_PARAM_set_int(p, ctr->use_df))
  588. goto err;
  589. p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_CIPHER);
  590. if (p != NULL) {
  591. if (ctr->cipher_ctr == NULL
  592. || !OSSL_PARAM_set_utf8_string(p,
  593. EVP_CIPHER_get0_name(ctr->cipher_ctr)))
  594. goto err;
  595. }
  596. ret = ossl_drbg_get_ctx_params(drbg, params);
  597. err:
  598. if (drbg->lock != NULL)
  599. CRYPTO_THREAD_unlock(drbg->lock);
  600. return ret;
  601. }
  602. static const OSSL_PARAM *drbg_ctr_gettable_ctx_params(ossl_unused void *vctx,
  603. ossl_unused void *provctx)
  604. {
  605. static const OSSL_PARAM known_gettable_ctx_params[] = {
  606. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_CIPHER, NULL, 0),
  607. OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),
  608. OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
  609. OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
  610. OSSL_PARAM_END
  611. };
  612. return known_gettable_ctx_params;
  613. }
  614. static int drbg_ctr_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[])
  615. {
  616. PROV_DRBG *ctx = (PROV_DRBG *)vctx;
  617. PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)ctx->data;
  618. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  619. OSSL_PROVIDER *prov = NULL;
  620. const OSSL_PARAM *p;
  621. char *ecb;
  622. const char *propquery = NULL;
  623. int i, cipher_init = 0;
  624. if ((p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_USE_DF)) != NULL
  625. && OSSL_PARAM_get_int(p, &i)) {
  626. /* FIPS errors out in the drbg_ctr_init() call later */
  627. ctr->use_df = i != 0;
  628. cipher_init = 1;
  629. }
  630. if ((p = OSSL_PARAM_locate_const(params,
  631. OSSL_DRBG_PARAM_PROPERTIES)) != NULL) {
  632. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  633. return 0;
  634. propquery = (const char *)p->data;
  635. }
  636. if ((p = OSSL_PARAM_locate_const(params,
  637. OSSL_PROV_PARAM_CORE_PROV_NAME)) != NULL) {
  638. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  639. return 0;
  640. if ((prov = ossl_provider_find(libctx,
  641. (const char *)p->data, 1)) == NULL)
  642. return 0;
  643. }
  644. if ((p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_CIPHER)) != NULL) {
  645. const char *base = (const char *)p->data;
  646. size_t ctr_str_len = sizeof("CTR") - 1;
  647. size_t ecb_str_len = sizeof("ECB") - 1;
  648. if (p->data_type != OSSL_PARAM_UTF8_STRING
  649. || p->data_size < ctr_str_len) {
  650. ossl_provider_free(prov);
  651. return 0;
  652. }
  653. if (OPENSSL_strcasecmp("CTR", base + p->data_size - ctr_str_len) != 0) {
  654. ERR_raise(ERR_LIB_PROV, PROV_R_REQUIRE_CTR_MODE_CIPHER);
  655. ossl_provider_free(prov);
  656. return 0;
  657. }
  658. if ((ecb = OPENSSL_strndup(base, p->data_size)) == NULL) {
  659. ossl_provider_free(prov);
  660. return 0;
  661. }
  662. strcpy(ecb + p->data_size - ecb_str_len, "ECB");
  663. EVP_CIPHER_free(ctr->cipher_ecb);
  664. EVP_CIPHER_free(ctr->cipher_ctr);
  665. /*
  666. * Try to fetch algorithms from our own provider code, fallback
  667. * to generic fetch only if that fails
  668. */
  669. (void)ERR_set_mark();
  670. ctr->cipher_ctr = evp_cipher_fetch_from_prov(prov, base, NULL);
  671. if (ctr->cipher_ctr == NULL) {
  672. (void)ERR_pop_to_mark();
  673. ctr->cipher_ctr = EVP_CIPHER_fetch(libctx, base, propquery);
  674. } else {
  675. (void)ERR_clear_last_mark();
  676. }
  677. (void)ERR_set_mark();
  678. ctr->cipher_ecb = evp_cipher_fetch_from_prov(prov, ecb, NULL);
  679. if (ctr->cipher_ecb == NULL) {
  680. (void)ERR_pop_to_mark();
  681. ctr->cipher_ecb = EVP_CIPHER_fetch(libctx, ecb, propquery);
  682. } else {
  683. (void)ERR_clear_last_mark();
  684. }
  685. OPENSSL_free(ecb);
  686. if (ctr->cipher_ctr == NULL || ctr->cipher_ecb == NULL) {
  687. ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_FIND_CIPHERS);
  688. ossl_provider_free(prov);
  689. return 0;
  690. }
  691. cipher_init = 1;
  692. }
  693. ossl_provider_free(prov);
  694. if (cipher_init && !drbg_ctr_init(ctx))
  695. return 0;
  696. return ossl_drbg_set_ctx_params(ctx, params);
  697. }
  698. static int drbg_ctr_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  699. {
  700. PROV_DRBG *drbg = (PROV_DRBG *)vctx;
  701. int ret;
  702. if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))
  703. return 0;
  704. ret = drbg_ctr_set_ctx_params_locked(vctx, params);
  705. if (drbg->lock != NULL)
  706. CRYPTO_THREAD_unlock(drbg->lock);
  707. return ret;
  708. }
  709. static const OSSL_PARAM *drbg_ctr_settable_ctx_params(ossl_unused void *vctx,
  710. ossl_unused void *provctx)
  711. {
  712. static const OSSL_PARAM known_settable_ctx_params[] = {
  713. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
  714. OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_CIPHER, NULL, 0),
  715. OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),
  716. OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
  717. OSSL_PARAM_END
  718. };
  719. return known_settable_ctx_params;
  720. }
  721. const OSSL_DISPATCH ossl_drbg_ctr_functions[] = {
  722. { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_ctr_new_wrapper },
  723. { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_ctr_free },
  724. { OSSL_FUNC_RAND_INSTANTIATE,
  725. (void(*)(void))drbg_ctr_instantiate_wrapper },
  726. { OSSL_FUNC_RAND_UNINSTANTIATE,
  727. (void(*)(void))drbg_ctr_uninstantiate_wrapper },
  728. { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_ctr_generate_wrapper },
  729. { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_ctr_reseed_wrapper },
  730. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking },
  731. { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock },
  732. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock },
  733. { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
  734. (void(*)(void))drbg_ctr_settable_ctx_params },
  735. { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_ctr_set_ctx_params },
  736. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  737. (void(*)(void))drbg_ctr_gettable_ctx_params },
  738. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_ctr_get_ctx_params },
  739. { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
  740. (void(*)(void))drbg_ctr_verify_zeroization },
  741. { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed },
  742. { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed },
  743. OSSL_DISPATCH_END
  744. };