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