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