1 /*
2 * Copyright (C) 2025, BayLibre SAS
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6 #include <zephyr/kernel.h>
7 #include <psa/crypto.h>
8
9 #define BUF_SIZE 1024
10 #define LABEL_FORMAT "%-24s: "
11
12 #define TIMER_DURATION K_MSEC(1000)
13 #define TIMER_PERIOD TIMER_DURATION
14 volatile int timer_expired;
timer_expired_callback(struct k_timer * timer)15 void timer_expired_callback(struct k_timer *timer)
16 {
17 timer_expired = 1;
18 }
19
20 static struct k_timer timer;
21
22 uint8_t in_buf[BUF_SIZE];
23 uint8_t out_buf[BUF_SIZE];
24
25 #define COMPUTE_THROUGHPUT(LABEL, CODE) \
26 do { \
27 unsigned long i; \
28 \
29 printk(LABEL_FORMAT, LABEL); \
30 \
31 status = PSA_SUCCESS; \
32 timer_expired = 0; \
33 k_timer_start(&timer, TIMER_DURATION, TIMER_PERIOD); \
34 for (i = 1; status == PSA_SUCCESS && !timer_expired; i++) { \
35 status = CODE; \
36 } \
37 \
38 if (status != PSA_SUCCESS) { \
39 k_timer_stop(&timer); \
40 printk("Fail (%d)\n", status); \
41 } else { \
42 printk("%lu Ki/s\n", (i * BUF_SIZE) / 1024); \
43 } \
44 } while (0)
45
make_cipher_key(psa_key_type_t key_type,psa_algorithm_t alg,mbedtls_svc_key_id_t * key_id)46 static psa_status_t make_cipher_key(psa_key_type_t key_type,
47 psa_algorithm_t alg,
48 mbedtls_svc_key_id_t *key_id)
49 {
50 uint8_t tmp_key[32] = { 0x5 };
51 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
52
53 psa_set_key_type(&key_attr, key_type);
54 psa_set_key_algorithm(&key_attr, alg);
55 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_ENCRYPT);
56 if (psa_import_key(&key_attr, tmp_key, sizeof(tmp_key), key_id) != PSA_SUCCESS) {
57 printk("Key import failed\n");
58 return PSA_ERROR_GENERIC_ERROR;
59 }
60
61 return PSA_SUCCESS;
62 }
63
main(void)64 int main(void)
65 {
66 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
67 psa_status_t status = PSA_SUCCESS;
68 size_t out_len;
69
70 k_timer_init(&timer, timer_expired_callback, NULL);
71
72 memset(in_buf, 0xaa, sizeof(in_buf));
73
74 /* HASH */
75
76 COMPUTE_THROUGHPUT("SHA-1",
77 psa_hash_compute(PSA_ALG_SHA_1, in_buf, sizeof(in_buf),
78 out_buf, sizeof(out_buf), &out_len)
79 );
80
81 COMPUTE_THROUGHPUT("SHA-224",
82 psa_hash_compute(PSA_ALG_SHA_224, in_buf, sizeof(in_buf),
83 out_buf, sizeof(out_buf), &out_len)
84 );
85
86 COMPUTE_THROUGHPUT("SHA-256",
87 psa_hash_compute(PSA_ALG_SHA_256, in_buf, sizeof(in_buf),
88 out_buf, sizeof(out_buf), &out_len)
89 );
90
91 COMPUTE_THROUGHPUT("SHA-384",
92 psa_hash_compute(PSA_ALG_SHA_384, in_buf, sizeof(in_buf),
93 out_buf, sizeof(out_buf), &out_len)
94 );
95
96 COMPUTE_THROUGHPUT("SHA-512",
97 psa_hash_compute(PSA_ALG_SHA_512, in_buf, sizeof(in_buf),
98 out_buf, sizeof(out_buf), &out_len)
99 );
100
101 /* Ciphers */
102
103 status = make_cipher_key(PSA_KEY_TYPE_AES, PSA_ALG_ECB_NO_PADDING, &key_id);
104 if (status == PSA_SUCCESS) {
105 COMPUTE_THROUGHPUT("AES-256-ECB",
106 psa_cipher_encrypt(key_id, PSA_ALG_ECB_NO_PADDING,
107 in_buf, sizeof(in_buf),
108 out_buf, sizeof(out_buf), &out_len)
109 );
110 psa_destroy_key(key_id);
111 } else {
112 printk("Failed to import AES key (%d)", status);
113 }
114
115 status = make_cipher_key(PSA_KEY_TYPE_ARIA, PSA_ALG_ECB_NO_PADDING, &key_id);
116 if (status == PSA_SUCCESS) {
117 COMPUTE_THROUGHPUT("ARIA-256-ECB",
118 psa_cipher_encrypt(key_id, PSA_ALG_ECB_NO_PADDING,
119 in_buf, sizeof(in_buf),
120 out_buf, sizeof(out_buf), &out_len)
121 );
122 psa_destroy_key(key_id);
123 } else {
124 printk("Failed to import ARIA key (%d)", status);
125 }
126
127 status = make_cipher_key(PSA_KEY_TYPE_CAMELLIA, PSA_ALG_ECB_NO_PADDING, &key_id);
128 if (status == PSA_SUCCESS) {
129 COMPUTE_THROUGHPUT("CAMELLIA-256-ECB",
130 psa_cipher_encrypt(key_id, PSA_ALG_ECB_NO_PADDING,
131 in_buf, sizeof(in_buf),
132 out_buf, sizeof(out_buf), &out_len)
133 );
134 psa_destroy_key(key_id);
135 } else {
136 printk("Failed to import Camellia key (%d)", status);
137 }
138
139 printk("Benchmark completed\n");
140 return 0;
141 }
142