Mbed OS Reference
Loading...
Searching...
No Matches
crypto_compat.h
Go to the documentation of this file.
1/**
2 * \file
3 *
4 * \brief PSA cryptography module: Backward compatibility aliases
5 *
6 * This header declares alternative names for macro and functions.
7 * New application code should not use these names.
8 * These names may be removed in a future version of Mbed Crypto.
9 *
10 * \note This file may not be included directly. Applications must
11 * include psa/crypto.h.
12 */
13/*
14 * Copyright The Mbed TLS Contributors
15 * SPDX-License-Identifier: Apache-2.0
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 */
29
30#ifndef PSA_CRYPTO_COMPAT_H
31#define PSA_CRYPTO_COMPAT_H
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/*
38 * To support both openless APIs and psa_open_key() temporarily, define
39 * psa_key_handle_t to be equal to mbedtls_svc_key_id_t. Do not mark the
40 * type and its utility macros and functions deprecated yet. This will be done
41 * in a subsequent phase.
42 */
43typedef mbedtls_svc_key_id_t psa_key_handle_t;
44
45#define PSA_KEY_HANDLE_INIT MBEDTLS_SVC_KEY_ID_INIT
46
47/** Check wether an handle is null.
48 *
49 * \param handle Handle
50 *
51 * \return Non-zero if the handle is null, zero otherwise.
52 */
53static inline int psa_key_handle_is_null( psa_key_handle_t handle )
54{
55 return( mbedtls_svc_key_id_is_null( handle ) );
56}
57
58#if !defined(MBEDTLS_DEPRECATED_REMOVED)
59
60/*
61 * Mechanism for declaring deprecated values
62 */
63#if defined(MBEDTLS_DEPRECATED_WARNING) && !defined(MBEDTLS_PSA_DEPRECATED)
64#define MBEDTLS_PSA_DEPRECATED __attribute__((deprecated))
65#else
66#define MBEDTLS_PSA_DEPRECATED
67#endif
68
69typedef MBEDTLS_PSA_DEPRECATED size_t mbedtls_deprecated_size_t;
70typedef MBEDTLS_PSA_DEPRECATED psa_status_t mbedtls_deprecated_psa_status_t;
71typedef MBEDTLS_PSA_DEPRECATED psa_key_usage_t mbedtls_deprecated_psa_key_usage_t;
72typedef MBEDTLS_PSA_DEPRECATED psa_ecc_family_t mbedtls_deprecated_psa_ecc_family_t;
73typedef MBEDTLS_PSA_DEPRECATED psa_dh_family_t mbedtls_deprecated_psa_dh_family_t;
74typedef MBEDTLS_PSA_DEPRECATED psa_ecc_family_t psa_ecc_curve_t;
75typedef MBEDTLS_PSA_DEPRECATED psa_dh_family_t psa_dh_group_t;
76typedef MBEDTLS_PSA_DEPRECATED psa_algorithm_t mbedtls_deprecated_psa_algorithm_t;
77
78#define PSA_KEY_TYPE_GET_CURVE PSA_KEY_TYPE_ECC_GET_FAMILY
79#define PSA_KEY_TYPE_GET_GROUP PSA_KEY_TYPE_DH_GET_FAMILY
80
81#define MBEDTLS_DEPRECATED_CONSTANT( type, value ) \
82 ( (mbedtls_deprecated_##type) ( value ) )
83
84/*
85 * Deprecated PSA Crypto error code definitions (PSA Crypto API <= 1.0 beta2)
86 */
87/**
88 * \deprecated Replaced by #PSA_ERROR_GENERIC_ERROR
89 */
90#define PSA_ERROR_UNKNOWN_ERROR \
91 MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_GENERIC_ERROR )
92/**
93 * \deprecated Replaced by #PSA_ERROR_ALREADY_EXISTS
94 */
95#define PSA_ERROR_OCCUPIED_SLOT \
96 MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_ALREADY_EXISTS )
97/**
98 * \deprecated Replaced by #PSA_ERROR_DOES_NOT_EXIST
99 */
100#define PSA_ERROR_EMPTY_SLOT \
101 MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_DOES_NOT_EXIST )
102/**
103 * \deprecated Replaced by #PSA_ERROR_INSUFFICIENT_DATA
104 */
105#define PSA_ERROR_INSUFFICIENT_CAPACITY \
106 MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_INSUFFICIENT_DATA )
107/**
108 * \deprecated Replaced by #PSA_ERROR_CORRUPTION_DETECTED
109 */
110#define PSA_ERROR_TAMPERING_DETECTED \
111 MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_CORRUPTION_DETECTED )
112
113/*
114 * Deprecated PSA Crypto numerical encodings (PSA Crypto API <= 1.0 beta3)
115 */
116#define PSA_KEY_USAGE_SIGN \
117 MBEDTLS_DEPRECATED_CONSTANT( psa_key_usage_t, PSA_KEY_USAGE_SIGN_HASH )
118#define PSA_KEY_USAGE_VERIFY \
119 MBEDTLS_DEPRECATED_CONSTANT( psa_key_usage_t, PSA_KEY_USAGE_VERIFY_HASH )
120
121/*
122 * Deprecated PSA Crypto size calculation macros (PSA Crypto API <= 1.0 beta3)
123 */
124#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
125 MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_SIGNATURE_MAX_SIZE )
126#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ) \
127 MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ) )
128
129/*
130 * Deprecated PSA Crypto function names (PSA Crypto API <= 1.0 beta3)
131 */
132MBEDTLS_PSA_DEPRECATED static inline psa_status_t psa_asymmetric_sign( psa_key_handle_t key,
133 psa_algorithm_t alg,
134 const uint8_t *hash,
135 size_t hash_length,
136 uint8_t *signature,
137 size_t signature_size,
138 size_t *signature_length )
139{
140 return psa_sign_hash( key, alg, hash, hash_length, signature, signature_size, signature_length );
141}
142
143MBEDTLS_PSA_DEPRECATED static inline psa_status_t psa_asymmetric_verify( psa_key_handle_t key,
144 psa_algorithm_t alg,
145 const uint8_t *hash,
146 size_t hash_length,
147 const uint8_t *signature,
148 size_t signature_length )
149{
150 return psa_verify_hash( key, alg, hash, hash_length, signature, signature_length );
151}
152
153/*
154 * Size-specific elliptic curve families.
155 */
156#define PSA_ECC_CURVE_SECP160K1 \
157 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
158#define PSA_ECC_CURVE_SECP192K1 \
159 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
160#define PSA_ECC_CURVE_SECP224K1 \
161 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
162#define PSA_ECC_CURVE_SECP256K1 \
163 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
164#define PSA_ECC_CURVE_SECP160R1 \
165 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
166#define PSA_ECC_CURVE_SECP192R1 \
167 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
168#define PSA_ECC_CURVE_SECP224R1 \
169 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
170#define PSA_ECC_CURVE_SECP256R1 \
171 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
172#define PSA_ECC_CURVE_SECP384R1 \
173 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
174#define PSA_ECC_CURVE_SECP521R1 \
175 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
176#define PSA_ECC_CURVE_SECP160R2 \
177 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R2 )
178#define PSA_ECC_CURVE_SECT163K1 \
179 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
180#define PSA_ECC_CURVE_SECT233K1 \
181 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
182#define PSA_ECC_CURVE_SECT239K1 \
183 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
184#define PSA_ECC_CURVE_SECT283K1 \
185 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
186#define PSA_ECC_CURVE_SECT409K1 \
187 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
188#define PSA_ECC_CURVE_SECT571K1 \
189 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
190#define PSA_ECC_CURVE_SECT163R1 \
191 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
192#define PSA_ECC_CURVE_SECT193R1 \
193 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
194#define PSA_ECC_CURVE_SECT233R1 \
195 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
196#define PSA_ECC_CURVE_SECT283R1 \
197 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
198#define PSA_ECC_CURVE_SECT409R1 \
199 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
200#define PSA_ECC_CURVE_SECT571R1 \
201 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
202#define PSA_ECC_CURVE_SECT163R2 \
203 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R2 )
204#define PSA_ECC_CURVE_SECT193R2 \
205 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R2 )
206#define PSA_ECC_CURVE_BRAINPOOL_P256R1 \
207 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
208#define PSA_ECC_CURVE_BRAINPOOL_P384R1 \
209 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
210#define PSA_ECC_CURVE_BRAINPOOL_P512R1 \
211 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
212#define PSA_ECC_CURVE_CURVE25519 \
213 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_MONTGOMERY )
214#define PSA_ECC_CURVE_CURVE448 \
215 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_MONTGOMERY )
216
217/*
218 * Curves that changed name due to PSA specification.
219 */
220#define PSA_ECC_CURVE_SECP_K1 \
221 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
222#define PSA_ECC_CURVE_SECP_R1 \
223 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
224#define PSA_ECC_CURVE_SECP_R2 \
225 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R2 )
226#define PSA_ECC_CURVE_SECT_K1 \
227 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
228#define PSA_ECC_CURVE_SECT_R1 \
229 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
230#define PSA_ECC_CURVE_SECT_R2 \
231 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R2 )
232#define PSA_ECC_CURVE_BRAINPOOL_P_R1 \
233 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
234#define PSA_ECC_CURVE_MONTGOMERY \
235 MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_MONTGOMERY )
236
237/*
238 * Finite-field Diffie-Hellman families.
239 */
240#define PSA_DH_GROUP_FFDHE2048 \
241 MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
242#define PSA_DH_GROUP_FFDHE3072 \
243 MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
244#define PSA_DH_GROUP_FFDHE4096 \
245 MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
246#define PSA_DH_GROUP_FFDHE6144 \
247 MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
248#define PSA_DH_GROUP_FFDHE8192 \
249 MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
250
251/*
252 * Diffie-Hellman families that changed name due to PSA specification.
253 */
254#define PSA_DH_GROUP_RFC7919 \
255 MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
256#define PSA_DH_GROUP_CUSTOM \
257 MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_CUSTOM )
258
259/*
260 * Deprecated PSA Crypto stream cipher algorithms (PSA Crypto API <= 1.0 beta3)
261 */
262#define PSA_ALG_ARC4 \
263 MBEDTLS_DEPRECATED_CONSTANT( psa_algorithm_t, PSA_ALG_STREAM_CIPHER )
264#define PSA_ALG_CHACHA20 \
265 MBEDTLS_DEPRECATED_CONSTANT( psa_algorithm_t, PSA_ALG_STREAM_CIPHER )
266
267#endif /* MBEDTLS_DEPRECATED_REMOVED */
268
269/** Open a handle to an existing persistent key.
270 *
271 * Open a handle to a persistent key. A key is persistent if it was created
272 * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
273 * always has a nonzero key identifier, set with psa_set_key_id() when
274 * creating the key. Implementations may provide additional pre-provisioned
275 * keys that can be opened with psa_open_key(). Such keys have an application
276 * key identifier in the vendor range, as documented in the description of
277 * #psa_key_id_t.
278 *
279 * The application must eventually close the handle with psa_close_key() or
280 * psa_destroy_key() to release associated resources. If the application dies
281 * without calling one of these functions, the implementation should perform
282 * the equivalent of a call to psa_close_key().
283 *
284 * Some implementations permit an application to open the same key multiple
285 * times. If this is successful, each call to psa_open_key() will return a
286 * different key handle.
287 *
288 * \note This API is not part of the PSA Cryptography API Release 1.0.0
289 * specification. It was defined in the 1.0 Beta 3 version of the
290 * specification but was removed in the 1.0.0 released version. This API is
291 * kept for the time being to not break applications relying on it. It is not
292 * deprecated yet but will be in the near future.
293 *
294 * \note Applications that rely on opening a key multiple times will not be
295 * portable to implementations that only permit a single key handle to be
296 * opened. See also :ref:\`key-handles\`.
297 *
298 *
299 * \param key The persistent identifier of the key.
300 * \param[out] handle On success, a handle to the key.
301 *
302 * \retval #PSA_SUCCESS
303 * Success. The application can now use the value of `*handle`
304 * to access the key.
305 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
306 * The implementation does not have sufficient resources to open the
307 * key. This can be due to reaching an implementation limit on the
308 * number of open keys, the number of open key handles, or available
309 * memory.
310 * \retval #PSA_ERROR_DOES_NOT_EXIST
311 * There is no persistent key with key identifier \p id.
312 * \retval #PSA_ERROR_INVALID_ARGUMENT
313 * \p id is not a valid persistent key identifier.
314 * \retval #PSA_ERROR_NOT_PERMITTED
315 * The specified key exists, but the application does not have the
316 * permission to access it. Note that this specification does not
317 * define any way to create such a key, but it may be possible
318 * through implementation-specific means.
319 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
320 * \retval #PSA_ERROR_CORRUPTION_DETECTED
321 * \retval #PSA_ERROR_STORAGE_FAILURE
322 * \retval #PSA_ERROR_BAD_STATE
323 * The library has not been previously initialized by psa_crypto_init().
324 * It is implementation-dependent whether a failure to initialize
325 * results in this error code.
326 */
328 psa_key_handle_t *handle );
329
330/** Close a key handle.
331 *
332 * If the handle designates a volatile key, this will destroy the key material
333 * and free all associated resources, just like psa_destroy_key().
334 *
335 * If this is the last open handle to a persistent key, then closing the handle
336 * will free all resources associated with the key in volatile memory. The key
337 * data in persistent storage is not affected and can be opened again later
338 * with a call to psa_open_key().
339 *
340 * Closing the key handle makes the handle invalid, and the key handle
341 * must not be used again by the application.
342 *
343 * \note This API is not part of the PSA Cryptography API Release 1.0.0
344 * specification. It was defined in the 1.0 Beta 3 version of the
345 * specification but was removed in the 1.0.0 released version. This API is
346 * kept for the time being to not break applications relying on it. It is not
347 * deprecated yet but will be in the near future.
348 *
349 * \note If the key handle was used to set up an active
350 * :ref:\`multipart operation <multipart-operations>\`, then closing the
351 * key handle can cause the multipart operation to fail. Applications should
352 * maintain the key handle until after the multipart operation has finished.
353 *
354 * \param handle The key handle to close.
355 * If this is \c 0, do nothing and return \c PSA_SUCCESS.
356 *
357 * \retval #PSA_SUCCESS
358 * \p handle was a valid handle or \c 0. It is now closed.
359 * \retval #PSA_ERROR_INVALID_HANDLE
360 * \p handle is not a valid handle nor \c 0.
361 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
362 * \retval #PSA_ERROR_CORRUPTION_DETECTED
363 * \retval #PSA_ERROR_BAD_STATE
364 * The library has not been previously initialized by psa_crypto_init().
365 * It is implementation-dependent whether a failure to initialize
366 * results in this error code.
367 */
368psa_status_t psa_close_key(psa_key_handle_t handle);
369
370#ifdef __cplusplus
371}
372#endif
373
374#endif /* PSA_CRYPTO_COMPAT_H */
psa_status_t psa_open_key(mbedtls_svc_key_id_t key, psa_key_handle_t *handle)
Open a handle to an existing persistent key.
psa_status_t psa_close_key(psa_key_handle_t handle)
Close a key handle.
psa_status_t psa_sign_hash(mbedtls_svc_key_id_t key, psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, uint8_t *signature, size_t signature_size, size_t *signature_length)
Sign a hash or short message with a private key.
psa_status_t psa_verify_hash(mbedtls_svc_key_id_t key, psa_algorithm_t alg, const uint8_t *hash, size_t hash_length, const uint8_t *signature, size_t signature_length)
Verify the signature a hash or short message using a public key.
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:98
uint8_t psa_ecc_family_t
The type of PSA elliptic curve family identifiers.
Definition: crypto_types.h:77
uint8_t psa_dh_family_t
The type of PSA Diffie-Hellman group family identifiers.
Definition: crypto_types.h:88
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:55
psa_key_id_t mbedtls_svc_key_id_t
Identifier for persistent keys.
Definition: crypto_types.h:237
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:260