xref: /aosp_15_r20/external/mbedtls/tests/suites/test_suite_psa_crypto_attributes.function (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1/* BEGIN_HEADER */
2#include "psa/crypto.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_PSA_CRYPTO_CLIENT
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void attributes_set_get(int owner_id_arg, int id_arg, int lifetime_arg,
12                        int usage_flags_arg, int alg_arg,
13                        int type_arg, int bits_arg)
14{
15    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
16    mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make(owner_id_arg, id_arg);
17    psa_key_lifetime_t lifetime = lifetime_arg;
18    psa_key_usage_t usage_flags = usage_flags_arg;
19    psa_algorithm_t alg = alg_arg;
20    psa_key_type_t type = type_arg;
21    size_t bits = bits_arg;
22
23    TEST_EQUAL(
24        MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
25    TEST_EQUAL(
26        MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
27    TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
28    TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
29    TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
30    TEST_EQUAL(psa_get_key_type(&attributes), 0);
31    TEST_EQUAL(psa_get_key_bits(&attributes), 0);
32
33    psa_set_key_id(&attributes, id);
34    psa_set_key_lifetime(&attributes, lifetime);
35    psa_set_key_usage_flags(&attributes, usage_flags);
36    psa_set_key_algorithm(&attributes, alg);
37    psa_set_key_type(&attributes, type);
38    psa_set_key_bits(&attributes, bits);
39
40    TEST_ASSERT(mbedtls_svc_key_id_equal(
41                    psa_get_key_id(&attributes), id));
42    TEST_EQUAL(psa_get_key_lifetime(&attributes), lifetime);
43    TEST_EQUAL(psa_get_key_usage_flags(&attributes), usage_flags);
44    TEST_EQUAL(psa_get_key_algorithm(&attributes), alg);
45    TEST_EQUAL(psa_get_key_type(&attributes), type);
46    TEST_EQUAL(psa_get_key_bits(&attributes), bits);
47
48    psa_reset_key_attributes(&attributes);
49
50    TEST_EQUAL(
51        MBEDTLS_SVC_KEY_ID_GET_KEY_ID(psa_get_key_id(&attributes)), 0);
52    TEST_EQUAL(
53        MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(psa_get_key_id(&attributes)), 0);
54    TEST_EQUAL(psa_get_key_lifetime(&attributes), 0);
55    TEST_EQUAL(psa_get_key_usage_flags(&attributes), 0);
56    TEST_EQUAL(psa_get_key_algorithm(&attributes), 0);
57    TEST_EQUAL(psa_get_key_type(&attributes), 0);
58    TEST_EQUAL(psa_get_key_bits(&attributes), 0);
59}
60/* END_CASE */
61
62/* BEGIN_CASE */
63void persistence_attributes(int id1_arg, int owner_id1_arg, int lifetime_arg,
64                            int id2_arg, int owner_id2_arg,
65                            int expected_id_arg, int expected_owner_id_arg,
66                            int expected_lifetime_arg)
67{
68    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
69    mbedtls_svc_key_id_t id1 =
70        mbedtls_svc_key_id_make(owner_id1_arg, id1_arg);
71    psa_key_lifetime_t lifetime = lifetime_arg;
72    mbedtls_svc_key_id_t id2 =
73        mbedtls_svc_key_id_make(owner_id2_arg, id2_arg);
74    mbedtls_svc_key_id_t expected_id =
75        mbedtls_svc_key_id_make(expected_owner_id_arg, expected_id_arg);
76    psa_key_lifetime_t expected_lifetime = expected_lifetime_arg;
77
78    if (id1_arg != -1) {
79        psa_set_key_id(&attributes, id1);
80    }
81    if (lifetime_arg != -1) {
82        psa_set_key_lifetime(&attributes, lifetime);
83    }
84    if (id2_arg != -1) {
85        psa_set_key_id(&attributes, id2);
86    }
87
88    TEST_ASSERT(mbedtls_svc_key_id_equal(
89                    psa_get_key_id(&attributes), expected_id));
90    TEST_EQUAL(psa_get_key_lifetime(&attributes), expected_lifetime);
91}
92/* END_CASE */
93
94/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_SE_C */
95void slot_number_attribute()
96{
97    psa_key_slot_number_t slot_number = 0xdeadbeef;
98    psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
99
100    /* Initially, there is no slot number. */
101    TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
102               PSA_ERROR_INVALID_ARGUMENT);
103
104    /* Test setting a slot number. */
105    psa_set_key_slot_number(&attributes, 0);
106    PSA_ASSERT(psa_get_key_slot_number(&attributes, &slot_number));
107    TEST_EQUAL(slot_number, 0);
108
109    /* Test changing the slot number. */
110    psa_set_key_slot_number(&attributes, 42);
111    PSA_ASSERT(psa_get_key_slot_number(&attributes, &slot_number));
112    TEST_EQUAL(slot_number, 42);
113
114    /* Test clearing the slot number. */
115    psa_clear_key_slot_number(&attributes);
116    TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
117               PSA_ERROR_INVALID_ARGUMENT);
118
119    /* Clearing again should have no effect. */
120    psa_clear_key_slot_number(&attributes);
121    TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
122               PSA_ERROR_INVALID_ARGUMENT);
123
124    /* Test that reset clears the slot number. */
125    psa_set_key_slot_number(&attributes, 42);
126    PSA_ASSERT(psa_get_key_slot_number(&attributes, &slot_number));
127    TEST_EQUAL(slot_number, 42);
128    psa_reset_key_attributes(&attributes);
129    TEST_EQUAL(psa_get_key_slot_number(&attributes, &slot_number),
130               PSA_ERROR_INVALID_ARGUMENT);
131}
132/* END_CASE */
133