blob: 4b2789ff22f77b4759ae2a9c91371847e1e8df6d [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"
Moran Pekera26d7642018-11-20 18:33:41 +020031#include "psa/crypto.h"
32#include "psa_crypto_storage_backend.h"
Oren Cohen23a67842019-01-24 14:32:11 +020033#include "psa/internal_trusted_storage.h"
Moran Pekera26d7642018-11-20 18:33:41 +020034
35#if defined(MBEDTLS_PLATFORM_C)
36#include "mbedtls/platform.h"
37#endif
38
Gilles Peskine572f0672019-02-19 14:16:17 +010039/* Determine a file name (ITS file identifier) for the given key file
40 * identifier. The file name must be distinct from any file that is used
41 * for a purpose other than storing a key. Currently, the only such file
42 * is the random seed file whose name is PSA_CRYPTO_ITS_RANDOM_SEED_UID
43 * and whose value is 0xFFFFFF52. */
44static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_file_id_t file_id )
Moran Pekera26d7642018-11-20 18:33:41 +020045{
Gilles Peskine572f0672019-02-19 14:16:17 +010046#if defined(MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER) && \
47 defined(PSA_CRYPTO_SECURE)
48 /* Encode the owner in the upper 32 bits. This means that if
49 * owner values are nonzero (as they are on a PSA platform),
50 * no key file will ever have a value less than 0x100000000, so
51 * the whole range 0..0xffffffff is available for non-key files. */
52 uint32_t unsigned_owner = (uint32_t) file_id.owner;
53 return( (uint64_t) unsigned_owner << 32 | file_id.key_id );
54#else
55 /* Use the key id directly as a file name.
56 * psa_is_key_file_id_valid() in psa_crypto_slot_management.c
57 * is responsible for ensuring that key identifiers do not have a
58 * value that is reserved for non-key files. */
59 return( file_id );
60#endif
Moran Pekera26d7642018-11-20 18:33:41 +020061}
62
Gilles Peskine5b229a02019-02-19 13:24:37 +010063psa_status_t psa_crypto_storage_load( const psa_key_file_id_t key, uint8_t *data,
Moran Pekera26d7642018-11-20 18:33:41 +020064 size_t data_size )
65{
Moran Pekera26d7642018-11-20 18:33:41 +020066 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020067 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
68 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020069
David Saadaa2523b22019-02-18 13:56:26 +020070 status = psa_its_get_info( data_identifier, &data_identifier_info );
71 if( status != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +020072 return( status );
73
David Saadaa2523b22019-02-18 13:56:26 +020074 status = psa_its_get( data_identifier, 0, data_size, data );
Moran Pekera26d7642018-11-20 18:33:41 +020075
76 return( status );
77}
78
Gilles Peskine5b229a02019-02-19 13:24:37 +010079int psa_is_key_present_in_storage( const psa_key_file_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020080{
David Saadaa2523b22019-02-18 13:56:26 +020081 psa_status_t ret;
82 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
83 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020084
85 ret = psa_its_get_info( data_identifier, &data_identifier_info );
86
David Saadaa2523b22019-02-18 13:56:26 +020087 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +020088 return( 0 );
89 return( 1 );
90}
91
Gilles Peskine5b229a02019-02-19 13:24:37 +010092psa_status_t psa_crypto_storage_store( const psa_key_file_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +020093 const uint8_t *data,
94 size_t data_length )
95{
Moran Pekera26d7642018-11-20 18:33:41 +020096 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020097 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
98 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020099
100 if( psa_is_key_present_in_storage( key ) == 1 )
David Saadab4ecc272019-02-14 13:48:10 +0200101 return( PSA_ERROR_ALREADY_EXISTS );
Moran Pekera26d7642018-11-20 18:33:41 +0200102
David Saadaa2523b22019-02-18 13:56:26 +0200103 status = psa_its_set( data_identifier, data_length, data, 0 );
Moran Pekera26d7642018-11-20 18:33:41 +0200104 if( status != PSA_SUCCESS )
105 {
106 return( PSA_ERROR_STORAGE_FAILURE );
107 }
108
David Saadaa2523b22019-02-18 13:56:26 +0200109 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200110 if( status != PSA_SUCCESS )
111 {
112 goto exit;
113 }
114
115 if( data_identifier_info.size != data_length )
116 {
117 status = PSA_ERROR_STORAGE_FAILURE;
118 goto exit;
119 }
120
121exit:
122 if( status != PSA_SUCCESS )
123 psa_its_remove( data_identifier );
124 return( status );
125}
126
Gilles Peskine5b229a02019-02-19 13:24:37 +0100127psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +0200128{
David Saadaa2523b22019-02-18 13:56:26 +0200129 psa_status_t ret;
130 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
131 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200132
133 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200134 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200135 return( PSA_SUCCESS );
136
David Saadaa2523b22019-02-18 13:56:26 +0200137 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +0200138 return( PSA_ERROR_STORAGE_FAILURE );
139
140 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200141 if( ret != PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200142 return( PSA_ERROR_STORAGE_FAILURE );
143
144 return( PSA_SUCCESS );
145}
146
Gilles Peskine5b229a02019-02-19 13:24:37 +0100147psa_status_t psa_crypto_storage_get_data_length( const psa_key_file_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +0200148 size_t *data_length )
149{
Moran Pekera26d7642018-11-20 18:33:41 +0200150 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +0200151 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
152 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200153
David Saadaa2523b22019-02-18 13:56:26 +0200154 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200155 if( status != PSA_SUCCESS )
156 return( status );
157
158 *data_length = (size_t) data_identifier_info.size;
159
160 return( PSA_SUCCESS );
161}
162
163#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */