drbg_ctr.c 25 KB

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