Merge pull request #1137 from Ryan-Everett-arm/key-management-buffer-protection

Add buffer copying to the Key Management API
This commit is contained in:
David Horstmann 2024-02-02 17:43:14 +00:00 committed by GitHub
commit 52ff236a98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 11 deletions

View File

@ -1599,13 +1599,14 @@ psa_status_t psa_export_key_internal(
} }
psa_status_t psa_export_key(mbedtls_svc_key_id_t key, psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
uint8_t *data, uint8_t *data_external,
size_t data_size, size_t data_size,
size_t *data_length) size_t *data_length)
{ {
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
psa_key_slot_t *slot; psa_key_slot_t *slot;
LOCAL_OUTPUT_DECLARE(data_external, data);
/* Reject a zero-length output buffer now, since this can never be a /* Reject a zero-length output buffer now, since this can never be a
* valid key representation. This way we know that data must be a valid * valid key representation. This way we know that data must be a valid
@ -1630,15 +1631,20 @@ psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
return status; return status;
} }
LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
psa_key_attributes_t attributes = { psa_key_attributes_t attributes = {
.core = slot->attr .core = slot->attr
}; };
status = psa_driver_wrapper_export_key(&attributes, status = psa_driver_wrapper_export_key(&attributes,
slot->key.data, slot->key.bytes, slot->key.data, slot->key.bytes,
data, data_size, data_length); data, data_size, data_length);
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
exit:
#endif
unlock_status = psa_unlock_key_slot(slot); unlock_status = psa_unlock_key_slot(slot);
LOCAL_OUTPUT_FREE(data_external, data);
return (status == PSA_SUCCESS) ? unlock_status : status; return (status == PSA_SUCCESS) ? unlock_status : status;
} }
@ -1710,7 +1716,7 @@ psa_status_t psa_export_public_key_internal(
} }
psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
uint8_t *data, uint8_t *data_external,
size_t data_size, size_t data_size,
size_t *data_length) size_t *data_length)
{ {
@ -1718,6 +1724,7 @@ psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED; psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
psa_key_slot_t *slot; psa_key_slot_t *slot;
psa_key_attributes_t attributes; psa_key_attributes_t attributes;
LOCAL_OUTPUT_DECLARE(data_external, data);
/* Reject a zero-length output buffer now, since this can never be a /* Reject a zero-length output buffer now, since this can never be a
* valid key representation. This way we know that data must be a valid * valid key representation. This way we know that data must be a valid
@ -1738,6 +1745,8 @@ psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
return status; return status;
} }
LOCAL_OUTPUT_ALLOC(data_external, data_size, data);
if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) { if (!PSA_KEY_TYPE_IS_ASYMMETRIC(slot->attr.type)) {
status = PSA_ERROR_INVALID_ARGUMENT; status = PSA_ERROR_INVALID_ARGUMENT;
goto exit; goto exit;
@ -1753,6 +1762,7 @@ psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
exit: exit:
unlock_status = psa_unlock_key_slot(slot); unlock_status = psa_unlock_key_slot(slot);
LOCAL_OUTPUT_FREE(data_external, data);
return (status == PSA_SUCCESS) ? unlock_status : status; return (status == PSA_SUCCESS) ? unlock_status : status;
} }
@ -2199,11 +2209,12 @@ rsa_exit:
} }
psa_status_t psa_import_key(const psa_key_attributes_t *attributes, psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
const uint8_t *data, const uint8_t *data_external,
size_t data_length, size_t data_length,
mbedtls_svc_key_id_t *key) mbedtls_svc_key_id_t *key)
{ {
psa_status_t status; psa_status_t status;
LOCAL_INPUT_DECLARE(data_external, data);
psa_key_slot_t *slot = NULL; psa_key_slot_t *slot = NULL;
psa_se_drv_table_entry_t *driver = NULL; psa_se_drv_table_entry_t *driver = NULL;
size_t bits; size_t bits;
@ -2223,6 +2234,8 @@ psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
return PSA_ERROR_NOT_SUPPORTED; return PSA_ERROR_NOT_SUPPORTED;
} }
LOCAL_INPUT_ALLOC(data_external, data_length, data);
status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes, status = psa_start_key_creation(PSA_KEY_CREATION_IMPORT, attributes,
&slot, &driver); &slot, &driver);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@ -2277,6 +2290,7 @@ psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
status = psa_finish_key_creation(slot, driver, key); status = psa_finish_key_creation(slot, driver, key);
exit: exit:
LOCAL_INPUT_FREE(data_external, data);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
psa_fail_key_creation(slot, driver); psa_fail_key_creation(slot, driver);
} }

View File

@ -16,13 +16,6 @@
#include <psa/crypto.h> #include <psa/crypto.h>
#endif #endif
#include "test/psa_test_wrappers.h"
#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C) \
&& defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
#include "test/psa_memory_poisoning_wrappers.h"
#endif
#if defined(MBEDTLS_PSA_CRYPTO_C) #if defined(MBEDTLS_PSA_CRYPTO_C)
/** Initialize the PSA Crypto subsystem. */ /** Initialize the PSA Crypto subsystem. */
#define PSA_INIT() PSA_ASSERT(psa_crypto_init()) #define PSA_INIT() PSA_ASSERT(psa_crypto_init())

View File

@ -145,6 +145,10 @@ class PSAWrapperGenerator(c_wrapper_generator.Base):
# Proof-of-concept: just instrument one function for now # Proof-of-concept: just instrument one function for now
if function_name == 'psa_cipher_encrypt': if function_name == 'psa_cipher_encrypt':
return True return True
if function_name in ('psa_import_key',
'psa_export_key',
'psa_export_public_key'):
return True
if function_name in ('psa_sign_message', if function_name in ('psa_sign_message',
'psa_verify_message', 'psa_verify_message',
'psa_sign_hash', 'psa_sign_hash',

View File

@ -435,7 +435,13 @@ psa_status_t mbedtls_test_wrap_psa_export_key(
size_t arg2_data_size, size_t arg2_data_size,
size_t *arg3_data_length) size_t *arg3_data_length)
{ {
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
MBEDTLS_TEST_MEMORY_POISON(arg1_data, arg2_data_size);
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
psa_status_t status = (psa_export_key)(arg0_key, arg1_data, arg2_data_size, arg3_data_length); psa_status_t status = (psa_export_key)(arg0_key, arg1_data, arg2_data_size, arg3_data_length);
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_data, arg2_data_size);
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
return status; return status;
} }
@ -446,7 +452,13 @@ psa_status_t mbedtls_test_wrap_psa_export_public_key(
size_t arg2_data_size, size_t arg2_data_size,
size_t *arg3_data_length) size_t *arg3_data_length)
{ {
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
MBEDTLS_TEST_MEMORY_POISON(arg1_data, arg2_data_size);
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
psa_status_t status = (psa_export_public_key)(arg0_key, arg1_data, arg2_data_size, arg3_data_length); psa_status_t status = (psa_export_public_key)(arg0_key, arg1_data, arg2_data_size, arg3_data_length);
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_data, arg2_data_size);
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
return status; return status;
} }
@ -566,7 +578,13 @@ psa_status_t mbedtls_test_wrap_psa_import_key(
size_t arg2_data_length, size_t arg2_data_length,
mbedtls_svc_key_id_t *arg3_key) mbedtls_svc_key_id_t *arg3_key)
{ {
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
MBEDTLS_TEST_MEMORY_POISON(arg1_data, arg2_data_length);
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
psa_status_t status = (psa_import_key)(arg0_attributes, arg1_data, arg2_data_length, arg3_key); psa_status_t status = (psa_import_key)(arg0_attributes, arg1_data, arg2_data_length, arg3_key);
#if defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS)
MBEDTLS_TEST_MEMORY_UNPOISON(arg1_data, arg2_data_length);
#endif /* defined(MBEDTLS_PSA_COPY_CALLER_BUFFERS) */
return status; return status;
} }