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) |
itayzafrir | 7132dd9 | 2019-01-29 14:23:52 +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 | #endif |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 43 | |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 44 | #include "psa/error.h" |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 45 | #include "psa/crypto.h" |
| 46 | #include "psa_crypto_storage_backend.h" |
Oren Cohen | 23a6784 | 2019-01-24 14:32:11 +0200 | [diff] [blame] | 47 | #include "psa/internal_trusted_storage.h" |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 48 | |
| 49 | #if defined(MBEDTLS_PLATFORM_C) |
| 50 | #include "mbedtls/platform.h" |
| 51 | #endif |
| 52 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 53 | static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_id_t key ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 54 | { |
| 55 | return( key ); |
| 56 | } |
| 57 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 58 | 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] | 59 | size_t data_size ) |
| 60 | { |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 61 | psa_status_t status; |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 62 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 63 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 64 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 65 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 66 | if( status != PSA_SUCCESS ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 67 | return( status ); |
| 68 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 69 | status = psa_its_get( data_identifier, 0, data_size, data ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 70 | |
| 71 | return( status ); |
| 72 | } |
| 73 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 74 | 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] | 75 | { |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 76 | psa_status_t ret; |
| 77 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 78 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 79 | |
| 80 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
| 81 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 82 | if( ret == PSA_ERROR_DOES_NOT_EXIST ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 83 | return( 0 ); |
| 84 | return( 1 ); |
| 85 | } |
| 86 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 87 | 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] | 88 | const uint8_t *data, |
| 89 | size_t data_length ) |
| 90 | { |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 91 | psa_status_t status; |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 92 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 93 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 94 | |
| 95 | if( psa_is_key_present_in_storage( key ) == 1 ) |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 96 | return( PSA_ERROR_ALREADY_EXISTS ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 97 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 98 | status = psa_its_set( data_identifier, data_length, data, 0 ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 99 | if( status != PSA_SUCCESS ) |
| 100 | { |
| 101 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 102 | } |
| 103 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 104 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 105 | if( status != PSA_SUCCESS ) |
| 106 | { |
| 107 | goto exit; |
| 108 | } |
| 109 | |
| 110 | if( data_identifier_info.size != data_length ) |
| 111 | { |
| 112 | status = PSA_ERROR_STORAGE_FAILURE; |
| 113 | goto exit; |
| 114 | } |
| 115 | |
| 116 | exit: |
| 117 | if( status != PSA_SUCCESS ) |
| 118 | psa_its_remove( data_identifier ); |
| 119 | return( status ); |
| 120 | } |
| 121 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 122 | 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] | 123 | { |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 124 | psa_status_t ret; |
| 125 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 126 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 127 | |
| 128 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 129 | if( ret == PSA_ERROR_DOES_NOT_EXIST ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 130 | return( PSA_SUCCESS ); |
| 131 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 132 | if( psa_its_remove( data_identifier ) != PSA_SUCCESS ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 133 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 134 | |
| 135 | ret = psa_its_get_info( data_identifier, &data_identifier_info ); |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 136 | if( ret != PSA_ERROR_DOES_NOT_EXIST ) |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 137 | return( PSA_ERROR_STORAGE_FAILURE ); |
| 138 | |
| 139 | return( PSA_SUCCESS ); |
| 140 | } |
| 141 | |
Gilles Peskine | 8d4919b | 2018-12-03 16:48:09 +0100 | [diff] [blame] | 142 | 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] | 143 | size_t *data_length ) |
| 144 | { |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 145 | psa_status_t status; |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 146 | psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key ); |
| 147 | struct psa_storage_info_t data_identifier_info; |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 148 | |
David Saada | a2523b2 | 2019-02-18 13:56:26 +0200 | [diff] [blame] | 149 | status = psa_its_get_info( data_identifier, &data_identifier_info ); |
Moran Peker | a26d764 | 2018-11-20 18:33:41 +0200 | [diff] [blame] | 150 | if( status != PSA_SUCCESS ) |
| 151 | return( status ); |
| 152 | |
| 153 | *data_length = (size_t) data_identifier_info.size; |
| 154 | |
| 155 | return( PSA_SUCCESS ); |
| 156 | } |
| 157 | |
| 158 | #endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */ |