ctr_drbg.c 32 KB

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