ctr_drbg.c 29 KB

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