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