psasim: add support for psa_generate_random()

Signed-off-by: Tom Cosgrove <tom.cosgrove@arm.com>
This commit is contained in:
Tom Cosgrove 2024-06-21 10:38:49 +01:00 committed by Valerio Setti
parent 2a674bd9ce
commit baace2f7ba
4 changed files with 169 additions and 0 deletions

View File

@ -25,6 +25,7 @@ enum {
PSA_AEAD_UPDATE_AD,
PSA_AEAD_VERIFY,
PSA_DESTROY_KEY,
PSA_GENERATE_RANDOM,
PSA_GET_KEY_ATTRIBUTES,
PSA_HASH_ABORT,
PSA_HASH_CLONE,

View File

@ -1191,6 +1191,69 @@ fail:
}
psa_status_t psa_generate_random(
uint8_t *output, size_t output_size
)
{
uint8_t *params = NULL;
uint8_t *result = NULL;
size_t result_length;
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
size_t needed = psasim_serialise_begin_needs() +
psasim_serialise_buffer_needs(output, output_size);
params = malloc(needed);
if (params == NULL) {
status = PSA_ERROR_INSUFFICIENT_MEMORY;
goto fail;
}
uint8_t *pos = params;
size_t remaining = needed;
int ok;
ok = psasim_serialise_begin(&pos, &remaining);
if (!ok) {
goto fail;
}
ok = psasim_serialise_buffer(&pos, &remaining, output, output_size);
if (!ok) {
goto fail;
}
ok = psa_crypto_call(PSA_GENERATE_RANDOM,
params, (size_t) (pos - params), &result, &result_length);
if (!ok) {
printf("PSA_GENERATE_RANDOM server call failed\n");
goto fail;
}
uint8_t *rpos = result;
size_t rremain = result_length;
ok = psasim_deserialise_begin(&rpos, &rremain);
if (!ok) {
goto fail;
}
ok = psasim_deserialise_psa_status_t(&rpos, &rremain, &status);
if (!ok) {
goto fail;
}
ok = psasim_deserialise_return_buffer(&rpos, &rremain, output, output_size);
if (!ok) {
goto fail;
}
fail:
free(params);
free(result);
return status;
}
psa_status_t psa_get_key_attributes(
mbedtls_svc_key_id_t key,
psa_key_attributes_t *attributes

View File

@ -1324,6 +1324,80 @@ fail:
return 0; // This shouldn't happen!
}
// Returns 1 for success, 0 for failure
int psa_generate_random_wrapper(
uint8_t *in_params, size_t in_params_len,
uint8_t **out_params, size_t *out_params_len)
{
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
uint8_t *output = NULL;
size_t output_size;
uint8_t *pos = in_params;
size_t remaining = in_params_len;
uint8_t *result = NULL;
int ok;
ok = psasim_deserialise_begin(&pos, &remaining);
if (!ok) {
goto fail;
}
ok = psasim_deserialise_buffer(&pos, &remaining, &output, &output_size);
if (!ok) {
goto fail;
}
// Now we call the actual target function
status = psa_generate_random(
output, output_size
);
// NOTE: Should really check there is no overflow as we go along.
size_t result_size =
psasim_serialise_begin_needs() +
psasim_serialise_psa_status_t_needs(status) +
psasim_serialise_buffer_needs(output, output_size);
result = malloc(result_size);
if (result == NULL) {
goto fail;
}
uint8_t *rpos = result;
size_t rremain = result_size;
ok = psasim_serialise_begin(&rpos, &rremain);
if (!ok) {
goto fail;
}
ok = psasim_serialise_psa_status_t(&rpos, &rremain, status);
if (!ok) {
goto fail;
}
ok = psasim_serialise_buffer(&rpos, &rremain, output, output_size);
if (!ok) {
goto fail;
}
*out_params = result;
*out_params_len = result_size;
free(output);
return 1; // success
fail:
free(result);
free(output);
return 0; // This shouldn't happen!
}
// Returns 1 for success, 0 for failure
int psa_get_key_attributes_wrapper(
uint8_t *in_params, size_t in_params_len,
@ -2252,6 +2326,10 @@ psa_status_t psa_crypto_call(psa_msg_t msg)
ok = psa_destroy_key_wrapper(in_params, in_params_len,
&out_params, &out_params_len);
break;
case PSA_GENERATE_RANDOM:
ok = psa_generate_random_wrapper(in_params, in_params_len,
&out_params, &out_params_len);
break;
case PSA_GET_KEY_ATTRIBUTES:
ok = psa_get_key_attributes_wrapper(in_params, in_params_len,
&out_params, &out_params_len);

View File

@ -2371,3 +2371,30 @@ psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
* results in this error code.
*/
psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
/**
* \brief Generate random bytes.
*
* \warning This function **can** fail! Callers MUST check the return status
* and MUST NOT use the content of the output buffer if the return
* status is not #PSA_SUCCESS.
*
* \note To generate a key, use psa_generate_key() instead.
*
* \param[out] output Output buffer for the generated data.
* \param output_size Number of bytes to generate and output.
*
* \retval #PSA_SUCCESS \emptydescription
* \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
* \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
* \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
* \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
* \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
* \retval #PSA_ERROR_BAD_STATE
* The library has not been previously initialized by psa_crypto_init().
* It is implementation-dependent whether a failure to initialize
* results in this error code.
*/
psa_status_t psa_generate_random(uint8_t *output,
size_t output_size);