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 | |
| 30 | #include "psa/crypto.h" |
| 31 | #include "psa_crypto_storage_backend.h" |
Oren Cohen | 23a6784 | 2019-01-24 14:32:11 +0200 | [diff] [blame^] | 32 | #include "psa/internal_trusted_storage.h" |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 33 | |
| 34 | #if defined(MBEDTLS_PLATFORM_C) |
| 35 | #include "mbedtls/platform.h" |
| 36 | #endif |
| 37 | |
| 38 | static psa_status_t its_to_psa_error( psa_its_status_t ret ) |
| 39 | { |
| 40 | switch( ret ) |
| 41 | { |
| 42 | case PSA_ITS_SUCCESS: |
| 43 | return( PSA_SUCCESS ); |
| 44 | |
Oren Cohen | 23a6784 | 2019-01-24 14:32:11 +0200 | [diff] [blame^] | 45 | case PSA_ITS_ERROR_UID_NOT_FOUND: |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 46 | return( PSA_ERROR_EMPTY_SLOT ); |
| 47 | |
| 48 | case PSA_ITS_ERROR_STORAGE_FAILURE: |
| 49 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 50 | |
| 51 | case PSA_ITS_ERROR_INSUFFICIENT_SPACE: |
| 52 | return( PSA_ERROR_INSUFFICIENT_STORAGE ); |
| 53 | |
Jaeden Amero | 5860055 | 2018-11-30 12:04:38 +0000 | [diff] [blame] | 54 | case PSA_ITS_ERROR_OFFSET_INVALID: |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 55 | case PSA_ITS_ERROR_INCORRECT_SIZE: |
Oren Cohen | 23a6784 | 2019-01-24 14:32:11 +0200 | [diff] [blame^] | 56 | case PSA_ITS_ERROR_INVALID_ARGUMENTS: |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 57 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 58 | |
| 59 | case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED: |
| 60 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 61 | |
| 62 | case PSA_ITS_ERROR_WRITE_ONCE: |
| 63 | return( PSA_ERROR_OCCUPIED_SLOT ); |
| 64 | |
| 65 | default: |
| 66 | return( PSA_ERROR_UNKNOWN_ERROR ); |
| 67 | } |
| 68 | } |
| 69 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 70 | static uint32_t psa_its_identifier_of_slot( psa_key_id_t key ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 71 | { |
| 72 | return( key ); |
| 73 | } |
| 74 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 75 | psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data, |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 76 | size_t data_size ) |
| 77 | { |
| 78 | psa_its_status_t ret; |
| 79 | psa_status_t status; |
| 80 | uint32_t data_identifier = psa_its_identifier_of_slot( key ); |
| 81 | struct psa_its_info_t data_identifier_info; |
| 82 | |
| 83 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 84 | status = its_to_psa_error( ret ); |
| 85 | if( status != PSA_SUCCESS ) |
| 86 | return( status ); |
| 87 | |
| 88 | ret = psa_its_get( data_identifier, 0, data_size, data ); |
| 89 | status = its_to_psa_error( ret ); |
| 90 | |
| 91 | return( status ); |
| 92 | } |
| 93 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 94 | int psa_is_key_present_in_storage( const psa_key_id_t key ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 95 | { |
| 96 | psa_its_status_t ret; |
| 97 | uint32_t data_identifier = psa_its_identifier_of_slot( key ); |
| 98 | struct psa_its_info_t data_identifier_info; |
| 99 | |
| 100 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 101 | |
Oren Cohen | 23a6784 | 2019-01-24 14:32:11 +0200 | [diff] [blame^] | 102 | if( ret == PSA_ITS_ERROR_UID_NOT_FOUND ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 103 | return( 0 ); |
| 104 | return( 1 ); |
| 105 | } |
| 106 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 107 | psa_status_t psa_crypto_storage_store( const psa_key_id_t key, |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 108 | const uint8_t *data, |
| 109 | size_t data_length ) |
| 110 | { |
| 111 | psa_its_status_t ret; |
| 112 | psa_status_t status; |
| 113 | uint32_t data_identifier = psa_its_identifier_of_slot( key ); |
| 114 | struct psa_its_info_t data_identifier_info; |
| 115 | |
| 116 | if( psa_is_key_present_in_storage( key ) == 1 ) |
| 117 | return( PSA_ERROR_OCCUPIED_SLOT ); |
| 118 | |
| 119 | ret = psa_its_set( data_identifier, data_length, data, 0 ); |
| 120 | status = its_to_psa_error( ret ); |
| 121 | if( status != PSA_SUCCESS ) |
| 122 | { |
| 123 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 124 | } |
| 125 | |
| 126 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 127 | status = its_to_psa_error( ret ); |
| 128 | if( status != PSA_SUCCESS ) |
| 129 | { |
| 130 | goto exit; |
| 131 | } |
| 132 | |
| 133 | if( data_identifier_info.size != data_length ) |
| 134 | { |
| 135 | status = PSA_ERROR_STORAGE_FAILURE; |
| 136 | goto exit; |
| 137 | } |
| 138 | |
| 139 | exit: |
| 140 | if( status != PSA_SUCCESS ) |
| 141 | psa_its_remove( data_identifier ); |
| 142 | return( status ); |
| 143 | } |
| 144 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 145 | psa_status_t psa_destroy_persistent_key( const psa_key_id_t key ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 146 | { |
| 147 | psa_its_status_t ret; |
| 148 | uint32_t data_identifier = psa_its_identifier_of_slot( key ); |
| 149 | struct psa_its_info_t data_identifier_info; |
| 150 | |
| 151 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
Oren Cohen | 23a6784 | 2019-01-24 14:32:11 +0200 | [diff] [blame^] | 152 | if( ret == PSA_ITS_ERROR_UID_NOT_FOUND ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 153 | return( PSA_SUCCESS ); |
| 154 | |
| 155 | if( psa_its_remove( data_identifier ) != PSA_ITS_SUCCESS ) |
| 156 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 157 | |
| 158 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
Oren Cohen | 23a6784 | 2019-01-24 14:32:11 +0200 | [diff] [blame^] | 159 | if( ret != PSA_ITS_ERROR_UID_NOT_FOUND ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 160 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 161 | |
| 162 | return( PSA_SUCCESS ); |
| 163 | } |
| 164 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 165 | psa_status_t psa_crypto_storage_get_data_length( const psa_key_id_t key, |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 166 | size_t *data_length ) |
| 167 | { |
| 168 | psa_its_status_t ret; |
| 169 | psa_status_t status; |
| 170 | uint32_t data_identifier = psa_its_identifier_of_slot( key ); |
| 171 | struct psa_its_info_t data_identifier_info; |
| 172 | |
| 173 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 174 | status = its_to_psa_error( ret ); |
| 175 | if( status != PSA_SUCCESS ) |
| 176 | return( status ); |
| 177 | |
| 178 | *data_length = (size_t) data_identifier_info.size; |
| 179 | |
| 180 | return( PSA_SUCCESS ); |
| 181 | } |
| 182 | |
| 183 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */ |