blob: 25049b08de4ddf360dfc51f0581f0046ff9cede5 [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
Darryl Greendb2b8db2018-06-15 13:06:04 +010032#include "psa/crypto.h"
Gilles Peskinefc762652019-07-22 19:30:34 +020033#include "psa/crypto_se_driver.h"
34
Darryl Greendb2b8db2018-06-15 13:06:04 +010035#include <stdint.h>
Gilles Peskinec8336cb2019-07-22 19:26:12 +020036#include <string.h>
Darryl Greendb2b8db2018-06-15 13:06:04 +010037
38/* Limit the maximum key size to 30kB (just in case someone tries to
39 * inadvertently store an obscene amount of data) */
40#define PSA_CRYPTO_MAX_STORAGE_SIZE ( 30 * 1024 )
41
Gilles Peskine48868122018-12-10 17:30:29 +010042/** The maximum permitted persistent slot number.
43 *
44 * In Mbed Crypto 0.1.0b:
45 * - Using the file backend, all key ids are ok except 0.
46 * - Using the ITS backend, all key ids are ok except 0xFFFFFF52
47 * (#PSA_CRYPTO_ITS_RANDOM_SEED_UID) for which the file contains the
48 * device's random seed (if this feature is enabled).
49 * - Only key ids from 1 to #PSA_KEY_SLOT_COUNT are actually used.
50 *
51 * Since we need to preserve the random seed, avoid using that key slot.
52 * Reserve a whole range of key slots just in case something else comes up.
53 *
54 * This limitation will probably become moot when we implement client
55 * separation for key storage.
56 */
Gilles Peskinef9666592019-05-06 18:56:30 +020057#define PSA_MAX_PERSISTENT_KEY_IDENTIFIER PSA_KEY_ID_VENDOR_MAX
Gilles Peskine48868122018-12-10 17:30:29 +010058
Darryl Greendb2b8db2018-06-15 13:06:04 +010059/**
Gilles Peskine5e80d912019-02-24 17:10:18 +010060 * \brief Checks if persistent data is stored for the given key slot number
61 *
62 * This function checks if any key data or metadata exists for the key slot in
63 * the persistent storage.
64 *
65 * \param key Persistent identifier to check.
66 *
67 * \retval 0
68 * No persistent data present for slot number
69 * \retval 1
70 * Persistent data present for slot number
71 */
72int psa_is_key_present_in_storage( const psa_key_file_id_t key );
73
74/**
Darryl Greendb2b8db2018-06-15 13:06:04 +010075 * \brief Format key data and metadata and save to a location for given key
76 * slot.
77 *
78 * This function formats the key data and metadata and saves it to a
79 * persistent storage backend. The storage location corresponding to the
80 * key slot must be empty, otherwise this function will fail. This function
81 * should be called after psa_import_key_into_slot() to ensure the
82 * persistent key is not saved into a storage location corresponding to an
83 * already occupied non-persistent key, as well as validating the key data.
84 *
85 *
Gilles Peskinebfd322f2019-07-23 11:58:03 +020086 * \param[in] attributes The attributes of the key to save.
87 * The key identifier field in the attributes
88 * determines the key's location.
89 * \param[in] data Buffer containing the key data.
90 * \param data_length The number of bytes that make up the key data.
Darryl Greendb2b8db2018-06-15 13:06:04 +010091 *
92 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +010093 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
Darryl Greendb2b8db2018-06-15 13:06:04 +010094 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
95 * \retval PSA_ERROR_STORAGE_FAILURE
David Saadab4ecc272019-02-14 13:48:10 +020096 * \retval PSA_ERROR_ALREADY_EXISTS
Darryl Greendb2b8db2018-06-15 13:06:04 +010097 */
Gilles Peskinebfd322f2019-07-23 11:58:03 +020098psa_status_t psa_save_persistent_key( const psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +010099 const uint8_t *data,
100 const size_t data_length );
101
102/**
103 * \brief Parses key data and metadata and load persistent key for given
104 * key slot number.
105 *
106 * This function reads from a storage backend, parses the key data and
107 * metadata and writes them to the appropriate output parameters.
108 *
109 * Note: This function allocates a buffer and returns a pointer to it through
110 * the data parameter. psa_free_persistent_key_data() must be called after
111 * this function to zeroize and free this buffer, regardless of whether this
112 * function succeeds or fails.
113 *
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200114 * \param[in,out] attributes
115 * On input, the key identifier field identifies
116 * the key to load. Other fields are ignored.
117 * On success, the attribute structure contains
118 * the key metadata that was loaded from storage.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100119 * \param[out] data Pointer to an allocated key data buffer on return.
120 * \param[out] data_length The number of bytes that make up the key data.
121 *
122 * \retval PSA_SUCCESS
123 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
124 * \retval PSA_ERROR_STORAGE_FAILURE
David Saadab4ecc272019-02-14 13:48:10 +0200125 * \retval PSA_ERROR_DOES_NOT_EXIST
Darryl Greendb2b8db2018-06-15 13:06:04 +0100126 */
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200127psa_status_t psa_load_persistent_key( psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100128 uint8_t **data,
129 size_t *data_length );
130
131/**
132 * \brief Remove persistent data for the given key slot number.
133 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100134 * \param key Persistent identifier of the key to remove
Darryl Greendb2b8db2018-06-15 13:06:04 +0100135 * from persistent storage.
136 *
137 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100138 * The key was successfully removed,
139 * or the key did not exist.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100140 * \retval PSA_ERROR_STORAGE_FAILURE
141 */
Gilles Peskine5b229a02019-02-19 13:24:37 +0100142psa_status_t psa_destroy_persistent_key( const psa_key_file_id_t key );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100143
144/**
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100145 * \brief Free the temporary buffer allocated by psa_load_persistent_key().
Darryl Greendb2b8db2018-06-15 13:06:04 +0100146 *
147 * This function must be called at some point after psa_load_persistent_key()
148 * to zeroize and free the memory allocated to the buffer in that function.
149 *
150 * \param key_data Buffer for the key data.
151 * \param key_data_length Size of the key data buffer.
152 *
153 */
154void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length );
155
156/**
157 * \brief Formats key data and metadata for persistent storage
158 *
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200159 * \param[in] data Buffer containing the key data.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100160 * \param data_length Length of the key data buffer.
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200161 * \param[in] attributes The attributes of the key.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100162 * \param[out] storage_data Output buffer for the formatted data.
163 *
164 */
165void psa_format_key_data_for_storage( const uint8_t *data,
166 const size_t data_length,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200167 const psa_key_attributes_t *attributes,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100168 uint8_t *storage_data );
169
170/**
171 * \brief Parses persistent storage data into key data and metadata
172 *
173 * \param[in] storage_data Buffer for the storage data.
174 * \param storage_data_length Length of the storage data buffer
175 * \param[out] key_data On output, pointer to a newly allocated buffer
176 * containing the key data. This must be freed
177 * using psa_free_persistent_key_data()
178 * \param[out] key_data_length Length of the key data buffer
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200179 * \param[out] attributes On success, the attribute structure is filled
180 * with the loaded key metadata.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100181 *
182 * \retval PSA_SUCCESS
183 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
184 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
185 * \retval PSA_ERROR_STORAGE_FAILURE
186 */
187psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
188 size_t storage_data_length,
189 uint8_t **key_data,
190 size_t *key_data_length,
Gilles Peskinebfd322f2019-07-23 11:58:03 +0200191 psa_key_attributes_t *attributes );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100192
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200193#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
194/** This symbol is defined if transaction support is required. */
195#define PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS
196#endif
197
198#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
199
200/** The type of transaction that is in progress.
201 */
202/* This is an integer type rather than an enum for two reasons: to support
203 * unknown values when loading a transaction file, and to ensure that the
204 * type has a known size.
205 */
206typedef uint16_t psa_crypto_transaction_type_t;
207
208/** No transaction is in progress.
209 */
210#define PSA_CRYPTO_TRANSACTION_NONE ( (psa_crypto_transaction_type_t) 0x0000 )
211
Gilles Peskinefc762652019-07-22 19:30:34 +0200212/** A key creation transaction.
213 *
214 * This is only used for keys in an external cryptoprocessor (secure element).
215 * Keys in RAM or in internal storage are created atomically in storage
216 * (simple file creation), so they do not need a transaction mechanism.
217 */
218#define PSA_CRYPTO_TRANSACTION_CREATE_KEY ( (psa_crypto_transaction_type_t) 0x0001 )
219
220/** A key destruction transaction.
221 *
222 * This is only used for keys in an external cryptoprocessor (secure element).
223 * Keys in RAM or in internal storage are destroyed atomically in storage
224 * (simple file deletion), so they do not need a transaction mechanism.
225 */
226#define PSA_CRYPTO_TRANSACTION_DESTROY_KEY ( (psa_crypto_transaction_type_t) 0x0002 )
227
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200228/** Transaction data.
229 *
230 * This type is designed to be serialized by writing the memory representation
231 * and reading it back on the same device.
232 *
233 * \note The transaction mechanism is designed for a single active transaction
234 * at a time. The transaction object is #psa_crypto_transaction.
235 *
236 * \note If an API call starts a transaction, it must complete this transaction
237 * before returning to the application.
238 *
239 * The lifetime of a transaction is the following (note that only one
240 * transaction may be active at a time):
241 *
242 * -# Call psa_crypto_prepare_transaction() to initialize the transaction
243 * object in memory and declare the type of transaction that is starting.
244 * -# Fill in the type-specific fields of #psa_crypto_transaction.
245 * -# Call psa_crypto_save_transaction() to start the transaction. This
246 * saves the transaction data to internal storage.
247 * -# If there are intermediate stages in the transaction, update
248 * the fields of #psa_crypto_transaction and call
249 * psa_crypto_save_transaction() again when each stage is reached.
250 * -# When the transaction is over, whether it has been committed or aborted,
251 * call psa_crypto_stop_transaction() to remove the transaction data in
252 * storage and in memory.
253 *
254 * If the system crashes while a transaction is in progress, psa_crypto_init()
255 * calls psa_crypto_load_transaction() and takes care of completing or
256 * rewinding the transaction.
257 */
258typedef union
259{
260 /* Each element of this union must have the following properties
261 * to facilitate serialization and deserialization:
262 *
263 * - The element is a struct.
264 * - The first field of the struct is `psa_crypto_transaction_type_t type`.
265 * - Elements of the struct are arranged such a way that there is
266 * no padding.
267 */
268 struct psa_crypto_transaction_unknown_s
269 {
270 psa_crypto_transaction_type_t type;
Gilles Peskinefc762652019-07-22 19:30:34 +0200271 uint16_t unused1;
272 uint32_t unused2;
273 uint64_t unused3;
274 uint64_t unused4;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200275 } unknown;
Gilles Peskinefc762652019-07-22 19:30:34 +0200276 /* ::type is #PSA_CRYPTO_TRANSACTION_CREATE_KEY or
277 * #PSA_CRYPTO_TRANSACTION_DESTROY_KEY. */
278 struct psa_crypto_transaction_key_s
279 {
280 psa_crypto_transaction_type_t type;
281 uint16_t unused1;
282 psa_key_lifetime_t lifetime;
283 psa_key_slot_number_t slot;
284 psa_key_id_t id;
285 } key;
Gilles Peskinec8336cb2019-07-22 19:26:12 +0200286} psa_crypto_transaction_t;
287
288/** The single active transaction.
289 */
290extern psa_crypto_transaction_t psa_crypto_transaction;
291
292/** Prepare for a transaction.
293 *
294 * There must not be an ongoing transaction.
295 *
296 * \param type The type of transaction to start.
297 */
298static inline void psa_crypto_prepare_transaction(
299 psa_crypto_transaction_type_t type )
300{
301 psa_crypto_transaction.unknown.type = type;
302}
303
304/** Save the transaction data to storage.
305 *
306 * You may call this function multiple times during a transaction to
307 * atomically update the transaction state.
308 *
309 * \retval #PSA_SUCCESS
310 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
311 * \retval #PSA_ERROR_STORAGE_FAILURE
312 */
313psa_status_t psa_crypto_save_transaction( void );
314
315/** Load the transaction data from storage, if any.
316 *
317 * This function is meant to be called from psa_crypto_init() to recover
318 * in case a transaction was interrupted by a system crash.
319 *
320 * \retval #PSA_SUCCESS
321 * The data about the ongoing transaction has been loaded to
322 * #psa_crypto_transaction.
323 * \retval #PSA_ERROR_DOES_NOT_EXIST
324 * There is no ongoing transaction.
325 * \retval #PSA_ERROR_STORAGE_FAILURE
326 */
327psa_status_t psa_crypto_load_transaction( void );
328
329/** Indicate that the current transaction is finished.
330 *
331 * Call this function at the very end of transaction processing, whether
332 * the transaction has been committed or aborted.
333 *
334 * This function erases the transaction data in storage (if any) and
335 * resets the transaction data in memory.
336 *
337 * \retval #PSA_SUCCESS
338 * There was transaction data in storage.
339 * \retval #PSA_ERROR_DOES_NOT_EXIST
340 * There was no transaction data in storage.
341 * \retval #PSA_ERROR_STORAGE_FAILURE
342 * It was impossible to determine whether there was transaction data
343 * in storage, or the transaction data could not be erased.
344 */
345psa_status_t psa_crypto_stop_transaction( void );
346
347/** The ITS file identifier for the transaction data.
348 *
349 * 0xffffffNN = special file; 0x74 = 't' for transaction.
350 */
351#define PSA_CRYPTO_ITS_TRANSACTION_UID ( (psa_key_id_t) 0xffffff74 )
352
353#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
354
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100355#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
356/** Backend side of mbedtls_psa_inject_entropy().
357 *
358 * This function stores the supplied data into the entropy seed file.
359 *
360 * \retval #PSA_SUCCESS
361 * Success
362 * \retval #PSA_ERROR_STORAGE_FAILURE
363 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
364 * \retval #PSA_ERROR_NOT_PERMITTED
365 * The entropy seed file already exists.
366 */
367psa_status_t mbedtls_psa_storage_inject_entropy( const unsigned char *seed,
368 size_t seed_size );
369#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
370
Darryl Greendb2b8db2018-06-15 13:06:04 +0100371#ifdef __cplusplus
372}
373#endif
374
375#endif /* PSA_CRYPTO_STORAGE_H */