Skip to content

Commit 43c55ef

Browse files
committed
tests: crypto: use IS_ENABLED() for conditional logic instead of #if
Refactor psa_crypto_test to use IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS) inside the test function for better readability and maintainability. - Replaced #if / #endif blocks inside functions with IS_ENABLED() checks so conditions can be expressed as normal C expressions. - Retained #if only for global constant definitions where IS_ENABLED() cannot be used by the preprocessor. - Added <zephyr/sys/util.h> include to access IS_ENABLED(). - Ensures dead code is optimized out by the compiler without runtime cost. Signed-off-by: Aasim Shaik <[email protected]>
1 parent 8c02821 commit 43c55ef

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

tests/crypto/psa_crypto/src/cipher.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
9-
9+
#include <zephyr/sys/util.h> /* for IS_ENABLED() */
1010
#include "test_vectors.h"
1111

1212
uint8_t key_256[32] = {
@@ -40,15 +40,15 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_multipart)
4040
psa_set_key_algorithm(&attributes, alg);
4141
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
4242
psa_set_key_bits(&attributes, 256);
43-
#if (CONFIG_TEST_WRAPPED_KEYS)
43+
if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) {
4444
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
4545
PSA_KEY_PERSISTENCE_VOLATILE, 0));
4646
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
4747
"Failed to generate key");
48-
# else
48+
} else {
4949
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
5050
PSA_SUCCESS, "Failed to import key");
51-
#endif
51+
}
5252
psa_reset_key_attributes(&attributes);
5353
zassert_equal(psa_cipher_encrypt_setup(&operation, key_id, alg), PSA_SUCCESS,
5454
"Failed to begin encrypt operation");
@@ -95,15 +95,15 @@ ZTEST(psa_crypto_test, test_cipher_aes_cbc_256_single)
9595
psa_set_key_algorithm(&attributes, alg);
9696
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
9797
psa_set_key_bits(&attributes, 256);
98-
#if (CONFIG_TEST_WRAPPED_KEYS)
98+
if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) {
9999
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
100100
PSA_KEY_PERSISTENCE_VOLATILE, 1));
101101
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
102102
"Failed to generate key");
103-
# else
103+
} else {
104104
zassert_equal(psa_import_key(&attributes, key_256, sizeof(key_256), &key_id),
105105
PSA_SUCCESS, "Failed to import key");
106-
#endif
106+
}
107107
psa_reset_key_attributes(&attributes);
108108

109109
zassert_equal(psa_cipher_encrypt(key_id, alg, plaintext, sizeof(plaintext),
@@ -133,16 +133,16 @@ ZTEST(psa_crypto_test, test_cipher_aes_ecb_128_single)
133133
psa_set_key_algorithm(&attributes, alg);
134134
psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
135135
psa_set_key_bits(&attributes, 128);
136-
#if (CONFIG_TEST_WRAPPED_KEYS)
136+
if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) {
137137
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
138138
PSA_KEY_PERSISTENCE_VOLATILE, 1));
139139

140140
zassert_equal(psa_generate_key(&attributes, &key_id), PSA_SUCCESS,
141141
"Failed to generate key");
142-
# else
142+
} else {
143143
zassert_equal(psa_import_key(&attributes, key_128, sizeof(key_128), &key_id),
144144
PSA_SUCCESS, "Failed to import key");
145-
#endif
145+
}
146146
psa_reset_key_attributes(&attributes);
147147

148148
zassert_equal(psa_cipher_encrypt(key_id, alg, plaintext, sizeof(plaintext), ciphertext,

tests/crypto/psa_crypto/src/key_agreement.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <zephyr/ztest.h>
88
#include <psa/crypto.h>
9+
#include <zephyr/sys/util.h> /* for IS_ENABLED() */
910

1011
static const uint8_t client_private_key[] = {
1112
0xB0, 0x76, 0x51, 0xEA, 0x20, 0xF0, 0x28, 0xA8, 0x16, 0xEE, 0x01,
@@ -27,7 +28,7 @@ static const uint8_t server_public_key[] = {
2728
0x8D, 0x49, 0x85, 0x8C, 0x7A, 0x9F, 0xC1, 0x46, 0xDA, 0xCC, 0x96,
2829
0xEF, 0x6E, 0xD4, 0xDA, 0x71, 0xBF, 0xED, 0x32, 0x0D, 0x76,
2930
};
30-
#if (!CONFIG_TEST_WRAPPED_KEYS)
31+
#if !IS_ENABLE(CONFIG_TEST_WRAPPED_KEYS)
3132
static const uint8_t expected_shared_secret[] = {
3233
0xF2, 0xE6, 0x0E, 0x1C, 0xB7, 0x64, 0xBC, 0x48, 0xF2, 0x9D, 0xBB,
3334
0x12, 0xFB, 0x12, 0x17, 0x31, 0x32, 0x1D, 0x79, 0xAF, 0x0A, 0x9F,
@@ -45,10 +46,10 @@ ZTEST(psa_crypto_test, test_key_agreement_ecdh_25519)
4546
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY));
4647
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
4748
psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
48-
#if (CONFIG_TEST_WRAPPED_KEYS)
49+
if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)){
4950
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
5051
PSA_KEY_PERSISTENCE_VOLATILE, 0));
51-
#endif
52+
}
5253
zassert_equal(psa_import_key(&attributes, client_private_key, sizeof(client_private_key),
5354
&key_id),
5455
PSA_SUCCESS, "Failed to import client key");
@@ -66,10 +67,10 @@ ZTEST(psa_crypto_test, test_key_agreement_ecdh_25519)
6667
psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY));
6768
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
6869
psa_set_key_algorithm(&attributes, PSA_ALG_ECDH);
69-
#if (CONFIG_TEST_WRAPPED_KEYS)
70+
if (IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) {
7071
psa_set_key_lifetime(&attributes, PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
7172
PSA_KEY_PERSISTENCE_VOLATILE, 0));
72-
#endif
73+
}
7374
zassert_equal(psa_import_key(&attributes, server_private_key, sizeof(server_private_key),
7475
&key_id),
7576
PSA_SUCCESS, "Failed to import server key");
@@ -81,10 +82,10 @@ ZTEST(psa_crypto_test, test_key_agreement_ecdh_25519)
8182
PSA_SUCCESS, "Failed to perform key agreement with client");
8283

8384
zassert_equal(psa_destroy_key(key_id), PSA_SUCCESS, "Failed to destroy server key");
84-
#if (!CONFIG_TEST_WRAPPED_KEYS)
85+
if (!IS_ENABLED(CONFIG_TEST_WRAPPED_KEYS)) {
8586
/* Verify shared secret */
8687
zassert_mem_equal(shared_secret_buf, expected_shared_secret,
8788
sizeof(expected_shared_secret),
8889
"Key agreement did not resolve the correct shared secret");
89-
#endif
90+
}
9091
}

0 commit comments

Comments
 (0)