update atrs list
[RRG-proxmark3.git] / common / mbedtls / psa_crypto_core.h
blob59fb49388ca3b336c2852366becf9627edbd1b2b
1 /*
2 * PSA crypto core internal interfaces
3 */
4 /*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 #ifndef PSA_CRYPTO_CORE_H
22 #define PSA_CRYPTO_CORE_H
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
30 #include "psa/crypto.h"
31 #include "psa/crypto_se_driver.h"
33 #include <mbedtls/md_internal.h>
35 /** The data structure representing a key slot, containing key material
36 * and metadata for one key.
38 typedef struct {
39 psa_core_key_attributes_t attr;
42 * Number of locks on the key slot held by the library.
44 * This counter is incremented by one each time a library function
45 * retrieves through one of the dedicated internal API a pointer to the
46 * key slot.
48 * This counter is decremented by one each time a library function stops
49 * accessing the key slot and states it by calling the
50 * psa_unlock_key_slot() API.
52 * This counter is used to prevent resetting the key slot while the library
53 * may access it. For example, such control is needed in the following
54 * scenarios:
55 * . In case of key slot starvation, all key slots contain the description
56 * of a key, and the library asks for the description of a persistent
57 * key not present in the key slots, the key slots currently accessed by
58 * the library cannot be reclaimed to free a key slot to load the
59 * persistent key.
60 * . In case of a multi-threaded application where one thread asks to close
61 * or purge or destroy a key while it is in used by the library through
62 * another thread.
64 size_t lock_count;
66 /* Dynamically allocated key data buffer.
67 * Format as specified in psa_export_key(). */
68 struct key_data {
69 uint8_t *data;
70 size_t bytes;
71 } key;
72 } psa_key_slot_t;
74 /* A mask of key attribute flags used only internally.
75 * Currently there aren't any. */
76 #define PSA_KA_MASK_INTERNAL_ONLY ( \
77 0 )
79 /** Test whether a key slot is occupied.
81 * A key slot is occupied iff the key type is nonzero. This works because
82 * no valid key can have 0 as its key type.
84 * \param[in] slot The key slot to test.
86 * \return 1 if the slot is occupied, 0 otherwise.
88 static inline int psa_is_key_slot_occupied(const psa_key_slot_t *slot) {
89 return (slot->attr.type != 0);
92 /** Test whether a key slot is locked.
94 * A key slot is locked iff its lock counter is strictly greater than 0.
96 * \param[in] slot The key slot to test.
98 * \return 1 if the slot is locked, 0 otherwise.
100 static inline int psa_is_key_slot_locked(const psa_key_slot_t *slot) {
101 return (slot->lock_count > 0);
104 /** Retrieve flags from psa_key_slot_t::attr::core::flags.
106 * \param[in] slot The key slot to query.
107 * \param mask The mask of bits to extract.
109 * \return The key attribute flags in the given slot,
110 * bitwise-anded with \p mask.
112 static inline uint16_t psa_key_slot_get_flags(const psa_key_slot_t *slot,
113 uint16_t mask) {
114 return (slot->attr.flags & mask);
117 /** Set flags in psa_key_slot_t::attr::core::flags.
119 * \param[in,out] slot The key slot to modify.
120 * \param mask The mask of bits to modify.
121 * \param value The new value of the selected bits.
123 static inline void psa_key_slot_set_flags(psa_key_slot_t *slot,
124 uint16_t mask,
125 uint16_t value) {
126 slot->attr.flags = ((~mask & slot->attr.flags) |
127 (mask & value));
130 /** Turn on flags in psa_key_slot_t::attr::core::flags.
132 * \param[in,out] slot The key slot to modify.
133 * \param mask The mask of bits to set.
135 static inline void psa_key_slot_set_bits_in_flags(psa_key_slot_t *slot,
136 uint16_t mask) {
137 slot->attr.flags |= mask;
140 /** Turn off flags in psa_key_slot_t::attr::core::flags.
142 * \param[in,out] slot The key slot to modify.
143 * \param mask The mask of bits to clear.
145 static inline void psa_key_slot_clear_bits(psa_key_slot_t *slot,
146 uint16_t mask) {
147 slot->attr.flags &= ~mask;
150 #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
151 /** Get the SE slot number of a key from the key slot storing its description.
153 * \param[in] slot The key slot to query. This must be a key slot storing
154 * the description of a key of a dynamically registered
155 * secure element, otherwise the behaviour is undefined.
157 static inline psa_key_slot_number_t psa_key_slot_get_slot_number(
158 const psa_key_slot_t *slot) {
159 return (*((psa_key_slot_number_t *)(slot->key.data)));
161 #endif
163 /** Completely wipe a slot in memory, including its policy.
165 * Persistent storage is not affected.
167 * \param[in,out] slot The key slot to wipe.
169 * \retval #PSA_SUCCESS
170 * Success. This includes the case of a key slot that was
171 * already fully wiped.
172 * \retval #PSA_ERROR_CORRUPTION_DETECTED
174 psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot);
176 /** Copy key data (in export format) into an empty key slot.
178 * This function assumes that the slot does not contain
179 * any key material yet. On failure, the slot content is unchanged.
181 * \param[in,out] slot Key slot to copy the key into.
182 * \param[in] data Buffer containing the key material.
183 * \param data_length Size of the key buffer.
185 * \retval #PSA_SUCCESS
186 * The key has been copied successfully.
187 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
188 * Not enough memory was available for allocation of the
189 * copy buffer.
190 * \retval #PSA_ERROR_ALREADY_EXISTS
191 * There was other key material already present in the slot.
193 psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
194 const uint8_t *data,
195 size_t data_length);
197 /** Convert an mbed TLS error code to a PSA error code
199 * \note This function is provided solely for the convenience of
200 * Mbed TLS and may be removed at any time without notice.
202 * \param ret An mbed TLS-thrown error code
204 * \return The corresponding PSA error code
206 psa_status_t mbedtls_to_psa_error(int ret);
208 /** Get Mbed TLS MD information of a hash algorithm given its PSA identifier
210 * \param[in] alg PSA hash algorithm identifier
212 * \return The Mbed TLS MD information of the hash algorithm. \c NULL if the
213 * PSA hash algorithm is not supported.
215 const mbedtls_md_info_t *mbedtls_md_info_from_psa(psa_algorithm_t alg);
217 /** Import a key in binary format.
219 * \note The signature of this function is that of a PSA driver
220 * import_key entry point. This function behaves as an import_key
221 * entry point as defined in the PSA driver interface specification for
222 * transparent drivers.
224 * \param[in] attributes The attributes for the key to import.
225 * \param[in] data The buffer containing the key data in import
226 * format.
227 * \param[in] data_length Size of the \p data buffer in bytes.
228 * \param[out] key_buffer The buffer to contain the key data in output
229 * format upon successful return.
230 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
231 * size is greater or equal to \p data_length.
232 * \param[out] key_buffer_length The length of the data written in \p
233 * key_buffer in bytes.
234 * \param[out] bits The key size in number of bits.
236 * \retval #PSA_SUCCESS The key was imported successfully.
237 * \retval #PSA_ERROR_INVALID_ARGUMENT
238 * The key data is not correctly formatted.
239 * \retval #PSA_ERROR_NOT_SUPPORTED
240 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
241 * \retval #PSA_ERROR_CORRUPTION_DETECTED
243 psa_status_t psa_import_key_into_slot(
244 const psa_key_attributes_t *attributes,
245 const uint8_t *data, size_t data_length,
246 uint8_t *key_buffer, size_t key_buffer_size,
247 size_t *key_buffer_length, size_t *bits);
249 /** Export a key in binary format
251 * \note The signature of this function is that of a PSA driver export_key
252 * entry point. This function behaves as an export_key entry point as
253 * defined in the PSA driver interface specification.
255 * \param[in] attributes The attributes for the key to export.
256 * \param[in] key_buffer Material or context of the key to export.
257 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
258 * \param[out] data Buffer where the key data is to be written.
259 * \param[in] data_size Size of the \p data buffer in bytes.
260 * \param[out] data_length On success, the number of bytes written in
261 * \p data
263 * \retval #PSA_SUCCESS The key was exported successfully.
264 * \retval #PSA_ERROR_NOT_SUPPORTED
265 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
266 * \retval #PSA_ERROR_HARDWARE_FAILURE
267 * \retval #PSA_ERROR_CORRUPTION_DETECTED
268 * \retval #PSA_ERROR_STORAGE_FAILURE
269 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
271 psa_status_t psa_export_key_internal(
272 const psa_key_attributes_t *attributes,
273 const uint8_t *key_buffer, size_t key_buffer_size,
274 uint8_t *data, size_t data_size, size_t *data_length);
276 /** Export a public key or the public part of a key pair in binary format.
278 * \note The signature of this function is that of a PSA driver
279 * export_public_key entry point. This function behaves as an
280 * export_public_key entry point as defined in the PSA driver interface
281 * specification.
283 * \param[in] attributes The attributes for the key to export.
284 * \param[in] key_buffer Material or context of the key to export.
285 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
286 * \param[out] data Buffer where the key data is to be written.
287 * \param[in] data_size Size of the \p data buffer in bytes.
288 * \param[out] data_length On success, the number of bytes written in
289 * \p data
291 * \retval #PSA_SUCCESS The public key was exported successfully.
292 * \retval #PSA_ERROR_NOT_SUPPORTED
293 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
294 * \retval #PSA_ERROR_HARDWARE_FAILURE
295 * \retval #PSA_ERROR_CORRUPTION_DETECTED
296 * \retval #PSA_ERROR_STORAGE_FAILURE
297 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
299 psa_status_t psa_export_public_key_internal(
300 const psa_key_attributes_t *attributes,
301 const uint8_t *key_buffer, size_t key_buffer_size,
302 uint8_t *data, size_t data_size, size_t *data_length);
305 * \brief Generate a key.
307 * \note The signature of the function is that of a PSA driver generate_key
308 * entry point.
310 * \param[in] attributes The attributes for the key to generate.
311 * \param[out] key_buffer Buffer where the key data is to be written.
312 * \param[in] key_buffer_size Size of \p key_buffer in bytes.
313 * \param[out] key_buffer_length On success, the number of bytes written in
314 * \p key_buffer.
316 * \retval #PSA_SUCCESS
317 * The key was generated successfully.
318 * \retval #PSA_ERROR_INVALID_ARGUMENT
319 * \retval #PSA_ERROR_NOT_SUPPORTED
320 * Key size in bits or type not supported.
321 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
322 * The size of \p key_buffer is too small.
324 psa_status_t psa_generate_key_internal(const psa_key_attributes_t *attributes,
325 uint8_t *key_buffer,
326 size_t key_buffer_size,
327 size_t *key_buffer_length);
329 /** Sign an already-calculated hash with a private key.
331 * \note The signature of this function is that of a PSA driver
332 * sign_hash entry point. This function behaves as a sign_hash
333 * entry point as defined in the PSA driver interface specification for
334 * transparent drivers.
336 * \param[in] attributes The attributes of the key to use for the
337 * operation.
338 * \param[in] key_buffer The buffer containing the key context.
339 * format.
340 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
341 * \param[in] alg A signature algorithm that is compatible with
342 * the type of the key.
343 * \param[in] hash The hash or message to sign.
344 * \param[in] hash_length Size of the \p hash buffer in bytes.
345 * \param[out] signature Buffer where the signature is to be written.
346 * \param[in] signature_size Size of the \p signature buffer in bytes.
347 * \param[out] signature_length On success, the number of bytes
348 * that make up the returned signature value.
350 * \retval #PSA_SUCCESS
351 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
352 * The size of the \p signature buffer is too small. You can
353 * determine a sufficient buffer size by calling
354 * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
355 * where \c key_type and \c key_bits are the type and bit-size
356 * respectively of the key.
357 * \retval #PSA_ERROR_NOT_SUPPORTED
358 * \retval #PSA_ERROR_INVALID_ARGUMENT
359 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
360 * \retval #PSA_ERROR_CORRUPTION_DETECTED
361 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
363 psa_status_t psa_sign_hash_internal(
364 const psa_key_attributes_t *attributes,
365 const uint8_t *key_buffer, size_t key_buffer_size,
366 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
367 uint8_t *signature, size_t signature_size, size_t *signature_length);
370 * \brief Verify the signature a hash or short message using a public key.
372 * \note The signature of this function is that of a PSA driver
373 * verify_hash entry point. This function behaves as a verify_hash
374 * entry point as defined in the PSA driver interface specification for
375 * transparent drivers.
377 * \param[in] attributes The attributes of the key to use for the
378 * operation.
379 * \param[in] key_buffer The buffer containing the key context.
380 * format.
381 * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
382 * \param[in] alg A signature algorithm that is compatible with
383 * the type of the key.
384 * \param[in] hash The hash or message whose signature is to be
385 * verified.
386 * \param[in] hash_length Size of the \p hash buffer in bytes.
387 * \param[in] signature Buffer containing the signature to verify.
388 * \param[in] signature_length Size of the \p signature buffer in bytes.
390 * \retval #PSA_SUCCESS
391 * The signature is valid.
392 * \retval #PSA_ERROR_INVALID_SIGNATURE
393 * The calculation was performed successfully, but the passed
394 * signature is not a valid signature.
395 * \retval #PSA_ERROR_NOT_SUPPORTED
396 * \retval #PSA_ERROR_INVALID_ARGUMENT
397 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
399 psa_status_t psa_verify_hash_internal(
400 const psa_key_attributes_t *attributes,
401 const uint8_t *key_buffer, size_t key_buffer_size,
402 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
403 const uint8_t *signature, size_t signature_length);
405 #endif /* PSA_CRYPTO_CORE_H */