xref: /aosp_15_r20/external/mbedtls/include/psa/crypto_compat.h (revision 62c56f9862f102b96d72393aff6076c951fb8148)
1*62c56f98SSadaf Ebrahimi /**
2*62c56f98SSadaf Ebrahimi  * \file psa/crypto_compat.h
3*62c56f98SSadaf Ebrahimi  *
4*62c56f98SSadaf Ebrahimi  * \brief PSA cryptography module: Backward compatibility aliases
5*62c56f98SSadaf Ebrahimi  *
6*62c56f98SSadaf Ebrahimi  * This header declares alternative names for macro and functions.
7*62c56f98SSadaf Ebrahimi  * New application code should not use these names.
8*62c56f98SSadaf Ebrahimi  * These names may be removed in a future version of Mbed TLS.
9*62c56f98SSadaf Ebrahimi  *
10*62c56f98SSadaf Ebrahimi  * \note This file may not be included directly. Applications must
11*62c56f98SSadaf Ebrahimi  * include psa/crypto.h.
12*62c56f98SSadaf Ebrahimi  */
13*62c56f98SSadaf Ebrahimi /*
14*62c56f98SSadaf Ebrahimi  *  Copyright The Mbed TLS Contributors
15*62c56f98SSadaf Ebrahimi  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
16*62c56f98SSadaf Ebrahimi  */
17*62c56f98SSadaf Ebrahimi 
18*62c56f98SSadaf Ebrahimi #ifndef PSA_CRYPTO_COMPAT_H
19*62c56f98SSadaf Ebrahimi #define PSA_CRYPTO_COMPAT_H
20*62c56f98SSadaf Ebrahimi 
21*62c56f98SSadaf Ebrahimi #ifdef __cplusplus
22*62c56f98SSadaf Ebrahimi extern "C" {
23*62c56f98SSadaf Ebrahimi #endif
24*62c56f98SSadaf Ebrahimi 
25*62c56f98SSadaf Ebrahimi /*
26*62c56f98SSadaf Ebrahimi  * To support both openless APIs and psa_open_key() temporarily, define
27*62c56f98SSadaf Ebrahimi  * psa_key_handle_t to be equal to mbedtls_svc_key_id_t. Do not mark the
28*62c56f98SSadaf Ebrahimi  * type and its utility macros and functions deprecated yet. This will be done
29*62c56f98SSadaf Ebrahimi  * in a subsequent phase.
30*62c56f98SSadaf Ebrahimi  */
31*62c56f98SSadaf Ebrahimi typedef mbedtls_svc_key_id_t psa_key_handle_t;
32*62c56f98SSadaf Ebrahimi 
33*62c56f98SSadaf Ebrahimi #define PSA_KEY_HANDLE_INIT MBEDTLS_SVC_KEY_ID_INIT
34*62c56f98SSadaf Ebrahimi 
35*62c56f98SSadaf Ebrahimi /** Check whether a handle is null.
36*62c56f98SSadaf Ebrahimi  *
37*62c56f98SSadaf Ebrahimi  * \param handle  Handle
38*62c56f98SSadaf Ebrahimi  *
39*62c56f98SSadaf Ebrahimi  * \return Non-zero if the handle is null, zero otherwise.
40*62c56f98SSadaf Ebrahimi  */
psa_key_handle_is_null(psa_key_handle_t handle)41*62c56f98SSadaf Ebrahimi static inline int psa_key_handle_is_null(psa_key_handle_t handle)
42*62c56f98SSadaf Ebrahimi {
43*62c56f98SSadaf Ebrahimi     return mbedtls_svc_key_id_is_null(handle);
44*62c56f98SSadaf Ebrahimi }
45*62c56f98SSadaf Ebrahimi 
46*62c56f98SSadaf Ebrahimi /** Open a handle to an existing persistent key.
47*62c56f98SSadaf Ebrahimi  *
48*62c56f98SSadaf Ebrahimi  * Open a handle to a persistent key. A key is persistent if it was created
49*62c56f98SSadaf Ebrahimi  * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
50*62c56f98SSadaf Ebrahimi  * always has a nonzero key identifier, set with psa_set_key_id() when
51*62c56f98SSadaf Ebrahimi  * creating the key. Implementations may provide additional pre-provisioned
52*62c56f98SSadaf Ebrahimi  * keys that can be opened with psa_open_key(). Such keys have an application
53*62c56f98SSadaf Ebrahimi  * key identifier in the vendor range, as documented in the description of
54*62c56f98SSadaf Ebrahimi  * #psa_key_id_t.
55*62c56f98SSadaf Ebrahimi  *
56*62c56f98SSadaf Ebrahimi  * The application must eventually close the handle with psa_close_key() or
57*62c56f98SSadaf Ebrahimi  * psa_destroy_key() to release associated resources. If the application dies
58*62c56f98SSadaf Ebrahimi  * without calling one of these functions, the implementation should perform
59*62c56f98SSadaf Ebrahimi  * the equivalent of a call to psa_close_key().
60*62c56f98SSadaf Ebrahimi  *
61*62c56f98SSadaf Ebrahimi  * Some implementations permit an application to open the same key multiple
62*62c56f98SSadaf Ebrahimi  * times. If this is successful, each call to psa_open_key() will return a
63*62c56f98SSadaf Ebrahimi  * different key handle.
64*62c56f98SSadaf Ebrahimi  *
65*62c56f98SSadaf Ebrahimi  * \note This API is not part of the PSA Cryptography API Release 1.0.0
66*62c56f98SSadaf Ebrahimi  * specification. It was defined in the 1.0 Beta 3 version of the
67*62c56f98SSadaf Ebrahimi  * specification but was removed in the 1.0.0 released version. This API is
68*62c56f98SSadaf Ebrahimi  * kept for the time being to not break applications relying on it. It is not
69*62c56f98SSadaf Ebrahimi  * deprecated yet but will be in the near future.
70*62c56f98SSadaf Ebrahimi  *
71*62c56f98SSadaf Ebrahimi  * \note Applications that rely on opening a key multiple times will not be
72*62c56f98SSadaf Ebrahimi  * portable to implementations that only permit a single key handle to be
73*62c56f98SSadaf Ebrahimi  * opened. See also :ref:\`key-handles\`.
74*62c56f98SSadaf Ebrahimi  *
75*62c56f98SSadaf Ebrahimi  *
76*62c56f98SSadaf Ebrahimi  * \param key           The persistent identifier of the key.
77*62c56f98SSadaf Ebrahimi  * \param[out] handle   On success, a handle to the key.
78*62c56f98SSadaf Ebrahimi  *
79*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
80*62c56f98SSadaf Ebrahimi  *         Success. The application can now use the value of `*handle`
81*62c56f98SSadaf Ebrahimi  *         to access the key.
82*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
83*62c56f98SSadaf Ebrahimi  *         The implementation does not have sufficient resources to open the
84*62c56f98SSadaf Ebrahimi  *         key. This can be due to reaching an implementation limit on the
85*62c56f98SSadaf Ebrahimi  *         number of open keys, the number of open key handles, or available
86*62c56f98SSadaf Ebrahimi  *         memory.
87*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DOES_NOT_EXIST
88*62c56f98SSadaf Ebrahimi  *         There is no persistent key with key identifier \p key.
89*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_ARGUMENT
90*62c56f98SSadaf Ebrahimi  *         \p key is not a valid persistent key identifier.
91*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_NOT_PERMITTED
92*62c56f98SSadaf Ebrahimi  *         The specified key exists, but the application does not have the
93*62c56f98SSadaf Ebrahimi  *         permission to access it. Note that this specification does not
94*62c56f98SSadaf Ebrahimi  *         define any way to create such a key, but it may be possible
95*62c56f98SSadaf Ebrahimi  *         through implementation-specific means.
96*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
97*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
98*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
99*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
100*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
101*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
102*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
103*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
104*62c56f98SSadaf Ebrahimi  *         results in this error code.
105*62c56f98SSadaf Ebrahimi  */
106*62c56f98SSadaf Ebrahimi psa_status_t psa_open_key(mbedtls_svc_key_id_t key,
107*62c56f98SSadaf Ebrahimi                           psa_key_handle_t *handle);
108*62c56f98SSadaf Ebrahimi 
109*62c56f98SSadaf Ebrahimi /** Close a key handle.
110*62c56f98SSadaf Ebrahimi  *
111*62c56f98SSadaf Ebrahimi  * If the handle designates a volatile key, this will destroy the key material
112*62c56f98SSadaf Ebrahimi  * and free all associated resources, just like psa_destroy_key().
113*62c56f98SSadaf Ebrahimi  *
114*62c56f98SSadaf Ebrahimi  * If this is the last open handle to a persistent key, then closing the handle
115*62c56f98SSadaf Ebrahimi  * will free all resources associated with the key in volatile memory. The key
116*62c56f98SSadaf Ebrahimi  * data in persistent storage is not affected and can be opened again later
117*62c56f98SSadaf Ebrahimi  * with a call to psa_open_key().
118*62c56f98SSadaf Ebrahimi  *
119*62c56f98SSadaf Ebrahimi  * Closing the key handle makes the handle invalid, and the key handle
120*62c56f98SSadaf Ebrahimi  * must not be used again by the application.
121*62c56f98SSadaf Ebrahimi  *
122*62c56f98SSadaf Ebrahimi  * \note This API is not part of the PSA Cryptography API Release 1.0.0
123*62c56f98SSadaf Ebrahimi  * specification. It was defined in the 1.0 Beta 3 version of the
124*62c56f98SSadaf Ebrahimi  * specification but was removed in the 1.0.0 released version. This API is
125*62c56f98SSadaf Ebrahimi  * kept for the time being to not break applications relying on it. It is not
126*62c56f98SSadaf Ebrahimi  * deprecated yet but will be in the near future.
127*62c56f98SSadaf Ebrahimi  *
128*62c56f98SSadaf Ebrahimi  * \note If the key handle was used to set up an active
129*62c56f98SSadaf Ebrahimi  * :ref:\`multipart operation <multipart-operations>\`, then closing the
130*62c56f98SSadaf Ebrahimi  * key handle can cause the multipart operation to fail. Applications should
131*62c56f98SSadaf Ebrahimi  * maintain the key handle until after the multipart operation has finished.
132*62c56f98SSadaf Ebrahimi  *
133*62c56f98SSadaf Ebrahimi  * \param handle        The key handle to close.
134*62c56f98SSadaf Ebrahimi  *                      If this is \c 0, do nothing and return \c PSA_SUCCESS.
135*62c56f98SSadaf Ebrahimi  *
136*62c56f98SSadaf Ebrahimi  * \retval #PSA_SUCCESS
137*62c56f98SSadaf Ebrahimi  *         \p handle was a valid handle or \c 0. It is now closed.
138*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_INVALID_HANDLE
139*62c56f98SSadaf Ebrahimi  *         \p handle is not a valid handle nor \c 0.
140*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
141*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
142*62c56f98SSadaf Ebrahimi  * \retval #PSA_ERROR_BAD_STATE
143*62c56f98SSadaf Ebrahimi  *         The library has not been previously initialized by psa_crypto_init().
144*62c56f98SSadaf Ebrahimi  *         It is implementation-dependent whether a failure to initialize
145*62c56f98SSadaf Ebrahimi  *         results in this error code.
146*62c56f98SSadaf Ebrahimi  */
147*62c56f98SSadaf Ebrahimi psa_status_t psa_close_key(psa_key_handle_t handle);
148*62c56f98SSadaf Ebrahimi 
149*62c56f98SSadaf Ebrahimi #ifdef __cplusplus
150*62c56f98SSadaf Ebrahimi }
151*62c56f98SSadaf Ebrahimi #endif
152*62c56f98SSadaf Ebrahimi 
153*62c56f98SSadaf Ebrahimi #endif /* PSA_CRYPTO_COMPAT_H */
154