ctr_drbg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. * CTR_DRBG implementation based on AES-256 (NIST SP 800-90)
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * The NIST SP 800-90 DRBGs are described in the following publucation.
  25. *
  26. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  27. */
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "mbedtls/config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #if defined(MBEDTLS_CTR_DRBG_C)
  34. #include "mbedtls/ctr_drbg.h"
  35. #include <string.h>
  36. #if defined(MBEDTLS_FS_IO)
  37. #include <stdio.h>
  38. #endif
  39. #if defined(MBEDTLS_SELF_TEST)
  40. #if defined(MBEDTLS_PLATFORM_C)
  41. #include "mbedtls/platform.h"
  42. #else
  43. #include <stdio.h>
  44. #define mbedtls_printf printf
  45. #endif /* MBEDTLS_PLATFORM_C */
  46. #endif /* MBEDTLS_SELF_TEST */
  47. /* Implementation that should never be optimized out by the compiler */
  48. static void mbedtls_zeroize( void *v, size_t n ) {
  49. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  50. }
  51. /*
  52. * CTR_DRBG context initialization
  53. */
  54. void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
  55. {
  56. memset( ctx, 0, sizeof( mbedtls_ctr_drbg_context ) );
  57. #if defined(MBEDTLS_THREADING_C)
  58. mbedtls_mutex_init( &ctx->mutex );
  59. #endif
  60. }
  61. /*
  62. * Non-public function wrapped by mbedtls_ctr_drbg_seed(). Necessary to allow
  63. * NIST tests to succeed (which require known length fixed entropy)
  64. */
  65. int mbedtls_ctr_drbg_seed_entropy_len(
  66. mbedtls_ctr_drbg_context *ctx,
  67. int (*f_entropy)(void *, unsigned char *, size_t),
  68. void *p_entropy,
  69. const unsigned char *custom,
  70. size_t len,
  71. size_t entropy_len )
  72. {
  73. int ret;
  74. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  75. memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE );
  76. mbedtls_aes_init( &ctx->aes_ctx );
  77. ctx->f_entropy = f_entropy;
  78. ctx->p_entropy = p_entropy;
  79. ctx->entropy_len = entropy_len;
  80. ctx->reseed_interval = MBEDTLS_CTR_DRBG_RESEED_INTERVAL;
  81. /*
  82. * Initialize with an empty key
  83. */
  84. mbedtls_aes_setkey_enc( &ctx->aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS );
  85. if( ( ret = mbedtls_ctr_drbg_reseed( ctx, custom, len ) ) != 0 )
  86. return( ret );
  87. return( 0 );
  88. }
  89. int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
  90. int (*f_entropy)(void *, unsigned char *, size_t),
  91. void *p_entropy,
  92. const unsigned char *custom,
  93. size_t len )
  94. {
  95. return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, custom, len,
  96. MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
  97. }
  98. void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx )
  99. {
  100. if( ctx == NULL )
  101. return;
  102. #if defined(MBEDTLS_THREADING_C)
  103. mbedtls_mutex_free( &ctx->mutex );
  104. #endif
  105. mbedtls_aes_free( &ctx->aes_ctx );
  106. mbedtls_zeroize( ctx, sizeof( mbedtls_ctr_drbg_context ) );
  107. }
  108. void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx, int resistance )
  109. {
  110. ctx->prediction_resistance = resistance;
  111. }
  112. void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx, size_t len )
  113. {
  114. ctx->entropy_len = len;
  115. }
  116. void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx, int interval )
  117. {
  118. ctx->reseed_interval = interval;
  119. }
  120. static int block_cipher_df( unsigned char *output,
  121. const unsigned char *data, size_t data_len )
  122. {
  123. unsigned char buf[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16];
  124. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  125. unsigned char key[MBEDTLS_CTR_DRBG_KEYSIZE];
  126. unsigned char chain[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  127. unsigned char *p, *iv;
  128. mbedtls_aes_context aes_ctx;
  129. int i, j;
  130. size_t buf_len, use_len;
  131. if( data_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
  132. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  133. memset( buf, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + MBEDTLS_CTR_DRBG_BLOCKSIZE + 16 );
  134. mbedtls_aes_init( &aes_ctx );
  135. /*
  136. * Construct IV (16 bytes) and S in buffer
  137. * IV = Counter (in 32-bits) padded to 16 with zeroes
  138. * S = Length input string (in 32-bits) || Length of output (in 32-bits) ||
  139. * data || 0x80
  140. * (Total is padded to a multiple of 16-bytes with zeroes)
  141. */
  142. p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE;
  143. *p++ = ( data_len >> 24 ) & 0xff;
  144. *p++ = ( data_len >> 16 ) & 0xff;
  145. *p++ = ( data_len >> 8 ) & 0xff;
  146. *p++ = ( data_len ) & 0xff;
  147. p += 3;
  148. *p++ = MBEDTLS_CTR_DRBG_SEEDLEN;
  149. memcpy( p, data, data_len );
  150. p[data_len] = 0x80;
  151. buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1;
  152. for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ )
  153. key[i] = i;
  154. mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS );
  155. /*
  156. * Reduce data to MBEDTLS_CTR_DRBG_SEEDLEN bytes of data
  157. */
  158. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  159. {
  160. p = buf;
  161. memset( chain, 0, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  162. use_len = buf_len;
  163. while( use_len > 0 )
  164. {
  165. for( i = 0; i < MBEDTLS_CTR_DRBG_BLOCKSIZE; i++ )
  166. chain[i] ^= p[i];
  167. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  168. use_len -= ( use_len >= MBEDTLS_CTR_DRBG_BLOCKSIZE ) ?
  169. MBEDTLS_CTR_DRBG_BLOCKSIZE : use_len;
  170. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, chain, chain );
  171. }
  172. memcpy( tmp + j, chain, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  173. /*
  174. * Update IV
  175. */
  176. buf[3]++;
  177. }
  178. /*
  179. * Do final encryption with reduced data
  180. */
  181. mbedtls_aes_setkey_enc( &aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS );
  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. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, iv, iv );
  187. memcpy( p, iv, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  188. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  189. }
  190. mbedtls_aes_free( &aes_ctx );
  191. return( 0 );
  192. }
  193. static int ctr_drbg_update_internal( mbedtls_ctr_drbg_context *ctx,
  194. const unsigned char data[MBEDTLS_CTR_DRBG_SEEDLEN] )
  195. {
  196. unsigned char tmp[MBEDTLS_CTR_DRBG_SEEDLEN];
  197. unsigned char *p = tmp;
  198. int i, j;
  199. memset( tmp, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
  200. for( j = 0; j < MBEDTLS_CTR_DRBG_SEEDLEN; j += MBEDTLS_CTR_DRBG_BLOCKSIZE )
  201. {
  202. /*
  203. * Increase counter
  204. */
  205. for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
  206. if( ++ctx->counter[i - 1] != 0 )
  207. break;
  208. /*
  209. * Crypt counter block
  210. */
  211. mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, p );
  212. p += MBEDTLS_CTR_DRBG_BLOCKSIZE;
  213. }
  214. for( i = 0; i < MBEDTLS_CTR_DRBG_SEEDLEN; i++ )
  215. tmp[i] ^= data[i];
  216. /*
  217. * Update key and counter
  218. */
  219. mbedtls_aes_setkey_enc( &ctx->aes_ctx, tmp, MBEDTLS_CTR_DRBG_KEYBITS );
  220. memcpy( ctx->counter, tmp + MBEDTLS_CTR_DRBG_KEYSIZE, MBEDTLS_CTR_DRBG_BLOCKSIZE );
  221. return( 0 );
  222. }
  223. void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
  224. const unsigned char *additional, size_t add_len )
  225. {
  226. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  227. if( add_len > 0 )
  228. {
  229. /* MAX_INPUT would be more logical here, but we have to match
  230. * block_cipher_df()'s limits since we can't propagate errors */
  231. if( add_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT )
  232. add_len = MBEDTLS_CTR_DRBG_MAX_SEED_INPUT;
  233. block_cipher_df( add_input, additional, add_len );
  234. ctr_drbg_update_internal( ctx, add_input );
  235. }
  236. }
  237. int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
  238. const unsigned char *additional, size_t len )
  239. {
  240. unsigned char seed[MBEDTLS_CTR_DRBG_MAX_SEED_INPUT];
  241. size_t seedlen = 0;
  242. if( ctx->entropy_len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT ||
  243. len > MBEDTLS_CTR_DRBG_MAX_SEED_INPUT - ctx->entropy_len )
  244. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  245. memset( seed, 0, MBEDTLS_CTR_DRBG_MAX_SEED_INPUT );
  246. /*
  247. * Gather entropy_len bytes of entropy to seed state
  248. */
  249. if( 0 != ctx->f_entropy( ctx->p_entropy, seed,
  250. ctx->entropy_len ) )
  251. {
  252. return( MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED );
  253. }
  254. seedlen += ctx->entropy_len;
  255. /*
  256. * Add additional data
  257. */
  258. if( additional && len )
  259. {
  260. memcpy( seed + seedlen, additional, len );
  261. seedlen += len;
  262. }
  263. /*
  264. * Reduce to 384 bits
  265. */
  266. block_cipher_df( seed, seed, seedlen );
  267. /*
  268. * Update state
  269. */
  270. ctr_drbg_update_internal( ctx, seed );
  271. ctx->reseed_counter = 1;
  272. return( 0 );
  273. }
  274. int mbedtls_ctr_drbg_random_with_add( void *p_rng,
  275. unsigned char *output, size_t output_len,
  276. const unsigned char *additional, size_t add_len )
  277. {
  278. int ret = 0;
  279. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  280. unsigned char add_input[MBEDTLS_CTR_DRBG_SEEDLEN];
  281. unsigned char *p = output;
  282. unsigned char tmp[MBEDTLS_CTR_DRBG_BLOCKSIZE];
  283. int i;
  284. size_t use_len;
  285. if( output_len > MBEDTLS_CTR_DRBG_MAX_REQUEST )
  286. return( MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
  287. if( add_len > MBEDTLS_CTR_DRBG_MAX_INPUT )
  288. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  289. memset( add_input, 0, MBEDTLS_CTR_DRBG_SEEDLEN );
  290. if( ctx->reseed_counter > ctx->reseed_interval ||
  291. ctx->prediction_resistance )
  292. {
  293. if( ( ret = mbedtls_ctr_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  294. return( ret );
  295. add_len = 0;
  296. }
  297. if( add_len > 0 )
  298. {
  299. block_cipher_df( add_input, additional, add_len );
  300. ctr_drbg_update_internal( ctx, add_input );
  301. }
  302. while( output_len > 0 )
  303. {
  304. /*
  305. * Increase counter
  306. */
  307. for( i = MBEDTLS_CTR_DRBG_BLOCKSIZE; i > 0; i-- )
  308. if( ++ctx->counter[i - 1] != 0 )
  309. break;
  310. /*
  311. * Crypt counter block
  312. */
  313. mbedtls_aes_crypt_ecb( &ctx->aes_ctx, MBEDTLS_AES_ENCRYPT, ctx->counter, tmp );
  314. use_len = ( output_len > MBEDTLS_CTR_DRBG_BLOCKSIZE ) ? MBEDTLS_CTR_DRBG_BLOCKSIZE :
  315. output_len;
  316. /*
  317. * Copy random block to destination
  318. */
  319. memcpy( p, tmp, use_len );
  320. p += use_len;
  321. output_len -= use_len;
  322. }
  323. ctr_drbg_update_internal( ctx, add_input );
  324. ctx->reseed_counter++;
  325. return( 0 );
  326. }
  327. int mbedtls_ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
  328. {
  329. int ret;
  330. mbedtls_ctr_drbg_context *ctx = (mbedtls_ctr_drbg_context *) p_rng;
  331. #if defined(MBEDTLS_THREADING_C)
  332. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  333. return( ret );
  334. #endif
  335. ret = mbedtls_ctr_drbg_random_with_add( ctx, output, output_len, NULL, 0 );
  336. #if defined(MBEDTLS_THREADING_C)
  337. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  338. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  339. #endif
  340. return( ret );
  341. }
  342. #if defined(MBEDTLS_FS_IO)
  343. int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
  344. {
  345. int ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  346. FILE *f;
  347. unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
  348. if( ( f = fopen( path, "wb" ) ) == NULL )
  349. return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
  350. if( ( ret = mbedtls_ctr_drbg_random( ctx, buf, MBEDTLS_CTR_DRBG_MAX_INPUT ) ) != 0 )
  351. goto exit;
  352. if( fwrite( buf, 1, MBEDTLS_CTR_DRBG_MAX_INPUT, f ) != MBEDTLS_CTR_DRBG_MAX_INPUT )
  353. {
  354. ret = MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR;
  355. goto exit;
  356. }
  357. ret = 0;
  358. exit:
  359. fclose( f );
  360. return( ret );
  361. }
  362. int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path )
  363. {
  364. FILE *f;
  365. size_t n;
  366. unsigned char buf[ MBEDTLS_CTR_DRBG_MAX_INPUT ];
  367. if( ( f = fopen( path, "rb" ) ) == NULL )
  368. return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
  369. fseek( f, 0, SEEK_END );
  370. n = (size_t) ftell( f );
  371. fseek( f, 0, SEEK_SET );
  372. if( n > MBEDTLS_CTR_DRBG_MAX_INPUT )
  373. {
  374. fclose( f );
  375. return( MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  376. }
  377. if( fread( buf, 1, n, f ) != n )
  378. {
  379. fclose( f );
  380. return( MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR );
  381. }
  382. fclose( f );
  383. mbedtls_ctr_drbg_update( ctx, buf, n );
  384. return( mbedtls_ctr_drbg_write_seed_file( ctx, path ) );
  385. }
  386. #endif /* MBEDTLS_FS_IO */
  387. #if defined(MBEDTLS_SELF_TEST)
  388. static const unsigned char entropy_source_pr[96] =
  389. { 0xc1, 0x80, 0x81, 0xa6, 0x5d, 0x44, 0x02, 0x16,
  390. 0x19, 0xb3, 0xf1, 0x80, 0xb1, 0xc9, 0x20, 0x02,
  391. 0x6a, 0x54, 0x6f, 0x0c, 0x70, 0x81, 0x49, 0x8b,
  392. 0x6e, 0xa6, 0x62, 0x52, 0x6d, 0x51, 0xb1, 0xcb,
  393. 0x58, 0x3b, 0xfa, 0xd5, 0x37, 0x5f, 0xfb, 0xc9,
  394. 0xff, 0x46, 0xd2, 0x19, 0xc7, 0x22, 0x3e, 0x95,
  395. 0x45, 0x9d, 0x82, 0xe1, 0xe7, 0x22, 0x9f, 0x63,
  396. 0x31, 0x69, 0xd2, 0x6b, 0x57, 0x47, 0x4f, 0xa3,
  397. 0x37, 0xc9, 0x98, 0x1c, 0x0b, 0xfb, 0x91, 0x31,
  398. 0x4d, 0x55, 0xb9, 0xe9, 0x1c, 0x5a, 0x5e, 0xe4,
  399. 0x93, 0x92, 0xcf, 0xc5, 0x23, 0x12, 0xd5, 0x56,
  400. 0x2c, 0x4a, 0x6e, 0xff, 0xdc, 0x10, 0xd0, 0x68 };
  401. static const unsigned char entropy_source_nopr[64] =
  402. { 0x5a, 0x19, 0x4d, 0x5e, 0x2b, 0x31, 0x58, 0x14,
  403. 0x54, 0xde, 0xf6, 0x75, 0xfb, 0x79, 0x58, 0xfe,
  404. 0xc7, 0xdb, 0x87, 0x3e, 0x56, 0x89, 0xfc, 0x9d,
  405. 0x03, 0x21, 0x7c, 0x68, 0xd8, 0x03, 0x38, 0x20,
  406. 0xf9, 0xe6, 0x5e, 0x04, 0xd8, 0x56, 0xf3, 0xa9,
  407. 0xc4, 0x4a, 0x4c, 0xbd, 0xc1, 0xd0, 0x08, 0x46,
  408. 0xf5, 0x98, 0x3d, 0x77, 0x1c, 0x1b, 0x13, 0x7e,
  409. 0x4e, 0x0f, 0x9d, 0x8e, 0xf4, 0x09, 0xf9, 0x2e };
  410. static const unsigned char nonce_pers_pr[16] =
  411. { 0xd2, 0x54, 0xfc, 0xff, 0x02, 0x1e, 0x69, 0xd2,
  412. 0x29, 0xc9, 0xcf, 0xad, 0x85, 0xfa, 0x48, 0x6c };
  413. static const unsigned char nonce_pers_nopr[16] =
  414. { 0x1b, 0x54, 0xb8, 0xff, 0x06, 0x42, 0xbf, 0xf5,
  415. 0x21, 0xf1, 0x5c, 0x1c, 0x0b, 0x66, 0x5f, 0x3f };
  416. static const unsigned char result_pr[16] =
  417. { 0x34, 0x01, 0x16, 0x56, 0xb4, 0x29, 0x00, 0x8f,
  418. 0x35, 0x63, 0xec, 0xb5, 0xf2, 0x59, 0x07, 0x23 };
  419. static const unsigned char result_nopr[16] =
  420. { 0xa0, 0x54, 0x30, 0x3d, 0x8a, 0x7e, 0xa9, 0x88,
  421. 0x9d, 0x90, 0x3e, 0x07, 0x7c, 0x6f, 0x21, 0x8f };
  422. static size_t test_offset;
  423. static int ctr_drbg_self_test_entropy( void *data, unsigned char *buf,
  424. size_t len )
  425. {
  426. const unsigned char *p = data;
  427. memcpy( buf, p + test_offset, len );
  428. test_offset += len;
  429. return( 0 );
  430. }
  431. #define CHK( c ) if( (c) != 0 ) \
  432. { \
  433. if( verbose != 0 ) \
  434. mbedtls_printf( "failed\n" ); \
  435. return( 1 ); \
  436. }
  437. /*
  438. * Checkup routine
  439. */
  440. int mbedtls_ctr_drbg_self_test( int verbose )
  441. {
  442. mbedtls_ctr_drbg_context ctx;
  443. unsigned char buf[16];
  444. mbedtls_ctr_drbg_init( &ctx );
  445. /*
  446. * Based on a NIST CTR_DRBG test vector (PR = True)
  447. */
  448. if( verbose != 0 )
  449. mbedtls_printf( " CTR_DRBG (PR = TRUE) : " );
  450. test_offset = 0;
  451. CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
  452. (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) );
  453. mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
  454. CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  455. CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  456. CHK( memcmp( buf, result_pr, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
  457. mbedtls_ctr_drbg_free( &ctx );
  458. if( verbose != 0 )
  459. mbedtls_printf( "passed\n" );
  460. /*
  461. * Based on a NIST CTR_DRBG test vector (PR = FALSE)
  462. */
  463. if( verbose != 0 )
  464. mbedtls_printf( " CTR_DRBG (PR = FALSE): " );
  465. mbedtls_ctr_drbg_init( &ctx );
  466. test_offset = 0;
  467. CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
  468. (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
  469. CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
  470. CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
  471. CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
  472. CHK( memcmp( buf, result_nopr, 16 ) );
  473. mbedtls_ctr_drbg_free( &ctx );
  474. if( verbose != 0 )
  475. mbedtls_printf( "passed\n" );
  476. if( verbose != 0 )
  477. mbedtls_printf( "\n" );
  478. return( 0 );
  479. }
  480. #endif /* MBEDTLS_SELF_TEST */
  481. #endif /* MBEDTLS_CTR_DRBG_C */