fips_drbg_ctr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /* fips/rand/fips_drbg_ctr.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. * project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. */
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <openssl/crypto.h>
  56. #include <openssl/evp.h>
  57. #include <openssl/aes.h>
  58. #include <openssl/fips.h>
  59. #include <openssl/fips_rand.h>
  60. #include "fips_rand_lcl.h"
  61. static void inc_128(DRBG_CTR_CTX *cctx)
  62. {
  63. int i;
  64. unsigned char c;
  65. unsigned char *p = cctx->V + 15;
  66. for (i = 0; i < 16; i++)
  67. {
  68. c = *p;
  69. c++;
  70. *p = c;
  71. if (c)
  72. return;
  73. p--;
  74. }
  75. }
  76. static void ctr_XOR(DRBG_CTR_CTX *cctx, const unsigned char *in, size_t inlen)
  77. {
  78. size_t i, n;
  79. /* Any zero padding will have no effect on the result as we
  80. * are XORing. So just process however much input we have.
  81. */
  82. if (!in || !inlen)
  83. return;
  84. if (inlen < cctx->keylen)
  85. n = inlen;
  86. else
  87. n = cctx->keylen;
  88. for (i = 0; i < n; i++)
  89. cctx->K[i] ^= in[i];
  90. if (inlen <= cctx->keylen)
  91. return;
  92. n = inlen - cctx->keylen;
  93. /* Should never happen */
  94. if (n > 16)
  95. n = 16;
  96. for (i = 0; i < 16; i++)
  97. cctx->V[i] ^= in[i + cctx->keylen];
  98. }
  99. /* Process a complete block using BCC algorithm of SPP 800-90 10.4.3 */
  100. static void ctr_BCC_block(DRBG_CTR_CTX *cctx, unsigned char *out,
  101. const unsigned char *in)
  102. {
  103. int i;
  104. for (i = 0; i < 16; i++)
  105. out[i] ^= in[i];
  106. AES_encrypt(out, out, &cctx->df_ks);
  107. #if 0
  108. fprintf(stderr, "BCC in+out\n");
  109. BIO_dump_fp(stderr, in, 16);
  110. BIO_dump_fp(stderr, out, 16);
  111. #endif
  112. }
  113. /* Handle several BCC operations for as much data as we need for K and X */
  114. static void ctr_BCC_blocks(DRBG_CTR_CTX *cctx, const unsigned char *in)
  115. {
  116. ctr_BCC_block(cctx, cctx->KX, in);
  117. ctr_BCC_block(cctx, cctx->KX + 16, in);
  118. if (cctx->keylen != 16)
  119. ctr_BCC_block(cctx, cctx->KX + 32, in);
  120. }
  121. /* Initialise BCC blocks: these have the value 0,1,2 in leftmost positions:
  122. * see 10.4.2 stage 7.
  123. */
  124. static void ctr_BCC_init(DRBG_CTR_CTX *cctx)
  125. {
  126. memset(cctx->KX, 0, 48);
  127. memset(cctx->bltmp, 0, 16);
  128. ctr_BCC_block(cctx, cctx->KX, cctx->bltmp);
  129. cctx->bltmp[3] = 1;
  130. ctr_BCC_block(cctx, cctx->KX + 16, cctx->bltmp);
  131. if (cctx->keylen != 16)
  132. {
  133. cctx->bltmp[3] = 2;
  134. ctr_BCC_block(cctx, cctx->KX + 32, cctx->bltmp);
  135. }
  136. }
  137. /* Process several blocks into BCC algorithm, some possibly partial */
  138. static void ctr_BCC_update(DRBG_CTR_CTX *cctx,
  139. const unsigned char *in, size_t inlen)
  140. {
  141. if (!in || !inlen)
  142. return;
  143. /* If we have partial block handle it first */
  144. if (cctx->bltmp_pos)
  145. {
  146. size_t left = 16 - cctx->bltmp_pos;
  147. /* If we now have a complete block process it */
  148. if (inlen >= left)
  149. {
  150. memcpy(cctx->bltmp + cctx->bltmp_pos, in, left);
  151. ctr_BCC_blocks(cctx, cctx->bltmp);
  152. cctx->bltmp_pos = 0;
  153. inlen -= left;
  154. in += left;
  155. }
  156. }
  157. /* Process zero or more complete blocks */
  158. while (inlen >= 16)
  159. {
  160. ctr_BCC_blocks(cctx, in);
  161. in += 16;
  162. inlen -= 16;
  163. }
  164. /* Copy any remaining partial block to the temporary buffer */
  165. if (inlen > 0)
  166. {
  167. memcpy(cctx->bltmp + cctx->bltmp_pos, in, inlen);
  168. cctx->bltmp_pos += inlen;
  169. }
  170. }
  171. static void ctr_BCC_final(DRBG_CTR_CTX *cctx)
  172. {
  173. if (cctx->bltmp_pos)
  174. {
  175. memset(cctx->bltmp + cctx->bltmp_pos, 0, 16 - cctx->bltmp_pos);
  176. ctr_BCC_blocks(cctx, cctx->bltmp);
  177. }
  178. }
  179. static void ctr_df(DRBG_CTR_CTX *cctx,
  180. const unsigned char *in1, size_t in1len,
  181. const unsigned char *in2, size_t in2len,
  182. const unsigned char *in3, size_t in3len)
  183. {
  184. size_t inlen;
  185. unsigned char *p = cctx->bltmp;
  186. static unsigned char c80 = 0x80;
  187. ctr_BCC_init(cctx);
  188. if (!in1)
  189. in1len = 0;
  190. if (!in2)
  191. in2len = 0;
  192. if (!in3)
  193. in3len = 0;
  194. inlen = in1len + in2len + in3len;
  195. /* Initialise L||N in temporary block */
  196. *p++ = (inlen >> 24) & 0xff;
  197. *p++ = (inlen >> 16) & 0xff;
  198. *p++ = (inlen >> 8) & 0xff;
  199. *p++ = inlen & 0xff;
  200. /* NB keylen is at most 32 bytes */
  201. *p++ = 0;
  202. *p++ = 0;
  203. *p++ = 0;
  204. *p = (unsigned char)((cctx->keylen + 16) & 0xff);
  205. cctx->bltmp_pos = 8;
  206. ctr_BCC_update(cctx, in1, in1len);
  207. ctr_BCC_update(cctx, in2, in2len);
  208. ctr_BCC_update(cctx, in3, in3len);
  209. ctr_BCC_update(cctx, &c80, 1);
  210. ctr_BCC_final(cctx);
  211. /* Set up key K */
  212. AES_set_encrypt_key(cctx->KX, cctx->keylen * 8, &cctx->df_kxks);
  213. /* X follows key K */
  214. AES_encrypt(cctx->KX + cctx->keylen, cctx->KX, &cctx->df_kxks);
  215. AES_encrypt(cctx->KX, cctx->KX + 16, &cctx->df_kxks);
  216. if (cctx->keylen != 16)
  217. AES_encrypt(cctx->KX + 16, cctx->KX + 32, &cctx->df_kxks);
  218. #if 0
  219. fprintf(stderr, "Output of ctr_df:\n");
  220. BIO_dump_fp(stderr, cctx->KX, cctx->keylen + 16);
  221. #endif
  222. }
  223. /* NB the no-df Update in SP800-90 specifies a constant input length
  224. * of seedlen, however other uses of this algorithm pad the input with
  225. * zeroes if necessary and have up to two parameters XORed together,
  226. * handle both cases in this function instead.
  227. */
  228. static void ctr_Update(DRBG_CTX *dctx,
  229. const unsigned char *in1, size_t in1len,
  230. const unsigned char *in2, size_t in2len,
  231. const unsigned char *nonce, size_t noncelen)
  232. {
  233. DRBG_CTR_CTX *cctx = &dctx->d.ctr;
  234. /* ks is already setup for correct key */
  235. inc_128(cctx);
  236. AES_encrypt(cctx->V, cctx->K, &cctx->ks);
  237. /* If keylen longer than 128 bits need extra encrypt */
  238. if (cctx->keylen != 16)
  239. {
  240. inc_128(cctx);
  241. AES_encrypt(cctx->V, cctx->K + 16, &cctx->ks);
  242. }
  243. inc_128(cctx);
  244. AES_encrypt(cctx->V, cctx->V, &cctx->ks);
  245. /* If 192 bit key part of V is on end of K */
  246. if (cctx->keylen == 24)
  247. {
  248. memcpy(cctx->V + 8, cctx->V, 8);
  249. memcpy(cctx->V, cctx->K + 24, 8);
  250. }
  251. if (dctx->flags & DRBG_FLAG_CTR_USE_DF)
  252. {
  253. /* If no input reuse existing derived value */
  254. if (in1 || nonce || in2)
  255. ctr_df(cctx, in1, in1len, nonce, noncelen, in2, in2len);
  256. /* If this a reuse input in1len != 0 */
  257. if (in1len)
  258. ctr_XOR(cctx, cctx->KX, dctx->seedlen);
  259. }
  260. else
  261. {
  262. ctr_XOR(cctx, in1, in1len);
  263. ctr_XOR(cctx, in2, in2len);
  264. }
  265. AES_set_encrypt_key(cctx->K, dctx->strength, &cctx->ks);
  266. #if 0
  267. fprintf(stderr, "K+V after update is:\n");
  268. BIO_dump_fp(stderr, cctx->K, cctx->keylen);
  269. BIO_dump_fp(stderr, cctx->V, 16);
  270. #endif
  271. }
  272. static int drbg_ctr_instantiate(DRBG_CTX *dctx,
  273. const unsigned char *ent, size_t entlen,
  274. const unsigned char *nonce, size_t noncelen,
  275. const unsigned char *pers, size_t perslen)
  276. {
  277. DRBG_CTR_CTX *cctx = &dctx->d.ctr;
  278. memset(cctx->K, 0, sizeof(cctx->K));
  279. memset(cctx->V, 0, sizeof(cctx->V));
  280. AES_set_encrypt_key(cctx->K, dctx->strength, &cctx->ks);
  281. ctr_Update(dctx, ent, entlen, pers, perslen, nonce, noncelen);
  282. return 1;
  283. }
  284. static int drbg_ctr_reseed(DRBG_CTX *dctx,
  285. const unsigned char *ent, size_t entlen,
  286. const unsigned char *adin, size_t adinlen)
  287. {
  288. ctr_Update(dctx, ent, entlen, adin, adinlen, NULL, 0);
  289. return 1;
  290. }
  291. static int drbg_ctr_generate(DRBG_CTX *dctx,
  292. unsigned char *out, size_t outlen,
  293. const unsigned char *adin, size_t adinlen)
  294. {
  295. DRBG_CTR_CTX *cctx = &dctx->d.ctr;
  296. if (adin && adinlen)
  297. {
  298. ctr_Update(dctx, adin, adinlen, NULL, 0, NULL, 0);
  299. /* This means we reuse derived value */
  300. if (dctx->flags & DRBG_FLAG_CTR_USE_DF)
  301. {
  302. adin = NULL;
  303. adinlen = 1;
  304. }
  305. }
  306. else
  307. adinlen = 0;
  308. for (;;)
  309. {
  310. inc_128(cctx);
  311. if (!(dctx->flags & DRBG_FLAG_TEST) && !dctx->lb_valid)
  312. {
  313. AES_encrypt(cctx->V, dctx->lb, &cctx->ks);
  314. dctx->lb_valid = 1;
  315. continue;
  316. }
  317. if (outlen < 16)
  318. {
  319. /* Use K as temp space as it will be updated */
  320. AES_encrypt(cctx->V, cctx->K, &cctx->ks);
  321. if (!fips_drbg_cprng_test(dctx, cctx->K))
  322. return 0;
  323. memcpy(out, cctx->K, outlen);
  324. break;
  325. }
  326. AES_encrypt(cctx->V, out, &cctx->ks);
  327. if (!fips_drbg_cprng_test(dctx, out))
  328. return 0;
  329. out += 16;
  330. outlen -= 16;
  331. if (outlen == 0)
  332. break;
  333. }
  334. ctr_Update(dctx, adin, adinlen, NULL, 0, NULL, 0);
  335. return 1;
  336. }
  337. static int drbg_ctr_uninstantiate(DRBG_CTX *dctx)
  338. {
  339. memset(&dctx->d.ctr, 0, sizeof(DRBG_CTR_CTX));
  340. return 1;
  341. }
  342. int fips_drbg_ctr_init(DRBG_CTX *dctx)
  343. {
  344. DRBG_CTR_CTX *cctx = &dctx->d.ctr;
  345. size_t keylen;
  346. switch (dctx->type)
  347. {
  348. case NID_aes_128_ctr:
  349. keylen = 16;
  350. break;
  351. case NID_aes_192_ctr:
  352. keylen = 24;
  353. break;
  354. case NID_aes_256_ctr:
  355. keylen = 32;
  356. break;
  357. default:
  358. return -2;
  359. }
  360. dctx->instantiate = drbg_ctr_instantiate;
  361. dctx->reseed = drbg_ctr_reseed;
  362. dctx->generate = drbg_ctr_generate;
  363. dctx->uninstantiate = drbg_ctr_uninstantiate;
  364. cctx->keylen = keylen;
  365. dctx->strength = keylen * 8;
  366. dctx->blocklength = 16;
  367. dctx->seedlen = keylen + 16;
  368. if (dctx->flags & DRBG_FLAG_CTR_USE_DF)
  369. {
  370. /* df initialisation */
  371. static unsigned char df_key[32] =
  372. {
  373. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  374. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  375. 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,
  376. 0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f
  377. };
  378. /* Set key schedule for df_key */
  379. AES_set_encrypt_key(df_key, dctx->strength, &cctx->df_ks);
  380. dctx->min_entropy = cctx->keylen;
  381. dctx->max_entropy = DRBG_MAX_LENGTH;
  382. dctx->min_nonce = dctx->min_entropy / 2;
  383. dctx->max_nonce = DRBG_MAX_LENGTH;
  384. dctx->max_pers = DRBG_MAX_LENGTH;
  385. dctx->max_adin = DRBG_MAX_LENGTH;
  386. }
  387. else
  388. {
  389. dctx->min_entropy = dctx->seedlen;
  390. dctx->max_entropy = dctx->seedlen;
  391. /* Nonce not used */
  392. dctx->min_nonce = 0;
  393. dctx->max_nonce = 0;
  394. dctx->max_pers = dctx->seedlen;
  395. dctx->max_adin = dctx->seedlen;
  396. }
  397. dctx->max_request = 1<<19;
  398. dctx->reseed_interval = 1<<24;
  399. return 1;
  400. }