Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PSA interruptible export public-key get num ops API #9820

Open
wants to merge 19 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c1fc136
Add Header for mbedtls_psa_ecp_export_public_key_iop_complete()
waleed-elmelegy-arm Dec 3, 2024
af2595b
Add implementation for mbedtls_psa_ecp_export_public_key_iop_complete()
waleed-elmelegy-arm Dec 3, 2024
2cfce63
Fix status variable type in mbedtls_psa_ecp_export_public_key_iop_set…
waleed-elmelegy-arm Dec 3, 2024
4cffd5d
Add implementaion for psa_export_public_key_iop_complete()
waleed-elmelegy-arm Dec 3, 2024
f466a28
Fix checks for key type in psa_export_public_key_iop_setup()
waleed-elmelegy-arm Dec 3, 2024
54ba963
Add interuptible export public-key testing to invalid key tests
waleed-elmelegy-arm Dec 3, 2024
81a5258
Add interuptible export public-key to current export public-key tests
waleed-elmelegy-arm Dec 3, 2024
a04e88a
Fix export public-key opaque key test paramters
waleed-elmelegy-arm Dec 3, 2024
e283ed9
Add testing of complete API of interruptible export public-key
waleed-elmelegy-arm Dec 3, 2024
3c46535
Rename mbedtls_psa_export_public_key_iop_operation_t
waleed-elmelegy-arm Dec 3, 2024
1daabc1
Refactor and improve iop export public-key setup and abort APIs
waleed-elmelegy-arm Dec 9, 2024
c66147d
Refactor & improve internal iop export public-key setup and complete …
waleed-elmelegy-arm Dec 9, 2024
0843214
Remove Invalid import/export key test
waleed-elmelegy-arm Dec 9, 2024
e330e58
Improve iop export public-key testing
waleed-elmelegy-arm Dec 9, 2024
50d8567
Add Header and implementation of internal iop export public-key get_n…
waleed-elmelegy-arm Dec 3, 2024
5784dc5
Add PSA iop export public-key get_num_ops() functionality
waleed-elmelegy-arm Dec 3, 2024
314af07
Rename interruptible_key_agreement_get_min_num_ops()
waleed-elmelegy-arm Dec 3, 2024
c017e85
Add testing for psa_export_public_key_iop_get_num_ops() API
waleed-elmelegy-arm Dec 3, 2024
ef5c8a9
Add Changelog entry for interruptible export public-key
waleed-elmelegy-arm Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ChangeLog.d/add-psa-iop-export-public-key.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Features
* Add an interruptible version of export public-key to the PSA interface.
See psa_export_public_key_iop_setup() and related functions.
53 changes: 37 additions & 16 deletions tf-psa-crypto/core/psa_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,8 @@ static psa_status_t psa_export_public_key_iop_abort_internal(psa_export_public_k

status = mbedtls_psa_ecp_export_public_key_iop_abort(&operation->ctx);

memset(&operation->ctx, 0, sizeof(operation->ctx));

operation->id = 0;

return status;
Expand All @@ -1684,8 +1686,12 @@ static psa_status_t psa_export_public_key_iop_abort_internal(psa_export_public_k

uint32_t psa_export_public_key_iop_get_num_ops(psa_export_public_key_iop_t *operation)
{
#if defined(MBEDTLS_ECP_RESTARTABLE)
return operation->num_ops;
#else
(void) operation;
return 0;
#endif
}

psa_status_t psa_export_public_key_iop_setup(psa_export_public_key_iop_t *operation,
Expand All @@ -1694,9 +1700,8 @@ psa_status_t psa_export_public_key_iop_setup(psa_export_public_key_iop_t *operat
#if defined(MBEDTLS_ECP_RESTARTABLE)
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
size_t key_size = 0;
psa_key_attributes_t private_key_attributes;
psa_key_type_t private_key_type;
psa_key_attributes_t key_attributes;
psa_key_type_t key_type;
psa_key_slot_t *slot = NULL;

if (operation->id != 0 || operation->error_occurred) {
Expand All @@ -1713,29 +1718,22 @@ psa_status_t psa_export_public_key_iop_setup(psa_export_public_key_iop_t *operat
goto exit;
}

private_key_attributes = slot->attr;
key_attributes = slot->attr;

private_key_type = psa_get_key_type(&private_key_attributes);
key_type = psa_get_key_type(&key_attributes);

if (!PSA_KEY_TYPE_IS_KEY_PAIR(private_key_type)) {
if (!PSA_KEY_TYPE_IS_ASYMMETRIC(key_type)) {
status = PSA_ERROR_INVALID_ARGUMENT;
goto exit;
}

if (!PSA_KEY_TYPE_IS_ECC_KEY_PAIR(private_key_type)) {
status = PSA_ERROR_NOT_SUPPORTED;
goto exit;
}

key_size = PSA_EXPORT_KEY_OUTPUT_SIZE(private_key_type,
psa_get_key_bits(&private_key_attributes));
if (key_size == 0) {
if (!PSA_KEY_TYPE_IS_ECC(key_type)) {
status = PSA_ERROR_NOT_SUPPORTED;
goto exit;
}

status = mbedtls_psa_ecp_export_public_key_iop_setup(&operation->ctx, slot->key.data,
slot->key.bytes, &private_key_attributes);
slot->key.bytes, &key_attributes);

exit:
unlock_status = psa_unregister_read_under_mutex(slot);
Expand All @@ -1757,12 +1755,35 @@ psa_status_t psa_export_public_key_iop_complete(psa_export_public_key_iop_t *ope
size_t data_size,
size_t *data_length)
{
#if defined(MBEDTLS_ECP_RESTARTABLE)
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;

if (operation->id == 0 || operation->error_occurred) {
return PSA_ERROR_BAD_STATE;
}

status = mbedtls_psa_ecp_export_public_key_iop_complete(&operation->ctx, data, data_size,
data_length);

operation->num_ops = mbedtls_psa_ecp_export_public_key_iop_get_num_ops(&operation->ctx);

if (status != PSA_OPERATION_INCOMPLETE) {
psa_export_public_key_iop_abort_internal(operation);

if (status != PSA_SUCCESS) {
operation->error_occurred = 1;
}
}

return status;
#else
(void) operation;
(void) data;
(void) data_size;
(void) data_length;

return PSA_ERROR_NOT_SUPPORTED;
return PSA_ERROR_BAD_STATE;
#endif
}

psa_status_t psa_export_public_key_iop_abort(psa_export_public_key_iop_t *operation)
Expand Down
51 changes: 42 additions & 9 deletions tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,19 +664,25 @@ psa_status_t mbedtls_psa_ecp_generate_key_iop_abort(
return PSA_SUCCESS;
}

uint32_t mbedtls_psa_ecp_export_public_key_iop_get_num_ops(
mbedtls_psa_export_public_key_iop_t *operation)
{
return operation->num_ops;
}

psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup(
mbedtls_psa_export_public_key_iop_operation_t *operation,
uint8_t *private_key,
size_t private_key_len,
const psa_key_attributes_t *private_key_attributes)
mbedtls_psa_export_public_key_iop_t *operation,
uint8_t *key,
size_t key_len,
const psa_key_attributes_t *key_attributes)
{
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;

status = mbedtls_psa_ecp_load_representation(
psa_get_key_type(private_key_attributes),
psa_get_key_bits(private_key_attributes),
private_key,
private_key_len,
psa_get_key_type(key_attributes),
psa_get_key_bits(key_attributes),
key,
key_len,
&operation->key);
if (status != PSA_SUCCESS) {
goto exit;
Expand All @@ -689,8 +695,35 @@ psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup(
return status;
}

psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete(
mbedtls_psa_export_public_key_iop_t *operation,
uint8_t *pub_key,
size_t pub_key_size,
size_t *pub_key_len)
{
int ret = 0;

if (mbedtls_ecp_is_zero(&operation->key->Q)) {
mbedtls_psa_interruptible_set_max_ops(psa_interruptible_get_max_ops());

ret = mbedtls_ecp_mul_restartable(&operation->key->grp, &operation->key->Q,
&operation->key->d, &operation->key->grp.G,
mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE,
&operation->restart_ctx);
operation->num_ops += operation->restart_ctx.ops_done;
}

if (ret == 0) {
ret = mbedtls_ecp_write_public_key(operation->key,
MBEDTLS_ECP_PF_UNCOMPRESSED, pub_key_len,
pub_key, pub_key_size);
}

return mbedtls_to_psa_error(ret);
}

psa_status_t mbedtls_psa_ecp_export_public_key_iop_abort(
mbedtls_psa_export_public_key_iop_operation_t *operation)
mbedtls_psa_export_public_key_iop_t *operation)
{
mbedtls_ecp_keypair_free(operation->key);
mbedtls_free(operation->key);
Expand Down
44 changes: 40 additions & 4 deletions tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,21 @@ psa_status_t mbedtls_psa_ecp_export_public_key(
const uint8_t *key_buffer, size_t key_buffer_size,
uint8_t *data, size_t data_size, size_t *data_length);

/**
* \brief Get the total number of ops that an export public-key operation has taken
* Since it's start.
*
* \param[in] operation The \c mbedtls_psa_export_public_key_iop_t to use.
*
* \return Total number of operations.
*/
uint32_t mbedtls_psa_ecp_export_public_key_iop_get_num_ops(
mbedtls_psa_export_public_key_iop_t *operation);

/**
* \brief Setup a new interruptible export public-key operation.
*
* \param[in] operation The \c mbedtls_psa_export_public_key_iop_operation_t to use.
* \param[in] operation The \c mbedtls_psa_export_public_key_iop_t to use.
* This must be initialized first.
* \param[in] private_key pointer to private key.
* \param[in] private_key_len size of \p private_key in bytes.
Expand All @@ -142,21 +153,46 @@ psa_status_t mbedtls_psa_ecp_export_public_key(
*
*/
psa_status_t mbedtls_psa_ecp_export_public_key_iop_setup(
mbedtls_psa_export_public_key_iop_operation_t *operation,
mbedtls_psa_export_public_key_iop_t *operation,
uint8_t *private_key,
size_t private_key_len,
const psa_key_attributes_t *private_key_attributes);


/**
* \brief Continue and eventually complete an export public-key operation.
*
* \param[in] operation The \c mbedtls_psa_export_public_key_iop_t to use.
* This must be initialized first and
* had \c mbedtls_psa_ecp_export_public_key_iop_setup()
* called successfully.
* \param[out] pub_key Buffer where the public key data is to be written.
* \param[in] pub_key_size Size of the \p pub_key buffer in bytes.
* \param[out] pub_key_len On success, the number of bytes that make up the public key data.
*
* \retval #PSA_SUCCESS
* The key was exported successfully.
* \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
* \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
*
*/
psa_status_t mbedtls_psa_ecp_export_public_key_iop_complete(
mbedtls_psa_export_public_key_iop_t *operation,
uint8_t *pub_key,
size_t pub_key_size,
size_t *pub_key_len);

/**
* \brief Abort an interruptible export public-key operation.
*
* \param[in] operation The \c mbedtls_psa_export_public_key_iop_operation_t to abort.
* \param[in] operation The \c mbedtls_psa_export_public_key_iop_t to abort.
*
* \retval #PSA_SUCCESS
* The operation was aborted successfully.
*/
psa_status_t mbedtls_psa_ecp_export_public_key_iop_abort(
mbedtls_psa_export_public_key_iop_operation_t *operation);
mbedtls_psa_export_public_key_iop_t *operation);

/**
* \brief Generate an ECP key.
Expand Down
2 changes: 1 addition & 1 deletion tf-psa-crypto/include/psa/crypto_builtin_composites.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ typedef struct {
/* Make the struct non-empty if algs not supported. */
unsigned MBEDTLS_PRIVATE(dummy);
#endif
} mbedtls_psa_export_public_key_iop_operation_t;
} mbedtls_psa_export_public_key_iop_t;

#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_ECP_RESTARTABLE)
#define MBEDTLS_PSA_EXPORT_PUBLIC_KEY_IOP_INIT { NULL, MBEDTLS_ECP_RESTART_INIT, 0 }
Expand Down
2 changes: 1 addition & 1 deletion tf-psa-crypto/include/psa/crypto_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ struct psa_export_public_key_iop_s {
* any driver (i.e. none of the driver contexts are active).
*/
unsigned int MBEDTLS_PRIVATE(id);
mbedtls_psa_export_public_key_iop_operation_t MBEDTLS_PRIVATE(ctx);
mbedtls_psa_export_public_key_iop_t MBEDTLS_PRIVATE(ctx);
unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
uint32_t MBEDTLS_PRIVATE(num_ops);
#endif
Expand Down
Loading