ctr_drbg.c 23 KB

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