blob: 8291f1fc38343eadf104e105d4e4c50ccbae82a1 [file] [log] [blame]
Moran Pekera26d7642018-11-20 18:33:41 +02001/*
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
Gilles Peskine23793482019-02-24 12:32:16 +010030#if defined(MBEDTLS_PSA_ITS_FILE_C)
31#include "psa_crypto_its.h"
32#else /* Native ITS implementation */
David Saadab4ecc272019-02-14 13:48:10 +020033#include "psa/error.h"
itayzafrir7723ab12019-02-14 10:28:02 +020034#include "psa_crypto_service_integration.h"
Gilles Peskine23793482019-02-24 12:32:16 +010035#include "psa/internal_trusted_storage.h"
36#endif
37
Moran Pekera26d7642018-11-20 18:33:41 +020038#include "psa/crypto.h"
39#include "psa_crypto_storage_backend.h"
Moran Pekera26d7642018-11-20 18:33:41 +020040
41#if defined(MBEDTLS_PLATFORM_C)
42#include "mbedtls/platform.h"
43#endif
44
Gilles Peskine572f0672019-02-19 14:16:17 +010045/* Determine a file name (ITS file identifier) for the given key file
46 * identifier. The file name must be distinct from any file that is used
47 * for a purpose other than storing a key. Currently, the only such file
48 * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID
49 * and whose value is 0xFFFFFF52. */
50static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id )
Moran Pekera26d7642018-11-20 18:33:41 +020051{
Gilles Peskine572f0672019-02-19 14:16:17 +010052#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \
53 defined(PSA_CRYPTO_SECURE)
54 /* Encode the owner in the upper 32 bits. This means that if
55 * owner values are nonzero (as they are on a PSA platform),
56 * no key file will ever have a value less than 0x100000000, so
57 * the whole range 0..0xffffffff is available for non-key files. */
58 uint32_t unsigned_owner = (uint32_t) file_id.owner;
59 return( (uint64_t) unsigned_owner << 32 | file_id.key_id );
60#else
61 /* Use the key id directly as a file name.
62 * psa_is_key_file_id_valid() in psa_crypto_slot_management.c
63 * is responsible for ensuring that key identifiers do not have a
64 * value that is reserved for non-key files. */
65 return( file_id );
66#endif
Moran Pekera26d7642018-11-20 18:33:41 +020067}
68
Gilles Peskine5b229a02019-02-19 13:24:37 +010069psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data,
Moran Pekera26d7642018-11-20 18:33:41 +020070 size_t data_size )
71{
Moran Pekera26d7642018-11-20 18:33:41 +020072 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020073 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
74 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020075
David Saadaa2523b22019-02-18 13:56:26 +020076 status = psa_its_get_info( data_identifier, &data_identifier_info );
77 if( status != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +020078 return( status );
79
David Saadaa2523b22019-02-18 13:56:26 +020080 status = psa_its_get( data_identifier, 0, data_size, data );
Moran Pekera26d7642018-11-20 18:33:41 +020081
82 return( status );
83}
84
Gilles Peskine5b229a02019-02-19 13:24:37 +010085int psa_is_key_present_in_storage( const psa_key_file_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020086{
David Saadaa2523b22019-02-18 13:56:26 +020087 psa_status_t ret;
88 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
89 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020090
91 ret = psa_its_get_info( data_identifier, &data_identifier_info );
92
David Saadaa2523b22019-02-18 13:56:26 +020093 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +020094 return( 0 );
95 return( 1 );
96}
97
Gilles Peskine5b229a02019-02-19 13:24:37 +010098psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +020099 const uint8_t *data,
100 size_t data_length )
101{
Moran Pekera26d7642018-11-20 18:33:41 +0200102 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +0200103 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
104 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200105
106 if( psa_is_key_present_in_storage( key ) == 1 )
David Saadab4ecc272019-02-14 13:48:10 +0200107 return( PSA_ERROR_ALREADY_EXISTS );
Moran Pekera26d7642018-11-20 18:33:41 +0200108
David Saadaa2523b22019-02-18 13:56:26 +0200109 status = psa_its_set( data_identifier, data_length, data, 0 );
Moran Pekera26d7642018-11-20 18:33:41 +0200110 if( status != PSA_SUCCESS )
111 {
112 return( PSA_ERROR_STORAGE_FAILURE );
113 }
114
David Saadaa2523b22019-02-18 13:56:26 +0200115 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200116 if( status != PSA_SUCCESS )
117 {
118 goto exit;
119 }
120
121 if( data_identifier_info.size != data_length )
122 {
123 status = PSA_ERROR_STORAGE_FAILURE;
124 goto exit;
125 }
126
127exit:
128 if( status != PSA_SUCCESS )
129 psa_its_remove( data_identifier );
130 return( status );
131}
132
Gilles Peskine5b229a02019-02-19 13:24:37 +0100133psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +0200134{
David Saadaa2523b22019-02-18 13:56:26 +0200135 psa_status_t ret;
136 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
137 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200138
139 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200140 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200141 return( PSA_SUCCESS );
142
David Saadaa2523b22019-02-18 13:56:26 +0200143 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +0200144 return( PSA_ERROR_STORAGE_FAILURE );
145
146 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200147 if( ret != PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200148 return( PSA_ERROR_STORAGE_FAILURE );
149
150 return( PSA_SUCCESS );
151}
152
Gilles Peskine5b229a02019-02-19 13:24:37 +0100153psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +0200154 size_t *data_length )
155{
Moran Pekera26d7642018-11-20 18:33:41 +0200156 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +0200157 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
158 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200159
David Saadaa2523b22019-02-18 13:56:26 +0200160 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200161 if( status != PSA_SUCCESS )
162 return( status );
163
164 *data_length = (size_t) data_identifier_info.size;
165
166 return( PSA_SUCCESS );
167}
168
169#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */