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.

drbg.c 13KB

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