hmac_drbg.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. * HMAC_DRBG implementation (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-90A DRBGs are described in the following publication.
  48. * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
  49. * References below are based on rev. 1 (January 2012).
  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_HMAC_DRBG_C)
  57. #include "mbedtls/hmac_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_SELF_TEST */
  70. #endif /* MBEDTLS_PLATFORM_C */
  71. /*
  72. * HMAC_DRBG context initialization
  73. */
  74. void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx )
  75. {
  76. memset( ctx, 0, sizeof( mbedtls_hmac_drbg_context ) );
  77. ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
  78. }
  79. /*
  80. * HMAC_DRBG update, using optional additional data (10.1.2.2)
  81. */
  82. int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx,
  83. const unsigned char *additional,
  84. size_t add_len )
  85. {
  86. size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  87. unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
  88. unsigned char sep[1];
  89. unsigned char K[MBEDTLS_MD_MAX_SIZE];
  90. int ret;
  91. for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
  92. {
  93. /* Step 1 or 4 */
  94. if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 )
  95. goto exit;
  96. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  97. ctx->V, md_len ) ) != 0 )
  98. goto exit;
  99. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  100. sep, 1 ) ) != 0 )
  101. goto exit;
  102. if( rounds == 2 )
  103. {
  104. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  105. additional, add_len ) ) != 0 )
  106. goto exit;
  107. }
  108. if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, K ) ) != 0 )
  109. goto exit;
  110. /* Step 2 or 5 */
  111. if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len ) ) != 0 )
  112. goto exit;
  113. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  114. ctx->V, md_len ) ) != 0 )
  115. goto exit;
  116. if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 )
  117. goto exit;
  118. }
  119. exit:
  120. mbedtls_platform_zeroize( K, sizeof( K ) );
  121. return( ret );
  122. }
  123. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  124. void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx,
  125. const unsigned char *additional,
  126. size_t add_len )
  127. {
  128. (void) mbedtls_hmac_drbg_update_ret( ctx, additional, add_len );
  129. }
  130. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  131. /*
  132. * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
  133. */
  134. int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
  135. const mbedtls_md_info_t * md_info,
  136. const unsigned char *data, size_t data_len )
  137. {
  138. int ret;
  139. if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  140. return( ret );
  141. #if defined(MBEDTLS_THREADING_C)
  142. mbedtls_mutex_init( &ctx->mutex );
  143. #endif
  144. /*
  145. * Set initial working state.
  146. * Use the V memory location, which is currently all 0, to initialize the
  147. * MD context with an all-zero key. Then set V to its initial value.
  148. */
  149. if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V,
  150. mbedtls_md_get_size( md_info ) ) ) != 0 )
  151. return( ret );
  152. memset( ctx->V, 0x01, mbedtls_md_get_size( md_info ) );
  153. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, data, data_len ) ) != 0 )
  154. return( ret );
  155. return( 0 );
  156. }
  157. /*
  158. * Internal function used both for seeding and reseeding the DRBG.
  159. * Comments starting with arabic numbers refer to section 10.1.2.4
  160. * of SP800-90A, while roman numbers refer to section 9.2.
  161. */
  162. static int hmac_drbg_reseed_core( mbedtls_hmac_drbg_context *ctx,
  163. const unsigned char *additional, size_t len,
  164. int use_nonce )
  165. {
  166. unsigned char seed[MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT];
  167. size_t seedlen = 0;
  168. int ret;
  169. {
  170. size_t total_entropy_len;
  171. if( use_nonce == 0 )
  172. total_entropy_len = ctx->entropy_len;
  173. else
  174. total_entropy_len = ctx->entropy_len * 3 / 2;
  175. /* III. Check input length */
  176. if( len > MBEDTLS_HMAC_DRBG_MAX_INPUT ||
  177. total_entropy_len + len > MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT )
  178. {
  179. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  180. }
  181. }
  182. memset( seed, 0, MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT );
  183. /* IV. Gather entropy_len bytes of entropy for the seed */
  184. if( ( ret = ctx->f_entropy( ctx->p_entropy,
  185. seed, ctx->entropy_len ) ) != 0 )
  186. {
  187. return( MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
  188. }
  189. seedlen += ctx->entropy_len;
  190. /* For initial seeding, allow adding of nonce generated
  191. * from the entropy source. See Sect 8.6.7 in SP800-90A. */
  192. if( use_nonce )
  193. {
  194. /* Note: We don't merge the two calls to f_entropy() in order
  195. * to avoid requesting too much entropy from f_entropy()
  196. * at once. Specifically, if the underlying digest is not
  197. * SHA-1, 3 / 2 * entropy_len is at least 36 Bytes, which
  198. * is larger than the maximum of 32 Bytes that our own
  199. * entropy source implementation can emit in a single
  200. * call in configurations disabling SHA-512. */
  201. if( ( ret = ctx->f_entropy( ctx->p_entropy,
  202. seed + seedlen,
  203. ctx->entropy_len / 2 ) ) != 0 )
  204. {
  205. return( MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
  206. }
  207. seedlen += ctx->entropy_len / 2;
  208. }
  209. /* 1. Concatenate entropy and additional data if any */
  210. if( additional != NULL && len != 0 )
  211. {
  212. memcpy( seed + seedlen, additional, len );
  213. seedlen += len;
  214. }
  215. /* 2. Update state */
  216. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, seed, seedlen ) ) != 0 )
  217. goto exit;
  218. /* 3. Reset reseed_counter */
  219. ctx->reseed_counter = 1;
  220. exit:
  221. /* 4. Done */
  222. mbedtls_platform_zeroize( seed, seedlen );
  223. return( ret );
  224. }
  225. /*
  226. * HMAC_DRBG reseeding: 10.1.2.4 + 9.2
  227. */
  228. int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
  229. const unsigned char *additional, size_t len )
  230. {
  231. return( hmac_drbg_reseed_core( ctx, additional, len, 0 ) );
  232. }
  233. /*
  234. * HMAC_DRBG initialisation (10.1.2.3 + 9.1)
  235. *
  236. * The nonce is not passed as a separate parameter but extracted
  237. * from the entropy source as suggested in 8.6.7.
  238. */
  239. int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
  240. const mbedtls_md_info_t * md_info,
  241. int (*f_entropy)(void *, unsigned char *, size_t),
  242. void *p_entropy,
  243. const unsigned char *custom,
  244. size_t len )
  245. {
  246. int ret;
  247. size_t md_size;
  248. if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 )
  249. return( ret );
  250. /* The mutex is initialized iff the md context is set up. */
  251. #if defined(MBEDTLS_THREADING_C)
  252. mbedtls_mutex_init( &ctx->mutex );
  253. #endif
  254. md_size = mbedtls_md_get_size( md_info );
  255. /*
  256. * Set initial working state.
  257. * Use the V memory location, which is currently all 0, to initialize the
  258. * MD context with an all-zero key. Then set V to its initial value.
  259. */
  260. if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V, md_size ) ) != 0 )
  261. return( ret );
  262. memset( ctx->V, 0x01, md_size );
  263. ctx->f_entropy = f_entropy;
  264. ctx->p_entropy = p_entropy;
  265. if( ctx->entropy_len == 0 )
  266. {
  267. /*
  268. * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
  269. * each hash function, then according to SP800-90A rev1 10.1 table 2,
  270. * min_entropy_len (in bits) is security_strength.
  271. *
  272. * (This also matches the sizes used in the NIST test vectors.)
  273. */
  274. ctx->entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
  275. md_size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
  276. 32; /* better (256+) -> 256 bits */
  277. }
  278. if( ( ret = hmac_drbg_reseed_core( ctx, custom, len,
  279. 1 /* add nonce */ ) ) != 0 )
  280. {
  281. return( ret );
  282. }
  283. return( 0 );
  284. }
  285. /*
  286. * Set prediction resistance
  287. */
  288. void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
  289. int resistance )
  290. {
  291. ctx->prediction_resistance = resistance;
  292. }
  293. /*
  294. * Set entropy length grabbed for seeding
  295. */
  296. void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len )
  297. {
  298. ctx->entropy_len = len;
  299. }
  300. /*
  301. * Set reseed interval
  302. */
  303. void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx, int interval )
  304. {
  305. ctx->reseed_interval = interval;
  306. }
  307. /*
  308. * HMAC_DRBG random function with optional additional data:
  309. * 10.1.2.5 (arabic) + 9.3 (Roman)
  310. */
  311. int mbedtls_hmac_drbg_random_with_add( void *p_rng,
  312. unsigned char *output, size_t out_len,
  313. const unsigned char *additional, size_t add_len )
  314. {
  315. int ret;
  316. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  317. size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info );
  318. size_t left = out_len;
  319. unsigned char *out = output;
  320. /* II. Check request length */
  321. if( out_len > MBEDTLS_HMAC_DRBG_MAX_REQUEST )
  322. return( MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG );
  323. /* III. Check input length */
  324. if( add_len > MBEDTLS_HMAC_DRBG_MAX_INPUT )
  325. return( MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  326. /* 1. (aka VII and IX) Check reseed counter and PR */
  327. if( ctx->f_entropy != NULL && /* For no-reseeding instances */
  328. ( ctx->prediction_resistance == MBEDTLS_HMAC_DRBG_PR_ON ||
  329. ctx->reseed_counter > ctx->reseed_interval ) )
  330. {
  331. if( ( ret = mbedtls_hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  332. return( ret );
  333. add_len = 0; /* VII.4 */
  334. }
  335. /* 2. Use additional data if any */
  336. if( additional != NULL && add_len != 0 )
  337. {
  338. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx,
  339. additional, add_len ) ) != 0 )
  340. goto exit;
  341. }
  342. /* 3, 4, 5. Generate bytes */
  343. while( left != 0 )
  344. {
  345. size_t use_len = left > md_len ? md_len : left;
  346. if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 )
  347. goto exit;
  348. if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx,
  349. ctx->V, md_len ) ) != 0 )
  350. goto exit;
  351. if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 )
  352. goto exit;
  353. memcpy( out, ctx->V, use_len );
  354. out += use_len;
  355. left -= use_len;
  356. }
  357. /* 6. Update */
  358. if( ( ret = mbedtls_hmac_drbg_update_ret( ctx,
  359. additional, add_len ) ) != 0 )
  360. goto exit;
  361. /* 7. Update reseed counter */
  362. ctx->reseed_counter++;
  363. exit:
  364. /* 8. Done */
  365. return( ret );
  366. }
  367. /*
  368. * HMAC_DRBG random function
  369. */
  370. int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
  371. {
  372. int ret;
  373. mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng;
  374. #if defined(MBEDTLS_THREADING_C)
  375. if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
  376. return( ret );
  377. #endif
  378. ret = mbedtls_hmac_drbg_random_with_add( ctx, output, out_len, NULL, 0 );
  379. #if defined(MBEDTLS_THREADING_C)
  380. if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
  381. return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
  382. #endif
  383. return( ret );
  384. }
  385. /*
  386. * This function resets HMAC_DRBG context to the state immediately
  387. * after initial call of mbedtls_hmac_drbg_init().
  388. */
  389. void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx )
  390. {
  391. if( ctx == NULL )
  392. return;
  393. #if defined(MBEDTLS_THREADING_C)
  394. /* The mutex is initialized iff the md context is set up. */
  395. if( ctx->md_ctx.md_info != NULL )
  396. mbedtls_mutex_free( &ctx->mutex );
  397. #endif
  398. mbedtls_md_free( &ctx->md_ctx );
  399. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_hmac_drbg_context ) );
  400. ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL;
  401. }
  402. #if defined(MBEDTLS_FS_IO)
  403. int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  404. {
  405. int ret;
  406. FILE *f;
  407. unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  408. if( ( f = fopen( path, "wb" ) ) == NULL )
  409. return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  410. if( ( ret = mbedtls_hmac_drbg_random( ctx, buf, sizeof( buf ) ) ) != 0 )
  411. goto exit;
  412. if( fwrite( buf, 1, sizeof( buf ), f ) != sizeof( buf ) )
  413. {
  414. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  415. goto exit;
  416. }
  417. ret = 0;
  418. exit:
  419. fclose( f );
  420. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  421. return( ret );
  422. }
  423. int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path )
  424. {
  425. int ret = 0;
  426. FILE *f = NULL;
  427. size_t n;
  428. unsigned char buf[ MBEDTLS_HMAC_DRBG_MAX_INPUT ];
  429. unsigned char c;
  430. if( ( f = fopen( path, "rb" ) ) == NULL )
  431. return( MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR );
  432. n = fread( buf, 1, sizeof( buf ), f );
  433. if( fread( &c, 1, 1, f ) != 0 )
  434. {
  435. ret = MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG;
  436. goto exit;
  437. }
  438. if( n == 0 || ferror( f ) )
  439. {
  440. ret = MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR;
  441. goto exit;
  442. }
  443. fclose( f );
  444. f = NULL;
  445. ret = mbedtls_hmac_drbg_update_ret( ctx, buf, n );
  446. exit:
  447. mbedtls_platform_zeroize( buf, sizeof( buf ) );
  448. if( f != NULL )
  449. fclose( f );
  450. if( ret != 0 )
  451. return( ret );
  452. return( mbedtls_hmac_drbg_write_seed_file( ctx, path ) );
  453. }
  454. #endif /* MBEDTLS_FS_IO */
  455. #if defined(MBEDTLS_SELF_TEST)
  456. #if !defined(MBEDTLS_SHA1_C)
  457. /* Dummy checkup routine */
  458. int mbedtls_hmac_drbg_self_test( int verbose )
  459. {
  460. (void) verbose;
  461. return( 0 );
  462. }
  463. #else
  464. #define OUTPUT_LEN 80
  465. /* From a NIST PR=true test vector */
  466. static const unsigned char entropy_pr[] = {
  467. 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f,
  468. 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11,
  469. 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42,
  470. 0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3,
  471. 0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4 };
  472. static const unsigned char result_pr[OUTPUT_LEN] = {
  473. 0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39,
  474. 0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94,
  475. 0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54,
  476. 0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e,
  477. 0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab,
  478. 0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3,
  479. 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 };
  480. /* From a NIST PR=false test vector */
  481. static const unsigned char entropy_nopr[] = {
  482. 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66,
  483. 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8,
  484. 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3,
  485. 0xe9, 0x9d, 0xfe, 0xdf };
  486. static const unsigned char result_nopr[OUTPUT_LEN] = {
  487. 0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f,
  488. 0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6,
  489. 0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a,
  490. 0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec,
  491. 0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd,
  492. 0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49,
  493. 0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7 };
  494. /* "Entropy" from buffer */
  495. static size_t test_offset;
  496. static int hmac_drbg_self_test_entropy( void *data,
  497. unsigned char *buf, size_t len )
  498. {
  499. const unsigned char *p = data;
  500. memcpy( buf, p + test_offset, len );
  501. test_offset += len;
  502. return( 0 );
  503. }
  504. #define CHK( c ) if( (c) != 0 ) \
  505. { \
  506. if( verbose != 0 ) \
  507. mbedtls_printf( "failed\n" ); \
  508. return( 1 ); \
  509. }
  510. /*
  511. * Checkup routine for HMAC_DRBG with SHA-1
  512. */
  513. int mbedtls_hmac_drbg_self_test( int verbose )
  514. {
  515. mbedtls_hmac_drbg_context ctx;
  516. unsigned char buf[OUTPUT_LEN];
  517. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
  518. mbedtls_hmac_drbg_init( &ctx );
  519. /*
  520. * PR = True
  521. */
  522. if( verbose != 0 )
  523. mbedtls_printf( " HMAC_DRBG (PR = True) : " );
  524. test_offset = 0;
  525. CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  526. hmac_drbg_self_test_entropy, (void *) entropy_pr,
  527. NULL, 0 ) );
  528. mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
  529. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  530. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  531. CHK( memcmp( buf, result_pr, OUTPUT_LEN ) );
  532. mbedtls_hmac_drbg_free( &ctx );
  533. mbedtls_hmac_drbg_free( &ctx );
  534. if( verbose != 0 )
  535. mbedtls_printf( "passed\n" );
  536. /*
  537. * PR = False
  538. */
  539. if( verbose != 0 )
  540. mbedtls_printf( " HMAC_DRBG (PR = False) : " );
  541. mbedtls_hmac_drbg_init( &ctx );
  542. test_offset = 0;
  543. CHK( mbedtls_hmac_drbg_seed( &ctx, md_info,
  544. hmac_drbg_self_test_entropy, (void *) entropy_nopr,
  545. NULL, 0 ) );
  546. CHK( mbedtls_hmac_drbg_reseed( &ctx, NULL, 0 ) );
  547. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  548. CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  549. CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) );
  550. mbedtls_hmac_drbg_free( &ctx );
  551. mbedtls_hmac_drbg_free( &ctx );
  552. if( verbose != 0 )
  553. mbedtls_printf( "passed\n" );
  554. if( verbose != 0 )
  555. mbedtls_printf( "\n" );
  556. return( 0 );
  557. }
  558. #endif /* MBEDTLS_SHA1_C */
  559. #endif /* MBEDTLS_SELF_TEST */
  560. #endif /* MBEDTLS_HMAC_DRBG_C */