Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA crypto layer on top of Mbed TLS crypto |
| 3 | */ |
| 4 | /* Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * |
| 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 23 | #include "mbedtls/config.h" |
| 24 | #else |
| 25 | #include MBEDTLS_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
itayzafrir | 99974e3 | 2019-01-14 18:10:49 +0200 | [diff] [blame] | 29 | /* |
| 30 | * When MBEDTLS_PSA_CRYPTO_SPM is defined, the code is being built for SPM |
| 31 | * (Secure Partition Manager) integration which separates the code into two |
| 32 | * parts: NSPE (Non-Secure Processing Environment) and SPE (Secure Processing |
| 33 | * Environment). When building for the SPE, an additional header file should be |
| 34 | * included. |
| 35 | */ |
| 36 | #if defined(MBEDTLS_PSA_CRYPTO_SPM) |
| 37 | /* |
| 38 | * PSA_CRYPTO_SECURE means that this file is compiled for the SPE. |
| 39 | * Some headers will be affected by this flag. |
| 40 | */ |
| 41 | #define PSA_CRYPTO_SECURE 1 |
| 42 | #include "crypto_spe.h" |
| 43 | #endif |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 44 | |
| 45 | #include "psa/crypto.h" |
| 46 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 47 | #include "psa_crypto_core.h" |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 48 | #include "psa_crypto_slot_management.h" |
| 49 | #include "psa_crypto_storage.h" |
| 50 | |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | #if defined(MBEDTLS_PLATFORM_C) |
| 54 | #include "mbedtls/platform.h" |
| 55 | #else |
| 56 | #define mbedtls_calloc calloc |
| 57 | #define mbedtls_free free |
| 58 | #endif |
| 59 | |
| 60 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 61 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 62 | typedef struct |
| 63 | { |
| 64 | psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT]; |
| 65 | unsigned key_slots_initialized : 1; |
| 66 | } psa_global_data_t; |
| 67 | |
Gilles Peskine | 2e14bd3 | 2018-12-12 14:05:08 +0100 | [diff] [blame] | 68 | static psa_global_data_t global_data; |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 69 | |
| 70 | /* Access a key slot at the given handle. The handle of a key slot is |
| 71 | * the index of the slot in the global slot array, plus one so that handles |
| 72 | * start at 1 and not 0. */ |
| 73 | psa_status_t psa_get_key_slot( psa_key_handle_t handle, |
| 74 | psa_key_slot_t **p_slot ) |
| 75 | { |
| 76 | psa_key_slot_t *slot = NULL; |
| 77 | |
| 78 | if( ! global_data.key_slots_initialized ) |
| 79 | return( PSA_ERROR_BAD_STATE ); |
| 80 | |
| 81 | /* 0 is not a valid handle under any circumstance. This |
| 82 | * implementation provides slots number 1 to N where N is the |
| 83 | * number of available slots. */ |
| 84 | if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) ) |
| 85 | return( PSA_ERROR_INVALID_HANDLE ); |
| 86 | slot = &global_data.key_slots[handle - 1]; |
| 87 | |
| 88 | /* If the slot hasn't been allocated, the handle is invalid. */ |
| 89 | if( ! slot->allocated ) |
| 90 | return( PSA_ERROR_INVALID_HANDLE ); |
| 91 | |
| 92 | *p_slot = slot; |
| 93 | return( PSA_SUCCESS ); |
| 94 | } |
| 95 | |
| 96 | psa_status_t psa_initialize_key_slots( void ) |
| 97 | { |
| 98 | /* Nothing to do: program startup and psa_wipe_all_key_slots() both |
| 99 | * guarantee that the key slots are initialized to all-zero, which |
| 100 | * means that all the key slots are in a valid, empty state. */ |
| 101 | global_data.key_slots_initialized = 1; |
| 102 | return( PSA_SUCCESS ); |
| 103 | } |
| 104 | |
| 105 | void psa_wipe_all_key_slots( void ) |
| 106 | { |
| 107 | psa_key_handle_t key; |
| 108 | for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ ) |
| 109 | { |
| 110 | psa_key_slot_t *slot = &global_data.key_slots[key - 1]; |
| 111 | (void) psa_wipe_key_slot( slot ); |
| 112 | } |
| 113 | global_data.key_slots_initialized = 0; |
| 114 | } |
| 115 | |
| 116 | /** Find a free key slot and mark it as in use. |
| 117 | * |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 118 | * \param[out] handle On success, a slot number that is not in use. This |
| 119 | * value can be used as a handle to the slot. |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame] | 120 | * |
| 121 | * \retval #PSA_SUCCESS |
| 122 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 123 | */ |
| 124 | static psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle ) |
| 125 | { |
| 126 | for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) ) |
| 127 | { |
| 128 | psa_key_slot_t *slot = &global_data.key_slots[*handle - 1]; |
| 129 | if( ! slot->allocated ) |
| 130 | { |
| 131 | slot->allocated = 1; |
| 132 | return( PSA_SUCCESS ); |
| 133 | } |
| 134 | } |
| 135 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 136 | } |
| 137 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 138 | /** Wipe a key slot and mark it as available. |
| 139 | * |
| 140 | * This does not affect persistent storage. |
| 141 | * |
Gilles Peskine | 23fd2bd | 2018-12-11 15:51:32 +0100 | [diff] [blame] | 142 | * \param handle The handle to the key slot to release. |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 143 | * |
| 144 | * \retval #PSA_SUCCESS |
| 145 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 146 | * \retval #PSA_ERROR_TAMPERING_DETECTED |
| 147 | */ |
| 148 | static psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle ) |
| 149 | { |
| 150 | psa_key_slot_t *slot; |
| 151 | psa_status_t status; |
| 152 | |
| 153 | status = psa_get_key_slot( handle, &slot ); |
| 154 | if( status != PSA_SUCCESS ) |
| 155 | return( status ); |
| 156 | |
| 157 | return( psa_wipe_key_slot( slot ) ); |
| 158 | } |
| 159 | |
Gilles Peskine | d40c1fb | 2019-01-19 12:20:52 +0100 | [diff] [blame] | 160 | psa_status_t psa_allocate_key( psa_key_handle_t *handle ) |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 161 | { |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 162 | *handle = 0; |
| 163 | return( psa_internal_allocate_key_slot( handle ) ); |
| 164 | } |
| 165 | |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 166 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 167 | static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot ) |
| 168 | { |
| 169 | psa_status_t status = PSA_SUCCESS; |
| 170 | uint8_t *key_data = NULL; |
| 171 | size_t key_data_length = 0; |
| 172 | |
| 173 | status = psa_load_persistent_key( p_slot->persistent_storage_id, |
| 174 | &( p_slot )->type, |
| 175 | &( p_slot )->policy, &key_data, |
| 176 | &key_data_length ); |
| 177 | if( status != PSA_SUCCESS ) |
| 178 | goto exit; |
| 179 | status = psa_import_key_into_slot( p_slot, |
| 180 | key_data, key_data_length ); |
| 181 | exit: |
| 182 | psa_free_persistent_key_data( key_data, key_data_length ); |
| 183 | return( status ); |
| 184 | } |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame^] | 185 | |
| 186 | /** Check whether a key identifier is acceptable. |
| 187 | * |
| 188 | * For backward compatibility, key identifiers that were valid in a |
| 189 | * past released version must remain valid, unless a migration path |
| 190 | * is provided. |
| 191 | * |
| 192 | * \param key_id The key identifier to check. |
| 193 | * |
| 194 | * \return 1 if \p key_id is acceptable, otherwise 0. |
| 195 | */ |
| 196 | static int psa_is_key_id_valid( psa_key_id_t key_id ) |
| 197 | { |
| 198 | /* Reject id=0 because by general library conventions, 0 is an invalid |
| 199 | * value wherever possible. */ |
| 200 | if( key_id == 0 ) |
| 201 | return( 0 ); |
| 202 | /* Reject high values because the file names are reserved for the |
| 203 | * library's internal use. */ |
| 204 | if( key_id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER ) |
| 205 | return( 0 ); |
| 206 | return( 1 ); |
| 207 | } |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 208 | #endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */ |
| 209 | |
| 210 | /** Declare a slot as persistent and load it from storage. |
| 211 | * |
| 212 | * This function may only be called immediately after a successful call |
| 213 | * to psa_internal_allocate_key_slot(). |
| 214 | * |
| 215 | * \param handle A handle to a key slot freshly allocated with |
| 216 | * psa_internal_allocate_key_slot(). |
| 217 | * |
| 218 | * \retval #PSA_SUCCESS |
| 219 | * The slot content was loaded successfully. |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 220 | * \retval #PSA_ERROR_DOES_NOT_EXIST |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 221 | * There is no content for this slot in persistent storage. |
| 222 | * \retval #PSA_ERROR_INVALID_HANDLE |
| 223 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 224 | * \p id is not acceptable. |
| 225 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 226 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 227 | */ |
| 228 | static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle, |
| 229 | psa_key_id_t id ) |
| 230 | { |
| 231 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) |
| 232 | psa_key_slot_t *slot; |
| 233 | psa_status_t status; |
| 234 | |
Gilles Peskine | c8569bc | 2019-02-19 13:04:02 +0100 | [diff] [blame^] | 235 | if( ! psa_is_key_id_valid( id ) ) |
Gilles Peskine | fa4135b | 2018-12-10 16:48:53 +0100 | [diff] [blame] | 236 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 237 | |
| 238 | status = psa_get_key_slot( handle, &slot ); |
| 239 | if( status != PSA_SUCCESS ) |
| 240 | return( status ); |
| 241 | |
| 242 | slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT; |
| 243 | slot->persistent_storage_id = id; |
| 244 | status = psa_load_persistent_key_into_slot( slot ); |
| 245 | |
| 246 | return( status ); |
| 247 | |
| 248 | #else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 249 | (void) handle; |
| 250 | (void) id; |
| 251 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 252 | #endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
| 253 | } |
| 254 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 255 | static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime, |
| 256 | psa_key_id_t id, |
| 257 | psa_key_handle_t *handle, |
| 258 | psa_status_t wanted_load_status ) |
| 259 | { |
| 260 | psa_status_t status; |
| 261 | |
| 262 | *handle = 0; |
| 263 | |
| 264 | if( lifetime != PSA_KEY_LIFETIME_PERSISTENT ) |
| 265 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 266 | |
| 267 | status = psa_internal_allocate_key_slot( handle ); |
| 268 | if( status != PSA_SUCCESS ) |
| 269 | return( status ); |
| 270 | |
| 271 | status = psa_internal_make_key_persistent( *handle, id ); |
| 272 | if( status != wanted_load_status ) |
| 273 | { |
| 274 | psa_internal_release_key_slot( *handle ); |
| 275 | *handle = 0; |
| 276 | } |
| 277 | return( status ); |
| 278 | } |
| 279 | |
| 280 | psa_status_t psa_open_key( psa_key_lifetime_t lifetime, |
| 281 | psa_key_id_t id, |
| 282 | psa_key_handle_t *handle ) |
| 283 | { |
| 284 | return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) ); |
| 285 | } |
| 286 | |
| 287 | psa_status_t psa_create_key( psa_key_lifetime_t lifetime, |
| 288 | psa_key_id_t id, |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 289 | psa_key_handle_t *handle ) |
| 290 | { |
| 291 | psa_status_t status; |
| 292 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 293 | status = persistent_key_setup( lifetime, id, handle, |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 294 | PSA_ERROR_DOES_NOT_EXIST ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 295 | switch( status ) |
| 296 | { |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 297 | case PSA_SUCCESS: return( PSA_ERROR_ALREADY_EXISTS ); |
| 298 | case PSA_ERROR_DOES_NOT_EXIST: return( PSA_SUCCESS ); |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 299 | default: return( status ); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | psa_status_t psa_close_key( psa_key_handle_t handle ) |
| 304 | { |
| 305 | return( psa_internal_release_key_slot( handle ) ); |
| 306 | } |
| 307 | |
| 308 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |