blob: 478daef8f972e440ab1557370c970b37df58c5cc [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
47/**
48 * \brief Format key data and metadata and save to a location for given key
49 * slot.
50 *
51 * This function formats the key data and metadata and saves it to a
52 * persistent storage backend. The storage location corresponding to the
53 * key slot must be empty, otherwise this function will fail. This function
54 * should be called after psa_import_key_into_slot() to ensure the
55 * persistent key is not saved into a storage location corresponding to an
56 * already occupied non-persistent key, as well as validating the key data.
57 *
58 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +010059 * \param key Persistent identifier of the key to be stored. This
60 * should be an unoccupied storage location.
Darryl Greendb2b8db2018-06-15 13:06:04 +010061 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
62 * \param[in] policy The key policy to save.
63 * \param[in] data Buffer containing the key data.
64 * \param data_length The number of bytes that make up the key data.
65 *
66 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +010067 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
Darryl Greendb2b8db2018-06-15 13:06:04 +010068 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
69 * \retval PSA_ERROR_STORAGE_FAILURE
Gilles Peskine8d4919b2018-12-03 16:48:09 +010070 * \retval PSA_ERROR_OCCUPIED_SLOT
Darryl Greendb2b8db2018-06-15 13:06:04 +010071 */
Gilles Peskine8d4919b2018-12-03 16:48:09 +010072psa_status_t psa_save_persistent_key( const psa_key_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +010073 const psa_key_type_t type,
74 const psa_key_policy_t *policy,
75 const uint8_t *data,
76 const size_t data_length );
77
78/**
79 * \brief Parses key data and metadata and load persistent key for given
80 * key slot number.
81 *
82 * This function reads from a storage backend, parses the key data and
83 * metadata and writes them to the appropriate output parameters.
84 *
85 * Note: This function allocates a buffer and returns a pointer to it through
86 * the data parameter. psa_free_persistent_key_data() must be called after
87 * this function to zeroize and free this buffer, regardless of whether this
88 * function succeeds or fails.
89 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +010090 * \param key Persistent identifier of the key to be loaded. This
91 * should be an occupied storage location.
Darryl Greendb2b8db2018-06-15 13:06:04 +010092 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX
93 * value).
94 * \param[out] policy On success, the key's policy.
95 * \param[out] data Pointer to an allocated key data buffer on return.
96 * \param[out] data_length The number of bytes that make up the key data.
97 *
98 * \retval PSA_SUCCESS
99 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
100 * \retval PSA_ERROR_STORAGE_FAILURE
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100101 * \retval PSA_ERROR_EMPTY_SLOT
Darryl Greendb2b8db2018-06-15 13:06:04 +0100102 */
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100103psa_status_t psa_load_persistent_key( psa_key_id_t key,
Darryl Greendb2b8db2018-06-15 13:06:04 +0100104 psa_key_type_t *type,
105 psa_key_policy_t *policy,
106 uint8_t **data,
107 size_t *data_length );
108
109/**
110 * \brief Remove persistent data for the given key slot number.
111 *
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100112 * \param key Persistent identifier of the key to remove
Darryl Greendb2b8db2018-06-15 13:06:04 +0100113 * from persistent storage.
114 *
115 * \retval PSA_SUCCESS
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100116 * The key was successfully removed,
117 * or the key did not exist.
Darryl Greendb2b8db2018-06-15 13:06:04 +0100118 * \retval PSA_ERROR_STORAGE_FAILURE
119 */
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100120psa_status_t psa_destroy_persistent_key( const psa_key_id_t key );
Darryl Greendb2b8db2018-06-15 13:06:04 +0100121
122/**
Gilles Peskine8d4919b2018-12-03 16:48:09 +0100123 * \brief Free the temporary buffer allocated by psa_load_persistent_key().
Darryl Greendb2b8db2018-06-15 13:06:04 +0100124 *
125 * This function must be called at some point after psa_load_persistent_key()
126 * to zeroize and free the memory allocated to the buffer in that function.
127 *
128 * \param key_data Buffer for the key data.
129 * \param key_data_length Size of the key data buffer.
130 *
131 */
132void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length );
133
134/**
135 * \brief Formats key data and metadata for persistent storage
136 *
137 * \param[in] data Buffer for the key data.
138 * \param data_length Length of the key data buffer.
139 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
140 * \param policy The key policy.
141 * \param[out] storage_data Output buffer for the formatted data.
142 *
143 */
144void psa_format_key_data_for_storage( const uint8_t *data,
145 const size_t data_length,
146 const psa_key_type_t type,
147 const psa_key_policy_t *policy,
148 uint8_t *storage_data );
149
150/**
151 * \brief Parses persistent storage data into key data and metadata
152 *
153 * \param[in] storage_data Buffer for the storage data.
154 * \param storage_data_length Length of the storage data buffer
155 * \param[out] key_data On output, pointer to a newly allocated buffer
156 * containing the key data. This must be freed
157 * using psa_free_persistent_key_data()
158 * \param[out] key_data_length Length of the key data buffer
159 * \param[out] type Key type (a \c PSA_KEY_TYPE_XXX value).
160 * \param[out] policy The key policy.
161 *
162 * \retval PSA_SUCCESS
163 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
164 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
165 * \retval PSA_ERROR_STORAGE_FAILURE
166 */
167psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
168 size_t storage_data_length,
169 uint8_t **key_data,
170 size_t *key_data_length,
171 psa_key_type_t *type,
172 psa_key_policy_t *policy );
173
174#ifdef __cplusplus
175}
176#endif
177
178#endif /* PSA_CRYPTO_STORAGE_H */