-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/sys: Add tests for PSA Crypto CBOR key encoder
- Loading branch information
1 parent
07be2fa
commit 4003957
Showing
8 changed files
with
517 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
include ../Makefile.sys_common | ||
|
||
USEMODULE += embunit | ||
|
||
USEMODULE += psa_crypto | ||
|
||
USEMODULE += psa_cipher | ||
USEMODULE += psa_cipher_aes_128_cbc | ||
|
||
USEMODULE += psa_asymmetric | ||
USEMODULE += psa_asymmetric_ecc_p256r1 | ||
|
||
CFLAGS += -DCONFIG_PSA_ASYMMETRIC_KEYPAIR_COUNT=1 | ||
CFLAGS += -DCONFIG_PSA_SINGLE_KEY_COUNT=1 | ||
CFLAGS += -DCONFIG_PSA_PROTECTED_KEY_COUNT=1 | ||
|
||
include $(RIOTBASE)/Makefile.include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CONFIG_MODULE_EMBUNIT=y | ||
|
||
CONFIG_MODULE_PSA_CRYPTO=y | ||
|
||
CONFIG_MODULE_PSA_CIPHER=y | ||
CONFIG_MODULE_PSA_CIPHER_AES_128_CBC=y | ||
|
||
CONFIG_MODULE_PSA_ASYMMETRIC=y | ||
CONFIG_MODULE_PSA_ASYMMETRIC_ECC_P256R1=y | ||
|
||
CONFIG_PSA_SINGLE_KEY_COUNT=1 | ||
CONFIG_PSA_ASYMMETRIC_KEYPAIR_COUNT=1 | ||
CONFIG_PSA_PROTECTED_KEY_COUNT=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (C) 2023 HAW Hamburg | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup tests | ||
* @{ | ||
* | ||
* @file | ||
* @brief Test application for the PSA Crypto key encoding module | ||
* | ||
* @author Lena Boeckmann <lena.boeckmann@haw-hamburg.de> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include "tests_psa_cbor_enc_dec.h" | ||
|
||
int main(void) | ||
{ | ||
TESTS_START(); | ||
TESTS_RUN(tests_psa_crypto_enc_dec_single_key()); | ||
TESTS_RUN(tests_psa_crypto_enc_dec_keypair()); | ||
TESTS_RUN(tests_psa_crypto_enc_dec_protected_key()); | ||
TESTS_END(); | ||
return 0; | ||
} |
113 changes: 113 additions & 0 deletions
113
tests/sys/psa_crypto_cbor_encoder/test_psa_crypto_enc_dec_keypair.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright (C) 2023 HAW Hamburg | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup tests | ||
* @{ | ||
* | ||
* @file | ||
* @brief Tests CBOR encoding of a PSA Crypto asymmetric key pair slot. | ||
* | ||
* @author Lena Boeckmann <lena.boeckmann@haw-hamburg.de> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include "embUnit.h" | ||
#include "psa/crypto.h" | ||
#include "psa_crypto_cbor_encoder.h" | ||
#include "tests_psa_cbor_enc_dec.h" | ||
|
||
static uint8_t cbor_encoded_data[] = { | ||
0x9f, 0x85, 0x00, 0x19, 0x71, 0x12, 0x19, 0x01, | ||
0x00, 0x01, 0x82, 0x19, 0x30, 0x00, 0x1a, 0x06, | ||
0x00, 0x06, 0x09, 0x82, 0x82, 0x18, 0x20, 0x98, | ||
0x20, 0x18, 0x27, 0x18, 0xfc, 0x18, 0x4d, 0x18, | ||
0x41, 0x18, 0xf3, 0x18, 0xde, 0x18, 0x49, 0x18, | ||
0xf7, 0x18, 0x86, 0x18, 0x31, 0x18, 0x4b, 0x18, | ||
0x74, 0x18, 0xaa, 0x18, 0x67, 0x18, 0xde, 0x18, | ||
0x4b, 0x18, 0xa9, 0x18, 0x61, 0x18, 0xc3, 0x18, | ||
0x8f, 0x18, 0x4f, 0x18, 0x89, 0x18, 0x6f, 0x04, | ||
0x18, 0x5a, 0x18, 0x53, 0x18, 0x71, 0x02, 0x18, | ||
0xb4, 0x18, 0x3d, 0x18, 0x20, 0x18, 0x39, 0x82, | ||
0x18, 0x41, 0x98, 0x41, 0x04, 0x18, 0x56, 0x18, | ||
0xcb, 0x18, 0x81, 0x18, 0xd1, 0x18, 0xcb, 0x18, | ||
0xde, 0x18, 0x44, 0x18, 0xf5, 0x18, 0x1d, 0x18, | ||
0xcc, 0x18, 0xb1, 0x18, 0x26, 0x02, 0x18, 0x67, | ||
0x0d, 0x18, 0x76, 0x18, 0xdd, 0x18, 0xe7, 0x18, | ||
0x84, 0x18, 0xed, 0x18, 0x8d, 0x18, 0x30, 0x18, | ||
0x72, 0x18, 0x1c, 0x18, 0xca, 0x18, 0x50, 0x18, | ||
0x59, 0x18, 0xf9, 0x18, 0x20, 0x18, 0xad, 0x18, | ||
0x62, 0x18, 0x87, 0x18, 0x74, 0x18, 0x9e, 0x18, | ||
0xc9, 0x18, 0xcb, 0x18, 0x26, 0x18, 0x75, 0x18, | ||
0xc5, 0x18, 0x1b, 0x18, 0x69, 0x18, 0xa6, 0x18, | ||
0x89, 0x18, 0x56, 0x10, 0x18, 0x2e, 0x18, 0x8f, | ||
0x18, 0x6f, 0x18, 0x72, 0x18, 0x57, 0x18, 0xb9, | ||
0x18, 0xb9, 0x18, 0x93, 0x18, 0xed, 0x18, 0x88, | ||
0x18, 0x99, 0x18, 0xea, 0x18, 0xfd, 0x18, 0x53, | ||
0x18, 0x82, 0x18, 0x3d, 0x18, 0xca, 0x18, 0xb6, | ||
0x18, 0x41, 0xff | ||
}; | ||
|
||
static size_t encoded_size; | ||
static psa_key_attributes_t attr; | ||
static psa_key_pair_slot_t slot; | ||
static psa_key_pair_slot_t dec_slot; | ||
|
||
static uint8_t cbor_enc[sizeof(cbor_encoded_data)]; | ||
|
||
/** | ||
* @brief CBOR encoding of key pair slot should equal @c cbor_encoded_data | ||
*/ | ||
static void test_encode_asymmetric_keypair_slot(void) | ||
{ | ||
psa_key_lifetime_t lifetime = 1; | ||
psa_key_usage_t usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH; | ||
|
||
psa_set_key_lifetime(&attr, lifetime); | ||
psa_set_key_algorithm(&attr, ECC_ALG); | ||
psa_set_key_usage_flags(&attr, usage); | ||
psa_set_key_type(&attr, ECC_KEY_TYPE); | ||
psa_set_key_bits(&attr, ECC_KEY_SIZE); | ||
|
||
slot.attr = attr; | ||
memcpy(slot.key.privkey_data, privkey, sizeof(privkey)); | ||
slot.key.privkey_data_len = sizeof(privkey); | ||
|
||
memcpy(slot.key.pubkey_data, pubkey, sizeof(pubkey)); | ||
slot.key.pubkey_data_len = sizeof(pubkey); | ||
|
||
TEST_ASSERT_PSA(psa_encode_key_slot((psa_key_slot_t *)&slot, cbor_enc, sizeof(cbor_enc), &encoded_size)); | ||
TEST_ASSERT_EQUAL_INT(sizeof(cbor_encoded_data), encoded_size); | ||
TEST_ASSERT_MESSAGE(1 == compare(cbor_enc, cbor_encoded_data, | ||
encoded_size), "wrong cbor encoding"); | ||
} | ||
|
||
/** | ||
* @brief Decoded key slot should equal key slot structure initialized | ||
* in @c test_encode_asymmetric_keypair_slot. | ||
*/ | ||
static void test_decode_asymmetric_keypair_slot(void) | ||
{ | ||
TEST_ASSERT_PSA(psa_decode_key_attributes(&dec_slot.attr, cbor_encoded_data, sizeof(cbor_encoded_data))); | ||
TEST_ASSERT_PSA(psa_decode_key_slot_data((psa_key_slot_t *)&dec_slot, cbor_encoded_data, sizeof(cbor_encoded_data))); | ||
TEST_ASSERT_MESSAGE(1 == compare((uint8_t *)&slot, (uint8_t *)&dec_slot, | ||
sizeof(psa_key_pair_slot_t)), "wrong cbor decoding"); | ||
} | ||
|
||
Test* tests_psa_crypto_enc_dec_keypair(void) | ||
{ | ||
EMB_UNIT_TESTFIXTURES(fixtures) { | ||
new_TestFixture(test_encode_asymmetric_keypair_slot), | ||
new_TestFixture(test_decode_asymmetric_keypair_slot), | ||
}; | ||
|
||
EMB_UNIT_TESTCALLER(psa_crypto_enc_dec_keypair_tests, NULL, NULL, fixtures); | ||
|
||
return (Test *)&psa_crypto_enc_dec_keypair_tests; | ||
} |
159 changes: 159 additions & 0 deletions
159
tests/sys/psa_crypto_cbor_encoder/test_psa_crypto_enc_dec_prot_key.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright (C) 2023 HAW Hamburg | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
*/ | ||
|
||
/** | ||
* @ingroup tests | ||
* @{ | ||
* | ||
* @file | ||
* @brief Tests CBOR encoding of PSA Crypto protected key slots | ||
* | ||
* @author Lena Boeckmann <lena.boeckmann@haw-hamburg.de> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include "embUnit.h" | ||
#include "psa/crypto.h" | ||
#include "psa_crypto_cbor_encoder.h" | ||
#include "tests_psa_cbor_enc_dec.h" | ||
|
||
static uint8_t cbor_encoded_data[] = { | ||
0x9f, 0x85, 0x08, 0x19, 0x24, 0x00, 0x18, 0x80, | ||
0x1a, 0x80, 0x00, 0x00, 0x00, 0x82, 0x19, 0x03, | ||
0x00, 0x1a, 0x04, 0x40, 0x40, 0x00, 0x81, 0x04, | ||
0xff | ||
}; | ||
|
||
static uint8_t cbor_encoded_data_with_pubkey[] = { | ||
0x9f, 0x85, 0x08, 0x19, 0x71, 0x12, 0x19, 0x01, | ||
0x00, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x82, 0x19, | ||
0x30, 0x00, 0x1a, 0x06, 0x00, 0x06, 0x09, 0x82, | ||
0x04, 0x82, 0x18, 0x41, 0x98, 0x41, 0x04, 0x18, | ||
0x56, 0x18, 0xcb, 0x18, 0x81, 0x18, 0xd1, 0x18, | ||
0xcb, 0x18, 0xde, 0x18, 0x44, 0x18, 0xf5, 0x18, | ||
0x1d, 0x18, 0xcc, 0x18, 0xb1, 0x18, 0x26, 0x02, | ||
0x18, 0x67, 0x0d, 0x18, 0x76, 0x18, 0xdd, 0x18, | ||
0xe7, 0x18, 0x84, 0x18, 0xed, 0x18, 0x8d, 0x18, | ||
0x30, 0x18, 0x72, 0x18, 0x1c, 0x18, 0xca, 0x18, | ||
0x50, 0x18, 0x59, 0x18, 0xf9, 0x18, 0x20, 0x18, | ||
0xad, 0x18, 0x62, 0x18, 0x87, 0x18, 0x74, 0x18, | ||
0x9e, 0x18, 0xc9, 0x18, 0xcb, 0x18, 0x26, 0x18, | ||
0x75, 0x18, 0xc5, 0x18, 0x1b, 0x18, 0x69, 0x18, | ||
0xa6, 0x18, 0x89, 0x18, 0x56, 0x10, 0x18, 0x2e, | ||
0x18, 0x8f, 0x18, 0x6f, 0x18, 0x72, 0x18, 0x57, | ||
0x18, 0xb9, 0x18, 0xb9, 0x18, 0x93, 0x18, 0xed, | ||
0x18, 0x88, 0x18, 0x99, 0x18, 0xea, 0x18, 0xfd, | ||
0x18, 0x53, 0x18, 0x82, 0x18, 0x3d, 0x18, 0xca, | ||
0x18, 0xb6, 0x18, 0x41, 0xff | ||
}; | ||
|
||
static size_t encoded_size; | ||
static psa_key_attributes_t attr; | ||
static psa_prot_key_slot_t slot; | ||
static psa_prot_key_slot_t dec_slot; | ||
|
||
static uint8_t cbor_enc[sizeof(cbor_encoded_data)]; | ||
static uint8_t cbor_enc_with_pubkey[sizeof(cbor_encoded_data_with_pubkey)]; | ||
|
||
/** | ||
* @brief CBOR encoding of key pair slot should equal @c cbor_encoded_data | ||
*/ | ||
static void test_encode_protected_key_slot(void) | ||
{ | ||
memset(&slot, 0, sizeof(psa_prot_key_slot_t)); | ||
memset(&dec_slot, 0, sizeof(psa_prot_key_slot_t)); | ||
|
||
psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION | ||
(PSA_KEY_LIFETIME_VOLATILE, PSA_KEY_LOCATION_SE_MIN); | ||
psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; | ||
|
||
attr = psa_key_attributes_init(); | ||
psa_set_key_lifetime(&attr, lifetime); | ||
psa_set_key_algorithm(&attr, PSA_ALG_CBC_NO_PADDING); | ||
psa_set_key_usage_flags(&attr, usage); | ||
psa_set_key_type(&attr, PSA_KEY_TYPE_AES); | ||
psa_set_key_bits(&attr, PSA_BYTES_TO_BITS(sizeof(AES_KEY))); | ||
psa_set_key_id(&attr, 8); | ||
|
||
slot.attr = attr; | ||
slot.key.slot_number = 4; | ||
|
||
TEST_ASSERT_PSA(psa_encode_key_slot((psa_key_slot_t *)&slot, cbor_enc, sizeof(cbor_enc), &encoded_size)); | ||
TEST_ASSERT_EQUAL_INT(sizeof(cbor_encoded_data), encoded_size); | ||
TEST_ASSERT_MESSAGE(1 == compare(cbor_enc, cbor_encoded_data, encoded_size), | ||
"wrong cbor encoding"); | ||
} | ||
|
||
/** | ||
* @brief CBOR encoding of key pair slot should equal @c cbor_encoded_data_with_pubkey | ||
*/ | ||
static void test_encode_protected_key_slot_with_pubkey(void) | ||
{ | ||
psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION | ||
(PSA_KEY_LIFETIME_VOLATILE, PSA_KEY_LOCATION_SE_MIN); | ||
psa_key_usage_t usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH; | ||
|
||
attr = psa_key_attributes_init(); | ||
psa_set_key_lifetime(&attr, lifetime); | ||
psa_set_key_algorithm(&attr, ECC_ALG); | ||
psa_set_key_usage_flags(&attr, usage); | ||
psa_set_key_type(&attr, ECC_KEY_TYPE); | ||
psa_set_key_bits(&attr, ECC_KEY_SIZE); | ||
psa_set_key_id(&attr, 8); | ||
|
||
slot.attr = attr; | ||
slot.key.slot_number = 4; | ||
|
||
memcpy(slot.key.pubkey_data, pubkey, sizeof(pubkey)); | ||
slot.key.pubkey_data_len = sizeof(pubkey); | ||
|
||
TEST_ASSERT_PSA(psa_encode_key_slot((psa_key_slot_t *)&slot, cbor_enc_with_pubkey, sizeof(cbor_enc_with_pubkey), &encoded_size)); | ||
TEST_ASSERT_EQUAL_INT(sizeof(cbor_encoded_data_with_pubkey), encoded_size); | ||
TEST_ASSERT_MESSAGE(1 == compare(cbor_enc_with_pubkey, cbor_encoded_data_with_pubkey, | ||
encoded_size), "wrong cbor encoding"); | ||
} | ||
|
||
/** | ||
* @brief Decoded key slot should equal key slot structure initialized | ||
* in @c test_encode_protected_key_slot. | ||
*/ | ||
static void test_decode_protected_key_slot(void) | ||
{ | ||
TEST_ASSERT_PSA(psa_decode_key_attributes(&dec_slot.attr, cbor_encoded_data, sizeof(cbor_encoded_data))); | ||
TEST_ASSERT_PSA(psa_decode_key_slot_data((psa_key_slot_t *)&dec_slot, cbor_encoded_data, sizeof(cbor_encoded_data))); | ||
TEST_ASSERT_MESSAGE(1 == compare((uint8_t *)&slot, (uint8_t *)&dec_slot, | ||
sizeof(psa_prot_key_slot_t)), "wrong cbor decoding"); | ||
} | ||
|
||
/** | ||
* @brief Decoded key slot should equal key slot structure initialized | ||
* in @c test_encode_protected_key_slot_with_pubkey. | ||
*/ | ||
static void test_decode_protected_key_slot_with_pubkey(void) | ||
{ | ||
TEST_ASSERT_PSA(psa_decode_key_attributes(&dec_slot.attr, cbor_encoded_data_with_pubkey, sizeof(cbor_encoded_data_with_pubkey))); | ||
TEST_ASSERT_PSA(psa_decode_key_slot_data((psa_key_slot_t *)&dec_slot, cbor_encoded_data_with_pubkey, sizeof(cbor_encoded_data_with_pubkey))); | ||
|
||
TEST_ASSERT_MESSAGE(1 == compare((uint8_t *)&slot, (uint8_t *)&dec_slot, | ||
sizeof(psa_prot_key_slot_t)), "wrong cbor decoding"); | ||
} | ||
|
||
Test* tests_psa_crypto_enc_dec_protected_key(void) | ||
{ | ||
EMB_UNIT_TESTFIXTURES(fixtures) { | ||
new_TestFixture(test_encode_protected_key_slot), | ||
new_TestFixture(test_decode_protected_key_slot), | ||
new_TestFixture(test_encode_protected_key_slot_with_pubkey), | ||
new_TestFixture(test_decode_protected_key_slot_with_pubkey) | ||
}; | ||
|
||
EMB_UNIT_TESTCALLER(psa_crypto_enc_dec_protected_key_tests, NULL, NULL, fixtures); | ||
|
||
return (Test *)&psa_crypto_enc_dec_protected_key_tests; | ||
} |
Oops, something went wrong.