You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hmac_drbg.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * HMAC_DRBG algorithm
  23. *
  24. * This algorithm is designed to comply with ANS X9.82 Part 3-2007
  25. * Section 10.2.2.2. This standard is not freely available, but most
  26. * of the text appears to be shared with NIST SP 800-90, which can be
  27. * downloaded from
  28. *
  29. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  30. *
  31. * Where possible, references are given to both documents. In the
  32. * case of any disagreement, ANS X9.82 takes priority over NIST SP
  33. * 800-90. (In particular, note that some algorithms that are
  34. * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
  35. */
  36. #include <stdint.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #include <assert.h>
  40. #include <ipxe/crypto.h>
  41. #include <ipxe/hmac.h>
  42. #include <ipxe/hmac_drbg.h>
  43. /**
  44. * Update the HMAC_DRBG key
  45. *
  46. * @v hash Underlying hash algorithm
  47. * @v state HMAC_DRBG internal state
  48. * @v data Provided data
  49. * @v len Length of provided data
  50. * @v single Single byte used in concatenation
  51. *
  52. * This function carries out the operation
  53. *
  54. * K = HMAC ( K, V || single || provided_data )
  55. *
  56. * as used by hmac_drbg_update()
  57. */
  58. static void hmac_drbg_update_key ( struct digest_algorithm *hash,
  59. struct hmac_drbg_state *state,
  60. const void *data, size_t len,
  61. const uint8_t single ) {
  62. uint8_t context[ hash->ctxsize ];
  63. size_t out_len = hash->digestsize;
  64. DBGC ( state, "HMAC_DRBG_%s %p provided data :\n", hash->name, state );
  65. DBGC_HDA ( state, 0, data, len );
  66. /* Sanity checks */
  67. assert ( hash != NULL );
  68. assert ( state != NULL );
  69. assert ( ( data != NULL ) || ( len == 0 ) );
  70. assert ( ( single == 0x00 ) || ( single == 0x01 ) );
  71. /* K = HMAC ( K, V || single || provided_data ) */
  72. hmac_init ( hash, context, state->key, &out_len );
  73. assert ( out_len == hash->digestsize );
  74. hmac_update ( hash, context, state->value, out_len );
  75. hmac_update ( hash, context, &single, sizeof ( single ) );
  76. hmac_update ( hash, context, data, len );
  77. hmac_final ( hash, context, state->key, &out_len, state->key );
  78. assert ( out_len == hash->digestsize );
  79. DBGC ( state, "HMAC_DRBG_%s %p K = HMAC ( K, V || %#02x || "
  80. "provided_data ) :\n", hash->name, state, single );
  81. DBGC_HDA ( state, 0, state->key, out_len );
  82. }
  83. /**
  84. * Update the HMAC_DRBG value
  85. *
  86. * @v hash Underlying hash algorithm
  87. * @v state HMAC_DRBG internal state
  88. * @v data Provided data
  89. * @v len Length of provided data
  90. * @v single Single byte used in concatenation
  91. *
  92. * This function carries out the operation
  93. *
  94. * V = HMAC ( K, V )
  95. *
  96. * as used by hmac_drbg_update() and hmac_drbg_generate()
  97. */
  98. static void hmac_drbg_update_value ( struct digest_algorithm *hash,
  99. struct hmac_drbg_state *state ) {
  100. uint8_t context[ hash->ctxsize ];
  101. size_t out_len = hash->digestsize;
  102. /* Sanity checks */
  103. assert ( hash != NULL );
  104. assert ( state != NULL );
  105. /* V = HMAC ( K, V ) */
  106. hmac_init ( hash, context, state->key, &out_len );
  107. assert ( out_len == hash->digestsize );
  108. hmac_update ( hash, context, state->value, out_len );
  109. hmac_final ( hash, context, state->key, &out_len, state->value );
  110. assert ( out_len == hash->digestsize );
  111. DBGC ( state, "HMAC_DRBG_%s %p V = HMAC ( K, V ) :\n",
  112. hash->name, state );
  113. DBGC_HDA ( state, 0, state->value, out_len );
  114. }
  115. /**
  116. * Update HMAC_DRBG internal state
  117. *
  118. * @v hash Underlying hash algorithm
  119. * @v state HMAC_DRBG internal state
  120. * @v data Provided data
  121. * @v len Length of provided data
  122. *
  123. * This is the HMAC_DRBG_Update function defined in ANS X9.82 Part
  124. * 3-2007 Section 10.2.2.2.2 (NIST SP 800-90 Section 10.1.2.2).
  125. *
  126. * The key and value are updated in-place within the HMAC_DRBG
  127. * internal state.
  128. */
  129. static void hmac_drbg_update ( struct digest_algorithm *hash,
  130. struct hmac_drbg_state *state,
  131. const void *data, size_t len ) {
  132. DBGC ( state, "HMAC_DRBG_%s %p update\n", hash->name, state );
  133. /* Sanity checks */
  134. assert ( hash != NULL );
  135. assert ( state != NULL );
  136. assert ( ( data != NULL ) || ( len == 0 ) );
  137. /* 1. K = HMAC ( K, V || 0x00 || provided_data ) */
  138. hmac_drbg_update_key ( hash, state, data, len, 0x00 );
  139. /* 2. V = HMAC ( K, V ) */
  140. hmac_drbg_update_value ( hash, state );
  141. /* 3. If ( provided_data = Null ), then return K and V */
  142. if ( ! len )
  143. return;
  144. /* 4. K = HMAC ( K, V || 0x01 || provided_data ) */
  145. hmac_drbg_update_key ( hash, state, data, len, 0x01 );
  146. /* 5. V = HMAC ( K, V ) */
  147. hmac_drbg_update_value ( hash, state );
  148. /* 6. Return K and V */
  149. }
  150. /**
  151. * Instantiate HMAC_DRBG
  152. *
  153. * @v hash Underlying hash algorithm
  154. * @v state HMAC_DRBG internal state to be initialised
  155. * @v entropy Entropy input
  156. * @v entropy_len Length of entropy input
  157. * @v personal Personalisation string
  158. * @v personal_len Length of personalisation string
  159. *
  160. * This is the HMAC_DRBG_Instantiate_algorithm function defined in ANS
  161. * X9.82 Part 3-2007 Section 10.2.2.2.3 (NIST SP 800-90 Section
  162. * 10.1.2.3).
  163. *
  164. * The nonce must be included within the entropy input (i.e. the
  165. * entropy input must contain at least 3/2 * security_strength bits of
  166. * entropy, as per ANS X9.82 Part 3-2007 Section 8.4.2 (NIST SP 800-90
  167. * Section 8.6.7).
  168. *
  169. * The key, value and reseed counter are updated in-place within the
  170. * HMAC_DRBG internal state.
  171. */
  172. void hmac_drbg_instantiate ( struct digest_algorithm *hash,
  173. struct hmac_drbg_state *state,
  174. const void *entropy, size_t entropy_len,
  175. const void *personal, size_t personal_len ){
  176. size_t out_len = hash->digestsize;
  177. DBGC ( state, "HMAC_DRBG_%s %p instantiate\n", hash->name, state );
  178. /* Sanity checks */
  179. assert ( hash != NULL );
  180. assert ( state != NULL );
  181. assert ( entropy != NULL );
  182. assert ( ( personal != NULL ) || ( personal_len == 0 ) );
  183. /* 1. seed_material = entropy_input || nonce ||
  184. * personalisation_string
  185. */
  186. /* 2. Key = 0x00 00..00 */
  187. memset ( state->key, 0x00, out_len );
  188. /* 3. V = 0x01 01...01 */
  189. memset ( state->value, 0x01, out_len );
  190. /* 4. ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V )
  191. * 5. reseed_counter = 1
  192. * 6. Return V, Key and reseed_counter as the
  193. * initial_working_state
  194. */
  195. hmac_drbg_reseed ( hash, state, entropy, entropy_len,
  196. personal, personal_len );
  197. }
  198. /**
  199. * Reseed HMAC_DRBG
  200. *
  201. * @v hash Underlying hash algorithm
  202. * @v state HMAC_DRBG internal state
  203. * @v entropy Entropy input
  204. * @v entropy_len Length of entropy input
  205. * @v additional Additional input
  206. * @v additional_len Length of additional input
  207. *
  208. * This is the HMAC_DRBG_Reseed_algorithm function defined in ANS X9.82
  209. * Part 3-2007 Section 10.2.2.2.4 (NIST SP 800-90 Section 10.1.2.4).
  210. *
  211. * The key, value and reseed counter are updated in-place within the
  212. * HMAC_DRBG internal state.
  213. */
  214. void hmac_drbg_reseed ( struct digest_algorithm *hash,
  215. struct hmac_drbg_state *state,
  216. const void *entropy, size_t entropy_len,
  217. const void *additional, size_t additional_len ) {
  218. uint8_t seed_material[ entropy_len + additional_len ];
  219. DBGC ( state, "HMAC_DRBG_%s %p (re)seed\n", hash->name, state );
  220. /* Sanity checks */
  221. assert ( hash != NULL );
  222. assert ( state != NULL );
  223. assert ( entropy != NULL );
  224. assert ( ( additional != NULL ) || ( additional_len == 0 ) );
  225. /* 1. seed_material = entropy_input || additional_input */
  226. memcpy ( seed_material, entropy, entropy_len );
  227. memcpy ( ( seed_material + entropy_len ), additional, additional_len );
  228. DBGC ( state, "HMAC_DRBG_%s %p seed material :\n", hash->name, state );
  229. DBGC_HDA ( state, 0, seed_material, sizeof ( seed_material ) );
  230. /* 2. ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V ) */
  231. hmac_drbg_update ( hash, state, seed_material,
  232. sizeof ( seed_material ) );
  233. /* 3. reseed_counter = 1 */
  234. state->reseed_counter = 1;
  235. /* 4. Return V, Key and reseed_counter as the new_working_state */
  236. }
  237. /**
  238. * Generate pseudorandom bits using HMAC_DRBG
  239. *
  240. * @v hash Underlying hash algorithm
  241. * @v state HMAC_DRBG internal state
  242. * @v additional Additional input
  243. * @v additional_len Length of additional input
  244. * @v data Output buffer
  245. * @v len Length of output buffer
  246. * @ret rc Return status code
  247. *
  248. * This is the HMAC_DRBG_Generate_algorithm function defined in ANS X9.82
  249. * Part 3-2007 Section 10.2.2.2.5 (NIST SP 800-90 Section 10.1.2.5).
  250. *
  251. * Requests must be for an integral number of bytes.
  252. *
  253. * The key, value and reseed counter are updated in-place within the
  254. * HMAC_DRBG internal state.
  255. *
  256. * Note that the only permitted error is "reseed required".
  257. */
  258. int hmac_drbg_generate ( struct digest_algorithm *hash,
  259. struct hmac_drbg_state *state,
  260. const void *additional, size_t additional_len,
  261. void *data, size_t len ) {
  262. size_t out_len = hash->digestsize;
  263. void *orig_data = data;
  264. size_t orig_len = len;
  265. size_t frag_len;
  266. DBGC ( state, "HMAC_DRBG_%s %p generate\n", hash->name, state );
  267. /* Sanity checks */
  268. assert ( hash != NULL );
  269. assert ( state != NULL );
  270. assert ( data != NULL );
  271. assert ( ( additional != NULL ) || ( additional_len == 0 ) );
  272. /* 1. If reseed_counter > reseed_interval, then return an
  273. * indication that a reseed is required
  274. */
  275. if ( state->reseed_counter > HMAC_DRBG_RESEED_INTERVAL ) {
  276. DBGC ( state, "HMAC_DRBG_%s %p reseed interval exceeded\n",
  277. hash->name, state );
  278. return -ESTALE;
  279. }
  280. /* 2. If additional_input != Null, then
  281. * ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V )
  282. */
  283. if ( additional_len )
  284. hmac_drbg_update ( hash, state, additional, additional_len );
  285. /* 3. temp = Null
  286. * 4. While ( len ( temp ) < requested_number_of_bits ) do:
  287. */
  288. while ( len ) {
  289. /* 4.1 V = HMAC ( Key, V ) */
  290. hmac_drbg_update_value ( hash, state );
  291. /* 4.2. temp = temp || V
  292. * 5. returned_bits = Leftmost requested_number_of_bits
  293. * of temp
  294. */
  295. frag_len = len;
  296. if ( frag_len > out_len )
  297. frag_len = out_len;
  298. memcpy ( data, state->value, frag_len );
  299. data += frag_len;
  300. len -= frag_len;
  301. }
  302. /* 6. ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V ) */
  303. hmac_drbg_update ( hash, state, additional, additional_len );
  304. /* 7. reseed_counter = reseed_counter + 1 */
  305. state->reseed_counter++;
  306. DBGC ( state, "HMAC_DRBG_%s %p generated :\n", hash->name, state );
  307. DBGC_HDA ( state, 0, orig_data, orig_len );
  308. /* 8. Return SUCCESS, returned_bits, and the new values of
  309. * Key, V and reseed_counter as the new_working_state
  310. */
  311. return 0;
  312. }