blob: d939f0dc1ebdfaf1a13a69e049bac8879b9af3ff [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
David Saadaa2523b22019-02-18 13:56:26 +020040static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020041{
42 return( key );
43}
44
Gilles Peskine8d4919b2018-12-03 16:48:09 +010045psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
Moran Pekera26d7642018-11-20 18:33:41 +020046 size_t data_size )
47{
Moran Pekera26d7642018-11-20 18:33:41 +020048 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020049 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
50 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020051
David Saadaa2523b22019-02-18 13:56:26 +020052 status = psa_its_get_info( data_identifier, &data_identifier_info );
53 if( status != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +020054 return( status );
55
David Saadaa2523b22019-02-18 13:56:26 +020056 status = psa_its_get( data_identifier, 0, data_size, data );
Moran Pekera26d7642018-11-20 18:33:41 +020057
58 return( status );
59}
60
Gilles Peskine8d4919b2018-12-03 16:48:09 +010061int psa_is_key_present_in_storage( const psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020062{
David Saadaa2523b22019-02-18 13:56:26 +020063 psa_status_t ret;
64 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
65 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020066
67 ret = psa_its_get_info( data_identifier, &data_identifier_info );
68
David Saadaa2523b22019-02-18 13:56:26 +020069 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +020070 return( 0 );
71 return( 1 );
72}
73
Gilles Peskine8d4919b2018-12-03 16:48:09 +010074psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +020075 const uint8_t *data,
76 size_t data_length )
77{
Moran Pekera26d7642018-11-20 18:33:41 +020078 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020079 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
80 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020081
82 if( psa_is_key_present_in_storage( key ) == 1 )
David Saadab4ecc272019-02-14 13:48:10 +020083 return( PSA_ERROR_ALREADY_EXISTS );
Moran Pekera26d7642018-11-20 18:33:41 +020084
David Saadaa2523b22019-02-18 13:56:26 +020085 status = psa_its_set( data_identifier, data_length, data, 0 );
Moran Pekera26d7642018-11-20 18:33:41 +020086 if( status != PSA_SUCCESS )
87 {
88 return( PSA_ERROR_STORAGE_FAILURE );
89 }
90
David Saadaa2523b22019-02-18 13:56:26 +020091 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +020092 if( status != PSA_SUCCESS )
93 {
94 goto exit;
95 }
96
97 if( data_identifier_info.size != data_length )
98 {
99 status = PSA_ERROR_STORAGE_FAILURE;
100 goto exit;
101 }
102
103exit:
104 if( status != PSA_SUCCESS )
105 psa_its_remove( data_identifier );
106 return( status );
107}
108
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100109psa_status_t psa_destroy_persistent_key( const psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +0200110{
David Saadaa2523b22019-02-18 13:56:26 +0200111 psa_status_t ret;
112 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
113 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200114
115 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200116 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200117 return( PSA_SUCCESS );
118
David Saadaa2523b22019-02-18 13:56:26 +0200119 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +0200120 return( PSA_ERROR_STORAGE_FAILURE );
121
122 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200123 if( ret != PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200124 return( PSA_ERROR_STORAGE_FAILURE );
125
126 return( PSA_SUCCESS );
127}
128
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100129psa_status_t psa_crypto_storage_get_data_length( const psa_key_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +0200130 size_t *data_length )
131{
Moran Pekera26d7642018-11-20 18:33:41 +0200132 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +0200133 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
134 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200135
David Saadaa2523b22019-02-18 13:56:26 +0200136 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200137 if( status != PSA_SUCCESS )
138 return( status );
139
140 *data_length = (size_t) data_identifier_info.size;
141
142 return( PSA_SUCCESS );
143}
144
145#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */