blob: 85881c16440eaa5615151f561bba45f2dab1054a [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 */
62#define PSA_MAX_PERSISTENT_KEY_IDENTIFIER 0xffff0000
63
Darryl Greendb2b8db2018-06-15 13:06:04 +010064/**
65 * \brief Format key data and metadata and save to a location for given key
66 * slot.
67 *
68 * This function formats the key data and metadata and saves it to a
69 * persistent storage backend. The storage location corresponding to the
70 * key slot must be empty, otherwise this function will fail. This function
71 * should be called after psa_import_key_into_slot() to ensure the
72 * persistent key is not saved into a storage location corresponding to an
73 * already occupied non-persistent key, as well as validating the key data.
74 *
75 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +010076 * \param key Persistent identifier of the key to be stored. This
77 * should be an unoccupied storage location.
Darryl Greendb2b8db2018-06-15 13:06:04 +010078 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
79 * \param[in] policy The key policy to save.
80 * \param[in] data Buffer containing the key data.
81 * \param data_length The number of bytes that make up the key data.
82 *
83 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +010084 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
Darryl Greendb2b8db2018-06-15 13:06:04 +010085 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
86 * \retval PSA_ERROR_STORAGE_FAILURE
Gilles Peskine8d4919b2018-12-03 16:48:09 +010087 * \retval PSA_ERROR_OCCUPIED_SLOT
Darryl Greendb2b8db2018-06-15 13:06:04 +010088 */
Gilles Peskine8d4919b2018-12-03 16:48:09 +010089psa_status_t psa_save_persistent_key( const psa_key_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +010090 const psa_key_type_t type,
91 const psa_key_policy_t *policy,
92 const uint8_t *data,
93 const size_t data_length );
94
95/**
96 * \brief Parses key data and metadata and load persistent key for given
97 * key slot number.
98 *
99 * This function reads from a storage backend, parses the key data and
100 * metadata and writes them to the appropriate output parameters.
101 *
102 * Note: This function allocates a buffer and returns a pointer to it through
103 * the data parameter. psa_free_persistent_key_data() must be called after
104 * this function to zeroize and free this buffer, regardless of whether this
105 * function succeeds or fails.
106 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100107 * \param key Persistent identifier of the key to be loaded. This
108 * should be an occupied storage location.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100109 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX
110 * value).
111 * \param[out] policy On success, the key's policy.
112 * \param[out] data Pointer to an allocated key data buffer on return.
113 * \param[out] data_length The number of bytes that make up the key data.
114 *
115 * \retval PSA_SUCCESS
116 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
117 * \retval PSA_ERROR_STORAGE_FAILURE
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100118 * \retval PSA_ERROR_EMPTY_SLOT
Darryl Greendb2b8db2018-06-15 13:06:04 +0100119 */
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100120psa_status_t psa_load_persistent_key( psa_key_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100121 psa_key_type_t *type,
122 psa_key_policy_t *policy,
123 uint8_t **data,
124 size_t *data_length );
125
126/**
127 * \brief Remove persistent data for the given key slot number.
128 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100129 * \param key Persistent identifier of the key to remove
Darryl Greendb2b8db2018-06-15 13:06:04 +0100130 * from persistent storage.
131 *
132 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100133 * The key was successfully removed,
134 * or the key did not exist.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100135 * \retval PSA_ERROR_STORAGE_FAILURE
136 */
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100137psa_status_t psa_destroy_persistent_key( const psa_key_id_t key );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100138
139/**
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100140 * \brief Free the temporary buffer allocated by psa_load_persistent_key().
Darryl Greendb2b8db2018-06-15 13:06:04 +0100141 *
142 * This function must be called at some point after psa_load_persistent_key()
143 * to zeroize and free the memory allocated to the buffer in that function.
144 *
145 * \param key_data Buffer for the key data.
146 * \param key_data_length Size of the key data buffer.
147 *
148 */
149void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length );
150
151/**
152 * \brief Formats key data and metadata for persistent storage
153 *
154 * \param[in] data Buffer for the key data.
155 * \param data_length Length of the key data buffer.
156 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
157 * \param policy The key policy.
158 * \param[out] storage_data Output buffer for the formatted data.
159 *
160 */
161void psa_format_key_data_for_storage( const uint8_t *data,
162 const size_t data_length,
163 const psa_key_type_t type,
164 const psa_key_policy_t *policy,
165 uint8_t *storage_data );
166
167/**
168 * \brief Parses persistent storage data into key data and metadata
169 *
170 * \param[in] storage_data Buffer for the storage data.
171 * \param storage_data_length Length of the storage data buffer
172 * \param[out] key_data On output, pointer to a newly allocated buffer
173 * containing the key data. This must be freed
174 * using psa_free_persistent_key_data()
175 * \param[out] key_data_length Length of the key data buffer
176 * \param[out] type Key type (a \c PSA_KEY_TYPE_XXX value).
177 * \param[out] policy The key policy.
178 *
179 * \retval PSA_SUCCESS
180 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
181 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
182 * \retval PSA_ERROR_STORAGE_FAILURE
183 */
184psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
185 size_t storage_data_length,
186 uint8_t **key_data,
187 size_t *key_data_length,
188 psa_key_type_t *type,
189 psa_key_policy_t *policy );
190
191#ifdef __cplusplus
192}
193#endif
194
195#endif /* PSA_CRYPTO_STORAGE_H */