Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 1 | /* |
Summer Qin | f07cc31 | 2022-01-05 16:52:54 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | /** |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 8 | * \file psa/crypto_struct.h |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 9 | * |
| 10 | * \brief PSA cryptography module: structured type implementations |
| 11 | * |
| 12 | * \note This file may not be included directly. Applications must |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 13 | * include psa/crypto.h. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 14 | * |
| 15 | * This file contains the definitions of some data structures with |
| 16 | * implementation-specific definitions. |
| 17 | * |
| 18 | * In implementations with isolation between the application and the |
| 19 | * cryptography module, it is expected that the front-end and the back-end |
| 20 | * would have different versions of this file. |
| 21 | */ |
| 22 | |
| 23 | #ifndef PSA_CRYPTO_STRUCT_H |
| 24 | #define PSA_CRYPTO_STRUCT_H |
| 25 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | /* |
| 31 | * Note that the below structures are different from the decalrations in |
| 32 | * mbed-crypto. This is because TF-M maintains 'front-end' and 'back-end' |
| 33 | * versions of this header. In the front-end version, exported to NS |
| 34 | * clients in interface/include/psa, a crypto operation is defined as an |
| 35 | * opaque handle to a context in the Crypto service. The back-end |
| 36 | * version, directly included from the mbed-crypto repo by the Crypto |
| 37 | * service, contains the full definition of the operation structs. |
| 38 | * |
| 39 | * One of the functions of the Crypto service is to allocate the back-end |
| 40 | * operation contexts in its own partition memory (in crypto_alloc.c), |
| 41 | * and then do the mapping between front-end operation handles passed by |
| 42 | * NS clients and the corresponding back-end operation contexts. The |
| 43 | * advantage of doing it this way is that internal mbed-crypto state is never |
| 44 | * exposed to the NS client. |
| 45 | */ |
| 46 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 47 | struct psa_hash_operation_s |
| 48 | { |
| 49 | uint32_t handle; |
| 50 | }; |
| 51 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 52 | #define PSA_HASH_OPERATION_INIT {0} |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 53 | static inline struct psa_hash_operation_s psa_hash_operation_init(void) |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 54 | { |
| 55 | const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 56 | return v; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 59 | struct psa_mac_operation_s |
| 60 | { |
| 61 | uint32_t handle; |
| 62 | }; |
| 63 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 64 | #define PSA_MAC_OPERATION_INIT {0} |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 65 | static inline struct psa_mac_operation_s psa_mac_operation_init(void) |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 66 | { |
| 67 | const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 68 | return v; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 69 | } |
| 70 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 71 | struct psa_cipher_operation_s |
| 72 | { |
| 73 | uint32_t handle; |
| 74 | }; |
| 75 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 76 | #define PSA_CIPHER_OPERATION_INIT {0} |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 77 | static inline struct psa_cipher_operation_s psa_cipher_operation_init(void) |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 78 | { |
| 79 | const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 80 | return v; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 83 | struct psa_aead_operation_s |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 84 | { |
| 85 | uint32_t handle; |
| 86 | }; |
| 87 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 88 | #define PSA_AEAD_OPERATION_INIT {0} |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 89 | static inline struct psa_aead_operation_s psa_aead_operation_init(void) |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 90 | { |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 91 | const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT; |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 92 | return v; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | struct psa_key_derivation_s |
| 96 | { |
| 97 | uint32_t handle; |
| 98 | }; |
| 99 | |
| 100 | #define PSA_KEY_DERIVATION_OPERATION_INIT {0} |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 101 | static inline struct psa_key_derivation_s psa_key_derivation_operation_init(void) |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 102 | { |
| 103 | const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 104 | return v; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 107 | /* The type used internally for key sizes. |
| 108 | * Public interfaces use size_t, but internally we use a smaller type. */ |
| 109 | typedef uint16_t psa_key_bits_t; |
| 110 | /* The maximum value of the type used to represent bit-sizes. |
| 111 | * This is used to mark an invalid key size. */ |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 112 | #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1) |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 113 | /* The maximum size of a key in bits. |
| 114 | * Currently defined as the maximum that can be represented, rounded down |
| 115 | * to a whole number of bytes. |
| 116 | * This is an uncast value so that it can be used in preprocessor |
| 117 | * conditionals. */ |
| 118 | #define PSA_MAX_KEY_BITS 0xfff8 |
| 119 | |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 120 | /* On the client side, only some key attributes are visible. |
| 121 | * The server has a different definition of psa_key_attributes_s which |
| 122 | * maintains more attributes. |
| 123 | */ |
Antonio de Angelis | 380b2b4 | 2023-03-17 13:54:37 +0000 | [diff] [blame] | 124 | #include "crypto_client_struct.h" |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 125 | struct psa_key_attributes_s { |
| 126 | struct psa_client_key_attributes_s client; |
| 127 | }; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 128 | |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 129 | #define PSA_KEY_ATTRIBUTES_INIT {PSA_CLIENT_KEY_ATTRIBUTES_INIT} |
| 130 | |
| 131 | static inline struct psa_key_attributes_s psa_key_attributes_init(void) |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 132 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 133 | const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | ba5aae9 | 2023-03-02 13:14:49 +0100 | [diff] [blame] | 134 | return v; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static inline void psa_set_key_id(psa_key_attributes_t *attributes, |
Antonio de Angelis | 34a0ffd | 2023-02-16 11:56:46 +0000 | [diff] [blame] | 138 | mbedtls_svc_key_id_t key) |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 139 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 140 | psa_key_lifetime_t lifetime = attributes->client.lifetime; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 141 | |
Antonio de Angelis | 34a0ffd | 2023-02-16 11:56:46 +0000 | [diff] [blame] | 142 | attributes->client.id = (psa_key_id_t)key; |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 143 | |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 144 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 145 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 146 | attributes->client.lifetime = |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 147 | PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( |
| 148 | PSA_KEY_LIFETIME_PERSISTENT, |
| 149 | PSA_KEY_LIFETIME_GET_LOCATION(lifetime)); |
| 150 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Antonio de Angelis | 34a0ffd | 2023-02-16 11:56:46 +0000 | [diff] [blame] | 153 | static inline mbedtls_svc_key_id_t psa_get_key_id( |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 154 | const psa_key_attributes_t *attributes) |
| 155 | { |
Antonio de Angelis | 34a0ffd | 2023-02-16 11:56:46 +0000 | [diff] [blame] | 156 | return (mbedtls_svc_key_id_t)attributes->client.id; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes, |
| 160 | psa_key_lifetime_t lifetime) |
| 161 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 162 | attributes->client.lifetime = lifetime; |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 163 | if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 164 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 165 | attributes->client.id = 0; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | static inline psa_key_lifetime_t psa_get_key_lifetime( |
| 170 | const psa_key_attributes_t *attributes) |
| 171 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 172 | return attributes->client.lifetime; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Summer Qin | f07cc31 | 2022-01-05 16:52:54 +0800 | [diff] [blame] | 175 | static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags) |
| 176 | { |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 177 | if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) { |
Summer Qin | f07cc31 | 2022-01-05 16:52:54 +0800 | [diff] [blame] | 178 | *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE; |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 179 | } |
Summer Qin | f07cc31 | 2022-01-05 16:52:54 +0800 | [diff] [blame] | 180 | |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 181 | if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) { |
Summer Qin | f07cc31 | 2022-01-05 16:52:54 +0800 | [diff] [blame] | 182 | *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE; |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 183 | } |
Summer Qin | f07cc31 | 2022-01-05 16:52:54 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 186 | static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes, |
| 187 | psa_key_usage_t usage_flags) |
| 188 | { |
Summer Qin | f07cc31 | 2022-01-05 16:52:54 +0800 | [diff] [blame] | 189 | psa_extend_key_usage_flags(&usage_flags); |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 190 | attributes->client.usage = usage_flags; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static inline psa_key_usage_t psa_get_key_usage_flags( |
| 194 | const psa_key_attributes_t *attributes) |
| 195 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 196 | return attributes->client.usage; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes, |
| 200 | psa_algorithm_t alg) |
| 201 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 202 | attributes->client.alg = alg; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static inline psa_algorithm_t psa_get_key_algorithm( |
| 206 | const psa_key_attributes_t *attributes) |
| 207 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 208 | return attributes->client.alg; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 209 | } |
| 210 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 211 | static inline void psa_set_key_type(psa_key_attributes_t *attributes, |
| 212 | psa_key_type_t type) |
| 213 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 214 | attributes->client.type = type; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static inline psa_key_type_t psa_get_key_type( |
| 218 | const psa_key_attributes_t *attributes) |
| 219 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 220 | return attributes->client.type; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static inline void psa_set_key_bits(psa_key_attributes_t *attributes, |
| 224 | size_t bits) |
| 225 | { |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 226 | if (bits > PSA_MAX_KEY_BITS) { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 227 | attributes->client.bits = PSA_KEY_BITS_TOO_LARGE; |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 228 | } else { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 229 | attributes->client.bits = bits; |
Summer Qin | d635cd0 | 2023-03-31 18:07:38 +0800 | [diff] [blame] | 230 | } |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static inline size_t psa_get_key_bits( |
| 234 | const psa_key_attributes_t *attributes) |
| 235 | { |
Gilles Peskine | 2f82041 | 2023-03-01 21:28:46 +0100 | [diff] [blame] | 236 | return attributes->client.bits; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | #ifdef __cplusplus |
| 240 | } |
| 241 | #endif |
| 242 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 243 | #endif /* PSA_CRYPTO_STRUCT_H */ |