blob: 902e3026b1e7784cfea4988957fce625a59b2e63 [file] [log] [blame]
Darryl Greendb2b8db2018-06-15 13:06:04 +01001/**
2 * \file psa_crypto_storage.h
3 *
4 * \brief PSA cryptography module: Mbed TLS key storage
5 */
6/*
7 * Copyright (C) 2018, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24
25#ifndef PSA_CRYPTO_STORAGE_H
26#define PSA_CRYPTO_STORAGE_H
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
32/* Include the Mbed TLS configuration file, the way Mbed TLS does it
33 * in each of its header files. */
34#if defined(MBEDTLS_CONFIG_FILE)
35#include MBEDTLS_CONFIG_FILE
36#else
37#include "mbedtls/config.h"
38#endif
39
40#include "psa/crypto.h"
41#include <stdint.h>
42
43/* Limit the maximum key size to 30kB (just in case someone tries to
44 * inadvertently store an obscene amount of data) */
45#define PSA_CRYPTO_MAX_STORAGE_SIZE ( 30 * 1024 )
46
Gilles Peskine48868122018-12-10 17:30:29 +010047/** The maximum permitted persistent slot number.
48 *
49 * In Mbed Crypto 0.1.0b:
50 * - Using the file backend, all key ids are ok except 0.
51 * - Using the ITS backend, all key ids are ok except 0xFFFFFF52
52 * (#PSA_CRYPTO_ITS_RANDOM_SEED_UID) for which the file contains the
53 * device's random seed (if this feature is enabled).
54 * - Only key ids from 1 to #PSA_KEY_SLOT_COUNT are actually used.
55 *
56 * Since we need to preserve the random seed, avoid using that key slot.
57 * Reserve a whole range of key slots just in case something else comes up.
58 *
59 * This limitation will probably become moot when we implement client
60 * separation for key storage.
61 */
Gilles Peskinee988a662019-02-18 17:33:52 +010062#define PSA_MAX_PERSISTENT_KEY_IDENTIFIER 0xfffeffff
Gilles Peskine48868122018-12-10 17:30:29 +010063
Darryl Greendb2b8db2018-06-15 13:06:04 +010064/**
Gilles Peskine5e80d912019-02-24 17:10:18 +010065 * \brief Checks if persistent data is stored for the given key slot number
66 *
67 * This function checks if any key data or metadata exists for the key slot in
68 * the persistent storage.
69 *
70 * \param key Persistent identifier to check.
71 *
72 * \retval 0
73 * No persistent data present for slot number
74 * \retval 1
75 * Persistent data present for slot number
76 */
77int psa_is_key_present_in_storage( const psa_key_file_id_t key );
78
79/**
Darryl Greendb2b8db2018-06-15 13:06:04 +010080 * \brief Format key data and metadata and save to a location for given key
81 * slot.
82 *
83 * This function formats the key data and metadata and saves it to a
84 * persistent storage backend. The storage location corresponding to the
85 * key slot must be empty, otherwise this function will fail. This function
86 * should be called after psa_import_key_into_slot() to ensure the
87 * persistent key is not saved into a storage location corresponding to an
88 * already occupied non-persistent key, as well as validating the key data.
89 *
90 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +010091 * \param key Persistent identifier of the key to be stored. This
92 * should be an unoccupied storage location.
Darryl Greendb2b8db2018-06-15 13:06:04 +010093 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
94 * \param[in] policy The key policy to save.
95 * \param[in] data Buffer containing the key data.
96 * \param data_length The number of bytes that make up the key data.
97 *
98 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +010099 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
Darryl Greendb2b8db2018-06-15 13:06:04 +0100100 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
101 * \retval PSA_ERROR_STORAGE_FAILURE
David Saadab4ecc272019-02-14 13:48:10 +0200102 * \retval PSA_ERROR_ALREADY_EXISTS
Darryl Greendb2b8db2018-06-15 13:06:04 +0100103 */
Gilles Peskine5b229a02019-02-19 13:24:37 +0100104psa_status_t psa_save_persistent_key( const psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100105 const psa_key_type_t type,
106 const psa_key_policy_t *policy,
107 const uint8_t *data,
108 const size_t data_length );
109
110/**
111 * \brief Parses key data and metadata and load persistent key for given
112 * key slot number.
113 *
114 * This function reads from a storage backend, parses the key data and
115 * metadata and writes them to the appropriate output parameters.
116 *
117 * Note: This function allocates a buffer and returns a pointer to it through
118 * the data parameter. psa_free_persistent_key_data() must be called after
119 * this function to zeroize and free this buffer, regardless of whether this
120 * function succeeds or fails.
121 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100122 * \param key Persistent identifier of the key to be loaded. This
123 * should be an occupied storage location.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100124 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX
125 * value).
126 * \param[out] policy On success, the key's policy.
127 * \param[out] data Pointer to an allocated key data buffer on return.
128 * \param[out] data_length The number of bytes that make up the key data.
129 *
130 * \retval PSA_SUCCESS
131 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
132 * \retval PSA_ERROR_STORAGE_FAILURE
David Saadab4ecc272019-02-14 13:48:10 +0200133 * \retval PSA_ERROR_DOES_NOT_EXIST
Darryl Greendb2b8db2018-06-15 13:06:04 +0100134 */
Gilles Peskine5b229a02019-02-19 13:24:37 +0100135psa_status_t psa_load_persistent_key( psa_key_file_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100136 psa_key_type_t *type,
137 psa_key_policy_t *policy,
138 uint8_t **data,
139 size_t *data_length );
140
141/**
142 * \brief Remove persistent data for the given key slot number.
143 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100144 * \param key Persistent identifier of the key to remove
Darryl Greendb2b8db2018-06-15 13:06:04 +0100145 * from persistent storage.
146 *
147 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100148 * The key was successfully removed,
149 * or the key did not exist.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100150 * \retval PSA_ERROR_STORAGE_FAILURE
151 */
Gilles Peskine5b229a02019-02-19 13:24:37 +0100152psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100153
154/**
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100155 * \brief Free the temporary buffer allocated by psa_load_persistent_key().
Darryl Greendb2b8db2018-06-15 13:06:04 +0100156 *
157 * This function must be called at some point after psa_load_persistent_key()
158 * to zeroize and free the memory allocated to the buffer in that function.
159 *
160 * \param key_data Buffer for the key data.
161 * \param key_data_length Size of the key data buffer.
162 *
163 */
164void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length );
165
166/**
167 * \brief Formats key data and metadata for persistent storage
168 *
169 * \param[in] data Buffer for the key data.
170 * \param data_length Length of the key data buffer.
171 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
172 * \param policy The key policy.
173 * \param[out] storage_data Output buffer for the formatted data.
174 *
175 */
176void psa_format_key_data_for_storage( const uint8_t *data,
177 const size_t data_length,
178 const psa_key_type_t type,
179 const psa_key_policy_t *policy,
180 uint8_t *storage_data );
181
182/**
183 * \brief Parses persistent storage data into key data and metadata
184 *
185 * \param[in] storage_data Buffer for the storage data.
186 * \param storage_data_length Length of the storage data buffer
187 * \param[out] key_data On output, pointer to a newly allocated buffer
188 * containing the key data. This must be freed
189 * using psa_free_persistent_key_data()
190 * \param[out] key_data_length Length of the key data buffer
191 * \param[out] type Key type (a \c PSA_KEY_TYPE_XXX value).
192 * \param[out] policy The key policy.
193 *
194 * \retval PSA_SUCCESS
195 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
196 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
197 * \retval PSA_ERROR_STORAGE_FAILURE
198 */
199psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
200 size_t storage_data_length,
201 uint8_t **key_data,
202 size_t *key_data_length,
203 psa_key_type_t *type,
204 psa_key_policy_t *policy );
205
206#ifdef __cplusplus
207}
208#endif
209
210#endif /* PSA_CRYPTO_STORAGE_H */