iPXE
hmac_drbg.c File Reference

HMAC_DRBG algorithm. More...

#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/hmac.h>
#include <ipxe/hmac_drbg.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
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.
static void hmac_drbg_update_value (struct digest_algorithm *hash, struct hmac_drbg_state *state)
 Update the HMAC_DRBG value.
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.
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.
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.
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.

Detailed Description

HMAC_DRBG algorithm.

This algorithm is designed to comply with ANS X9.82 Part 3-2007 Section 10.2.2.2. This standard is not freely available, but most of the text appears to be shared with NIST SP 800-90, which can be downloaded from

http://csrc.nist.gov/publications/nistpubs/800-90/SP800-90revised_March2007.pdf

Where possible, references are given to both documents. In the case of any disagreement, ANS X9.82 takes priority over NIST SP 800-90. (In particular, note that some algorithms that are Approved by NIST SP 800-90 are not Approved by ANS X9.82.)

Definition in file hmac_drbg.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ hmac_drbg_update_key()

void hmac_drbg_update_key ( struct digest_algorithm * hash,
struct hmac_drbg_state * state,
const void * data,
size_t len,
const uint8_t single )
static

Update the HMAC_DRBG key.

Parameters
hashUnderlying hash algorithm
stateHMAC_DRBG internal state
dataProvided data
lenLength of provided data
singleSingle byte used in concatenation

This function carries out the operation

K = HMAC ( K, V || single || provided_data )

as used by hmac_drbg_update()

Definition at line 79 of file hmac_drbg.c.

82 {
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}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
pseudo_bit_t hash[0x00010]
Definition arbel.h:2
unsigned char uint8_t
Definition stdint.h:10
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint8_t state
State.
Definition eth_slow.h:36
#define DBGC(...)
Definition compiler.h:505
#define DBGC_HDA(...)
Definition compiler.h:506
void hmac_init(struct digest_algorithm *digest, void *ctx, const void *key, size_t key_len)
Initialise HMAC.
Definition hmac.c:58
void hmac_final(struct digest_algorithm *digest, void *ctx, void *hmac)
Finalise HMAC.
Definition hmac.c:88
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

References assert, data, DBGC, DBGC_HDA, hash, hmac_ctxsize(), hmac_final(), hmac_init(), hmac_update(), len, NULL, and state.

Referenced by hmac_drbg_update().

◆ hmac_drbg_update_value()

void hmac_drbg_update_value ( struct digest_algorithm * hash,
struct hmac_drbg_state * state )
static

Update the HMAC_DRBG value.

Parameters
hashUnderlying hash algorithm
stateHMAC_DRBG internal state
dataProvided data
lenLength of provided data
singleSingle byte used in concatenation

This function carries out the operation

V = HMAC ( K, V )

as used by hmac_drbg_update() and hmac_drbg_generate()

Definition at line 122 of file hmac_drbg.c.

123 {
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}

References assert, DBGC, DBGC_HDA, hash, hmac_ctxsize(), hmac_final(), hmac_init(), hmac_update(), NULL, and state.

Referenced by hmac_drbg_generate(), and hmac_drbg_update().

◆ hmac_drbg_update()

void hmac_drbg_update ( struct digest_algorithm * hash,
struct hmac_drbg_state * state,
const void * data,
size_t len )
static

Update HMAC_DRBG internal state.

Parameters
hashUnderlying hash algorithm
stateHMAC_DRBG internal state
dataProvided data
lenLength of provided data

This is the HMAC_DRBG_Update function defined in ANS X9.82 Part 3-2007 Section 10.2.2.2.2 (NIST SP 800-90 Section 10.1.2.2).

The key and value are updated in-place within the HMAC_DRBG internal state.

Definition at line 155 of file hmac_drbg.c.

157 {
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 ) */
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 ) */
178
179 /* 5. V = HMAC ( K, V ) */
181
182 /* 6. Return K and V */
183}
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
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

References assert, data, DBGC, hash, hmac_drbg_update_key(), hmac_drbg_update_value(), len, NULL, and state.

Referenced by hmac_drbg_generate(), and hmac_drbg_reseed().

◆ hmac_drbg_instantiate()

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.

Parameters
hashUnderlying hash algorithm
stateHMAC_DRBG internal state to be initialised
entropyEntropy input
entropy_lenLength of entropy input
personalPersonalisation string
personal_lenLength of personalisation string

This is the HMAC_DRBG_Instantiate_algorithm function defined in ANS X9.82 Part 3-2007 Section 10.2.2.2.3 (NIST SP 800-90 Section 10.1.2.3).

The nonce must be included within the entropy input (i.e. the entropy input must contain at least 3/2 * security_strength bits of entropy, as per ANS X9.82 Part 3-2007 Section 8.4.2 (NIST SP 800-90 Section 8.6.7).

The key, value and reseed counter are updated in-place within the HMAC_DRBG internal state.

Definition at line 207 of file hmac_drbg.c.

210 {
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}
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
void * memset(void *dest, int character, size_t len) __nonnull

References assert, DBGC, hash, hmac_drbg_reseed(), memset(), NULL, and state.

Referenced by drbg_instantiate_algorithm(), and ecdsa_sign().

◆ hmac_drbg_reseed()

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.

Parameters
hashUnderlying hash algorithm
stateHMAC_DRBG internal state
entropyEntropy input
entropy_lenLength of entropy input
additionalAdditional input
additional_lenLength of additional input

This is the HMAC_DRBG_Reseed_algorithm function defined in ANS X9.82 Part 3-2007 Section 10.2.2.2.4 (NIST SP 800-90 Section 10.1.2.4).

The key, value and reseed counter are updated in-place within the HMAC_DRBG internal state.

Definition at line 256 of file hmac_drbg.c.

259 {
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}
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
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint16_t additional
Additional sense code and qualifier.
Definition scsi.h:13

References additional, assert, DBGC, DBGC_HDA, hash, hmac_drbg_update(), memcpy(), NULL, and state.

Referenced by drbg_reseed_algorithm(), and hmac_drbg_instantiate().

◆ hmac_drbg_generate()

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.

Parameters
hashUnderlying hash algorithm
stateHMAC_DRBG internal state
additionalAdditional input
additional_lenLength of additional input
dataOutput buffer
lenLength of output buffer
Return values
rcReturn status code

This is the HMAC_DRBG_Generate_algorithm function defined in ANS X9.82 Part 3-2007 Section 10.2.2.2.5 (NIST SP 800-90 Section 10.1.2.5).

Requests must be for an integral number of bytes.

The key, value and reseed counter are updated in-place within the HMAC_DRBG internal state.

Note that the only permitted error is "reseed required".

Definition at line 307 of file hmac_drbg.c.

310 {
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}
#define ESTALE
Stale file handle.
Definition errno.h:660
#define HMAC_DRBG_RESEED_INTERVAL
Reseed interval.
Definition hmac_drbg.h:207

References additional, assert, data, DBGC, DBGC_HDA, ESTALE, hash, HMAC_DRBG_RESEED_INTERVAL, hmac_drbg_update(), hmac_drbg_update_value(), len, memcpy(), NULL, and state.

Referenced by drbg_generate_algorithm(), and ecdsa_sign_rs().