ctr_drbg.c 28 KB

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