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