blob: bb0d0cdf19ec0fe17abb835a5b68aadc349c8c1c [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
David Saadaa2523b22019-02-18 13:56:26 +020039static psa_storage_uid_t psa_its_identifier_of_slot( psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020040{
41 return( key );
42}
43
Gilles Peskine8d4919b2018-12-03 16:48:09 +010044psa_status_t psa_crypto_storage_load( const psa_key_id_t key, uint8_t *data,
Moran Pekera26d7642018-11-20 18:33:41 +020045 size_t data_size )
46{
Moran Pekera26d7642018-11-20 18:33:41 +020047 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020048 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
49 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020050
David Saadaa2523b22019-02-18 13:56:26 +020051 status = psa_its_get_info( data_identifier, &data_identifier_info );
52 if( status != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +020053 return( status );
54
David Saadaa2523b22019-02-18 13:56:26 +020055 status = psa_its_get( data_identifier, 0, data_size, data );
Moran Pekera26d7642018-11-20 18:33:41 +020056
57 return( status );
58}
59
Gilles Peskine8d4919b2018-12-03 16:48:09 +010060int psa_is_key_present_in_storage( const psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +020061{
David Saadaa2523b22019-02-18 13:56:26 +020062 psa_status_t ret;
63 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
64 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020065
66 ret = psa_its_get_info( data_identifier, &data_identifier_info );
67
David Saadaa2523b22019-02-18 13:56:26 +020068 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +020069 return( 0 );
70 return( 1 );
71}
72
Gilles Peskine8d4919b2018-12-03 16:48:09 +010073psa_status_t psa_crypto_storage_store( const psa_key_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +020074 const uint8_t *data,
75 size_t data_length )
76{
Moran Pekera26d7642018-11-20 18:33:41 +020077 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +020078 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
79 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +020080
81 if( psa_is_key_present_in_storage( key ) == 1 )
David Saadab4ecc272019-02-14 13:48:10 +020082 return( PSA_ERROR_ALREADY_EXISTS );
Moran Pekera26d7642018-11-20 18:33:41 +020083
David Saadaa2523b22019-02-18 13:56:26 +020084 status = psa_its_set( data_identifier, data_length, data, 0 );
Moran Pekera26d7642018-11-20 18:33:41 +020085 if( status != PSA_SUCCESS )
86 {
87 return( PSA_ERROR_STORAGE_FAILURE );
88 }
89
David Saadaa2523b22019-02-18 13:56:26 +020090 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +020091 if( status != PSA_SUCCESS )
92 {
93 goto exit;
94 }
95
96 if( data_identifier_info.size != data_length )
97 {
98 status = PSA_ERROR_STORAGE_FAILURE;
99 goto exit;
100 }
101
102exit:
103 if( status != PSA_SUCCESS )
104 psa_its_remove( data_identifier );
105 return( status );
106}
107
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100108psa_status_t psa_destroy_persistent_key( const psa_key_id_t key )
Moran Pekera26d7642018-11-20 18:33:41 +0200109{
David Saadaa2523b22019-02-18 13:56:26 +0200110 psa_status_t ret;
111 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
112 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200113
114 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200115 if( ret == PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200116 return( PSA_SUCCESS );
117
David Saadaa2523b22019-02-18 13:56:26 +0200118 if( psa_its_remove( data_identifier ) != PSA_SUCCESS )
Moran Pekera26d7642018-11-20 18:33:41 +0200119 return( PSA_ERROR_STORAGE_FAILURE );
120
121 ret = psa_its_get_info( data_identifier, &data_identifier_info );
David Saadaa2523b22019-02-18 13:56:26 +0200122 if( ret != PSA_ERROR_DOES_NOT_EXIST )
Moran Pekera26d7642018-11-20 18:33:41 +0200123 return( PSA_ERROR_STORAGE_FAILURE );
124
125 return( PSA_SUCCESS );
126}
127
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100128psa_status_t psa_crypto_storage_get_data_length( const psa_key_id_t key,
Moran Pekera26d7642018-11-20 18:33:41 +0200129 size_t *data_length )
130{
Moran Pekera26d7642018-11-20 18:33:41 +0200131 psa_status_t status;
David Saadaa2523b22019-02-18 13:56:26 +0200132 psa_storage_uid_t data_identifier = psa_its_identifier_of_slot( key );
133 struct psa_storage_info_t data_identifier_info;
Moran Pekera26d7642018-11-20 18:33:41 +0200134
David Saadaa2523b22019-02-18 13:56:26 +0200135 status = psa_its_get_info( data_identifier, &data_identifier_info );
Moran Pekera26d7642018-11-20 18:33:41 +0200136 if( status != PSA_SUCCESS )
137 return( status );
138
139 *data_length = (size_t) data_identifier_info.size;
140
141 return( PSA_SUCCESS );
142}
143
144#endif /* MBEDTLS_PSA_CRYPTO_STORAGE_ITS_C */