FFDH: clean up size check

Signed-off-by: Manuel Pégourié-Gonnard <[email protected]>
This commit is contained in:
Manuel Pégourié-Gonnard
2026-03-05 12:52:19 +01:00
parent 470ee785ca
commit 10c5db67be
2 changed files with 8 additions and 9 deletions
+2 -4
View File
@@ -7923,10 +7923,8 @@ psa_status_t psa_raw_key_agreement(psa_algorithm_t alg,
* for the output size. The PSA specification only guarantees that this
* function works if output_size >= PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(...),
* but it might be nice to allow smaller buffers if the output fits.
* At the time of writing this comment, with only ECDH implemented,
* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot.
* If FFDH is implemented, PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() can easily
* be exact for it as well. */
* At the time of writing this comment, for both FFDH and ECDH,
* PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE() is exact so the point is moot. */
expected_length =
PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(slot->attr.type, slot->attr.bits);
if (output_size < expected_length) {
+6 -5
View File
@@ -267,14 +267,15 @@ psa_status_t mbedtls_psa_ffdh_key_agreement(
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
mbedtls_mpi P, X, GY, K;
const size_t calculated_shared_secret_size = peer_key_length;
const size_t calculated_shared_secret_size = key_buffer_size;
if (peer_key_length != key_buffer_size ||
calculated_shared_secret_size > shared_secret_size) {
return PSA_ERROR_INVALID_ARGUMENT;
/* This has been checked by the library, but keep a local check too. */
if (calculated_shared_secret_size > shared_secret_size) {
return PSA_ERROR_BUFFER_TOO_SMALL;
}
if (!PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_get_key_type(attributes))) {
if (peer_key_length != key_buffer_size ||
!PSA_KEY_TYPE_IS_DH_KEY_PAIR(psa_get_key_type(attributes))) {
return PSA_ERROR_INVALID_ARGUMENT;
}