drbg_ctr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright 2011-2020 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 "crypto/modes.h"
  15. #include "internal/thread_once.h"
  16. #include "rand_local.h"
  17. /*
  18. * Implementation of NIST SP 800-90A CTR DRBG.
  19. */
  20. static void inc_128(RAND_DRBG_CTR *ctr)
  21. {
  22. unsigned char *p = &ctr->V[0];
  23. u32 n = 16, c = 1;
  24. do {
  25. --n;
  26. c += p[n];
  27. p[n] = (u8)c;
  28. c >>= 8;
  29. } while (n);
  30. }
  31. static void ctr_XOR(RAND_DRBG_CTR *ctr, const unsigned char *in, size_t inlen)
  32. {
  33. size_t i, n;
  34. if (in == NULL || inlen == 0)
  35. return;
  36. /*
  37. * Any zero padding will have no effect on the result as we
  38. * are XORing. So just process however much input we have.
  39. */
  40. n = inlen < ctr->keylen ? inlen : ctr->keylen;
  41. for (i = 0; i < n; i++)
  42. ctr->K[i] ^= in[i];
  43. if (inlen <= ctr->keylen)
  44. return;
  45. n = inlen - ctr->keylen;
  46. if (n > 16) {
  47. /* Should never happen */
  48. n = 16;
  49. }
  50. for (i = 0; i < n; i++)
  51. ctr->V[i] ^= in[i + ctr->keylen];
  52. }
  53. /*
  54. * Process a complete block using BCC algorithm of SP 800-90A 10.3.3
  55. */
  56. __owur static int ctr_BCC_block(RAND_DRBG_CTR *ctr, unsigned char *out,
  57. const unsigned char *in, int len)
  58. {
  59. int i, outlen = AES_BLOCK_SIZE;
  60. for (i = 0; i < len; i++)
  61. out[i] ^= in[i];
  62. if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, len)
  63. || outlen != len)
  64. return 0;
  65. return 1;
  66. }
  67. /*
  68. * Handle several BCC operations for as much data as we need for K and X
  69. */
  70. __owur static int ctr_BCC_blocks(RAND_DRBG_CTR *ctr, const unsigned char *in)
  71. {
  72. unsigned char in_tmp[48];
  73. unsigned char num_of_blk = 2;
  74. memcpy(in_tmp, in, 16);
  75. memcpy(in_tmp + 16, in, 16);
  76. if (ctr->keylen != 16) {
  77. memcpy(in_tmp + 32, in, 16);
  78. num_of_blk = 3;
  79. }
  80. return ctr_BCC_block(ctr, ctr->KX, in_tmp, AES_BLOCK_SIZE * num_of_blk);
  81. }
  82. /*
  83. * Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:
  84. * see 10.3.1 stage 7.
  85. */
  86. __owur static int ctr_BCC_init(RAND_DRBG_CTR *ctr)
  87. {
  88. unsigned char bltmp[48] = {0};
  89. unsigned char num_of_blk;
  90. memset(ctr->KX, 0, 48);
  91. num_of_blk = ctr->keylen == 16 ? 2 : 3;
  92. bltmp[(AES_BLOCK_SIZE * 1) + 3] = 1;
  93. bltmp[(AES_BLOCK_SIZE * 2) + 3] = 2;
  94. return ctr_BCC_block(ctr, ctr->KX, bltmp, num_of_blk * AES_BLOCK_SIZE);
  95. }
  96. /*
  97. * Process several blocks into BCC algorithm, some possibly partial
  98. */
  99. __owur static int ctr_BCC_update(RAND_DRBG_CTR *ctr,
  100. const unsigned char *in, size_t inlen)
  101. {
  102. if (in == NULL || inlen == 0)
  103. return 1;
  104. /* If we have partial block handle it first */
  105. if (ctr->bltmp_pos) {
  106. size_t left = 16 - ctr->bltmp_pos;
  107. /* If we now have a complete block process it */
  108. if (inlen >= left) {
  109. memcpy(ctr->bltmp + ctr->bltmp_pos, in, left);
  110. if (!ctr_BCC_blocks(ctr, ctr->bltmp))
  111. return 0;
  112. ctr->bltmp_pos = 0;
  113. inlen -= left;
  114. in += left;
  115. }
  116. }
  117. /* Process zero or more complete blocks */
  118. for (; inlen >= 16; in += 16, inlen -= 16) {
  119. if (!ctr_BCC_blocks(ctr, in))
  120. return 0;
  121. }
  122. /* Copy any remaining partial block to the temporary buffer */
  123. if (inlen > 0) {
  124. memcpy(ctr->bltmp + ctr->bltmp_pos, in, inlen);
  125. ctr->bltmp_pos += inlen;
  126. }
  127. return 1;
  128. }
  129. __owur static int ctr_BCC_final(RAND_DRBG_CTR *ctr)
  130. {
  131. if (ctr->bltmp_pos) {
  132. memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos);
  133. if (!ctr_BCC_blocks(ctr, ctr->bltmp))
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. __owur static int ctr_df(RAND_DRBG_CTR *ctr,
  139. const unsigned char *in1, size_t in1len,
  140. const unsigned char *in2, size_t in2len,
  141. const unsigned char *in3, size_t in3len)
  142. {
  143. static unsigned char c80 = 0x80;
  144. size_t inlen;
  145. unsigned char *p = ctr->bltmp;
  146. int outlen = AES_BLOCK_SIZE;
  147. if (!ctr_BCC_init(ctr))
  148. return 0;
  149. if (in1 == NULL)
  150. in1len = 0;
  151. if (in2 == NULL)
  152. in2len = 0;
  153. if (in3 == NULL)
  154. in3len = 0;
  155. inlen = in1len + in2len + in3len;
  156. /* Initialise L||N in temporary block */
  157. *p++ = (inlen >> 24) & 0xff;
  158. *p++ = (inlen >> 16) & 0xff;
  159. *p++ = (inlen >> 8) & 0xff;
  160. *p++ = inlen & 0xff;
  161. /* NB keylen is at most 32 bytes */
  162. *p++ = 0;
  163. *p++ = 0;
  164. *p++ = 0;
  165. *p = (unsigned char)((ctr->keylen + 16) & 0xff);
  166. ctr->bltmp_pos = 8;
  167. if (!ctr_BCC_update(ctr, in1, in1len)
  168. || !ctr_BCC_update(ctr, in2, in2len)
  169. || !ctr_BCC_update(ctr, in3, in3len)
  170. || !ctr_BCC_update(ctr, &c80, 1)
  171. || !ctr_BCC_final(ctr))
  172. return 0;
  173. /* Set up key K */
  174. if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->KX, NULL, -1))
  175. return 0;
  176. /* X follows key K */
  177. if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX, &outlen, ctr->KX + ctr->keylen,
  178. AES_BLOCK_SIZE)
  179. || outlen != AES_BLOCK_SIZE)
  180. return 0;
  181. if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 16, &outlen, ctr->KX,
  182. AES_BLOCK_SIZE)
  183. || outlen != AES_BLOCK_SIZE)
  184. return 0;
  185. if (ctr->keylen != 16)
  186. if (!EVP_CipherUpdate(ctr->ctx_ecb, ctr->KX + 32, &outlen,
  187. ctr->KX + 16, AES_BLOCK_SIZE)
  188. || outlen != AES_BLOCK_SIZE)
  189. return 0;
  190. return 1;
  191. }
  192. /*
  193. * NB the no-df Update in SP800-90A specifies a constant input length
  194. * of seedlen, however other uses of this algorithm pad the input with
  195. * zeroes if necessary and have up to two parameters XORed together,
  196. * so we handle both cases in this function instead.
  197. */
  198. __owur static int ctr_update(RAND_DRBG *drbg,
  199. const unsigned char *in1, size_t in1len,
  200. const unsigned char *in2, size_t in2len,
  201. const unsigned char *nonce, size_t noncelen)
  202. {
  203. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  204. int outlen = AES_BLOCK_SIZE;
  205. unsigned char V_tmp[48], out[48];
  206. unsigned char len;
  207. /* correct key is already set up. */
  208. memcpy(V_tmp, ctr->V, 16);
  209. inc_128(ctr);
  210. memcpy(V_tmp + 16, ctr->V, 16);
  211. if (ctr->keylen == 16) {
  212. len = 32;
  213. } else {
  214. inc_128(ctr);
  215. memcpy(V_tmp + 32, ctr->V, 16);
  216. len = 48;
  217. }
  218. if (!EVP_CipherUpdate(ctr->ctx_ecb, out, &outlen, V_tmp, len)
  219. || outlen != len)
  220. return 0;
  221. memcpy(ctr->K, out, ctr->keylen);
  222. memcpy(ctr->V, out + ctr->keylen, 16);
  223. if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
  224. /* If no input reuse existing derived value */
  225. if (in1 != NULL || nonce != NULL || in2 != NULL)
  226. if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))
  227. return 0;
  228. /* If this a reuse input in1len != 0 */
  229. if (in1len)
  230. ctr_XOR(ctr, ctr->KX, drbg->seedlen);
  231. } else {
  232. ctr_XOR(ctr, in1, in1len);
  233. ctr_XOR(ctr, in2, in2len);
  234. }
  235. if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1)
  236. || !EVP_CipherInit_ex(ctr->ctx_ctr, NULL, NULL, ctr->K, NULL, -1))
  237. return 0;
  238. return 1;
  239. }
  240. __owur static int drbg_ctr_instantiate(RAND_DRBG *drbg,
  241. const unsigned char *entropy, size_t entropylen,
  242. const unsigned char *nonce, size_t noncelen,
  243. const unsigned char *pers, size_t perslen)
  244. {
  245. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  246. if (entropy == NULL)
  247. return 0;
  248. memset(ctr->K, 0, sizeof(ctr->K));
  249. memset(ctr->V, 0, sizeof(ctr->V));
  250. if (!EVP_CipherInit_ex(ctr->ctx_ecb, NULL, NULL, ctr->K, NULL, -1))
  251. return 0;
  252. inc_128(ctr);
  253. if (!ctr_update(drbg, entropy, entropylen, pers, perslen, nonce, noncelen))
  254. return 0;
  255. return 1;
  256. }
  257. __owur static int drbg_ctr_reseed(RAND_DRBG *drbg,
  258. const unsigned char *entropy, size_t entropylen,
  259. const unsigned char *adin, size_t adinlen)
  260. {
  261. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  262. if (entropy == NULL)
  263. return 0;
  264. inc_128(ctr);
  265. if (!ctr_update(drbg, entropy, entropylen, adin, adinlen, NULL, 0))
  266. return 0;
  267. return 1;
  268. }
  269. static void ctr96_inc(unsigned char *counter)
  270. {
  271. u32 n = 12, c = 1;
  272. do {
  273. --n;
  274. c += counter[n];
  275. counter[n] = (u8)c;
  276. c >>= 8;
  277. } while (n);
  278. }
  279. __owur static int drbg_ctr_generate(RAND_DRBG *drbg,
  280. unsigned char *out, size_t outlen,
  281. const unsigned char *adin, size_t adinlen)
  282. {
  283. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  284. unsigned int ctr32, blocks;
  285. int outl, buflen;
  286. if (adin != NULL && adinlen != 0) {
  287. inc_128(ctr);
  288. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  289. return 0;
  290. /* This means we reuse derived value */
  291. if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
  292. adin = NULL;
  293. adinlen = 1;
  294. }
  295. } else {
  296. adinlen = 0;
  297. }
  298. inc_128(ctr);
  299. if (outlen == 0) {
  300. inc_128(ctr);
  301. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  302. return 0;
  303. return 1;
  304. }
  305. memset(out, 0, outlen);
  306. do {
  307. if (!EVP_CipherInit_ex(ctr->ctx_ctr,
  308. NULL, NULL, NULL, ctr->V, -1))
  309. return 0;
  310. /*-
  311. * outlen has type size_t while EVP_CipherUpdate takes an
  312. * int argument and thus cannot be guaranteed to process more
  313. * than 2^31-1 bytes at a time. We process such huge generate
  314. * requests in 2^30 byte chunks, which is the greatest multiple
  315. * of AES block size lower than or equal to 2^31-1.
  316. */
  317. buflen = outlen > (1U << 30) ? (1U << 30) : outlen;
  318. blocks = (buflen + 15) / 16;
  319. ctr32 = GETU32(ctr->V + 12) + blocks;
  320. if (ctr32 < blocks) {
  321. /* 32-bit counter overflow into V. */
  322. blocks -= ctr32;
  323. buflen = blocks * 16;
  324. ctr32 = 0;
  325. ctr96_inc(ctr->V);
  326. }
  327. PUTU32(ctr->V + 12, ctr32);
  328. if (!EVP_CipherUpdate(ctr->ctx_ctr, out, &outl, out, buflen)
  329. || outl != buflen)
  330. return 0;
  331. out += buflen;
  332. outlen -= buflen;
  333. } while (outlen);
  334. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  335. return 0;
  336. return 1;
  337. }
  338. static int drbg_ctr_uninstantiate(RAND_DRBG *drbg)
  339. {
  340. EVP_CIPHER_CTX_free(drbg->data.ctr.ctx_ecb);
  341. EVP_CIPHER_CTX_free(drbg->data.ctr.ctx_ctr);
  342. EVP_CIPHER_CTX_free(drbg->data.ctr.ctx_df);
  343. EVP_CIPHER_free(drbg->data.ctr.cipher_ecb);
  344. EVP_CIPHER_free(drbg->data.ctr.cipher_ctr);
  345. OPENSSL_cleanse(&drbg->data.ctr, sizeof(drbg->data.ctr));
  346. return 1;
  347. }
  348. static RAND_DRBG_METHOD drbg_ctr_meth = {
  349. drbg_ctr_instantiate,
  350. drbg_ctr_reseed,
  351. drbg_ctr_generate,
  352. drbg_ctr_uninstantiate
  353. };
  354. int drbg_ctr_init(RAND_DRBG *drbg)
  355. {
  356. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  357. size_t keylen;
  358. EVP_CIPHER *cipher_ecb = NULL;
  359. EVP_CIPHER *cipher_ctr = NULL;
  360. switch (drbg->type) {
  361. default:
  362. /* This can't happen, but silence the compiler warning. */
  363. return 0;
  364. case NID_aes_128_ctr:
  365. keylen = 16;
  366. cipher_ecb = EVP_CIPHER_fetch(drbg->libctx, "AES-128-ECB", "");
  367. cipher_ctr = EVP_CIPHER_fetch(drbg->libctx, "AES-128-CTR", "");
  368. break;
  369. case NID_aes_192_ctr:
  370. keylen = 24;
  371. cipher_ecb = EVP_CIPHER_fetch(drbg->libctx, "AES-192-ECB", "");
  372. cipher_ctr = EVP_CIPHER_fetch(drbg->libctx, "AES-192-CTR", "");
  373. break;
  374. case NID_aes_256_ctr:
  375. keylen = 32;
  376. cipher_ecb = EVP_CIPHER_fetch(drbg->libctx, "AES-256-ECB", "");
  377. cipher_ctr = EVP_CIPHER_fetch(drbg->libctx, "AES-256-CTR", "");
  378. break;
  379. }
  380. if (cipher_ecb == NULL || cipher_ctr == NULL)
  381. return 0;
  382. EVP_CIPHER_free(ctr->cipher_ecb);
  383. ctr->cipher_ecb = cipher_ecb;
  384. EVP_CIPHER_free(ctr->cipher_ctr);
  385. ctr->cipher_ctr = cipher_ctr;
  386. ctr->keylen = keylen;
  387. if (ctr->ctx_ecb == NULL)
  388. ctr->ctx_ecb = EVP_CIPHER_CTX_new();
  389. if (ctr->ctx_ctr == NULL)
  390. ctr->ctx_ctr = EVP_CIPHER_CTX_new();
  391. if (ctr->ctx_ecb == NULL || ctr->ctx_ctr == NULL
  392. || !EVP_CipherInit_ex(ctr->ctx_ecb,
  393. ctr->cipher_ecb, NULL, NULL, NULL, 1)
  394. || !EVP_CipherInit_ex(ctr->ctx_ctr,
  395. ctr->cipher_ctr, NULL, NULL, NULL, 1))
  396. return 0;
  397. drbg->meth = &drbg_ctr_meth;
  398. drbg->strength = keylen * 8;
  399. drbg->seedlen = keylen + 16;
  400. if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
  401. /* df initialisation */
  402. static const unsigned char df_key[32] = {
  403. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  404. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  405. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  406. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  407. };
  408. if (ctr->ctx_df == NULL)
  409. ctr->ctx_df = EVP_CIPHER_CTX_new();
  410. if (ctr->ctx_df == NULL)
  411. return 0;
  412. /* Set key schedule for df_key */
  413. if (!EVP_CipherInit_ex(ctr->ctx_df,
  414. ctr->cipher_ecb, NULL, df_key, NULL, 1))
  415. return 0;
  416. drbg->min_entropylen = ctr->keylen;
  417. drbg->max_entropylen = DRBG_MAX_LENGTH;
  418. drbg->min_noncelen = drbg->min_entropylen / 2;
  419. drbg->max_noncelen = DRBG_MAX_LENGTH;
  420. drbg->max_perslen = DRBG_MAX_LENGTH;
  421. drbg->max_adinlen = DRBG_MAX_LENGTH;
  422. } else {
  423. #ifdef FIPS_MODULE
  424. RANDerr(RAND_F_DRBG_CTR_INIT,
  425. RAND_R_DERIVATION_FUNCTION_MANDATORY_FOR_FIPS);
  426. return 0;
  427. #else
  428. drbg->min_entropylen = drbg->seedlen;
  429. drbg->max_entropylen = drbg->seedlen;
  430. /* Nonce not used */
  431. drbg->min_noncelen = 0;
  432. drbg->max_noncelen = 0;
  433. drbg->max_perslen = drbg->seedlen;
  434. drbg->max_adinlen = drbg->seedlen;
  435. #endif
  436. }
  437. drbg->max_request = 1 << 16;
  438. return 1;
  439. }