xref: /optee_os/lib/libmbedtls/mbedtls/include/psa/crypto_extra.h (revision 32b3180828fa15a49ccc86ecb4be9d274c140c89)
1*32b31808SJens Wiklander /**
2*32b31808SJens Wiklander  * \file psa/crypto_extra.h
3*32b31808SJens Wiklander  *
4*32b31808SJens Wiklander  * \brief PSA cryptography module: Mbed TLS vendor extensions
5*32b31808SJens Wiklander  *
6*32b31808SJens Wiklander  * \note This file may not be included directly. Applications must
7*32b31808SJens Wiklander  * include psa/crypto.h.
8*32b31808SJens Wiklander  *
9*32b31808SJens Wiklander  * This file is reserved for vendor-specific definitions.
10*32b31808SJens Wiklander  */
11*32b31808SJens Wiklander /*
12*32b31808SJens Wiklander  *  Copyright The Mbed TLS Contributors
13*32b31808SJens Wiklander  *  SPDX-License-Identifier: Apache-2.0
14*32b31808SJens Wiklander  *
15*32b31808SJens Wiklander  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
16*32b31808SJens Wiklander  *  not use this file except in compliance with the License.
17*32b31808SJens Wiklander  *  You may obtain a copy of the License at
18*32b31808SJens Wiklander  *
19*32b31808SJens Wiklander  *  http://www.apache.org/licenses/LICENSE-2.0
20*32b31808SJens Wiklander  *
21*32b31808SJens Wiklander  *  Unless required by applicable law or agreed to in writing, software
22*32b31808SJens Wiklander  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23*32b31808SJens Wiklander  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24*32b31808SJens Wiklander  *  See the License for the specific language governing permissions and
25*32b31808SJens Wiklander  *  limitations under the License.
26*32b31808SJens Wiklander  */
27*32b31808SJens Wiklander 
28*32b31808SJens Wiklander #ifndef PSA_CRYPTO_EXTRA_H
29*32b31808SJens Wiklander #define PSA_CRYPTO_EXTRA_H
30*32b31808SJens Wiklander #include "mbedtls/private_access.h"
31*32b31808SJens Wiklander 
32*32b31808SJens Wiklander #include "mbedtls/platform_util.h"
33*32b31808SJens Wiklander 
34*32b31808SJens Wiklander #include "crypto_types.h"
35*32b31808SJens Wiklander #include "crypto_compat.h"
36*32b31808SJens Wiklander 
37*32b31808SJens Wiklander #ifdef __cplusplus
38*32b31808SJens Wiklander extern "C" {
39*32b31808SJens Wiklander #endif
40*32b31808SJens Wiklander 
41*32b31808SJens Wiklander /* UID for secure storage seed */
42*32b31808SJens Wiklander #define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
43*32b31808SJens Wiklander 
44*32b31808SJens Wiklander /* See mbedtls_config.h for definition */
45*32b31808SJens Wiklander #if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
46*32b31808SJens Wiklander #define MBEDTLS_PSA_KEY_SLOT_COUNT 32
47*32b31808SJens Wiklander #endif
48*32b31808SJens Wiklander 
49*32b31808SJens Wiklander /** \addtogroup attributes
50*32b31808SJens Wiklander  * @{
51*32b31808SJens Wiklander  */
52*32b31808SJens Wiklander 
53*32b31808SJens Wiklander /** \brief Declare the enrollment algorithm for a key.
54*32b31808SJens Wiklander  *
55*32b31808SJens Wiklander  * An operation on a key may indifferently use the algorithm set with
56*32b31808SJens Wiklander  * psa_set_key_algorithm() or with this function.
57*32b31808SJens Wiklander  *
58*32b31808SJens Wiklander  * \param[out] attributes       The attribute structure to write to.
59*32b31808SJens Wiklander  * \param alg2                  A second algorithm that the key may be used
60*32b31808SJens Wiklander  *                              for, in addition to the algorithm set with
61*32b31808SJens Wiklander  *                              psa_set_key_algorithm().
62*32b31808SJens Wiklander  *
63*32b31808SJens Wiklander  * \warning Setting an enrollment algorithm is not recommended, because
64*32b31808SJens Wiklander  *          using the same key with different algorithms can allow some
65*32b31808SJens Wiklander  *          attacks based on arithmetic relations between different
66*32b31808SJens Wiklander  *          computations made with the same key, or can escalate harmless
67*32b31808SJens Wiklander  *          side channels into exploitable ones. Use this function only
68*32b31808SJens Wiklander  *          if it is necessary to support a protocol for which it has been
69*32b31808SJens Wiklander  *          verified that the usage of the key with multiple algorithms
70*32b31808SJens Wiklander  *          is safe.
71*32b31808SJens Wiklander  */
72*32b31808SJens Wiklander static inline void psa_set_key_enrollment_algorithm(
73*32b31808SJens Wiklander     psa_key_attributes_t *attributes,
74*32b31808SJens Wiklander     psa_algorithm_t alg2)
75*32b31808SJens Wiklander {
76*32b31808SJens Wiklander     attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2) = alg2;
77*32b31808SJens Wiklander }
78*32b31808SJens Wiklander 
79*32b31808SJens Wiklander /** Retrieve the enrollment algorithm policy from key attributes.
80*32b31808SJens Wiklander  *
81*32b31808SJens Wiklander  * \param[in] attributes        The key attribute structure to query.
82*32b31808SJens Wiklander  *
83*32b31808SJens Wiklander  * \return The enrollment algorithm stored in the attribute structure.
84*32b31808SJens Wiklander  */
85*32b31808SJens Wiklander static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
86*32b31808SJens Wiklander     const psa_key_attributes_t *attributes)
87*32b31808SJens Wiklander {
88*32b31808SJens Wiklander     return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg2);
89*32b31808SJens Wiklander }
90*32b31808SJens Wiklander 
91*32b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
92*32b31808SJens Wiklander 
93*32b31808SJens Wiklander /** Retrieve the slot number where a key is stored.
94*32b31808SJens Wiklander  *
95*32b31808SJens Wiklander  * A slot number is only defined for keys that are stored in a secure
96*32b31808SJens Wiklander  * element.
97*32b31808SJens Wiklander  *
98*32b31808SJens Wiklander  * This information is only useful if the secure element is not entirely
99*32b31808SJens Wiklander  * managed through the PSA Cryptography API. It is up to the secure
100*32b31808SJens Wiklander  * element driver to decide how PSA slot numbers map to any other interface
101*32b31808SJens Wiklander  * that the secure element may have.
102*32b31808SJens Wiklander  *
103*32b31808SJens Wiklander  * \param[in] attributes        The key attribute structure to query.
104*32b31808SJens Wiklander  * \param[out] slot_number      On success, the slot number containing the key.
105*32b31808SJens Wiklander  *
106*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
107*32b31808SJens Wiklander  *         The key is located in a secure element, and \p *slot_number
108*32b31808SJens Wiklander  *         indicates the slot number that contains it.
109*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
110*32b31808SJens Wiklander  *         The caller is not permitted to query the slot number.
111*32b31808SJens Wiklander  *         Mbed Crypto currently does not return this error.
112*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
113*32b31808SJens Wiklander  *         The key is not located in a secure element.
114*32b31808SJens Wiklander  */
115*32b31808SJens Wiklander psa_status_t psa_get_key_slot_number(
116*32b31808SJens Wiklander     const psa_key_attributes_t *attributes,
117*32b31808SJens Wiklander     psa_key_slot_number_t *slot_number);
118*32b31808SJens Wiklander 
119*32b31808SJens Wiklander /** Choose the slot number where a key is stored.
120*32b31808SJens Wiklander  *
121*32b31808SJens Wiklander  * This function declares a slot number in the specified attribute
122*32b31808SJens Wiklander  * structure.
123*32b31808SJens Wiklander  *
124*32b31808SJens Wiklander  * A slot number is only meaningful for keys that are stored in a secure
125*32b31808SJens Wiklander  * element. It is up to the secure element driver to decide how PSA slot
126*32b31808SJens Wiklander  * numbers map to any other interface that the secure element may have.
127*32b31808SJens Wiklander  *
128*32b31808SJens Wiklander  * \note Setting a slot number in key attributes for a key creation can
129*32b31808SJens Wiklander  *       cause the following errors when creating the key:
130*32b31808SJens Wiklander  *       - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
131*32b31808SJens Wiklander  *         not support choosing a specific slot number.
132*32b31808SJens Wiklander  *       - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
133*32b31808SJens Wiklander  *         choose slot numbers in general or to choose this specific slot.
134*32b31808SJens Wiklander  *       - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
135*32b31808SJens Wiklander  *         valid in general or not valid for this specific key.
136*32b31808SJens Wiklander  *       - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
137*32b31808SJens Wiklander  *         selected slot.
138*32b31808SJens Wiklander  *
139*32b31808SJens Wiklander  * \param[out] attributes       The attribute structure to write to.
140*32b31808SJens Wiklander  * \param slot_number           The slot number to set.
141*32b31808SJens Wiklander  */
142*32b31808SJens Wiklander static inline void psa_set_key_slot_number(
143*32b31808SJens Wiklander     psa_key_attributes_t *attributes,
144*32b31808SJens Wiklander     psa_key_slot_number_t slot_number)
145*32b31808SJens Wiklander {
146*32b31808SJens Wiklander     attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
147*32b31808SJens Wiklander     attributes->MBEDTLS_PRIVATE(slot_number) = slot_number;
148*32b31808SJens Wiklander }
149*32b31808SJens Wiklander 
150*32b31808SJens Wiklander /** Remove the slot number attribute from a key attribute structure.
151*32b31808SJens Wiklander  *
152*32b31808SJens Wiklander  * This function undoes the action of psa_set_key_slot_number().
153*32b31808SJens Wiklander  *
154*32b31808SJens Wiklander  * \param[out] attributes       The attribute structure to write to.
155*32b31808SJens Wiklander  */
156*32b31808SJens Wiklander static inline void psa_clear_key_slot_number(
157*32b31808SJens Wiklander     psa_key_attributes_t *attributes)
158*32b31808SJens Wiklander {
159*32b31808SJens Wiklander     attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(flags) &=
160*32b31808SJens Wiklander         ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
161*32b31808SJens Wiklander }
162*32b31808SJens Wiklander 
163*32b31808SJens Wiklander /** Register a key that is already present in a secure element.
164*32b31808SJens Wiklander  *
165*32b31808SJens Wiklander  * The key must be located in a secure element designated by the
166*32b31808SJens Wiklander  * lifetime field in \p attributes, in the slot set with
167*32b31808SJens Wiklander  * psa_set_key_slot_number() in the attribute structure.
168*32b31808SJens Wiklander  * This function makes the key available through the key identifier
169*32b31808SJens Wiklander  * specified in \p attributes.
170*32b31808SJens Wiklander  *
171*32b31808SJens Wiklander  * \param[in] attributes        The attributes of the existing key.
172*32b31808SJens Wiklander  *
173*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
174*32b31808SJens Wiklander  *         The key was successfully registered.
175*32b31808SJens Wiklander  *         Note that depending on the design of the driver, this may or may
176*32b31808SJens Wiklander  *         not guarantee that a key actually exists in the designated slot
177*32b31808SJens Wiklander  *         and is compatible with the specified attributes.
178*32b31808SJens Wiklander  * \retval #PSA_ERROR_ALREADY_EXISTS
179*32b31808SJens Wiklander  *         There is already a key with the identifier specified in
180*32b31808SJens Wiklander  *         \p attributes.
181*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
182*32b31808SJens Wiklander  *         The secure element driver for the specified lifetime does not
183*32b31808SJens Wiklander  *         support registering a key.
184*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
185*32b31808SJens Wiklander  *         The identifier in \p attributes is invalid, namely the identifier is
186*32b31808SJens Wiklander  *         not in the user range, or
187*32b31808SJens Wiklander  *         \p attributes specifies a lifetime which is not located
188*32b31808SJens Wiklander  *         in a secure element, or no slot number is specified in \p attributes,
189*32b31808SJens Wiklander  *         or the specified slot number is not valid.
190*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
191*32b31808SJens Wiklander  *         The caller is not authorized to register the specified key slot.
192*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
193*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
194*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
195*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
196*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
197*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
198*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
199*32b31808SJens Wiklander  *         The library has not been previously initialized by psa_crypto_init().
200*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
201*32b31808SJens Wiklander  *         results in this error code.
202*32b31808SJens Wiklander  */
203*32b31808SJens Wiklander psa_status_t mbedtls_psa_register_se_key(
204*32b31808SJens Wiklander     const psa_key_attributes_t *attributes);
205*32b31808SJens Wiklander 
206*32b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
207*32b31808SJens Wiklander 
208*32b31808SJens Wiklander /**@}*/
209*32b31808SJens Wiklander 
210*32b31808SJens Wiklander /**
211*32b31808SJens Wiklander  * \brief Library deinitialization.
212*32b31808SJens Wiklander  *
213*32b31808SJens Wiklander  * This function clears all data associated with the PSA layer,
214*32b31808SJens Wiklander  * including the whole key store.
215*32b31808SJens Wiklander  *
216*32b31808SJens Wiklander  * This is an Mbed TLS extension.
217*32b31808SJens Wiklander  */
218*32b31808SJens Wiklander void mbedtls_psa_crypto_free(void);
219*32b31808SJens Wiklander 
220*32b31808SJens Wiklander /** \brief Statistics about
221*32b31808SJens Wiklander  * resource consumption related to the PSA keystore.
222*32b31808SJens Wiklander  *
223*32b31808SJens Wiklander  * \note The content of this structure is not part of the stable API and ABI
224*32b31808SJens Wiklander  *       of Mbed Crypto and may change arbitrarily from version to version.
225*32b31808SJens Wiklander  */
226*32b31808SJens Wiklander typedef struct mbedtls_psa_stats_s {
227*32b31808SJens Wiklander     /** Number of slots containing key material for a volatile key. */
228*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(volatile_slots);
229*32b31808SJens Wiklander     /** Number of slots containing key material for a key which is in
230*32b31808SJens Wiklander      * internal persistent storage. */
231*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(persistent_slots);
232*32b31808SJens Wiklander     /** Number of slots containing a reference to a key in a
233*32b31808SJens Wiklander      * secure element. */
234*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(external_slots);
235*32b31808SJens Wiklander     /** Number of slots which are occupied, but do not contain
236*32b31808SJens Wiklander      * key material yet. */
237*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(half_filled_slots);
238*32b31808SJens Wiklander     /** Number of slots that contain cache data. */
239*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(cache_slots);
240*32b31808SJens Wiklander     /** Number of slots that are not used for anything. */
241*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(empty_slots);
242*32b31808SJens Wiklander     /** Number of slots that are locked. */
243*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(locked_slots);
244*32b31808SJens Wiklander     /** Largest key id value among open keys in internal persistent storage. */
245*32b31808SJens Wiklander     psa_key_id_t MBEDTLS_PRIVATE(max_open_internal_key_id);
246*32b31808SJens Wiklander     /** Largest key id value among open keys in secure elements. */
247*32b31808SJens Wiklander     psa_key_id_t MBEDTLS_PRIVATE(max_open_external_key_id);
248*32b31808SJens Wiklander } mbedtls_psa_stats_t;
249*32b31808SJens Wiklander 
250*32b31808SJens Wiklander /** \brief Get statistics about
251*32b31808SJens Wiklander  * resource consumption related to the PSA keystore.
252*32b31808SJens Wiklander  *
253*32b31808SJens Wiklander  * \note When Mbed Crypto is built as part of a service, with isolation
254*32b31808SJens Wiklander  *       between the application and the keystore, the service may or
255*32b31808SJens Wiklander  *       may not expose this function.
256*32b31808SJens Wiklander  */
257*32b31808SJens Wiklander void mbedtls_psa_get_stats(mbedtls_psa_stats_t *stats);
258*32b31808SJens Wiklander 
259*32b31808SJens Wiklander /**
260*32b31808SJens Wiklander  * \brief Inject an initial entropy seed for the random generator into
261*32b31808SJens Wiklander  *        secure storage.
262*32b31808SJens Wiklander  *
263*32b31808SJens Wiklander  * This function injects data to be used as a seed for the random generator
264*32b31808SJens Wiklander  * used by the PSA Crypto implementation. On devices that lack a trusted
265*32b31808SJens Wiklander  * entropy source (preferably a hardware random number generator),
266*32b31808SJens Wiklander  * the Mbed PSA Crypto implementation uses this value to seed its
267*32b31808SJens Wiklander  * random generator.
268*32b31808SJens Wiklander  *
269*32b31808SJens Wiklander  * On devices without a trusted entropy source, this function must be
270*32b31808SJens Wiklander  * called exactly once in the lifetime of the device. On devices with
271*32b31808SJens Wiklander  * a trusted entropy source, calling this function is optional.
272*32b31808SJens Wiklander  * In all cases, this function may only be called before calling any
273*32b31808SJens Wiklander  * other function in the PSA Crypto API, including psa_crypto_init().
274*32b31808SJens Wiklander  *
275*32b31808SJens Wiklander  * When this function returns successfully, it populates a file in
276*32b31808SJens Wiklander  * persistent storage. Once the file has been created, this function
277*32b31808SJens Wiklander  * can no longer succeed.
278*32b31808SJens Wiklander  *
279*32b31808SJens Wiklander  * If any error occurs, this function does not change the system state.
280*32b31808SJens Wiklander  * You can call this function again after correcting the reason for the
281*32b31808SJens Wiklander  * error if possible.
282*32b31808SJens Wiklander  *
283*32b31808SJens Wiklander  * \warning This function **can** fail! Callers MUST check the return status.
284*32b31808SJens Wiklander  *
285*32b31808SJens Wiklander  * \warning If you use this function, you should use it as part of a
286*32b31808SJens Wiklander  *          factory provisioning process. The value of the injected seed
287*32b31808SJens Wiklander  *          is critical to the security of the device. It must be
288*32b31808SJens Wiklander  *          *secret*, *unpredictable* and (statistically) *unique per device*.
289*32b31808SJens Wiklander  *          You should be generate it randomly using a cryptographically
290*32b31808SJens Wiklander  *          secure random generator seeded from trusted entropy sources.
291*32b31808SJens Wiklander  *          You should transmit it securely to the device and ensure
292*32b31808SJens Wiklander  *          that its value is not leaked or stored anywhere beyond the
293*32b31808SJens Wiklander  *          needs of transmitting it from the point of generation to
294*32b31808SJens Wiklander  *          the call of this function, and erase all copies of the value
295*32b31808SJens Wiklander  *          once this function returns.
296*32b31808SJens Wiklander  *
297*32b31808SJens Wiklander  * This is an Mbed TLS extension.
298*32b31808SJens Wiklander  *
299*32b31808SJens Wiklander  * \note This function is only available on the following platforms:
300*32b31808SJens Wiklander  * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
301*32b31808SJens Wiklander  *   Note that you must provide compatible implementations of
302*32b31808SJens Wiklander  *   mbedtls_nv_seed_read and mbedtls_nv_seed_write.
303*32b31808SJens Wiklander  * * In a client-server integration of PSA Cryptography, on the client side,
304*32b31808SJens Wiklander  *   if the server supports this feature.
305*32b31808SJens Wiklander  * \param[in] seed          Buffer containing the seed value to inject.
306*32b31808SJens Wiklander  * \param[in] seed_size     Size of the \p seed buffer.
307*32b31808SJens Wiklander  *                          The size of the seed in bytes must be greater
308*32b31808SJens Wiklander  *                          or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
309*32b31808SJens Wiklander  *                          and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
310*32b31808SJens Wiklander  *                          in `library/entropy_poll.h` in the Mbed TLS source
311*32b31808SJens Wiklander  *                          code.
312*32b31808SJens Wiklander  *                          It must be less or equal to
313*32b31808SJens Wiklander  *                          #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
314*32b31808SJens Wiklander  *
315*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
316*32b31808SJens Wiklander  *         The seed value was injected successfully. The random generator
317*32b31808SJens Wiklander  *         of the PSA Crypto implementation is now ready for use.
318*32b31808SJens Wiklander  *         You may now call psa_crypto_init() and use the PSA Crypto
319*32b31808SJens Wiklander  *         implementation.
320*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
321*32b31808SJens Wiklander  *         \p seed_size is out of range.
322*32b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE
323*32b31808SJens Wiklander  *         There was a failure reading or writing from storage.
324*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
325*32b31808SJens Wiklander  *         The library has already been initialized. It is no longer
326*32b31808SJens Wiklander  *         possible to call this function.
327*32b31808SJens Wiklander  */
328*32b31808SJens Wiklander psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
329*32b31808SJens Wiklander                                         size_t seed_size);
330*32b31808SJens Wiklander 
331*32b31808SJens Wiklander /** \addtogroup crypto_types
332*32b31808SJens Wiklander  * @{
333*32b31808SJens Wiklander  */
334*32b31808SJens Wiklander 
335*32b31808SJens Wiklander /** DSA public key.
336*32b31808SJens Wiklander  *
337*32b31808SJens Wiklander  * The import and export format is the
338*32b31808SJens Wiklander  * representation of the public key `y = g^x mod p` as a big-endian byte
339*32b31808SJens Wiklander  * string. The length of the byte string is the length of the base prime `p`
340*32b31808SJens Wiklander  * in bytes.
341*32b31808SJens Wiklander  */
342*32b31808SJens Wiklander #define PSA_KEY_TYPE_DSA_PUBLIC_KEY                 ((psa_key_type_t) 0x4002)
343*32b31808SJens Wiklander 
344*32b31808SJens Wiklander /** DSA key pair (private and public key).
345*32b31808SJens Wiklander  *
346*32b31808SJens Wiklander  * The import and export format is the
347*32b31808SJens Wiklander  * representation of the private key `x` as a big-endian byte string. The
348*32b31808SJens Wiklander  * length of the byte string is the private key size in bytes (leading zeroes
349*32b31808SJens Wiklander  * are not stripped).
350*32b31808SJens Wiklander  *
351*32b31808SJens Wiklander  * Deterministic DSA key derivation with psa_generate_derived_key follows
352*32b31808SJens Wiklander  * FIPS 186-4 §B.1.2: interpret the byte string as integer
353*32b31808SJens Wiklander  * in big-endian order. Discard it if it is not in the range
354*32b31808SJens Wiklander  * [0, *N* - 2] where *N* is the boundary of the private key domain
355*32b31808SJens Wiklander  * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
356*32b31808SJens Wiklander  * or the order of the curve's base point for ECC).
357*32b31808SJens Wiklander  * Add 1 to the resulting integer and use this as the private key *x*.
358*32b31808SJens Wiklander  *
359*32b31808SJens Wiklander  */
360*32b31808SJens Wiklander #define PSA_KEY_TYPE_DSA_KEY_PAIR                    ((psa_key_type_t) 0x7002)
361*32b31808SJens Wiklander 
362*32b31808SJens Wiklander /** Whether a key type is a DSA key (pair or public-only). */
363*32b31808SJens Wiklander #define PSA_KEY_TYPE_IS_DSA(type)                                       \
364*32b31808SJens Wiklander     (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
365*32b31808SJens Wiklander 
366*32b31808SJens Wiklander #define PSA_ALG_DSA_BASE                        ((psa_algorithm_t) 0x06000400)
367*32b31808SJens Wiklander /** DSA signature with hashing.
368*32b31808SJens Wiklander  *
369*32b31808SJens Wiklander  * This is the signature scheme defined by FIPS 186-4,
370*32b31808SJens Wiklander  * with a random per-message secret number (*k*).
371*32b31808SJens Wiklander  *
372*32b31808SJens Wiklander  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
373*32b31808SJens Wiklander  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
374*32b31808SJens Wiklander  *                      This includes #PSA_ALG_ANY_HASH
375*32b31808SJens Wiklander  *                      when specifying the algorithm in a usage policy.
376*32b31808SJens Wiklander  *
377*32b31808SJens Wiklander  * \return              The corresponding DSA signature algorithm.
378*32b31808SJens Wiklander  * \return              Unspecified if \p hash_alg is not a supported
379*32b31808SJens Wiklander  *                      hash algorithm.
380*32b31808SJens Wiklander  */
381*32b31808SJens Wiklander #define PSA_ALG_DSA(hash_alg)                             \
382*32b31808SJens Wiklander     (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
383*32b31808SJens Wiklander #define PSA_ALG_DETERMINISTIC_DSA_BASE          ((psa_algorithm_t) 0x06000500)
384*32b31808SJens Wiklander #define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
385*32b31808SJens Wiklander /** Deterministic DSA signature with hashing.
386*32b31808SJens Wiklander  *
387*32b31808SJens Wiklander  * This is the deterministic variant defined by RFC 6979 of
388*32b31808SJens Wiklander  * the signature scheme defined by FIPS 186-4.
389*32b31808SJens Wiklander  *
390*32b31808SJens Wiklander  * \param hash_alg      A hash algorithm (\c PSA_ALG_XXX value such that
391*32b31808SJens Wiklander  *                      #PSA_ALG_IS_HASH(\p hash_alg) is true).
392*32b31808SJens Wiklander  *                      This includes #PSA_ALG_ANY_HASH
393*32b31808SJens Wiklander  *                      when specifying the algorithm in a usage policy.
394*32b31808SJens Wiklander  *
395*32b31808SJens Wiklander  * \return              The corresponding DSA signature algorithm.
396*32b31808SJens Wiklander  * \return              Unspecified if \p hash_alg is not a supported
397*32b31808SJens Wiklander  *                      hash algorithm.
398*32b31808SJens Wiklander  */
399*32b31808SJens Wiklander #define PSA_ALG_DETERMINISTIC_DSA(hash_alg)                             \
400*32b31808SJens Wiklander     (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
401*32b31808SJens Wiklander #define PSA_ALG_IS_DSA(alg)                                             \
402*32b31808SJens Wiklander     (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) ==  \
403*32b31808SJens Wiklander      PSA_ALG_DSA_BASE)
404*32b31808SJens Wiklander #define PSA_ALG_DSA_IS_DETERMINISTIC(alg)               \
405*32b31808SJens Wiklander     (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
406*32b31808SJens Wiklander #define PSA_ALG_IS_DETERMINISTIC_DSA(alg)                       \
407*32b31808SJens Wiklander     (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
408*32b31808SJens Wiklander #define PSA_ALG_IS_RANDOMIZED_DSA(alg)                          \
409*32b31808SJens Wiklander     (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
410*32b31808SJens Wiklander 
411*32b31808SJens Wiklander 
412*32b31808SJens Wiklander /* We need to expand the sample definition of this macro from
413*32b31808SJens Wiklander  * the API definition. */
414*32b31808SJens Wiklander #undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
415*32b31808SJens Wiklander #define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg)    \
416*32b31808SJens Wiklander     PSA_ALG_IS_DSA(alg)
417*32b31808SJens Wiklander 
418*32b31808SJens Wiklander /**@}*/
419*32b31808SJens Wiklander 
420*32b31808SJens Wiklander /** \addtogroup attributes
421*32b31808SJens Wiklander  * @{
422*32b31808SJens Wiklander  */
423*32b31808SJens Wiklander 
424*32b31808SJens Wiklander /** Custom Diffie-Hellman group.
425*32b31808SJens Wiklander  *
426*32b31808SJens Wiklander  * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
427*32b31808SJens Wiklander  * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
428*32b31808SJens Wiklander  * from domain parameters set by psa_set_key_domain_parameters().
429*32b31808SJens Wiklander  */
430*32b31808SJens Wiklander #define PSA_DH_FAMILY_CUSTOM             ((psa_dh_family_t) 0x7e)
431*32b31808SJens Wiklander 
432*32b31808SJens Wiklander /** PAKE operation stages. */
433*32b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_SETUP 0
434*32b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_COLLECT_INPUTS 1
435*32b31808SJens Wiklander #define PSA_PAKE_OPERATION_STAGE_COMPUTATION 2
436*32b31808SJens Wiklander 
437*32b31808SJens Wiklander /**
438*32b31808SJens Wiklander  * \brief Set domain parameters for a key.
439*32b31808SJens Wiklander  *
440*32b31808SJens Wiklander  * Some key types require additional domain parameters in addition to
441*32b31808SJens Wiklander  * the key type identifier and the key size. Use this function instead
442*32b31808SJens Wiklander  * of psa_set_key_type() when you need to specify domain parameters.
443*32b31808SJens Wiklander  *
444*32b31808SJens Wiklander  * The format for the required domain parameters varies based on the key type.
445*32b31808SJens Wiklander  *
446*32b31808SJens Wiklander  * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
447*32b31808SJens Wiklander  *   the domain parameter data consists of the public exponent,
448*32b31808SJens Wiklander  *   represented as a big-endian integer with no leading zeros.
449*32b31808SJens Wiklander  *   This information is used when generating an RSA key pair.
450*32b31808SJens Wiklander  *   When importing a key, the public exponent is read from the imported
451*32b31808SJens Wiklander  *   key data and the exponent recorded in the attribute structure is ignored.
452*32b31808SJens Wiklander  *   As an exception, the public exponent 65537 is represented by an empty
453*32b31808SJens Wiklander  *   byte string.
454*32b31808SJens Wiklander  * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
455*32b31808SJens Wiklander  *   the `Dss-Params` format as defined by RFC 3279 §2.3.2.
456*32b31808SJens Wiklander  *   ```
457*32b31808SJens Wiklander  *   Dss-Params ::= SEQUENCE  {
458*32b31808SJens Wiklander  *      p       INTEGER,
459*32b31808SJens Wiklander  *      q       INTEGER,
460*32b31808SJens Wiklander  *      g       INTEGER
461*32b31808SJens Wiklander  *   }
462*32b31808SJens Wiklander  *   ```
463*32b31808SJens Wiklander  * - For Diffie-Hellman key exchange keys
464*32b31808SJens Wiklander  *   (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
465*32b31808SJens Wiklander  *   #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
466*32b31808SJens Wiklander  *   `DomainParameters` format as defined by RFC 3279 §2.3.3.
467*32b31808SJens Wiklander  *   ```
468*32b31808SJens Wiklander  *   DomainParameters ::= SEQUENCE {
469*32b31808SJens Wiklander  *      p               INTEGER,                    -- odd prime, p=jq +1
470*32b31808SJens Wiklander  *      g               INTEGER,                    -- generator, g
471*32b31808SJens Wiklander  *      q               INTEGER,                    -- factor of p-1
472*32b31808SJens Wiklander  *      j               INTEGER OPTIONAL,           -- subgroup factor
473*32b31808SJens Wiklander  *      validationParams ValidationParams OPTIONAL
474*32b31808SJens Wiklander  *   }
475*32b31808SJens Wiklander  *   ValidationParams ::= SEQUENCE {
476*32b31808SJens Wiklander  *      seed            BIT STRING,
477*32b31808SJens Wiklander  *      pgenCounter     INTEGER
478*32b31808SJens Wiklander  *   }
479*32b31808SJens Wiklander  *   ```
480*32b31808SJens Wiklander  *
481*32b31808SJens Wiklander  * \note This function may allocate memory or other resources.
482*32b31808SJens Wiklander  *       Once you have called this function on an attribute structure,
483*32b31808SJens Wiklander  *       you must call psa_reset_key_attributes() to free these resources.
484*32b31808SJens Wiklander  *
485*32b31808SJens Wiklander  * \note This is an experimental extension to the interface. It may change
486*32b31808SJens Wiklander  *       in future versions of the library.
487*32b31808SJens Wiklander  *
488*32b31808SJens Wiklander  * \param[in,out] attributes    Attribute structure where the specified domain
489*32b31808SJens Wiklander  *                              parameters will be stored.
490*32b31808SJens Wiklander  *                              If this function fails, the content of
491*32b31808SJens Wiklander  *                              \p attributes is not modified.
492*32b31808SJens Wiklander  * \param type                  Key type (a \c PSA_KEY_TYPE_XXX value).
493*32b31808SJens Wiklander  * \param[in] data              Buffer containing the key domain parameters.
494*32b31808SJens Wiklander  *                              The content of this buffer is interpreted
495*32b31808SJens Wiklander  *                              according to \p type as described above.
496*32b31808SJens Wiklander  * \param data_length           Size of the \p data buffer in bytes.
497*32b31808SJens Wiklander  *
498*32b31808SJens Wiklander  * \retval #PSA_SUCCESS \emptydescription
499*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
500*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
501*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
502*32b31808SJens Wiklander  */
503*32b31808SJens Wiklander psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
504*32b31808SJens Wiklander                                            psa_key_type_t type,
505*32b31808SJens Wiklander                                            const uint8_t *data,
506*32b31808SJens Wiklander                                            size_t data_length);
507*32b31808SJens Wiklander 
508*32b31808SJens Wiklander /**
509*32b31808SJens Wiklander  * \brief Get domain parameters for a key.
510*32b31808SJens Wiklander  *
511*32b31808SJens Wiklander  * Get the domain parameters for a key with this function, if any. The format
512*32b31808SJens Wiklander  * of the domain parameters written to \p data is specified in the
513*32b31808SJens Wiklander  * documentation for psa_set_key_domain_parameters().
514*32b31808SJens Wiklander  *
515*32b31808SJens Wiklander  * \note This is an experimental extension to the interface. It may change
516*32b31808SJens Wiklander  *       in future versions of the library.
517*32b31808SJens Wiklander  *
518*32b31808SJens Wiklander  * \param[in] attributes        The key attribute structure to query.
519*32b31808SJens Wiklander  * \param[out] data             On success, the key domain parameters.
520*32b31808SJens Wiklander  * \param data_size             Size of the \p data buffer in bytes.
521*32b31808SJens Wiklander  *                              The buffer is guaranteed to be large
522*32b31808SJens Wiklander  *                              enough if its size in bytes is at least
523*32b31808SJens Wiklander  *                              the value given by
524*32b31808SJens Wiklander  *                              PSA_KEY_DOMAIN_PARAMETERS_SIZE().
525*32b31808SJens Wiklander  * \param[out] data_length      On success, the number of bytes
526*32b31808SJens Wiklander  *                              that make up the key domain parameters data.
527*32b31808SJens Wiklander  *
528*32b31808SJens Wiklander  * \retval #PSA_SUCCESS \emptydescription
529*32b31808SJens Wiklander  * \retval #PSA_ERROR_BUFFER_TOO_SMALL \emptydescription
530*32b31808SJens Wiklander  */
531*32b31808SJens Wiklander psa_status_t psa_get_key_domain_parameters(
532*32b31808SJens Wiklander     const psa_key_attributes_t *attributes,
533*32b31808SJens Wiklander     uint8_t *data,
534*32b31808SJens Wiklander     size_t data_size,
535*32b31808SJens Wiklander     size_t *data_length);
536*32b31808SJens Wiklander 
537*32b31808SJens Wiklander /** Safe output buffer size for psa_get_key_domain_parameters().
538*32b31808SJens Wiklander  *
539*32b31808SJens Wiklander  * This macro returns a compile-time constant if its arguments are
540*32b31808SJens Wiklander  * compile-time constants.
541*32b31808SJens Wiklander  *
542*32b31808SJens Wiklander  * \warning This function may call its arguments multiple times or
543*32b31808SJens Wiklander  *          zero times, so you should not pass arguments that contain
544*32b31808SJens Wiklander  *          side effects.
545*32b31808SJens Wiklander  *
546*32b31808SJens Wiklander  * \note This is an experimental extension to the interface. It may change
547*32b31808SJens Wiklander  *       in future versions of the library.
548*32b31808SJens Wiklander  *
549*32b31808SJens Wiklander  * \param key_type  A supported key type.
550*32b31808SJens Wiklander  * \param key_bits  The size of the key in bits.
551*32b31808SJens Wiklander  *
552*32b31808SJens Wiklander  * \return If the parameters are valid and supported, return
553*32b31808SJens Wiklander  *         a buffer size in bytes that guarantees that
554*32b31808SJens Wiklander  *         psa_get_key_domain_parameters() will not fail with
555*32b31808SJens Wiklander  *         #PSA_ERROR_BUFFER_TOO_SMALL.
556*32b31808SJens Wiklander  *         If the parameters are a valid combination that is not supported
557*32b31808SJens Wiklander  *         by the implementation, this macro shall return either a
558*32b31808SJens Wiklander  *         sensible size or 0.
559*32b31808SJens Wiklander  *         If the parameters are not valid, the
560*32b31808SJens Wiklander  *         return value is unspecified.
561*32b31808SJens Wiklander  */
562*32b31808SJens Wiklander #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits)              \
563*32b31808SJens Wiklander     (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) :                      \
564*32b31808SJens Wiklander      PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
565*32b31808SJens Wiklander      PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
566*32b31808SJens Wiklander      0)
567*32b31808SJens Wiklander #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits)     \
568*32b31808SJens Wiklander     (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
569*32b31808SJens Wiklander #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits)    \
570*32b31808SJens Wiklander     (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
571*32b31808SJens Wiklander 
572*32b31808SJens Wiklander /**@}*/
573*32b31808SJens Wiklander 
574*32b31808SJens Wiklander /** \defgroup psa_tls_helpers TLS helper functions
575*32b31808SJens Wiklander  * @{
576*32b31808SJens Wiklander  */
577*32b31808SJens Wiklander 
578*32b31808SJens Wiklander #if defined(MBEDTLS_ECP_C)
579*32b31808SJens Wiklander #include <mbedtls/ecp.h>
580*32b31808SJens Wiklander 
581*32b31808SJens Wiklander /** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
582*32b31808SJens Wiklander  *
583*32b31808SJens Wiklander  * \note This function is provided solely for the convenience of
584*32b31808SJens Wiklander  *       Mbed TLS and may be removed at any time without notice.
585*32b31808SJens Wiklander  *
586*32b31808SJens Wiklander  * \param grpid         An Mbed TLS elliptic curve identifier
587*32b31808SJens Wiklander  *                      (`MBEDTLS_ECP_DP_xxx`).
588*32b31808SJens Wiklander  * \param[out] bits     On success, the bit size of the curve.
589*32b31808SJens Wiklander  *
590*32b31808SJens Wiklander  * \return              The corresponding PSA elliptic curve identifier
591*32b31808SJens Wiklander  *                      (`PSA_ECC_FAMILY_xxx`).
592*32b31808SJens Wiklander  * \return              \c 0 on failure (\p grpid is not recognized).
593*32b31808SJens Wiklander  */
594*32b31808SJens Wiklander static inline psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
595*32b31808SJens Wiklander                                                         size_t *bits)
596*32b31808SJens Wiklander {
597*32b31808SJens Wiklander     switch (grpid) {
598*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP192R1:
599*32b31808SJens Wiklander             *bits = 192;
600*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_R1;
601*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP224R1:
602*32b31808SJens Wiklander             *bits = 224;
603*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_R1;
604*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP256R1:
605*32b31808SJens Wiklander             *bits = 256;
606*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_R1;
607*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP384R1:
608*32b31808SJens Wiklander             *bits = 384;
609*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_R1;
610*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP521R1:
611*32b31808SJens Wiklander             *bits = 521;
612*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_R1;
613*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_BP256R1:
614*32b31808SJens Wiklander             *bits = 256;
615*32b31808SJens Wiklander             return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
616*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_BP384R1:
617*32b31808SJens Wiklander             *bits = 384;
618*32b31808SJens Wiklander             return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
619*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_BP512R1:
620*32b31808SJens Wiklander             *bits = 512;
621*32b31808SJens Wiklander             return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
622*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_CURVE25519:
623*32b31808SJens Wiklander             *bits = 255;
624*32b31808SJens Wiklander             return PSA_ECC_FAMILY_MONTGOMERY;
625*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP192K1:
626*32b31808SJens Wiklander             *bits = 192;
627*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_K1;
628*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP224K1:
629*32b31808SJens Wiklander             *bits = 224;
630*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_K1;
631*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_SECP256K1:
632*32b31808SJens Wiklander             *bits = 256;
633*32b31808SJens Wiklander             return PSA_ECC_FAMILY_SECP_K1;
634*32b31808SJens Wiklander         case MBEDTLS_ECP_DP_CURVE448:
635*32b31808SJens Wiklander             *bits = 448;
636*32b31808SJens Wiklander             return PSA_ECC_FAMILY_MONTGOMERY;
637*32b31808SJens Wiklander         default:
638*32b31808SJens Wiklander             *bits = 0;
639*32b31808SJens Wiklander             return 0;
640*32b31808SJens Wiklander     }
641*32b31808SJens Wiklander }
642*32b31808SJens Wiklander 
643*32b31808SJens Wiklander /** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
644*32b31808SJens Wiklander  *
645*32b31808SJens Wiklander  * \note This function is provided solely for the convenience of
646*32b31808SJens Wiklander  *       Mbed TLS and may be removed at any time without notice.
647*32b31808SJens Wiklander  *
648*32b31808SJens Wiklander  * \param curve         A PSA elliptic curve identifier
649*32b31808SJens Wiklander  *                      (`PSA_ECC_FAMILY_xxx`).
650*32b31808SJens Wiklander  * \param bits          The bit-length of a private key on \p curve.
651*32b31808SJens Wiklander  * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up
652*32b31808SJens Wiklander  *                      to the nearest multiple of 8. This allows the caller
653*32b31808SJens Wiklander  *                      to infer the exact curve from the length of a key
654*32b31808SJens Wiklander  *                      which is supplied as a byte string.
655*32b31808SJens Wiklander  *
656*32b31808SJens Wiklander  * \return              The corresponding Mbed TLS elliptic curve identifier
657*32b31808SJens Wiklander  *                      (`MBEDTLS_ECP_DP_xxx`).
658*32b31808SJens Wiklander  * \return              #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
659*32b31808SJens Wiklander  * \return              #MBEDTLS_ECP_DP_NONE if \p bits is not
660*32b31808SJens Wiklander  *                      correct for \p curve.
661*32b31808SJens Wiklander  */
662*32b31808SJens Wiklander mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
663*32b31808SJens Wiklander                                               size_t bits,
664*32b31808SJens Wiklander                                               int bits_is_sloppy);
665*32b31808SJens Wiklander #endif /* MBEDTLS_ECP_C */
666*32b31808SJens Wiklander 
667*32b31808SJens Wiklander /**@}*/
668*32b31808SJens Wiklander 
669*32b31808SJens Wiklander /** \defgroup psa_external_rng External random generator
670*32b31808SJens Wiklander  * @{
671*32b31808SJens Wiklander  */
672*32b31808SJens Wiklander 
673*32b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
674*32b31808SJens Wiklander /** External random generator function, implemented by the platform.
675*32b31808SJens Wiklander  *
676*32b31808SJens Wiklander  * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
677*32b31808SJens Wiklander  * this function replaces Mbed TLS's entropy and DRBG modules for all
678*32b31808SJens Wiklander  * random generation triggered via PSA crypto interfaces.
679*32b31808SJens Wiklander  *
680*32b31808SJens Wiklander  * \note This random generator must deliver random numbers with cryptographic
681*32b31808SJens Wiklander  *       quality and high performance. It must supply unpredictable numbers
682*32b31808SJens Wiklander  *       with a uniform distribution. The implementation of this function
683*32b31808SJens Wiklander  *       is responsible for ensuring that the random generator is seeded
684*32b31808SJens Wiklander  *       with sufficient entropy. If you have a hardware TRNG which is slow
685*32b31808SJens Wiklander  *       or delivers non-uniform output, declare it as an entropy source
686*32b31808SJens Wiklander  *       with mbedtls_entropy_add_source() instead of enabling this option.
687*32b31808SJens Wiklander  *
688*32b31808SJens Wiklander  * \param[in,out] context       Pointer to the random generator context.
689*32b31808SJens Wiklander  *                              This is all-bits-zero on the first call
690*32b31808SJens Wiklander  *                              and preserved between successive calls.
691*32b31808SJens Wiklander  * \param[out] output           Output buffer. On success, this buffer
692*32b31808SJens Wiklander  *                              contains random data with a uniform
693*32b31808SJens Wiklander  *                              distribution.
694*32b31808SJens Wiklander  * \param output_size           The size of the \p output buffer in bytes.
695*32b31808SJens Wiklander  * \param[out] output_length    On success, set this value to \p output_size.
696*32b31808SJens Wiklander  *
697*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
698*32b31808SJens Wiklander  *         Success. The output buffer contains \p output_size bytes of
699*32b31808SJens Wiklander  *         cryptographic-quality random data, and \c *output_length is
700*32b31808SJens Wiklander  *         set to \p output_size.
701*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
702*32b31808SJens Wiklander  *         The random generator requires extra entropy and there is no
703*32b31808SJens Wiklander  *         way to obtain entropy under current environment conditions.
704*32b31808SJens Wiklander  *         This error should not happen under normal circumstances since
705*32b31808SJens Wiklander  *         this function is responsible for obtaining as much entropy as
706*32b31808SJens Wiklander  *         it needs. However implementations of this function may return
707*32b31808SJens Wiklander  *         #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
708*32b31808SJens Wiklander  *         entropy without blocking indefinitely.
709*32b31808SJens Wiklander  * \retval #PSA_ERROR_HARDWARE_FAILURE
710*32b31808SJens Wiklander  *         A failure of the random generator hardware that isn't covered
711*32b31808SJens Wiklander  *         by #PSA_ERROR_INSUFFICIENT_ENTROPY.
712*32b31808SJens Wiklander  */
713*32b31808SJens Wiklander psa_status_t mbedtls_psa_external_get_random(
714*32b31808SJens Wiklander     mbedtls_psa_external_random_context_t *context,
715*32b31808SJens Wiklander     uint8_t *output, size_t output_size, size_t *output_length);
716*32b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
717*32b31808SJens Wiklander 
718*32b31808SJens Wiklander /**@}*/
719*32b31808SJens Wiklander 
720*32b31808SJens Wiklander /** \defgroup psa_builtin_keys Built-in keys
721*32b31808SJens Wiklander  * @{
722*32b31808SJens Wiklander  */
723*32b31808SJens Wiklander 
724*32b31808SJens Wiklander /** The minimum value for a key identifier that is built into the
725*32b31808SJens Wiklander  * implementation.
726*32b31808SJens Wiklander  *
727*32b31808SJens Wiklander  * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
728*32b31808SJens Wiklander  * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
729*32b31808SJens Wiklander  * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
730*32b31808SJens Wiklander  * with any other set of implementation-chosen key identifiers.
731*32b31808SJens Wiklander  *
732*32b31808SJens Wiklander  * This value is part of the library's ABI since changing it would invalidate
733*32b31808SJens Wiklander  * the values of built-in key identifiers in applications.
734*32b31808SJens Wiklander  */
735*32b31808SJens Wiklander #define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN          ((psa_key_id_t) 0x7fff0000)
736*32b31808SJens Wiklander 
737*32b31808SJens Wiklander /** The maximum value for a key identifier that is built into the
738*32b31808SJens Wiklander  * implementation.
739*32b31808SJens Wiklander  *
740*32b31808SJens Wiklander  * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
741*32b31808SJens Wiklander  */
742*32b31808SJens Wiklander #define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX          ((psa_key_id_t) 0x7fffefff)
743*32b31808SJens Wiklander 
744*32b31808SJens Wiklander /** A slot number identifying a key in a driver.
745*32b31808SJens Wiklander  *
746*32b31808SJens Wiklander  * Values of this type are used to identify built-in keys.
747*32b31808SJens Wiklander  */
748*32b31808SJens Wiklander typedef uint64_t psa_drv_slot_number_t;
749*32b31808SJens Wiklander 
750*32b31808SJens Wiklander #if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
751*32b31808SJens Wiklander /** Test whether a key identifier belongs to the builtin key range.
752*32b31808SJens Wiklander  *
753*32b31808SJens Wiklander  * \param key_id  Key identifier to test.
754*32b31808SJens Wiklander  *
755*32b31808SJens Wiklander  * \retval 1
756*32b31808SJens Wiklander  *         The key identifier is a builtin key identifier.
757*32b31808SJens Wiklander  * \retval 0
758*32b31808SJens Wiklander  *         The key identifier is not a builtin key identifier.
759*32b31808SJens Wiklander  */
760*32b31808SJens Wiklander static inline int psa_key_id_is_builtin(psa_key_id_t key_id)
761*32b31808SJens Wiklander {
762*32b31808SJens Wiklander     return (key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN) &&
763*32b31808SJens Wiklander            (key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX);
764*32b31808SJens Wiklander }
765*32b31808SJens Wiklander 
766*32b31808SJens Wiklander /** Platform function to obtain the location and slot number of a built-in key.
767*32b31808SJens Wiklander  *
768*32b31808SJens Wiklander  * An application-specific implementation of this function must be provided if
769*32b31808SJens Wiklander  * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
770*32b31808SJens Wiklander  * as part of a platform's system image.
771*32b31808SJens Wiklander  *
772*32b31808SJens Wiklander  * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
773*32b31808SJens Wiklander  * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
774*32b31808SJens Wiklander  *
775*32b31808SJens Wiklander  * In a multi-application configuration
776*32b31808SJens Wiklander  * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
777*32b31808SJens Wiklander  * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
778*32b31808SJens Wiklander  * is allowed to use the given key.
779*32b31808SJens Wiklander  *
780*32b31808SJens Wiklander  * \param key_id                The key ID for which to retrieve the
781*32b31808SJens Wiklander  *                              location and slot attributes.
782*32b31808SJens Wiklander  * \param[out] lifetime         On success, the lifetime associated with the key
783*32b31808SJens Wiklander  *                              corresponding to \p key_id. Lifetime is a
784*32b31808SJens Wiklander  *                              combination of which driver contains the key,
785*32b31808SJens Wiklander  *                              and with what persistence level the key is
786*32b31808SJens Wiklander  *                              intended to be used. If the platform
787*32b31808SJens Wiklander  *                              implementation does not contain specific
788*32b31808SJens Wiklander  *                              information about the intended key persistence
789*32b31808SJens Wiklander  *                              level, the persistence level may be reported as
790*32b31808SJens Wiklander  *                              #PSA_KEY_PERSISTENCE_DEFAULT.
791*32b31808SJens Wiklander  * \param[out] slot_number      On success, the slot number known to the driver
792*32b31808SJens Wiklander  *                              registered at the lifetime location reported
793*32b31808SJens Wiklander  *                              through \p lifetime which corresponds to the
794*32b31808SJens Wiklander  *                              requested built-in key.
795*32b31808SJens Wiklander  *
796*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
797*32b31808SJens Wiklander  *         The requested key identifier designates a built-in key.
798*32b31808SJens Wiklander  *         In a multi-application configuration, the requested owner
799*32b31808SJens Wiklander  *         is allowed to access it.
800*32b31808SJens Wiklander  * \retval #PSA_ERROR_DOES_NOT_EXIST
801*32b31808SJens Wiklander  *         The requested key identifier is not a built-in key which is known
802*32b31808SJens Wiklander  *         to this function. If a key exists in the key storage with this
803*32b31808SJens Wiklander  *         identifier, the data from the storage will be used.
804*32b31808SJens Wiklander  * \return (any other error)
805*32b31808SJens Wiklander  *         Any other error is propagated to the function that requested the key.
806*32b31808SJens Wiklander  *         Common errors include:
807*32b31808SJens Wiklander  *         - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
808*32b31808SJens Wiklander  *           is not allowed to access it.
809*32b31808SJens Wiklander  */
810*32b31808SJens Wiklander psa_status_t mbedtls_psa_platform_get_builtin_key(
811*32b31808SJens Wiklander     mbedtls_svc_key_id_t key_id,
812*32b31808SJens Wiklander     psa_key_lifetime_t *lifetime,
813*32b31808SJens Wiklander     psa_drv_slot_number_t *slot_number);
814*32b31808SJens Wiklander #endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
815*32b31808SJens Wiklander 
816*32b31808SJens Wiklander /** @} */
817*32b31808SJens Wiklander 
818*32b31808SJens Wiklander /** \addtogroup crypto_types
819*32b31808SJens Wiklander  * @{
820*32b31808SJens Wiklander  */
821*32b31808SJens Wiklander 
822*32b31808SJens Wiklander #define PSA_ALG_CATEGORY_PAKE                   ((psa_algorithm_t) 0x0a000000)
823*32b31808SJens Wiklander 
824*32b31808SJens Wiklander /** Whether the specified algorithm is a password-authenticated key exchange.
825*32b31808SJens Wiklander  *
826*32b31808SJens Wiklander  * \param alg An algorithm identifier (value of type #psa_algorithm_t).
827*32b31808SJens Wiklander  *
828*32b31808SJens Wiklander  * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
829*32b31808SJens Wiklander  *         algorithm, 0 otherwise.
830*32b31808SJens Wiklander  *         This macro may return either 0 or 1 if \p alg is not a supported
831*32b31808SJens Wiklander  *         algorithm identifier.
832*32b31808SJens Wiklander  */
833*32b31808SJens Wiklander #define PSA_ALG_IS_PAKE(alg)                                        \
834*32b31808SJens Wiklander     (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
835*32b31808SJens Wiklander 
836*32b31808SJens Wiklander /** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
837*32b31808SJens Wiklander  *
838*32b31808SJens Wiklander  * This is J-PAKE as defined by RFC 8236, instantiated with the following
839*32b31808SJens Wiklander  * parameters:
840*32b31808SJens Wiklander  *
841*32b31808SJens Wiklander  * - The group can be either an elliptic curve or defined over a finite field.
842*32b31808SJens Wiklander  * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
843*32b31808SJens Wiklander  *   J-PAKE algorithm.
844*32b31808SJens Wiklander  * - A cryptographic hash function.
845*32b31808SJens Wiklander  *
846*32b31808SJens Wiklander  * To select these parameters and set up the cipher suite, call these functions
847*32b31808SJens Wiklander  * in any order:
848*32b31808SJens Wiklander  *
849*32b31808SJens Wiklander  * \code
850*32b31808SJens Wiklander  * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
851*32b31808SJens Wiklander  * psa_pake_cs_set_primitive(cipher_suite,
852*32b31808SJens Wiklander  *                           PSA_PAKE_PRIMITIVE(type, family, bits));
853*32b31808SJens Wiklander  * psa_pake_cs_set_hash(cipher_suite, hash);
854*32b31808SJens Wiklander  * \endcode
855*32b31808SJens Wiklander  *
856*32b31808SJens Wiklander  * For more information on how to set a specific curve or field, refer to the
857*32b31808SJens Wiklander  * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
858*32b31808SJens Wiklander  *
859*32b31808SJens Wiklander  * After initializing a J-PAKE operation, call
860*32b31808SJens Wiklander  *
861*32b31808SJens Wiklander  * \code
862*32b31808SJens Wiklander  * psa_pake_setup(operation, cipher_suite);
863*32b31808SJens Wiklander  * psa_pake_set_user(operation, ...);
864*32b31808SJens Wiklander  * psa_pake_set_peer(operation, ...);
865*32b31808SJens Wiklander  * psa_pake_set_password_key(operation, ...);
866*32b31808SJens Wiklander  * \endcode
867*32b31808SJens Wiklander  *
868*32b31808SJens Wiklander  * The password is provided as a key. This can be the password text itself,
869*32b31808SJens Wiklander  * in an agreed character encoding, or some value derived from the password
870*32b31808SJens Wiklander  * as required by a higher level protocol.
871*32b31808SJens Wiklander  *
872*32b31808SJens Wiklander  * (The implementation converts the key material to a number as described in
873*32b31808SJens Wiklander  * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
874*32b31808SJens Wiklander  * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
875*32b31808SJens Wiklander  * \c q is order of the group defined by the primitive set in the cipher suite.
876*32b31808SJens Wiklander  * The \c psa_pake_set_password_key() function returns an error if the result
877*32b31808SJens Wiklander  * of the reduction is 0.)
878*32b31808SJens Wiklander  *
879*32b31808SJens Wiklander  * The key exchange flow for J-PAKE is as follows:
880*32b31808SJens Wiklander  * -# To get the first round data that needs to be sent to the peer, call
881*32b31808SJens Wiklander  *    \code
882*32b31808SJens Wiklander  *    // Get g1
883*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
884*32b31808SJens Wiklander  *    // Get the ZKP public key for x1
885*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
886*32b31808SJens Wiklander  *    // Get the ZKP proof for x1
887*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
888*32b31808SJens Wiklander  *    // Get g2
889*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
890*32b31808SJens Wiklander  *    // Get the ZKP public key for x2
891*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
892*32b31808SJens Wiklander  *    // Get the ZKP proof for x2
893*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
894*32b31808SJens Wiklander  *    \endcode
895*32b31808SJens Wiklander  * -# To provide the first round data received from the peer to the operation,
896*32b31808SJens Wiklander  *    call
897*32b31808SJens Wiklander  *    \code
898*32b31808SJens Wiklander  *    // Set g3
899*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
900*32b31808SJens Wiklander  *    // Set the ZKP public key for x3
901*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
902*32b31808SJens Wiklander  *    // Set the ZKP proof for x3
903*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
904*32b31808SJens Wiklander  *    // Set g4
905*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
906*32b31808SJens Wiklander  *    // Set the ZKP public key for x4
907*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
908*32b31808SJens Wiklander  *    // Set the ZKP proof for x4
909*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
910*32b31808SJens Wiklander  *    \endcode
911*32b31808SJens Wiklander  * -# To get the second round data that needs to be sent to the peer, call
912*32b31808SJens Wiklander  *    \code
913*32b31808SJens Wiklander  *    // Get A
914*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
915*32b31808SJens Wiklander  *    // Get ZKP public key for x2*s
916*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
917*32b31808SJens Wiklander  *    // Get ZKP proof for x2*s
918*32b31808SJens Wiklander  *    psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
919*32b31808SJens Wiklander  *    \endcode
920*32b31808SJens Wiklander  * -# To provide the second round data received from the peer to the operation,
921*32b31808SJens Wiklander  *    call
922*32b31808SJens Wiklander  *    \code
923*32b31808SJens Wiklander  *    // Set B
924*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
925*32b31808SJens Wiklander  *    // Set ZKP public key for x4*s
926*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
927*32b31808SJens Wiklander  *    // Set ZKP proof for x4*s
928*32b31808SJens Wiklander  *    psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
929*32b31808SJens Wiklander  *    \endcode
930*32b31808SJens Wiklander  * -# To access the shared secret call
931*32b31808SJens Wiklander  *    \code
932*32b31808SJens Wiklander  *    // Get Ka=Kb=K
933*32b31808SJens Wiklander  *    psa_pake_get_implicit_key()
934*32b31808SJens Wiklander  *    \endcode
935*32b31808SJens Wiklander  *
936*32b31808SJens Wiklander  * For more information consult the documentation of the individual
937*32b31808SJens Wiklander  * \c PSA_PAKE_STEP_XXX constants.
938*32b31808SJens Wiklander  *
939*32b31808SJens Wiklander  * At this point there is a cryptographic guarantee that only the authenticated
940*32b31808SJens Wiklander  * party who used the same password is able to compute the key. But there is no
941*32b31808SJens Wiklander  * guarantee that the peer is the party it claims to be and was able to do so.
942*32b31808SJens Wiklander  *
943*32b31808SJens Wiklander  * That is, the authentication is only implicit (the peer is not authenticated
944*32b31808SJens Wiklander  * at this point, and no action should be taken that assume that they are - like
945*32b31808SJens Wiklander  * for example accessing restricted files).
946*32b31808SJens Wiklander  *
947*32b31808SJens Wiklander  * To make the authentication explicit there are various methods, see Section 5
948*32b31808SJens Wiklander  * of RFC 8236 for two examples.
949*32b31808SJens Wiklander  *
950*32b31808SJens Wiklander  */
951*32b31808SJens Wiklander #define PSA_ALG_JPAKE                   ((psa_algorithm_t) 0x0a000100)
952*32b31808SJens Wiklander 
953*32b31808SJens Wiklander /** @} */
954*32b31808SJens Wiklander 
955*32b31808SJens Wiklander /** \defgroup pake Password-authenticated key exchange (PAKE)
956*32b31808SJens Wiklander  *
957*32b31808SJens Wiklander  * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
958*32b31808SJens Wiklander  * the official PSA Crypto API yet.
959*32b31808SJens Wiklander  *
960*32b31808SJens Wiklander  * \note The content of this section is not part of the stable API and ABI
961*32b31808SJens Wiklander  *       of Mbed Crypto and may change arbitrarily from version to version.
962*32b31808SJens Wiklander  *       Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
963*32b31808SJens Wiklander  *       #PSA_ALG_JPAKE.
964*32b31808SJens Wiklander  * @{
965*32b31808SJens Wiklander  */
966*32b31808SJens Wiklander 
967*32b31808SJens Wiklander /** \brief Encoding of the application role of PAKE
968*32b31808SJens Wiklander  *
969*32b31808SJens Wiklander  * Encodes the application's role in the algorithm is being executed. For more
970*32b31808SJens Wiklander  * information see the documentation of individual \c PSA_PAKE_ROLE_XXX
971*32b31808SJens Wiklander  * constants.
972*32b31808SJens Wiklander  */
973*32b31808SJens Wiklander typedef uint8_t psa_pake_role_t;
974*32b31808SJens Wiklander 
975*32b31808SJens Wiklander /** Encoding of input and output indicators for PAKE.
976*32b31808SJens Wiklander  *
977*32b31808SJens Wiklander  * Some PAKE algorithms need to exchange more data than just a single key share.
978*32b31808SJens Wiklander  * This type is for encoding additional input and output data for such
979*32b31808SJens Wiklander  * algorithms.
980*32b31808SJens Wiklander  */
981*32b31808SJens Wiklander typedef uint8_t psa_pake_step_t;
982*32b31808SJens Wiklander 
983*32b31808SJens Wiklander /** Encoding of the type of the PAKE's primitive.
984*32b31808SJens Wiklander  *
985*32b31808SJens Wiklander  * Values defined by this standard will never be in the range 0x80-0xff.
986*32b31808SJens Wiklander  * Vendors who define additional types must use an encoding in this range.
987*32b31808SJens Wiklander  *
988*32b31808SJens Wiklander  * For more information see the documentation of individual
989*32b31808SJens Wiklander  * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
990*32b31808SJens Wiklander  */
991*32b31808SJens Wiklander typedef uint8_t psa_pake_primitive_type_t;
992*32b31808SJens Wiklander 
993*32b31808SJens Wiklander /** \brief Encoding of the family of the primitive associated with the PAKE.
994*32b31808SJens Wiklander  *
995*32b31808SJens Wiklander  * For more information see the documentation of individual
996*32b31808SJens Wiklander  * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
997*32b31808SJens Wiklander  */
998*32b31808SJens Wiklander typedef uint8_t psa_pake_family_t;
999*32b31808SJens Wiklander 
1000*32b31808SJens Wiklander /** \brief Encoding of the primitive associated with the PAKE.
1001*32b31808SJens Wiklander  *
1002*32b31808SJens Wiklander  * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
1003*32b31808SJens Wiklander  */
1004*32b31808SJens Wiklander typedef uint32_t psa_pake_primitive_t;
1005*32b31808SJens Wiklander 
1006*32b31808SJens Wiklander /** A value to indicate no role in a PAKE algorithm.
1007*32b31808SJens Wiklander  * This value can be used in a call to psa_pake_set_role() for symmetric PAKE
1008*32b31808SJens Wiklander  * algorithms which do not assign roles.
1009*32b31808SJens Wiklander  */
1010*32b31808SJens Wiklander #define PSA_PAKE_ROLE_NONE                  ((psa_pake_role_t) 0x00)
1011*32b31808SJens Wiklander 
1012*32b31808SJens Wiklander /** The first peer in a balanced PAKE.
1013*32b31808SJens Wiklander  *
1014*32b31808SJens Wiklander  * Although balanced PAKE algorithms are symmetric, some of them needs an
1015*32b31808SJens Wiklander  * ordering of peers for the transcript calculations. If the algorithm does not
1016*32b31808SJens Wiklander  * need this, both #PSA_PAKE_ROLE_FIRST and #PSA_PAKE_ROLE_SECOND are
1017*32b31808SJens Wiklander  * accepted.
1018*32b31808SJens Wiklander  */
1019*32b31808SJens Wiklander #define PSA_PAKE_ROLE_FIRST                ((psa_pake_role_t) 0x01)
1020*32b31808SJens Wiklander 
1021*32b31808SJens Wiklander /** The second peer in a balanced PAKE.
1022*32b31808SJens Wiklander  *
1023*32b31808SJens Wiklander  * Although balanced PAKE algorithms are symmetric, some of them needs an
1024*32b31808SJens Wiklander  * ordering of peers for the transcript calculations. If the algorithm does not
1025*32b31808SJens Wiklander  * need this, either #PSA_PAKE_ROLE_FIRST or #PSA_PAKE_ROLE_SECOND are
1026*32b31808SJens Wiklander  * accepted.
1027*32b31808SJens Wiklander  */
1028*32b31808SJens Wiklander #define PSA_PAKE_ROLE_SECOND                ((psa_pake_role_t) 0x02)
1029*32b31808SJens Wiklander 
1030*32b31808SJens Wiklander /** The client in an augmented PAKE.
1031*32b31808SJens Wiklander  *
1032*32b31808SJens Wiklander  * Augmented PAKE algorithms need to differentiate between client and server.
1033*32b31808SJens Wiklander  */
1034*32b31808SJens Wiklander #define PSA_PAKE_ROLE_CLIENT                ((psa_pake_role_t) 0x11)
1035*32b31808SJens Wiklander 
1036*32b31808SJens Wiklander /** The server in an augmented PAKE.
1037*32b31808SJens Wiklander  *
1038*32b31808SJens Wiklander  * Augmented PAKE algorithms need to differentiate between client and server.
1039*32b31808SJens Wiklander  */
1040*32b31808SJens Wiklander #define PSA_PAKE_ROLE_SERVER                ((psa_pake_role_t) 0x12)
1041*32b31808SJens Wiklander 
1042*32b31808SJens Wiklander /** The PAKE primitive type indicating the use of elliptic curves.
1043*32b31808SJens Wiklander  *
1044*32b31808SJens Wiklander  * The values of the \c family and \c bits fields of the cipher suite identify a
1045*32b31808SJens Wiklander  * specific elliptic curve, using the same mapping that is used for ECC
1046*32b31808SJens Wiklander  * (::psa_ecc_family_t) keys.
1047*32b31808SJens Wiklander  *
1048*32b31808SJens Wiklander  * (Here \c family means the value returned by psa_pake_cs_get_family() and
1049*32b31808SJens Wiklander  * \c bits means the value returned by psa_pake_cs_get_bits().)
1050*32b31808SJens Wiklander  *
1051*32b31808SJens Wiklander  * Input and output during the operation can involve group elements and scalar
1052*32b31808SJens Wiklander  * values:
1053*32b31808SJens Wiklander  * -# The format for group elements is the same as for public keys on the
1054*32b31808SJens Wiklander  *  specific curve would be. For more information, consult the documentation of
1055*32b31808SJens Wiklander  *  psa_export_public_key().
1056*32b31808SJens Wiklander  * -# The format for scalars is the same as for private keys on the specific
1057*32b31808SJens Wiklander  *  curve would be. For more information, consult the documentation of
1058*32b31808SJens Wiklander  *  psa_export_key().
1059*32b31808SJens Wiklander  */
1060*32b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE_TYPE_ECC       ((psa_pake_primitive_type_t) 0x01)
1061*32b31808SJens Wiklander 
1062*32b31808SJens Wiklander /** The PAKE primitive type indicating the use of Diffie-Hellman groups.
1063*32b31808SJens Wiklander  *
1064*32b31808SJens Wiklander  * The values of the \c family and \c bits fields of the cipher suite identify
1065*32b31808SJens Wiklander  * a specific Diffie-Hellman group, using the same mapping that is used for
1066*32b31808SJens Wiklander  * Diffie-Hellman (::psa_dh_family_t) keys.
1067*32b31808SJens Wiklander  *
1068*32b31808SJens Wiklander  * (Here \c family means the value returned by psa_pake_cs_get_family() and
1069*32b31808SJens Wiklander  * \c bits means the value returned by psa_pake_cs_get_bits().)
1070*32b31808SJens Wiklander  *
1071*32b31808SJens Wiklander  * Input and output during the operation can involve group elements and scalar
1072*32b31808SJens Wiklander  * values:
1073*32b31808SJens Wiklander  * -# The format for group elements is the same as for public keys on the
1074*32b31808SJens Wiklander  *  specific group would be. For more information, consult the documentation of
1075*32b31808SJens Wiklander  *  psa_export_public_key().
1076*32b31808SJens Wiklander  * -# The format for scalars is the same as for private keys on the specific
1077*32b31808SJens Wiklander  *  group would be. For more information, consult the documentation of
1078*32b31808SJens Wiklander  *  psa_export_key().
1079*32b31808SJens Wiklander  */
1080*32b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE_TYPE_DH       ((psa_pake_primitive_type_t) 0x02)
1081*32b31808SJens Wiklander 
1082*32b31808SJens Wiklander /** Construct a PAKE primitive from type, family and bit-size.
1083*32b31808SJens Wiklander  *
1084*32b31808SJens Wiklander  * \param pake_type     The type of the primitive
1085*32b31808SJens Wiklander  *                      (value of type ::psa_pake_primitive_type_t).
1086*32b31808SJens Wiklander  * \param pake_family   The family of the primitive
1087*32b31808SJens Wiklander  *                      (the type and interpretation of this parameter depends
1088*32b31808SJens Wiklander  *                      on \p type, for more information consult the
1089*32b31808SJens Wiklander  *                      documentation of individual ::psa_pake_primitive_type_t
1090*32b31808SJens Wiklander  *                      constants).
1091*32b31808SJens Wiklander  * \param pake_bits     The bit-size of the primitive
1092*32b31808SJens Wiklander  *                      (Value of type \c size_t. The interpretation
1093*32b31808SJens Wiklander  *                      of this parameter depends on \p family, for more
1094*32b31808SJens Wiklander  *                      information consult the documentation of individual
1095*32b31808SJens Wiklander  *                      ::psa_pake_primitive_type_t constants).
1096*32b31808SJens Wiklander  *
1097*32b31808SJens Wiklander  * \return The constructed primitive value of type ::psa_pake_primitive_t.
1098*32b31808SJens Wiklander  *         Return 0 if the requested primitive can't be encoded as
1099*32b31808SJens Wiklander  *         ::psa_pake_primitive_t.
1100*32b31808SJens Wiklander  */
1101*32b31808SJens Wiklander #define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
1102*32b31808SJens Wiklander     ((pake_bits & 0xFFFF) != pake_bits) ? 0 :                 \
1103*32b31808SJens Wiklander     ((psa_pake_primitive_t) (((pake_type) << 24 |             \
1104*32b31808SJens Wiklander                               (pake_family) << 16) | (pake_bits)))
1105*32b31808SJens Wiklander 
1106*32b31808SJens Wiklander /** The key share being sent to or received from the peer.
1107*32b31808SJens Wiklander  *
1108*32b31808SJens Wiklander  * The format for both input and output at this step is the same as for public
1109*32b31808SJens Wiklander  * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1110*32b31808SJens Wiklander  * be.
1111*32b31808SJens Wiklander  *
1112*32b31808SJens Wiklander  * For more information on the format, consult the documentation of
1113*32b31808SJens Wiklander  * psa_export_public_key().
1114*32b31808SJens Wiklander  *
1115*32b31808SJens Wiklander  * For information regarding how the group is determined, consult the
1116*32b31808SJens Wiklander  * documentation #PSA_PAKE_PRIMITIVE.
1117*32b31808SJens Wiklander  */
1118*32b31808SJens Wiklander #define PSA_PAKE_STEP_KEY_SHARE                 ((psa_pake_step_t) 0x01)
1119*32b31808SJens Wiklander 
1120*32b31808SJens Wiklander /** A Schnorr NIZKP public key.
1121*32b31808SJens Wiklander  *
1122*32b31808SJens Wiklander  * This is the ephemeral public key in the Schnorr Non-Interactive
1123*32b31808SJens Wiklander  * Zero-Knowledge Proof (the value denoted by the letter 'V' in RFC 8235).
1124*32b31808SJens Wiklander  *
1125*32b31808SJens Wiklander  * The format for both input and output at this step is the same as for public
1126*32b31808SJens Wiklander  * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1127*32b31808SJens Wiklander  * be.
1128*32b31808SJens Wiklander  *
1129*32b31808SJens Wiklander  * For more information on the format, consult the documentation of
1130*32b31808SJens Wiklander  * psa_export_public_key().
1131*32b31808SJens Wiklander  *
1132*32b31808SJens Wiklander  * For information regarding how the group is determined, consult the
1133*32b31808SJens Wiklander  * documentation #PSA_PAKE_PRIMITIVE.
1134*32b31808SJens Wiklander  */
1135*32b31808SJens Wiklander #define PSA_PAKE_STEP_ZK_PUBLIC                 ((psa_pake_step_t) 0x02)
1136*32b31808SJens Wiklander 
1137*32b31808SJens Wiklander /** A Schnorr NIZKP proof.
1138*32b31808SJens Wiklander  *
1139*32b31808SJens Wiklander  * This is the proof in the Schnorr Non-Interactive Zero-Knowledge Proof (the
1140*32b31808SJens Wiklander  * value denoted by the letter 'r' in RFC 8235).
1141*32b31808SJens Wiklander  *
1142*32b31808SJens Wiklander  * Both for input and output, the value at this step is an integer less than
1143*32b31808SJens Wiklander  * the order of the group selected in the cipher suite. The format depends on
1144*32b31808SJens Wiklander  * the group as well:
1145*32b31808SJens Wiklander  *
1146*32b31808SJens Wiklander  * - For Montgomery curves, the encoding is little endian.
1147*32b31808SJens Wiklander  * - For everything else the encoding is big endian (see Section 2.3.8 of
1148*32b31808SJens Wiklander  *   _SEC 1: Elliptic Curve Cryptography_ at https://www.secg.org/sec1-v2.pdf).
1149*32b31808SJens Wiklander  *
1150*32b31808SJens Wiklander  * In both cases leading zeroes are allowed as long as the length in bytes does
1151*32b31808SJens Wiklander  * not exceed the byte length of the group order.
1152*32b31808SJens Wiklander  *
1153*32b31808SJens Wiklander  * For information regarding how the group is determined, consult the
1154*32b31808SJens Wiklander  * documentation #PSA_PAKE_PRIMITIVE.
1155*32b31808SJens Wiklander  */
1156*32b31808SJens Wiklander #define PSA_PAKE_STEP_ZK_PROOF                  ((psa_pake_step_t) 0x03)
1157*32b31808SJens Wiklander 
1158*32b31808SJens Wiklander /** The type of the data structure for PAKE cipher suites.
1159*32b31808SJens Wiklander  *
1160*32b31808SJens Wiklander  * This is an implementation-defined \c struct. Applications should not
1161*32b31808SJens Wiklander  * make any assumptions about the content of this structure.
1162*32b31808SJens Wiklander  * Implementation details can change in future versions without notice.
1163*32b31808SJens Wiklander  */
1164*32b31808SJens Wiklander typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1165*32b31808SJens Wiklander 
1166*32b31808SJens Wiklander /** Return an initial value for a PAKE cipher suite object.
1167*32b31808SJens Wiklander  */
1168*32b31808SJens Wiklander static psa_pake_cipher_suite_t psa_pake_cipher_suite_init(void);
1169*32b31808SJens Wiklander 
1170*32b31808SJens Wiklander /** Retrieve the PAKE algorithm from a PAKE cipher suite.
1171*32b31808SJens Wiklander  *
1172*32b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
1173*32b31808SJens Wiklander  *
1174*32b31808SJens Wiklander  * \return The PAKE algorithm stored in the cipher suite structure.
1175*32b31808SJens Wiklander  */
1176*32b31808SJens Wiklander static psa_algorithm_t psa_pake_cs_get_algorithm(
1177*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
1178*32b31808SJens Wiklander 
1179*32b31808SJens Wiklander /** Declare the PAKE algorithm for the cipher suite.
1180*32b31808SJens Wiklander  *
1181*32b31808SJens Wiklander  * This function overwrites any PAKE algorithm
1182*32b31808SJens Wiklander  * previously set in \p cipher_suite.
1183*32b31808SJens Wiklander  *
1184*32b31808SJens Wiklander  * \param[out] cipher_suite    The cipher suite structure to write to.
1185*32b31808SJens Wiklander  * \param algorithm            The PAKE algorithm to write.
1186*32b31808SJens Wiklander  *                             (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1187*32b31808SJens Wiklander  *                             such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1188*32b31808SJens Wiklander  *                             If this is 0, the PAKE algorithm in
1189*32b31808SJens Wiklander  *                             \p cipher_suite becomes unspecified.
1190*32b31808SJens Wiklander  */
1191*32b31808SJens Wiklander static void psa_pake_cs_set_algorithm(psa_pake_cipher_suite_t *cipher_suite,
1192*32b31808SJens Wiklander                                       psa_algorithm_t algorithm);
1193*32b31808SJens Wiklander 
1194*32b31808SJens Wiklander /** Retrieve the primitive from a PAKE cipher suite.
1195*32b31808SJens Wiklander  *
1196*32b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
1197*32b31808SJens Wiklander  *
1198*32b31808SJens Wiklander  * \return The primitive stored in the cipher suite structure.
1199*32b31808SJens Wiklander  */
1200*32b31808SJens Wiklander static psa_pake_primitive_t psa_pake_cs_get_primitive(
1201*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
1202*32b31808SJens Wiklander 
1203*32b31808SJens Wiklander /** Declare the primitive for a PAKE cipher suite.
1204*32b31808SJens Wiklander  *
1205*32b31808SJens Wiklander  * This function overwrites any primitive previously set in \p cipher_suite.
1206*32b31808SJens Wiklander  *
1207*32b31808SJens Wiklander  * \param[out] cipher_suite    The cipher suite structure to write to.
1208*32b31808SJens Wiklander  * \param primitive            The primitive to write. If this is 0, the
1209*32b31808SJens Wiklander  *                             primitive type in \p cipher_suite becomes
1210*32b31808SJens Wiklander  *                             unspecified.
1211*32b31808SJens Wiklander  */
1212*32b31808SJens Wiklander static void psa_pake_cs_set_primitive(psa_pake_cipher_suite_t *cipher_suite,
1213*32b31808SJens Wiklander                                       psa_pake_primitive_t primitive);
1214*32b31808SJens Wiklander 
1215*32b31808SJens Wiklander /** Retrieve the PAKE family from a PAKE cipher suite.
1216*32b31808SJens Wiklander  *
1217*32b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
1218*32b31808SJens Wiklander  *
1219*32b31808SJens Wiklander  * \return The PAKE family stored in the cipher suite structure.
1220*32b31808SJens Wiklander  */
1221*32b31808SJens Wiklander static psa_pake_family_t psa_pake_cs_get_family(
1222*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
1223*32b31808SJens Wiklander 
1224*32b31808SJens Wiklander /** Retrieve the PAKE primitive bit-size from a PAKE cipher suite.
1225*32b31808SJens Wiklander  *
1226*32b31808SJens Wiklander  * \param[in] cipher_suite     The cipher suite structure to query.
1227*32b31808SJens Wiklander  *
1228*32b31808SJens Wiklander  * \return The PAKE primitive bit-size stored in the cipher suite structure.
1229*32b31808SJens Wiklander  */
1230*32b31808SJens Wiklander static uint16_t psa_pake_cs_get_bits(
1231*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
1232*32b31808SJens Wiklander 
1233*32b31808SJens Wiklander /** Retrieve the hash algorithm from a PAKE cipher suite.
1234*32b31808SJens Wiklander  *
1235*32b31808SJens Wiklander  * \param[in] cipher_suite      The cipher suite structure to query.
1236*32b31808SJens Wiklander  *
1237*32b31808SJens Wiklander  * \return The hash algorithm stored in the cipher suite structure. The return
1238*32b31808SJens Wiklander  *         value is 0 if the PAKE is not parametrised by a hash algorithm or if
1239*32b31808SJens Wiklander  *         the hash algorithm is not set.
1240*32b31808SJens Wiklander  */
1241*32b31808SJens Wiklander static psa_algorithm_t psa_pake_cs_get_hash(
1242*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite);
1243*32b31808SJens Wiklander 
1244*32b31808SJens Wiklander /** Declare the hash algorithm for a PAKE cipher suite.
1245*32b31808SJens Wiklander  *
1246*32b31808SJens Wiklander  * This function overwrites any hash algorithm
1247*32b31808SJens Wiklander  * previously set in \p cipher_suite.
1248*32b31808SJens Wiklander  *
1249*32b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1250*32b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1251*32b31808SJens Wiklander  * for more information.
1252*32b31808SJens Wiklander  *
1253*32b31808SJens Wiklander  * \param[out] cipher_suite     The cipher suite structure to write to.
1254*32b31808SJens Wiklander  * \param hash                  The hash involved in the cipher suite.
1255*32b31808SJens Wiklander  *                              (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1256*32b31808SJens Wiklander  *                              such that #PSA_ALG_IS_HASH(\c alg) is true.)
1257*32b31808SJens Wiklander  *                              If this is 0, the hash algorithm in
1258*32b31808SJens Wiklander  *                              \p cipher_suite becomes unspecified.
1259*32b31808SJens Wiklander  */
1260*32b31808SJens Wiklander static void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
1261*32b31808SJens Wiklander                                  psa_algorithm_t hash);
1262*32b31808SJens Wiklander 
1263*32b31808SJens Wiklander /** The type of the state data structure for PAKE operations.
1264*32b31808SJens Wiklander  *
1265*32b31808SJens Wiklander  * Before calling any function on a PAKE operation object, the application
1266*32b31808SJens Wiklander  * must initialize it by any of the following means:
1267*32b31808SJens Wiklander  * - Set the structure to all-bits-zero, for example:
1268*32b31808SJens Wiklander  *   \code
1269*32b31808SJens Wiklander  *   psa_pake_operation_t operation;
1270*32b31808SJens Wiklander  *   memset(&operation, 0, sizeof(operation));
1271*32b31808SJens Wiklander  *   \endcode
1272*32b31808SJens Wiklander  * - Initialize the structure to logical zero values, for example:
1273*32b31808SJens Wiklander  *   \code
1274*32b31808SJens Wiklander  *   psa_pake_operation_t operation = {0};
1275*32b31808SJens Wiklander  *   \endcode
1276*32b31808SJens Wiklander  * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1277*32b31808SJens Wiklander  *   for example:
1278*32b31808SJens Wiklander  *   \code
1279*32b31808SJens Wiklander  *   psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1280*32b31808SJens Wiklander  *   \endcode
1281*32b31808SJens Wiklander  * - Assign the result of the function psa_pake_operation_init()
1282*32b31808SJens Wiklander  *   to the structure, for example:
1283*32b31808SJens Wiklander  *   \code
1284*32b31808SJens Wiklander  *   psa_pake_operation_t operation;
1285*32b31808SJens Wiklander  *   operation = psa_pake_operation_init();
1286*32b31808SJens Wiklander  *   \endcode
1287*32b31808SJens Wiklander  *
1288*32b31808SJens Wiklander  * This is an implementation-defined \c struct. Applications should not
1289*32b31808SJens Wiklander  * make any assumptions about the content of this structure.
1290*32b31808SJens Wiklander  * Implementation details can change in future versions without notice. */
1291*32b31808SJens Wiklander typedef struct psa_pake_operation_s psa_pake_operation_t;
1292*32b31808SJens Wiklander 
1293*32b31808SJens Wiklander /** The type of input values for PAKE operations. */
1294*32b31808SJens Wiklander typedef struct psa_crypto_driver_pake_inputs_s psa_crypto_driver_pake_inputs_t;
1295*32b31808SJens Wiklander 
1296*32b31808SJens Wiklander /** The type of computation stage for J-PAKE operations. */
1297*32b31808SJens Wiklander typedef struct psa_jpake_computation_stage_s psa_jpake_computation_stage_t;
1298*32b31808SJens Wiklander 
1299*32b31808SJens Wiklander /** Return an initial value for a PAKE operation object.
1300*32b31808SJens Wiklander  */
1301*32b31808SJens Wiklander static psa_pake_operation_t psa_pake_operation_init(void);
1302*32b31808SJens Wiklander 
1303*32b31808SJens Wiklander /** Get the length of the password in bytes from given inputs.
1304*32b31808SJens Wiklander  *
1305*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1306*32b31808SJens Wiklander  * \param[out] password_len     Password length.
1307*32b31808SJens Wiklander  *
1308*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1309*32b31808SJens Wiklander  *         Success.
1310*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1311*32b31808SJens Wiklander  *         Password hasn't been set yet.
1312*32b31808SJens Wiklander  */
1313*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_password_len(
1314*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1315*32b31808SJens Wiklander     size_t *password_len);
1316*32b31808SJens Wiklander 
1317*32b31808SJens Wiklander /** Get the password from given inputs.
1318*32b31808SJens Wiklander  *
1319*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1320*32b31808SJens Wiklander  * \param[out] buffer           Return buffer for password.
1321*32b31808SJens Wiklander  * \param      buffer_size      Size of the return buffer in bytes.
1322*32b31808SJens Wiklander  * \param[out] buffer_length    Actual size of the password in bytes.
1323*32b31808SJens Wiklander  *
1324*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1325*32b31808SJens Wiklander  *         Success.
1326*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1327*32b31808SJens Wiklander  *         Password hasn't been set yet.
1328*32b31808SJens Wiklander  */
1329*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_password(
1330*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1331*32b31808SJens Wiklander     uint8_t *buffer, size_t buffer_size, size_t *buffer_length);
1332*32b31808SJens Wiklander 
1333*32b31808SJens Wiklander /** Get the role from given inputs.
1334*32b31808SJens Wiklander  *
1335*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1336*32b31808SJens Wiklander  * \param[out] role             Return buffer for role.
1337*32b31808SJens Wiklander  *
1338*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1339*32b31808SJens Wiklander  *         Success.
1340*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1341*32b31808SJens Wiklander  *         Role hasn't been set yet.
1342*32b31808SJens Wiklander  */
1343*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_role(
1344*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1345*32b31808SJens Wiklander     psa_pake_role_t *role);
1346*32b31808SJens Wiklander 
1347*32b31808SJens Wiklander /** Get the length of the user id in bytes from given inputs.
1348*32b31808SJens Wiklander  *
1349*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1350*32b31808SJens Wiklander  * \param[out] user_len         User id length.
1351*32b31808SJens Wiklander  *
1352*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1353*32b31808SJens Wiklander  *         Success.
1354*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1355*32b31808SJens Wiklander  *         User id hasn't been set yet.
1356*32b31808SJens Wiklander  */
1357*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_user_len(
1358*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1359*32b31808SJens Wiklander     size_t *user_len);
1360*32b31808SJens Wiklander 
1361*32b31808SJens Wiklander /** Get the length of the peer id in bytes from given inputs.
1362*32b31808SJens Wiklander  *
1363*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1364*32b31808SJens Wiklander  * \param[out] peer_len         Peer id length.
1365*32b31808SJens Wiklander  *
1366*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1367*32b31808SJens Wiklander  *         Success.
1368*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1369*32b31808SJens Wiklander  *         Peer id hasn't been set yet.
1370*32b31808SJens Wiklander  */
1371*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_peer_len(
1372*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1373*32b31808SJens Wiklander     size_t *peer_len);
1374*32b31808SJens Wiklander 
1375*32b31808SJens Wiklander /** Get the user id from given inputs.
1376*32b31808SJens Wiklander  *
1377*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1378*32b31808SJens Wiklander  * \param[out] user_id          User id.
1379*32b31808SJens Wiklander  * \param      user_id_size     Size of \p user_id in bytes.
1380*32b31808SJens Wiklander  * \param[out] user_id_len      Size of the user id in bytes.
1381*32b31808SJens Wiklander  *
1382*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1383*32b31808SJens Wiklander  *         Success.
1384*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1385*32b31808SJens Wiklander  *         User id hasn't been set yet.
1386*32b31808SJens Wiklander  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1387*32b31808SJens Wiklander  *         The size of the \p user_id is too small.
1388*32b31808SJens Wiklander  */
1389*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_user(
1390*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1391*32b31808SJens Wiklander     uint8_t *user_id, size_t user_id_size, size_t *user_id_len);
1392*32b31808SJens Wiklander 
1393*32b31808SJens Wiklander /** Get the peer id from given inputs.
1394*32b31808SJens Wiklander  *
1395*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1396*32b31808SJens Wiklander  * \param[out] peer_id          Peer id.
1397*32b31808SJens Wiklander  * \param      peer_id_size     Size of \p peer_id in bytes.
1398*32b31808SJens Wiklander  * \param[out] peer_id_length   Size of the peer id in bytes.
1399*32b31808SJens Wiklander  *
1400*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1401*32b31808SJens Wiklander  *         Success.
1402*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1403*32b31808SJens Wiklander  *         Peer id hasn't been set yet.
1404*32b31808SJens Wiklander  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1405*32b31808SJens Wiklander  *         The size of the \p peer_id is too small.
1406*32b31808SJens Wiklander  */
1407*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_peer(
1408*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1409*32b31808SJens Wiklander     uint8_t *peer_id, size_t peer_id_size, size_t *peer_id_length);
1410*32b31808SJens Wiklander 
1411*32b31808SJens Wiklander /** Get the cipher suite from given inputs.
1412*32b31808SJens Wiklander  *
1413*32b31808SJens Wiklander  * \param[in]  inputs           Operation inputs.
1414*32b31808SJens Wiklander  * \param[out] cipher_suite     Return buffer for role.
1415*32b31808SJens Wiklander  *
1416*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1417*32b31808SJens Wiklander  *         Success.
1418*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1419*32b31808SJens Wiklander  *         Cipher_suite hasn't been set yet.
1420*32b31808SJens Wiklander  */
1421*32b31808SJens Wiklander psa_status_t psa_crypto_driver_pake_get_cipher_suite(
1422*32b31808SJens Wiklander     const psa_crypto_driver_pake_inputs_t *inputs,
1423*32b31808SJens Wiklander     psa_pake_cipher_suite_t *cipher_suite);
1424*32b31808SJens Wiklander 
1425*32b31808SJens Wiklander /** Set the session information for a password-authenticated key exchange.
1426*32b31808SJens Wiklander  *
1427*32b31808SJens Wiklander  * The sequence of operations to set up a password-authenticated key exchange
1428*32b31808SJens Wiklander  * is as follows:
1429*32b31808SJens Wiklander  * -# Allocate an operation object which will be passed to all the functions
1430*32b31808SJens Wiklander  *    listed here.
1431*32b31808SJens Wiklander  * -# Initialize the operation object with one of the methods described in the
1432*32b31808SJens Wiklander  *    documentation for #psa_pake_operation_t, e.g.
1433*32b31808SJens Wiklander  *    #PSA_PAKE_OPERATION_INIT.
1434*32b31808SJens Wiklander  * -# Call psa_pake_setup() to specify the cipher suite.
1435*32b31808SJens Wiklander  * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1436*32b31808SJens Wiklander  *    setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1437*32b31808SJens Wiklander  *    to be called depends on the algorithm in use.
1438*32b31808SJens Wiklander  *
1439*32b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1440*32b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1441*32b31808SJens Wiklander  * for more information.
1442*32b31808SJens Wiklander  *
1443*32b31808SJens Wiklander  * A typical sequence of calls to perform a password-authenticated key
1444*32b31808SJens Wiklander  * exchange:
1445*32b31808SJens Wiklander  * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1446*32b31808SJens Wiklander  *    key share that needs to be sent to the peer.
1447*32b31808SJens Wiklander  * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1448*32b31808SJens Wiklander  *    the key share that was received from the peer.
1449*32b31808SJens Wiklander  * -# Depending on the algorithm additional calls to psa_pake_output() and
1450*32b31808SJens Wiklander  *    psa_pake_input() might be necessary.
1451*32b31808SJens Wiklander  * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1452*32b31808SJens Wiklander  *
1453*32b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1454*32b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1455*32b31808SJens Wiklander  * for more information.
1456*32b31808SJens Wiklander  *
1457*32b31808SJens Wiklander  * If an error occurs at any step after a call to psa_pake_setup(),
1458*32b31808SJens Wiklander  * the operation will need to be reset by a call to psa_pake_abort(). The
1459*32b31808SJens Wiklander  * application may call psa_pake_abort() at any time after the operation
1460*32b31808SJens Wiklander  * has been initialized.
1461*32b31808SJens Wiklander  *
1462*32b31808SJens Wiklander  * After a successful call to psa_pake_setup(), the application must
1463*32b31808SJens Wiklander  * eventually terminate the operation. The following events terminate an
1464*32b31808SJens Wiklander  * operation:
1465*32b31808SJens Wiklander  * - A call to psa_pake_abort().
1466*32b31808SJens Wiklander  * - A successful call to psa_pake_get_implicit_key().
1467*32b31808SJens Wiklander  *
1468*32b31808SJens Wiklander  * \param[in,out] operation     The operation object to set up. It must have
1469*32b31808SJens Wiklander  *                              been initialized but not set up yet.
1470*32b31808SJens Wiklander  * \param[in] cipher_suite      The cipher suite to use. (A cipher suite fully
1471*32b31808SJens Wiklander  *                              characterizes a PAKE algorithm and determines
1472*32b31808SJens Wiklander  *                              the algorithm as well.)
1473*32b31808SJens Wiklander  *
1474*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1475*32b31808SJens Wiklander  *         Success.
1476*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1477*32b31808SJens Wiklander  *         The algorithm in \p cipher_suite is not a PAKE algorithm, or the
1478*32b31808SJens Wiklander  *         PAKE primitive in \p cipher_suite is not compatible with the
1479*32b31808SJens Wiklander  *         PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
1480*32b31808SJens Wiklander  *         or not compatible with the PAKE algorithm and primitive.
1481*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1482*32b31808SJens Wiklander  *         The algorithm in \p cipher_suite is not a supported PAKE algorithm,
1483*32b31808SJens Wiklander  *         or the PAKE primitive in \p cipher_suite is not supported or not
1484*32b31808SJens Wiklander  *         compatible with the PAKE algorithm, or the hash algorithm in
1485*32b31808SJens Wiklander  *         \p cipher_suite is not supported or not compatible with the PAKE
1486*32b31808SJens Wiklander  *         algorithm and primitive.
1487*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1488*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1489*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1490*32b31808SJens Wiklander  *         The operation state is not valid, or
1491*32b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
1492*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1493*32b31808SJens Wiklander  *         results in this error code.
1494*32b31808SJens Wiklander  */
1495*32b31808SJens Wiklander psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1496*32b31808SJens Wiklander                             const psa_pake_cipher_suite_t *cipher_suite);
1497*32b31808SJens Wiklander 
1498*32b31808SJens Wiklander /** Set the password for a password-authenticated key exchange from key ID.
1499*32b31808SJens Wiklander  *
1500*32b31808SJens Wiklander  * Call this function when the password, or a value derived from the password,
1501*32b31808SJens Wiklander  * is already present in the key store.
1502*32b31808SJens Wiklander  *
1503*32b31808SJens Wiklander  * \param[in,out] operation     The operation object to set the password for. It
1504*32b31808SJens Wiklander  *                              must have been set up by psa_pake_setup() and
1505*32b31808SJens Wiklander  *                              not yet in use (neither psa_pake_output() nor
1506*32b31808SJens Wiklander  *                              psa_pake_input() has been called yet). It must
1507*32b31808SJens Wiklander  *                              be on operation for which the password hasn't
1508*32b31808SJens Wiklander  *                              been set yet (psa_pake_set_password_key()
1509*32b31808SJens Wiklander  *                              hasn't been called yet).
1510*32b31808SJens Wiklander  * \param password              Identifier of the key holding the password or a
1511*32b31808SJens Wiklander  *                              value derived from the password (eg. by a
1512*32b31808SJens Wiklander  *                              memory-hard function).  It must remain valid
1513*32b31808SJens Wiklander  *                              until the operation terminates. It must be of
1514*32b31808SJens Wiklander  *                              type #PSA_KEY_TYPE_PASSWORD or
1515*32b31808SJens Wiklander  *                              #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1516*32b31808SJens Wiklander  *                              the usage #PSA_KEY_USAGE_DERIVE.
1517*32b31808SJens Wiklander  *
1518*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1519*32b31808SJens Wiklander  *         Success.
1520*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_HANDLE
1521*32b31808SJens Wiklander  *         \p password is not a valid key identifier.
1522*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_PERMITTED
1523*32b31808SJens Wiklander  *         The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
1524*32b31808SJens Wiklander  *         permit the \p operation's algorithm.
1525*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1526*32b31808SJens Wiklander  *         The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
1527*32b31808SJens Wiklander  *         #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
1528*32b31808SJens Wiklander  *         the \p operation's cipher suite.
1529*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1530*32b31808SJens Wiklander  *         The key type or key size of \p password is not supported with the
1531*32b31808SJens Wiklander  *         \p operation's cipher suite.
1532*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1533*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1534*32b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1535*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1536*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1537*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1538*32b31808SJens Wiklander  *         The operation state is not valid (it must have been set up.), or
1539*32b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
1540*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1541*32b31808SJens Wiklander  *         results in this error code.
1542*32b31808SJens Wiklander  */
1543*32b31808SJens Wiklander psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1544*32b31808SJens Wiklander                                        mbedtls_svc_key_id_t password);
1545*32b31808SJens Wiklander 
1546*32b31808SJens Wiklander /** Set the user ID for a password-authenticated key exchange.
1547*32b31808SJens Wiklander  *
1548*32b31808SJens Wiklander  * Call this function to set the user ID. For PAKE algorithms that associate a
1549*32b31808SJens Wiklander  * user identifier with each side of the session you need to call
1550*32b31808SJens Wiklander  * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1551*32b31808SJens Wiklander  * user identifier with the session, call psa_pake_set_user() only.
1552*32b31808SJens Wiklander  *
1553*32b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1554*32b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1555*32b31808SJens Wiklander  * for more information.
1556*32b31808SJens Wiklander  *
1557*32b31808SJens Wiklander  * \param[in,out] operation     The operation object to set the user ID for. It
1558*32b31808SJens Wiklander  *                              must have been set up by psa_pake_setup() and
1559*32b31808SJens Wiklander  *                              not yet in use (neither psa_pake_output() nor
1560*32b31808SJens Wiklander  *                              psa_pake_input() has been called yet). It must
1561*32b31808SJens Wiklander  *                              be on operation for which the user ID hasn't
1562*32b31808SJens Wiklander  *                              been set (psa_pake_set_user() hasn't been
1563*32b31808SJens Wiklander  *                              called yet).
1564*32b31808SJens Wiklander  * \param[in] user_id           The user ID to authenticate with.
1565*32b31808SJens Wiklander  *                              (temporary limitation: "client" or "server" only)
1566*32b31808SJens Wiklander  * \param user_id_len           Size of the \p user_id buffer in bytes.
1567*32b31808SJens Wiklander  *
1568*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1569*32b31808SJens Wiklander  *         Success.
1570*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1571*32b31808SJens Wiklander  *         \p user_id is not valid for the \p operation's algorithm and cipher
1572*32b31808SJens Wiklander  *         suite.
1573*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1574*32b31808SJens Wiklander  *         The value of \p user_id is not supported by the implementation.
1575*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1576*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1577*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1578*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1579*32b31808SJens Wiklander  *         The operation state is not valid, or
1580*32b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
1581*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1582*32b31808SJens Wiklander  *         results in this error code.
1583*32b31808SJens Wiklander  */
1584*32b31808SJens Wiklander psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1585*32b31808SJens Wiklander                                const uint8_t *user_id,
1586*32b31808SJens Wiklander                                size_t user_id_len);
1587*32b31808SJens Wiklander 
1588*32b31808SJens Wiklander /** Set the peer ID for a password-authenticated key exchange.
1589*32b31808SJens Wiklander  *
1590*32b31808SJens Wiklander  * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1591*32b31808SJens Wiklander  * that associate a user identifier with each side of the session. For PAKE
1592*32b31808SJens Wiklander  * algorithms that associate a single user identifier with the session, call
1593*32b31808SJens Wiklander  * psa_pake_set_user() only.
1594*32b31808SJens Wiklander  *
1595*32b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1596*32b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1597*32b31808SJens Wiklander  * for more information.
1598*32b31808SJens Wiklander  *
1599*32b31808SJens Wiklander  * \param[in,out] operation     The operation object to set the peer ID for. It
1600*32b31808SJens Wiklander  *                              must have been set up by psa_pake_setup() and
1601*32b31808SJens Wiklander  *                              not yet in use (neither psa_pake_output() nor
1602*32b31808SJens Wiklander  *                              psa_pake_input() has been called yet). It must
1603*32b31808SJens Wiklander  *                              be on operation for which the peer ID hasn't
1604*32b31808SJens Wiklander  *                              been set (psa_pake_set_peer() hasn't been
1605*32b31808SJens Wiklander  *                              called yet).
1606*32b31808SJens Wiklander  * \param[in] peer_id           The peer's ID to authenticate.
1607*32b31808SJens Wiklander  *                              (temporary limitation: "client" or "server" only)
1608*32b31808SJens Wiklander  * \param peer_id_len           Size of the \p peer_id buffer in bytes.
1609*32b31808SJens Wiklander  *
1610*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1611*32b31808SJens Wiklander  *         Success.
1612*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1613*32b31808SJens Wiklander  *         \p user_id is not valid for the \p operation's algorithm and cipher
1614*32b31808SJens Wiklander  *         suite.
1615*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1616*32b31808SJens Wiklander  *         The algorithm doesn't associate a second identity with the session.
1617*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1618*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1619*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1620*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1621*32b31808SJens Wiklander  *         Calling psa_pake_set_peer() is invalid with the \p operation's
1622*32b31808SJens Wiklander  *         algorithm, the operation state is not valid, or the library has not
1623*32b31808SJens Wiklander  *         been previously initialized by psa_crypto_init().
1624*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1625*32b31808SJens Wiklander  *         results in this error code.
1626*32b31808SJens Wiklander  */
1627*32b31808SJens Wiklander psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1628*32b31808SJens Wiklander                                const uint8_t *peer_id,
1629*32b31808SJens Wiklander                                size_t peer_id_len);
1630*32b31808SJens Wiklander 
1631*32b31808SJens Wiklander /** Set the application role for a password-authenticated key exchange.
1632*32b31808SJens Wiklander  *
1633*32b31808SJens Wiklander  * Not all PAKE algorithms need to differentiate the communicating entities.
1634*32b31808SJens Wiklander  * It is optional to call this function for PAKEs that don't require a role
1635*32b31808SJens Wiklander  * to be specified. For such PAKEs the application role parameter is ignored,
1636*32b31808SJens Wiklander  * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
1637*32b31808SJens Wiklander  *
1638*32b31808SJens Wiklander  * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1639*32b31808SJens Wiklander  * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1640*32b31808SJens Wiklander  * for more information.
1641*32b31808SJens Wiklander  *
1642*32b31808SJens Wiklander  * \param[in,out] operation     The operation object to specify the
1643*32b31808SJens Wiklander  *                              application's role for. It must have been set up
1644*32b31808SJens Wiklander  *                              by psa_pake_setup() and not yet in use (neither
1645*32b31808SJens Wiklander  *                              psa_pake_output() nor psa_pake_input() has been
1646*32b31808SJens Wiklander  *                              called yet). It must be on operation for which
1647*32b31808SJens Wiklander  *                              the application's role hasn't been specified
1648*32b31808SJens Wiklander  *                              (psa_pake_set_role() hasn't been called yet).
1649*32b31808SJens Wiklander  * \param role                  A value of type ::psa_pake_role_t indicating the
1650*32b31808SJens Wiklander  *                              application's role in the PAKE the algorithm
1651*32b31808SJens Wiklander  *                              that is being set up. For more information see
1652*32b31808SJens Wiklander  *                              the documentation of \c PSA_PAKE_ROLE_XXX
1653*32b31808SJens Wiklander  *                              constants.
1654*32b31808SJens Wiklander  *
1655*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1656*32b31808SJens Wiklander  *         Success.
1657*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1658*32b31808SJens Wiklander  *         The \p role is not a valid PAKE role in the \p operation’s algorithm.
1659*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1660*32b31808SJens Wiklander  *         The \p role for this algorithm is not supported or is not valid.
1661*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1662*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1663*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1664*32b31808SJens Wiklander  *         The operation state is not valid, or
1665*32b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
1666*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1667*32b31808SJens Wiklander  *         results in this error code.
1668*32b31808SJens Wiklander  */
1669*32b31808SJens Wiklander psa_status_t psa_pake_set_role(psa_pake_operation_t *operation,
1670*32b31808SJens Wiklander                                psa_pake_role_t role);
1671*32b31808SJens Wiklander 
1672*32b31808SJens Wiklander /** Get output for a step of a password-authenticated key exchange.
1673*32b31808SJens Wiklander  *
1674*32b31808SJens Wiklander  * Depending on the algorithm being executed, you might need to call this
1675*32b31808SJens Wiklander  * function several times or you might not need to call this at all.
1676*32b31808SJens Wiklander  *
1677*32b31808SJens Wiklander  * The exact sequence of calls to perform a password-authenticated key
1678*32b31808SJens Wiklander  * exchange depends on the algorithm in use.  Refer to the documentation of
1679*32b31808SJens Wiklander  * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1680*32b31808SJens Wiklander  * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1681*32b31808SJens Wiklander  * information.
1682*32b31808SJens Wiklander  *
1683*32b31808SJens Wiklander  * If this function returns an error status, the operation enters an error
1684*32b31808SJens Wiklander  * state and must be aborted by calling psa_pake_abort().
1685*32b31808SJens Wiklander  *
1686*32b31808SJens Wiklander  * \param[in,out] operation    Active PAKE operation.
1687*32b31808SJens Wiklander  * \param step                 The step of the algorithm for which the output is
1688*32b31808SJens Wiklander  *                             requested.
1689*32b31808SJens Wiklander  * \param[out] output          Buffer where the output is to be written in the
1690*32b31808SJens Wiklander  *                             format appropriate for this \p step. Refer to
1691*32b31808SJens Wiklander  *                             the documentation of the individual
1692*32b31808SJens Wiklander  *                             \c PSA_PAKE_STEP_XXX constants for more
1693*32b31808SJens Wiklander  *                             information.
1694*32b31808SJens Wiklander  * \param output_size          Size of the \p output buffer in bytes. This must
1695*32b31808SJens Wiklander  *                             be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p
1696*32b31808SJens Wiklander  *                             primitive, \p step) where \p alg and
1697*32b31808SJens Wiklander  *                             \p primitive are the PAKE algorithm and primitive
1698*32b31808SJens Wiklander  *                             in the operation's cipher suite, and \p step is
1699*32b31808SJens Wiklander  *                             the output step.
1700*32b31808SJens Wiklander  *
1701*32b31808SJens Wiklander  * \param[out] output_length   On success, the number of bytes of the returned
1702*32b31808SJens Wiklander  *                             output.
1703*32b31808SJens Wiklander  *
1704*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1705*32b31808SJens Wiklander  *         Success.
1706*32b31808SJens Wiklander  * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1707*32b31808SJens Wiklander  *         The size of the \p output buffer is too small.
1708*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1709*32b31808SJens Wiklander  *         \p step is not compatible with the operation's algorithm.
1710*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1711*32b31808SJens Wiklander  *         \p step is not supported with the operation's algorithm.
1712*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
1713*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1714*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1715*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1716*32b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1717*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1718*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1719*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1720*32b31808SJens Wiklander  *         The operation state is not valid (it must be active, and fully set
1721*32b31808SJens Wiklander  *         up, and this call must conform to the algorithm's requirements
1722*32b31808SJens Wiklander  *         for ordering of input and output steps), or
1723*32b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
1724*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1725*32b31808SJens Wiklander  *         results in this error code.
1726*32b31808SJens Wiklander  */
1727*32b31808SJens Wiklander psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1728*32b31808SJens Wiklander                              psa_pake_step_t step,
1729*32b31808SJens Wiklander                              uint8_t *output,
1730*32b31808SJens Wiklander                              size_t output_size,
1731*32b31808SJens Wiklander                              size_t *output_length);
1732*32b31808SJens Wiklander 
1733*32b31808SJens Wiklander /** Provide input for a step of a password-authenticated key exchange.
1734*32b31808SJens Wiklander  *
1735*32b31808SJens Wiklander  * Depending on the algorithm being executed, you might need to call this
1736*32b31808SJens Wiklander  * function several times or you might not need to call this at all.
1737*32b31808SJens Wiklander  *
1738*32b31808SJens Wiklander  * The exact sequence of calls to perform a password-authenticated key
1739*32b31808SJens Wiklander  * exchange depends on the algorithm in use.  Refer to the documentation of
1740*32b31808SJens Wiklander  * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1741*32b31808SJens Wiklander  * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1742*32b31808SJens Wiklander  * information.
1743*32b31808SJens Wiklander  *
1744*32b31808SJens Wiklander  * If this function returns an error status, the operation enters an error
1745*32b31808SJens Wiklander  * state and must be aborted by calling psa_pake_abort().
1746*32b31808SJens Wiklander  *
1747*32b31808SJens Wiklander  * \param[in,out] operation    Active PAKE operation.
1748*32b31808SJens Wiklander  * \param step                 The step for which the input is provided.
1749*32b31808SJens Wiklander  * \param[in] input            Buffer containing the input in the format
1750*32b31808SJens Wiklander  *                             appropriate for this \p step. Refer to the
1751*32b31808SJens Wiklander  *                             documentation of the individual
1752*32b31808SJens Wiklander  *                             \c PSA_PAKE_STEP_XXX constants for more
1753*32b31808SJens Wiklander  *                             information.
1754*32b31808SJens Wiklander  * \param input_length         Size of the \p input buffer in bytes.
1755*32b31808SJens Wiklander  *
1756*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1757*32b31808SJens Wiklander  *         Success.
1758*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_SIGNATURE
1759*32b31808SJens Wiklander  *         The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
1760*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1761*32b31808SJens Wiklander  *         \p is not compatible with the \p operation’s algorithm, or the
1762*32b31808SJens Wiklander  *         \p input is not valid for the \p operation's algorithm, cipher suite
1763*32b31808SJens Wiklander  *         or \p step.
1764*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1765*32b31808SJens Wiklander  *         \p step p is not supported with the \p operation's algorithm, or the
1766*32b31808SJens Wiklander  *         \p input is not supported for the \p operation's algorithm, cipher
1767*32b31808SJens Wiklander  *         suite or \p step.
1768*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1769*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1770*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1771*32b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1772*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1773*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1774*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1775*32b31808SJens Wiklander  *         The operation state is not valid (it must be active, and fully set
1776*32b31808SJens Wiklander  *         up, and this call must conform to the algorithm's requirements
1777*32b31808SJens Wiklander  *         for ordering of input and output steps), or
1778*32b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init().
1779*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1780*32b31808SJens Wiklander  *         results in this error code.
1781*32b31808SJens Wiklander  */
1782*32b31808SJens Wiklander psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1783*32b31808SJens Wiklander                             psa_pake_step_t step,
1784*32b31808SJens Wiklander                             const uint8_t *input,
1785*32b31808SJens Wiklander                             size_t input_length);
1786*32b31808SJens Wiklander 
1787*32b31808SJens Wiklander /** Get implicitly confirmed shared secret from a PAKE.
1788*32b31808SJens Wiklander  *
1789*32b31808SJens Wiklander  * At this point there is a cryptographic guarantee that only the authenticated
1790*32b31808SJens Wiklander  * party who used the same password is able to compute the key. But there is no
1791*32b31808SJens Wiklander  * guarantee that the peer is the party it claims to be and was able to do so.
1792*32b31808SJens Wiklander  *
1793*32b31808SJens Wiklander  * That is, the authentication is only implicit. Since the peer is not
1794*32b31808SJens Wiklander  * authenticated yet, no action should be taken yet that assumes that the peer
1795*32b31808SJens Wiklander  * is who it claims to be. For example, do not access restricted files on the
1796*32b31808SJens Wiklander  * peer's behalf until an explicit authentication has succeeded.
1797*32b31808SJens Wiklander  *
1798*32b31808SJens Wiklander  * This function can be called after the key exchange phase of the operation
1799*32b31808SJens Wiklander  * has completed. It imports the shared secret output of the PAKE into the
1800*32b31808SJens Wiklander  * provided derivation operation. The input step
1801*32b31808SJens Wiklander  * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1802*32b31808SJens Wiklander  * material in the key derivation operation.
1803*32b31808SJens Wiklander  *
1804*32b31808SJens Wiklander  * The exact sequence of calls to perform a password-authenticated key
1805*32b31808SJens Wiklander  * exchange depends on the algorithm in use.  Refer to the documentation of
1806*32b31808SJens Wiklander  * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1807*32b31808SJens Wiklander  * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1808*32b31808SJens Wiklander  * information.
1809*32b31808SJens Wiklander  *
1810*32b31808SJens Wiklander  * When this function returns successfully, \p operation becomes inactive.
1811*32b31808SJens Wiklander  * If this function returns an error status, both \p operation
1812*32b31808SJens Wiklander  * and \p key_derivation operations enter an error state and must be aborted by
1813*32b31808SJens Wiklander  * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1814*32b31808SJens Wiklander  *
1815*32b31808SJens Wiklander  * \param[in,out] operation    Active PAKE operation.
1816*32b31808SJens Wiklander  * \param[out] output          A key derivation operation that is ready
1817*32b31808SJens Wiklander  *                             for an input step of type
1818*32b31808SJens Wiklander  *                             #PSA_KEY_DERIVATION_INPUT_SECRET.
1819*32b31808SJens Wiklander  *
1820*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1821*32b31808SJens Wiklander  *         Success.
1822*32b31808SJens Wiklander  * \retval #PSA_ERROR_INVALID_ARGUMENT
1823*32b31808SJens Wiklander  *         #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
1824*32b31808SJens Wiklander  *         algorithm in the \p output key derivation operation.
1825*32b31808SJens Wiklander  * \retval #PSA_ERROR_NOT_SUPPORTED
1826*32b31808SJens Wiklander  *         Input from a PAKE is not supported by the algorithm in the \p output
1827*32b31808SJens Wiklander  *         key derivation operation.
1828*32b31808SJens Wiklander  * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
1829*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1830*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1831*32b31808SJens Wiklander  * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
1832*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
1833*32b31808SJens Wiklander  * \retval #PSA_ERROR_DATA_INVALID \emptydescription
1834*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1835*32b31808SJens Wiklander  *         The PAKE operation state is not valid (it must be active, but beyond
1836*32b31808SJens Wiklander  *         that validity is specific to the algorithm), or
1837*32b31808SJens Wiklander  *         the library has not been previously initialized by psa_crypto_init(),
1838*32b31808SJens Wiklander  *         or the state of \p output is not valid for
1839*32b31808SJens Wiklander  *         the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1840*32b31808SJens Wiklander  *         step is out of order or the application has done this step already
1841*32b31808SJens Wiklander  *         and it may not be repeated.
1842*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1843*32b31808SJens Wiklander  *         results in this error code.
1844*32b31808SJens Wiklander  */
1845*32b31808SJens Wiklander psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1846*32b31808SJens Wiklander                                        psa_key_derivation_operation_t *output);
1847*32b31808SJens Wiklander 
1848*32b31808SJens Wiklander /** Abort a PAKE operation.
1849*32b31808SJens Wiklander  *
1850*32b31808SJens Wiklander  * Aborting an operation frees all associated resources except for the \c
1851*32b31808SJens Wiklander  * operation structure itself. Once aborted, the operation object can be reused
1852*32b31808SJens Wiklander  * for another operation by calling psa_pake_setup() again.
1853*32b31808SJens Wiklander  *
1854*32b31808SJens Wiklander  * This function may be called at any time after the operation
1855*32b31808SJens Wiklander  * object has been initialized as described in #psa_pake_operation_t.
1856*32b31808SJens Wiklander  *
1857*32b31808SJens Wiklander  * In particular, calling psa_pake_abort() after the operation has been
1858*32b31808SJens Wiklander  * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
1859*32b31808SJens Wiklander  * is safe and has no effect.
1860*32b31808SJens Wiklander  *
1861*32b31808SJens Wiklander  * \param[in,out] operation    The operation to abort.
1862*32b31808SJens Wiklander  *
1863*32b31808SJens Wiklander  * \retval #PSA_SUCCESS
1864*32b31808SJens Wiklander  *         Success.
1865*32b31808SJens Wiklander  * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
1866*32b31808SJens Wiklander  * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
1867*32b31808SJens Wiklander  * \retval #PSA_ERROR_BAD_STATE
1868*32b31808SJens Wiklander  *         The library has not been previously initialized by psa_crypto_init().
1869*32b31808SJens Wiklander  *         It is implementation-dependent whether a failure to initialize
1870*32b31808SJens Wiklander  *         results in this error code.
1871*32b31808SJens Wiklander  */
1872*32b31808SJens Wiklander psa_status_t psa_pake_abort(psa_pake_operation_t *operation);
1873*32b31808SJens Wiklander 
1874*32b31808SJens Wiklander /**@}*/
1875*32b31808SJens Wiklander 
1876*32b31808SJens Wiklander /** A sufficient output buffer size for psa_pake_output().
1877*32b31808SJens Wiklander  *
1878*32b31808SJens Wiklander  * If the size of the output buffer is at least this large, it is guaranteed
1879*32b31808SJens Wiklander  * that psa_pake_output() will not fail due to an insufficient output buffer
1880*32b31808SJens Wiklander  * size. The actual size of the output might be smaller in any given call.
1881*32b31808SJens Wiklander  *
1882*32b31808SJens Wiklander  * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1883*32b31808SJens Wiklander  *
1884*32b31808SJens Wiklander  * \param alg           A PAKE algorithm (\c PSA_ALG_XXX value such that
1885*32b31808SJens Wiklander  *                      #PSA_ALG_IS_PAKE(\p alg) is true).
1886*32b31808SJens Wiklander  * \param primitive     A primitive of type ::psa_pake_primitive_t that is
1887*32b31808SJens Wiklander  *                      compatible with algorithm \p alg.
1888*32b31808SJens Wiklander  * \param output_step   A value of type ::psa_pake_step_t that is valid for the
1889*32b31808SJens Wiklander  *                      algorithm \p alg.
1890*32b31808SJens Wiklander  * \return              A sufficient output buffer size for the specified
1891*32b31808SJens Wiklander  *                      PAKE algorithm, primitive, and output step. If the
1892*32b31808SJens Wiklander  *                      PAKE algorithm, primitive, or output step is not
1893*32b31808SJens Wiklander  *                      recognized, or the parameters are incompatible,
1894*32b31808SJens Wiklander  *                      return 0.
1895*32b31808SJens Wiklander  */
1896*32b31808SJens Wiklander #define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step)               \
1897*32b31808SJens Wiklander     (alg == PSA_ALG_JPAKE &&                                           \
1898*32b31808SJens Wiklander      primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,      \
1899*32b31808SJens Wiklander                                      PSA_ECC_FAMILY_SECP_R1, 256) ?    \
1900*32b31808SJens Wiklander      (                                                                 \
1901*32b31808SJens Wiklander          output_step == PSA_PAKE_STEP_KEY_SHARE ? 65 :                   \
1902*32b31808SJens Wiklander          output_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 :                   \
1903*32b31808SJens Wiklander          32                                                              \
1904*32b31808SJens Wiklander      ) :                                                               \
1905*32b31808SJens Wiklander      0)
1906*32b31808SJens Wiklander 
1907*32b31808SJens Wiklander /** A sufficient input buffer size for psa_pake_input().
1908*32b31808SJens Wiklander  *
1909*32b31808SJens Wiklander  * The value returned by this macro is guaranteed to be large enough for any
1910*32b31808SJens Wiklander  * valid input to psa_pake_input() in an operation with the specified
1911*32b31808SJens Wiklander  * parameters.
1912*32b31808SJens Wiklander  *
1913*32b31808SJens Wiklander  * See also #PSA_PAKE_INPUT_MAX_SIZE
1914*32b31808SJens Wiklander  *
1915*32b31808SJens Wiklander  * \param alg           A PAKE algorithm (\c PSA_ALG_XXX value such that
1916*32b31808SJens Wiklander  *                      #PSA_ALG_IS_PAKE(\p alg) is true).
1917*32b31808SJens Wiklander  * \param primitive     A primitive of type ::psa_pake_primitive_t that is
1918*32b31808SJens Wiklander  *                      compatible with algorithm \p alg.
1919*32b31808SJens Wiklander  * \param input_step    A value of type ::psa_pake_step_t that is valid for the
1920*32b31808SJens Wiklander  *                      algorithm \p alg.
1921*32b31808SJens Wiklander  * \return              A sufficient input buffer size for the specified
1922*32b31808SJens Wiklander  *                      input, cipher suite and algorithm. If the cipher suite,
1923*32b31808SJens Wiklander  *                      the input type or PAKE algorithm is not recognized, or
1924*32b31808SJens Wiklander  *                      the parameters are incompatible, return 0.
1925*32b31808SJens Wiklander  */
1926*32b31808SJens Wiklander #define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step)                 \
1927*32b31808SJens Wiklander     (alg == PSA_ALG_JPAKE &&                                           \
1928*32b31808SJens Wiklander      primitive == PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,      \
1929*32b31808SJens Wiklander                                      PSA_ECC_FAMILY_SECP_R1, 256) ?    \
1930*32b31808SJens Wiklander      (                                                                 \
1931*32b31808SJens Wiklander          input_step == PSA_PAKE_STEP_KEY_SHARE ? 65 :                    \
1932*32b31808SJens Wiklander          input_step == PSA_PAKE_STEP_ZK_PUBLIC ? 65 :                    \
1933*32b31808SJens Wiklander          32                                                              \
1934*32b31808SJens Wiklander      ) :                                                               \
1935*32b31808SJens Wiklander      0)
1936*32b31808SJens Wiklander 
1937*32b31808SJens Wiklander /** Output buffer size for psa_pake_output() for any of the supported PAKE
1938*32b31808SJens Wiklander  * algorithm and primitive suites and output step.
1939*32b31808SJens Wiklander  *
1940*32b31808SJens Wiklander  * This macro must expand to a compile-time constant integer.
1941*32b31808SJens Wiklander  *
1942*32b31808SJens Wiklander  * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p primitive, \p step).
1943*32b31808SJens Wiklander  */
1944*32b31808SJens Wiklander #define PSA_PAKE_OUTPUT_MAX_SIZE 65
1945*32b31808SJens Wiklander 
1946*32b31808SJens Wiklander /** Input buffer size for psa_pake_input() for any of the supported PAKE
1947*32b31808SJens Wiklander  * algorithm and primitive suites and input step.
1948*32b31808SJens Wiklander  *
1949*32b31808SJens Wiklander  * This macro must expand to a compile-time constant integer.
1950*32b31808SJens Wiklander  *
1951*32b31808SJens Wiklander  * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p primitive, \p step).
1952*32b31808SJens Wiklander  */
1953*32b31808SJens Wiklander #define PSA_PAKE_INPUT_MAX_SIZE 65
1954*32b31808SJens Wiklander 
1955*32b31808SJens Wiklander /** Returns a suitable initializer for a PAKE cipher suite object of type
1956*32b31808SJens Wiklander  * psa_pake_cipher_suite_t.
1957*32b31808SJens Wiklander  */
1958*32b31808SJens Wiklander #define PSA_PAKE_CIPHER_SUITE_INIT { PSA_ALG_NONE, 0, 0, 0, PSA_ALG_NONE }
1959*32b31808SJens Wiklander 
1960*32b31808SJens Wiklander /** Returns a suitable initializer for a PAKE operation object of type
1961*32b31808SJens Wiklander  * psa_pake_operation_t.
1962*32b31808SJens Wiklander  */
1963*32b31808SJens Wiklander #define PSA_PAKE_OPERATION_INIT { 0, PSA_ALG_NONE, PSA_PAKE_OPERATION_STAGE_SETUP, \
1964*32b31808SJens Wiklander                                   { 0 }, { { 0 } } }
1965*32b31808SJens Wiklander 
1966*32b31808SJens Wiklander struct psa_pake_cipher_suite_s {
1967*32b31808SJens Wiklander     psa_algorithm_t algorithm;
1968*32b31808SJens Wiklander     psa_pake_primitive_type_t type;
1969*32b31808SJens Wiklander     psa_pake_family_t family;
1970*32b31808SJens Wiklander     uint16_t  bits;
1971*32b31808SJens Wiklander     psa_algorithm_t hash;
1972*32b31808SJens Wiklander };
1973*32b31808SJens Wiklander 
1974*32b31808SJens Wiklander static inline psa_algorithm_t psa_pake_cs_get_algorithm(
1975*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
1976*32b31808SJens Wiklander {
1977*32b31808SJens Wiklander     return cipher_suite->algorithm;
1978*32b31808SJens Wiklander }
1979*32b31808SJens Wiklander 
1980*32b31808SJens Wiklander static inline void psa_pake_cs_set_algorithm(
1981*32b31808SJens Wiklander     psa_pake_cipher_suite_t *cipher_suite,
1982*32b31808SJens Wiklander     psa_algorithm_t algorithm)
1983*32b31808SJens Wiklander {
1984*32b31808SJens Wiklander     if (!PSA_ALG_IS_PAKE(algorithm)) {
1985*32b31808SJens Wiklander         cipher_suite->algorithm = 0;
1986*32b31808SJens Wiklander     } else {
1987*32b31808SJens Wiklander         cipher_suite->algorithm = algorithm;
1988*32b31808SJens Wiklander     }
1989*32b31808SJens Wiklander }
1990*32b31808SJens Wiklander 
1991*32b31808SJens Wiklander static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
1992*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
1993*32b31808SJens Wiklander {
1994*32b31808SJens Wiklander     return PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1995*32b31808SJens Wiklander                               cipher_suite->bits);
1996*32b31808SJens Wiklander }
1997*32b31808SJens Wiklander 
1998*32b31808SJens Wiklander static inline void psa_pake_cs_set_primitive(
1999*32b31808SJens Wiklander     psa_pake_cipher_suite_t *cipher_suite,
2000*32b31808SJens Wiklander     psa_pake_primitive_t primitive)
2001*32b31808SJens Wiklander {
2002*32b31808SJens Wiklander     cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
2003*32b31808SJens Wiklander     cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
2004*32b31808SJens Wiklander     cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
2005*32b31808SJens Wiklander }
2006*32b31808SJens Wiklander 
2007*32b31808SJens Wiklander static inline psa_pake_family_t psa_pake_cs_get_family(
2008*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
2009*32b31808SJens Wiklander {
2010*32b31808SJens Wiklander     return cipher_suite->family;
2011*32b31808SJens Wiklander }
2012*32b31808SJens Wiklander 
2013*32b31808SJens Wiklander static inline uint16_t psa_pake_cs_get_bits(
2014*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
2015*32b31808SJens Wiklander {
2016*32b31808SJens Wiklander     return cipher_suite->bits;
2017*32b31808SJens Wiklander }
2018*32b31808SJens Wiklander 
2019*32b31808SJens Wiklander static inline psa_algorithm_t psa_pake_cs_get_hash(
2020*32b31808SJens Wiklander     const psa_pake_cipher_suite_t *cipher_suite)
2021*32b31808SJens Wiklander {
2022*32b31808SJens Wiklander     return cipher_suite->hash;
2023*32b31808SJens Wiklander }
2024*32b31808SJens Wiklander 
2025*32b31808SJens Wiklander static inline void psa_pake_cs_set_hash(psa_pake_cipher_suite_t *cipher_suite,
2026*32b31808SJens Wiklander                                         psa_algorithm_t hash)
2027*32b31808SJens Wiklander {
2028*32b31808SJens Wiklander     if (!PSA_ALG_IS_HASH(hash)) {
2029*32b31808SJens Wiklander         cipher_suite->hash = 0;
2030*32b31808SJens Wiklander     } else {
2031*32b31808SJens Wiklander         cipher_suite->hash = hash;
2032*32b31808SJens Wiklander     }
2033*32b31808SJens Wiklander }
2034*32b31808SJens Wiklander 
2035*32b31808SJens Wiklander struct psa_crypto_driver_pake_inputs_s {
2036*32b31808SJens Wiklander     uint8_t *MBEDTLS_PRIVATE(password);
2037*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(password_len);
2038*32b31808SJens Wiklander     psa_pake_role_t MBEDTLS_PRIVATE(role);
2039*32b31808SJens Wiklander     uint8_t *MBEDTLS_PRIVATE(user);
2040*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(user_len);
2041*32b31808SJens Wiklander     uint8_t *MBEDTLS_PRIVATE(peer);
2042*32b31808SJens Wiklander     size_t MBEDTLS_PRIVATE(peer_len);
2043*32b31808SJens Wiklander     psa_key_attributes_t MBEDTLS_PRIVATE(attributes);
2044*32b31808SJens Wiklander     psa_pake_cipher_suite_t MBEDTLS_PRIVATE(cipher_suite);
2045*32b31808SJens Wiklander };
2046*32b31808SJens Wiklander 
2047*32b31808SJens Wiklander typedef enum psa_jpake_step {
2048*32b31808SJens Wiklander     PSA_PAKE_STEP_INVALID       = 0,
2049*32b31808SJens Wiklander     PSA_PAKE_STEP_X1_X2         = 1,
2050*32b31808SJens Wiklander     PSA_PAKE_STEP_X2S           = 2,
2051*32b31808SJens Wiklander     PSA_PAKE_STEP_DERIVE        = 3,
2052*32b31808SJens Wiklander } psa_jpake_step_t;
2053*32b31808SJens Wiklander 
2054*32b31808SJens Wiklander typedef enum psa_jpake_state {
2055*32b31808SJens Wiklander     PSA_PAKE_STATE_INVALID      = 0,
2056*32b31808SJens Wiklander     PSA_PAKE_STATE_SETUP        = 1,
2057*32b31808SJens Wiklander     PSA_PAKE_STATE_READY        = 2,
2058*32b31808SJens Wiklander     PSA_PAKE_OUTPUT_X1_X2       = 3,
2059*32b31808SJens Wiklander     PSA_PAKE_OUTPUT_X2S         = 4,
2060*32b31808SJens Wiklander     PSA_PAKE_INPUT_X1_X2        = 5,
2061*32b31808SJens Wiklander     PSA_PAKE_INPUT_X4S          = 6,
2062*32b31808SJens Wiklander } psa_jpake_state_t;
2063*32b31808SJens Wiklander 
2064*32b31808SJens Wiklander typedef enum psa_jpake_sequence {
2065*32b31808SJens Wiklander     PSA_PAKE_SEQ_INVALID        = 0,
2066*32b31808SJens Wiklander     PSA_PAKE_X1_STEP_KEY_SHARE  = 1,    /* also X2S & X4S KEY_SHARE */
2067*32b31808SJens Wiklander     PSA_PAKE_X1_STEP_ZK_PUBLIC  = 2,    /* also X2S & X4S ZK_PUBLIC */
2068*32b31808SJens Wiklander     PSA_PAKE_X1_STEP_ZK_PROOF   = 3,    /* also X2S & X4S ZK_PROOF */
2069*32b31808SJens Wiklander     PSA_PAKE_X2_STEP_KEY_SHARE  = 4,
2070*32b31808SJens Wiklander     PSA_PAKE_X2_STEP_ZK_PUBLIC  = 5,
2071*32b31808SJens Wiklander     PSA_PAKE_X2_STEP_ZK_PROOF   = 6,
2072*32b31808SJens Wiklander     PSA_PAKE_SEQ_END            = 7,
2073*32b31808SJens Wiklander } psa_jpake_sequence_t;
2074*32b31808SJens Wiklander 
2075*32b31808SJens Wiklander typedef enum psa_crypto_driver_pake_step {
2076*32b31808SJens Wiklander     PSA_JPAKE_STEP_INVALID        = 0,  /* Invalid step */
2077*32b31808SJens Wiklander     PSA_JPAKE_X1_STEP_KEY_SHARE   = 1,  /* Round 1: input/output key share (for ephemeral private key X1).*/
2078*32b31808SJens Wiklander     PSA_JPAKE_X1_STEP_ZK_PUBLIC   = 2,  /* Round 1: input/output Schnorr NIZKP public key for the X1 key */
2079*32b31808SJens Wiklander     PSA_JPAKE_X1_STEP_ZK_PROOF    = 3,  /* Round 1: input/output Schnorr NIZKP proof for the X1 key */
2080*32b31808SJens Wiklander     PSA_JPAKE_X2_STEP_KEY_SHARE   = 4,  /* Round 1: input/output key share (for ephemeral private key X2).*/
2081*32b31808SJens Wiklander     PSA_JPAKE_X2_STEP_ZK_PUBLIC   = 5,  /* Round 1: input/output Schnorr NIZKP public key for the X2 key */
2082*32b31808SJens Wiklander     PSA_JPAKE_X2_STEP_ZK_PROOF    = 6,  /* Round 1: input/output Schnorr NIZKP proof for the X2 key */
2083*32b31808SJens Wiklander     PSA_JPAKE_X2S_STEP_KEY_SHARE  = 7,  /* Round 2: output X2S key (our key) */
2084*32b31808SJens Wiklander     PSA_JPAKE_X2S_STEP_ZK_PUBLIC  = 8,  /* Round 2: output Schnorr NIZKP public key for the X2S key (our key) */
2085*32b31808SJens Wiklander     PSA_JPAKE_X2S_STEP_ZK_PROOF   = 9,  /* Round 2: output Schnorr NIZKP proof for the X2S key (our key) */
2086*32b31808SJens Wiklander     PSA_JPAKE_X4S_STEP_KEY_SHARE  = 10, /* Round 2: input X4S key (from peer) */
2087*32b31808SJens Wiklander     PSA_JPAKE_X4S_STEP_ZK_PUBLIC  = 11, /* Round 2: input Schnorr NIZKP public key for the X4S key (from peer) */
2088*32b31808SJens Wiklander     PSA_JPAKE_X4S_STEP_ZK_PROOF   = 12  /* Round 2: input Schnorr NIZKP proof for the X4S key (from peer) */
2089*32b31808SJens Wiklander } psa_crypto_driver_pake_step_t;
2090*32b31808SJens Wiklander 
2091*32b31808SJens Wiklander 
2092*32b31808SJens Wiklander struct psa_jpake_computation_stage_s {
2093*32b31808SJens Wiklander     psa_jpake_state_t MBEDTLS_PRIVATE(state);
2094*32b31808SJens Wiklander     psa_jpake_sequence_t MBEDTLS_PRIVATE(sequence);
2095*32b31808SJens Wiklander     psa_jpake_step_t MBEDTLS_PRIVATE(input_step);
2096*32b31808SJens Wiklander     psa_jpake_step_t MBEDTLS_PRIVATE(output_step);
2097*32b31808SJens Wiklander };
2098*32b31808SJens Wiklander 
2099*32b31808SJens Wiklander struct psa_pake_operation_s {
2100*32b31808SJens Wiklander     /** Unique ID indicating which driver got assigned to do the
2101*32b31808SJens Wiklander      * operation. Since driver contexts are driver-specific, swapping
2102*32b31808SJens Wiklander      * drivers halfway through the operation is not supported.
2103*32b31808SJens Wiklander      * ID values are auto-generated in psa_crypto_driver_wrappers.h
2104*32b31808SJens Wiklander      * ID value zero means the context is not valid or not assigned to
2105*32b31808SJens Wiklander      * any driver (i.e. none of the driver contexts are active). */
2106*32b31808SJens Wiklander     unsigned int MBEDTLS_PRIVATE(id);
2107*32b31808SJens Wiklander     /* Algorithm of the PAKE operation */
2108*32b31808SJens Wiklander     psa_algorithm_t MBEDTLS_PRIVATE(alg);
2109*32b31808SJens Wiklander     /* Stage of the PAKE operation: waiting for the setup, collecting inputs
2110*32b31808SJens Wiklander      * or computing. */
2111*32b31808SJens Wiklander     uint8_t MBEDTLS_PRIVATE(stage);
2112*32b31808SJens Wiklander     /* Holds computation stage of the PAKE algorithms. */
2113*32b31808SJens Wiklander     union {
2114*32b31808SJens Wiklander         uint8_t MBEDTLS_PRIVATE(dummy);
2115*32b31808SJens Wiklander #if defined(PSA_WANT_ALG_JPAKE)
2116*32b31808SJens Wiklander         psa_jpake_computation_stage_t MBEDTLS_PRIVATE(jpake);
2117*32b31808SJens Wiklander #endif
2118*32b31808SJens Wiklander     } MBEDTLS_PRIVATE(computation_stage);
2119*32b31808SJens Wiklander     union {
2120*32b31808SJens Wiklander         psa_driver_pake_context_t MBEDTLS_PRIVATE(ctx);
2121*32b31808SJens Wiklander         psa_crypto_driver_pake_inputs_t MBEDTLS_PRIVATE(inputs);
2122*32b31808SJens Wiklander     } MBEDTLS_PRIVATE(data);
2123*32b31808SJens Wiklander };
2124*32b31808SJens Wiklander 
2125*32b31808SJens Wiklander static inline struct psa_pake_cipher_suite_s psa_pake_cipher_suite_init(void)
2126*32b31808SJens Wiklander {
2127*32b31808SJens Wiklander     const struct psa_pake_cipher_suite_s v = PSA_PAKE_CIPHER_SUITE_INIT;
2128*32b31808SJens Wiklander     return v;
2129*32b31808SJens Wiklander }
2130*32b31808SJens Wiklander 
2131*32b31808SJens Wiklander static inline struct psa_pake_operation_s psa_pake_operation_init(void)
2132*32b31808SJens Wiklander {
2133*32b31808SJens Wiklander     const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
2134*32b31808SJens Wiklander     return v;
2135*32b31808SJens Wiklander }
2136*32b31808SJens Wiklander 
2137*32b31808SJens Wiklander #ifdef __cplusplus
2138*32b31808SJens Wiklander }
2139*32b31808SJens Wiklander #endif
2140*32b31808SJens Wiklander 
2141*32b31808SJens Wiklander #endif /* PSA_CRYPTO_EXTRA_H */
2142