ctr_drbg.c 16 KB

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