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) |
| 29 | |
| 30 | #include "psa/crypto.h" |
| 31 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame^] | 32 | #include "psa_crypto_core.h" |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 33 | #include "psa_crypto_slot_management.h" |
| 34 | #include "psa_crypto_storage.h" |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | #if defined(MBEDTLS_PLATFORM_C) |
| 39 | #include "mbedtls/platform.h" |
| 40 | #else |
| 41 | #define mbedtls_calloc calloc |
| 42 | #define mbedtls_free free |
| 43 | #endif |
| 44 | |
| 45 | #define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) ) |
| 46 | |
Gilles Peskine | 66fb126 | 2018-12-10 16:29:04 +0100 | [diff] [blame^] | 47 | typedef struct |
| 48 | { |
| 49 | psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT]; |
| 50 | unsigned key_slots_initialized : 1; |
| 51 | } psa_global_data_t; |
| 52 | |
| 53 | psa_global_data_t global_data; |
| 54 | |
| 55 | /* Access a key slot at the given handle. The handle of a key slot is |
| 56 | * the index of the slot in the global slot array, plus one so that handles |
| 57 | * start at 1 and not 0. */ |
| 58 | psa_status_t psa_get_key_slot( psa_key_handle_t handle, |
| 59 | psa_key_slot_t **p_slot ) |
| 60 | { |
| 61 | psa_key_slot_t *slot = NULL; |
| 62 | |
| 63 | if( ! global_data.key_slots_initialized ) |
| 64 | return( PSA_ERROR_BAD_STATE ); |
| 65 | |
| 66 | /* 0 is not a valid handle under any circumstance. This |
| 67 | * implementation provides slots number 1 to N where N is the |
| 68 | * number of available slots. */ |
| 69 | if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) ) |
| 70 | return( PSA_ERROR_INVALID_HANDLE ); |
| 71 | slot = &global_data.key_slots[handle - 1]; |
| 72 | |
| 73 | /* If the slot hasn't been allocated, the handle is invalid. */ |
| 74 | if( ! slot->allocated ) |
| 75 | return( PSA_ERROR_INVALID_HANDLE ); |
| 76 | |
| 77 | *p_slot = slot; |
| 78 | return( PSA_SUCCESS ); |
| 79 | } |
| 80 | |
| 81 | psa_status_t psa_initialize_key_slots( void ) |
| 82 | { |
| 83 | /* Nothing to do: program startup and psa_wipe_all_key_slots() both |
| 84 | * guarantee that the key slots are initialized to all-zero, which |
| 85 | * means that all the key slots are in a valid, empty state. */ |
| 86 | global_data.key_slots_initialized = 1; |
| 87 | return( PSA_SUCCESS ); |
| 88 | } |
| 89 | |
| 90 | void psa_wipe_all_key_slots( void ) |
| 91 | { |
| 92 | psa_key_handle_t key; |
| 93 | for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ ) |
| 94 | { |
| 95 | psa_key_slot_t *slot = &global_data.key_slots[key - 1]; |
| 96 | (void) psa_wipe_key_slot( slot ); |
| 97 | } |
| 98 | global_data.key_slots_initialized = 0; |
| 99 | } |
| 100 | |
| 101 | /** Find a free key slot and mark it as in use. |
| 102 | * |
| 103 | * \param[out] handle On success, a slot number that is not in use. |
| 104 | * |
| 105 | * \retval #PSA_SUCCESS |
| 106 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 107 | */ |
| 108 | static psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle ) |
| 109 | { |
| 110 | for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) ) |
| 111 | { |
| 112 | psa_key_slot_t *slot = &global_data.key_slots[*handle - 1]; |
| 113 | if( ! slot->allocated ) |
| 114 | { |
| 115 | slot->allocated = 1; |
| 116 | return( PSA_SUCCESS ); |
| 117 | } |
| 118 | } |
| 119 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 120 | } |
| 121 | |
Gilles Peskine | 961849f | 2018-11-30 18:54:54 +0100 | [diff] [blame] | 122 | psa_status_t psa_allocate_key( psa_key_type_t type, |
| 123 | size_t max_bits, |
| 124 | psa_key_handle_t *handle ) |
| 125 | { |
| 126 | /* This implementation doesn't reserve memory for the keys. */ |
| 127 | (void) type; |
| 128 | (void) max_bits; |
| 129 | *handle = 0; |
| 130 | return( psa_internal_allocate_key_slot( handle ) ); |
| 131 | } |
| 132 | |
| 133 | static psa_status_t persistent_key_setup( psa_key_lifetime_t lifetime, |
| 134 | psa_key_id_t id, |
| 135 | psa_key_handle_t *handle, |
| 136 | psa_status_t wanted_load_status ) |
| 137 | { |
| 138 | psa_status_t status; |
| 139 | |
| 140 | *handle = 0; |
| 141 | |
| 142 | if( lifetime != PSA_KEY_LIFETIME_PERSISTENT ) |
| 143 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 144 | |
| 145 | status = psa_internal_allocate_key_slot( handle ); |
| 146 | if( status != PSA_SUCCESS ) |
| 147 | return( status ); |
| 148 | |
| 149 | status = psa_internal_make_key_persistent( *handle, id ); |
| 150 | if( status != wanted_load_status ) |
| 151 | { |
| 152 | psa_internal_release_key_slot( *handle ); |
| 153 | *handle = 0; |
| 154 | } |
| 155 | return( status ); |
| 156 | } |
| 157 | |
| 158 | psa_status_t psa_open_key( psa_key_lifetime_t lifetime, |
| 159 | psa_key_id_t id, |
| 160 | psa_key_handle_t *handle ) |
| 161 | { |
| 162 | return( persistent_key_setup( lifetime, id, handle, PSA_SUCCESS ) ); |
| 163 | } |
| 164 | |
| 165 | psa_status_t psa_create_key( psa_key_lifetime_t lifetime, |
| 166 | psa_key_id_t id, |
| 167 | psa_key_type_t type, |
| 168 | size_t max_bits, |
| 169 | psa_key_handle_t *handle ) |
| 170 | { |
| 171 | psa_status_t status; |
| 172 | |
| 173 | /* This implementation doesn't reserve memory for the keys. */ |
| 174 | (void) type; |
| 175 | (void) max_bits; |
| 176 | |
| 177 | status = persistent_key_setup( lifetime, id, handle, |
| 178 | PSA_ERROR_EMPTY_SLOT ); |
| 179 | switch( status ) |
| 180 | { |
| 181 | case PSA_SUCCESS: return( PSA_ERROR_OCCUPIED_SLOT ); |
| 182 | case PSA_ERROR_EMPTY_SLOT: return( PSA_SUCCESS ); |
| 183 | default: return( status ); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | psa_status_t psa_close_key( psa_key_handle_t handle ) |
| 188 | { |
| 189 | return( psa_internal_release_key_slot( handle ) ); |
| 190 | } |
| 191 | |
| 192 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |