drbg_ctr.c 14 KB

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