Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

drbg.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. /** @file
  20. *
  21. * DRBG mechanism
  22. *
  23. * This mechanism is designed to comply with ANS X9.82 Part 3-2007
  24. * Section 9. This standard is not freely available, but most of the
  25. * text appears to be shared with NIST SP 800-90, which can be
  26. * downloaded from
  27. *
  28. * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
  29. *
  30. * Where possible, references are given to both documents. In the
  31. * case of any disagreement, ANS X9.82 takes priority over NIST SP
  32. * 800-90. (In particular, note that some algorithms that are
  33. * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
  34. */
  35. #include <stdint.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <ipxe/entropy.h>
  40. #include <ipxe/drbg.h>
  41. /**
  42. * Instantiate DRBG
  43. *
  44. * @v state Algorithm state to be initialised
  45. * @v personal Personalisation string
  46. * @v personal_len Length of personalisation string
  47. * @ret rc Return status code
  48. *
  49. * This is the Instantiate_function defined in ANS X9.82 Part 3-2007
  50. * Section 9.2 (NIST SP 800-90 Section 9.1).
  51. *
  52. * Only a single security strength is supported, and prediction
  53. * resistance is always enabled. The nonce is accounted for by
  54. * increasing the entropy input, as per ANS X9.82 Part 3-2007 Section
  55. * 8.4.2 (NIST SP 800-90 Section 8.6.7).
  56. */
  57. int drbg_instantiate ( struct drbg_state *state, const void *personal,
  58. size_t personal_len ) {
  59. unsigned int entropy_bits = ( ( 3 * DRBG_SECURITY_STRENGTH + 1 ) / 2 );
  60. size_t min_len = DRBG_MIN_ENTROPY_LEN_BYTES;
  61. size_t max_len = DRBG_MAX_ENTROPY_LEN_BYTES;
  62. uint8_t data[max_len];
  63. int len;
  64. int rc;
  65. DBGC ( state, "DRBG %p instantiate\n", state );
  66. /* Sanity checks */
  67. assert ( state != NULL );
  68. /* 1. If requested_instantiation_security_strength >
  69. * highest_supported_security_strength, then return an
  70. * ERROR_FLAG
  71. */
  72. if ( DRBG_SECURITY_STRENGTH > DRBG_MAX_SECURITY_STRENGTH ) {
  73. DBGC ( state, "DRBG %p cannot support security strength %d\n",
  74. state, DRBG_SECURITY_STRENGTH );
  75. return -ENOTSUP;
  76. }
  77. /* 2. If prediction_resistance_flag is set, and prediction
  78. * resistance is not supported, then return an ERROR_FLAG
  79. *
  80. * (Nothing to do since prediction resistance is always
  81. * supported.)
  82. */
  83. /* 3. If the length of the personalization_string >
  84. * max_personalization_string_length, return an ERROR_FLAG
  85. */
  86. if ( personal_len > DRBG_MAX_PERSONAL_LEN_BYTES ) {
  87. DBGC ( state, "DRBG %p personalisation string too long (%zd "
  88. "bytes)\n", state, personal_len );
  89. return -ERANGE;
  90. }
  91. /* 4. Set security_strength to the nearest security strength
  92. * greater than or equal to
  93. * requested_instantiation_security_strength.
  94. *
  95. * (Nothing to do since we support only a single security
  96. * strength.)
  97. */
  98. /* 5. Using the security_strength, select appropriate DRBG
  99. * mechanism parameters.
  100. *
  101. * (Nothing to do since we support only a single security
  102. * strength.)
  103. */
  104. /* 6. ( status, entropy_input ) = Get_entropy_input (
  105. * security_strength, min_length, max_length,
  106. * prediction_resistance_request )
  107. * 7. If an ERROR is returned in step 6, return a
  108. * CATASTROPHIC_ERROR_FLAG.
  109. * 8. Obtain a nonce.
  110. */
  111. len = get_entropy_input ( entropy_bits, data, min_len,
  112. sizeof ( data ) );
  113. if ( len < 0 ) {
  114. rc = len;
  115. DBGC ( state, "DRBG %p could not get entropy input: %s\n",
  116. state, strerror ( rc ) );
  117. return rc;
  118. }
  119. assert ( len >= ( int ) min_len );
  120. assert ( len <= ( int ) sizeof ( data ) );
  121. /* 9. initial_working_state = Instantiate_algorithm (
  122. * entropy_input, nonce, personalization_string ).
  123. */
  124. drbg_instantiate_algorithm ( state, data, len, personal, personal_len );
  125. /* 10. Get a state_handle for a currently empty state. If an
  126. * empty internal state cannot be found, return an
  127. * ERROR_FLAG.
  128. * 11. Set the internal state indicated by state_handle to
  129. * the initial values for the internal state (i.e. set
  130. * the working_state to the values returned as
  131. * initial_working_state in step 9 and any other values
  132. * required for the working_state, and set the
  133. * administrative information to the appropriate values.
  134. *
  135. * (Almost nothing to do since the memory to hold the state
  136. * was passed in by the caller and has already been updated
  137. * in-situ.)
  138. */
  139. state->reseed_required = 0;
  140. state->valid = 1;
  141. /* 12. Return SUCCESS and state_handle. */
  142. return 0;
  143. }
  144. /**
  145. * Reseed DRBG
  146. *
  147. * @v state Algorithm state
  148. * @v additional Additional input
  149. * @v additional_len Length of additional input
  150. * @ret rc Return status code
  151. *
  152. * This is the Reseed_function defined in ANS X9.82 Part 3-2007
  153. * Section 9.3 (NIST SP 800-90 Section 9.2).
  154. *
  155. * Prediction resistance is always enabled.
  156. */
  157. int drbg_reseed ( struct drbg_state *state, const void *additional,
  158. size_t additional_len ) {
  159. unsigned int entropy_bits = DRBG_SECURITY_STRENGTH;
  160. size_t min_len = DRBG_MIN_ENTROPY_LEN_BYTES;
  161. size_t max_len = DRBG_MAX_ENTROPY_LEN_BYTES;
  162. uint8_t data[max_len];
  163. int len;
  164. int rc;
  165. DBGC ( state, "DRBG %p reseed\n", state );
  166. /* Sanity checks */
  167. assert ( state != NULL );
  168. /* 1. Using state_handle, obtain the current internal state.
  169. * If state_handle indicates an invalid or empty internal
  170. * state, return an ERROR_FLAG.
  171. *
  172. * (Almost nothing to do since the memory holding the internal
  173. * state was passed in by the caller.)
  174. */
  175. if ( ! state->valid ) {
  176. DBGC ( state, "DRBG %p not valid\n", state );
  177. return -EINVAL;
  178. }
  179. /* 2. If prediction_resistance_request is set, and
  180. * prediction_resistance_flag is not set, then return an
  181. * ERROR_FLAG.
  182. *
  183. * (Nothing to do since prediction resistance is always
  184. * supported.)
  185. */
  186. /* 3. If the length of the additional_input >
  187. * max_additional_input_length, return an ERROR_FLAG.
  188. */
  189. if ( additional_len > DRBG_MAX_ADDITIONAL_LEN_BYTES ) {
  190. DBGC ( state, "DRBG %p additional input too long (%zd bytes)\n",
  191. state, additional_len );
  192. return -ERANGE;
  193. }
  194. /* 4. ( status, entropy_input ) = Get_entropy_input (
  195. * security_strength, min_length, max_length,
  196. * prediction_resistance_request ).
  197. *
  198. * 5. If an ERROR is returned in step 4, return a
  199. * CATASTROPHIC_ERROR_FLAG.
  200. */
  201. len = get_entropy_input ( entropy_bits, data, min_len,
  202. sizeof ( data ) );
  203. if ( len < 0 ) {
  204. rc = len;
  205. DBGC ( state, "DRBG %p could not get entropy input: %s\n",
  206. state, strerror ( rc ) );
  207. return rc;
  208. }
  209. /* 6. new_working_state = Reseed_algorithm ( working_state,
  210. * entropy_input, additional_input ).
  211. */
  212. drbg_reseed_algorithm ( state, data, len, additional, additional_len );
  213. /* 7. Replace the working_state in the internal state
  214. * indicated by state_handle with the values of
  215. * new_working_state obtained in step 6.
  216. *
  217. * (Nothing to do since the state has already been updated in-situ.)
  218. */
  219. /* 8. Return SUCCESS. */
  220. return 0;
  221. }
  222. /**
  223. * Generate pseudorandom bits using DRBG
  224. *
  225. * @v state Algorithm state
  226. * @v additional Additional input
  227. * @v additional_len Length of additional input
  228. * @v prediction_resist Prediction resistance is required
  229. * @v data Output buffer
  230. * @v len Length of output buffer
  231. * @ret rc Return status code
  232. *
  233. * This is the Generate_function defined in ANS X9.82 Part 3-2007
  234. * Section 9.4 (NIST SP 800-90 Section 9.3).
  235. *
  236. * Requests must be for an integral number of bytes. Only a single
  237. * security strength is supported. Prediction resistance is supported
  238. * if requested.
  239. */
  240. int drbg_generate ( struct drbg_state *state, const void *additional,
  241. size_t additional_len, int prediction_resist,
  242. void *data, size_t len ) {
  243. int rc;
  244. DBGC ( state, "DRBG %p generate\n", state );
  245. /* Sanity checks */
  246. assert ( state != NULL );
  247. assert ( data != NULL );
  248. /* 1. Using state_handle, obtain the current internal state
  249. * for the instantiation. If state_handle indicates an
  250. * invalid or empty internal state, then return an ERROR_FLAG.
  251. *
  252. * (Almost nothing to do since the memory holding the internal
  253. * state was passed in by the caller.)
  254. */
  255. if ( ! state->valid ) {
  256. DBGC ( state, "DRBG %p not valid\n", state );
  257. return -EINVAL;
  258. }
  259. /* 2. If requested_number_of_bits >
  260. * max_number_of_bits_per_request, then return an
  261. * ERROR_FLAG.
  262. */
  263. if ( len > DRBG_MAX_GENERATED_LEN_BYTES ) {
  264. DBGC ( state, "DRBG %p request too long (%zd bytes)\n",
  265. state, len );
  266. return -ERANGE;
  267. }
  268. /* 3. If requested_security_strength > the security_strength
  269. * indicated in the internal state, then return an
  270. * ERROR_FLAG.
  271. *
  272. * (Nothing to do since only a single security strength is
  273. * supported.)
  274. */
  275. /* 4. If the length of the additional_input >
  276. * max_additional_input_length, then return an ERROR_FLAG.
  277. */
  278. if ( additional_len > DRBG_MAX_ADDITIONAL_LEN_BYTES ) {
  279. DBGC ( state, "DRBG %p additional input too long (%zd bytes)\n",
  280. state, additional_len );
  281. return -ERANGE;
  282. }
  283. /* 5. If prediction_resistance_request is set, and
  284. * prediction_resistance_flag is not set, then return an
  285. * ERROR_FLAG.
  286. *
  287. * (Nothing to do since prediction resistance is always
  288. * supported.)
  289. */
  290. /* 6. Clear the reseed_required_flag. */
  291. state->reseed_required = 0;
  292. step_7:
  293. /* 7. If reseed_required_flag is set, or if
  294. * prediction_resistance_request is set, then
  295. */
  296. if ( state->reseed_required || prediction_resist ) {
  297. /* 7.1 status = Reseed_function ( state_handle,
  298. * prediction_resistance_request,
  299. * additional_input )
  300. * 7.2 If status indicates an ERROR, then return
  301. * status.
  302. */
  303. if ( ( rc = drbg_reseed ( state, additional,
  304. additional_len ) ) != 0 ) {
  305. DBGC ( state, "DRBG %p could not reseed: %s\n",
  306. state, strerror ( rc ) );
  307. return rc;
  308. }
  309. /* 7.3 Using state_handle, obtain the new internal
  310. * state.
  311. *
  312. * (Nothing to do since the internal state has been
  313. * updated in-situ.)
  314. */
  315. /* 7.4 additional_input = the Null string. */
  316. additional = NULL;
  317. additional_len = 0;
  318. /* 7.5 Clear the reseed_required_flag. */
  319. state->reseed_required = 0;
  320. }
  321. /* 8. ( status, pseudorandom_bits, new_working_state ) =
  322. * Generate_algorithm ( working_state,
  323. * requested_number_of_bits, additional_input ).
  324. */
  325. rc = drbg_generate_algorithm ( state, additional, additional_len,
  326. data, len );
  327. /* 9. If status indicates that a reseed is required before
  328. * the requested bits can be generated, then
  329. */
  330. if ( rc != 0 ) {
  331. /* 9.1 Set the reseed_required_flag. */
  332. state->reseed_required = 1;
  333. /* 9.2 If the prediction_resistance_flag is set, then
  334. * set the prediction_resistance_request
  335. * indication.
  336. */
  337. prediction_resist = 1;
  338. /* 9.3 Go to step 7. */
  339. goto step_7;
  340. }
  341. /* 10. Replace the old working_state in the internal state
  342. * indicated by state_handle with the values of
  343. * new_working_state.
  344. *
  345. * (Nothing to do since the working state has already been
  346. * updated in-situ.)
  347. */
  348. /* 11. Return SUCCESS and pseudorandom_bits. */
  349. return 0;
  350. }
  351. /**
  352. * Uninstantiate DRBG
  353. *
  354. * @v state Algorithm state
  355. *
  356. * This is the Uninstantiate_function defined in ANS X9.82 Part 3-2007
  357. * Section 9.5 (NIST SP 800-90 Section 9.4).
  358. */
  359. void drbg_uninstantiate ( struct drbg_state *state ) {
  360. DBGC ( state, "DRBG %p uninstantiate\n", state );
  361. /* Sanity checks */
  362. assert ( state != NULL );
  363. /* 1. If state_handle indicates an invalid state, then return
  364. * an ERROR_FLAG.
  365. *
  366. * (Nothing to do since the memory holding the internal state
  367. * was passed in by the caller.)
  368. */
  369. /* 2. Erase the contents of the internal state indicated by
  370. * state_handle.
  371. */
  372. memset ( state, 0, sizeof ( *state ) );
  373. /* 3. Return SUCCESS. */
  374. }