drbg_ctr.c 25 KB

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