Skip to content

Commit f9c3005

Browse files
committed
tests/sys: add psa_crypto aes ccm test
Update tests/sys/psa_crypto_aead/example_aead_aes_128.c Co-authored-by: mguetschow <[email protected]> Update tests/sys/psa_crypto_aead/example_aead_aes_128.c Co-authored-by: mguetschow <[email protected]> Update tests/sys/psa_crypto_aead/example_aead_aes_128.c Co-authored-by: mguetschow <[email protected]> Update tests/sys/psa_crypto_aead/example_aead_aes_128.c Update tests/sys/psa_crypto_aead/example_aead_aes_128.c
1 parent b07d0fb commit f9c3005

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

tests/sys/psa_crypto_aead/Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
include ../Makefile.sys_common
2+
3+
USEMODULE += ztimer
4+
USEMODULE += ztimer_usec
5+
USEMODULE += psa_crypto
6+
7+
USEMODULE += psa_aead
8+
USEMODULE += psa_aead_aes_128_ccm
9+
10+
CFLAGS += -DCONFIG_PSA_SINGLE_KEY_COUNT=2
11+
12+
include $(RIOTBASE)/Makefile.include
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (C) 2025 TU Dresden
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup tests
11+
* @{
12+
*
13+
* @brief Tests the PSA aead configurations
14+
*
15+
* @author Lukas Luger <[email protected]>
16+
*
17+
* @}
18+
*/
19+
20+
#include <stdio.h>
21+
#include <stdint.h>
22+
#include "od.h"
23+
#include "psa/crypto.h"
24+
25+
#define AES_128_KEY_SIZE (16)
26+
27+
static const uint8_t KEY_128[] = {
28+
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
29+
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
30+
};
31+
/* certain PSA backends require the data to be in RAM rather than ROM
32+
* so these values cannot be `const` */
33+
static uint8_t NONCE[] = {
34+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
35+
};
36+
37+
static uint8_t ADDITIONAL_DATA[] = {
38+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
39+
};
40+
41+
static uint8_t PLAINTEXT[] = {
42+
0x20, 0x21, 0x22, 0x23
43+
};
44+
45+
static uint8_t CIPHERTEXT[] = {
46+
0x71, 0x62, 0x01, 0x5b, 0x4d, 0xac, 0x25, 0x5d
47+
};
48+
49+
/**
50+
* @brief Example function to perform an AES-128 CCM encryption and decryption
51+
* with the PSA Crypto API.
52+
*
53+
* @return psa_status_t
54+
*/
55+
psa_status_t example_aead_aes_128_ccm(void)
56+
{
57+
psa_status_t status = PSA_ERROR_DOES_NOT_EXIST;
58+
psa_key_id_t key_id = 0;
59+
psa_key_attributes_t attr = psa_key_attributes_init();
60+
psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
61+
psa_algorithm_t algo = PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 4);
62+
size_t encr_output_size = PSA_AEAD_ENCRYPT_OUTPUT_SIZE(PSA_KEY_TYPE_AES,
63+
algo, sizeof(PLAINTEXT));
64+
65+
uint8_t cipher_out[encr_output_size];
66+
uint8_t plain_out[sizeof(PLAINTEXT)];
67+
size_t output_len = 0;
68+
69+
psa_set_key_algorithm(&attr, algo);
70+
psa_set_key_usage_flags(&attr, usage);
71+
psa_set_key_bits(&attr, 128);
72+
psa_set_key_type(&attr, PSA_KEY_TYPE_AES);
73+
74+
status = psa_import_key(&attr, KEY_128, AES_128_KEY_SIZE, &key_id);
75+
if (status != PSA_SUCCESS) {
76+
psa_destroy_key(key_id);
77+
return status;
78+
}
79+
80+
status = psa_aead_encrypt(key_id, algo, NONCE, sizeof(NONCE), ADDITIONAL_DATA, sizeof(ADDITIONAL_DATA),
81+
PLAINTEXT, sizeof(PLAINTEXT), cipher_out, encr_output_size, &output_len);
82+
if (status != PSA_SUCCESS) {
83+
psa_destroy_key(key_id);
84+
return status;
85+
}
86+
87+
if (memcmp(CIPHERTEXT, cipher_out, sizeof(CIPHERTEXT))) {
88+
printf("AES 128 CCM: wrong ciphertext on encryption\n");
89+
psa_destroy_key(key_id);
90+
return -1;
91+
}
92+
93+
status = psa_aead_decrypt(key_id, algo, NONCE, sizeof(NONCE), ADDITIONAL_DATA, sizeof(ADDITIONAL_DATA),
94+
cipher_out, sizeof(cipher_out), plain_out, sizeof(plain_out),
95+
&output_len);
96+
psa_destroy_key(key_id);
97+
if (status == PSA_SUCCESS && memcmp(PLAINTEXT, plain_out, sizeof(plain_out))) {
98+
printf("AES 128 CCM: wrong plaintext on decryption\n");
99+
return -1;
100+
}
101+
return status;
102+
}

tests/sys/psa_crypto_aead/main.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2025 TU Dresden
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
/**
10+
* @ingroup tests
11+
* @{
12+
*
13+
* @brief Tests the PSA aead configurations
14+
*
15+
* @author Lukas Luger <[email protected]>
16+
*
17+
* @}
18+
*/
19+
20+
#include <stdio.h>
21+
#include "psa/crypto.h"
22+
#include "ztimer.h"
23+
24+
extern psa_status_t example_aead_aes_128_ccm(void);
25+
26+
int main(void)
27+
{
28+
bool failed = false;
29+
psa_status_t status;
30+
31+
psa_crypto_init();
32+
33+
ztimer_acquire(ZTIMER_USEC);
34+
ztimer_now_t start = ztimer_now(ZTIMER_USEC);
35+
36+
start = ztimer_now(ZTIMER_USEC);
37+
status = example_aead_aes_128_ccm();
38+
printf("Authenticated encryption with associated data AES 128 CCM took %d us\n",
39+
(int)(ztimer_now(ZTIMER_USEC) - start));
40+
if (status != PSA_SUCCESS) {
41+
failed = true;
42+
printf("Authenticated encryption with associated data AES 128 CCM failed: %s\n",
43+
psa_status_to_humanly_readable(status));
44+
}
45+
46+
ztimer_release(ZTIMER_USEC);
47+
48+
if (failed) {
49+
puts("Tests failed...");
50+
}
51+
else {
52+
puts("All Done");
53+
}
54+
return 0;
55+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from testrunner import run
5+
6+
7+
def testfunc(child):
8+
child.expect_exact('All Done')
9+
print("[TEST PASSED]")
10+
11+
12+
if __name__ == "__main__":
13+
sys.exit(run(testfunc))

0 commit comments

Comments
 (0)