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.

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