ctr_drbg.c 30 KB

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