8000 Merge pull request #125 from gilles-peskine-arm/mbedtls_test_psa_raw_… · Mbed-TLS/mbedtls-framework@7ef92b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ef92b1

Browse files
Merge pull request #125 from gilles-peskine-arm/mbedtls_test_psa_raw_key_agreement_with_self-iop-consistency-framework
Add consistency checks to mbedtls_test_psa_raw_key_agreement_with_self()
2 parents 9e612a4 + fb43aa3 commit 7ef92b1

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

tests/include/test/psa_exercise_key.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,30 @@ int mbedtls_test_psa_setup_key_derivation_wrap(
138138
size_t capacity, int key_destroyable);
139139

140140
/** Perform a key agreement using the given key pair against its public key
141-
* using psa_raw_key_agreement() and psa_key_agreement().
141+
* (not combined with a key derivation).
142142
*
143-
* The result is discarded. The purpose of this function is to smoke-test a key.
143+
* The result is discarded. Thus this function can be used for smoke-testing
144+
* a key, and to validate input validation, but not to validate results.
144145
*
145-
* In case of failure, mark the current test case as failed.
146+
* Depending on the library version, there can be multiple interfaces for key
147+
* agreement. This test function performs the ones that are available amongst:
< 8000 /code>148+
* - psa_raw_key_agreement()
149+
* - psa_key_agreement()
150+
* - psa_key_agreement_iop_setup() and psa_key_agreement_iop_complete()
151+
*
152+
* Mark the current test case as failed in the following cases:
153+
* - Operational errors such as failure to allocate memory for an intermediate
154+
* buffer.
155+
* - Results are not consistent between the methods that are performed:
156+
* different statuses, or inconsistent metadata, or different shared secret.
146157
*
147158
* \param alg A key agreement algorithm compatible with \p key.
148159
* \param key A key that allows key agreement with \p alg.
149160
* \param key_destroyable If set to 1, a failure due to the key not existing
150161
* or the key being destroyed mid-operation will only
151162
* be reported if the error code is unexpected.
152163
*
153-
* \return \c 1 on success, \c 0 on failure.
164+
* \return The status from psa_raw_key_agreement().
154165
*/
155166
psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
156167
psa_algorithm_t alg,

tests/src/psa_exercise_key.c

Copy file name to clipboard
Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,9 @@ psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
732732
}
733733
PSA_ASSERT(status);
734734

735-
status = psa_raw_key_agreement(alg, key,
736-
public_key, public_key_length,
737-
output, sizeof(output), &output_length);
735+
status = psa_raw_key_agreement(
736+
alg, key, public_key, public_key_length,
737+
output, sizeof(output), &output_length);
738738
if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) {
739739
/* The key has been destroyed. */
740740
status = PSA_SUCCESS;
@@ -749,8 +749,11 @@ psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
749749
}
750750

751751
#if MBEDTLS_VERSION_MAJOR >= 4
752+
psa_status_t raw_status = status;
753+
752754
psa_set_key_type(&shared_secret_attributes, PSA_KEY_TYPE_DERIVE);
753-
psa_set_key_usage_flags(&shared_secret_attributes, PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
755+
psa_set_key_usage_flags(&shared_secret_attributes,
756+
PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
754757

755758
status = psa_key_agreement(key, public_key, public_key_length, alg,
756759
&shared_secret_attributes, &shared_secret_id);
@@ -759,27 +762,37 @@ psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
759762
/* The key has been destroyed. */
760763
status = PSA_SUCCESS;
761764
goto exit;
762-
} else if (status == PSA_SUCCESS) {
765+
}
766+
767+
/* In this function, we expect either success or a validation failure,
768+
* which should be identical for raw output and key output. So flag any
769+
* discrepancy between the two (in particular a key creation failure)
770+
* as a test failure. */
771+
TEST_EQUAL(raw_status, status);
763772

773+
if (status == PSA_SUCCESS) {
764774
status = psa_get_key_attributes(shared_secret_id, &export_attributes);
765775
if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) {
766776
/* The key 8000 has been destroyed. */
767777
status = PSA_SUCCESS;
768778
goto exit;
769779
}
770780

771-
exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(psa_get_key_type(&export_attributes),
772-
psa_get_key_bits(&export_attributes));
781+
exported_size =
782+
PSA_EXPORT_KEY_OUTPUT_SIZE(psa_get_key_type(&export_attributes),
783+
psa_get_key_bits(&export_attributes));
773784
TEST_CALLOC(exported, exported_size);
774785

775-
status = psa_export_key(shared_secret_id, exported, exported_size, &exported_length);
776-
786+
status = psa_export_key(shared_secret_id,
787+
exported, exported_size, &exported_length);
777788
if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) {
778789
/* The key has been destroyed. */
779790
status = PSA_SUCCESS;
791+
} else {
792+
PSA_ASSERT(status);
793+
TEST_MEMORY_COMPARE(exported, exported_length,
794+
output, output_length);
780795
}
781-
782-
PSA_ASSERT(status);
783796
}
784797

785798
#if defined(MBEDTLS_ECP_RESTARTABLE) && defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
@@ -798,18 +811,39 @@ psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
798811
/* The key has been destroyed. */
799812
status = PSA_SUCCESS;
800813
goto exit;
801-
} else if (status == PSA_SUCCESS) {
814+
}
815+
816+
/* In this function, we expect either success or a validation failure,
817+
* which should be identical for one-shot and interruptible. For an
818+
* interruptible operation, we insist on detecting error conditions
819+
* early, in setup() rather than complete(). So flag any discrepancy
820+
* between one-shot and interruptible-setup as a test failure. */
821+
TEST_EQUAL(raw_status, status);
822+
823+
if (status == PSA_SUCCESS) {
802824

803825
do {
804-
status = psa_key_agreement_iop_complete(&iop_operation, &shared_secret_id);
826+
status = psa_key_agreement_iop_complete(&iop_operation,
827+
&shared_secret_id);
805828
} while (status == PSA_OPERATION_INCOMPLETE);
806829

807830
if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) {
808831
/* The key has been destroyed. */
809832
status = PSA_SUCCESS;
833+
} else {
834+
PSA_ASSERT(status);
835+
status = psa_export_key(shared_secret_id,
836+
exported, exported_size,
837+
&exported_length);
838+
if (key_destroyable && status == PSA_ERROR_INVALID_HANDLE) {
839+
/* The key has been destroyed. */
840+
status = PSA_SUCCESS;
841+
} else {
842+
PSA_ASSERT(status);
843+
TEST_MEMORY_COMPARE(exported, exported_length,
844+
output, output_length);
845+
}
810846
}
811-
812-
PSA_ASSERT(status);
813847
}
814848
} else {
815849
TEST_EQUAL(psa_key_agreement_iop_setup(&iop_operation, key, public_key,

0 commit comments

Comments
 (0)
0