iPXE
hmac_drbg.c
Go to the documentation of this file.
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 
36 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
37 FILE_SECBOOT ( PERMITTED );
38 
39 /** @file
40  *
41  * HMAC_DRBG algorithm
42  *
43  * This algorithm is designed to comply with ANS X9.82 Part 3-2007
44  * Section 10.2.2.2. This standard is not freely available, but most
45  * of the text appears to be shared with NIST SP 800-90, which can be
46  * downloaded from
47  *
48  * http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf
49  *
50  * Where possible, references are given to both documents. In the
51  * case of any disagreement, ANS X9.82 takes priority over NIST SP
52  * 800-90. (In particular, note that some algorithms that are
53  * Approved by NIST SP 800-90 are not Approved by ANS X9.82.)
54  */
55 
56 #include <stdint.h>
57 #include <string.h>
58 #include <errno.h>
59 #include <assert.h>
60 #include <ipxe/crypto.h>
61 #include <ipxe/hmac.h>
62 #include <ipxe/hmac_drbg.h>
63 
64 /**
65  * Update the HMAC_DRBG key
66  *
67  * @v hash Underlying hash algorithm
68  * @v state HMAC_DRBG internal state
69  * @v data Provided data
70  * @v len Length of provided data
71  * @v single Single byte used in concatenation
72  *
73  * This function carries out the operation
74  *
75  * K = HMAC ( K, V || single || provided_data )
76  *
77  * as used by hmac_drbg_update()
78  */
80  struct hmac_drbg_state *state,
81  const void *data, size_t len,
82  const uint8_t single ) {
83  uint8_t context[ hmac_ctxsize ( hash ) ];
84  size_t out_len = hash->digestsize;
85 
86  DBGC ( state, "HMAC_DRBG_%s %p provided data :\n", hash->name, state );
87  DBGC_HDA ( state, 0, data, len );
88 
89  /* Sanity checks */
90  assert ( hash != NULL );
91  assert ( state != NULL );
92  assert ( ( data != NULL ) || ( len == 0 ) );
93  assert ( ( single == 0x00 ) || ( single == 0x01 ) );
94 
95  /* K = HMAC ( K, V || single || provided_data ) */
96  hmac_init ( hash, context, state->key, out_len );
97  hmac_update ( hash, context, state->value, out_len );
98  hmac_update ( hash, context, &single, sizeof ( single ) );
99  hmac_update ( hash, context, data, len );
100  hmac_final ( hash, context, state->key );
101 
102  DBGC ( state, "HMAC_DRBG_%s %p K = HMAC ( K, V || %#02x || "
103  "provided_data ) :\n", hash->name, state, single );
104  DBGC_HDA ( state, 0, state->key, out_len );
105 }
106 
107 /**
108  * Update the HMAC_DRBG value
109  *
110  * @v hash Underlying hash algorithm
111  * @v state HMAC_DRBG internal state
112  * @v data Provided data
113  * @v len Length of provided data
114  * @v single Single byte used in concatenation
115  *
116  * This function carries out the operation
117  *
118  * V = HMAC ( K, V )
119  *
120  * as used by hmac_drbg_update() and hmac_drbg_generate()
121  */
123  struct hmac_drbg_state *state ) {
124  uint8_t context[ hmac_ctxsize ( hash ) ];
125  size_t out_len = hash->digestsize;
126 
127  /* Sanity checks */
128  assert ( hash != NULL );
129  assert ( state != NULL );
130 
131  /* V = HMAC ( K, V ) */
132  hmac_init ( hash, context, state->key, out_len );
133  hmac_update ( hash, context, state->value, out_len );
134  hmac_final ( hash, context, state->value );
135 
136  DBGC ( state, "HMAC_DRBG_%s %p V = HMAC ( K, V ) :\n",
137  hash->name, state );
138  DBGC_HDA ( state, 0, state->value, out_len );
139 }
140 
141 /**
142  * Update HMAC_DRBG internal state
143  *
144  * @v hash Underlying hash algorithm
145  * @v state HMAC_DRBG internal state
146  * @v data Provided data
147  * @v len Length of provided data
148  *
149  * This is the HMAC_DRBG_Update function defined in ANS X9.82 Part
150  * 3-2007 Section 10.2.2.2.2 (NIST SP 800-90 Section 10.1.2.2).
151  *
152  * The key and value are updated in-place within the HMAC_DRBG
153  * internal state.
154  */
155 static void hmac_drbg_update ( struct digest_algorithm *hash,
156  struct hmac_drbg_state *state,
157  const void *data, size_t len ) {
158 
159  DBGC ( state, "HMAC_DRBG_%s %p update\n", hash->name, state );
160 
161  /* Sanity checks */
162  assert ( hash != NULL );
163  assert ( state != NULL );
164  assert ( ( data != NULL ) || ( len == 0 ) );
165 
166  /* 1. K = HMAC ( K, V || 0x00 || provided_data ) */
167  hmac_drbg_update_key ( hash, state, data, len, 0x00 );
168 
169  /* 2. V = HMAC ( K, V ) */
171 
172  /* 3. If ( provided_data = Null ), then return K and V */
173  if ( ! len )
174  return;
175 
176  /* 4. K = HMAC ( K, V || 0x01 || provided_data ) */
177  hmac_drbg_update_key ( hash, state, data, len, 0x01 );
178 
179  /* 5. V = HMAC ( K, V ) */
181 
182  /* 6. Return K and V */
183 }
184 
185 /**
186  * Instantiate HMAC_DRBG
187  *
188  * @v hash Underlying hash algorithm
189  * @v state HMAC_DRBG internal state to be initialised
190  * @v entropy Entropy input
191  * @v entropy_len Length of entropy input
192  * @v personal Personalisation string
193  * @v personal_len Length of personalisation string
194  *
195  * This is the HMAC_DRBG_Instantiate_algorithm function defined in ANS
196  * X9.82 Part 3-2007 Section 10.2.2.2.3 (NIST SP 800-90 Section
197  * 10.1.2.3).
198  *
199  * The nonce must be included within the entropy input (i.e. the
200  * entropy input must contain at least 3/2 * security_strength bits of
201  * entropy, as per ANS X9.82 Part 3-2007 Section 8.4.2 (NIST SP 800-90
202  * Section 8.6.7).
203  *
204  * The key, value and reseed counter are updated in-place within the
205  * HMAC_DRBG internal state.
206  */
208  struct hmac_drbg_state *state,
209  const void *entropy, size_t entropy_len,
210  const void *personal, size_t personal_len ){
211  size_t out_len = hash->digestsize;
212 
213  DBGC ( state, "HMAC_DRBG_%s %p instantiate\n", hash->name, state );
214 
215  /* Sanity checks */
216  assert ( hash != NULL );
217  assert ( state != NULL );
218  assert ( entropy != NULL );
219  assert ( ( personal != NULL ) || ( personal_len == 0 ) );
220 
221  /* 1. seed_material = entropy_input || nonce ||
222  * personalisation_string
223  */
224 
225  /* 2. Key = 0x00 00..00 */
226  memset ( state->key, 0x00, out_len );
227 
228  /* 3. V = 0x01 01...01 */
229  memset ( state->value, 0x01, out_len );
230 
231  /* 4. ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V )
232  * 5. reseed_counter = 1
233  * 6. Return V, Key and reseed_counter as the
234  * initial_working_state
235  */
236  hmac_drbg_reseed ( hash, state, entropy, entropy_len,
237  personal, personal_len );
238 }
239 
240 /**
241  * Reseed HMAC_DRBG
242  *
243  * @v hash Underlying hash algorithm
244  * @v state HMAC_DRBG internal state
245  * @v entropy Entropy input
246  * @v entropy_len Length of entropy input
247  * @v additional Additional input
248  * @v additional_len Length of additional input
249  *
250  * This is the HMAC_DRBG_Reseed_algorithm function defined in ANS X9.82
251  * Part 3-2007 Section 10.2.2.2.4 (NIST SP 800-90 Section 10.1.2.4).
252  *
253  * The key, value and reseed counter are updated in-place within the
254  * HMAC_DRBG internal state.
255  */
257  struct hmac_drbg_state *state,
258  const void *entropy, size_t entropy_len,
259  const void *additional, size_t additional_len ) {
260  uint8_t seed_material[ entropy_len + additional_len ];
261 
262  DBGC ( state, "HMAC_DRBG_%s %p (re)seed\n", hash->name, state );
263 
264  /* Sanity checks */
265  assert ( hash != NULL );
266  assert ( state != NULL );
267  assert ( entropy != NULL );
268  assert ( ( additional != NULL ) || ( additional_len == 0 ) );
269 
270  /* 1. seed_material = entropy_input || additional_input */
271  memcpy ( seed_material, entropy, entropy_len );
272  memcpy ( ( seed_material + entropy_len ), additional, additional_len );
273  DBGC ( state, "HMAC_DRBG_%s %p seed material :\n", hash->name, state );
274  DBGC_HDA ( state, 0, seed_material, sizeof ( seed_material ) );
275 
276  /* 2. ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V ) */
277  hmac_drbg_update ( hash, state, seed_material,
278  sizeof ( seed_material ) );
279 
280  /* 3. reseed_counter = 1 */
281  state->reseed_counter = 1;
282 
283  /* 4. Return V, Key and reseed_counter as the new_working_state */
284 }
285 
286 /**
287  * Generate pseudorandom bits using HMAC_DRBG
288  *
289  * @v hash Underlying hash algorithm
290  * @v state HMAC_DRBG internal state
291  * @v additional Additional input
292  * @v additional_len Length of additional input
293  * @v data Output buffer
294  * @v len Length of output buffer
295  * @ret rc Return status code
296  *
297  * This is the HMAC_DRBG_Generate_algorithm function defined in ANS X9.82
298  * Part 3-2007 Section 10.2.2.2.5 (NIST SP 800-90 Section 10.1.2.5).
299  *
300  * Requests must be for an integral number of bytes.
301  *
302  * The key, value and reseed counter are updated in-place within the
303  * HMAC_DRBG internal state.
304  *
305  * Note that the only permitted error is "reseed required".
306  */
308  struct hmac_drbg_state *state,
309  const void *additional, size_t additional_len,
310  void *data, size_t len ) {
311  size_t out_len = hash->digestsize;
312  void *orig_data = data;
313  size_t orig_len = len;
314  size_t frag_len;
315 
316  DBGC ( state, "HMAC_DRBG_%s %p generate\n", hash->name, state );
317 
318  /* Sanity checks */
319  assert ( hash != NULL );
320  assert ( state != NULL );
321  assert ( data != NULL );
322  assert ( ( additional != NULL ) || ( additional_len == 0 ) );
323 
324  /* 1. If reseed_counter > reseed_interval, then return an
325  * indication that a reseed is required
326  */
327  if ( state->reseed_counter > HMAC_DRBG_RESEED_INTERVAL ) {
328  DBGC ( state, "HMAC_DRBG_%s %p reseed interval exceeded\n",
329  hash->name, state );
330  return -ESTALE;
331  }
332 
333  /* 2. If additional_input != Null, then
334  * ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V )
335  */
336  if ( additional_len )
337  hmac_drbg_update ( hash, state, additional, additional_len );
338 
339  /* 3. temp = Null
340  * 4. While ( len ( temp ) < requested_number_of_bits ) do:
341  */
342  while ( len ) {
343 
344  /* 4.1 V = HMAC ( Key, V ) */
346 
347  /* 4.2. temp = temp || V
348  * 5. returned_bits = Leftmost requested_number_of_bits
349  * of temp
350  */
351  frag_len = len;
352  if ( frag_len > out_len )
353  frag_len = out_len;
354  memcpy ( data, state->value, frag_len );
355  data += frag_len;
356  len -= frag_len;
357  }
358 
359  /* 6. ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V ) */
360  hmac_drbg_update ( hash, state, additional, additional_len );
361 
362  /* 7. reseed_counter = reseed_counter + 1 */
363  state->reseed_counter++;
364 
365  DBGC ( state, "HMAC_DRBG_%s %p generated :\n", hash->name, state );
366  DBGC_HDA ( state, 0, orig_data, orig_len );
367 
368  /* 8. Return SUCCESS, returned_bits, and the new values of
369  * Key, V and reseed_counter as the new_working_state
370  */
371  return 0;
372 }
int hmac_drbg_generate(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *additional, size_t additional_len, void *data, size_t len)
Generate pseudorandom bits using HMAC_DRBG.
Definition: hmac_drbg.c:307
void hmac_init(struct digest_algorithm *digest, void *ctx, const void *key, size_t key_len)
Initialise HMAC.
Definition: hmac.c:58
pseudo_bit_t hash[0x00010]
Definition: arbel.h:13
uint8_t state
State.
Definition: eth_slow.h:48
Error codes.
void hmac_drbg_instantiate(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *entropy, size_t entropy_len, const void *personal, size_t personal_len)
Instantiate HMAC_DRBG.
Definition: hmac_drbg.c:207
#define DBGC(...)
Definition: compiler.h:505
void hmac_drbg_reseed(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *entropy, size_t entropy_len, const void *additional, size_t additional_len)
Reseed HMAC_DRBG.
Definition: hmac_drbg.c:256
Cryptographic API.
static void hmac_drbg_update_key(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *data, size_t len, const uint8_t single)
Update the HMAC_DRBG key.
Definition: hmac_drbg.c:79
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
uint16_t additional
Additional sense code and qualifier.
Definition: scsi.h:28
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
Keyed-Hashing for Message Authentication.
#define DBGC_HDA(...)
Definition: compiler.h:506
ring len
Length.
Definition: dwmac.h:231
static void hmac_drbg_update_value(struct digest_algorithm *hash, struct hmac_drbg_state *state)
Update the HMAC_DRBG value.
Definition: hmac_drbg.c:122
#define HMAC_DRBG_RESEED_INTERVAL
Reseed interval.
Definition: hmac_drbg.h:207
static void hmac_update(struct digest_algorithm *digest, void *ctx, const void *data, size_t len)
Update HMAC.
Definition: hmac.h:43
static size_t hmac_ctxsize(struct digest_algorithm *digest)
Calculate HMAC context size.
Definition: hmac.h:29
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned char uint8_t
Definition: stdint.h:10
FILE_SECBOOT(PERMITTED)
HMAC_DRBG internal state.
Definition: hmac_drbg.h:219
A message digest algorithm.
Definition: crypto.h:19
uint8_t data[48]
Additional event data.
Definition: ena.h:22
void hmac_final(struct digest_algorithm *digest, void *ctx, void *hmac)
Finalise HMAC.
Definition: hmac.c:88
static void hmac_drbg_update(struct digest_algorithm *hash, struct hmac_drbg_state *state, const void *data, size_t len)
Update HMAC_DRBG internal state.
Definition: hmac_drbg.c:155
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
#define ESTALE
Stale file handle.
Definition: errno.h:660
void * memset(void *dest, int character, size_t len) __nonnull
HMAC_DRBG algorithm.