blob: 167b0db05869be4e30290d65fc5e5603d5e6f95a [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 *
59 * \param key Slot number of the key to be stored. This must be a
60 * valid slot for a key of the chosen type. This should be
61 * an occupied key slot with an unoccupied corresponding
62 * storage location.
63 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
64 * \param[in] policy The key policy to save.
65 * \param[in] data Buffer containing the key data.
66 * \param data_length The number of bytes that make up the key data.
67 *
68 * \retval PSA_SUCCESS
69 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
70 * \retval PSA_ERROR_STORAGE_FAILURE
71 */
72psa_status_t psa_save_persistent_key( const psa_key_slot_t key,
73 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 *
90 * \param key Slot number whose content is to be loaded. This
91 * must be an unoccupied key slot with an occupied
92 * corresponding storage location. The key slot
93 * lifetime must be set to persistent.
94 * \param[out] type On success, the key type (a \c PSA_KEY_TYPE_XXX
95 * value).
96 * \param[out] policy On success, the key's policy.
97 * \param[out] data Pointer to an allocated key data buffer on return.
98 * \param[out] data_length The number of bytes that make up the key data.
99 *
100 * \retval PSA_SUCCESS
101 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
102 * \retval PSA_ERROR_STORAGE_FAILURE
103 */
104psa_status_t psa_load_persistent_key( psa_key_slot_t key,
105 psa_key_type_t *type,
106 psa_key_policy_t *policy,
107 uint8_t **data,
108 size_t *data_length );
109
110/**
111 * \brief Remove persistent data for the given key slot number.
112 *
113 * \param key Slot number whose content is to be removed
114 * from persistent storage.
115 *
116 * \retval PSA_SUCCESS
117 * \retval PSA_ERROR_STORAGE_FAILURE
118 */
119psa_status_t psa_destroy_persistent_key( const psa_key_slot_t key );
120
121/**
122 * \brief Zeroizes and frees the given buffer.
123 *
124 * This function must be called at some point after psa_load_persistent_key()
125 * to zeroize and free the memory allocated to the buffer in that function.
126 *
127 * \param key_data Buffer for the key data.
128 * \param key_data_length Size of the key data buffer.
129 *
130 */
131void psa_free_persistent_key_data( uint8_t *key_data, size_t key_data_length );
132
133/**
134 * \brief Formats key data and metadata for persistent storage
135 *
136 * \param[in] data Buffer for the key data.
137 * \param data_length Length of the key data buffer.
138 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
139 * \param policy The key policy.
140 * \param[out] storage_data Output buffer for the formatted data.
141 *
142 */
143void psa_format_key_data_for_storage( const uint8_t *data,
144 const size_t data_length,
145 const psa_key_type_t type,
146 const psa_key_policy_t *policy,
147 uint8_t *storage_data );
148
149/**
150 * \brief Parses persistent storage data into key data and metadata
151 *
152 * \param[in] storage_data Buffer for the storage data.
153 * \param storage_data_length Length of the storage data buffer
154 * \param[out] key_data On output, pointer to a newly allocated buffer
155 * containing the key data. This must be freed
156 * using psa_free_persistent_key_data()
157 * \param[out] key_data_length Length of the key data buffer
158 * \param[out] type Key type (a \c PSA_KEY_TYPE_XXX value).
159 * \param[out] policy The key policy.
160 *
161 * \retval PSA_SUCCESS
162 * \retval PSA_ERROR_INSUFFICIENT_STORAGE
163 * \retval PSA_ERROR_INSUFFICIENT_MEMORY
164 * \retval PSA_ERROR_STORAGE_FAILURE
165 */
166psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data,
167 size_t storage_data_length,
168 uint8_t **key_data,
169 size_t *key_data_length,
170 psa_key_type_t *type,
171 psa_key_policy_t *policy );
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif /* PSA_CRYPTO_STORAGE_H */