drbg_ctr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. /*
  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)
  58. {
  59. int i, outlen = AES_BLOCK_SIZE;
  60. for (i = 0; i < 16; i++)
  61. out[i] ^= in[i];
  62. if (!EVP_CipherUpdate(ctr->ctx_df, out, &outlen, out, AES_BLOCK_SIZE)
  63. || outlen != AES_BLOCK_SIZE)
  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. if (!ctr_BCC_block(ctr, ctr->KX, in)
  73. || !ctr_BCC_block(ctr, ctr->KX + 16, in))
  74. return 0;
  75. if (ctr->keylen != 16 && !ctr_BCC_block(ctr, ctr->KX + 32, in))
  76. return 0;
  77. return 1;
  78. }
  79. /*
  80. * Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:
  81. * see 10.3.1 stage 7.
  82. */
  83. __owur static int ctr_BCC_init(RAND_DRBG_CTR *ctr)
  84. {
  85. memset(ctr->KX, 0, 48);
  86. memset(ctr->bltmp, 0, 16);
  87. if (!ctr_BCC_block(ctr, ctr->KX, ctr->bltmp))
  88. return 0;
  89. ctr->bltmp[3] = 1;
  90. if (!ctr_BCC_block(ctr, ctr->KX + 16, ctr->bltmp))
  91. return 0;
  92. if (ctr->keylen != 16) {
  93. ctr->bltmp[3] = 2;
  94. if (!ctr_BCC_block(ctr, ctr->KX + 32, ctr->bltmp))
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. /*
  100. * Process several blocks into BCC algorithm, some possibly partial
  101. */
  102. __owur static int ctr_BCC_update(RAND_DRBG_CTR *ctr,
  103. const unsigned char *in, size_t inlen)
  104. {
  105. if (in == NULL || inlen == 0)
  106. return 1;
  107. /* If we have partial block handle it first */
  108. if (ctr->bltmp_pos) {
  109. size_t left = 16 - ctr->bltmp_pos;
  110. /* If we now have a complete block process it */
  111. if (inlen >= left) {
  112. memcpy(ctr->bltmp + ctr->bltmp_pos, in, left);
  113. if (!ctr_BCC_blocks(ctr, ctr->bltmp))
  114. return 0;
  115. ctr->bltmp_pos = 0;
  116. inlen -= left;
  117. in += left;
  118. }
  119. }
  120. /* Process zero or more complete blocks */
  121. for (; inlen >= 16; in += 16, inlen -= 16) {
  122. if (!ctr_BCC_blocks(ctr, in))
  123. return 0;
  124. }
  125. /* Copy any remaining partial block to the temporary buffer */
  126. if (inlen > 0) {
  127. memcpy(ctr->bltmp + ctr->bltmp_pos, in, inlen);
  128. ctr->bltmp_pos += inlen;
  129. }
  130. return 1;
  131. }
  132. __owur static int ctr_BCC_final(RAND_DRBG_CTR *ctr)
  133. {
  134. if (ctr->bltmp_pos) {
  135. memset(ctr->bltmp + ctr->bltmp_pos, 0, 16 - ctr->bltmp_pos);
  136. if (!ctr_BCC_blocks(ctr, ctr->bltmp))
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. __owur static int ctr_df(RAND_DRBG_CTR *ctr,
  142. const unsigned char *in1, size_t in1len,
  143. const unsigned char *in2, size_t in2len,
  144. const unsigned char *in3, size_t in3len)
  145. {
  146. static unsigned char c80 = 0x80;
  147. size_t inlen;
  148. unsigned char *p = ctr->bltmp;
  149. int outlen = AES_BLOCK_SIZE;
  150. if (!ctr_BCC_init(ctr))
  151. return 0;
  152. if (in1 == NULL)
  153. in1len = 0;
  154. if (in2 == NULL)
  155. in2len = 0;
  156. if (in3 == NULL)
  157. in3len = 0;
  158. inlen = in1len + in2len + in3len;
  159. /* Initialise L||N in temporary block */
  160. *p++ = (inlen >> 24) & 0xff;
  161. *p++ = (inlen >> 16) & 0xff;
  162. *p++ = (inlen >> 8) & 0xff;
  163. *p++ = inlen & 0xff;
  164. /* NB keylen is at most 32 bytes */
  165. *p++ = 0;
  166. *p++ = 0;
  167. *p++ = 0;
  168. *p = (unsigned char)((ctr->keylen + 16) & 0xff);
  169. ctr->bltmp_pos = 8;
  170. if (!ctr_BCC_update(ctr, in1, in1len)
  171. || !ctr_BCC_update(ctr, in2, in2len)
  172. || !ctr_BCC_update(ctr, in3, in3len)
  173. || !ctr_BCC_update(ctr, &c80, 1)
  174. || !ctr_BCC_final(ctr))
  175. return 0;
  176. /* Set up key K */
  177. if (!EVP_CipherInit_ex(ctr->ctx, ctr->cipher, NULL, ctr->KX, NULL, 1))
  178. return 0;
  179. /* X follows key K */
  180. if (!EVP_CipherUpdate(ctr->ctx, ctr->KX, &outlen, ctr->KX + ctr->keylen,
  181. AES_BLOCK_SIZE)
  182. || outlen != AES_BLOCK_SIZE)
  183. return 0;
  184. if (!EVP_CipherUpdate(ctr->ctx, ctr->KX + 16, &outlen, ctr->KX,
  185. AES_BLOCK_SIZE)
  186. || outlen != AES_BLOCK_SIZE)
  187. return 0;
  188. if (ctr->keylen != 16)
  189. if (!EVP_CipherUpdate(ctr->ctx, ctr->KX + 32, &outlen, ctr->KX + 16,
  190. AES_BLOCK_SIZE)
  191. || outlen != AES_BLOCK_SIZE)
  192. return 0;
  193. return 1;
  194. }
  195. /*
  196. * NB the no-df Update in SP800-90A specifies a constant input length
  197. * of seedlen, however other uses of this algorithm pad the input with
  198. * zeroes if necessary and have up to two parameters XORed together,
  199. * so we handle both cases in this function instead.
  200. */
  201. __owur static int ctr_update(RAND_DRBG *drbg,
  202. const unsigned char *in1, size_t in1len,
  203. const unsigned char *in2, size_t in2len,
  204. const unsigned char *nonce, size_t noncelen)
  205. {
  206. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  207. int outlen = AES_BLOCK_SIZE;
  208. /* correct key is already set up. */
  209. inc_128(ctr);
  210. if (!EVP_CipherUpdate(ctr->ctx, ctr->K, &outlen, ctr->V, AES_BLOCK_SIZE)
  211. || outlen != AES_BLOCK_SIZE)
  212. return 0;
  213. /* If keylen longer than 128 bits need extra encrypt */
  214. if (ctr->keylen != 16) {
  215. inc_128(ctr);
  216. if (!EVP_CipherUpdate(ctr->ctx, ctr->K+16, &outlen, ctr->V,
  217. AES_BLOCK_SIZE)
  218. || outlen != AES_BLOCK_SIZE)
  219. return 0;
  220. }
  221. inc_128(ctr);
  222. if (!EVP_CipherUpdate(ctr->ctx, ctr->V, &outlen, ctr->V, AES_BLOCK_SIZE)
  223. || outlen != AES_BLOCK_SIZE)
  224. return 0;
  225. /* If 192 bit key part of V is on end of K */
  226. if (ctr->keylen == 24) {
  227. memcpy(ctr->V + 8, ctr->V, 8);
  228. memcpy(ctr->V, ctr->K + 24, 8);
  229. }
  230. if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
  231. /* If no input reuse existing derived value */
  232. if (in1 != NULL || nonce != NULL || in2 != NULL)
  233. if (!ctr_df(ctr, in1, in1len, nonce, noncelen, in2, in2len))
  234. return 0;
  235. /* If this a reuse input in1len != 0 */
  236. if (in1len)
  237. ctr_XOR(ctr, ctr->KX, drbg->seedlen);
  238. } else {
  239. ctr_XOR(ctr, in1, in1len);
  240. ctr_XOR(ctr, in2, in2len);
  241. }
  242. if (!EVP_CipherInit_ex(ctr->ctx, ctr->cipher, NULL, ctr->K, NULL, 1))
  243. return 0;
  244. return 1;
  245. }
  246. __owur static int drbg_ctr_instantiate(RAND_DRBG *drbg,
  247. const unsigned char *entropy, size_t entropylen,
  248. const unsigned char *nonce, size_t noncelen,
  249. const unsigned char *pers, size_t perslen)
  250. {
  251. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  252. if (entropy == NULL)
  253. return 0;
  254. memset(ctr->K, 0, sizeof(ctr->K));
  255. memset(ctr->V, 0, sizeof(ctr->V));
  256. if (!EVP_CipherInit_ex(ctr->ctx, ctr->cipher, NULL, ctr->K, NULL, 1))
  257. return 0;
  258. if (!ctr_update(drbg, entropy, entropylen, pers, perslen, nonce, noncelen))
  259. return 0;
  260. return 1;
  261. }
  262. __owur static int drbg_ctr_reseed(RAND_DRBG *drbg,
  263. const unsigned char *entropy, size_t entropylen,
  264. const unsigned char *adin, size_t adinlen)
  265. {
  266. if (entropy == NULL)
  267. return 0;
  268. if (!ctr_update(drbg, entropy, entropylen, adin, adinlen, NULL, 0))
  269. return 0;
  270. return 1;
  271. }
  272. __owur static int drbg_ctr_generate(RAND_DRBG *drbg,
  273. unsigned char *out, size_t outlen,
  274. const unsigned char *adin, size_t adinlen)
  275. {
  276. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  277. if (adin != NULL && adinlen != 0) {
  278. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  279. return 0;
  280. /* This means we reuse derived value */
  281. if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
  282. adin = NULL;
  283. adinlen = 1;
  284. }
  285. } else {
  286. adinlen = 0;
  287. }
  288. for ( ; ; ) {
  289. int outl = AES_BLOCK_SIZE;
  290. inc_128(ctr);
  291. if (outlen < 16) {
  292. /* Use K as temp space as it will be updated */
  293. if (!EVP_CipherUpdate(ctr->ctx, ctr->K, &outl, ctr->V,
  294. AES_BLOCK_SIZE)
  295. || outl != AES_BLOCK_SIZE)
  296. return 0;
  297. memcpy(out, ctr->K, outlen);
  298. break;
  299. }
  300. if (!EVP_CipherUpdate(ctr->ctx, out, &outl, ctr->V, AES_BLOCK_SIZE)
  301. || outl != AES_BLOCK_SIZE)
  302. return 0;
  303. out += 16;
  304. outlen -= 16;
  305. if (outlen == 0)
  306. break;
  307. }
  308. if (!ctr_update(drbg, adin, adinlen, NULL, 0, NULL, 0))
  309. return 0;
  310. return 1;
  311. }
  312. static int drbg_ctr_uninstantiate(RAND_DRBG *drbg)
  313. {
  314. EVP_CIPHER_CTX_free(drbg->data.ctr.ctx);
  315. EVP_CIPHER_CTX_free(drbg->data.ctr.ctx_df);
  316. OPENSSL_cleanse(&drbg->data.ctr, sizeof(drbg->data.ctr));
  317. return 1;
  318. }
  319. static RAND_DRBG_METHOD drbg_ctr_meth = {
  320. drbg_ctr_instantiate,
  321. drbg_ctr_reseed,
  322. drbg_ctr_generate,
  323. drbg_ctr_uninstantiate
  324. };
  325. int drbg_ctr_init(RAND_DRBG *drbg)
  326. {
  327. RAND_DRBG_CTR *ctr = &drbg->data.ctr;
  328. size_t keylen;
  329. switch (drbg->type) {
  330. default:
  331. /* This can't happen, but silence the compiler warning. */
  332. return 0;
  333. case NID_aes_128_ctr:
  334. keylen = 16;
  335. ctr->cipher = EVP_aes_128_ecb();
  336. break;
  337. case NID_aes_192_ctr:
  338. keylen = 24;
  339. ctr->cipher = EVP_aes_192_ecb();
  340. break;
  341. case NID_aes_256_ctr:
  342. keylen = 32;
  343. ctr->cipher = EVP_aes_256_ecb();
  344. break;
  345. }
  346. drbg->meth = &drbg_ctr_meth;
  347. ctr->keylen = keylen;
  348. if (ctr->ctx == NULL)
  349. ctr->ctx = EVP_CIPHER_CTX_new();
  350. if (ctr->ctx == NULL)
  351. return 0;
  352. drbg->strength = keylen * 8;
  353. drbg->seedlen = keylen + 16;
  354. if ((drbg->flags & RAND_DRBG_FLAG_CTR_NO_DF) == 0) {
  355. /* df initialisation */
  356. static const unsigned char df_key[32] = {
  357. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  358. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  359. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  360. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
  361. };
  362. if (ctr->ctx_df == NULL)
  363. ctr->ctx_df = EVP_CIPHER_CTX_new();
  364. if (ctr->ctx_df == NULL)
  365. return 0;
  366. /* Set key schedule for df_key */
  367. if (!EVP_CipherInit_ex(ctr->ctx_df, ctr->cipher, NULL, df_key, NULL, 1))
  368. return 0;
  369. drbg->min_entropylen = ctr->keylen;
  370. drbg->max_entropylen = DRBG_MAX_LENGTH;
  371. drbg->min_noncelen = drbg->min_entropylen / 2;
  372. drbg->max_noncelen = DRBG_MAX_LENGTH;
  373. drbg->max_perslen = DRBG_MAX_LENGTH;
  374. drbg->max_adinlen = DRBG_MAX_LENGTH;
  375. } else {
  376. drbg->min_entropylen = drbg->seedlen;
  377. drbg->max_entropylen = drbg->seedlen;
  378. /* Nonce not used */
  379. drbg->min_noncelen = 0;
  380. drbg->max_noncelen = 0;
  381. drbg->max_perslen = drbg->seedlen;
  382. drbg->max_adinlen = drbg->seedlen;
  383. }
  384. drbg->max_request = 1 << 16;
  385. return 1;
  386. }