1 /*
2  * Copyright (c) 2022-2024 Texas Instruments Incorporated - http://www.ti.com
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdint.h>
34 
35 /* CryptoKey headers */
36 #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyKeyStore_PSA.h>
37 #include <ti/drivers/cryptoutils/cryptokey/CryptoKeyKeyStore_PSA_init.h>
38 #include <ti/drivers/cryptoutils/cryptokey/CryptoKey.h>
39 
40 /*
41  *  ======== KeyStore_PSA_initKey ========
42  */
KeyStore_PSA_initKey(CryptoKey * keyHandle,KeyStore_PSA_KeyFileId keyID,size_t keyLength,void * keyAttributes)43 int_fast16_t KeyStore_PSA_initKey(CryptoKey *keyHandle,
44                                   KeyStore_PSA_KeyFileId keyID,
45                                   size_t keyLength,
46                                   void *keyAttributes)
47 {
48     keyHandle->encoding = CryptoKey_KEYSTORE;
49 
50     SET_KEY_ID(keyHandle->u.keyStore.keyID, keyID);
51 
52     keyHandle->u.keyStore.keyLength = keyLength;
53 
54     keyHandle->u.keyStore.keyAttributes = keyAttributes;
55 
56     return CryptoKey_STATUS_SUCCESS;
57 }
58 
59 /*
60  *  ======== KeyStore_PSA_initBlankKey ========
61  */
KeyStore_PSA_initBlankKey(CryptoKey * keyHandle,KeyStore_PSA_KeyFileId keyID,size_t keyLength,void * keyAttributes)62 int_fast16_t KeyStore_PSA_initBlankKey(CryptoKey *keyHandle,
63                                        KeyStore_PSA_KeyFileId keyID,
64                                        size_t keyLength,
65                                        void *keyAttributes)
66 {
67     keyHandle->encoding = CryptoKey_BLANK_KEYSTORE;
68 
69     SET_KEY_ID(keyHandle->u.keyStore.keyID, keyID);
70 
71     keyHandle->u.keyStore.keyLength = keyLength;
72 
73     keyHandle->u.keyStore.keyAttributes = keyAttributes;
74 
75     return CryptoKey_STATUS_SUCCESS;
76 }
77 
78 /*
79  *  ======== KeyStore_PSA_setKeyId ========
80  */
KeyStore_PSA_setKeyId(KeyStore_PSA_KeyAttributes * attributes,KeyStore_PSA_KeyFileId key)81 void KeyStore_PSA_setKeyId(KeyStore_PSA_KeyAttributes *attributes, KeyStore_PSA_KeyFileId key)
82 {
83     psa_set_key_id(attributes, key);
84 }
85 
86 /*
87  *  ======== KeyStore_PSA_setKeyLifetime ========
88  */
KeyStore_PSA_setKeyLifetime(KeyStore_PSA_KeyAttributes * attributes,KeyStore_PSA_KeyLifetime lifetime)89 void KeyStore_PSA_setKeyLifetime(KeyStore_PSA_KeyAttributes *attributes, KeyStore_PSA_KeyLifetime lifetime)
90 {
91     psa_set_key_lifetime(attributes, lifetime);
92 }
93 
94 /*
95  *  ======== KeyStore_PSA_getKeyId ========
96  */
KeyStore_PSA_getKeyId(KeyStore_PSA_KeyAttributes * attributes)97 KeyStore_PSA_KeyFileId KeyStore_PSA_getKeyId(KeyStore_PSA_KeyAttributes *attributes)
98 {
99     return psa_get_key_id(attributes);
100 }
101 
102 /*
103  *  ======== KeyStore_PSA_getKeyLifetime ========
104  */
KeyStore_PSA_getKeyLifetime(KeyStore_PSA_KeyAttributes * attributes)105 KeyStore_PSA_KeyLifetime KeyStore_PSA_getKeyLifetime(KeyStore_PSA_KeyAttributes *attributes)
106 {
107     return psa_get_key_lifetime(attributes);
108 }
109 
110 /*
111  *  ======== KeyStore_PSA_setKeyUsageFlags ========
112  */
KeyStore_PSA_setKeyUsageFlags(KeyStore_PSA_KeyAttributes * attributes,KeyStore_PSA_KeyUsage usageFlags)113 void KeyStore_PSA_setKeyUsageFlags(KeyStore_PSA_KeyAttributes *attributes, KeyStore_PSA_KeyUsage usageFlags)
114 {
115     psa_set_key_usage_flags(attributes, usageFlags);
116 }
117 
118 /*
119  *  ======== KeyStore_PSA_getKeyUsageFlags ========
120  */
KeyStore_PSA_getKeyUsageFlags(KeyStore_PSA_KeyAttributes * attributes)121 KeyStore_PSA_KeyUsage KeyStore_PSA_getKeyUsageFlags(KeyStore_PSA_KeyAttributes *attributes)
122 {
123     return psa_get_key_usage_flags(attributes);
124 }
125 
126 /*
127  *  ======== KeyStore_PSA_setKeyAlgorithm ========
128  */
KeyStore_PSA_setKeyAlgorithm(KeyStore_PSA_KeyAttributes * attributes,KeyStore_PSA_Algorithm alg)129 void KeyStore_PSA_setKeyAlgorithm(KeyStore_PSA_KeyAttributes *attributes, KeyStore_PSA_Algorithm alg)
130 {
131     psa_set_key_algorithm(attributes, alg);
132 }
133 
134 /*
135  *  ======== KeyStore_PSA_getKeyAlgorithm ========
136  */
KeyStore_PSA_getKeyAlgorithm(KeyStore_PSA_KeyAttributes * attributes)137 KeyStore_PSA_Algorithm KeyStore_PSA_getKeyAlgorithm(KeyStore_PSA_KeyAttributes *attributes)
138 {
139     return psa_get_key_algorithm(attributes);
140 }
141 
142 /*
143  *  ======== KeyStore_PSA_setKeyType ========
144  */
KeyStore_PSA_setKeyType(KeyStore_PSA_KeyAttributes * attributes,KeyStore_PSA_KeyType type)145 void KeyStore_PSA_setKeyType(KeyStore_PSA_KeyAttributes *attributes, KeyStore_PSA_KeyType type)
146 {
147     psa_set_key_type(attributes, type);
148 }
149 
150 /*
151  *  ======== KeyStore_PSA_setKeyBits ========
152  */
KeyStore_PSA_setKeyBits(KeyStore_PSA_KeyAttributes * attributes,size_t bits)153 void KeyStore_PSA_setKeyBits(KeyStore_PSA_KeyAttributes *attributes, size_t bits)
154 {
155     psa_set_key_bits(attributes, bits);
156 }
157 
158 /*
159  *  ======== KeyStore_PSA_getKeyType ========
160  */
KeyStore_PSA_getKeyType(KeyStore_PSA_KeyAttributes * attributes)161 KeyStore_PSA_KeyType KeyStore_PSA_getKeyType(KeyStore_PSA_KeyAttributes *attributes)
162 {
163     return psa_get_key_type(attributes);
164 }
165 
166 /*
167  *  ======== KeyStore_PSA_getKeyBits ========
168  */
KeyStore_PSA_getKeyBits(KeyStore_PSA_KeyAttributes * attributes)169 size_t KeyStore_PSA_getKeyBits(KeyStore_PSA_KeyAttributes *attributes)
170 {
171     return psa_get_key_bits(attributes);
172 }
173