Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PSA storage backend for persistent keys using psa_its APIs. |
| 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_FILE |
| 24 | #else |
| 25 | #include "mbedtls/config.h" |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C) |
| 29 | |
Gilles Peskine | 2379348 | 2019-02-24 12:32:16 +0100 | [diff] [blame] | 30 | #if defined(MBEDTLS_PSA_ITS_FILE_C) |
| 31 | #include "psa_crypto_its.h" |
| 32 | #else /* Native ITS implementation */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 33 | #include "psa/error.h" |
itayzafrir | 7723ab1 | 2019-02-14 10:28:02 +0200 | [diff] [blame] | 34 | #include "psa_crypto_service_integration.h" |
Gilles Peskine | 2379348 | 2019-02-24 12:32:16 +0100 | [diff] [blame] | 35 | #include "psa/internal_trusted_storage.h" |
| 36 | #endif |
| 37 | |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 38 | #include "psa/crypto.h" |
| 39 | #include "psa_crypto_storage_backend.h" |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 40 | |
| 41 | #if defined(MBEDTLS_PLATFORM_C) |
| 42 | #include "mbedtls/platform.h" |
| 43 | #endif |
| 44 | |
Gilles Peskine | 572f067 | 2019-02-19 14:16:17 +0100 | [diff] [blame] | 45 | /* Determine a file name (ITS file identifier) for the given key file |
| 46 | * identifier. The file name must be distinct from any file that is used |
| 47 | * for a purpose other than storing a key. Currently, the only such file |
| 48 | * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID |
| 49 | * and whose value is 0xFFFFFF52. */ |
| 50 | static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 51 | { |
Gilles Peskine | 572f067 | 2019-02-19 14:16:17 +0100 | [diff] [blame] | 52 | #if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \ |
| 53 | defined(PSA_CRYPTO_SECURE) |
| 54 | /* Encode the owner in the upper 32 bits. This means that if |
| 55 | * owner values are nonzero (as they are on a PSA platform), |
| 56 | * no key file will ever have a value less than 0x100000000, so |
| 57 | * the whole range 0..0xffffffff is available for non-key files. */ |
| 58 | uint32_t unsigned_owner = (uint32_t) file_id.owner; |
| 59 | return( (uint64_t) unsigned_owner << 32 | file_id.key_id ); |
| 60 | #else |
| 61 | /* Use the key id directly as a file name. |
| 62 | * psa_is_key_file_id_valid() in psa_crypto_slot_management.c |
| 63 | * is responsible for ensuring that key identifiers do not have a |
| 64 | * value that is reserved for non-key files. */ |
| 65 | return( file_id ); |
| 66 | #endif |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 69 | psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data, |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 70 | size_t data_size ) |
| 71 | { |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 72 | psa_status_t status; |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 73 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 74 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 75 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 76 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 77 | if( status != PSA_SUCCESS ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 78 | return( status ); |
| 79 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 80 | status = psa_its_get( data_identifier, 0, data_size, data ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 81 | |
| 82 | return( status ); |
| 83 | } |
| 84 | |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 85 | int psa_is_key_present_in_storage( const psa_key_file_id_t key ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 86 | { |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 87 | psa_status_t ret; |
| 88 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 89 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 90 | |
| 91 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 92 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 93 | if( ret == PSA_ERROR_DOES_NOT_EXIST ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 94 | return( 0 ); |
| 95 | return( 1 ); |
| 96 | } |
| 97 | |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 98 | psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key, |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 99 | const uint8_t *data, |
| 100 | size_t data_length ) |
| 101 | { |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 102 | psa_status_t status; |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 103 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 104 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 105 | |
| 106 | if( psa_is_key_present_in_storage( key ) == 1 ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 107 | return( PSA_ERROR_ALREADY_EXISTS ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 108 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 109 | status = psa_its_set( data_identifier, data_length, data, 0 ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 110 | if( status != PSA_SUCCESS ) |
| 111 | { |
| 112 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 113 | } |
| 114 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 115 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 116 | if( status != PSA_SUCCESS ) |
| 117 | { |
| 118 | goto exit; |
| 119 | } |
| 120 | |
| 121 | if( data_identifier_info.size != data_length ) |
| 122 | { |
| 123 | status = PSA_ERROR_STORAGE_FAILURE; |
| 124 | goto exit; |
| 125 | } |
| 126 | |
| 127 | exit: |
| 128 | if( status != PSA_SUCCESS ) |
| 129 | psa_its_remove( data_identifier ); |
| 130 | return( status ); |
| 131 | } |
| 132 | |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 133 | psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 134 | { |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 135 | psa_status_t ret; |
| 136 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 137 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 138 | |
| 139 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 140 | if( ret == PSA_ERROR_DOES_NOT_EXIST ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 141 | return( PSA_SUCCESS ); |
| 142 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 143 | if( psa_its_remove( data_identifier ) != PSA_SUCCESS ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 144 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 145 | |
| 146 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 147 | if( ret != PSA_ERROR_DOES_NOT_EXIST ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 148 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 149 | |
| 150 | return( PSA_SUCCESS ); |
| 151 | } |
| 152 | |
Gilles Peskine | 5b229a0 | 2019-02-19 13:24:37 +0100 | [diff] [blame] | 153 | psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key, |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 154 | size_t *data_length ) |
| 155 | { |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 156 | psa_status_t status; |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 157 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 158 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 159 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 160 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 161 | if( status != PSA_SUCCESS ) |
| 162 | return( status ); |
| 163 | |
| 164 | *data_length = (size_t) data_identifier_info.size; |
| 165 | |
| 166 | return( PSA_SUCCESS ); |
| 167 | } |
| 168 | |
| 169 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */ |