hmac_drbg.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * HMAC_DRBG implementation (NIST SP 800-90)
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /*
  22. * The NIST SP 800-90A DRBGs are described in the following publication.
  23. * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
  24. * References below are based on rev. 1 (January 2012).
  25. */
  26. #if !defined(MBEDTLS_CONFIG_FILE)
  27. #include "mbedtls/config.h"
  28. #else
  29. #include MBEDTLS_CONFIG_FILE
  30. #endif
  31. #if defined(MBEDTLS_HMAC_DRBG_C)
  32. #include "mbedtls/hmac_drbg.h"
  33. #include <string.h>
  34. #if defined(MBEDTLS_FS_IO)
  35. #include <stdio.h>
  36. #endif
  37. #if defined(MBEDTLS_SELF_TEST)
  38. #if defined(MBEDTLS_PLATFORM_C)
  39. #include "mbedtls/platform.h"
  40. #else
  41. #include <stdio.h>
  42. #define mbedtls_printf printf
  43. #endif /* MBEDTLS_SELF_TEST */
  44. #endif /* MBEDTLS_PLATFORM_C */
  45. /* Implementation that should never be optimized out by the compiler */
  46. static void mbedtls_zeroize( void *v, size_t n ) {
  47. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  48. }
  49. /*
  50. * HMAC_DRBG context initialization
  51. */
  52. void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx )
  53. {
  54. memset( ctx, 0, sizeof( mbedtls_hmac_drbg_context ) );
  55. #if defined(MBEDTLS_THREADING_C)
  56. mbedtls_mutex_init( &ctx->mutex );
  57. #endif
  58. }
  59. /*
  60. * HMAC_DRBG update, using optional additional data (10.1.2.2)
  61. */
  62. int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx,
  63. const unsigned char *additional,
  64. size_t add_len )
  65. {
  66. size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  67. unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
  68. unsigned char sep[1];
  69. unsigned char K[MBEDTLS_MD_MAX_SIZE];
  70. int ret;
  71. for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
  72. {
  73. /* Step 1 or 4 */
  74. if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 )
  75. goto exit;
  76. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  77. ctx->V, md_len ) ) != 0 )
  78. goto exit;
  79. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  80. sep, 1 ) ) != 0 )
  81. goto exit;
  82. if( rounds == 2 )
  83. {
  84. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  85. additional, add_len ) ) != 0 )
  86. goto exit;
  87. }
  88. if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, K ) ) != 0 )
  89. goto exit;
  90. /* Step 2 or 5 */
  91. if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len ) ) != 0 )
  92. goto exit;
  93. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  94. ctx->V, md_len ) ) != 0 )
  95. goto exit;
  96. if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 )
  97. goto exit;
  98. }
  99. exit:
  100. mbedtls_zeroize( K, sizeof( K ) );
  101. return( ret );
  102. }
  103. void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
  104. const unsigned char *additional,
  105. size_t add_len )
  106. {
  107. (void) mbedtls_hmac_drbg_update_ret( ctx, additional, add_len );
  108. }
  109. /*
  110. * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
  111. */
  112. int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
  113. const mbedtls_md_info_t * md_info,
  114. const unsigned char *data, size_t data_len )
  115. {
  116. int ret;
  117. if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  118. return( ret );
  119. /*
  120. * Set initial working state.
  121. * Use the V memory location, which is currently all 0, to initialize the
  122. * MD context with an all-zero key. Then set V to its initial value.
  123. */
  124. if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V,
  125. mbedtls_md_get_size( md_info ) ) ) != 0 )
  126. return( ret );
  127. memset( ctx->V, 0x01, mbedtls_md_get_size( md_info ) );
  128. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, data, data_len ) ) != 0 )
  129. return( ret );
  130. return( 0 );
  131. }
  132. /*
  133. * HMAC_DRBG reseeding: 10.1.2.4 (arabic) + 9.2 (Roman)
  134. */
  135. int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
  136. const unsigned char *additional, size_t len )
  137. {
  138. unsigned char seed[MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT];
  139. size_t seedlen;
  140. int ret;
  141. /* III. Check input length */
  142. if( len > MBEDTLS_HMAC_DRBG_MAX_INPUT ||
  143. ctx->entropy_len + len > MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT )
  144. {
  145. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  146. }
  147. memset( seed, 0, MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT );
  148. /* IV. Gather entropy_len bytes of entropy for the seed */
  149. if( ( ret = ctx->f_entropy( ctx->p_entropy,
  150. seed, ctx->entropy_len ) ) != 0 )
  151. return( MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
  152. seedlen = ctx->entropy_len;
  153. /* 1. Concatenate entropy and additional data if any */
  154. if( additional != NULL && len != 0 )
  155. {
  156. memcpy( seed + seedlen, additional, len );
  157. seedlen += len;
  158. }
  159. /* 2. Update state */
  160. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, seed, seedlen ) ) != 0 )
  161. goto exit;
  162. /* 3. Reset reseed_counter */
  163. ctx->reseed_counter = 1;
  164. exit:
  165. /* 4. Done */
  166. mbedtls_zeroize( seed, seedlen );
  167. return( ret );
  168. }
  169. /*
  170. * HMAC_DRBG initialisation (10.1.2.3 + 9.1)
  171. */
  172. int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
  173. const mbedtls_md_info_t * md_info,
  174. int (*f_entropy)(void *, unsigned char *, size_t),
  175. void *p_entropy,
  176. const unsigned char *custom,
  177. size_t len )
  178. {
  179. int ret;
  180. size_t entropy_len, md_size;
  181. if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  182. return( ret );
  183. md_size = mbedtls_md_get_size( md_info );
  184. /*
  185. * Set initial working state.
  186. * Use the V memory location, which is currently all 0, to initialize the
  187. * MD context with an all-zero key. Then set V to its initial value.
  188. */
  189. if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V, md_size ) ) != 0 )
  190. return( ret );
  191. memset( ctx->V, 0x01, md_size );
  192. ctx->f_entropy = f_entropy;
  193. ctx->p_entropy = p_entropy;
  194. ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
  195. /*
  196. * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
  197. * each hash function, then according to SP800-90A rev1 10.1 table 2,
  198. * min_entropy_len (in bits) is security_strength.
  199. *
  200. * (This also matches the sizes used in the NIST test vectors.)
  201. */
  202. entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
  203. md_size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
  204. 32; /* better (256+) -> 256 bits */
  205. /*
  206. * For initialisation, use more entropy to emulate a nonce
  207. * (Again, matches test vectors.)
  208. */
  209. ctx->entropy_len = entropy_len * 3 / 2;
  210. if( ( ret = mbedtls_hmac_drbg_reseed( ctx, custom, len ) ) != 0 )
  211. return( ret );
  212. ctx->entropy_len = entropy_len;
  213. return( 0 );
  214. }
  215. /*
  216. * Set prediction resistance
  217. */
  218. void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
  219. int resistance )
  220. {
  221. ctx->prediction_resistance = resistance;
  222. }
  223. /*
  224. * Set entropy length grabbed for reseeds
  225. */
  226. void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len )
  227. {
  228. ctx->entropy_len = len;
  229. }
  230. /*
  231. * Set reseed interval
  232. */
  233. void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx, int interval )
  234. {
  235. ctx->reseed_interval = interval;
  236. }
  237. /*
  238. * HMAC_DRBG random function with optional additional data:
  239. * 10.1.2.5 (arabic) + 9.3 (Roman)
  240. */
  241. int mbedtls_hmac_drbg_random_with_add( void *p_rng,
  242. unsigned char *output, size_t out_len,
  243. const unsigned char *additional, size_t add_len )
  244. {
  245. int ret;
  246. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  247. size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  248. size_t left = out_len;
  249. unsigned char *out = output;
  250. /* II. Check request length */
  251. if( out_len > MBEDTLS_HMAC_DRBG_MAX_REQUEST )
  252. return( MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG );
  253. /* III. Check input length */
  254. if( add_len > MBEDTLS_HMAC_DRBG_MAX_INPUT )
  255. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  256. /* 1. (aka VII and IX) Check reseed counter and PR */
  257. if( ctx->f_entropy != NULL && /* For no-reseeding instances */
  258. ( ctx->prediction_resistance == MBEDTLS_HMAC_DRBG_PR_ON ||
  259. ctx->reseed_counter > ctx->reseed_interval ) )
  260. {
  261. if( ( ret = mbedtls_hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  262. return( ret );
  263. add_len = 0; /* VII.4 */
  264. }
  265. /* 2. Use additional data if any */
  266. if( additional != NULL && add_len != 0 )
  267. {
  268. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx,
  269. additional, add_len ) ) != 0 )
  270. goto exit;
  271. }
  272. /* 3, 4, 5. Generate bytes */
  273. while( left != 0 )
  274. {
  275. size_t use_len = left > md_len ? md_len : left;
  276. if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 )
  277. goto exit;
  278. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  279. ctx->V, md_len ) ) != 0 )
  280. goto exit;
  281. if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 )
  282. goto exit;
  283. memcpy( out, ctx->V, use_len );
  284. out += use_len;
  285. left -= use_len;
  286. }
  287. /* 6. Update */
  288. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx,
  289. additional, add_len ) ) != 0 )
  290. goto exit;
  291. /* 7. Update reseed counter */
  292. ctx->reseed_counter++;
  293. exit:
  294. /* 8. Done */
  295. return( ret );
  296. }
  297. /*
  298. * HMAC_DRBG random function
  299. */
  300. int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
  301. {
  302. int ret;
  303. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  304. #if defined(MBEDTLS_THREADING_C)
  305. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  306. return( ret );
  307. #endif
  308. ret = mbedtls_hmac_drbg_random_with_add( ctx, output, out_len, NULL, 0 );
  309. #if defined(MBEDTLS_THREADING_C)
  310. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  311. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  312. #endif
  313. return( ret );
  314. }
  315. /*
  316. * Free an HMAC_DRBG context
  317. */
  318. void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx )
  319. {
  320. if( ctx == NULL )
  321. return;
  322. #if defined(MBEDTLS_THREADING_C)
  323. mbedtls_mutex_free( &ctx->mutex );
  324. #endif
  325. mbedtls_md_free( &ctx->md_ctx );
  326. mbedtls_zeroize( ctx, sizeof( mbedtls_hmac_drbg_context ) );
  327. }
  328. #if defined(MBEDTLS_FS_IO)
  329. int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  330. {
  331. int ret;
  332. FILE *f;
  333. unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  334. if( ( f = fopen( path, "wb" ) ) == NULL )
  335. return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  336. if( ( ret = mbedtls_hmac_drbg_random( ctx, buf, sizeof( buf ) ) ) != 0 )
  337. goto exit;
  338. if( fwrite( buf, 1, sizeof( buf ), f ) != sizeof( buf ) )
  339. {
  340. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  341. goto exit;
  342. }
  343. ret = 0;
  344. exit:
  345. fclose( f );
  346. mbedtls_zeroize( buf, sizeof( buf ) );
  347. return( ret );
  348. }
  349. int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  350. {
  351. int ret = 0;
  352. FILE *f;
  353. size_t n;
  354. unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  355. if( ( f = fopen( path, "rb" ) ) == NULL )
  356. return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  357. fseek( f, 0, SEEK_END );
  358. n = (size_t) ftell( f );
  359. fseek( f, 0, SEEK_SET );
  360. if( n > MBEDTLS_HMAC_DRBG_MAX_INPUT )
  361. {
  362. fclose( f );
  363. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  364. }
  365. if( fread( buf, 1, n, f ) != n )
  366. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  367. else
  368. ret = mbedtls_hmac_drbg_update_ret( ctx, buf, n );
  369. fclose( f );
  370. mbedtls_zeroize( buf, sizeof( buf ) );
  371. if( ret != 0 )
  372. return( ret );
  373. return( mbedtls_hmac_drbg_write_seed_file( ctx, path ) );
  374. }
  375. #endif /* MBEDTLS_FS_IO */
  376. #if defined(MBEDTLS_SELF_TEST)
  377. #if !defined(MBEDTLS_SHA1_C)
  378. /* Dummy checkup routine */
  379. int mbedtls_hmac_drbg_self_test( int verbose )
  380. {
  381. (void) verbose;
  382. return( 0 );
  383. }
  384. #else
  385. #define OUTPUT_LEN 80
  386. /* From a NIST PR=true test vector */
  387. static const unsigned char entropy_pr[] = {
  388. 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f,
  389. 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11,
  390. 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42,
  391. 0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3,
  392. 0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4 };
  393. static const unsigned char result_pr[OUTPUT_LEN] = {
  394. 0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39,
  395. 0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94,
  396. 0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54,
  397. 0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e,
  398. 0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab,
  399. 0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3,
  400. 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 };
  401. /* From a NIST PR=false test vector */
  402. static const unsigned char entropy_nopr[] = {
  403. 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66,
  404. 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8,
  405. 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3,
  406. 0xe9, 0x9d, 0xfe, 0xdf };
  407. static const unsigned char result_nopr[OUTPUT_LEN] = {
  408. 0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f,
  409. 0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6,
  410. 0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a,
  411. 0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec,
  412. 0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd,
  413. 0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49,
  414. 0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7 };
  415. /* "Entropy" from buffer */
  416. static size_t test_offset;
  417. static int hmac_drbg_self_test_entropy( void *data,
  418. unsigned char *buf, size_t len )
  419. {
  420. const unsigned char *p = data;
  421. memcpy( buf, p + test_offset, len );
  422. test_offset += len;
  423. return( 0 );
  424. }
  425. #define CHK( c ) if( (c) != 0 ) \
  426. { \
  427. if( verbose != 0 ) \
  428. mbedtls_printf( "failed\n" ); \
  429. return( 1 ); \
  430. }
  431. /*
  432. * Checkup routine for HMAC_DRBG with SHA-1
  433. */
  434. int mbedtls_hmac_drbg_self_test( int verbose )
  435. {
  436. mbedtls_hmac_drbg_context ctx;
  437. unsigned char buf[OUTPUT_LEN];
  438. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  439. mbedtls_hmac_drbg_init( &ctx );
  440. /*
  441. * PR = True
  442. */
  443. if( verbose != 0 )
  444. mbedtls_printf( " HMAC_DRBG (PR = True) : " );
  445. test_offset = 0;
  446. CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  447. hmac_drbg_self_test_entropy, (void *) entropy_pr,
  448. NULL, 0 ) );
  449. mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
  450. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  451. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  452. CHK( memcmp( buf, result_pr, OUTPUT_LEN ) );
  453. mbedtls_hmac_drbg_free( &ctx );
  454. mbedtls_hmac_drbg_free( &ctx );
  455. if( verbose != 0 )
  456. mbedtls_printf( "passed\n" );
  457. /*
  458. * PR = False
  459. */
  460. if( verbose != 0 )
  461. mbedtls_printf( " HMAC_DRBG (PR = False) : " );
  462. mbedtls_hmac_drbg_init( &ctx );
  463. test_offset = 0;
  464. CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  465. hmac_drbg_self_test_entropy, (void *) entropy_nopr,
  466. NULL, 0 ) );
  467. CHK( mbedtls_hmac_drbg_reseed( &ctx, NULL, 0 ) );
  468. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  469. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  470. CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) );
  471. mbedtls_hmac_drbg_free( &ctx );
  472. mbedtls_hmac_drbg_free( &ctx );
  473. if( verbose != 0 )
  474. mbedtls_printf( "passed\n" );
  475. if( verbose != 0 )
  476. mbedtls_printf( "\n" );
  477. return( 0 );
  478. }
  479. #endif /* MBEDTLS_SHA1_C */
  480. #endif /* MBEDTLS_SELF_TEST */
  481. #endif /* MBEDTLS_HMAC_DRBG_C */