1a8e1175bSopenharmony_ci/* BEGIN_HEADER */
2a8e1175bSopenharmony_ci/* Test macros that provide metadata about algorithms and key types.
3a8e1175bSopenharmony_ci * This test suite only contains tests that don't require executing
4a8e1175bSopenharmony_ci * code. Other test suites validate macros that require creating a key
5a8e1175bSopenharmony_ci * and using it. */
6a8e1175bSopenharmony_ci
7a8e1175bSopenharmony_ci#if defined(MBEDTLS_PSA_CRYPTO_SPM)
8a8e1175bSopenharmony_ci#include "spm/psa_defs.h"
9a8e1175bSopenharmony_ci#endif
10a8e1175bSopenharmony_ci
11a8e1175bSopenharmony_ci#include "psa/crypto.h"
12a8e1175bSopenharmony_ci#include "psa_crypto_invasive.h"
13a8e1175bSopenharmony_ci
14a8e1175bSopenharmony_ci/* Flags for algorithm classification macros. There is a flag for every
15a8e1175bSopenharmony_ci * algorithm classification macro PSA_ALG_IS_xxx except for the
16a8e1175bSopenharmony_ci * category test macros, which are hard-coded in each
17a8e1175bSopenharmony_ci * category-specific function. The name of the flag is the name of the
18a8e1175bSopenharmony_ci * classification macro without the PSA_ prefix. */
19a8e1175bSopenharmony_ci#define ALG_IS_VENDOR_DEFINED           (1u << 0)
20a8e1175bSopenharmony_ci#define ALG_IS_HMAC                     (1u << 1)
21a8e1175bSopenharmony_ci#define ALG_IS_BLOCK_CIPHER_MAC         (1u << 2)
22a8e1175bSopenharmony_ci#define ALG_IS_STREAM_CIPHER            (1u << 3)
23a8e1175bSopenharmony_ci#define ALG_IS_RSA_PKCS1V15_SIGN        (1u << 4)
24a8e1175bSopenharmony_ci#define ALG_IS_RSA_PSS                  (1u << 5)
25a8e1175bSopenharmony_ci#define ALG_IS_RSA_PSS_ANY_SALT         (1u << 6)
26a8e1175bSopenharmony_ci#define ALG_IS_RSA_PSS_STANDARD_SALT    (1u << 7)
27a8e1175bSopenharmony_ci#define ALG_IS_DSA                      (1u << 8)
28a8e1175bSopenharmony_ci#define ALG_DSA_IS_DETERMINISTIC        (1u << 9)
29a8e1175bSopenharmony_ci#define ALG_IS_DETERMINISTIC_DSA        (1u << 10)
30a8e1175bSopenharmony_ci#define ALG_IS_RANDOMIZED_DSA           (1u << 11)
31a8e1175bSopenharmony_ci#define ALG_IS_ECDSA                    (1u << 12)
32a8e1175bSopenharmony_ci#define ALG_ECDSA_IS_DETERMINISTIC      (1u << 13)
33a8e1175bSopenharmony_ci#define ALG_IS_DETERMINISTIC_ECDSA      (1u << 14)
34a8e1175bSopenharmony_ci#define ALG_IS_RANDOMIZED_ECDSA         (1u << 15)
35a8e1175bSopenharmony_ci#define ALG_IS_HASH_EDDSA               (1u << 16)
36a8e1175bSopenharmony_ci#define ALG_IS_SIGN_HASH                (1u << 17)
37a8e1175bSopenharmony_ci#define ALG_IS_HASH_AND_SIGN            (1u << 18)
38a8e1175bSopenharmony_ci#define ALG_IS_RSA_OAEP                 (1u << 19)
39a8e1175bSopenharmony_ci#define ALG_IS_HKDF                     (1u << 20)
40a8e1175bSopenharmony_ci#define ALG_IS_HKDF_EXTRACT             (1u << 21)
41a8e1175bSopenharmony_ci#define ALG_IS_HKDF_EXPAND              (1u << 22)
42a8e1175bSopenharmony_ci#define ALG_IS_FFDH                     (1u << 23)
43a8e1175bSopenharmony_ci#define ALG_IS_ECDH                     (1u << 24)
44a8e1175bSopenharmony_ci#define ALG_IS_WILDCARD                 (1u << 25)
45a8e1175bSopenharmony_ci#define ALG_IS_RAW_KEY_AGREEMENT        (1u << 26)
46a8e1175bSopenharmony_ci#define ALG_IS_AEAD_ON_BLOCK_CIPHER     (1u << 27)
47a8e1175bSopenharmony_ci#define ALG_IS_TLS12_PRF                (1u << 28)
48a8e1175bSopenharmony_ci#define ALG_IS_TLS12_PSK_TO_MS          (1u << 29)
49a8e1175bSopenharmony_ci#define ALG_FLAG_MASK_PLUS_ONE          (1u << 30)   /* must be last! */
50a8e1175bSopenharmony_ci
51a8e1175bSopenharmony_ci/* Flags for key type classification macros. There is a flag for every
52a8e1175bSopenharmony_ci * key type classification macro PSA_KEY_TYPE_IS_xxx except for some that
53a8e1175bSopenharmony_ci * are tested as derived from other macros. The name of the flag is
54a8e1175bSopenharmony_ci * the name of the classification macro without the PSA_ prefix. */
55a8e1175bSopenharmony_ci#define KEY_TYPE_IS_VENDOR_DEFINED      (1u << 0)
56a8e1175bSopenharmony_ci#define KEY_TYPE_IS_UNSTRUCTURED        (1u << 1)
57a8e1175bSopenharmony_ci#define KEY_TYPE_IS_PUBLIC_KEY          (1u << 2)
58a8e1175bSopenharmony_ci#define KEY_TYPE_IS_KEY_PAIR            (1u << 3)
59a8e1175bSopenharmony_ci#define KEY_TYPE_IS_RSA                 (1u << 4)
60a8e1175bSopenharmony_ci#define KEY_TYPE_IS_DSA                 (1u << 5)
61a8e1175bSopenharmony_ci#define KEY_TYPE_IS_ECC                 (1u << 6)
62a8e1175bSopenharmony_ci#define KEY_TYPE_IS_DH                  (1u << 7)
63a8e1175bSopenharmony_ci#define KEY_TYPE_FLAG_MASK_PLUS_ONE     (1u << 8)   /* must be last! */
64a8e1175bSopenharmony_ci
65a8e1175bSopenharmony_ci/* Flags for lifetime classification macros. There is a flag for every
66a8e1175bSopenharmony_ci * lifetime classification macro PSA_KEY_LIFETIME_IS_xxx. The name of the
67a8e1175bSopenharmony_ci * flag is the name of the classification macro without the PSA_ prefix. */
68a8e1175bSopenharmony_ci#define KEY_LIFETIME_IS_VOLATILE        (1u << 0)
69a8e1175bSopenharmony_ci#define KEY_LIFETIME_IS_READ_ONLY       (1u << 1)
70a8e1175bSopenharmony_ci#define KEY_LIFETIME_FLAG_MASK_PLUS_ONE (1u << 2)   /* must be last! */
71a8e1175bSopenharmony_ci
72a8e1175bSopenharmony_ci/* Check that in the value of flags, the bit flag (which should be a macro
73a8e1175bSopenharmony_ci * expanding to a number of the form 1 << k) is set if and only if
74a8e1175bSopenharmony_ci * PSA_##flag(alg) is true.
75a8e1175bSopenharmony_ci *
76a8e1175bSopenharmony_ci * Only perform this check if cond is true. Typically cond is 1, but it can
77a8e1175bSopenharmony_ci * be different if the value of the flag bit is only specified under specific
78a8e1175bSopenharmony_ci * conditions.
79a8e1175bSopenharmony_ci *
80a8e1175bSopenharmony_ci * Unconditionally mask flag into the ambient variable
81a8e1175bSopenharmony_ci * classification_flags_tested.
82a8e1175bSopenharmony_ci */
83a8e1175bSopenharmony_ci#define TEST_CLASSIFICATION_MACRO(cond, flag, alg, flags)     \
84a8e1175bSopenharmony_ci    do                                                          \
85a8e1175bSopenharmony_ci    {                                                           \
86a8e1175bSopenharmony_ci        if (cond)                                              \
87a8e1175bSopenharmony_ci        {                                                       \
88a8e1175bSopenharmony_ci            if ((flags) & (flag))                          \
89a8e1175bSopenharmony_ci            TEST_ASSERT(PSA_##flag(alg));               \
90a8e1175bSopenharmony_ci            else                                                \
91a8e1175bSopenharmony_ci            TEST_ASSERT(!PSA_##flag(alg));             \
92a8e1175bSopenharmony_ci        }                                                       \
93a8e1175bSopenharmony_ci        classification_flags_tested |= (flag);                \
94a8e1175bSopenharmony_ci    }                                                           \
95a8e1175bSopenharmony_ci    while (0)
96a8e1175bSopenharmony_ci
97a8e1175bSopenharmony_ci/* Check the parity of value.
98a8e1175bSopenharmony_ci *
99a8e1175bSopenharmony_ci * There are several numerical encodings for which the PSA Cryptography API
100a8e1175bSopenharmony_ci * specification deliberately defines encodings that all have the same
101a8e1175bSopenharmony_ci * parity. This way, a data glitch that flips one bit in the data cannot
102a8e1175bSopenharmony_ci * possibly turn a valid encoding into another valid encoding. Here in
103a8e1175bSopenharmony_ci * the tests, we check that the values (including Mbed TLS vendor-specific
104a8e1175bSopenharmony_ci * values) have the expected parity.
105a8e1175bSopenharmony_ci *
106a8e1175bSopenharmony_ci * The expected parity is even so that 0 is considered a valid encoding.
107a8e1175bSopenharmony_ci *
108a8e1175bSopenharmony_ci * Return a nonzero value if value has even parity and 0 otherwise. */
109a8e1175bSopenharmony_ciint has_even_parity(uint32_t value)
110a8e1175bSopenharmony_ci{
111a8e1175bSopenharmony_ci    value ^= value >> 16;
112a8e1175bSopenharmony_ci    value ^= value >> 8;
113a8e1175bSopenharmony_ci    value ^= value >> 4;
114a8e1175bSopenharmony_ci    return 0x9669 & 1 << (value & 0xf);
115a8e1175bSopenharmony_ci}
116a8e1175bSopenharmony_ci#define TEST_PARITY(value)                    \
117a8e1175bSopenharmony_ci    TEST_ASSERT(has_even_parity(value))
118a8e1175bSopenharmony_ci
119a8e1175bSopenharmony_civoid algorithm_classification(psa_algorithm_t alg, unsigned flags)
120a8e1175bSopenharmony_ci{
121a8e1175bSopenharmony_ci    unsigned classification_flags_tested = 0;
122a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_VENDOR_DEFINED, alg, flags);
123a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_HMAC, alg, flags);
124a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_BLOCK_CIPHER_MAC, alg, flags);
125a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_STREAM_CIPHER, alg, flags);
126a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RSA_PKCS1V15_SIGN, alg, flags);
127a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RSA_PSS, alg, flags);
128a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RSA_PSS_ANY_SALT, alg, flags);
129a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RSA_PSS_STANDARD_SALT, alg, flags);
130a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_DSA, alg, flags);
131a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(PSA_ALG_IS_DSA(alg),
132a8e1175bSopenharmony_ci                              ALG_DSA_IS_DETERMINISTIC, alg, flags);
133a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_DETERMINISTIC_DSA, alg, flags);
134a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RANDOMIZED_DSA, alg, flags);
135a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_ECDSA, alg, flags);
136a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(PSA_ALG_IS_ECDSA(alg),
137a8e1175bSopenharmony_ci                              ALG_ECDSA_IS_DETERMINISTIC, alg, flags);
138a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_DETERMINISTIC_ECDSA, alg, flags);
139a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RANDOMIZED_ECDSA, alg, flags);
140a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_HASH_EDDSA, alg, flags);
141a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_SIGN_HASH, alg, flags);
142a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_HASH_AND_SIGN, alg, flags);
143a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RSA_OAEP, alg, flags);
144a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_HKDF, alg, flags);
145a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_HKDF_EXTRACT, alg, flags);
146a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_HKDF_EXPAND, alg, flags);
147a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_WILDCARD, alg, flags);
148a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_ECDH, alg, flags);
149a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_FFDH, alg, flags);
150a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_RAW_KEY_AGREEMENT, alg, flags);
151a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_AEAD_ON_BLOCK_CIPHER, alg, flags);
152a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_TLS12_PRF, alg, flags);
153a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, ALG_IS_TLS12_PSK_TO_MS, alg, flags);
154a8e1175bSopenharmony_ci    TEST_EQUAL(classification_flags_tested, ALG_FLAG_MASK_PLUS_ONE - 1);
155a8e1175bSopenharmony_ciexit:;
156a8e1175bSopenharmony_ci}
157a8e1175bSopenharmony_ci
158a8e1175bSopenharmony_civoid key_type_classification(psa_key_type_t type, unsigned flags)
159a8e1175bSopenharmony_ci{
160a8e1175bSopenharmony_ci    unsigned classification_flags_tested = 0;
161a8e1175bSopenharmony_ci
162a8e1175bSopenharmony_ci    /* Macros tested based on the test case parameter */
163a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_VENDOR_DEFINED, type, flags);
164a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_UNSTRUCTURED, type, flags);
165a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_PUBLIC_KEY, type, flags);
166a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_KEY_PAIR, type, flags);
167a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_RSA, type, flags);
168a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_DSA, type, flags);
169a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_ECC, type, flags);
170a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_TYPE_IS_DH, type, flags);
171a8e1175bSopenharmony_ci    TEST_EQUAL(classification_flags_tested, KEY_TYPE_FLAG_MASK_PLUS_ONE - 1);
172a8e1175bSopenharmony_ci
173a8e1175bSopenharmony_ci    /* Macros with derived semantics */
174a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_IS_ASYMMETRIC(type),
175a8e1175bSopenharmony_ci               (PSA_KEY_TYPE_IS_PUBLIC_KEY(type) ||
176a8e1175bSopenharmony_ci                PSA_KEY_TYPE_IS_KEY_PAIR(type)));
177a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type),
178a8e1175bSopenharmony_ci               (PSA_KEY_TYPE_IS_ECC(type) &&
179a8e1175bSopenharmony_ci                PSA_KEY_TYPE_IS_KEY_PAIR(type)));
180a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(type),
181a8e1175bSopenharmony_ci               (PSA_KEY_TYPE_IS_ECC(type) &&
182a8e1175bSopenharmony_ci                PSA_KEY_TYPE_IS_PUBLIC_KEY(type)));
183a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_IS_DH_KEY_PAIR(type),
184a8e1175bSopenharmony_ci               (PSA_KEY_TYPE_IS_DH(type) &&
185a8e1175bSopenharmony_ci                PSA_KEY_TYPE_IS_KEY_PAIR(type)));
186a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_IS_DH_PUBLIC_KEY(type),
187a8e1175bSopenharmony_ci               (PSA_KEY_TYPE_IS_DH(type) &&
188a8e1175bSopenharmony_ci                PSA_KEY_TYPE_IS_PUBLIC_KEY(type)));
189a8e1175bSopenharmony_ci
190a8e1175bSopenharmony_ci    TEST_PARITY(type);
191a8e1175bSopenharmony_ci
192a8e1175bSopenharmony_ciexit:;
193a8e1175bSopenharmony_ci}
194a8e1175bSopenharmony_ci
195a8e1175bSopenharmony_civoid mac_algorithm_core(psa_algorithm_t alg, int classification_flags,
196a8e1175bSopenharmony_ci                        psa_key_type_t key_type, size_t key_bits,
197a8e1175bSopenharmony_ci                        size_t length)
198a8e1175bSopenharmony_ci{
199a8e1175bSopenharmony_ci    /* Algorithm classification */
200a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
201a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_MAC(alg));
202a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
203a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
204a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
205a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
206a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
207a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
208a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
209a8e1175bSopenharmony_ci    algorithm_classification(alg, classification_flags);
210a8e1175bSopenharmony_ci
211a8e1175bSopenharmony_ci    /* Length */
212a8e1175bSopenharmony_ci    TEST_EQUAL(length, PSA_MAC_LENGTH(key_type, key_bits, alg));
213a8e1175bSopenharmony_ci
214a8e1175bSopenharmony_ci#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_PSA_CRYPTO_C)
215a8e1175bSopenharmony_ci    PSA_ASSERT(psa_mac_key_can_do(alg, key_type));
216a8e1175bSopenharmony_ci#endif
217a8e1175bSopenharmony_ci
218a8e1175bSopenharmony_ciexit:;
219a8e1175bSopenharmony_ci}
220a8e1175bSopenharmony_ci
221a8e1175bSopenharmony_civoid aead_algorithm_core(psa_algorithm_t alg, int classification_flags,
222a8e1175bSopenharmony_ci                         psa_key_type_t key_type, size_t key_bits,
223a8e1175bSopenharmony_ci                         size_t tag_length)
224a8e1175bSopenharmony_ci{
225a8e1175bSopenharmony_ci    /* Algorithm classification */
226a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
227a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
228a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
229a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_AEAD(alg));
230a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
231a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
232a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
233a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
234a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
235a8e1175bSopenharmony_ci    algorithm_classification(alg, classification_flags);
236a8e1175bSopenharmony_ci
237a8e1175bSopenharmony_ci    /* Tag length */
238a8e1175bSopenharmony_ci    TEST_EQUAL(tag_length, PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg));
239a8e1175bSopenharmony_ci
240a8e1175bSopenharmony_ciexit:;
241a8e1175bSopenharmony_ci}
242a8e1175bSopenharmony_ci
243a8e1175bSopenharmony_ci/* END_HEADER */
244a8e1175bSopenharmony_ci
245a8e1175bSopenharmony_ci/* BEGIN_DEPENDENCIES
246a8e1175bSopenharmony_ci * depends_on:MBEDTLS_PSA_CRYPTO_CLIENT
247a8e1175bSopenharmony_ci * END_DEPENDENCIES
248a8e1175bSopenharmony_ci */
249a8e1175bSopenharmony_ci
250a8e1175bSopenharmony_ci/* BEGIN_CASE */
251a8e1175bSopenharmony_civoid hash_algorithm(int alg_arg, int length_arg)
252a8e1175bSopenharmony_ci{
253a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
254a8e1175bSopenharmony_ci    size_t length = length_arg;
255a8e1175bSopenharmony_ci    psa_algorithm_t hmac_alg = PSA_ALG_HMAC(alg);
256a8e1175bSopenharmony_ci    psa_algorithm_t rsa_pkcs1v15_sign_alg = PSA_ALG_RSA_PKCS1V15_SIGN(alg);
257a8e1175bSopenharmony_ci    psa_algorithm_t rsa_pss_alg = PSA_ALG_RSA_PSS(alg);
258a8e1175bSopenharmony_ci    psa_algorithm_t dsa_alg = PSA_ALG_DSA(alg);
259a8e1175bSopenharmony_ci    psa_algorithm_t deterministic_dsa_alg = PSA_ALG_DETERMINISTIC_DSA(alg);
260a8e1175bSopenharmony_ci    psa_algorithm_t ecdsa_alg = PSA_ALG_ECDSA(alg);
261a8e1175bSopenharmony_ci    psa_algorithm_t deterministic_ecdsa_alg = PSA_ALG_DETERMINISTIC_ECDSA(alg);
262a8e1175bSopenharmony_ci    psa_algorithm_t rsa_oaep_alg = PSA_ALG_RSA_OAEP(alg);
263a8e1175bSopenharmony_ci    psa_algorithm_t hkdf_alg = PSA_ALG_HKDF(alg);
264a8e1175bSopenharmony_ci
265a8e1175bSopenharmony_ci    /* Algorithm classification */
266a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_HASH(alg));
267a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
268a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
269a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
270a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
271a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
272a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
273a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
274a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
275a8e1175bSopenharmony_ci    algorithm_classification(alg, 0);
276a8e1175bSopenharmony_ci
277a8e1175bSopenharmony_ci    /* Dependent algorithms */
278a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_HMAC_GET_HASH(hmac_alg), alg);
279a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_SIGN_GET_HASH(rsa_pkcs1v15_sign_alg), alg);
280a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_SIGN_GET_HASH(rsa_pss_alg), alg);
281a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_SIGN_GET_HASH(dsa_alg), alg);
282a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_SIGN_GET_HASH(deterministic_dsa_alg), alg);
283a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_SIGN_GET_HASH(ecdsa_alg), alg);
284a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_SIGN_GET_HASH(deterministic_ecdsa_alg), alg);
285a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_RSA_OAEP_GET_HASH(rsa_oaep_alg), alg);
286a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_HKDF_GET_HASH(hkdf_alg), alg);
287a8e1175bSopenharmony_ci
288a8e1175bSopenharmony_ci    /* Hash length */
289a8e1175bSopenharmony_ci    TEST_EQUAL(length, PSA_HASH_LENGTH(alg));
290a8e1175bSopenharmony_ci    TEST_ASSERT(length <= PSA_HASH_MAX_SIZE);
291a8e1175bSopenharmony_ci}
292a8e1175bSopenharmony_ci/* END_CASE */
293a8e1175bSopenharmony_ci
294a8e1175bSopenharmony_ci/* BEGIN_CASE */
295a8e1175bSopenharmony_civoid mac_algorithm(int alg_arg, int classification_flags,
296a8e1175bSopenharmony_ci                   int length_arg,
297a8e1175bSopenharmony_ci                   int key_type_arg, int key_bits_arg)
298a8e1175bSopenharmony_ci{
299a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
300a8e1175bSopenharmony_ci    size_t length = length_arg;
301a8e1175bSopenharmony_ci    size_t n;
302a8e1175bSopenharmony_ci    size_t key_type = key_type_arg;
303a8e1175bSopenharmony_ci    size_t key_bits = key_bits_arg;
304a8e1175bSopenharmony_ci
305a8e1175bSopenharmony_ci    mac_algorithm_core(alg, classification_flags,
306a8e1175bSopenharmony_ci                       key_type, key_bits, length);
307a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_FULL_LENGTH_MAC(alg), alg);
308a8e1175bSopenharmony_ci    TEST_ASSERT(length <= PSA_MAC_MAX_SIZE);
309a8e1175bSopenharmony_ci
310a8e1175bSopenharmony_ci    /* Truncated versions */
311a8e1175bSopenharmony_ci    for (n = 1; n <= length; n++) {
312a8e1175bSopenharmony_ci        psa_algorithm_t truncated_alg = PSA_ALG_TRUNCATED_MAC(alg, n);
313a8e1175bSopenharmony_ci        mac_algorithm_core(truncated_alg, classification_flags,
314a8e1175bSopenharmony_ci                           key_type, key_bits, n);
315a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_FULL_LENGTH_MAC(truncated_alg), alg);
316a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_TRUNCATED_MAC twice gives the length
317a8e1175bSopenharmony_ci         * of the outer truncation (even if the outer length is smaller than
318a8e1175bSopenharmony_ci         * the inner length). */
319a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_TRUNCATED_MAC(truncated_alg, 1),
320a8e1175bSopenharmony_ci                   PSA_ALG_TRUNCATED_MAC(alg, 1));
321a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_TRUNCATED_MAC(truncated_alg, length - 1),
322a8e1175bSopenharmony_ci                   PSA_ALG_TRUNCATED_MAC(alg, length - 1));
323a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_TRUNCATED_MAC(truncated_alg, length),
324a8e1175bSopenharmony_ci                   PSA_ALG_TRUNCATED_MAC(alg, length));
325a8e1175bSopenharmony_ci
326a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_TRUNCATED_MAC on an algorithm
327a8e1175bSopenharmony_ci         * earlier constructed with PSA_ALG_AT_LEAST_THIS_LENGTH_MAC gives the
328a8e1175bSopenharmony_ci         * length of the outer truncation (even if the outer length is smaller
329a8e1175bSopenharmony_ci         * than the inner length). */
330a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_TRUNCATED_MAC(
331a8e1175bSopenharmony_ci                       PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(truncated_alg, n), 1),
332a8e1175bSopenharmony_ci                   PSA_ALG_TRUNCATED_MAC(alg, 1));
333a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_TRUNCATED_MAC(
334a8e1175bSopenharmony_ci                       PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(truncated_alg, n), length - 1),
335a8e1175bSopenharmony_ci                   PSA_ALG_TRUNCATED_MAC(alg, length - 1));
336a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_TRUNCATED_MAC(
337a8e1175bSopenharmony_ci                       PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(truncated_alg, n), length),
338a8e1175bSopenharmony_ci                   PSA_ALG_TRUNCATED_MAC(alg, length));
339a8e1175bSopenharmony_ci    }
340a8e1175bSopenharmony_ci
341a8e1175bSopenharmony_ci    /* At-leat-this-length versions */
342a8e1175bSopenharmony_ci    for (n = 1; n <= length; n++) {
343a8e1175bSopenharmony_ci        psa_algorithm_t policy_alg = PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg, n);
344a8e1175bSopenharmony_ci        mac_algorithm_core(policy_alg, classification_flags | ALG_IS_WILDCARD,
345a8e1175bSopenharmony_ci                           key_type, key_bits, n);
346a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_FULL_LENGTH_MAC(policy_alg), alg);
347a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_AT_LEAST_THIS_LENGTH_MAC twice gives the
348a8e1175bSopenharmony_ci         * length of the outer truncation (even if the outer length is smaller
349a8e1175bSopenharmony_ci         * than the inner length). */
350a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(policy_alg, 1),
351a8e1175bSopenharmony_ci                   PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg, 1));
352a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(policy_alg, length - 1),
353a8e1175bSopenharmony_ci                   PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg, length - 1));
354a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(policy_alg, length),
355a8e1175bSopenharmony_ci                   PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg, length));
356a8e1175bSopenharmony_ci
357a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_AT_LEAST_THIS_LENGTH_MAC on an algorithm
358a8e1175bSopenharmony_ci         * earlier constructed with PSA_ALG_TRUNCATED_MAC gives the length of
359a8e1175bSopenharmony_ci         * the outer truncation (even if the outer length is smaller than the
360a8e1175bSopenharmony_ci         * inner length). */
361a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(
362a8e1175bSopenharmony_ci                       PSA_ALG_TRUNCATED_MAC(policy_alg, n), 1),
363a8e1175bSopenharmony_ci                   PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg, 1));
364a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(
365a8e1175bSopenharmony_ci                       PSA_ALG_TRUNCATED_MAC(policy_alg, n), length - 1),
366a8e1175bSopenharmony_ci                   PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg, length - 1));
367a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(
368a8e1175bSopenharmony_ci                       PSA_ALG_TRUNCATED_MAC(policy_alg, n), length),
369a8e1175bSopenharmony_ci                   PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(alg, length));
370a8e1175bSopenharmony_ci    }
371a8e1175bSopenharmony_ci}
372a8e1175bSopenharmony_ci/* END_CASE */
373a8e1175bSopenharmony_ci
374a8e1175bSopenharmony_ci/* BEGIN_CASE */
375a8e1175bSopenharmony_civoid hmac_algorithm(int alg_arg,
376a8e1175bSopenharmony_ci                    int length_arg,
377a8e1175bSopenharmony_ci                    int block_size_arg)
378a8e1175bSopenharmony_ci{
379a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
380a8e1175bSopenharmony_ci    psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH(alg);
381a8e1175bSopenharmony_ci    size_t block_size = block_size_arg;
382a8e1175bSopenharmony_ci    size_t length = length_arg;
383a8e1175bSopenharmony_ci    size_t n;
384a8e1175bSopenharmony_ci
385a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_HASH(hash_alg));
386a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_HMAC(hash_alg), alg);
387a8e1175bSopenharmony_ci
388a8e1175bSopenharmony_ci    TEST_ASSERT(block_size == PSA_HASH_BLOCK_LENGTH(alg));
389a8e1175bSopenharmony_ci    TEST_ASSERT(block_size <= PSA_HMAC_MAX_HASH_BLOCK_SIZE);
390a8e1175bSopenharmony_ci
391a8e1175bSopenharmony_ci    test_mac_algorithm(alg_arg, ALG_IS_HMAC, length,
392a8e1175bSopenharmony_ci                       PSA_KEY_TYPE_HMAC, PSA_BYTES_TO_BITS(length));
393a8e1175bSopenharmony_ci
394a8e1175bSopenharmony_ci    for (n = 1; n <= length; n++) {
395a8e1175bSopenharmony_ci        psa_algorithm_t truncated_alg = PSA_ALG_TRUNCATED_MAC(alg, n);
396a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_HMAC_GET_HASH(truncated_alg), hash_alg);
397a8e1175bSopenharmony_ci    }
398a8e1175bSopenharmony_ci}
399a8e1175bSopenharmony_ci/* END_CASE */
400a8e1175bSopenharmony_ci
401a8e1175bSopenharmony_ci/* BEGIN_CASE */
402a8e1175bSopenharmony_civoid cipher_algorithm(int alg_arg, int classification_flags)
403a8e1175bSopenharmony_ci{
404a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
405a8e1175bSopenharmony_ci
406a8e1175bSopenharmony_ci    /* Algorithm classification */
407a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
408a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
409a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_CIPHER(alg));
410a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
411a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
412a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
413a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
414a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
415a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
416a8e1175bSopenharmony_ci    algorithm_classification(alg, classification_flags);
417a8e1175bSopenharmony_ci}
418a8e1175bSopenharmony_ci/* END_CASE */
419a8e1175bSopenharmony_ci
420a8e1175bSopenharmony_ci/* BEGIN_CASE */
421a8e1175bSopenharmony_civoid aead_algorithm(int alg_arg, int classification_flags,
422a8e1175bSopenharmony_ci                    int tag_length_arg,
423a8e1175bSopenharmony_ci                    int key_type_arg, int key_bits_arg)
424a8e1175bSopenharmony_ci{
425a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
426a8e1175bSopenharmony_ci    size_t tag_length = tag_length_arg;
427a8e1175bSopenharmony_ci    size_t n;
428a8e1175bSopenharmony_ci    psa_key_type_t key_type = key_type_arg;
429a8e1175bSopenharmony_ci    size_t key_bits = key_bits_arg;
430a8e1175bSopenharmony_ci
431a8e1175bSopenharmony_ci    aead_algorithm_core(alg, classification_flags,
432a8e1175bSopenharmony_ci                        key_type, key_bits, tag_length);
433a8e1175bSopenharmony_ci
434a8e1175bSopenharmony_ci    /* Truncated versions */
435a8e1175bSopenharmony_ci    for (n = 1; n <= tag_length; n++) {
436a8e1175bSopenharmony_ci        psa_algorithm_t truncated_alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, n);
437a8e1175bSopenharmony_ci        aead_algorithm_core(truncated_alg, classification_flags,
438a8e1175bSopenharmony_ci                            key_type, key_bits, n);
439a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(truncated_alg),
440a8e1175bSopenharmony_ci                   alg);
441a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_AEAD_WITH_SHORTENED_TAG twice gives
442a8e1175bSopenharmony_ci         * the length of the outer truncation (even if the outer length is
443a8e1175bSopenharmony_ci         * smaller than the inner length). */
444a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_SHORTENED_TAG(truncated_alg, 1),
445a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 1));
446a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_SHORTENED_TAG(truncated_alg, tag_length - 1),
447a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, tag_length - 1));
448a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_SHORTENED_TAG(truncated_alg, tag_length),
449a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, tag_length));
450a8e1175bSopenharmony_ci
451a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_AEAD_WITH_SHORTENED_TAG on an algorithm
452a8e1175bSopenharmony_ci         * earlier constructed with PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG
453a8e1175bSopenharmony_ci         * gives the length of the outer truncation (even if the outer length is
454a8e1175bSopenharmony_ci         * smaller than the inner length). */
455a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_SHORTENED_TAG(
456a8e1175bSopenharmony_ci                       PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(truncated_alg, n), 1),
457a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 1));
458a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_SHORTENED_TAG(
459a8e1175bSopenharmony_ci                       PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(truncated_alg,
460a8e1175bSopenharmony_ci                                                                  n), tag_length - 1),
461a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, tag_length - 1));
462a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_SHORTENED_TAG(
463a8e1175bSopenharmony_ci                       PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(truncated_alg, n), tag_length),
464a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, tag_length));
465a8e1175bSopenharmony_ci    }
466a8e1175bSopenharmony_ci
467a8e1175bSopenharmony_ci    /* At-leat-this-length versions */
468a8e1175bSopenharmony_ci    for (n = 1; n <= tag_length; n++) {
469a8e1175bSopenharmony_ci        psa_algorithm_t policy_alg = PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg, n);
470a8e1175bSopenharmony_ci        aead_algorithm_core(policy_alg, classification_flags | ALG_IS_WILDCARD,
471a8e1175bSopenharmony_ci                            key_type, key_bits, n);
472a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(policy_alg),
473a8e1175bSopenharmony_ci                   alg);
474a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG twice
475a8e1175bSopenharmony_ci         * gives the length of the outer truncation (even if the outer length is
476a8e1175bSopenharmony_ci         * smaller than the inner length). */
477a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(policy_alg, 1),
478a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg, 1));
479a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(policy_alg, tag_length - 1),
480a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg, tag_length - 1));
481a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(policy_alg, tag_length),
482a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg, tag_length));
483a8e1175bSopenharmony_ci
484a8e1175bSopenharmony_ci        /* Check that calling PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG on an
485a8e1175bSopenharmony_ci         * algorithm earlier constructed with PSA_ALG_AEAD_WITH_SHORTENED_TAG
486a8e1175bSopenharmony_ci         * gives the length of the outer truncation (even if the outer length is
487a8e1175bSopenharmony_ci         * smaller than the inner length). */
488a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
489a8e1175bSopenharmony_ci                       PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, n), 1),
490a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg, 1));
491a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
492a8e1175bSopenharmony_ci                       PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, n), tag_length - 1),
493a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg, tag_length - 1));
494a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
495a8e1175bSopenharmony_ci                       PSA_ALG_AEAD_WITH_SHORTENED_TAG(policy_alg, n), tag_length),
496a8e1175bSopenharmony_ci                   PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(alg, tag_length));
497a8e1175bSopenharmony_ci    }
498a8e1175bSopenharmony_ci}
499a8e1175bSopenharmony_ci/* END_CASE */
500a8e1175bSopenharmony_ci
501a8e1175bSopenharmony_ci/* BEGIN_CASE */
502a8e1175bSopenharmony_civoid asymmetric_signature_algorithm(int alg_arg, int classification_flags)
503a8e1175bSopenharmony_ci{
504a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
505a8e1175bSopenharmony_ci
506a8e1175bSopenharmony_ci    /* Algorithm classification */
507a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
508a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
509a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
510a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
511a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_SIGN(alg));
512a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
513a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
514a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
515a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
516a8e1175bSopenharmony_ci    algorithm_classification(alg, classification_flags);
517a8e1175bSopenharmony_ci}
518a8e1175bSopenharmony_ci/* END_CASE */
519a8e1175bSopenharmony_ci
520a8e1175bSopenharmony_ci/* BEGIN_CASE */
521a8e1175bSopenharmony_civoid asymmetric_signature_wildcard(int alg_arg, int classification_flags)
522a8e1175bSopenharmony_ci{
523a8e1175bSopenharmony_ci    classification_flags |= ALG_IS_WILDCARD;
524a8e1175bSopenharmony_ci    classification_flags |= ALG_IS_SIGN_HASH;
525a8e1175bSopenharmony_ci    classification_flags |= ALG_IS_HASH_AND_SIGN;
526a8e1175bSopenharmony_ci    test_asymmetric_signature_algorithm(alg_arg, classification_flags);
527a8e1175bSopenharmony_ci    /* Any failure of this test function comes from
528a8e1175bSopenharmony_ci     * asymmetric_signature_algorithm. Pacify -Werror=unused-label. */
529a8e1175bSopenharmony_ci    goto exit;
530a8e1175bSopenharmony_ci}
531a8e1175bSopenharmony_ci/* END_CASE */
532a8e1175bSopenharmony_ci
533a8e1175bSopenharmony_ci/* BEGIN_CASE */
534a8e1175bSopenharmony_civoid asymmetric_encryption_algorithm(int alg_arg, int classification_flags)
535a8e1175bSopenharmony_ci{
536a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
537a8e1175bSopenharmony_ci
538a8e1175bSopenharmony_ci    /* Algorithm classification */
539a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
540a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
541a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
542a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
543a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
544a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
545a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
546a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
547a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
548a8e1175bSopenharmony_ci    algorithm_classification(alg, classification_flags);
549a8e1175bSopenharmony_ci}
550a8e1175bSopenharmony_ci/* END_CASE */
551a8e1175bSopenharmony_ci
552a8e1175bSopenharmony_ci/* BEGIN_CASE */
553a8e1175bSopenharmony_civoid key_derivation_algorithm(int alg_arg, int classification_flags)
554a8e1175bSopenharmony_ci{
555a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
556a8e1175bSopenharmony_ci    psa_algorithm_t ecdh_alg = PSA_ALG_KEY_AGREEMENT(PSA_ALG_ECDH, alg);
557a8e1175bSopenharmony_ci    psa_algorithm_t ffdh_alg = PSA_ALG_KEY_AGREEMENT(PSA_ALG_FFDH, alg);
558a8e1175bSopenharmony_ci
559a8e1175bSopenharmony_ci    /* Algorithm classification */
560a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
561a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
562a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
563a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
564a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
565a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
566a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
567a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_KEY_DERIVATION(alg));
568a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
569a8e1175bSopenharmony_ci    algorithm_classification(alg, classification_flags);
570a8e1175bSopenharmony_ci
571a8e1175bSopenharmony_ci    /* Check combinations with key agreements */
572a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_KEY_AGREEMENT(ecdh_alg));
573a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_KEY_AGREEMENT(ffdh_alg));
574a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_KEY_AGREEMENT_GET_KDF(ecdh_alg), alg);
575a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_ALG_KEY_AGREEMENT_GET_KDF(ffdh_alg), alg);
576a8e1175bSopenharmony_ci}
577a8e1175bSopenharmony_ci/* END_CASE */
578a8e1175bSopenharmony_ci
579a8e1175bSopenharmony_ci/* BEGIN_CASE */
580a8e1175bSopenharmony_civoid key_agreement_algorithm(int alg_arg, int classification_flags,
581a8e1175bSopenharmony_ci                             int ka_alg_arg, int kdf_alg_arg)
582a8e1175bSopenharmony_ci{
583a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
584a8e1175bSopenharmony_ci    psa_algorithm_t actual_ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE(alg);
585a8e1175bSopenharmony_ci    psa_algorithm_t expected_ka_alg = ka_alg_arg;
586a8e1175bSopenharmony_ci    psa_algorithm_t actual_kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF(alg);
587a8e1175bSopenharmony_ci    psa_algorithm_t expected_kdf_alg = kdf_alg_arg;
588a8e1175bSopenharmony_ci
589a8e1175bSopenharmony_ci    /* Algorithm classification */
590a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
591a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
592a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
593a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
594a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
595a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
596a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_KEY_AGREEMENT(alg));
597a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
598a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_PAKE(alg));
599a8e1175bSopenharmony_ci    algorithm_classification(alg, classification_flags);
600a8e1175bSopenharmony_ci
601a8e1175bSopenharmony_ci    /* Shared secret derivation properties */
602a8e1175bSopenharmony_ci    TEST_EQUAL(actual_ka_alg, expected_ka_alg);
603a8e1175bSopenharmony_ci    TEST_EQUAL(actual_kdf_alg, expected_kdf_alg);
604a8e1175bSopenharmony_ci}
605a8e1175bSopenharmony_ci/* END_CASE */
606a8e1175bSopenharmony_ci
607a8e1175bSopenharmony_ci/* BEGIN_CASE */
608a8e1175bSopenharmony_civoid pake_algorithm(int alg_arg)
609a8e1175bSopenharmony_ci{
610a8e1175bSopenharmony_ci    psa_algorithm_t alg = alg_arg;
611a8e1175bSopenharmony_ci
612a8e1175bSopenharmony_ci    /* Algorithm classification */
613a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_HASH(alg));
614a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_MAC(alg));
615a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_CIPHER(alg));
616a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_AEAD(alg));
617a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_SIGN(alg));
618a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_ASYMMETRIC_ENCRYPTION(alg));
619a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_AGREEMENT(alg));
620a8e1175bSopenharmony_ci    TEST_ASSERT(!PSA_ALG_IS_KEY_DERIVATION(alg));
621a8e1175bSopenharmony_ci    TEST_ASSERT(PSA_ALG_IS_PAKE(alg));
622a8e1175bSopenharmony_ci}
623a8e1175bSopenharmony_ci
624a8e1175bSopenharmony_ci/* END_CASE */
625a8e1175bSopenharmony_ci/* BEGIN_CASE */
626a8e1175bSopenharmony_civoid key_type(int type_arg, int classification_flags)
627a8e1175bSopenharmony_ci{
628a8e1175bSopenharmony_ci    psa_key_type_t type = type_arg;
629a8e1175bSopenharmony_ci
630a8e1175bSopenharmony_ci    key_type_classification(type, classification_flags);
631a8e1175bSopenharmony_ci
632a8e1175bSopenharmony_ci    /* For asymmetric types, check the corresponding pair/public type */
633a8e1175bSopenharmony_ci    if (classification_flags & KEY_TYPE_IS_PUBLIC_KEY) {
634a8e1175bSopenharmony_ci        psa_key_type_t pair_type = PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type);
635a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(pair_type), type);
636a8e1175bSopenharmony_ci        key_type_classification(pair_type,
637a8e1175bSopenharmony_ci                                (classification_flags
638a8e1175bSopenharmony_ci                                 & ~KEY_TYPE_IS_PUBLIC_KEY)
639a8e1175bSopenharmony_ci                                | KEY_TYPE_IS_KEY_PAIR);
640a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type), type);
641a8e1175bSopenharmony_ci    }
642a8e1175bSopenharmony_ci    if (classification_flags & KEY_TYPE_IS_KEY_PAIR) {
643a8e1175bSopenharmony_ci        psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type);
644a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(public_type), type);
645a8e1175bSopenharmony_ci        key_type_classification(public_type,
646a8e1175bSopenharmony_ci                                (classification_flags
647a8e1175bSopenharmony_ci                                 & ~KEY_TYPE_IS_KEY_PAIR)
648a8e1175bSopenharmony_ci                                | KEY_TYPE_IS_PUBLIC_KEY);
649a8e1175bSopenharmony_ci        TEST_EQUAL(PSA_KEY_TYPE_KEY_PAIR_OF_PUBLIC_KEY(type), type);
650a8e1175bSopenharmony_ci    }
651a8e1175bSopenharmony_ci}
652a8e1175bSopenharmony_ci/* END_CASE */
653a8e1175bSopenharmony_ci
654a8e1175bSopenharmony_ci/* BEGIN_CASE */
655a8e1175bSopenharmony_civoid block_cipher_key_type(int type_arg, int block_size_arg)
656a8e1175bSopenharmony_ci{
657a8e1175bSopenharmony_ci    psa_key_type_t type = type_arg;
658a8e1175bSopenharmony_ci    size_t block_size = block_size_arg;
659a8e1175bSopenharmony_ci
660a8e1175bSopenharmony_ci    test_key_type(type_arg, KEY_TYPE_IS_UNSTRUCTURED);
661a8e1175bSopenharmony_ci
662a8e1175bSopenharmony_ci    TEST_EQUAL(type & PSA_KEY_TYPE_CATEGORY_MASK,
663a8e1175bSopenharmony_ci               PSA_KEY_TYPE_CATEGORY_SYMMETRIC);
664a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_BLOCK_CIPHER_BLOCK_LENGTH(type), block_size);
665a8e1175bSopenharmony_ci
666a8e1175bSopenharmony_ci    /* Check that the block size is a power of 2. This is required, at least,
667a8e1175bSopenharmony_ci       for PSA_ROUND_UP_TO_MULTIPLE(block_size, length) in crypto_sizes.h. */
668a8e1175bSopenharmony_ci    TEST_ASSERT(((block_size - 1) & block_size) == 0);
669a8e1175bSopenharmony_ci}
670a8e1175bSopenharmony_ci/* END_CASE */
671a8e1175bSopenharmony_ci
672a8e1175bSopenharmony_ci/* BEGIN_CASE */
673a8e1175bSopenharmony_civoid stream_cipher_key_type(int type_arg)
674a8e1175bSopenharmony_ci{
675a8e1175bSopenharmony_ci    psa_key_type_t type = type_arg;
676a8e1175bSopenharmony_ci
677a8e1175bSopenharmony_ci    test_key_type(type_arg, KEY_TYPE_IS_UNSTRUCTURED);
678a8e1175bSopenharmony_ci
679a8e1175bSopenharmony_ci    TEST_EQUAL(type & PSA_KEY_TYPE_CATEGORY_MASK,
680a8e1175bSopenharmony_ci               PSA_KEY_TYPE_CATEGORY_SYMMETRIC);
681a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_BLOCK_CIPHER_BLOCK_LENGTH(type), 1);
682a8e1175bSopenharmony_ci}
683a8e1175bSopenharmony_ci/* END_CASE */
684a8e1175bSopenharmony_ci
685a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:PSA_KEY_TYPE_ECC_PUBLIC_KEY:PSA_KEY_TYPE_ECC_KEY_PAIR */
686a8e1175bSopenharmony_civoid ecc_key_family(int curve_arg)
687a8e1175bSopenharmony_ci{
688a8e1175bSopenharmony_ci    psa_ecc_family_t curve = curve_arg;
689a8e1175bSopenharmony_ci    psa_key_type_t public_type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve);
690a8e1175bSopenharmony_ci    psa_key_type_t pair_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve);
691a8e1175bSopenharmony_ci
692a8e1175bSopenharmony_ci    TEST_PARITY(curve);
693a8e1175bSopenharmony_ci
694a8e1175bSopenharmony_ci    test_key_type(public_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_PUBLIC_KEY);
695a8e1175bSopenharmony_ci    test_key_type(pair_type, KEY_TYPE_IS_ECC | KEY_TYPE_IS_KEY_PAIR);
696a8e1175bSopenharmony_ci
697a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_ECC_GET_FAMILY(public_type), curve);
698a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_ECC_GET_FAMILY(pair_type), curve);
699a8e1175bSopenharmony_ci}
700a8e1175bSopenharmony_ci/* END_CASE */
701a8e1175bSopenharmony_ci
702a8e1175bSopenharmony_ci/* BEGIN_CASE depends_on:PSA_KEY_TYPE_DH_PUBLIC_KEY:PSA_KEY_TYPE_DH_KEY_PAIR */
703a8e1175bSopenharmony_civoid dh_key_family(int group_arg)
704a8e1175bSopenharmony_ci{
705a8e1175bSopenharmony_ci    psa_dh_family_t group = group_arg;
706a8e1175bSopenharmony_ci    psa_key_type_t public_type = PSA_KEY_TYPE_DH_PUBLIC_KEY(group);
707a8e1175bSopenharmony_ci    psa_key_type_t pair_type = PSA_KEY_TYPE_DH_KEY_PAIR(group);
708a8e1175bSopenharmony_ci
709a8e1175bSopenharmony_ci    TEST_PARITY(group);
710a8e1175bSopenharmony_ci
711a8e1175bSopenharmony_ci    test_key_type(public_type, KEY_TYPE_IS_DH | KEY_TYPE_IS_PUBLIC_KEY);
712a8e1175bSopenharmony_ci    test_key_type(pair_type, KEY_TYPE_IS_DH | KEY_TYPE_IS_KEY_PAIR);
713a8e1175bSopenharmony_ci
714a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_DH_GET_FAMILY(public_type), group);
715a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_TYPE_DH_GET_FAMILY(pair_type), group);
716a8e1175bSopenharmony_ci}
717a8e1175bSopenharmony_ci/* END_CASE */
718a8e1175bSopenharmony_ci
719a8e1175bSopenharmony_ci/* BEGIN_CASE */
720a8e1175bSopenharmony_civoid lifetime(int lifetime_arg, int classification_flags,
721a8e1175bSopenharmony_ci              int persistence_arg, int location_arg)
722a8e1175bSopenharmony_ci{
723a8e1175bSopenharmony_ci    psa_key_lifetime_t lifetime = lifetime_arg;
724a8e1175bSopenharmony_ci    psa_key_persistence_t persistence = persistence_arg;
725a8e1175bSopenharmony_ci    psa_key_location_t location = location_arg;
726a8e1175bSopenharmony_ci    unsigned flags = classification_flags;
727a8e1175bSopenharmony_ci    unsigned classification_flags_tested = 0;
728a8e1175bSopenharmony_ci
729a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_LIFETIME_IS_VOLATILE, lifetime, flags);
730a8e1175bSopenharmony_ci    TEST_CLASSIFICATION_MACRO(1, KEY_LIFETIME_IS_READ_ONLY, lifetime, flags);
731a8e1175bSopenharmony_ci    TEST_EQUAL(classification_flags_tested,
732a8e1175bSopenharmony_ci               KEY_LIFETIME_FLAG_MASK_PLUS_ONE - 1);
733a8e1175bSopenharmony_ci
734a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_LIFETIME_GET_PERSISTENCE(lifetime), persistence);
735a8e1175bSopenharmony_ci    TEST_EQUAL(PSA_KEY_LIFETIME_GET_LOCATION(lifetime), location);
736a8e1175bSopenharmony_ci}
737a8e1175bSopenharmony_ci/* END_CASE */
738