Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 1 | /* |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 8 | #include <stdbool.h> |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 9 | #include <stddef.h> |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 10 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 11 | #include "tfm_crypto_api.h" |
| 12 | #include "crypto_utils.h" |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame^] | 13 | #include "secure_fw/core/tfm_memory_utils.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 14 | #include "psa_crypto.h" |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 15 | #include "tfm_crypto_defs.h" |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 16 | |
| 17 | /** |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 18 | * \brief This is the default value of maximum number of simultaneous |
| 19 | * key stores supported. |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 20 | */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 21 | #ifndef TFM_CRYPTO_KEY_STORAGE_NUM |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 22 | #define TFM_CRYPTO_KEY_STORAGE_NUM (4) |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | /** |
| 26 | * \brief This is the default value of the maximum supported key length |
| 27 | * in bytes. |
| 28 | */ |
| 29 | #ifndef TFM_CRYPTO_MAX_KEY_LENGTH |
Tamas Ban | 28aeec3 | 2019-01-09 16:53:26 +0000 | [diff] [blame] | 30 | #define TFM_CRYPTO_MAX_KEY_LENGTH (64) |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 31 | #endif |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 32 | |
| 33 | struct tfm_crypto_key_storage_s { |
| 34 | uint8_t in_use; /*!< Indicates if the key store is in use */ |
| 35 | psa_key_type_t type; /*!< Type of the key stored */ |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 36 | psa_key_policy_t policy; /*!< Policy of the key stored */ |
| 37 | psa_key_lifetime_t lifetime; /*!< Lifetime of the key stored */ |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 38 | size_t data_length; /*!< Length of the key stored */ |
| 39 | uint8_t data[TFM_CRYPTO_MAX_KEY_LENGTH]; /*!< Buffer containining the key */ |
| 40 | }; |
| 41 | |
| 42 | static struct tfm_crypto_key_storage_s |
| 43 | key_storage[TFM_CRYPTO_KEY_STORAGE_NUM] = {{0}}; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * \brief Get a pointer to the key store for the provided key slot. |
| 47 | * |
| 48 | * \param[in] key Key slot |
| 49 | * |
| 50 | * \return Pointer to key store or NULL if key is not a valid key slot |
| 51 | */ |
| 52 | static struct tfm_crypto_key_storage_s *get_key_store(psa_key_slot_t key) |
| 53 | { |
| 54 | if (key == 0 || key > TFM_CRYPTO_KEY_STORAGE_NUM) { |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | return &key_storage[key - 1]; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * \brief Check that the key type is supported and that key_length is a |
| 63 | * supported key length for that key type. |
| 64 | * |
| 65 | * \param[in] type Key type |
| 66 | * \param[in] key_length Key data length in bytes |
| 67 | * |
| 68 | * \return True if the key type is supported and key_length is a supported |
| 69 | * key length for that key type, false otherwise |
| 70 | */ |
| 71 | static bool key_type_is_supported(psa_key_type_t type, size_t key_length) |
| 72 | { |
| 73 | if (key_length > TFM_CRYPTO_MAX_KEY_LENGTH) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | switch (type) { |
| 78 | case PSA_KEY_TYPE_RAW_DATA: |
| 79 | case PSA_KEY_TYPE_HMAC: |
| 80 | case PSA_KEY_TYPE_DERIVE: |
| 81 | return true; /* No further restictions on these key types */ |
| 82 | case PSA_KEY_TYPE_AES: |
| 83 | case PSA_KEY_TYPE_CAMELLIA: |
| 84 | return (key_length == 16 || key_length == 24 || key_length == 32); |
| 85 | case PSA_KEY_TYPE_DES: |
| 86 | return (key_length == 8 || key_length == 16 || key_length == 24); |
| 87 | case PSA_KEY_TYPE_ARC4: |
| 88 | return key_length >= 1; |
| 89 | default: |
| 90 | return false; /* Other key types are not supported */ |
| 91 | } |
| 92 | } |
| 93 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 94 | /*! |
| 95 | * \defgroup public Public functions |
| 96 | * |
| 97 | */ |
| 98 | |
| 99 | /*!@{*/ |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 100 | enum tfm_crypto_err_t tfm_crypto_init_key(void) |
| 101 | { |
| 102 | /* Clear the contents of the local key_storage */ |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame^] | 103 | (void)tfm_memset(key_storage, 0, sizeof(key_storage)); |
Antonio de Angelis | cf85ba2 | 2018-10-09 13:29:40 +0100 | [diff] [blame] | 104 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 105 | } |
| 106 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 107 | enum tfm_crypto_err_t tfm_crypto_get_key(psa_key_slot_t key, |
| 108 | psa_key_usage_t usage, |
| 109 | psa_algorithm_t alg, |
| 110 | uint8_t *data, |
| 111 | size_t data_size, |
| 112 | size_t *data_length) |
| 113 | { |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 114 | struct tfm_crypto_key_storage_s *key_store; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 115 | size_t i; |
| 116 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 117 | key_store = get_key_store(key); |
| 118 | if (key_store == NULL) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 119 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 120 | } |
| 121 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 122 | if (key_store->in_use == TFM_CRYPTO_NOT_IN_USE) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 123 | return TFM_CRYPTO_ERR_PSA_ERROR_EMPTY_SLOT; |
| 124 | } |
| 125 | |
| 126 | /* Check that usage is permitted for this key */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 127 | if ((usage & key_store->policy.usage) != usage) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 128 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED; |
| 129 | } |
| 130 | |
| 131 | /* Check that alg is compatible with this key */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 132 | if (alg != 0 && alg != key_store->policy.alg) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 133 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_PERMITTED; |
| 134 | } |
| 135 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 136 | if (key_store->data_length > data_size) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 137 | return TFM_CRYPTO_ERR_PSA_ERROR_BUFFER_TOO_SMALL; |
| 138 | } |
| 139 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 140 | for (i = 0; i < key_store->data_length; i++) { |
| 141 | data[i] = key_store->data[i]; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 144 | *data_length = key_store->data_length; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 145 | |
| 146 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 147 | } |
| 148 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 149 | enum tfm_crypto_err_t tfm_crypto_import_key(psa_key_slot_t key, |
| 150 | psa_key_type_t type, |
| 151 | const uint8_t *data, |
| 152 | size_t data_length) |
| 153 | { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 154 | enum tfm_crypto_err_t err; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 155 | struct tfm_crypto_key_storage_s *key_store; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 156 | size_t i; |
| 157 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 158 | err = tfm_crypto_memory_check((uint8_t *)data, data_length, |
| 159 | TFM_MEMORY_ACCESS_RO); |
| 160 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 161 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 162 | } |
| 163 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 164 | key_store = get_key_store(key); |
| 165 | if (key_store == NULL) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 166 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 167 | } |
| 168 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 169 | if (key_store->in_use != TFM_CRYPTO_NOT_IN_USE) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 170 | return TFM_CRYPTO_ERR_PSA_ERROR_OCCUPIED_SLOT; |
| 171 | } |
| 172 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 173 | if (!key_type_is_supported(type, data_length)) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 174 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_SUPPORTED; |
| 175 | } |
| 176 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 177 | key_store->in_use = TFM_CRYPTO_IN_USE; |
| 178 | key_store->type = type; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 179 | |
| 180 | for (i=0; i<data_length; i++) { |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 181 | key_store->data[i] = data[i]; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 182 | } |
| 183 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 184 | key_store->data_length = data_length; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 185 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 186 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | enum tfm_crypto_err_t tfm_crypto_destroy_key(psa_key_slot_t key) |
| 190 | { |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 191 | struct tfm_crypto_key_storage_s *key_store; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 192 | uint32_t i; |
| 193 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 194 | key_store = get_key_store(key); |
| 195 | if (key_store == NULL) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 196 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 197 | } |
| 198 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 199 | volatile uint8_t *p_mem = (uint8_t *)key_store; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 200 | uint32_t size_mem = sizeof(struct tfm_crypto_key_storage_s); |
| 201 | |
| 202 | /* memset the key_storage */ |
| 203 | for (i=0; i<size_mem; i++) { |
| 204 | p_mem[i] = 0; |
| 205 | } |
| 206 | |
| 207 | /* Set default values */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 208 | key_store->in_use = TFM_CRYPTO_NOT_IN_USE; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 209 | |
| 210 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 211 | } |
| 212 | |
| 213 | enum tfm_crypto_err_t tfm_crypto_get_key_information(psa_key_slot_t key, |
| 214 | psa_key_type_t *type, |
| 215 | size_t *bits) |
| 216 | { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 217 | enum tfm_crypto_err_t err; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 218 | struct tfm_crypto_key_storage_s *key_store; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 219 | |
| 220 | err = tfm_crypto_memory_check(type, sizeof(psa_key_type_t), |
| 221 | TFM_MEMORY_ACCESS_RW); |
| 222 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 223 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 224 | } |
| 225 | |
| 226 | err = tfm_crypto_memory_check(bits, sizeof(size_t), TFM_MEMORY_ACCESS_RW); |
| 227 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 228 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 229 | } |
| 230 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 231 | /* Initialise output parameters contents to zero */ |
| 232 | *type = (psa_key_type_t) 0; |
| 233 | *bits = (size_t)0; |
| 234 | |
| 235 | key_store = get_key_store(key); |
| 236 | if (key_store == NULL) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 237 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 238 | } |
| 239 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 240 | if (key_store->in_use == TFM_CRYPTO_NOT_IN_USE) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 241 | return TFM_CRYPTO_ERR_PSA_ERROR_EMPTY_SLOT; |
| 242 | } |
| 243 | |
| 244 | /* Get basic metadata */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 245 | *type = key_store->type; |
| 246 | *bits = PSA_BYTES_TO_BITS(key_store->data_length); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 247 | |
| 248 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 249 | } |
| 250 | |
| 251 | enum tfm_crypto_err_t tfm_crypto_export_key(psa_key_slot_t key, |
| 252 | uint8_t *data, |
| 253 | size_t data_size, |
| 254 | size_t *data_length) |
| 255 | { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 256 | enum tfm_crypto_err_t err; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 257 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 258 | err = tfm_crypto_memory_check(data, data_size, TFM_MEMORY_ACCESS_RW); |
| 259 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 260 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 261 | } |
| 262 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 263 | err = tfm_crypto_memory_check(data_length, sizeof(size_t), |
| 264 | TFM_MEMORY_ACCESS_RW); |
| 265 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 266 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 269 | return tfm_crypto_get_key(key, PSA_KEY_USAGE_EXPORT, 0, data, data_size, |
| 270 | data_length); |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | enum tfm_crypto_err_t tfm_crypto_export_public_key(psa_key_slot_t key, |
| 274 | uint8_t *data, |
| 275 | size_t data_size, |
| 276 | size_t *data_length) |
| 277 | { |
Hugues de Valon | 8b44244 | 2019-02-19 14:30:52 +0000 | [diff] [blame^] | 278 | (void)key; |
| 279 | (void)data; |
| 280 | (void)data_size; |
| 281 | (void)data_length; |
| 282 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 283 | /* FIXME: This API is not supported yet */ |
| 284 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_SUPPORTED; |
| 285 | } |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 286 | |
| 287 | enum tfm_crypto_err_t tfm_crypto_key_policy_init(psa_key_policy_t *policy) |
| 288 | { |
| 289 | enum tfm_crypto_err_t err; |
| 290 | |
| 291 | err = tfm_crypto_memory_check(policy, sizeof(psa_key_policy_t), |
| 292 | TFM_MEMORY_ACCESS_RW); |
| 293 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 294 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 295 | } |
| 296 | |
| 297 | policy->usage = 0; |
| 298 | policy->alg = 0; |
| 299 | |
| 300 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 301 | } |
| 302 | |
| 303 | enum tfm_crypto_err_t tfm_crypto_key_policy_set_usage(psa_key_policy_t *policy, |
| 304 | psa_key_usage_t usage, |
| 305 | psa_algorithm_t alg) |
| 306 | { |
| 307 | enum tfm_crypto_err_t err; |
| 308 | |
| 309 | err = tfm_crypto_memory_check(policy, sizeof(psa_key_policy_t), |
| 310 | TFM_MEMORY_ACCESS_RW); |
| 311 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 312 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 313 | } |
| 314 | |
| 315 | policy->usage = usage; |
| 316 | policy->alg = alg; |
| 317 | |
| 318 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 319 | } |
| 320 | |
| 321 | enum tfm_crypto_err_t tfm_crypto_key_policy_get_usage( |
| 322 | const psa_key_policy_t *policy, |
| 323 | psa_key_usage_t *usage) |
| 324 | { |
| 325 | enum tfm_crypto_err_t err; |
| 326 | |
| 327 | err = tfm_crypto_memory_check((psa_key_policy_t *)policy, |
| 328 | sizeof(psa_key_policy_t), |
| 329 | TFM_MEMORY_ACCESS_RO); |
| 330 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 331 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 332 | } |
| 333 | |
| 334 | err = tfm_crypto_memory_check((psa_key_policy_t *)usage, |
| 335 | sizeof(psa_key_usage_t), |
| 336 | TFM_MEMORY_ACCESS_RW); |
| 337 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 338 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 339 | } |
| 340 | |
| 341 | *usage = policy->usage; |
| 342 | |
| 343 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 344 | } |
| 345 | |
| 346 | enum tfm_crypto_err_t tfm_crypto_key_policy_get_algorithm( |
| 347 | const psa_key_policy_t *policy, |
| 348 | psa_algorithm_t *alg) |
| 349 | { |
| 350 | enum tfm_crypto_err_t err; |
| 351 | |
| 352 | err = tfm_crypto_memory_check((psa_key_policy_t *)policy, |
| 353 | sizeof(psa_key_policy_t), |
| 354 | TFM_MEMORY_ACCESS_RO); |
| 355 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 356 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 357 | } |
| 358 | |
| 359 | err = tfm_crypto_memory_check((psa_key_policy_t *)alg, |
| 360 | sizeof(psa_algorithm_t), |
| 361 | TFM_MEMORY_ACCESS_RW); |
| 362 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 363 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 364 | } |
| 365 | |
| 366 | *alg = policy->alg; |
| 367 | |
| 368 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 369 | } |
| 370 | |
| 371 | enum tfm_crypto_err_t tfm_crypto_set_key_policy(psa_key_slot_t key, |
| 372 | const psa_key_policy_t *policy) |
| 373 | { |
| 374 | enum tfm_crypto_err_t err; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 375 | struct tfm_crypto_key_storage_s *key_store; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 376 | |
| 377 | err = tfm_crypto_memory_check((psa_key_policy_t *)policy, |
| 378 | sizeof(psa_key_policy_t), |
| 379 | TFM_MEMORY_ACCESS_RO); |
| 380 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 381 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 382 | } |
| 383 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 384 | /* Check that the policy is valid */ |
| 385 | if (policy->usage & ~(PSA_KEY_USAGE_EXPORT |
| 386 | | PSA_KEY_USAGE_ENCRYPT |
| 387 | | PSA_KEY_USAGE_DECRYPT |
| 388 | | PSA_KEY_USAGE_SIGN |
| 389 | | PSA_KEY_USAGE_VERIFY |
| 390 | | PSA_KEY_USAGE_DERIVE)) { |
| 391 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 392 | } |
| 393 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 394 | key_store = get_key_store(key); |
| 395 | if (key_store == NULL) { |
| 396 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 397 | } |
| 398 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 399 | /* Changing the policy of an occupied slot is not permitted as |
| 400 | * this is a requirement of the PSA Crypto API |
| 401 | */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 402 | if (key_store->in_use != TFM_CRYPTO_NOT_IN_USE) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 403 | return TFM_CRYPTO_ERR_PSA_ERROR_OCCUPIED_SLOT; |
| 404 | } |
| 405 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 406 | key_store->policy = *policy; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 407 | |
| 408 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 409 | } |
| 410 | |
| 411 | enum tfm_crypto_err_t tfm_crypto_get_key_policy(psa_key_slot_t key, |
| 412 | psa_key_policy_t *policy) |
| 413 | { |
| 414 | enum tfm_crypto_err_t err; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 415 | struct tfm_crypto_key_storage_s *key_store; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 416 | |
| 417 | err = tfm_crypto_memory_check(policy, sizeof(psa_key_policy_t), |
| 418 | TFM_MEMORY_ACCESS_RW); |
| 419 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 420 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 421 | } |
| 422 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 423 | key_store = get_key_store(key); |
| 424 | if (key_store == NULL) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 425 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 426 | } |
| 427 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 428 | *policy = key_store->policy; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 429 | |
| 430 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 431 | } |
| 432 | |
| 433 | enum tfm_crypto_err_t tfm_crypto_set_key_lifetime(psa_key_slot_t key, |
| 434 | psa_key_lifetime_t lifetime) |
| 435 | { |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 436 | struct tfm_crypto_key_storage_s *key_store; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 437 | |
| 438 | /* Check that the lifetime is valid */ |
| 439 | if (lifetime != PSA_KEY_LIFETIME_VOLATILE |
| 440 | && lifetime != PSA_KEY_LIFETIME_PERSISTENT |
| 441 | && lifetime != PSA_KEY_LIFETIME_WRITE_ONCE) { |
| 442 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 443 | } |
| 444 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 445 | key_store = get_key_store(key); |
| 446 | if (key_store == NULL) { |
| 447 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 448 | } |
| 449 | |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 450 | /* TF-M Crypto service does not support changing the lifetime of an occupied |
| 451 | * slot. |
| 452 | */ |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 453 | if (key_store->in_use != TFM_CRYPTO_NOT_IN_USE) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 454 | return TFM_CRYPTO_ERR_PSA_ERROR_OCCUPIED_SLOT; |
| 455 | } |
| 456 | |
| 457 | /* Only volatile keys are currently supported */ |
| 458 | if (lifetime != PSA_KEY_LIFETIME_VOLATILE) { |
| 459 | return TFM_CRYPTO_ERR_PSA_ERROR_NOT_SUPPORTED; |
| 460 | } |
| 461 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 462 | key_store->lifetime = lifetime; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 463 | |
| 464 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 465 | } |
| 466 | |
| 467 | enum tfm_crypto_err_t tfm_crypto_get_key_lifetime(psa_key_slot_t key, |
| 468 | psa_key_lifetime_t *lifetime) |
| 469 | { |
| 470 | enum tfm_crypto_err_t err; |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 471 | struct tfm_crypto_key_storage_s *key_store; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 472 | |
| 473 | err = tfm_crypto_memory_check(lifetime, sizeof(psa_key_lifetime_t), |
| 474 | TFM_MEMORY_ACCESS_RW); |
| 475 | if (err != TFM_CRYPTO_ERR_PSA_SUCCESS) { |
| 476 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 477 | } |
| 478 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 479 | key_store = get_key_store(key); |
| 480 | if (key_store == NULL) { |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 481 | return TFM_CRYPTO_ERR_PSA_ERROR_INVALID_ARGUMENT; |
| 482 | } |
| 483 | |
Jamie Fox | 82b87ca | 2018-12-11 16:41:11 +0000 | [diff] [blame] | 484 | *lifetime = key_store->lifetime; |
Jamie Fox | efd8273 | 2018-11-26 10:34:32 +0000 | [diff] [blame] | 485 | |
| 486 | return TFM_CRYPTO_ERR_PSA_SUCCESS; |
| 487 | } |
| 488 | |
Antonio de Angelis | 8908f47 | 2018-08-31 15:44:25 +0100 | [diff] [blame] | 489 | /*!@}*/ |