ctr_drbg.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * CTR_DRBG implementation based on AES-256 (NIST SP 800-90)
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * The NIST SP 800-90 DRBGs are described in the following publication.
  21. *
  22. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  23. */
  24. #include "common.h"
  25. #if defined(MBEDTLS_CTR_DRBG_C)
  26. #include "mbedtls/ctr_drbg.h"
  27. #include "mbedtls/platform_util.h"
  28. #include "mbedtls/error.h"
  29. #include <limits.h>
  30. #include <string.h>
  31. #if defined(MBEDTLS_FS_IO)
  32. #include <stdio.h>
  33. #endif
  34. #include "mbedtls/platform.h"
  35. /*
  36. * CTR_DRBG context initialization
  37. */
  38. void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx)
  39. {
  40. memset(ctx, 0, sizeof(mbedtls_ctr_drbg_context));
  41. /* Indicate that the entropy nonce length is not set explicitly.
  42. * See mbedtls_ctr_drbg_set_nonce_len(). */
  43. ctx->reseed_counter = -1;
  44. ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
  45. }
  46. /*
  47. * This function resets CTR_DRBG context to the state immediately
  48. * after initial call of mbedtls_ctr_drbg_init().
  49. */
  50. void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
  51. {
  52. if (ctx == NULL) {
  53. return;
  54. }
  55. #if defined(MBEDTLS_THREADING_C)
  56. /* The mutex is initialized iff f_entropy is set. */
  57. if (ctx->f_entropy != NULL) {
  58. mbedtls_mutex_free(&ctx->mutex);
  59. }
  60. #endif
  61. mbedtls_aes_free(&ctx->aes_ctx);
  62. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ctr_drbg_context));
  63. ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
  64. ctx->reseed_counter = -1;
  65. }
  66. void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx,
  67. int resistance)
  68. {
  69. ctx->prediction_resistance = resistance;
  70. }
  71. void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx,
  72. size_t len)
  73. {
  74. ctx->entropy_len = len;
  75. }
  76. int mbedtls_ctr_drbg_set_nonce_len(mbedtls_ctr_drbg_context *ctx,
  77. size_t len)
  78. {
  79. /* If mbedtls_ctr_drbg_seed() has already been called, it's
  80. * too late. Return the error code that's closest to making sense. */
  81. if (ctx->f_entropy != NULL) {
  82. return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
  83. }
  84. if (len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) {
  85. return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  86. }
  87. #if SIZE_MAX > INT_MAX
  88. /* This shouldn't be an issue because
  89. * MBEDTLS_CTR_DRBG_MAX_SEED_INPUT < INT_MAX in any sensible
  90. * configuration, but make sure anyway. */
  91. if (len > INT_MAX) {
  92. return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  93. }
  94. #endif
  95. /* For backward compatibility with Mbed TLS <= 2.19, store the
  96. * entropy nonce length in a field that already exists, but isn't
  97. * used until after the initial seeding. */
  98. /* Due to the capping of len above, the value fits in an int. */
  99. ctx->reseed_counter = (int) len;
  100. return 0;
  101. }
  102. void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx,
  103. int interval)
  104. {
  105. ctx->reseed_interval = interval;
  106. }
  107. static int block_cipher_df(unsigned char *output,
  108. const unsigned char *data, size_t data_len)
  109. {
  110. unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT +
  111. MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
  112. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  113. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  114. unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  115. unsigned char *p, *iv;
  116. mbedtls_aes_context aes_ctx;
  117. int ret = 0;
  118. int i, j;
  119. size_t buf_len, use_len;
  120. if (data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) {
  121. return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  122. }
  123. memset(buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT +
  124. MBEDTLS_CTR_DRBG_BLOCKSIZE + 16);
  125. mbedtls_aes_init(&aes_ctx);
  126. /*
  127. * Construct IV (16 bytes) and S in buffer
  128. * IV = Counter (in 32-bits) padded to 16 with zeroes
  129. * S = Length input string (in 32-bits) || Length of output (in 32-bits) ||
  130. * data || 0x80
  131. * (Total is padded to a multiple of 16-bytes with zeroes)
  132. */
  133. p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE;
  134. MBEDTLS_PUT_UINT32_BE(data_len, p, 0);
  135. p += 4 + 3;
  136. *p++ = MBEDTLS_CTR_DRBG_SEEDLEN;
  137. memcpy(p, data, data_len);
  138. p[data_len] = 0x80;
  139. buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1;
  140. for (i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++) {
  141. key[i] = i;
  142. }
  143. if ((ret = mbedtls_aes_setkey_enc(&aes_ctx, key,
  144. MBEDTLS_CTR_DRBG_KEYBITS)) != 0) {
  145. goto exit;
  146. }
  147. /*
  148. * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data
  149. */
  150. for (j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE) {
  151. p = buf;
  152. memset(chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE);
  153. use_len = buf_len;
  154. while (use_len > 0) {
  155. for (i = 0; i < MBEDTLS_CTR_DRBG_BLOCKSIZE; i++) {
  156. chain[i] ^= p[i];
  157. }
  158. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  159. use_len -= (use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE) ?
  160. MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len;
  161. if ((ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT,
  162. chain, chain)) != 0) {
  163. goto exit;
  164. }
  165. }
  166. memcpy(tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE);
  167. /*
  168. * Update IV
  169. */
  170. buf[3]++;
  171. }
  172. /*
  173. * Do final encryption with reduced data
  174. */
  175. if ((ret = mbedtls_aes_setkey_enc(&aes_ctx, tmp,
  176. MBEDTLS_CTR_DRBG_KEYBITS)) != 0) {
  177. goto exit;
  178. }
  179. iv = tmp + MBEDTLS_CTR_DRBG_KEYSIZE;
  180. p = output;
  181. for (j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE) {
  182. if ((ret = mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT,
  183. iv, iv)) != 0) {
  184. goto exit;
  185. }
  186. memcpy(p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE);
  187. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  188. }
  189. exit:
  190. mbedtls_aes_free(&aes_ctx);
  191. /*
  192. * tidy up the stack
  193. */
  194. mbedtls_platform_zeroize(buf, sizeof(buf));
  195. mbedtls_platform_zeroize(tmp, sizeof(tmp));
  196. mbedtls_platform_zeroize(key, sizeof(key));
  197. mbedtls_platform_zeroize(chain, sizeof(chain));
  198. if (0 != ret) {
  199. /*
  200. * wipe partial seed from memory
  201. */
  202. mbedtls_platform_zeroize(output, MBEDTLS_CTR_DRBG_SEEDLEN);
  203. }
  204. return ret;
  205. }
  206. /* CTR_DRBG_Update (SP 800-90A &sect;10.2.1.2)
  207. * ctr_drbg_update_internal(ctx, provided_data)
  208. * implements
  209. * CTR_DRBG_Update(provided_data, Key, V)
  210. * with inputs and outputs
  211. * ctx->aes_ctx = Key
  212. * ctx->counter = V
  213. */
  214. static int ctr_drbg_update_internal(mbedtls_ctr_drbg_context *ctx,
  215. const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN])
  216. {
  217. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  218. unsigned char *p = tmp;
  219. int i, j;
  220. int ret = 0;
  221. memset(tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN);
  222. for (j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE) {
  223. /*
  224. * Increase counter
  225. */
  226. for (i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i--) {
  227. if (++ctx->counter[i - 1] != 0) {
  228. break;
  229. }
  230. }
  231. /*
  232. * Crypt counter block
  233. */
  234. if ((ret = mbedtls_aes_crypt_ecb(&ctx->aes_ctx, MBEDTLS_AES_ENCRYPT,
  235. ctx->counter, p)) != 0) {
  236. goto exit;
  237. }
  238. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  239. }
  240. for (i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++) {
  241. tmp[i] ^= data[i];
  242. }
  243. /*
  244. * Update key and counter
  245. */
  246. if ((ret = mbedtls_aes_setkey_enc(&ctx->aes_ctx, tmp,
  247. MBEDTLS_CTR_DRBG_KEYBITS)) != 0) {
  248. goto exit;
  249. }
  250. memcpy(ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE,
  251. MBEDTLS_CTR_DRBG_BLOCKSIZE);
  252. exit:
  253. mbedtls_platform_zeroize(tmp, sizeof(tmp));
  254. return ret;
  255. }
  256. /* CTR_DRBG_Instantiate with derivation function (SP 800-90A &sect;10.2.1.3.2)
  257. * mbedtls_ctr_drbg_update(ctx, additional, add_len)
  258. * implements
  259. * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
  260. * security_strength) -> initial_working_state
  261. * with inputs
  262. * ctx->counter = all-bits-0
  263. * ctx->aes_ctx = context from all-bits-0 key
  264. * additional[:add_len] = entropy_input || nonce || personalization_string
  265. * and with outputs
  266. * ctx = initial_working_state
  267. */
  268. int mbedtls_ctr_drbg_update_ret(mbedtls_ctr_drbg_context *ctx,
  269. const unsigned char *additional,
  270. size_t add_len)
  271. {
  272. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  273. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  274. if (add_len == 0) {
  275. return 0;
  276. }
  277. if ((ret = block_cipher_df(add_input, additional, add_len)) != 0) {
  278. goto exit;
  279. }
  280. if ((ret = ctr_drbg_update_internal(ctx, add_input)) != 0) {
  281. goto exit;
  282. }
  283. exit:
  284. mbedtls_platform_zeroize(add_input, sizeof(add_input));
  285. return ret;
  286. }
  287. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  288. void mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx,
  289. const unsigned char *additional,
  290. size_t add_len)
  291. {
  292. /* MAX_INPUT would be more logical here, but we have to match
  293. * block_cipher_df()'s limits since we can't propagate errors */
  294. if (add_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) {
  295. add_len = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT;
  296. }
  297. (void) mbedtls_ctr_drbg_update_ret(ctx, additional, add_len);
  298. }
  299. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  300. /* CTR_DRBG_Reseed with derivation function (SP 800-90A &sect;10.2.1.4.2)
  301. * mbedtls_ctr_drbg_reseed(ctx, additional, len, nonce_len)
  302. * implements
  303. * CTR_DRBG_Reseed(working_state, entropy_input, additional_input)
  304. * -> new_working_state
  305. * with inputs
  306. * ctx contains working_state
  307. * additional[:len] = additional_input
  308. * and entropy_input comes from calling ctx->f_entropy
  309. * for (ctx->entropy_len + nonce_len) bytes
  310. * and with output
  311. * ctx contains new_working_state
  312. */
  313. static int mbedtls_ctr_drbg_reseed_internal(mbedtls_ctr_drbg_context *ctx,
  314. const unsigned char *additional,
  315. size_t len,
  316. size_t nonce_len)
  317. {
  318. unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
  319. size_t seedlen = 0;
  320. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  321. if (ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT) {
  322. return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  323. }
  324. if (nonce_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len) {
  325. return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  326. }
  327. if (len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len - nonce_len) {
  328. return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  329. }
  330. memset(seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT);
  331. /* Gather entropy_len bytes of entropy to seed state. */
  332. if (0 != ctx->f_entropy(ctx->p_entropy, seed, ctx->entropy_len)) {
  333. return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
  334. }
  335. seedlen += ctx->entropy_len;
  336. /* Gather entropy for a nonce if requested. */
  337. if (nonce_len != 0) {
  338. if (0 != ctx->f_entropy(ctx->p_entropy, seed + seedlen, nonce_len)) {
  339. return MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED;
  340. }
  341. seedlen += nonce_len;
  342. }
  343. /* Add additional data if provided. */
  344. if (additional != NULL && len != 0) {
  345. memcpy(seed + seedlen, additional, len);
  346. seedlen += len;
  347. }
  348. /* Reduce to 384 bits. */
  349. if ((ret = block_cipher_df(seed, seed, seedlen)) != 0) {
  350. goto exit;
  351. }
  352. /* Update state. */
  353. if ((ret = ctr_drbg_update_internal(ctx, seed)) != 0) {
  354. goto exit;
  355. }
  356. ctx->reseed_counter = 1;
  357. exit:
  358. mbedtls_platform_zeroize(seed, sizeof(seed));
  359. return ret;
  360. }
  361. int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx,
  362. const unsigned char *additional, size_t len)
  363. {
  364. return mbedtls_ctr_drbg_reseed_internal(ctx, additional, len, 0);
  365. }
  366. /* Return a "good" nonce length for CTR_DRBG. The chosen nonce length
  367. * is sufficient to achieve the maximum security strength given the key
  368. * size and entropy length. If there is enough entropy in the initial
  369. * call to the entropy function to serve as both the entropy input and
  370. * the nonce, don't make a second call to get a nonce. */
  371. static size_t good_nonce_len(size_t entropy_len)
  372. {
  373. if (entropy_len >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2) {
  374. return 0;
  375. } else {
  376. return (entropy_len + 1) / 2;
  377. }
  378. }
  379. /* CTR_DRBG_Instantiate with derivation function (SP 800-90A &sect;10.2.1.3.2)
  380. * mbedtls_ctr_drbg_seed(ctx, f_entropy, p_entropy, custom, len)
  381. * implements
  382. * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
  383. * security_strength) -> initial_working_state
  384. * with inputs
  385. * custom[:len] = nonce || personalization_string
  386. * where entropy_input comes from f_entropy for ctx->entropy_len bytes
  387. * and with outputs
  388. * ctx = initial_working_state
  389. */
  390. int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx,
  391. int (*f_entropy)(void *, unsigned char *, size_t),
  392. void *p_entropy,
  393. const unsigned char *custom,
  394. size_t len)
  395. {
  396. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  397. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  398. size_t nonce_len;
  399. memset(key, 0, MBEDTLS_CTR_DRBG_KEYSIZE);
  400. /* The mutex is initialized iff f_entropy is set. */
  401. #if defined(MBEDTLS_THREADING_C)
  402. mbedtls_mutex_init(&ctx->mutex);
  403. #endif
  404. mbedtls_aes_init(&ctx->aes_ctx);
  405. ctx->f_entropy = f_entropy;
  406. ctx->p_entropy = p_entropy;
  407. if (ctx->entropy_len == 0) {
  408. ctx->entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN;
  409. }
  410. /* ctx->reseed_counter contains the desired amount of entropy to
  411. * grab for a nonce (see mbedtls_ctr_drbg_set_nonce_len()).
  412. * If it's -1, indicating that the entropy nonce length was not set
  413. * explicitly, use a sufficiently large nonce for security. */
  414. nonce_len = (ctx->reseed_counter >= 0 ?
  415. (size_t) ctx->reseed_counter :
  416. good_nonce_len(ctx->entropy_len));
  417. /* Initialize with an empty key. */
  418. if ((ret = mbedtls_aes_setkey_enc(&ctx->aes_ctx, key,
  419. MBEDTLS_CTR_DRBG_KEYBITS)) != 0) {
  420. return ret;
  421. }
  422. /* Do the initial seeding. */
  423. if ((ret = mbedtls_ctr_drbg_reseed_internal(ctx, custom, len,
  424. nonce_len)) != 0) {
  425. return ret;
  426. }
  427. return 0;
  428. }
  429. /* CTR_DRBG_Generate with derivation function (SP 800-90A &sect;10.2.1.5.2)
  430. * mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, additional, add_len)
  431. * implements
  432. * CTR_DRBG_Reseed(working_state, entropy_input, additional[:add_len])
  433. * -> working_state_after_reseed
  434. * if required, then
  435. * CTR_DRBG_Generate(working_state_after_reseed,
  436. * requested_number_of_bits, additional_input)
  437. * -> status, returned_bits, new_working_state
  438. * with inputs
  439. * ctx contains working_state
  440. * requested_number_of_bits = 8 * output_len
  441. * additional[:add_len] = additional_input
  442. * and entropy_input comes from calling ctx->f_entropy
  443. * and with outputs
  444. * status = SUCCESS (this function does the reseed internally)
  445. * returned_bits = output[:output_len]
  446. * ctx contains new_working_state
  447. */
  448. int mbedtls_ctr_drbg_random_with_add(void *p_rng,
  449. unsigned char *output, size_t output_len,
  450. const unsigned char *additional, size_t add_len)
  451. {
  452. int ret = 0;
  453. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  454. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  455. unsigned char *p = output;
  456. unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  457. int i;
  458. size_t use_len;
  459. if (output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST) {
  460. return MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG;
  461. }
  462. if (add_len > MBEDTLS_CTR_DRBG_MAX_INPUT) {
  463. return MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  464. }
  465. memset(add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN);
  466. if (ctx->reseed_counter > ctx->reseed_interval ||
  467. ctx->prediction_resistance) {
  468. if ((ret = mbedtls_ctr_drbg_reseed(ctx, additional, add_len)) != 0) {
  469. return ret;
  470. }
  471. add_len = 0;
  472. }
  473. if (add_len > 0) {
  474. if ((ret = block_cipher_df(add_input, additional, add_len)) != 0) {
  475. goto exit;
  476. }
  477. if ((ret = ctr_drbg_update_internal(ctx, add_input)) != 0) {
  478. goto exit;
  479. }
  480. }
  481. while (output_len > 0) {
  482. /*
  483. * Increase counter
  484. */
  485. for (i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i--) {
  486. if (++ctx->counter[i - 1] != 0) {
  487. break;
  488. }
  489. }
  490. /*
  491. * Crypt counter block
  492. */
  493. if ((ret = mbedtls_aes_crypt_ecb(&ctx->aes_ctx, MBEDTLS_AES_ENCRYPT,
  494. ctx->counter, tmp)) != 0) {
  495. goto exit;
  496. }
  497. use_len = (output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE)
  498. ? MBEDTLS_CTR_DRBG_BLOCKSIZE : output_len;
  499. /*
  500. * Copy random block to destination
  501. */
  502. memcpy(p, tmp, use_len);
  503. p += use_len;
  504. output_len -= use_len;
  505. }
  506. if ((ret = ctr_drbg_update_internal(ctx, add_input)) != 0) {
  507. goto exit;
  508. }
  509. ctx->reseed_counter++;
  510. exit:
  511. mbedtls_platform_zeroize(add_input, sizeof(add_input));
  512. mbedtls_platform_zeroize(tmp, sizeof(tmp));
  513. return ret;
  514. }
  515. int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output,
  516. size_t output_len)
  517. {
  518. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  519. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  520. #if defined(MBEDTLS_THREADING_C)
  521. if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
  522. return ret;
  523. }
  524. #endif
  525. ret = mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, NULL, 0);
  526. #if defined(MBEDTLS_THREADING_C)
  527. if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
  528. return MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  529. }
  530. #endif
  531. return ret;
  532. }
  533. #if defined(MBEDTLS_FS_IO)
  534. int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx,
  535. const char *path)
  536. {
  537. int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  538. FILE *f;
  539. unsigned char buf[MBEDTLS_CTR_DRBG_MAX_INPUT];
  540. if ((f = fopen(path, "wb")) == NULL) {
  541. return MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  542. }
  543. if ((ret = mbedtls_ctr_drbg_random(ctx, buf,
  544. MBEDTLS_CTR_DRBG_MAX_INPUT)) != 0) {
  545. goto exit;
  546. }
  547. if (fwrite(buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f) !=
  548. MBEDTLS_CTR_DRBG_MAX_INPUT) {
  549. ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  550. } else {
  551. ret = 0;
  552. }
  553. exit:
  554. mbedtls_platform_zeroize(buf, sizeof(buf));
  555. fclose(f);
  556. return ret;
  557. }
  558. int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx,
  559. const char *path)
  560. {
  561. int ret = 0;
  562. FILE *f = NULL;
  563. size_t n;
  564. unsigned char buf[MBEDTLS_CTR_DRBG_MAX_INPUT];
  565. unsigned char c;
  566. if ((f = fopen(path, "rb")) == NULL) {
  567. return MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  568. }
  569. n = fread(buf, 1, sizeof(buf), f);
  570. if (fread(&c, 1, 1, f) != 0) {
  571. ret = MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  572. goto exit;
  573. }
  574. if (n == 0 || ferror(f)) {
  575. ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  576. goto exit;
  577. }
  578. fclose(f);
  579. f = NULL;
  580. ret = mbedtls_ctr_drbg_update_ret(ctx, buf, n);
  581. exit:
  582. mbedtls_platform_zeroize(buf, sizeof(buf));
  583. if (f != NULL) {
  584. fclose(f);
  585. }
  586. if (ret != 0) {
  587. return ret;
  588. }
  589. return mbedtls_ctr_drbg_write_seed_file(ctx, path);
  590. }
  591. #endif /* MBEDTLS_FS_IO */
  592. #if defined(MBEDTLS_SELF_TEST)
  593. /* The CTR_DRBG NIST test vectors used here are available at
  594. * https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/drbg/drbgtestvectors.zip
  595. *
  596. * The parameters used to derive the test data are:
  597. *
  598. * [AES-128 use df]
  599. * [PredictionResistance = True/False]
  600. * [EntropyInputLen = 128]
  601. * [NonceLen = 64]
  602. * [PersonalizationStringLen = 128]
  603. * [AdditionalInputLen = 0]
  604. * [ReturnedBitsLen = 512]
  605. *
  606. * [AES-256 use df]
  607. * [PredictionResistance = True/False]
  608. * [EntropyInputLen = 256]
  609. * [NonceLen = 128]
  610. * [PersonalizationStringLen = 256]
  611. * [AdditionalInputLen = 0]
  612. * [ReturnedBitsLen = 512]
  613. *
  614. */
  615. #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
  616. static const unsigned char entropy_source_pr[] =
  617. { 0x04, 0xd9, 0x49, 0xa6, 0xdc, 0xe8, 0x6e, 0xbb,
  618. 0xf1, 0x08, 0x77, 0x2b, 0x9e, 0x08, 0xca, 0x92,
  619. 0x65, 0x16, 0xda, 0x99, 0xa2, 0x59, 0xf3, 0xe8,
  620. 0x38, 0x7e, 0x3f, 0x6b, 0x51, 0x70, 0x7b, 0x20,
  621. 0xec, 0x53, 0xd0, 0x66, 0xc3, 0x0f, 0xe3, 0xb0,
  622. 0xe0, 0x86, 0xa6, 0xaa, 0x5f, 0x72, 0x2f, 0xad,
  623. 0xf7, 0xef, 0x06, 0xb8, 0xd6, 0x9c, 0x9d, 0xe8 };
  624. static const unsigned char entropy_source_nopr[] =
  625. { 0x07, 0x0d, 0x59, 0x63, 0x98, 0x73, 0xa5, 0x45,
  626. 0x27, 0x38, 0x22, 0x7b, 0x76, 0x85, 0xd1, 0xa9,
  627. 0x74, 0x18, 0x1f, 0x3c, 0x22, 0xf6, 0x49, 0x20,
  628. 0x4a, 0x47, 0xc2, 0xf3, 0x85, 0x16, 0xb4, 0x6f,
  629. 0x00, 0x2e, 0x71, 0xda, 0xed, 0x16, 0x9b, 0x5c };
  630. static const unsigned char pers_pr[] =
  631. { 0xbf, 0xa4, 0x9a, 0x8f, 0x7b, 0xd8, 0xb1, 0x7a,
  632. 0x9d, 0xfa, 0x45, 0xed, 0x21, 0x52, 0xb3, 0xad };
  633. static const unsigned char pers_nopr[] =
  634. { 0x4e, 0x61, 0x79, 0xd4, 0xc2, 0x72, 0xa1, 0x4c,
  635. 0xf1, 0x3d, 0xf6, 0x5e, 0xa3, 0xa6, 0xe5, 0x0f };
  636. static const unsigned char result_pr[] =
  637. { 0xc9, 0x0a, 0xaf, 0x85, 0x89, 0x71, 0x44, 0x66,
  638. 0x4f, 0x25, 0x0b, 0x2b, 0xde, 0xd8, 0xfa, 0xff,
  639. 0x52, 0x5a, 0x1b, 0x32, 0x5e, 0x41, 0x7a, 0x10,
  640. 0x1f, 0xef, 0x1e, 0x62, 0x23, 0xe9, 0x20, 0x30,
  641. 0xc9, 0x0d, 0xad, 0x69, 0xb4, 0x9c, 0x5b, 0xf4,
  642. 0x87, 0x42, 0xd5, 0xae, 0x5e, 0x5e, 0x43, 0xcc,
  643. 0xd9, 0xfd, 0x0b, 0x93, 0x4a, 0xe3, 0xd4, 0x06,
  644. 0x37, 0x36, 0x0f, 0x3f, 0x72, 0x82, 0x0c, 0xcf };
  645. static const unsigned char result_nopr[] =
  646. { 0x31, 0xc9, 0x91, 0x09, 0xf8, 0xc5, 0x10, 0x13,
  647. 0x3c, 0xd3, 0x96, 0xf9, 0xbc, 0x2c, 0x12, 0xc0,
  648. 0x7c, 0xc1, 0x61, 0x5f, 0xa3, 0x09, 0x99, 0xaf,
  649. 0xd7, 0xf2, 0x36, 0xfd, 0x40, 0x1a, 0x8b, 0xf2,
  650. 0x33, 0x38, 0xee, 0x1d, 0x03, 0x5f, 0x83, 0xb7,
  651. 0xa2, 0x53, 0xdc, 0xee, 0x18, 0xfc, 0xa7, 0xf2,
  652. 0xee, 0x96, 0xc6, 0xc2, 0xcd, 0x0c, 0xff, 0x02,
  653. 0x76, 0x70, 0x69, 0xaa, 0x69, 0xd1, 0x3b, 0xe8 };
  654. #else /* MBEDTLS_CTR_DRBG_USE_128_BIT_KEY */
  655. static const unsigned char entropy_source_pr[] =
  656. { 0xca, 0x58, 0xfd, 0xf2, 0xb9, 0x77, 0xcb, 0x49,
  657. 0xd4, 0xe0, 0x5b, 0xe2, 0x39, 0x50, 0xd9, 0x8a,
  658. 0x6a, 0xb3, 0xc5, 0x2f, 0xdf, 0x74, 0xd5, 0x85,
  659. 0x8f, 0xd1, 0xba, 0x64, 0x54, 0x7b, 0xdb, 0x1e,
  660. 0xc5, 0xea, 0x24, 0xc0, 0xfa, 0x0c, 0x90, 0x15,
  661. 0x09, 0x20, 0x92, 0x42, 0x32, 0x36, 0x45, 0x45,
  662. 0x7d, 0x20, 0x76, 0x6b, 0xcf, 0xa2, 0x15, 0xc8,
  663. 0x2f, 0x9f, 0xbc, 0x88, 0x3f, 0x80, 0xd1, 0x2c,
  664. 0xb7, 0x16, 0xd1, 0x80, 0x9e, 0xe1, 0xc9, 0xb3,
  665. 0x88, 0x1b, 0x21, 0x45, 0xef, 0xa1, 0x7f, 0xce,
  666. 0xc8, 0x92, 0x35, 0x55, 0x2a, 0xd9, 0x1d, 0x8e,
  667. 0x12, 0x38, 0xac, 0x01, 0x4e, 0x38, 0x18, 0x76,
  668. 0x9c, 0xf2, 0xb6, 0xd4, 0x13, 0xb6, 0x2c, 0x77,
  669. 0xc0, 0xe7, 0xe6, 0x0c, 0x47, 0x44, 0x95, 0xbe };
  670. static const unsigned char entropy_source_nopr[] =
  671. { 0x4c, 0xfb, 0x21, 0x86, 0x73, 0x34, 0x6d, 0x9d,
  672. 0x50, 0xc9, 0x22, 0xe4, 0x9b, 0x0d, 0xfc, 0xd0,
  673. 0x90, 0xad, 0xf0, 0x4f, 0x5c, 0x3b, 0xa4, 0x73,
  674. 0x27, 0xdf, 0xcd, 0x6f, 0xa6, 0x3a, 0x78, 0x5c,
  675. 0x01, 0x69, 0x62, 0xa7, 0xfd, 0x27, 0x87, 0xa2,
  676. 0x4b, 0xf6, 0xbe, 0x47, 0xef, 0x37, 0x83, 0xf1,
  677. 0xb7, 0xec, 0x46, 0x07, 0x23, 0x63, 0x83, 0x4a,
  678. 0x1b, 0x01, 0x33, 0xf2, 0xc2, 0x38, 0x91, 0xdb,
  679. 0x4f, 0x11, 0xa6, 0x86, 0x51, 0xf2, 0x3e, 0x3a,
  680. 0x8b, 0x1f, 0xdc, 0x03, 0xb1, 0x92, 0xc7, 0xe7 };
  681. static const unsigned char pers_pr[] =
  682. { 0x5a, 0x70, 0x95, 0xe9, 0x81, 0x40, 0x52, 0x33,
  683. 0x91, 0x53, 0x7e, 0x75, 0xd6, 0x19, 0x9d, 0x1e,
  684. 0xad, 0x0d, 0xc6, 0xa7, 0xde, 0x6c, 0x1f, 0xe0,
  685. 0xea, 0x18, 0x33, 0xa8, 0x7e, 0x06, 0x20, 0xe9 };
  686. static const unsigned char pers_nopr[] =
  687. { 0x88, 0xee, 0xb8, 0xe0, 0xe8, 0x3b, 0xf3, 0x29,
  688. 0x4b, 0xda, 0xcd, 0x60, 0x99, 0xeb, 0xe4, 0xbf,
  689. 0x55, 0xec, 0xd9, 0x11, 0x3f, 0x71, 0xe5, 0xeb,
  690. 0xcb, 0x45, 0x75, 0xf3, 0xd6, 0xa6, 0x8a, 0x6b };
  691. static const unsigned char result_pr[] =
  692. { 0xce, 0x2f, 0xdb, 0xb6, 0xd9, 0xb7, 0x39, 0x85,
  693. 0x04, 0xc5, 0xc0, 0x42, 0xc2, 0x31, 0xc6, 0x1d,
  694. 0x9b, 0x5a, 0x59, 0xf8, 0x7e, 0x0d, 0xcc, 0x62,
  695. 0x7b, 0x65, 0x11, 0x55, 0x10, 0xeb, 0x9e, 0x3d,
  696. 0xa4, 0xfb, 0x1c, 0x6a, 0x18, 0xc0, 0x74, 0xdb,
  697. 0xdd, 0xe7, 0x02, 0x23, 0x63, 0x21, 0xd0, 0x39,
  698. 0xf9, 0xa7, 0xc4, 0x52, 0x84, 0x3b, 0x49, 0x40,
  699. 0x72, 0x2b, 0xb0, 0x6c, 0x9c, 0xdb, 0xc3, 0x43 };
  700. static const unsigned char result_nopr[] =
  701. { 0xa5, 0x51, 0x80, 0xa1, 0x90, 0xbe, 0xf3, 0xad,
  702. 0xaf, 0x28, 0xf6, 0xb7, 0x95, 0xe9, 0xf1, 0xf3,
  703. 0xd6, 0xdf, 0xa1, 0xb2, 0x7d, 0xd0, 0x46, 0x7b,
  704. 0x0c, 0x75, 0xf5, 0xfa, 0x93, 0x1e, 0x97, 0x14,
  705. 0x75, 0xb2, 0x7c, 0xae, 0x03, 0xa2, 0x96, 0x54,
  706. 0xe2, 0xf4, 0x09, 0x66, 0xea, 0x33, 0x64, 0x30,
  707. 0x40, 0xd1, 0x40, 0x0f, 0xe6, 0x77, 0x87, 0x3a,
  708. 0xf8, 0x09, 0x7c, 0x1f, 0xe9, 0xf0, 0x02, 0x98 };
  709. #endif /* MBEDTLS_CTR_DRBG_USE_128_BIT_KEY */
  710. static size_t test_offset;
  711. static int ctr_drbg_self_test_entropy(void *data, unsigned char *buf,
  712. size_t len)
  713. {
  714. const unsigned char *p = data;
  715. memcpy(buf, p + test_offset, len);
  716. test_offset += len;
  717. return 0;
  718. }
  719. #define CHK(c) if ((c) != 0) \
  720. { \
  721. if (verbose != 0) \
  722. mbedtls_printf("failed\n"); \
  723. return 1; \
  724. }
  725. #define SELF_TEST_OUTPUT_DISCARD_LENGTH 64
  726. /*
  727. * Checkup routine
  728. */
  729. int mbedtls_ctr_drbg_self_test(int verbose)
  730. {
  731. mbedtls_ctr_drbg_context ctx;
  732. unsigned char buf[sizeof(result_pr)];
  733. mbedtls_ctr_drbg_init(&ctx);
  734. /*
  735. * Based on a NIST CTR_DRBG test vector (PR = True)
  736. */
  737. if (verbose != 0) {
  738. mbedtls_printf(" CTR_DRBG (PR = TRUE) : ");
  739. }
  740. test_offset = 0;
  741. mbedtls_ctr_drbg_set_entropy_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE);
  742. mbedtls_ctr_drbg_set_nonce_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE / 2);
  743. CHK(mbedtls_ctr_drbg_seed(&ctx,
  744. ctr_drbg_self_test_entropy,
  745. (void *) entropy_source_pr,
  746. pers_pr, MBEDTLS_CTR_DRBG_KEYSIZE));
  747. mbedtls_ctr_drbg_set_prediction_resistance(&ctx, MBEDTLS_CTR_DRBG_PR_ON);
  748. CHK(mbedtls_ctr_drbg_random(&ctx, buf, SELF_TEST_OUTPUT_DISCARD_LENGTH));
  749. CHK(mbedtls_ctr_drbg_random(&ctx, buf, sizeof(result_pr)));
  750. CHK(memcmp(buf, result_pr, sizeof(result_pr)));
  751. mbedtls_ctr_drbg_free(&ctx);
  752. if (verbose != 0) {
  753. mbedtls_printf("passed\n");
  754. }
  755. /*
  756. * Based on a NIST CTR_DRBG test vector (PR = FALSE)
  757. */
  758. if (verbose != 0) {
  759. mbedtls_printf(" CTR_DRBG (PR = FALSE): ");
  760. }
  761. mbedtls_ctr_drbg_init(&ctx);
  762. test_offset = 0;
  763. mbedtls_ctr_drbg_set_entropy_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE);
  764. mbedtls_ctr_drbg_set_nonce_len(&ctx, MBEDTLS_CTR_DRBG_KEYSIZE / 2);
  765. CHK(mbedtls_ctr_drbg_seed(&ctx,
  766. ctr_drbg_self_test_entropy,
  767. (void *) entropy_source_nopr,
  768. pers_nopr, MBEDTLS_CTR_DRBG_KEYSIZE));
  769. CHK(mbedtls_ctr_drbg_reseed(&ctx, NULL, 0));
  770. CHK(mbedtls_ctr_drbg_random(&ctx, buf, SELF_TEST_OUTPUT_DISCARD_LENGTH));
  771. CHK(mbedtls_ctr_drbg_random(&ctx, buf, sizeof(result_nopr)));
  772. CHK(memcmp(buf, result_nopr, sizeof(result_nopr)));
  773. mbedtls_ctr_drbg_free(&ctx);
  774. if (verbose != 0) {
  775. mbedtls_printf("passed\n");
  776. }
  777. if (verbose != 0) {
  778. mbedtls_printf("\n");
  779. }
  780. return 0;
  781. }
  782. #endif /* MBEDTLS_SELF_TEST */
  783. #endif /* MBEDTLS_CTR_DRBG_C */