blob: f97a5d7de9819c8562ca5ea318a25293280db4e3 [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
39static psa_status_t its_to_psa_error( psa_its_status_t ret )
40{
41 switch( ret )
42 {
43 case PSA_ITS_SUCCESS:
44 return( PSA_SUCCESS );
45
Oren Cohen23a67842019-01-24 14:32:11 +020046 case PSA_ITS_ERROR_UID_NOT_FOUND:
David Saadab4ecc272019-02-14 13:48:10 +020047 return( PSA_ERROR_DOES_NOT_EXIST );
Moran Pekera26d7642018-11-20 18:33:41 +020048
49 case PSA_ITS_ERROR_STORAGE_FAILURE:
50 return( PSA_ERROR_STORAGE_FAILURE );
51
52 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
53 return( PSA_ERROR_INSUFFICIENT_STORAGE );
54
Jaeden Amero58600552018-11-30 12:04:38 +000055 case PSA_ITS_ERROR_OFFSET_INVALID:
Moran Pekera26d7642018-11-20 18:33:41 +020056 case PSA_ITS_ERROR_INCORRECT_SIZE:
Oren Cohen23a67842019-01-24 14:32:11 +020057 case PSA_ITS_ERROR_INVALID_ARGUMENTS:
Moran Pekera26d7642018-11-20 18:33:41 +020058 return( PSA_ERROR_INVALID_ARGUMENT );
59
60 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
61 return( PSA_ERROR_NOT_SUPPORTED );
62
63 case PSA_ITS_ERROR_WRITE_ONCE:
David Saadab4ecc272019-02-14 13:48:10 +020064 return( PSA_ERROR_ALREADY_EXISTS );
Moran Pekera26d7642018-11-20 18:33:41 +020065
66 default:
67 return( PSA_ERROR_UNKNOWN_ERROR );
68 }
69}
70
Oren Cohen231bf5b2019-01-28 14:50:31 +020071static psa_its_uid_t psa_its_identifier_of_slot( psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020072{
73 return( key );
74}
75
Gilles Peskine8d4919b2018-12-03 16:48:09 +010076psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
Moran Pekera26d7642018-11-20 18:33:41 +020077 size_t data_size )
78{
79 psa_its_status_t ret;
80 psa_status_t status;
Oren Cohen231bf5b2019-01-28 14:50:31 +020081 psa_its_uid_t data_identifier = psa_its_identifier_of_slot( key );
Moran Pekera26d7642018-11-20 18:33:41 +020082 struct psa_its_info_t data_identifier_info;
83
84 ret = psa_its_get_info( data_identifier, &data_identifier_info );
85 status = its_to_psa_error( ret );
86 if( status != PSA_SUCCESS )
87 return( status );
88
89 ret = psa_its_get( data_identifier, 0, data_size, data );
90 status = its_to_psa_error( ret );
91
92 return( status );
93}
94
Gilles Peskine8d4919b2018-12-03 16:48:09 +010095int psa_is_key_present_in_storage( const psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020096{
97 psa_its_status_t ret;
Oren Cohen231bf5b2019-01-28 14:50:31 +020098 psa_its_uid_t data_identifier = psa_its_identifier_of_slot( key );
Moran Pekera26d7642018-11-20 18:33:41 +020099 struct psa_its_info_t data_identifier_info;
100
101 ret = psa_its_get_info( data_identifier, &data_identifier_info );
102
Oren Cohen23a67842019-01-24 14:32:11 +0200103 if( ret == PSA_ITS_ERROR_UID_NOT_FOUND )
Moran Pekera26d7642018-11-20 18:33:41 +0200104 return( 0 );
105 return( 1 );
106}
107
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100108psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +0200109 const uint8_t *data,
110 size_t data_length )
111{
112 psa_its_status_t ret;
113 psa_status_t status;
Oren Cohen231bf5b2019-01-28 14:50:31 +0200114 psa_its_uid_t data_identifier = psa_its_identifier_of_slot( key );
Moran Pekera26d7642018-11-20 18:33:41 +0200115 struct psa_its_info_t data_identifier_info;
116
117 if( psa_is_key_present_in_storage( key ) == 1 )
David Saadab4ecc272019-02-14 13:48:10 +0200118 return( PSA_ERROR_ALREADY_EXISTS );
Moran Pekera26d7642018-11-20 18:33:41 +0200119
120 ret = psa_its_set( data_identifier, data_length, data, 0 );
121 status = its_to_psa_error( ret );
122 if( status != PSA_SUCCESS )
123 {
124 return( PSA_ERROR_STORAGE_FAILURE );
125 }
126
127 ret = psa_its_get_info( data_identifier, &data_identifier_info );
128 status = its_to_psa_error( ret );
129 if( status != PSA_SUCCESS )
130 {
131 goto exit;
132 }
133
134 if( data_identifier_info.size != data_length )
135 {
136 status = PSA_ERROR_STORAGE_FAILURE;
137 goto exit;
138 }
139
140exit:
141 if( status != PSA_SUCCESS )
142 psa_its_remove( data_identifier );
143 return( status );
144}
145
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100146psa_status_t psa_destroy_persistent_key( const psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +0200147{
148 psa_its_status_t ret;
Oren Cohen231bf5b2019-01-28 14:50:31 +0200149 psa_its_uid_t data_identifier = psa_its_identifier_of_slot( key );
Moran Pekera26d7642018-11-20 18:33:41 +0200150 struct psa_its_info_t data_identifier_info;
151
152 ret = psa_its_get_info( data_identifier, &data_identifier_info );
Oren Cohen23a67842019-01-24 14:32:11 +0200153 if( ret == PSA_ITS_ERROR_UID_NOT_FOUND )
Moran Pekera26d7642018-11-20 18:33:41 +0200154 return( PSA_SUCCESS );
155
156 if( psa_its_remove( data_identifier ) != PSA_ITS_SUCCESS )
157 return( PSA_ERROR_STORAGE_FAILURE );
158
159 ret = psa_its_get_info( data_identifier, &data_identifier_info );
Oren Cohen23a67842019-01-24 14:32:11 +0200160 if( ret != PSA_ITS_ERROR_UID_NOT_FOUND )
Moran Pekera26d7642018-11-20 18:33:41 +0200161 return( PSA_ERROR_STORAGE_FAILURE );
162
163 return( PSA_SUCCESS );
164}
165
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100166psa_status_t psa_crypto_storage_get_data_length( const psa_key_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +0200167 size_t *data_length )
168{
169 psa_its_status_t ret;
170 psa_status_t status;
Oren Cohen231bf5b2019-01-28 14:50:31 +0200171 psa_its_uid_t data_identifier = psa_its_identifier_of_slot( key );
Moran Pekera26d7642018-11-20 18:33:41 +0200172 struct psa_its_info_t data_identifier_info;
173
174 ret = psa_its_get_info( data_identifier, &data_identifier_info );
175 status = its_to_psa_error( ret );
176 if( status != PSA_SUCCESS )
177 return( status );
178
179 *data_length = (size_t) data_identifier_info.size;
180
181 return( PSA_SUCCESS );
182}
183
184#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */