ctr_drbg.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*
  2. * CTR_DRBG implementation based on AES-256 (NIST SP 800-90)
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * The NIST SP 800-90 DRBGs are described in the following publication.
  23. *
  24. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  25. */
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "mbedtls/config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #if defined(MBEDTLS_CTR_DRBG_C)
  32. #include "mbedtls/ctr_drbg.h"
  33. #include "mbedtls/platform_util.h"
  34. #include <string.h>
  35. #if defined(MBEDTLS_FS_IO)
  36. #include <stdio.h>
  37. #endif
  38. #if defined(MBEDTLS_SELF_TEST)
  39. #if defined(MBEDTLS_PLATFORM_C)
  40. #include "mbedtls/platform.h"
  41. #else
  42. #include <stdio.h>
  43. #define mbedtls_printf printf
  44. #endif /* MBEDTLS_PLATFORM_C */
  45. #endif /* MBEDTLS_SELF_TEST */
  46. /*
  47. * CTR_DRBG context initialization
  48. */
  49. void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
  50. {
  51. memset( ctx, 0, sizeof( mbedtls_ctr_drbg_context ) );
  52. #if defined(MBEDTLS_THREADING_C)
  53. mbedtls_mutex_init( &ctx->mutex );
  54. #endif
  55. }
  56. void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx )
  57. {
  58. if( ctx == NULL )
  59. return;
  60. #if defined(MBEDTLS_THREADING_C)
  61. mbedtls_mutex_free( &ctx->mutex );
  62. #endif
  63. mbedtls_aes_free( &ctx->aes_ctx );
  64. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_ctr_drbg_context ) );
  65. }
  66. void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, int resistance )
  67. {
  68. ctx->prediction_resistance = resistance;
  69. }
  70. void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, size_t len )
  71. {
  72. ctx->entropy_len = len;
  73. }
  74. void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int interval )
  75. {
  76. ctx->reseed_interval = interval;
  77. }
  78. static int block_cipher_df( unsigned char *output,
  79. const unsigned char *data, size_t data_len )
  80. {
  81. unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
  82. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  83. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  84. unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  85. unsigned char *p, *iv;
  86. mbedtls_aes_context aes_ctx;
  87. int ret = 0;
  88. int i, j;
  89. size_t buf_len, use_len;
  90. if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
  91. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  92. memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 );
  93. mbedtls_aes_init( &aes_ctx );
  94. /*
  95. * Construct IV (16 bytes) and S in buffer
  96. * IV = Counter (in 32-bits) padded to 16 with zeroes
  97. * S = Length input string (in 32-bits) || Length of output (in 32-bits) ||
  98. * data || 0x80
  99. * (Total is padded to a multiple of 16-bytes with zeroes)
  100. */
  101. p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE;
  102. *p++ = ( data_len >> 24 ) & 0xff;
  103. *p++ = ( data_len >> 16 ) & 0xff;
  104. *p++ = ( data_len >> 8 ) & 0xff;
  105. *p++ = ( data_len ) & 0xff;
  106. p += 3;
  107. *p++ = MBEDTLS_CTR_DRBG_SEEDLEN;
  108. memcpy( p, data, data_len );
  109. p[data_len] = 0x80;
  110. buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1;
  111. for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ )
  112. key[i] = i;
  113. if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  114. {
  115. goto exit;
  116. }
  117. /*
  118. * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data
  119. */
  120. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  121. {
  122. p = buf;
  123. memset( chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  124. use_len = buf_len;
  125. while( use_len > 0 )
  126. {
  127. for( i = 0; i < MBEDTLS_CTR_DRBG_BLOCKSIZE; i++ )
  128. chain[i] ^= p[i];
  129. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  130. use_len -= ( use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE ) ?
  131. MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len;
  132. if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, chain, chain ) ) != 0 )
  133. {
  134. goto exit;
  135. }
  136. }
  137. memcpy( tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  138. /*
  139. * Update IV
  140. */
  141. buf[3]++;
  142. }
  143. /*
  144. * Do final encryption with reduced data
  145. */
  146. if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  147. {
  148. goto exit;
  149. }
  150. iv = tmp + MBEDTLS_CTR_DRBG_KEYSIZE;
  151. p = output;
  152. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  153. {
  154. if( ( ret = mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, iv, iv ) ) != 0 )
  155. {
  156. goto exit;
  157. }
  158. memcpy( p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  159. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  160. }
  161. exit:
  162. mbedtls_aes_free( &aes_ctx );
  163. /*
  164. * tidy up the stack
  165. */
  166. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  167. mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
  168. mbedtls_platform_zeroize( key, sizeof( key ) );
  169. mbedtls_platform_zeroize( chain, sizeof( chain ) );
  170. if( 0 != ret )
  171. {
  172. /*
  173. * wipe partial seed from memory
  174. */
  175. mbedtls_platform_zeroize( output, MBEDTLS_CTR_DRBG_SEEDLEN );
  176. }
  177. return( ret );
  178. }
  179. /* CTR_DRBG_Update (SP 800-90A &sect;10.2.1.2)
  180. * ctr_drbg_update_internal(ctx, provided_data)
  181. * implements
  182. * CTR_DRBG_Update(provided_data, Key, V)
  183. * with inputs and outputs
  184. * ctx->aes_ctx = Key
  185. * ctx->counter = V
  186. */
  187. static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx,
  188. const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] )
  189. {
  190. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  191. unsigned char *p = tmp;
  192. int i, j;
  193. int ret = 0;
  194. memset( tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
  195. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  196. {
  197. /*
  198. * Increase counter
  199. */
  200. for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
  201. if( ++ctx->counter[i - 1] != 0 )
  202. break;
  203. /*
  204. * Crypt counter block
  205. */
  206. if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, p ) ) != 0 )
  207. goto exit;
  208. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  209. }
  210. for( i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++ )
  211. tmp[i] ^= data[i];
  212. /*
  213. * Update key and counter
  214. */
  215. if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  216. goto exit;
  217. memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  218. exit:
  219. mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
  220. return( ret );
  221. }
  222. /* CTR_DRBG_Instantiate with derivation function (SP 800-90A &sect;10.2.1.3.2)
  223. * mbedtls_ctr_drbg_update(ctx, additional, add_len)
  224. * implements
  225. * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
  226. * security_strength) -> initial_working_state
  227. * with inputs
  228. * ctx->counter = all-bits-0
  229. * ctx->aes_ctx = context from all-bits-0 key
  230. * additional[:add_len] = entropy_input || nonce || personalization_string
  231. * and with outputs
  232. * ctx = initial_working_state
  233. */
  234. int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
  235. const unsigned char *additional,
  236. size_t add_len )
  237. {
  238. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  239. int ret;
  240. if( add_len == 0 )
  241. return( 0 );
  242. if( ( ret = block_cipher_df( add_input, additional, add_len ) ) != 0 )
  243. goto exit;
  244. if( ( ret = ctr_drbg_update_internal( ctx, add_input ) ) != 0 )
  245. goto exit;
  246. exit:
  247. mbedtls_platform_zeroize( add_input, sizeof( add_input ) );
  248. return( ret );
  249. }
  250. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  251. void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  252. const unsigned char *additional,
  253. size_t add_len )
  254. {
  255. /* MAX_INPUT would be more logical here, but we have to match
  256. * block_cipher_df()'s limits since we can't propagate errors */
  257. if( add_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
  258. add_len = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT;
  259. (void) mbedtls_ctr_drbg_update_ret( ctx, additional, add_len );
  260. }
  261. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  262. /* CTR_DRBG_Reseed with derivation function (SP 800-90A &sect;10.2.1.4.2)
  263. * mbedtls_ctr_drbg_reseed(ctx, additional, len)
  264. * implements
  265. * CTR_DRBG_Reseed(working_state, entropy_input, additional_input)
  266. * -> new_working_state
  267. * with inputs
  268. * ctx contains working_state
  269. * additional[:len] = additional_input
  270. * and entropy_input comes from calling ctx->f_entropy
  271. * and with output
  272. * ctx contains new_working_state
  273. */
  274. int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  275. const unsigned char *additional, size_t len )
  276. {
  277. unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
  278. size_t seedlen = 0;
  279. int ret;
  280. if( ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ||
  281. len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len )
  282. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  283. memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT );
  284. /*
  285. * Gather entropy_len bytes of entropy to seed state
  286. */
  287. if( 0 != ctx->f_entropy( ctx->p_entropy, seed,
  288. ctx->entropy_len ) )
  289. {
  290. return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
  291. }
  292. seedlen += ctx->entropy_len;
  293. /*
  294. * Add additional data
  295. */
  296. if( additional && len )
  297. {
  298. memcpy( seed + seedlen, additional, len );
  299. seedlen += len;
  300. }
  301. /*
  302. * Reduce to 384 bits
  303. */
  304. if( ( ret = block_cipher_df( seed, seed, seedlen ) ) != 0 )
  305. goto exit;
  306. /*
  307. * Update state
  308. */
  309. if( ( ret = ctr_drbg_update_internal( ctx, seed ) ) != 0 )
  310. goto exit;
  311. ctx->reseed_counter = 1;
  312. exit:
  313. mbedtls_platform_zeroize( seed, sizeof( seed ) );
  314. return( ret );
  315. }
  316. /* CTR_DRBG_Instantiate with derivation function (SP 800-90A &sect;10.2.1.3.2)
  317. * mbedtls_ctr_drbg_seed(ctx, f_entropy, p_entropy, custom, len)
  318. * implements
  319. * CTR_DRBG_Instantiate(entropy_input, nonce, personalization_string,
  320. * security_strength) -> initial_working_state
  321. * with inputs
  322. * custom[:len] = nonce || personalization_string
  323. * where entropy_input comes from f_entropy for ctx->entropy_len bytes
  324. * and with outputs
  325. * ctx = initial_working_state
  326. */
  327. int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
  328. int (*f_entropy)(void *, unsigned char *, size_t),
  329. void *p_entropy,
  330. const unsigned char *custom,
  331. size_t len )
  332. {
  333. int ret;
  334. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  335. memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
  336. mbedtls_aes_init( &ctx->aes_ctx );
  337. ctx->f_entropy = f_entropy;
  338. ctx->p_entropy = p_entropy;
  339. if( ctx->entropy_len == 0 )
  340. ctx->entropy_len = MBEDTLS_CTR_DRBG_ENTROPY_LEN;
  341. ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
  342. /*
  343. * Initialize with an empty key
  344. */
  345. if( ( ret = mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 )
  346. {
  347. return( ret );
  348. }
  349. if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
  350. {
  351. return( ret );
  352. }
  353. return( 0 );
  354. }
  355. /* Backward compatibility wrapper */
  356. int mbedtls_ctr_drbg_seed_entropy_len(
  357. mbedtls_ctr_drbg_context *ctx,
  358. int (*f_entropy)(void *, unsigned char *, size_t), void *p_entropy,
  359. const unsigned char *custom, size_t len,
  360. size_t entropy_len )
  361. {
  362. mbedtls_ctr_drbg_set_entropy_len( ctx, entropy_len );
  363. return( mbedtls_ctr_drbg_seed( ctx, f_entropy, p_entropy, custom, len ) );
  364. }
  365. /* CTR_DRBG_Generate with derivation function (SP 800-90A &sect;10.2.1.5.2)
  366. * mbedtls_ctr_drbg_random_with_add(ctx, output, output_len, additional, add_len)
  367. * implements
  368. * CTR_DRBG_Reseed(working_state, entropy_input, additional[:add_len])
  369. * -> working_state_after_reseed
  370. * if required, then
  371. * CTR_DRBG_Generate(working_state_after_reseed,
  372. * requested_number_of_bits, additional_input)
  373. * -> status, returned_bits, new_working_state
  374. * with inputs
  375. * ctx contains working_state
  376. * requested_number_of_bits = 8 * output_len
  377. * additional[:add_len] = additional_input
  378. * and entropy_input comes from calling ctx->f_entropy
  379. * and with outputs
  380. * status = SUCCESS (this function does the reseed internally)
  381. * returned_bits = output[:output_len]
  382. * ctx contains new_working_state
  383. */
  384. int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  385. unsigned char *output, size_t output_len,
  386. const unsigned char *additional, size_t add_len )
  387. {
  388. int ret = 0;
  389. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  390. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  391. unsigned char *p = output;
  392. unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  393. int i;
  394. size_t use_len;
  395. if( output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST )
  396. return( MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
  397. if( add_len > MBEDTLS_CTR_DRBG_MAX_INPUT )
  398. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  399. memset( add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
  400. if( ctx->reseed_counter > ctx->reseed_interval ||
  401. ctx->prediction_resistance )
  402. {
  403. if( ( ret = mbedtls_ctr_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  404. {
  405. return( ret );
  406. }
  407. add_len = 0;
  408. }
  409. if( add_len > 0 )
  410. {
  411. if( ( ret = block_cipher_df( add_input, additional, add_len ) ) != 0 )
  412. goto exit;
  413. if( ( ret = ctr_drbg_update_internal( ctx, add_input ) ) != 0 )
  414. goto exit;
  415. }
  416. while( output_len > 0 )
  417. {
  418. /*
  419. * Increase counter
  420. */
  421. for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
  422. if( ++ctx->counter[i - 1] != 0 )
  423. break;
  424. /*
  425. * Crypt counter block
  426. */
  427. if( ( ret = mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, tmp ) ) != 0 )
  428. goto exit;
  429. use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? MBEDTLS_CTR_DRBG_BLOCKSIZE :
  430. output_len;
  431. /*
  432. * Copy random block to destination
  433. */
  434. memcpy( p, tmp, use_len );
  435. p += use_len;
  436. output_len -= use_len;
  437. }
  438. if( ( ret = ctr_drbg_update_internal( ctx, add_input ) ) != 0 )
  439. goto exit;
  440. ctx->reseed_counter++;
  441. exit:
  442. mbedtls_platform_zeroize( add_input, sizeof( add_input ) );
  443. mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
  444. return( ret );
  445. }
  446. int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
  447. {
  448. int ret;
  449. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  450. #if defined(MBEDTLS_THREADING_C)
  451. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  452. return( ret );
  453. #endif
  454. ret = mbedtls_ctr_drbg_random_with_add( ctx, output, output_len, NULL, 0 );
  455. #if defined(MBEDTLS_THREADING_C)
  456. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  457. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  458. #endif
  459. return( ret );
  460. }
  461. #if defined(MBEDTLS_FS_IO)
  462. int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
  463. {
  464. int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  465. FILE *f;
  466. unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
  467. if( ( f = fopen( path, "wb" ) ) == NULL )
  468. return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
  469. if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 )
  470. goto exit;
  471. if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != MBEDTLS_CTR_DRBG_MAX_INPUT )
  472. ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  473. else
  474. ret = 0;
  475. exit:
  476. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  477. fclose( f );
  478. return( ret );
  479. }
  480. int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
  481. {
  482. int ret = 0;
  483. FILE *f = NULL;
  484. size_t n;
  485. unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
  486. unsigned char c;
  487. if( ( f = fopen( path, "rb" ) ) == NULL )
  488. return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
  489. n = fread( buf, 1, sizeof( buf ), f );
  490. if( fread( &c, 1, 1, f ) != 0 )
  491. {
  492. ret = MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG;
  493. goto exit;
  494. }
  495. if( n == 0 || ferror( f ) )
  496. {
  497. ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  498. goto exit;
  499. }
  500. fclose( f );
  501. f = NULL;
  502. ret = mbedtls_ctr_drbg_update_ret( ctx, buf, n );
  503. exit:
  504. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  505. if( f != NULL )
  506. fclose( f );
  507. if( ret != 0 )
  508. return( ret );
  509. return( mbedtls_ctr_drbg_write_seed_file( ctx, path ) );
  510. }
  511. #endif /* MBEDTLS_FS_IO */
  512. #if defined(MBEDTLS_SELF_TEST)
  513. static const unsigned char entropy_source_pr[96] =
  514. { 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16,
  515. 0x19, 0xb3, 0xf1, 0x80, 0xb1, 0xc9, 0x20, 0x02,
  516. 0x6a, 0x54, 0x6f, 0x0c, 0x70, 0x81, 0x49, 0x8b,
  517. 0x6e, 0xa6, 0x62, 0x52, 0x6d, 0x51, 0xb1, 0xcb,
  518. 0x58, 0x3b, 0xfa, 0xd5, 0x37, 0x5f, 0xfb, 0xc9,
  519. 0xff, 0x46, 0xd2, 0x19, 0xc7, 0x22, 0x3e, 0x95,
  520. 0x45, 0x9d, 0x82, 0xe1, 0xe7, 0x22, 0x9f, 0x63,
  521. 0x31, 0x69, 0xd2, 0x6b, 0x57, 0x47, 0x4f, 0xa3,
  522. 0x37, 0xc9, 0x98, 0x1c, 0x0b, 0xfb, 0x91, 0x31,
  523. 0x4d, 0x55, 0xb9, 0xe9, 0x1c, 0x5a, 0x5e, 0xe4,
  524. 0x93, 0x92, 0xcf, 0xc5, 0x23, 0x12, 0xd5, 0x56,
  525. 0x2c, 0x4a, 0x6e, 0xff, 0xdc, 0x10, 0xd0, 0x68 };
  526. static const unsigned char entropy_source_nopr[64] =
  527. { 0x5a, 0x19, 0x4d, 0x5e, 0x2b, 0x31, 0x58, 0x14,
  528. 0x54, 0xde, 0xf6, 0x75, 0xfb, 0x79, 0x58, 0xfe,
  529. 0xc7, 0xdb, 0x87, 0x3e, 0x56, 0x89, 0xfc, 0x9d,
  530. 0x03, 0x21, 0x7c, 0x68, 0xd8, 0x03, 0x38, 0x20,
  531. 0xf9, 0xe6, 0x5e, 0x04, 0xd8, 0x56, 0xf3, 0xa9,
  532. 0xc4, 0x4a, 0x4c, 0xbd, 0xc1, 0xd0, 0x08, 0x46,
  533. 0xf5, 0x98, 0x3d, 0x77, 0x1c, 0x1b, 0x13, 0x7e,
  534. 0x4e, 0x0f, 0x9d, 0x8e, 0xf4, 0x09, 0xf9, 0x2e };
  535. static const unsigned char nonce_pers_pr[16] =
  536. { 0xd2, 0x54, 0xfc, 0xff, 0x02, 0x1e, 0x69, 0xd2,
  537. 0x29, 0xc9, 0xcf, 0xad, 0x85, 0xfa, 0x48, 0x6c };
  538. static const unsigned char nonce_pers_nopr[16] =
  539. { 0x1b, 0x54, 0xb8, 0xff, 0x06, 0x42, 0xbf, 0xf5,
  540. 0x21, 0xf1, 0x5c, 0x1c, 0x0b, 0x66, 0x5f, 0x3f };
  541. static const unsigned char result_pr[16] =
  542. { 0x34, 0x01, 0x16, 0x56, 0xb4, 0x29, 0x00, 0x8f,
  543. 0x35, 0x63, 0xec, 0xb5, 0xf2, 0x59, 0x07, 0x23 };
  544. static const unsigned char result_nopr[16] =
  545. { 0xa0, 0x54, 0x30, 0x3d, 0x8a, 0x7e, 0xa9, 0x88,
  546. 0x9d, 0x90, 0x3e, 0x07, 0x7c, 0x6f, 0x21, 0x8f };
  547. static size_t test_offset;
  548. static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf,
  549. size_t len )
  550. {
  551. const unsigned char *p = data;
  552. memcpy( buf, p + test_offset, len );
  553. test_offset += len;
  554. return( 0 );
  555. }
  556. #define CHK( c ) if( (c) != 0 ) \
  557. { \
  558. if( verbose != 0 ) \
  559. mbedtls_printf( "failed\n" ); \
  560. return( 1 ); \
  561. }
  562. /*
  563. * Checkup routine
  564. */
  565. int mbedtls_ctr_drbg_self_test( int verbose )
  566. {
  567. mbedtls_ctr_drbg_context ctx;
  568. unsigned char buf[16];
  569. mbedtls_ctr_drbg_init( &ctx );
  570. /*
  571. * Based on a NIST CTR_DRBG test vector (PR = True)
  572. */
  573. if( verbose != 0 )
  574. mbedtls_printf( " CTR_DRBG (PR = TRUE) : " );
  575. test_offset = 0;
  576. mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
  577. CHK( mbedtls_ctr_drbg_seed( &ctx,
  578. ctr_drbg_self_test_entropy,
  579. (void *) entropy_source_pr,
  580. nonce_pers_pr, 16 ) );
  581. mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
  582. CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  583. CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  584. CHK( memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  585. mbedtls_ctr_drbg_free( &ctx );
  586. if( verbose != 0 )
  587. mbedtls_printf( "passed\n" );
  588. /*
  589. * Based on a NIST CTR_DRBG test vector (PR = FALSE)
  590. */
  591. if( verbose != 0 )
  592. mbedtls_printf( " CTR_DRBG (PR = FALSE): " );
  593. mbedtls_ctr_drbg_init( &ctx );
  594. test_offset = 0;
  595. mbedtls_ctr_drbg_set_entropy_len( &ctx, 32 );
  596. CHK( mbedtls_ctr_drbg_seed( &ctx,
  597. ctr_drbg_self_test_entropy,
  598. (void *) entropy_source_nopr,
  599. nonce_pers_nopr, 16 ) );
  600. CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
  601. CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
  602. CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
  603. CHK( memcmp( buf, result_nopr, 16 ) );
  604. mbedtls_ctr_drbg_free( &ctx );
  605. if( verbose != 0 )
  606. mbedtls_printf( "passed\n" );
  607. if( verbose != 0 )
  608. mbedtls_printf( "\n" );
  609. return( 0 );
  610. }
  611. #endif /* MBEDTLS_SELF_TEST */
  612. #endif /* MBEDTLS_CTR_DRBG_C */