2
* PSA ITS simulator over stdio files.
5
* Copyright The Mbed TLS Contributors
6
* SPDX-License-Identifier: Apache-2.0
8
* Licensed under the Apache License, Version 2.0 (the "License"); you may
9
* not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
#if defined(MBEDTLS_CONFIG_FILE)
22
#include MBEDTLS_CONFIG_FILE
24
#include "mbedtls/config.h"
27
#if defined(MBEDTLS_PSA_ITS_FILE_C)
29
#if defined(MBEDTLS_PLATFORM_C)
30
#include "mbedtls/platform.h"
32
#define mbedtls_snprintf snprintf
39
#include "psa_crypto_its.h"
46
#if !defined(PSA_ITS_STORAGE_PREFIX)
47
#define PSA_ITS_STORAGE_PREFIX ""
50
#define PSA_ITS_STORAGE_FILENAME_PATTERN "%08lx%08lx"
51
#define PSA_ITS_STORAGE_SUFFIX ".psa_its"
52
#define PSA_ITS_STORAGE_FILENAME_LENGTH \
53
( sizeof( PSA_ITS_STORAGE_PREFIX ) - 1 + /*prefix without terminating 0*/ \
54
16 + /*UID (64-bit number in hex)*/ \
55
sizeof( PSA_ITS_STORAGE_SUFFIX ) - 1 + /*suffix without terminating 0*/ \
56
1 /*terminating null byte*/ )
57
#define PSA_ITS_STORAGE_TEMP \
58
PSA_ITS_STORAGE_PREFIX "tempfile" PSA_ITS_STORAGE_SUFFIX
60
/* The maximum value of psa_storage_info_t.size */
61
#define PSA_ITS_MAX_SIZE 0xffffffff
63
#define PSA_ITS_MAGIC_STRING "PSA\0ITS\0"
64
#define PSA_ITS_MAGIC_LENGTH 8
66
/* As rename fails on Windows if the new filepath already exists,
67
* use MoveFileExA with the MOVEFILE_REPLACE_EXISTING flag instead.
68
* Returns 0 on success, nonzero on failure. */
70
#define rename_replace_existing( oldpath, newpath ) \
71
( ! MoveFileExA( oldpath, newpath, MOVEFILE_REPLACE_EXISTING ) )
73
#define rename_replace_existing( oldpath, newpath ) rename( oldpath, newpath )
78
uint8_t magic[PSA_ITS_MAGIC_LENGTH];
79
uint8_t size[sizeof( uint32_t )];
80
uint8_t flags[sizeof( psa_storage_create_flags_t )];
81
} psa_its_file_header_t;
83
static void psa_its_fill_filename( psa_storage_uid_t uid, char *filename )
85
/* Break up the UID into two 32-bit pieces so as not to rely on
86
* long long support in snprintf. */
87
mbedtls_snprintf( filename, PSA_ITS_STORAGE_FILENAME_LENGTH,
88
"%s" PSA_ITS_STORAGE_FILENAME_PATTERN "%s",
89
PSA_ITS_STORAGE_PREFIX,
90
(unsigned long) ( uid >> 32 ),
91
(unsigned long) ( uid & 0xffffffff ),
92
PSA_ITS_STORAGE_SUFFIX );
95
static psa_status_t psa_its_read_file( psa_storage_uid_t uid,
96
struct psa_storage_info_t *p_info,
99
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
100
psa_its_file_header_t header;
104
psa_its_fill_filename( uid, filename );
105
*p_stream = fopen( filename, "rb" );
106
if( *p_stream == NULL )
107
return( PSA_ERROR_DOES_NOT_EXIST );
109
n = fread( &header, 1, sizeof( header ), *p_stream );
110
if( n != sizeof( header ) )
111
return( PSA_ERROR_DATA_CORRUPT );
112
if( memcmp( header.magic, PSA_ITS_MAGIC_STRING,
113
PSA_ITS_MAGIC_LENGTH ) != 0 )
114
return( PSA_ERROR_DATA_CORRUPT );
116
p_info->size = ( header.size[0] |
117
header.size[1] << 8 |
118
header.size[2] << 16 |
119
header.size[3] << 24 );
120
p_info->flags = ( header.flags[0] |
121
header.flags[1] << 8 |
122
header.flags[2] << 16 |
123
header.flags[3] << 24 );
124
return( PSA_SUCCESS );
127
psa_status_t psa_its_get_info( psa_storage_uid_t uid,
128
struct psa_storage_info_t *p_info )
132
status = psa_its_read_file( uid, p_info, &stream );
138
psa_status_t psa_its_get( psa_storage_uid_t uid,
139
uint32_t data_offset,
140
uint32_t data_length,
142
size_t *p_data_length )
147
struct psa_storage_info_t info;
149
status = psa_its_read_file( uid, &info, &stream );
150
if( status != PSA_SUCCESS )
152
status = PSA_ERROR_INVALID_ARGUMENT;
153
if( data_offset + data_length < data_offset )
155
#if SIZE_MAX < 0xffffffff
156
if( data_offset + data_length > SIZE_MAX )
159
if( data_offset + data_length > info.size )
162
status = PSA_ERROR_STORAGE_FAILURE;
163
#if LONG_MAX < 0xffffffff
164
while( data_offset > LONG_MAX )
166
if( fseek( stream, LONG_MAX, SEEK_CUR ) != 0 )
168
data_offset -= LONG_MAX;
171
if( fseek( stream, data_offset, SEEK_CUR ) != 0 )
173
n = fread( p_data, 1, data_length, stream );
174
if( n != data_length )
176
status = PSA_SUCCESS;
177
if( p_data_length != NULL )
186
psa_status_t psa_its_set( psa_storage_uid_t uid,
187
uint32_t data_length,
189
psa_storage_create_flags_t create_flags )
191
psa_status_t status = PSA_ERROR_STORAGE_FAILURE;
192
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
194
psa_its_file_header_t header;
197
memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH );
198
header.size[0] = data_length & 0xff;
199
header.size[1] = ( data_length >> 8 ) & 0xff;
200
header.size[2] = ( data_length >> 16 ) & 0xff;
201
header.size[3] = ( data_length >> 24 ) & 0xff;
202
header.flags[0] = create_flags & 0xff;
203
header.flags[1] = ( create_flags >> 8 ) & 0xff;
204
header.flags[2] = ( create_flags >> 16 ) & 0xff;
205
header.flags[3] = ( create_flags >> 24 ) & 0xff;
207
psa_its_fill_filename( uid, filename );
208
stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" );
212
status = PSA_ERROR_INSUFFICIENT_STORAGE;
213
n = fwrite( &header, 1, sizeof( header ), stream );
214
if( n != sizeof( header ) )
216
if( data_length != 0 )
218
n = fwrite( p_data, 1, data_length, stream );
219
if( n != data_length )
222
status = PSA_SUCCESS;
227
int ret = fclose( stream );
228
if( status == PSA_SUCCESS && ret != 0 )
229
status = PSA_ERROR_INSUFFICIENT_STORAGE;
231
if( status == PSA_SUCCESS )
233
if( rename_replace_existing( PSA_ITS_STORAGE_TEMP, filename ) != 0 )
234
status = PSA_ERROR_STORAGE_FAILURE;
236
/* The temporary file may still exist, but only in failure cases where
237
* we're already reporting an error. So there's nothing we can do on
238
* failure. If the function succeeded, and in some error cases, the
239
* temporary file doesn't exist and so remove() is expected to fail.
240
* Thus we just ignore the return status of remove(). */
241
(void) remove( PSA_ITS_STORAGE_TEMP );
245
psa_status_t psa_its_remove( psa_storage_uid_t uid )
247
char filename[PSA_ITS_STORAGE_FILENAME_LENGTH];
249
psa_its_fill_filename( uid, filename );
250
stream = fopen( filename, "rb" );
252
return( PSA_ERROR_DOES_NOT_EXIST );
254
if( remove( filename ) != 0 )
255
return( PSA_ERROR_STORAGE_FAILURE );
256
return( PSA_SUCCESS );
259
#endif /* MBEDTLS_PSA_ITS_FILE_C */