hmac_drbg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * HMAC_DRBG implementation (NIST SP 800-90)
  3. *
  4. * Copyright (C) 2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The NIST SP 800-90A DRBGs are described in the following publication.
  27. * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
  28. * References below are based on rev. 1 (January 2012).
  29. */
  30. #if !defined(POLARSSL_CONFIG_FILE)
  31. #include "polarssl/config.h"
  32. #else
  33. #include POLARSSL_CONFIG_FILE
  34. #endif
  35. #if defined(POLARSSL_HMAC_DRBG_C)
  36. #include "polarssl/hmac_drbg.h"
  37. #if defined(POLARSSL_FS_IO)
  38. #include <stdio.h>
  39. #endif
  40. #if defined(POLARSSL_PLATFORM_C)
  41. #include "polarssl/platform.h"
  42. #else
  43. #define polarssl_printf printf
  44. #endif
  45. /* Implementation that should never be optimized out by the compiler */
  46. static void polarssl_zeroize( void *v, size_t n ) {
  47. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  48. }
  49. /*
  50. * HMAC_DRBG update, using optional additional data (10.1.2.2)
  51. */
  52. void hmac_drbg_update( hmac_drbg_context *ctx,
  53. const unsigned char *additional, size_t add_len )
  54. {
  55. size_t md_len = ctx->md_ctx.md_info->size;
  56. unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
  57. unsigned char sep[1];
  58. unsigned char K[POLARSSL_MD_MAX_SIZE];
  59. for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
  60. {
  61. /* Step 1 or 4 */
  62. md_hmac_reset( &ctx->md_ctx );
  63. md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
  64. md_hmac_update( &ctx->md_ctx, sep, 1 );
  65. if( rounds == 2 )
  66. md_hmac_update( &ctx->md_ctx, additional, add_len );
  67. md_hmac_finish( &ctx->md_ctx, K );
  68. /* Step 2 or 5 */
  69. md_hmac_starts( &ctx->md_ctx, K, md_len );
  70. md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
  71. md_hmac_finish( &ctx->md_ctx, ctx->V );
  72. }
  73. }
  74. /*
  75. * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
  76. */
  77. int hmac_drbg_init_buf( hmac_drbg_context *ctx,
  78. const md_info_t * md_info,
  79. const unsigned char *data, size_t data_len )
  80. {
  81. int ret;
  82. memset( ctx, 0, sizeof( hmac_drbg_context ) );
  83. md_init( &ctx->md_ctx );
  84. if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
  85. return( ret );
  86. /*
  87. * Set initial working state.
  88. * Use the V memory location, which is currently all 0, to initialize the
  89. * MD context with an all-zero key. Then set V to its initial value.
  90. */
  91. md_hmac_starts( &ctx->md_ctx, ctx->V, md_info->size );
  92. memset( ctx->V, 0x01, md_info->size );
  93. hmac_drbg_update( ctx, data, data_len );
  94. return( 0 );
  95. }
  96. /*
  97. * HMAC_DRBG reseeding: 10.1.2.4 (arabic) + 9.2 (Roman)
  98. */
  99. int hmac_drbg_reseed( hmac_drbg_context *ctx,
  100. const unsigned char *additional, size_t len )
  101. {
  102. unsigned char seed[POLARSSL_HMAC_DRBG_MAX_SEED_INPUT];
  103. size_t seedlen;
  104. /* III. Check input length */
  105. if( len > POLARSSL_HMAC_DRBG_MAX_INPUT ||
  106. ctx->entropy_len + len > POLARSSL_HMAC_DRBG_MAX_SEED_INPUT )
  107. {
  108. return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  109. }
  110. memset( seed, 0, POLARSSL_HMAC_DRBG_MAX_SEED_INPUT );
  111. /* IV. Gather entropy_len bytes of entropy for the seed */
  112. if( ctx->f_entropy( ctx->p_entropy, seed, ctx->entropy_len ) != 0 )
  113. return( POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
  114. seedlen = ctx->entropy_len;
  115. /* 1. Concatenate entropy and additional data if any */
  116. if( additional != NULL && len != 0 )
  117. {
  118. memcpy( seed + seedlen, additional, len );
  119. seedlen += len;
  120. }
  121. /* 2. Update state */
  122. hmac_drbg_update( ctx, seed, seedlen );
  123. /* 3. Reset reseed_counter */
  124. ctx->reseed_counter = 1;
  125. /* 4. Done */
  126. return( 0 );
  127. }
  128. /*
  129. * HMAC_DRBG initialisation (10.1.2.3 + 9.1)
  130. */
  131. int hmac_drbg_init( hmac_drbg_context *ctx,
  132. const md_info_t * md_info,
  133. int (*f_entropy)(void *, unsigned char *, size_t),
  134. void *p_entropy,
  135. const unsigned char *custom,
  136. size_t len )
  137. {
  138. int ret;
  139. size_t entropy_len;
  140. memset( ctx, 0, sizeof( hmac_drbg_context ) );
  141. md_init( &ctx->md_ctx );
  142. if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
  143. return( ret );
  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. md_hmac_starts( &ctx->md_ctx, ctx->V, md_info->size );
  150. memset( ctx->V, 0x01, md_info->size );
  151. ctx->f_entropy = f_entropy;
  152. ctx->p_entropy = p_entropy;
  153. ctx->reseed_interval = POLARSSL_HMAC_DRBG_RESEED_INTERVAL;
  154. /*
  155. * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
  156. * each hash function, then according to SP800-90A rev1 10.1 table 2,
  157. * min_entropy_len (in bits) is security_strength.
  158. *
  159. * (This also matches the sizes used in the NIST test vectors.)
  160. */
  161. entropy_len = md_info->size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
  162. md_info->size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
  163. 32; /* better (256+) -> 256 bits */
  164. /*
  165. * For initialisation, use more entropy to emulate a nonce
  166. * (Again, matches test vectors.)
  167. */
  168. ctx->entropy_len = entropy_len * 3 / 2;
  169. if( ( ret = hmac_drbg_reseed( ctx, custom, len ) ) != 0 )
  170. return( ret );
  171. ctx->entropy_len = entropy_len;
  172. return( 0 );
  173. }
  174. /*
  175. * Set prediction resistance
  176. */
  177. void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
  178. int resistance )
  179. {
  180. ctx->prediction_resistance = resistance;
  181. }
  182. /*
  183. * Set entropy length grabbed for reseeds
  184. */
  185. void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx, size_t len )
  186. {
  187. ctx->entropy_len = len;
  188. }
  189. /*
  190. * Set reseed interval
  191. */
  192. void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx, int interval )
  193. {
  194. ctx->reseed_interval = interval;
  195. }
  196. /*
  197. * HMAC_DRBG random function with optional additional data:
  198. * 10.1.2.5 (arabic) + 9.3 (Roman)
  199. */
  200. int hmac_drbg_random_with_add( void *p_rng,
  201. unsigned char *output, size_t out_len,
  202. const unsigned char *additional, size_t add_len )
  203. {
  204. int ret;
  205. hmac_drbg_context *ctx = (hmac_drbg_context *) p_rng;
  206. size_t md_len = md_get_size( ctx->md_ctx.md_info );
  207. size_t left = out_len;
  208. unsigned char *out = output;
  209. /* II. Check request length */
  210. if( out_len > POLARSSL_HMAC_DRBG_MAX_REQUEST )
  211. return( POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG );
  212. /* III. Check input length */
  213. if( add_len > POLARSSL_HMAC_DRBG_MAX_INPUT )
  214. return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  215. /* 1. (aka VII and IX) Check reseed counter and PR */
  216. if( ctx->f_entropy != NULL && /* For no-reseeding instances */
  217. ( ctx->prediction_resistance == POLARSSL_HMAC_DRBG_PR_ON ||
  218. ctx->reseed_counter > ctx->reseed_interval ) )
  219. {
  220. if( ( ret = hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
  221. return( ret );
  222. add_len = 0; /* VII.4 */
  223. }
  224. /* 2. Use additional data if any */
  225. if( additional != NULL && add_len != 0 )
  226. hmac_drbg_update( ctx, additional, add_len );
  227. /* 3, 4, 5. Generate bytes */
  228. while( left != 0 )
  229. {
  230. size_t use_len = left > md_len ? md_len : left;
  231. md_hmac_reset( &ctx->md_ctx );
  232. md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
  233. md_hmac_finish( &ctx->md_ctx, ctx->V );
  234. memcpy( out, ctx->V, use_len );
  235. out += use_len;
  236. left -= use_len;
  237. }
  238. /* 6. Update */
  239. hmac_drbg_update( ctx, additional, add_len );
  240. /* 7. Update reseed counter */
  241. ctx->reseed_counter++;
  242. /* 8. Done */
  243. return( 0 );
  244. }
  245. /*
  246. * HMAC_DRBG random function
  247. */
  248. int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
  249. {
  250. return( hmac_drbg_random_with_add( p_rng, output, out_len, NULL, 0 ) );
  251. }
  252. /*
  253. * Free an HMAC_DRBG context
  254. */
  255. void hmac_drbg_free( hmac_drbg_context *ctx )
  256. {
  257. if( ctx == NULL )
  258. return;
  259. md_free_ctx( &ctx->md_ctx );
  260. polarssl_zeroize( ctx, sizeof( hmac_drbg_context ) );
  261. }
  262. #if defined(POLARSSL_FS_IO)
  263. int hmac_drbg_write_seed_file( hmac_drbg_context *ctx, const char *path )
  264. {
  265. int ret;
  266. FILE *f;
  267. unsigned char buf[ POLARSSL_HMAC_DRBG_MAX_INPUT ];
  268. if( ( f = fopen( path, "wb" ) ) == NULL )
  269. return( POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR );
  270. if( ( ret = hmac_drbg_random( ctx, buf, sizeof( buf ) ) ) != 0 )
  271. goto exit;
  272. if( fwrite( buf, 1, sizeof( buf ), f ) != sizeof( buf ) )
  273. {
  274. ret = POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR;
  275. goto exit;
  276. }
  277. ret = 0;
  278. exit:
  279. fclose( f );
  280. return( ret );
  281. }
  282. int hmac_drbg_update_seed_file( hmac_drbg_context *ctx, const char *path )
  283. {
  284. FILE *f;
  285. size_t n;
  286. unsigned char buf[ POLARSSL_HMAC_DRBG_MAX_INPUT ];
  287. if( ( f = fopen( path, "rb" ) ) == NULL )
  288. return( POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR );
  289. fseek( f, 0, SEEK_END );
  290. n = (size_t) ftell( f );
  291. fseek( f, 0, SEEK_SET );
  292. if( n > POLARSSL_HMAC_DRBG_MAX_INPUT )
  293. {
  294. fclose( f );
  295. return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
  296. }
  297. if( fread( buf, 1, n, f ) != n )
  298. {
  299. fclose( f );
  300. return( POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR );
  301. }
  302. fclose( f );
  303. hmac_drbg_update( ctx, buf, n );
  304. return( hmac_drbg_write_seed_file( ctx, path ) );
  305. }
  306. #endif /* POLARSSL_FS_IO */
  307. #if defined(POLARSSL_SELF_TEST)
  308. #include <stdio.h>
  309. #if !defined(POLARSSL_SHA1_C)
  310. /* Dummy checkup routine */
  311. int hmac_drbg_self_test( int verbose )
  312. {
  313. if( verbose != 0 )
  314. polarssl_printf( "\n" );
  315. return( 0 );
  316. }
  317. #else
  318. #define OUTPUT_LEN 80
  319. /* From a NIST PR=true test vector */
  320. static unsigned char entropy_pr[] = {
  321. 0xa0, 0xc9, 0xab, 0x58, 0xf1, 0xe2, 0xe5, 0xa4, 0xde, 0x3e, 0xbd, 0x4f,
  322. 0xf7, 0x3e, 0x9c, 0x5b, 0x64, 0xef, 0xd8, 0xca, 0x02, 0x8c, 0xf8, 0x11,
  323. 0x48, 0xa5, 0x84, 0xfe, 0x69, 0xab, 0x5a, 0xee, 0x42, 0xaa, 0x4d, 0x42,
  324. 0x17, 0x60, 0x99, 0xd4, 0x5e, 0x13, 0x97, 0xdc, 0x40, 0x4d, 0x86, 0xa3,
  325. 0x7b, 0xf5, 0x59, 0x54, 0x75, 0x69, 0x51, 0xe4 };
  326. static const unsigned char result_pr[OUTPUT_LEN] = {
  327. 0x9a, 0x00, 0xa2, 0xd0, 0x0e, 0xd5, 0x9b, 0xfe, 0x31, 0xec, 0xb1, 0x39,
  328. 0x9b, 0x60, 0x81, 0x48, 0xd1, 0x96, 0x9d, 0x25, 0x0d, 0x3c, 0x1e, 0x94,
  329. 0x10, 0x10, 0x98, 0x12, 0x93, 0x25, 0xca, 0xb8, 0xfc, 0xcc, 0x2d, 0x54,
  330. 0x73, 0x19, 0x70, 0xc0, 0x10, 0x7a, 0xa4, 0x89, 0x25, 0x19, 0x95, 0x5e,
  331. 0x4b, 0xc6, 0x00, 0x1d, 0x7f, 0x4e, 0x6a, 0x2b, 0xf8, 0xa3, 0x01, 0xab,
  332. 0x46, 0x05, 0x5c, 0x09, 0xa6, 0x71, 0x88, 0xf1, 0xa7, 0x40, 0xee, 0xf3,
  333. 0xe1, 0x5c, 0x02, 0x9b, 0x44, 0xaf, 0x03, 0x44 };
  334. /* From a NIST PR=false test vector */
  335. static unsigned char entropy_nopr[] = {
  336. 0x79, 0x34, 0x9b, 0xbf, 0x7c, 0xdd, 0xa5, 0x79, 0x95, 0x57, 0x86, 0x66,
  337. 0x21, 0xc9, 0x13, 0x83, 0x11, 0x46, 0x73, 0x3a, 0xbf, 0x8c, 0x35, 0xc8,
  338. 0xc7, 0x21, 0x5b, 0x5b, 0x96, 0xc4, 0x8e, 0x9b, 0x33, 0x8c, 0x74, 0xe3,
  339. 0xe9, 0x9d, 0xfe, 0xdf };
  340. static const unsigned char result_nopr[OUTPUT_LEN] = {
  341. 0xc6, 0xa1, 0x6a, 0xb8, 0xd4, 0x20, 0x70, 0x6f, 0x0f, 0x34, 0xab, 0x7f,
  342. 0xec, 0x5a, 0xdc, 0xa9, 0xd8, 0xca, 0x3a, 0x13, 0x3e, 0x15, 0x9c, 0xa6,
  343. 0xac, 0x43, 0xc6, 0xf8, 0xa2, 0xbe, 0x22, 0x83, 0x4a, 0x4c, 0x0a, 0x0a,
  344. 0xff, 0xb1, 0x0d, 0x71, 0x94, 0xf1, 0xc1, 0xa5, 0xcf, 0x73, 0x22, 0xec,
  345. 0x1a, 0xe0, 0x96, 0x4e, 0xd4, 0xbf, 0x12, 0x27, 0x46, 0xe0, 0x87, 0xfd,
  346. 0xb5, 0xb3, 0xe9, 0x1b, 0x34, 0x93, 0xd5, 0xbb, 0x98, 0xfa, 0xed, 0x49,
  347. 0xe8, 0x5f, 0x13, 0x0f, 0xc8, 0xa4, 0x59, 0xb7 };
  348. /* "Entropy" from buffer */
  349. static size_t test_offset;
  350. static int hmac_drbg_self_test_entropy( void *data,
  351. unsigned char *buf, size_t len )
  352. {
  353. const unsigned char *p = data;
  354. memcpy( buf, p + test_offset, len );
  355. test_offset += len;
  356. return( 0 );
  357. }
  358. #define CHK( c ) if( (c) != 0 ) \
  359. { \
  360. if( verbose != 0 ) \
  361. polarssl_printf( "failed\n" ); \
  362. return( 1 ); \
  363. }
  364. /*
  365. * Checkup routine for HMAC_DRBG with SHA-1
  366. */
  367. int hmac_drbg_self_test( int verbose )
  368. {
  369. hmac_drbg_context ctx;
  370. unsigned char buf[OUTPUT_LEN];
  371. const md_info_t *md_info = md_info_from_type( POLARSSL_MD_SHA1 );
  372. /*
  373. * PR = True
  374. */
  375. if( verbose != 0 )
  376. polarssl_printf( " HMAC_DRBG (PR = True) : " );
  377. test_offset = 0;
  378. CHK( hmac_drbg_init( &ctx, md_info,
  379. hmac_drbg_self_test_entropy, entropy_pr,
  380. NULL, 0 ) );
  381. hmac_drbg_set_prediction_resistance( &ctx, POLARSSL_HMAC_DRBG_PR_ON );
  382. CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  383. CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  384. CHK( memcmp( buf, result_pr, OUTPUT_LEN ) );
  385. hmac_drbg_free( &ctx );
  386. if( verbose != 0 )
  387. polarssl_printf( "passed\n" );
  388. /*
  389. * PR = False
  390. */
  391. if( verbose != 0 )
  392. polarssl_printf( " HMAC_DRBG (PR = False) : " );
  393. test_offset = 0;
  394. CHK( hmac_drbg_init( &ctx, md_info,
  395. hmac_drbg_self_test_entropy, entropy_nopr,
  396. NULL, 0 ) );
  397. CHK( hmac_drbg_reseed( &ctx, NULL, 0 ) );
  398. CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  399. CHK( hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) );
  400. CHK( memcmp( buf, result_nopr, OUTPUT_LEN ) );
  401. hmac_drbg_free( &ctx );
  402. if( verbose != 0 )
  403. polarssl_printf( "passed\n" );
  404. if( verbose != 0 )
  405. polarssl_printf( "\n" );
  406. return( 0 );
  407. }
  408. #endif /* POLARSSL_SHA1_C */
  409. #endif /* POLARSSL_SELF_TEST */
  410. #endif /* POLARSSL_HMAC_DRBG_C */