blob: 29394b5d89c63daeba9fbf45281728622f6e04e5 [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
30#include "psa/crypto.h"
31#include "psa_crypto_storage_backend.h"
32#include "psa_prot_internal_storage.h"
33
34#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#endif
37
38static psa_status_t its_to_psa_error( psa_its_status_t ret )
39{
40 switch( ret )
41 {
42 case PSA_ITS_SUCCESS:
43 return( PSA_SUCCESS );
44
45 case PSA_ITS_ERROR_KEY_NOT_FOUND:
46 return( PSA_ERROR_EMPTY_SLOT );
47
48 case PSA_ITS_ERROR_STORAGE_FAILURE:
49 return( PSA_ERROR_STORAGE_FAILURE );
50
51 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
52 return( PSA_ERROR_INSUFFICIENT_STORAGE );
53
54 case PSA_ITS_ERROR_INVALID_KEY:
55 case PSA_PS_ERROR_OFFSET_INVALID:
56 case PSA_ITS_ERROR_INCORRECT_SIZE:
57 case PSA_ITS_ERROR_BAD_POINTER:
58 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:
64 return( PSA_ERROR_OCCUPIED_SLOT );
65
66 default:
67 return( PSA_ERROR_UNKNOWN_ERROR );
68 }
69}
70
71static uint32_t psa_its_identifier_of_slot( psa_key_slot_t key )
72{
73 return( key );
74}
75
76psa_status_t psa_crypto_storage_load( const psa_key_slot_t key, uint8_t *data,
77 size_t data_size )
78{
79 psa_its_status_t ret;
80 psa_status_t status;
81 uint32_t data_identifier = psa_its_identifier_of_slot( key );
82 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
95int psa_is_key_present_in_storage( const psa_key_slot_t key )
96{
97 psa_its_status_t ret;
98 uint32_t data_identifier = psa_its_identifier_of_slot( key );
99 struct psa_its_info_t data_identifier_info;
100
101 ret = psa_its_get_info( data_identifier, &data_identifier_info );
102
103 if( ret == PSA_ITS_ERROR_KEY_NOT_FOUND )
104 return( 0 );
105 return( 1 );
106}
107
108psa_status_t psa_crypto_storage_store( const psa_key_slot_t key,
109 const uint8_t *data,
110 size_t data_length )
111{
112 psa_its_status_t ret;
113 psa_status_t status;
114 uint32_t data_identifier = psa_its_identifier_of_slot( key );
115 struct psa_its_info_t data_identifier_info;
116
117 if( psa_is_key_present_in_storage( key ) == 1 )
118 return( PSA_ERROR_OCCUPIED_SLOT );
119
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
146psa_status_t psa_destroy_persistent_key( const psa_key_slot_t key )
147{
148 psa_its_status_t ret;
149 uint32_t data_identifier = psa_its_identifier_of_slot( key );
150 struct psa_its_info_t data_identifier_info;
151
152 ret = psa_its_get_info( data_identifier, &data_identifier_info );
153 if( ret == PSA_ITS_ERROR_KEY_NOT_FOUND )
154 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 );
160 if( ret != PSA_ITS_ERROR_KEY_NOT_FOUND )
161 return( PSA_ERROR_STORAGE_FAILURE );
162
163 return( PSA_SUCCESS );
164}
165
166psa_status_t psa_crypto_storage_get_data_length( const psa_key_slot_t key,
167 size_t *data_length )
168{
169 psa_its_status_t ret;
170 psa_status_t status;
171 uint32_t data_identifier = psa_its_identifier_of_slot( key );
172 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 */