mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-10 06:44:28 +00:00
Merge pull request #1156 from Ryan-Everett-arm/key-derivation-buffer-protection
Add buffer copying to the Key Derivation API
This commit is contained in:
commit
b539126670
@ -5854,10 +5854,12 @@ static psa_status_t psa_key_derivation_pbkdf2_read(
|
||||
|
||||
psa_status_t psa_key_derivation_output_bytes(
|
||||
psa_key_derivation_operation_t *operation,
|
||||
uint8_t *output,
|
||||
uint8_t *output_external,
|
||||
size_t output_length)
|
||||
{
|
||||
psa_status_t status;
|
||||
LOCAL_OUTPUT_DECLARE(output_external, output);
|
||||
|
||||
psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg(operation);
|
||||
|
||||
if (operation->alg == 0) {
|
||||
@ -5865,13 +5867,6 @@ psa_status_t psa_key_derivation_output_bytes(
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
}
|
||||
|
||||
if (output_length > operation->capacity) {
|
||||
operation->capacity = 0;
|
||||
/* Go through the error path to wipe all confidential data now
|
||||
* that the operation object is useless. */
|
||||
status = PSA_ERROR_INSUFFICIENT_DATA;
|
||||
goto exit;
|
||||
}
|
||||
if (output_length == 0 && operation->capacity == 0) {
|
||||
/* Edge case: this is a finished operation, and 0 bytes
|
||||
* were requested. The right error in this case could
|
||||
@ -5881,6 +5876,16 @@ psa_status_t psa_key_derivation_output_bytes(
|
||||
* output_length > 0. */
|
||||
return PSA_ERROR_INSUFFICIENT_DATA;
|
||||
}
|
||||
|
||||
LOCAL_OUTPUT_ALLOC(output_external, output_length, output);
|
||||
if (output_length > operation->capacity) {
|
||||
operation->capacity = 0;
|
||||
/* Go through the error path to wipe all confidential data now
|
||||
* that the operation object is useless. */
|
||||
status = PSA_ERROR_INSUFFICIENT_DATA;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
operation->capacity -= output_length;
|
||||
|
||||
#if defined(BUILTIN_ALG_ANY_HKDF)
|
||||
@ -5914,7 +5919,10 @@ psa_status_t psa_key_derivation_output_bytes(
|
||||
|
||||
{
|
||||
(void) kdf_alg;
|
||||
return PSA_ERROR_BAD_STATE;
|
||||
status = PSA_ERROR_BAD_STATE;
|
||||
LOCAL_OUTPUT_FREE(output_external, output);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
exit:
|
||||
@ -5926,8 +5934,12 @@ exit:
|
||||
psa_algorithm_t alg = operation->alg;
|
||||
psa_key_derivation_abort(operation);
|
||||
operation->alg = alg;
|
||||
memset(output, '!', output_length);
|
||||
if (output != NULL) {
|
||||
memset(output, '!', output_length);
|
||||
}
|
||||
}
|
||||
|
||||
LOCAL_OUTPUT_FREE(output_external, output);
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -6907,12 +6919,12 @@ static psa_status_t psa_pbkdf2_hmac_set_password(psa_algorithm_t hash_alg,
|
||||
{
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
if (input_len > PSA_HASH_BLOCK_LENGTH(hash_alg)) {
|
||||
status = psa_hash_compute(hash_alg, input, input_len, output,
|
||||
PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
|
||||
} else {
|
||||
return psa_hash_compute(hash_alg, input, input_len, output,
|
||||
PSA_HMAC_MAX_HASH_BLOCK_SIZE, output_len);
|
||||
} else if (input_len > 0) {
|
||||
memcpy(output, input, input_len);
|
||||
*output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
|
||||
}
|
||||
*output_len = PSA_HASH_BLOCK_LENGTH(hash_alg);
|
||||
return status;
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_BUILTIN_ALG_PBKDF2_HMAC */
|
||||
@ -7146,12 +7158,22 @@ static psa_status_t psa_key_derivation_input_integer_internal(
|
||||
psa_status_t psa_key_derivation_input_bytes(
|
||||
psa_key_derivation_operation_t *operation,
|
||||
psa_key_derivation_step_t step,
|
||||
const uint8_t *data,
|
||||
const uint8_t *data_external,
|
||||
size_t data_length)
|
||||
{
|
||||
return psa_key_derivation_input_internal(operation, step,
|
||||
PSA_KEY_TYPE_NONE,
|
||||
data, data_length);
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
LOCAL_INPUT_DECLARE(data_external, data);
|
||||
|
||||
LOCAL_INPUT_ALLOC(data_external, data_length, data);
|
||||
|
||||
status = psa_key_derivation_input_internal(operation, step,
|
||||
PSA_KEY_TYPE_NONE,
|
||||
data, data_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
exit:
|
||||
#endif
|
||||
LOCAL_INPUT_FREE(data_external, data);
|
||||
return status;
|
||||
}
|
||||
|
||||
psa_status_t psa_key_derivation_input_integer(
|
||||
|
@ -145,6 +145,9 @@ class PSAWrapperGenerator(c_wrapper_generator.Base):
|
||||
# Proof-of-concept: just instrument one function for now
|
||||
if function_name == 'psa_cipher_encrypt':
|
||||
return True
|
||||
if function_name in ('psa_key_derivation_output_bytes',
|
||||
'psa_key_derivation_input_bytes'):
|
||||
return True
|
||||
if function_name in ('psa_import_key',
|
||||
'psa_export_key',
|
||||
'psa_export_public_key'):
|
||||
|
@ -646,7 +646,13 @@ psa_status_t mbedtls_test_wrap_psa_key_derivation_input_bytes(
|
||||
const uint8_t *arg2_data,
|
||||
size_t arg3_data_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg2_data, arg3_data_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_key_derivation_input_bytes)(arg0_operation, arg1_step, arg2_data, arg3_data_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg2_data, arg3_data_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
@ -688,7 +694,13 @@ psa_status_t mbedtls_test_wrap_psa_key_derivation_output_bytes(
|
||||
uint8_t *arg1_output,
|
||||
size_t arg2_output_length)
|
||||
{
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_POISON(arg1_output, arg2_output_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
psa_status_t status = (psa_key_derivation_output_bytes)(arg0_operation, arg1_output, arg2_output_length);
|
||||
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
|
||||
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_output, arg2_output_length);
|
||||
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
|
||||
return status;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user