-
Notifications
You must be signed in to change notification settings - Fork 23
tests: crypto: Add support for testing wrapped keys in PSA crypto tests. #108
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,8 @@ | |
|
|
||
| #include <zephyr/ztest.h> | ||
| #include <psa/crypto.h> | ||
|
|
||
| #include <zephyr/sys/util.h> /* for IS_ENABLED() */ | ||
| #include "test_vectors.h" | ||
|
Check notice on line 10 in tests/crypto/psa_crypto/src/cipher.c
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep code consistent: group There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, I am surprised you need this include while the original code didn't need it. |
||
|
|
||
| uint8_t key_256[32] = { | ||
| 0x31, 0x4b, 0x66, 0x77, 0x19, 0x39, 0x73, 0x17, 0x7d, 0xaf, 0x98, | ||
|
|
@@ -40,9 +40,9 @@ | |
| psa_set_key_algorithm(&attributes, alg); | ||
| psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); | ||
| psa_set_key_bits(&attributes, 256); | ||
| if (IS_ENABLED(TEST_WRAPPED_KEYS)) { | ||
| if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't do this. The way it was is correct, the define is conditionally set by the testcase. |
||
| psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 1)); | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 0)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong. Location 1 is the default SE location, and it should stay that way. Using location 0 does not exercise opaque drivers (=wrapped keys). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asmellby , We discussed during the meeting that location 1 does not generate wrap the keys. Location 0 generates the keys and gets it wrapped and stored in Host RAM(local storage). So, it should be 0. To make it consistent with PSA, we would have to modify the wrapper file and as we discussed, we dont want to have conflicting code in Wiseconnect and Zephyr. 1 is used only for import local key There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Location 0 does not wrap the keys. By changing to location 0 you are no longer testing what you want to test. |
||
| zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS, | ||
| "Failed to generate key"); | ||
| } else { | ||
|
|
@@ -96,9 +96,9 @@ | |
| psa_set_key_algorithm(&attributes, alg); | ||
| psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); | ||
| psa_set_key_bits(&attributes, 256); | ||
| if (IS_ENABLED(TEST_WRAPPED_KEYS)) { | ||
| if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) { | ||
| psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 1)); | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 0)); | ||
| zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS, | ||
| "Failed to generate key"); | ||
| } else { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asmellby , as per you message on teams - you want us to remove the else part where import of local keys is being done, is my understanding right? |
||
|
|
@@ -121,17 +121,6 @@ | |
|
|
||
| zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch"); | ||
| zassert_mem_equal(decrypted, plaintext, sizeof(plaintext)); | ||
|
|
||
| ciphertext_buffer_256[0] += 1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you removing tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asmellby , This should not be needed, right? I think this was added by you, not sure what was the purpose. Can you let us know There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a negative test demonstrating that decryption doesn't wrongly succeed on the wrong ciphertext. It is not a duplicate. |
||
| zassert_equal(psa_cipher_decrypt(key_id, alg, ciphertext_buffer_256, ciphertext_len, | ||
| decrypted, sizeof(decrypted), &decrypted_len), | ||
| PSA_SUCCESS, | ||
| "Failed to perform one-shot decrypt operation with modified ciphertext"); | ||
|
|
||
| zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch"); | ||
| zassert(memcmp(decrypted, plaintext, sizeof(plaintext)) != 0, | ||
| "Decrypted modified data identical to original plaintext"); | ||
|
|
||
| psa_destroy_key(key_id); | ||
| } | ||
|
|
||
|
|
@@ -145,9 +134,9 @@ | |
| psa_set_key_algorithm(&attributes, alg); | ||
| psa_set_key_type(&attributes, PSA_KEY_TYPE_AES); | ||
| psa_set_key_bits(&attributes, 128); | ||
| if (IS_ENABLED(TEST_WRAPPED_KEYS)) { | ||
| if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) { | ||
| psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 1)); | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 0)); | ||
|
|
||
| zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS, | ||
| "Failed to generate key"); | ||
|
|
@@ -170,16 +159,6 @@ | |
| zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch"); | ||
| zassert_mem_equal(decrypted, plaintext, sizeof(plaintext)); | ||
|
|
||
| ciphertext[0] += 1; | ||
| zassert_equal(psa_cipher_decrypt(key_id, alg, ciphertext, ciphertext_len, decrypted, | ||
| sizeof(decrypted), &decrypted_len), | ||
| PSA_SUCCESS, | ||
| "Failed to perform one-shot decrypt operation with modified ciphertext"); | ||
|
|
||
| zassert_equal(decrypted_len, sizeof(decrypted), "Decrypted length mismatch"); | ||
| zassert(memcmp(decrypted, plaintext, sizeof(plaintext)) != 0, | ||
| "Decrypted modified data identical to original plaintext"); | ||
|
|
||
| psa_destroy_key(key_id); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ | |
|
|
||
| #include <zephyr/ztest.h> | ||
| #include <psa/crypto.h> | ||
| #include <zephyr/sys/util.h> /* for IS_ENABLED() */ | ||
|
|
||
|
Check notice on line 10 in tests/crypto/psa_crypto/src/key_agreement.c
|
||
| static const uint8_t client_private_key[] = { | ||
| 0xB0, 0x76, 0x51, 0xEA, 0x20, 0xF0, 0x28, 0xA8, 0x16, 0xEE, 0x01, | ||
| 0xB0, 0xD1, 0x06, 0x2A, 0x7C, 0x81, 0x58, 0xE8, 0x84, 0xE9, 0xBC, | ||
|
|
@@ -27,12 +28,11 @@ | |
| 0x8D, 0x49, 0x85, 0x8C, 0x7A, 0x9F, 0xC1, 0x46, 0xDA, 0xCC, 0x96, | ||
| 0xEF, 0x6E, 0xD4, 0xDA, 0x71, 0xBF, 0xED, 0x32, 0x0D, 0x76, | ||
| }; | ||
| static const uint8_t expected_shared_secret[] = { | ||
| static const uint8_t expected_shared_secret[] __unused = { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually, one of the benefits of |
||
| 0xF2, 0xE6, 0x0E, 0x1C, 0xB7, 0x64, 0xBC, 0x48, 0xF2, 0x9D, 0xBB, | ||
| 0x12, 0xFB, 0x12, 0x17, 0x31, 0x32, 0x1D, 0x79, 0xAF, 0x0A, 0x9F, | ||
| 0xAB, 0xAD, 0x34, 0x05, 0xA2, 0x07, 0x39, 0x9C, 0x5F, 0x15, | ||
| }; | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Original code was correct. |
||
| ZTEST(psa_crypto_test, test_key_agreement_ecdh_25519) | ||
| { | ||
| uint8_t shared_secret_buf[32]; | ||
|
|
@@ -44,9 +44,9 @@ | |
| psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY)); | ||
| psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | ||
| psa_set_key_algorithm(&attributes, PSA_ALG_ECDH); | ||
| if (IS_ENABLED(TEST_WRAPPED_KEYS)) { | ||
| if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) { | ||
| psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 1)); | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 0)); | ||
| } | ||
| zassert_equal(psa_import_key(&attributes, client_private_key, sizeof(client_private_key), | ||
| &key_id), | ||
|
|
@@ -64,9 +64,9 @@ | |
| psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY)); | ||
| psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); | ||
| psa_set_key_algorithm(&attributes, PSA_ALG_ECDH); | ||
| if (IS_ENABLED(TEST_WRAPPED_KEYS)) { | ||
| if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) { | ||
| psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 1)); | ||
| PSA_KEY_PERSISTENCE_VOLATILE, 0)); | ||
| } | ||
| zassert_equal(psa_import_key(&attributes, server_private_key, sizeof(server_private_key), | ||
| &key_id), | ||
|
|
@@ -78,8 +78,10 @@ | |
| sizeof(shared_secret_buf), &shared_secret_len), | ||
| PSA_SUCCESS, "Failed to perform key agreement with client"); | ||
| zassert_equal(psa_destroy_key(key_id), PSA_SUCCESS, "Failed to destroy server key"); | ||
|
|
||
| /* Verify shared secret */ | ||
| zassert_mem_equal(shared_secret_buf, expected_shared_secret, sizeof(expected_shared_secret), | ||
| "Key agreement did not resolve the correct shared secret"); | ||
| if (!IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) { | ||
| /* Verify shared secret */ | ||
| zassert_mem_equal(shared_secret_buf, expected_shared_secret, | ||
| sizeof(expected_shared_secret), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indent with opening parenthesis. |
||
| "Key agreement did not resolve the correct shared secret"); | ||
| } | ||
|
Check notice on line 86 in tests/crypto/psa_crypto/src/key_agreement.c
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: 2025 Silicon Laboratories Inc. | ||
|
|
||
| common: | ||
| tags: | ||
| - crypto | ||
|
|
@@ -31,6 +34,7 @@ tests: | |
| - xg24_dk2601b | ||
| - xg24_rb4187c | ||
| - xg29_rb4412a | ||
| - siwx917_rb4338a | ||
| extra_args: | ||
| - TEST_WRAPPED_KEYS=1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am surprize this statement works. I believe you mean: Alternately, if you don't want to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I missed the content of |
||
| extra_configs: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This item will only appears when
SILABS_SIWX91Xis selected. You don't need to specify- specific to SiWG917.I believe this symbols is misplaced. I understand right, it is an option of
test/crypto/psa_cryptoapplication.