diff options
author | Michael Brown <mcb30@ipxe.org> | 2012-03-06 12:58:56 +0000 |
---|---|---|
committer | Michael Brown <mcb30@ipxe.org> | 2012-03-06 13:12:30 +0000 |
commit | b9d9c3f1d5b0a92cd7c07df1c180f915ad2e6018 (patch) | |
tree | ddb8ce9a5f466db5ecb13f70d5d695caf4ec3f34 /src/crypto/hmac_drbg.c | |
parent | 742e43be05d7525135b81a8bcde44083aa1a0ecd (diff) | |
download | ipxe-b9d9c3f1d5b0a92cd7c07df1c180f915ad2e6018.tar.gz |
[rng] Allow HMAC_DRBG to use multiple underlying hash algorithms
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/crypto/hmac_drbg.c')
-rw-r--r-- | src/crypto/hmac_drbg.c | 132 |
1 files changed, 73 insertions, 59 deletions
diff --git a/src/crypto/hmac_drbg.c b/src/crypto/hmac_drbg.c index 9e70e4e6f..3f56e1b73 100644 --- a/src/crypto/hmac_drbg.c +++ b/src/crypto/hmac_drbg.c @@ -46,6 +46,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); /** * Update the HMAC_DRBG key * + * @v hash Underlying hash algorithm * @v state HMAC_DRBG internal state * @v data Provided data * @v len Length of provided data @@ -57,40 +58,40 @@ FILE_LICENCE ( GPL2_OR_LATER ); * * as used by hmac_drbg_update() */ -static void hmac_drbg_update_key ( struct hmac_drbg_state *state, +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 ) { - uint8_t context[HMAC_DRBG_CTX_SIZE]; - size_t key_len = sizeof ( state->key ); + uint8_t context[ hash->ctxsize ]; + size_t out_len = hash->digestsize; - DBGC ( state, "HMAC_DRBG %p provided data :\n", state ); + DBGC ( state, "HMAC_DRBG_%s %p provided data :\n", hash->name, state ); DBGC_HDA ( state, 0, data, len ); /* Sanity checks */ + assert ( hash != NULL ); assert ( state != NULL ); assert ( ( data != NULL ) || ( len == 0 ) ); assert ( ( single == 0x00 ) || ( single == 0x01 ) ); /* K = HMAC ( K, V || single || provided_data ) */ - hmac_init ( &hmac_drbg_algorithm, context, state->key, &key_len ); - assert ( key_len == sizeof ( state->key ) ); - hmac_update ( &hmac_drbg_algorithm, context, - state->value, sizeof ( state->value ) ); - hmac_update ( &hmac_drbg_algorithm, context, - &single, sizeof ( single ) ); - hmac_update ( &hmac_drbg_algorithm, context, data, len ); - hmac_final ( &hmac_drbg_algorithm, context, state->key, &key_len, - state->key ); - assert ( key_len == sizeof ( state->key ) ); - - DBGC ( state, "HMAC_DRBG %p K = HMAC ( K, V || %#02x || " - "provided_data ) :\n", state, single ); - DBGC_HDA ( state, 0, state->key, sizeof ( state->key ) ); + hmac_init ( hash, context, state->key, &out_len ); + assert ( out_len == hash->digestsize ); + hmac_update ( hash, context, state->value, out_len ); + hmac_update ( hash, context, &single, sizeof ( single ) ); + hmac_update ( hash, context, data, len ); + hmac_final ( hash, context, state->key, &out_len, state->key ); + assert ( out_len == hash->digestsize ); + + DBGC ( state, "HMAC_DRBG_%s %p K = HMAC ( K, V || %#02x || " + "provided_data ) :\n", hash->name, state, single ); + DBGC_HDA ( state, 0, state->key, out_len ); } /** * Update the HMAC_DRBG value * + * @v hash Underlying hash algorithm * @v state HMAC_DRBG internal state * @v data Provided data * @v len Length of provided data @@ -102,29 +103,31 @@ static void hmac_drbg_update_key ( struct hmac_drbg_state *state, * * as used by hmac_drbg_update() and hmac_drbg_generate() */ -static void hmac_drbg_update_value ( struct hmac_drbg_state *state ) { - uint8_t context[HMAC_DRBG_CTX_SIZE]; - size_t key_len = sizeof ( state->key ); +static void hmac_drbg_update_value ( struct digest_algorithm *hash, + struct hmac_drbg_state *state ) { + uint8_t context[ hash->ctxsize ]; + size_t out_len = hash->digestsize; /* Sanity checks */ + assert ( hash != NULL ); assert ( state != NULL ); /* V = HMAC ( K, V ) */ - hmac_init ( &hmac_drbg_algorithm, context, state->key, &key_len ); - assert ( key_len == sizeof ( state->key ) ); - hmac_update ( &hmac_drbg_algorithm, context, - state->value, sizeof ( state->value ) ); - hmac_final ( &hmac_drbg_algorithm, context, state->key, &key_len, - state->value ); - assert ( key_len == sizeof ( state->key ) ); - - DBGC ( state, "HMAC_DRBG %p V = HMAC ( K, V ) :\n", state ); - DBGC_HDA ( state, 0, state->value, sizeof ( state->value ) ); + hmac_init ( hash, context, state->key, &out_len ); + assert ( out_len == hash->digestsize ); + hmac_update ( hash, context, state->value, out_len ); + hmac_final ( hash, context, state->key, &out_len, state->value ); + assert ( out_len == hash->digestsize ); + + DBGC ( state, "HMAC_DRBG_%s %p V = HMAC ( K, V ) :\n", + hash->name, state ); + DBGC_HDA ( state, 0, state->value, out_len ); } /** * Update HMAC_DRBG internal state * + * @v hash Underlying hash algorithm * @v state HMAC_DRBG internal state * @v data Provided data * @v len Length of provided data @@ -135,30 +138,32 @@ static void hmac_drbg_update_value ( struct hmac_drbg_state *state ) { * The key and value are updated in-place within the HMAC_DRBG * internal state. */ -static void hmac_drbg_update ( struct hmac_drbg_state *state, +static void hmac_drbg_update ( struct digest_algorithm *hash, + struct hmac_drbg_state *state, const void *data, size_t len ) { - DBGC ( state, "HMAC_DRBG %p update\n", state ); + DBGC ( state, "HMAC_DRBG_%s %p update\n", hash->name, state ); /* Sanity checks */ + assert ( hash != NULL ); assert ( state != NULL ); assert ( ( data != NULL ) || ( len == 0 ) ); /* 1. K = HMAC ( K, V || 0x00 || provided_data ) */ - hmac_drbg_update_key ( state, data, len, 0x00 ); + hmac_drbg_update_key ( hash, state, data, len, 0x00 ); /* 2. V = HMAC ( K, V ) */ - hmac_drbg_update_value ( state ); + hmac_drbg_update_value ( hash, state ); /* 3. If ( provided_data = Null ), then return K and V */ if ( ! len ) return; /* 4. K = HMAC ( K, V || 0x01 || provided_data ) */ - hmac_drbg_update_key ( state, data, len, 0x01 ); + hmac_drbg_update_key ( hash, state, data, len, 0x01 ); /* 5. V = HMAC ( K, V ) */ - hmac_drbg_update_value ( state ); + hmac_drbg_update_value ( hash, state ); /* 6. Return K and V */ } @@ -166,6 +171,7 @@ static void hmac_drbg_update ( struct hmac_drbg_state *state, /** * Instantiate HMAC_DRBG * + * @v hash Underlying hash algorithm * @v state HMAC_DRBG internal state to be initialised * @v entropy Entropy input * @v entropy_len Length of entropy input @@ -184,17 +190,18 @@ static void hmac_drbg_update ( struct hmac_drbg_state *state, * The key, value and reseed counter are updated in-place within the * HMAC_DRBG internal state. */ -void hmac_drbg_instantiate ( struct hmac_drbg_state *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 ){ + size_t out_len = hash->digestsize; - DBGC ( state, "HMAC_DRBG %p instantiate\n", state ); + DBGC ( state, "HMAC_DRBG_%s %p instantiate\n", hash->name, state ); /* Sanity checks */ + assert ( hash != NULL ); assert ( state != NULL ); assert ( entropy != NULL ); - assert ( ( 8 * entropy_len ) >= - ( 3 * HMAC_DRBG_SECURITY_STRENGTH / 2 ) ); assert ( ( personal != NULL ) || ( personal_len == 0 ) ); /* 1. seed_material = entropy_input || nonce || @@ -202,23 +209,24 @@ void hmac_drbg_instantiate ( struct hmac_drbg_state *state, */ /* 2. Key = 0x00 00..00 */ - memset ( state->key, 0x00, sizeof ( state->key ) ); + memset ( state->key, 0x00, out_len ); /* 3. V = 0x01 01...01 */ - memset ( state->value, 0x01, sizeof ( state->value ) ); + memset ( state->value, 0x01, out_len ); /* 4. ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V ) * 5. reseed_counter = 1 * 6. Return V, Key and reseed_counter as the * initial_working_state */ - hmac_drbg_reseed ( state, entropy, entropy_len, + hmac_drbg_reseed ( hash, state, entropy, entropy_len, personal, personal_len ); } /** * Reseed HMAC_DRBG * + * @v hash Underlying hash algorithm * @v state HMAC_DRBG internal state * @v entropy Entropy input * @v entropy_len Length of entropy input @@ -231,27 +239,29 @@ void hmac_drbg_instantiate ( struct hmac_drbg_state *state, * The key, value and reseed counter are updated in-place within the * HMAC_DRBG internal state. */ -void hmac_drbg_reseed ( struct hmac_drbg_state *state, +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 ) { uint8_t seed_material[ entropy_len + additional_len ]; - DBGC ( state, "HMAC_DRBG %p (re)seed\n", state ); + DBGC ( state, "HMAC_DRBG_%s %p (re)seed\n", hash->name, state ); /* Sanity checks */ + assert ( hash != NULL ); assert ( state != NULL ); assert ( entropy != NULL ); - assert ( ( 8 * entropy_len ) >= HMAC_DRBG_SECURITY_STRENGTH ); assert ( ( additional != NULL ) || ( additional_len == 0 ) ); /* 1. seed_material = entropy_input || additional_input */ memcpy ( seed_material, entropy, entropy_len ); memcpy ( ( seed_material + entropy_len ), additional, additional_len ); - DBGC ( state, "HMAC_DRBG %p seed material :\n", state ); + DBGC ( state, "HMAC_DRBG_%s %p seed material :\n", hash->name, state ); DBGC_HDA ( state, 0, seed_material, sizeof ( seed_material ) ); /* 2. ( Key, V ) = HMAC_DBRG_Update ( seed_material, Key, V ) */ - hmac_drbg_update ( state, seed_material, sizeof ( seed_material ) ); + hmac_drbg_update ( hash, state, seed_material, + sizeof ( seed_material ) ); /* 3. reseed_counter = 1 */ state->reseed_counter = 1; @@ -262,6 +272,7 @@ void hmac_drbg_reseed ( struct hmac_drbg_state *state, /** * Generate pseudorandom bits using HMAC_DRBG * + * @v hash Underlying hash algorithm * @v state HMAC_DRBG internal state * @v additional Additional input * @v additional_len Length of additional input @@ -279,16 +290,19 @@ void hmac_drbg_reseed ( struct hmac_drbg_state *state, * * Note that the only permitted error is "reseed required". */ -int hmac_drbg_generate ( struct hmac_drbg_state *state, +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 ) { + size_t out_len = hash->digestsize; void *orig_data = data; size_t orig_len = len; size_t frag_len; - DBGC ( state, "HMAC_DRBG %p generate\n", state ); + DBGC ( state, "HMAC_DRBG_%s %p generate\n", hash->name, state ); /* Sanity checks */ + assert ( hash != NULL ); assert ( state != NULL ); assert ( data != NULL ); assert ( ( additional != NULL ) || ( additional_len == 0 ) ); @@ -297,8 +311,8 @@ int hmac_drbg_generate ( struct hmac_drbg_state *state, * indication that a reseed is required */ if ( state->reseed_counter > HMAC_DRBG_RESEED_INTERVAL ) { - DBGC ( state, "HMAC_DRBG %p reseed interval exceeded\n", - state ); + DBGC ( state, "HMAC_DRBG_%s %p reseed interval exceeded\n", + hash->name, state ); return -ESTALE; } @@ -306,7 +320,7 @@ int hmac_drbg_generate ( struct hmac_drbg_state *state, * ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V ) */ if ( additional_len ) - hmac_drbg_update ( state, additional, additional_len ); + hmac_drbg_update ( hash, state, additional, additional_len ); /* 3. temp = Null * 4. While ( len ( temp ) < requested_number_of_bits ) do: @@ -314,27 +328,27 @@ int hmac_drbg_generate ( struct hmac_drbg_state *state, while ( len ) { /* 4.1 V = HMAC ( Key, V ) */ - hmac_drbg_update_value ( state ); + hmac_drbg_update_value ( hash, state ); /* 4.2. temp = temp || V * 5. returned_bits = Leftmost requested_number_of_bits * of temp */ frag_len = len; - if ( frag_len > sizeof ( state->value ) ) - frag_len = sizeof ( state->value ); + if ( frag_len > out_len ) + frag_len = out_len; memcpy ( data, state->value, frag_len ); data += frag_len; len -= frag_len; } /* 6. ( Key, V ) = HMAC_DRBG_Update ( additional_input, Key, V ) */ - hmac_drbg_update ( state, additional, additional_len ); + hmac_drbg_update ( hash, state, additional, additional_len ); /* 7. reseed_counter = reseed_counter + 1 */ state->reseed_counter++; - DBGC ( state, "HMAC_DRBG %p generated :\n", state ); + DBGC ( state, "HMAC_DRBG_%s %p generated :\n", hash->name, state ); DBGC_HDA ( state, 0, orig_data, orig_len ); /* 8. Return SUCCESS, returned_bits, and the new values of |