Move structure init calls as early as possible

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
This commit is contained in:
Paul Elliott 2023-02-15 16:38:04 +00:00
parent ebe225cf7b
commit a1c9409d88

View File

@ -3539,6 +3539,8 @@ psa_status_t mbedtls_psa_sign_hash_start(
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
defined(MBEDTLS_ECP_RESTARTABLE) defined(MBEDTLS_ECP_RESTARTABLE)
mbedtls_ecdsa_restart_init(&operation->restart_ctx);
/* Ensure default is set even if /* Ensure default is set even if
* mbedtls_psa_interruptible_set_max_ops() has not been called. */ * mbedtls_psa_interruptible_set_max_ops() has not been called. */
mbedtls_psa_interruptible_set_max_ops( mbedtls_psa_interruptible_set_max_ops(
@ -3554,8 +3556,6 @@ psa_status_t mbedtls_psa_sign_hash_start(
return status; return status;
} }
mbedtls_ecdsa_restart_init(&operation->restart_ctx);
operation->coordinate_bytes = PSA_BITS_TO_BYTES( operation->coordinate_bytes = PSA_BITS_TO_BYTES(
operation->ctx->grp.nbits); operation->ctx->grp.nbits);
@ -3594,22 +3594,22 @@ psa_status_t mbedtls_psa_sign_hash_complete(
uint8_t *signature, size_t signature_size, uint8_t *signature, size_t signature_size,
size_t *signature_length) size_t *signature_length)
{ {
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
#if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \ #if (defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
defined(MBEDTLS_ECP_RESTARTABLE) defined(MBEDTLS_ECP_RESTARTABLE)
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi r; mbedtls_mpi r;
mbedtls_mpi s; mbedtls_mpi s;
if (signature_size < 2 * operation->coordinate_bytes) {
return PSA_ERROR_BUFFER_TOO_SMALL;
}
mbedtls_mpi_init(&r); mbedtls_mpi_init(&r);
mbedtls_mpi_init(&s); mbedtls_mpi_init(&s);
if (signature_size < 2 * operation->coordinate_bytes) {
status = PSA_ERROR_BUFFER_TOO_SMALL;
goto exit;
}
if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) { if (PSA_ALG_ECDSA_IS_DETERMINISTIC(operation->alg)) {
#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) #if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
@ -3679,7 +3679,6 @@ exit:
#else #else
(void) operation; (void) operation;
(void) status;
(void) signature; (void) signature;
(void) signature_size; (void) signature_size;
(void) signature_length; (void) signature_length;
@ -3744,6 +3743,10 @@ psa_status_t mbedtls_psa_verify_hash_start(
defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \ defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)) && \
defined(MBEDTLS_ECP_RESTARTABLE) defined(MBEDTLS_ECP_RESTARTABLE)
mbedtls_ecdsa_restart_init(&operation->restart_ctx);
mbedtls_mpi_init(&operation->r);
mbedtls_mpi_init(&operation->s);
/* Ensure default is set even if /* Ensure default is set even if
* mbedtls_psa_interruptible_set_max_ops() has not been called. */ * mbedtls_psa_interruptible_set_max_ops() has not been called. */
mbedtls_psa_interruptible_set_max_ops( mbedtls_psa_interruptible_set_max_ops(
@ -3765,7 +3768,6 @@ psa_status_t mbedtls_psa_verify_hash_start(
return PSA_ERROR_INVALID_SIGNATURE; return PSA_ERROR_INVALID_SIGNATURE;
} }
mbedtls_mpi_init(&operation->r);
status = mbedtls_to_psa_error( status = mbedtls_to_psa_error(
mbedtls_mpi_read_binary(&operation->r, mbedtls_mpi_read_binary(&operation->r,
signature, signature,
@ -3775,7 +3777,6 @@ psa_status_t mbedtls_psa_verify_hash_start(
return status; return status;
} }
mbedtls_mpi_init(&operation->s);
status = mbedtls_to_psa_error( status = mbedtls_to_psa_error(
mbedtls_mpi_read_binary(&operation->s, mbedtls_mpi_read_binary(&operation->s,
signature + signature +
@ -3792,8 +3793,6 @@ psa_status_t mbedtls_psa_verify_hash_start(
return mbedtls_to_psa_error(ret); return mbedtls_to_psa_error(ret);
} }
mbedtls_ecdsa_restart_init(&operation->restart_ctx);
/* We only need to store the same length of hash as the private key size /* We only need to store the same length of hash as the private key size
* here, it would be truncated by the internal implementation anyway. */ * here, it would be truncated by the internal implementation anyway. */
required_hash_length = (hash_length < coordinate_bytes ? hash_length : required_hash_length = (hash_length < coordinate_bytes ? hash_length :