blob: 447c0aebb4126449ef7875425f0e184019e48609 [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
David Saadab4ecc272019-02-14 13:48:10 +020030#include "psa/error.h"
itayzafrir7723ab12019-02-14 10:28:02 +020031#include "psa_crypto_service_integration.h"
Moran Pekera26d7642018-11-20 18:33:41 +020032#include "psa/crypto.h"
33#include "psa_crypto_storage_backend.h"
Oren Cohen23a67842019-01-24 14:32:11 +020034#include "psa/internal_trusted_storage.h"
Moran Pekera26d7642018-11-20 18:33:41 +020035
36#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#endif
39
Gilles Peskine572f0672019-02-19 14:16:17 +010040/* Determine a file name (ITS file identifier) for the given key file
41 * identifier. The file name must be distinct from any file that is used
42 * for a purpose other than storing a key. Currently, the only such file
43 * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID
44 * and whose value is 0xFFFFFF52. */
45static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id )
Moran Pekera26d7642018-11-20 18:33:41 +020046{
Gilles Peskine572f0672019-02-19 14:16:17 +010047#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \
48 defined(PSA_CRYPTO_SECURE)
49 /* Encode the owner in the upper 32 bits. This means that if
50 * owner values are nonzero (as they are on a PSA platform),
51 * no key file will ever have a value less than 0x100000000, so
52 * the whole range 0..0xffffffff is available for non-key files. */
53 uint32_t unsigned_owner = (uint32_t) file_id.owner;
54 return( (uint64_t) unsigned_owner << 32 | file_id.key_id );
55#else
56 /* Use the key id directly as a file name.
57 * psa_is_key_file_id_valid() in psa_crypto_slot_management.c
58 * is responsible for ensuring that key identifiers do not have a
59 * value that is reserved for non-key files. */
60 return( file_id );
61#endif
Moran Pekera26d7642018-11-20 18:33:41 +020062}
63
Gilles Peskine5b229a02019-02-19 13:24:37 +010064psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data,
Moran Pekera26d7642018-11-20 18:33:41 +020065 size_t data_size )
66{
Moran Pekera26d7642018-11-20 18:33:41 +020067 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020068 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
69 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020070
David Saadaa2523b22019-02-18 13:56:26 +020071 status = psa_its_get_info( data_identifier, &data_identifier_info );
72 if( status != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +020073 return( status );
74
David Saadaa2523b22019-02-18 13:56:26 +020075 status = psa_its_get( data_identifier, 0, data_size, data );
Moran Pekera26d7642018-11-20 18:33:41 +020076
77 return( status );
78}
79
Gilles Peskine5b229a02019-02-19 13:24:37 +010080int psa_is_key_present_in_storage( const psa_key_file_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020081{
David Saadaa2523b22019-02-18 13:56:26 +020082 psa_status_t ret;
83 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
84 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020085
86 ret = psa_its_get_info( data_identifier, &data_identifier_info );
87
David Saadaa2523b22019-02-18 13:56:26 +020088 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +020089 return( 0 );
90 return( 1 );
91}
92
Gilles Peskine5b229a02019-02-19 13:24:37 +010093psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +020094 const uint8_t *data,
95 size_t data_length )
96{
Moran Pekera26d7642018-11-20 18:33:41 +020097 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020098 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
99 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200100
101 if( psa_is_key_present_in_storage( key ) == 1 )
David Saadab4ecc272019-02-14 13:48:10 +0200102 return( PSA_ERROR_ALREADY_EXISTS );
Moran Pekera26d7642018-11-20 18:33:41 +0200103
David Saadaa2523b22019-02-18 13:56:26 +0200104 status = psa_its_set( data_identifier, data_length, data, 0 );
Moran Pekera26d7642018-11-20 18:33:41 +0200105 if( status != PSA_SUCCESS )
106 {
107 return( PSA_ERROR_STORAGE_FAILURE );
108 }
109
David Saadaa2523b22019-02-18 13:56:26 +0200110 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200111 if( status != PSA_SUCCESS )
112 {
113 goto exit;
114 }
115
116 if( data_identifier_info.size != data_length )
117 {
118 status = PSA_ERROR_STORAGE_FAILURE;
119 goto exit;
120 }
121
122exit:
123 if( status != PSA_SUCCESS )
124 psa_its_remove( data_identifier );
125 return( status );
126}
127
Gilles Peskine5b229a02019-02-19 13:24:37 +0100128psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +0200129{
David Saadaa2523b22019-02-18 13:56:26 +0200130 psa_status_t ret;
131 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
132 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200133
134 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200135 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200136 return( PSA_SUCCESS );
137
David Saadaa2523b22019-02-18 13:56:26 +0200138 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +0200139 return( PSA_ERROR_STORAGE_FAILURE );
140
141 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200142 if( ret != PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200143 return( PSA_ERROR_STORAGE_FAILURE );
144
145 return( PSA_SUCCESS );
146}
147
Gilles Peskine5b229a02019-02-19 13:24:37 +0100148psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +0200149 size_t *data_length )
150{
Moran Pekera26d7642018-11-20 18:33:41 +0200151 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +0200152 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
153 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200154
David Saadaa2523b22019-02-18 13:56:26 +0200155 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200156 if( status != PSA_SUCCESS )
157 return( status );
158
159 *data_length = (size_t) data_identifier_info.size;
160
161 return( PSA_SUCCESS );
162}
163
164#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */