blob: 4478f3a43cac47e1ce4d755636c69bbc099f3e8f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/**
2 * \file psa/crypto_extra.h
3 *
4 * \brief PSA cryptography module: Mbed TLS vendor extensions
Gilles Peskine07c91f52018-06-28 18:02:53 +02005 *
6 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
9 * This file is reserved for vendor-specific definitions.
Gilles Peskinee59236f2018-01-27 23:32:46 +010010 */
11/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020012 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +010013 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010026 */
27
28#ifndef PSA_CRYPTO_EXTRA_H
29#define PSA_CRYPTO_EXTRA_H
30
Jaeden Amero81cefed2019-02-25 08:51:27 +000031#include "mbedtls/platform_util.h"
32
Gilles Peskine7a894f22019-11-26 16:06:46 +010033#include "crypto_compat.h"
34
Gilles Peskinee59236f2018-01-27 23:32:46 +010035#ifdef __cplusplus
36extern "C" {
37#endif
38
Netanel Gonen2bcd3122018-11-19 11:53:02 +020039/* UID for secure storage seed */
avolinski0d2c2662018-11-21 17:31:07 +020040#define PSA_CRYPTO_ITS_RANDOM_SEED_UID 0xFFFFFF52
Netanel Gonen2bcd3122018-11-19 11:53:02 +020041
Steven Cooreman1f968fd2021-02-15 14:00:24 +010042/* See config.h for definition */
Steven Cooreman863470a2021-02-15 14:03:19 +010043#if !defined(MBEDTLS_PSA_KEY_SLOT_COUNT)
44#define MBEDTLS_PSA_KEY_SLOT_COUNT 32
Steven Cooreman1f968fd2021-02-15 14:00:24 +010045#endif
Jaeden Amero5e6d24c2019-02-21 10:41:29 +000046
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020047/** \addtogroup attributes
48 * @{
49 */
50
51/** \brief Declare the enrollment algorithm for a key.
52 *
53 * An operation on a key may indifferently use the algorithm set with
54 * psa_set_key_algorithm() or with this function.
55 *
56 * \param[out] attributes The attribute structure to write to.
57 * \param alg2 A second algorithm that the key may be used
58 * for, in addition to the algorithm set with
59 * psa_set_key_algorithm().
60 *
61 * \warning Setting an enrollment algorithm is not recommended, because
62 * using the same key with different algorithms can allow some
63 * attacks based on arithmetic relations between different
64 * computations made with the same key, or can escalate harmless
65 * side channels into exploitable ones. Use this function only
Gilles Peskinef25c9ec2019-05-22 11:45:59 +020066 * if it is necessary to support a protocol for which it has been
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020067 * verified that the usage of the key with multiple algorithms
68 * is safe.
69 */
70static inline void psa_set_key_enrollment_algorithm(
71 psa_key_attributes_t *attributes,
72 psa_algorithm_t alg2)
73{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020074 attributes->core.policy.alg2 = alg2;
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020075}
76
77/** Retrieve the enrollment algorithm policy from key attributes.
78 *
79 * \param[in] attributes The key attribute structure to query.
80 *
81 * \return The enrollment algorithm stored in the attribute structure.
82 */
83static inline psa_algorithm_t psa_get_key_enrollment_algorithm(
84 const psa_key_attributes_t *attributes)
85{
Gilles Peskine7e0cff92019-07-30 13:48:52 +020086 return( attributes->core.policy.alg2 );
Gilles Peskine96f0b3b2019-05-10 19:33:38 +020087}
88
Gilles Peskinec8000c02019-08-02 20:15:51 +020089#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
90
91/** Retrieve the slot number where a key is stored.
92 *
93 * A slot number is only defined for keys that are stored in a secure
94 * element.
95 *
96 * This information is only useful if the secure element is not entirely
97 * managed through the PSA Cryptography API. It is up to the secure
98 * element driver to decide how PSA slot numbers map to any other interface
99 * that the secure element may have.
100 *
101 * \param[in] attributes The key attribute structure to query.
102 * \param[out] slot_number On success, the slot number containing the key.
103 *
104 * \retval #PSA_SUCCESS
105 * The key is located in a secure element, and \p *slot_number
106 * indicates the slot number that contains it.
107 * \retval #PSA_ERROR_NOT_PERMITTED
108 * The caller is not permitted to query the slot number.
109 * Mbed Crypto currently does not return this error.
110 * \retval #PSA_ERROR_INVALID_ARGUMENT
111 * The key is not located in a secure element.
112 */
113psa_status_t psa_get_key_slot_number(
114 const psa_key_attributes_t *attributes,
115 psa_key_slot_number_t *slot_number );
116
117/** Choose the slot number where a key is stored.
118 *
119 * This function declares a slot number in the specified attribute
120 * structure.
121 *
122 * A slot number is only meaningful for keys that are stored in a secure
123 * element. It is up to the secure element driver to decide how PSA slot
124 * numbers map to any other interface that the secure element may have.
125 *
126 * \note Setting a slot number in key attributes for a key creation can
127 * cause the following errors when creating the key:
128 * - #PSA_ERROR_NOT_SUPPORTED if the selected secure element does
129 * not support choosing a specific slot number.
130 * - #PSA_ERROR_NOT_PERMITTED if the caller is not permitted to
131 * choose slot numbers in general or to choose this specific slot.
132 * - #PSA_ERROR_INVALID_ARGUMENT if the chosen slot number is not
133 * valid in general or not valid for this specific key.
134 * - #PSA_ERROR_ALREADY_EXISTS if there is already a key in the
135 * selected slot.
136 *
137 * \param[out] attributes The attribute structure to write to.
138 * \param slot_number The slot number to set.
139 */
140static inline void psa_set_key_slot_number(
141 psa_key_attributes_t *attributes,
142 psa_key_slot_number_t slot_number )
143{
144 attributes->core.flags |= MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
145 attributes->slot_number = slot_number;
146}
147
Gilles Peskine5fe5e272019-08-02 20:30:01 +0200148/** Remove the slot number attribute from a key attribute structure.
149 *
150 * This function undoes the action of psa_set_key_slot_number().
151 *
152 * \param[out] attributes The attribute structure to write to.
153 */
154static inline void psa_clear_key_slot_number(
155 psa_key_attributes_t *attributes )
156{
157 attributes->core.flags &= ~MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER;
158}
159
Gilles Peskined7729582019-08-05 15:55:54 +0200160/** Register a key that is already present in a secure element.
161 *
162 * The key must be located in a secure element designated by the
163 * lifetime field in \p attributes, in the slot set with
164 * psa_set_key_slot_number() in the attribute structure.
165 * This function makes the key available through the key identifier
166 * specified in \p attributes.
167 *
168 * \param[in] attributes The attributes of the existing key.
169 *
170 * \retval #PSA_SUCCESS
171 * The key was successfully registered.
172 * Note that depending on the design of the driver, this may or may
173 * not guarantee that a key actually exists in the designated slot
174 * and is compatible with the specified attributes.
175 * \retval #PSA_ERROR_ALREADY_EXISTS
176 * There is already a key with the identifier specified in
177 * \p attributes.
Gilles Peskine3efcebb2019-10-01 14:18:35 +0200178 * \retval #PSA_ERROR_NOT_SUPPORTED
179 * The secure element driver for the specified lifetime does not
180 * support registering a key.
Gilles Peskined7729582019-08-05 15:55:54 +0200181 * \retval #PSA_ERROR_INVALID_ARGUMENT
Ronald Crond3b458c2021-03-31 17:51:29 +0200182 * The identifier in \p attributes is invalid, namely the identifier is
183 * not in the user range.
184 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskined7729582019-08-05 15:55:54 +0200185 * \p attributes specifies a lifetime which is not located
186 * in a secure element.
187 * \retval #PSA_ERROR_INVALID_ARGUMENT
188 * No slot number is specified in \p attributes,
189 * or the specified slot number is not valid.
190 * \retval #PSA_ERROR_NOT_PERMITTED
191 * The caller is not authorized to register the specified key slot.
192 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100193 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
Gilles Peskined7729582019-08-05 15:55:54 +0200194 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +0100195 * \retval #PSA_ERROR_DATA_INVALID
196 * \retval #PSA_ERROR_DATA_CORRUPT
Gilles Peskined7729582019-08-05 15:55:54 +0200197 * \retval #PSA_ERROR_CORRUPTION_DETECTED
198 * \retval #PSA_ERROR_BAD_STATE
199 * The library has not been previously initialized by psa_crypto_init().
200 * It is implementation-dependent whether a failure to initialize
201 * results in this error code.
202 */
203psa_status_t mbedtls_psa_register_se_key(
204 const psa_key_attributes_t *attributes);
205
Gilles Peskinec8000c02019-08-02 20:15:51 +0200206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
207
Gilles Peskine96f0b3b2019-05-10 19:33:38 +0200208/**@}*/
209
Gilles Peskinee59236f2018-01-27 23:32:46 +0100210/**
211 * \brief Library deinitialization.
212 *
213 * This function clears all data associated with the PSA layer,
214 * including the whole key store.
215 *
216 * This is an Mbed TLS extension.
217 */
218void mbedtls_psa_crypto_free( void );
219
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200220/** \brief Statistics about
221 * resource consumption related to the PSA keystore.
222 *
223 * \note The content of this structure is not part of the stable API and ABI
224 * of Mbed Crypto and may change arbitrarily from version to version.
225 */
226typedef struct mbedtls_psa_stats_s
227{
228 /** Number of slots containing key material for a volatile key. */
229 size_t volatile_slots;
230 /** Number of slots containing key material for a key which is in
231 * internal persistent storage. */
232 size_t persistent_slots;
233 /** Number of slots containing a reference to a key in a
234 * secure element. */
235 size_t external_slots;
236 /** Number of slots which are occupied, but do not contain
237 * key material yet. */
238 size_t half_filled_slots;
239 /** Number of slots that contain cache data. */
240 size_t cache_slots;
241 /** Number of slots that are not used for anything. */
242 size_t empty_slots;
Ronald Cron1ad1eee2020-11-15 14:21:04 +0100243 /** Number of slots that are locked. */
244 size_t locked_slots;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200245 /** Largest key id value among open keys in internal persistent storage. */
Ronald Cron039a98b2020-07-23 16:07:42 +0200246 psa_key_id_t max_open_internal_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200247 /** Largest key id value among open keys in secure elements. */
Ronald Cron039a98b2020-07-23 16:07:42 +0200248 psa_key_id_t max_open_external_key_id;
Gilles Peskine4bac9a42019-05-23 20:32:30 +0200249} mbedtls_psa_stats_t;
250
251/** \brief Get statistics about
252 * resource consumption related to the PSA keystore.
253 *
254 * \note When Mbed Crypto is built as part of a service, with isolation
255 * between the application and the keystore, the service may or
256 * may not expose this function.
257 */
258void mbedtls_psa_get_stats( mbedtls_psa_stats_t *stats );
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200259
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200260/**
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100261 * \brief Inject an initial entropy seed for the random generator into
262 * secure storage.
Gilles Peskine0338ded2018-11-15 18:19:27 +0100263 *
264 * This function injects data to be used as a seed for the random generator
265 * used by the PSA Crypto implementation. On devices that lack a trusted
266 * entropy source (preferably a hardware random number generator),
267 * the Mbed PSA Crypto implementation uses this value to seed its
268 * random generator.
269 *
270 * On devices without a trusted entropy source, this function must be
271 * called exactly once in the lifetime of the device. On devices with
272 * a trusted entropy source, calling this function is optional.
273 * In all cases, this function may only be called before calling any
274 * other function in the PSA Crypto API, including psa_crypto_init().
275 *
276 * When this function returns successfully, it populates a file in
277 * persistent storage. Once the file has been created, this function
278 * can no longer succeed.
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100279 *
280 * If any error occurs, this function does not change the system state.
281 * You can call this function again after correcting the reason for the
282 * error if possible.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200283 *
284 * \warning This function **can** fail! Callers MUST check the return status.
285 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100286 * \warning If you use this function, you should use it as part of a
287 * factory provisioning process. The value of the injected seed
288 * is critical to the security of the device. It must be
289 * *secret*, *unpredictable* and (statistically) *unique per device*.
290 * You should be generate it randomly using a cryptographically
291 * secure random generator seeded from trusted entropy sources.
292 * You should transmit it securely to the device and ensure
293 * that its value is not leaked or stored anywhere beyond the
294 * needs of transmitting it from the point of generation to
295 * the call of this function, and erase all copies of the value
296 * once this function returns.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200297 *
Gilles Peskine0338ded2018-11-15 18:19:27 +0100298 * This is an Mbed TLS extension.
299 *
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200300 * \note This function is only available on the following platforms:
Gilles Peskinee3dbdd82019-02-25 11:04:06 +0100301 * * If the compile-time option MBEDTLS_PSA_INJECT_ENTROPY is enabled.
302 * Note that you must provide compatible implementations of
303 * mbedtls_nv_seed_read and mbedtls_nv_seed_write.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200304 * * In a client-server integration of PSA Cryptography, on the client side,
Netanel Gonen1d7195f2018-11-22 16:24:48 +0200305 * if the server supports this feature.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200306 * \param[in] seed Buffer containing the seed value to inject.
Gilles Peskine0cfaed12018-11-22 17:11:45 +0200307 * \param[in] seed_size Size of the \p seed buffer.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200308 * The size of the seed in bytes must be greater
Chris Jones3848e312021-03-11 16:17:59 +0000309 * or equal to both #MBEDTLS_ENTROPY_BLOCK_SIZE
310 * and the value of \c MBEDTLS_ENTROPY_MIN_PLATFORM
311 * in `library/entropy_poll.h` in the Mbed TLS source
312 * code.
Netanel Gonen596e65e2018-11-22 18:41:43 +0200313 * It must be less or equal to
314 * #MBEDTLS_ENTROPY_MAX_SEED_SIZE.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200315 *
316 * \retval #PSA_SUCCESS
Gilles Peskine0338ded2018-11-15 18:19:27 +0100317 * The seed value was injected successfully. The random generator
318 * of the PSA Crypto implementation is now ready for use.
319 * You may now call psa_crypto_init() and use the PSA Crypto
320 * implementation.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200321 * \retval #PSA_ERROR_INVALID_ARGUMENT
Gilles Peskineee2ffd32018-11-16 11:02:49 +0100322 * \p seed_size is out of range.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200323 * \retval #PSA_ERROR_STORAGE_FAILURE
Gilles Peskine0338ded2018-11-15 18:19:27 +0100324 * There was a failure reading or writing from storage.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200325 * \retval #PSA_ERROR_NOT_PERMITTED
Gilles Peskine0338ded2018-11-15 18:19:27 +0100326 * The library has already been initialized. It is no longer
327 * possible to call this function.
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200328 */
Jaeden Ameroc7529c92019-08-19 11:08:04 +0100329psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +0200330 size_t seed_size);
331
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200332/** \addtogroup crypto_types
333 * @{
334 */
335
Gilles Peskinea1302192019-05-16 13:58:24 +0200336/** DSA public key.
337 *
338 * The import and export format is the
339 * representation of the public key `y = g^x mod p` as a big-endian byte
340 * string. The length of the byte string is the length of the base prime `p`
341 * in bytes.
342 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100343#define PSA_KEY_TYPE_DSA_PUBLIC_KEY ((psa_key_type_t)0x4002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200344
345/** DSA key pair (private and public key).
346 *
347 * The import and export format is the
348 * representation of the private key `x` as a big-endian byte string. The
349 * length of the byte string is the private key size in bytes (leading zeroes
350 * are not stripped).
351 *
352 * Determinstic DSA key derivation with psa_generate_derived_key follows
353 * FIPS 186-4 §B.1.2: interpret the byte string as integer
354 * in big-endian order. Discard it if it is not in the range
355 * [0, *N* - 2] where *N* is the boundary of the private key domain
356 * (the prime *p* for Diffie-Hellman, the subprime *q* for DSA,
357 * or the order of the curve's base point for ECC).
358 * Add 1 to the resulting integer and use this as the private key *x*.
359 *
360 */
Gilles Peskine7cfcb3f2019-12-04 18:58:44 +0100361#define PSA_KEY_TYPE_DSA_KEY_PAIR ((psa_key_type_t)0x7002)
Gilles Peskinea1302192019-05-16 13:58:24 +0200362
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200363/** Whether a key type is an DSA key (pair or public-only). */
364#define PSA_KEY_TYPE_IS_DSA(type) \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200365 (PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200366
Bence Szépkútia2945512020-12-03 21:40:17 +0100367#define PSA_ALG_DSA_BASE ((psa_algorithm_t)0x06000400)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200368/** DSA signature with hashing.
369 *
370 * This is the signature scheme defined by FIPS 186-4,
371 * with a random per-message secret number (*k*).
372 *
373 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
374 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
375 * This includes #PSA_ALG_ANY_HASH
376 * when specifying the algorithm in a usage policy.
377 *
378 * \return The corresponding DSA signature algorithm.
379 * \return Unspecified if \p hash_alg is not a supported
380 * hash algorithm.
381 */
382#define PSA_ALG_DSA(hash_alg) \
383 (PSA_ALG_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
Bence Szépkútia2945512020-12-03 21:40:17 +0100384#define PSA_ALG_DETERMINISTIC_DSA_BASE ((psa_algorithm_t)0x06000500)
Gilles Peskine972630e2019-11-29 11:55:48 +0100385#define PSA_ALG_DSA_DETERMINISTIC_FLAG PSA_ALG_ECDSA_DETERMINISTIC_FLAG
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200386/** Deterministic DSA signature with hashing.
387 *
388 * This is the deterministic variant defined by RFC 6979 of
389 * the signature scheme defined by FIPS 186-4.
390 *
391 * \param hash_alg A hash algorithm (\c PSA_ALG_XXX value such that
392 * #PSA_ALG_IS_HASH(\p hash_alg) is true).
393 * This includes #PSA_ALG_ANY_HASH
394 * when specifying the algorithm in a usage policy.
395 *
396 * \return The corresponding DSA signature algorithm.
397 * \return Unspecified if \p hash_alg is not a supported
398 * hash algorithm.
399 */
400#define PSA_ALG_DETERMINISTIC_DSA(hash_alg) \
401 (PSA_ALG_DETERMINISTIC_DSA_BASE | ((hash_alg) & PSA_ALG_HASH_MASK))
402#define PSA_ALG_IS_DSA(alg) \
403 (((alg) & ~PSA_ALG_HASH_MASK & ~PSA_ALG_DSA_DETERMINISTIC_FLAG) == \
404 PSA_ALG_DSA_BASE)
405#define PSA_ALG_DSA_IS_DETERMINISTIC(alg) \
406 (((alg) & PSA_ALG_DSA_DETERMINISTIC_FLAG) != 0)
407#define PSA_ALG_IS_DETERMINISTIC_DSA(alg) \
408 (PSA_ALG_IS_DSA(alg) && PSA_ALG_DSA_IS_DETERMINISTIC(alg))
409#define PSA_ALG_IS_RANDOMIZED_DSA(alg) \
410 (PSA_ALG_IS_DSA(alg) && !PSA_ALG_DSA_IS_DETERMINISTIC(alg))
411
412
413/* We need to expand the sample definition of this macro from
414 * the API definition. */
Gilles Peskine6d400852021-02-24 21:39:52 +0100415#undef PSA_ALG_IS_VENDOR_HASH_AND_SIGN
416#define PSA_ALG_IS_VENDOR_HASH_AND_SIGN(alg) \
417 PSA_ALG_IS_DSA(alg)
Gilles Peskinee38ab1a2019-05-16 13:51:50 +0200418
419/**@}*/
420
Gilles Peskine24f10f82019-05-16 12:18:32 +0200421/** \addtogroup attributes
422 * @{
423 */
424
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200425/** Custom Diffie-Hellman group.
426 *
Paul Elliott75e27032020-06-03 15:17:39 +0100427 * For keys of type #PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
428 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM), the group data comes
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200429 * from domain parameters set by psa_set_key_domain_parameters().
430 */
Paul Elliott75e27032020-06-03 15:17:39 +0100431#define PSA_DH_FAMILY_CUSTOM ((psa_dh_family_t) 0x7e)
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200432
433
Gilles Peskine24f10f82019-05-16 12:18:32 +0200434/**
435 * \brief Set domain parameters for a key.
436 *
437 * Some key types require additional domain parameters in addition to
438 * the key type identifier and the key size. Use this function instead
439 * of psa_set_key_type() when you need to specify domain parameters.
440 *
441 * The format for the required domain parameters varies based on the key type.
442 *
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200443 * - For RSA keys (#PSA_KEY_TYPE_RSA_PUBLIC_KEY or #PSA_KEY_TYPE_RSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200444 * the domain parameter data consists of the public exponent,
445 * represented as a big-endian integer with no leading zeros.
446 * This information is used when generating an RSA key pair.
447 * When importing a key, the public exponent is read from the imported
448 * key data and the exponent recorded in the attribute structure is ignored.
449 * As an exception, the public exponent 65537 is represented by an empty
450 * byte string.
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200451 * - For DSA keys (#PSA_KEY_TYPE_DSA_PUBLIC_KEY or #PSA_KEY_TYPE_DSA_KEY_PAIR),
Gilles Peskine24f10f82019-05-16 12:18:32 +0200452 * the `Dss-Parms` format as defined by RFC 3279 §2.3.2.
453 * ```
454 * Dss-Parms ::= SEQUENCE {
455 * p INTEGER,
456 * q INTEGER,
457 * g INTEGER
458 * }
459 * ```
Gilles Peskinedcaefae2019-05-16 12:55:35 +0200460 * - For Diffie-Hellman key exchange keys
Paul Elliott75e27032020-06-03 15:17:39 +0100461 * (#PSA_KEY_TYPE_DH_PUBLIC_KEY(#PSA_DH_FAMILY_CUSTOM) or
462 * #PSA_KEY_TYPE_DH_KEY_PAIR(#PSA_DH_FAMILY_CUSTOM)), the
Gilles Peskine24f10f82019-05-16 12:18:32 +0200463 * `DomainParameters` format as defined by RFC 3279 §2.3.3.
464 * ```
465 * DomainParameters ::= SEQUENCE {
466 * p INTEGER, -- odd prime, p=jq +1
467 * g INTEGER, -- generator, g
468 * q INTEGER, -- factor of p-1
469 * j INTEGER OPTIONAL, -- subgroup factor
470 * validationParms ValidationParms OPTIONAL
471 * }
472 * ValidationParms ::= SEQUENCE {
473 * seed BIT STRING,
474 * pgenCounter INTEGER
475 * }
476 * ```
477 *
478 * \note This function may allocate memory or other resources.
479 * Once you have called this function on an attribute structure,
480 * you must call psa_reset_key_attributes() to free these resources.
481 *
482 * \note This is an experimental extension to the interface. It may change
483 * in future versions of the library.
484 *
485 * \param[in,out] attributes Attribute structure where the specified domain
486 * parameters will be stored.
487 * If this function fails, the content of
488 * \p attributes is not modified.
489 * \param type Key type (a \c PSA_KEY_TYPE_XXX value).
490 * \param[in] data Buffer containing the key domain parameters.
491 * The content of this buffer is interpreted
492 * according to \p type as described above.
493 * \param data_length Size of the \p data buffer in bytes.
494 *
495 * \retval #PSA_SUCCESS
496 * \retval #PSA_ERROR_INVALID_ARGUMENT
497 * \retval #PSA_ERROR_NOT_SUPPORTED
498 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
499 */
500psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
501 psa_key_type_t type,
502 const uint8_t *data,
503 size_t data_length);
504
505/**
506 * \brief Get domain parameters for a key.
507 *
508 * Get the domain parameters for a key with this function, if any. The format
509 * of the domain parameters written to \p data is specified in the
510 * documentation for psa_set_key_domain_parameters().
511 *
512 * \note This is an experimental extension to the interface. It may change
513 * in future versions of the library.
514 *
515 * \param[in] attributes The key attribute structure to query.
516 * \param[out] data On success, the key domain parameters.
517 * \param data_size Size of the \p data buffer in bytes.
518 * The buffer is guaranteed to be large
519 * enough if its size in bytes is at least
520 * the value given by
521 * PSA_KEY_DOMAIN_PARAMETERS_SIZE().
522 * \param[out] data_length On success, the number of bytes
523 * that make up the key domain parameters data.
524 *
525 * \retval #PSA_SUCCESS
526 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
527 */
528psa_status_t psa_get_key_domain_parameters(
529 const psa_key_attributes_t *attributes,
530 uint8_t *data,
531 size_t data_size,
532 size_t *data_length);
533
534/** Safe output buffer size for psa_get_key_domain_parameters().
535 *
536 * This macro returns a compile-time constant if its arguments are
537 * compile-time constants.
538 *
539 * \warning This function may call its arguments multiple times or
540 * zero times, so you should not pass arguments that contain
541 * side effects.
542 *
543 * \note This is an experimental extension to the interface. It may change
544 * in future versions of the library.
545 *
546 * \param key_type A supported key type.
547 * \param key_bits The size of the key in bits.
548 *
549 * \return If the parameters are valid and supported, return
550 * a buffer size in bytes that guarantees that
551 * psa_get_key_domain_parameters() will not fail with
552 * #PSA_ERROR_BUFFER_TOO_SMALL.
553 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200554 * by the implementation, this macro shall return either a
Gilles Peskine24f10f82019-05-16 12:18:32 +0200555 * sensible size or 0.
556 * If the parameters are not valid, the
557 * return value is unspecified.
558 */
559#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
560 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
561 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
562 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
563 0)
564#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
565 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
566#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
567 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
568
569/**@}*/
570
Gilles Peskine5055b232019-12-12 17:49:31 +0100571/** \defgroup psa_tls_helpers TLS helper functions
572 * @{
573 */
574
575#if defined(MBEDTLS_ECP_C)
576#include <mbedtls/ecp.h>
577
578/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
579 *
580 * \note This function is provided solely for the convenience of
581 * Mbed TLS and may be removed at any time without notice.
582 *
583 * \param grpid An Mbed TLS elliptic curve identifier
584 * (`MBEDTLS_ECP_DP_xxx`).
585 * \param[out] bits On success, the bit size of the curve.
586 *
587 * \return The corresponding PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100588 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine5055b232019-12-12 17:49:31 +0100589 * \return \c 0 on failure (\p grpid is not recognized).
590 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100591static inline psa_ecc_family_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid,
Darryl Green2f0eb512020-04-24 15:21:14 +0100592 size_t *bits )
593{
594 switch( grpid )
595 {
596 case MBEDTLS_ECP_DP_SECP192R1:
597 *bits = 192;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100598 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100599 case MBEDTLS_ECP_DP_SECP224R1:
600 *bits = 224;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100601 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100602 case MBEDTLS_ECP_DP_SECP256R1:
603 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100604 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100605 case MBEDTLS_ECP_DP_SECP384R1:
606 *bits = 384;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100607 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100608 case MBEDTLS_ECP_DP_SECP521R1:
609 *bits = 521;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100610 return( PSA_ECC_FAMILY_SECP_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100611 case MBEDTLS_ECP_DP_BP256R1:
612 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100613 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100614 case MBEDTLS_ECP_DP_BP384R1:
615 *bits = 384;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100616 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100617 case MBEDTLS_ECP_DP_BP512R1:
618 *bits = 512;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100619 return( PSA_ECC_FAMILY_BRAINPOOL_P_R1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100620 case MBEDTLS_ECP_DP_CURVE25519:
621 *bits = 255;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100622 return( PSA_ECC_FAMILY_MONTGOMERY );
Darryl Green2f0eb512020-04-24 15:21:14 +0100623 case MBEDTLS_ECP_DP_SECP192K1:
624 *bits = 192;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100625 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100626 case MBEDTLS_ECP_DP_SECP224K1:
627 *bits = 224;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100628 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100629 case MBEDTLS_ECP_DP_SECP256K1:
630 *bits = 256;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100631 return( PSA_ECC_FAMILY_SECP_K1 );
Darryl Green2f0eb512020-04-24 15:21:14 +0100632 case MBEDTLS_ECP_DP_CURVE448:
633 *bits = 448;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100634 return( PSA_ECC_FAMILY_MONTGOMERY );
Darryl Green2f0eb512020-04-24 15:21:14 +0100635 default:
636 *bits = 0;
637 return( 0 );
638 }
639}
Gilles Peskine5055b232019-12-12 17:49:31 +0100640
641/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
642 *
643 * \note This function is provided solely for the convenience of
644 * Mbed TLS and may be removed at any time without notice.
645 *
646 * \param curve A PSA elliptic curve identifier
Paul Elliott8ff510a2020-06-02 17:19:28 +0100647 * (`PSA_ECC_FAMILY_xxx`).
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100648 * \param bits The bit-length of a private key on \p curve.
649 * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up
650 * to the nearest multiple of 8. This allows the caller
651 * to infer the exact curve from the length of a key
652 * which is supplied as a byte string.
Gilles Peskine5055b232019-12-12 17:49:31 +0100653 *
654 * \return The corresponding Mbed TLS elliptic curve identifier
655 * (`MBEDTLS_ECP_DP_xxx`).
656 * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100657 * \return #MBEDTLS_ECP_DP_NONE if \p bits is not
Gilles Peskine5055b232019-12-12 17:49:31 +0100658 * correct for \p curve.
659 */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100660mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100661 size_t bits,
662 int bits_is_sloppy );
Gilles Peskine5055b232019-12-12 17:49:31 +0100663#endif /* MBEDTLS_ECP_C */
664
665/**@}*/
666
Gilles Peskineb8af2282020-11-13 18:00:34 +0100667/** \defgroup psa_external_rng External random generator
668 * @{
669 */
670
671#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
672/** External random generator function, implemented by the platform.
673 *
674 * When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
675 * this function replaces Mbed TLS's entropy and DRBG modules for all
676 * random generation triggered via PSA crypto interfaces.
677 *
Gilles Peskineb663a602020-11-18 15:27:37 +0100678 * \note This random generator must deliver random numbers with cryptographic
679 * quality and high performance. It must supply unpredictable numbers
680 * with a uniform distribution. The implementation of this function
681 * is responsible for ensuring that the random generator is seeded
682 * with sufficient entropy. If you have a hardware TRNG which is slow
683 * or delivers non-uniform output, declare it as an entropy source
684 * with mbedtls_entropy_add_source() instead of enabling this option.
685 *
Gilles Peskineb8af2282020-11-13 18:00:34 +0100686 * \param[in,out] context Pointer to the random generator context.
687 * This is all-bits-zero on the first call
688 * and preserved between successive calls.
689 * \param[out] output Output buffer. On success, this buffer
690 * contains random data with a uniform
691 * distribution.
692 * \param output_size The size of the \p output buffer in bytes.
693 * \param[out] output_length On success, set this value to \p output_size.
694 *
695 * \retval #PSA_SUCCESS
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100696 * Success. The output buffer contains \p output_size bytes of
697 * cryptographic-quality random data, and \c *output_length is
698 * set to \p output_size.
699 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
700 * The random generator requires extra entropy and there is no
701 * way to obtain entropy under current environment conditions.
702 * This error should not happen under normal circumstances since
703 * this function is responsible for obtaining as much entropy as
704 * it needs. However implementations of this function may return
705 * #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
706 * entropy without blocking indefinitely.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100707 * \retval #PSA_ERROR_HARDWARE_FAILURE
Gilles Peskinee995b9b2020-11-30 12:08:00 +0100708 * A failure of the random generator hardware that isn't covered
709 * by #PSA_ERROR_INSUFFICIENT_ENTROPY.
Gilles Peskineb8af2282020-11-13 18:00:34 +0100710 */
711psa_status_t mbedtls_psa_external_get_random(
712 mbedtls_psa_external_random_context_t *context,
713 uint8_t *output, size_t output_size, size_t *output_length );
714#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
715
716/**@}*/
717
Steven Cooreman6801f082021-02-19 17:21:22 +0100718/** \defgroup psa_builtin_keys Built-in keys
719 * @{
720 */
721
722/** The minimum value for a key identifier that is built into the
723 * implementation.
724 *
725 * The range of key identifiers from #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN
726 * to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX within the range from
727 * #PSA_KEY_ID_VENDOR_MIN and #PSA_KEY_ID_VENDOR_MAX and must not intersect
728 * with any other set of implementation-chosen key identifiers.
729 *
730 * This value is part of the library's ABI since changing it would invalidate
731 * the values of built-in key identifiers in applications.
732 */
733#define MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ((psa_key_id_t)0x7fff0000)
734
735/** The maximum value for a key identifier that is built into the
736 * implementation.
737 *
738 * See #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN for more information.
739 */
740#define MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ((psa_key_id_t)0x7fffefff)
741
742/** A slot number identifying a key in a driver.
743 *
744 * Values of this type are used to identify built-in keys.
745 */
746typedef uint64_t psa_drv_slot_number_t;
747
748#if defined(MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS)
749/** Test whether a key identifier belongs to the builtin key range.
750 *
751 * \param key_id Key identifier to test.
752 *
753 * \retval 1
754 * The key identifier is a builtin key identifier.
755 * \retval 0
756 * The key identifier is not a builtin key identifier.
757 */
758static inline int psa_key_id_is_builtin( psa_key_id_t key_id )
759{
760 return( ( key_id >= MBEDTLS_PSA_KEY_ID_BUILTIN_MIN ) &&
761 ( key_id <= MBEDTLS_PSA_KEY_ID_BUILTIN_MAX ) );
762}
763
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200764/** Platform function to obtain the location and slot number of a built-in key.
Steven Cooreman6801f082021-02-19 17:21:22 +0100765 *
766 * An application-specific implementation of this function must be provided if
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100767 * #MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS is enabled. This would typically be provided
Steven Cooreman6801f082021-02-19 17:21:22 +0100768 * as part of a platform's system image.
769 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100770 * #MBEDTLS_SVC_KEY_ID_GET_KEY_ID(\p key_id) needs to be in the range from
Steven Cooreman6801f082021-02-19 17:21:22 +0100771 * #MBEDTLS_PSA_KEY_ID_BUILTIN_MIN to #MBEDTLS_PSA_KEY_ID_BUILTIN_MAX.
772 *
773 * In a multi-application configuration
774 * (\c MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER is defined),
775 * this function should check that #MBEDTLS_SVC_KEY_ID_GET_OWNER_ID(\p key_id)
776 * is allowed to use the given key.
777 *
Steven Cooremanc8b95342021-03-18 20:48:06 +0100778 * \param key_id The key ID for which to retrieve the
779 * location and slot attributes.
780 * \param[out] lifetime On success, the lifetime associated with the key
781 * corresponding to \p key_id. Lifetime is a
782 * combination of which driver contains the key,
Steven Cooreman31e27af2021-04-14 10:32:05 +0200783 * and with what persistence level the key is
784 * intended to be used. If the platform
785 * implementation does not contain specific
786 * information about the intended key persistence
787 * level, the persistence level may be reported as
788 * #PSA_KEY_PERSISTENCE_DEFAULT.
Steven Cooremanc8b95342021-03-18 20:48:06 +0100789 * \param[out] slot_number On success, the slot number known to the driver
790 * registered at the lifetime location reported
Steven Cooremanb938b0b2021-04-06 13:08:42 +0200791 * through \p lifetime which corresponds to the
Steven Cooreman6801f082021-02-19 17:21:22 +0100792 * requested built-in key.
793 *
794 * \retval #PSA_SUCCESS
795 * The requested key identifier designates a built-in key.
796 * In a multi-application configuration, the requested owner
797 * is allowed to access it.
798 * \retval #PSA_ERROR_DOES_NOT_EXIST
799 * The requested key identifier is not a built-in key which is known
800 * to this function. If a key exists in the key storage with this
801 * identifier, the data from the storage will be used.
Steven Cooreman203bcbb2021-03-18 17:17:40 +0100802 * \return (any other error)
Steven Cooreman6801f082021-02-19 17:21:22 +0100803 * Any other error is propagated to the function that requested the key.
804 * Common errors include:
805 * - #PSA_ERROR_NOT_PERMITTED: the key exists but the requested owner
806 * is not allowed to access it.
807 */
808psa_status_t mbedtls_psa_platform_get_builtin_key(
Steven Cooremanc8b95342021-03-18 20:48:06 +0100809 mbedtls_svc_key_id_t key_id,
810 psa_key_lifetime_t *lifetime,
811 psa_drv_slot_number_t *slot_number );
Steven Cooreman6801f082021-02-19 17:21:22 +0100812#endif /* MBEDTLS_PSA_CRYPTO_BUILTIN_KEYS */
813
814/** @} */
815
Janos Follath702cf092021-05-26 12:58:23 +0100816/** \addtogroup crypto_types
817 * @{
818 */
819
820#define PSA_ALG_CATEGORY_PAKE ((psa_algorithm_t)0x0a000000)
821
822/** Whether the specified algorithm is a password-authenticated key exchange.
823 *
824 * \param alg An algorithm identifier (value of type #psa_algorithm_t).
825 *
826 * \return 1 if \p alg is a password-authenticated key exchange (PAKE)
827 * algorithm, 0 otherwise.
828 * This macro may return either 0 or 1 if \p alg is not a supported
829 * algorithm identifier.
830 */
831#define PSA_ALG_IS_PAKE(alg) \
832 (((alg) & PSA_ALG_CATEGORY_MASK) == PSA_ALG_CATEGORY_PAKE)
833
834/** The Password-authenticated key exchange by juggling (J-PAKE) algorithm.
835 *
836 * This is J-PAKE as defined by RFC 8236, instantiated with the following
837 * parameters:
838 *
839 * - The group can be either an elliptic curve or defined over a finite field.
840 * - Schnorr NIZK proof as defined by RFC 8235 and using the same group as the
841 * J-PAKE algorithm.
842 * - A secure cryptographic hash function.
843 *
844 * To select these parameters and set up the cipher suite, call
845 * psa_pake_cs_set_algorithm(cipher_suite, PSA_ALG_JPAKE);
846 * psa_pake_cs_set_primitive(cipher_suite,
847 * PSA_PAKE_PRIMITIVE(type, family, bits));
848 * psa_pake_cs_set_hash(cipher_suite, hash);
849 *
850 * For more information on how to set a specific curve or field, refer to the
851 * documentation of the individual \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
852 *
853 * After initializing a J-PAKE operation, call
854 * psa_pake_setup(operation, cipher_suite);
855 * psa_pake_set_user(operation, ...);
856 * psa_pake_set_peer(operation, ...);
857 * and either
858 * psa_pake_set_password_stretch(operation, ...);
859 * or
860 * psa_pake_set_password_key(operation, ...);
861 *
862 * Either way the password is read as a byte array and must be non-empty. This
863 * can be the password itself (in some pre-defined character encoding) or some
864 * value derived from the password as mandated by some higher level protocol.
865 *
866 * (The implementation converts this byte array to a number as described in
867 * Section 2.3.8 of _SEC 1: Elliptic Curve Cryptography_
868 * (https://www.secg.org/sec1-v2.pdf), before reducing it modulo \c q. Here
869 * \c q is order of the group defined by the primitive set in the cipher suite.
870 * The \c psa_pake_set_password_xxx() functions return an error if the result
871 * of the reduction is 0.)
872 *
873 * The key exchange flow for J-PAKE is as follows:
874 * -# To get the first round data that needs to be sent to the peer, call
875 * // Get g1
876 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
877 * // Get the ZKP public key for x1
878 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
879 * // Get the ZKP proof for x1
880 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
881 * // Get g2
882 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
883 * // Get the ZKP public key for x2
884 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
885 * // Get the ZKP proof for x2
886 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
887 * -# To provide the first round data received from the peer to the operation,
888 * call
889 * // Set g3
890 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
891 * // Set the ZKP public key for x3
892 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
893 * // Set the ZKP proof for x3
894 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
895 * // Set g4
896 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
897 * // Set the ZKP public key for x4
898 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
899 * // Set the ZKP proof for x4
900 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
901 * -# To get the second round data that needs to be sent to the peer, call
902 * // Get A
903 * psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
904 * // Get ZKP public key for x2*s
905 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
906 * // Get ZKP proof for x2*s
907 * psa_pake_output(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
908 * -# To provide the second round data received from the peer to the operation,
909 * call
910 * // Set B
911 * psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...);
912 * // Set ZKP public key for x4*s
913 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PUBLIC, ...);
914 * // Set ZKP proof for x4*s
915 * psa_pake_input(operation, #PSA_PAKE_STEP_ZK_PROOF, ...);
916 * -# To access the shared secret call
917 * // Get Ka=Kb=K
918 * psa_pake_get_implicit_key()
919 *
920 * For more information consult the documentation of the individual
921 * \c PSA_PAKE_STEP_XXX constants.
922 *
923 * At this point there is a cryptographic guarantee that only the authenticated
924 * party who used the same password is able to compute the key. But there is no
925 * guarantee that the peer is the party he claims to be and was able to do so.
926 *
927 * That is, the authentication is only implicit (the peer is not authenticated
928 * at this point, and no action should be taken that assume that they are - like
929 * for example accessing restricted files).
930 *
931 * To make the authentication explicit there are various methods, see Section 5
932 * of RFC 8236 for two examples.
933 *
934 */
935#define PSA_ALG_JPAKE ((psa_algorithm_t)0x0a000100)
936
937/** @} */
938
939/** \defgroup pake Password-authenticated key exchange (PAKE)
Janos Follath7d69b3a2021-05-26 13:10:56 +0100940 *
941 * This is a proposed PAKE interface for the PSA Crypto API. It is not part of
942 * the official PSA Crypto API yet.
943 *
944 * \note The content of this section is not part of the stable API and ABI
945 * of Mbed Crypto and may change arbitrarily from version to version.
946 * Same holds for the corresponding macros #PSA_ALG_CATEGORY_PAKE and
947 * #PSA_ALG_JPAKE.
Janos Follath702cf092021-05-26 12:58:23 +0100948 * @{
949 */
950
951/** \brief Encoding of the side of PAKE
952 *
953 * Encodes which side of the algorithm is being executed. For more information
954 * see the documentation of individual \c PSA_PAKE_SIDE_XXX constants.
955 */
956typedef uint8_t psa_pake_side_t;
957
958/** Encoding of input and output indicators for PAKE.
959 *
960 * Some PAKE algorithms need to exchange more data than just a single key share.
961 * This type is for encoding additional input and output data for such
962 * algorithms.
963 */
964typedef uint8_t psa_pake_step_t;
965
966/** Encoding of the type of the PAKE's primitive.
967 *
968 * Values defined by this standard will never be in the range 0x80-0xff.
969 * Vendors who define additional types must use an encoding in this range.
970 *
971 * For more information see the documentation of individual
972 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
973 */
974typedef uint8_t psa_pake_primitive_type_t;
975
976/** \brief Encoding of the family of the primitive associated with the PAKE.
977 *
978 * For more information see the documentation of individual
979 * \c PSA_PAKE_PRIMITIVE_TYPE_XXX constants.
980 */
981typedef uint8_t psa_pake_family_t;
982
983/** \brief Encoding of the primitive associated with the PAKE.
984 *
985 * For more information see the documentation of the #PSA_PAKE_PRIMITIVE macro.
986 */
987typedef uint32_t psa_pake_primitive_t;
988
989/** The first peer in a balanced PAKE.
990 *
991 * Although balanced PAKE algorithms are symmetric, some of them needs an
992 * ordering of peers for the transcript calculations. If the algorithm does not
993 * need this, both #PSA_PAKE_SIDE_FIRST and #PSA_PAKE_SIDE_SECOND are
994 * accepted.
995 */
996#define PSA_PAKE_SIDE_FIRST ((psa_pake_side_t)0x01)
997
998/** The second peer in a balanced PAKE.
999 *
1000 * Although balanced PAKE algorithms are symmetric, some of them needs an
1001 * ordering of peers for the transcript calculations. If the algorithm does not
1002 * need this, either #PSA_PAKE_SIDE_FIRST or #PSA_PAKE_SIDE_SECOND are
1003 * accepted.
1004 */
1005#define PSA_PAKE_SIDE_SECOND ((psa_pake_side_t)0x02)
1006
1007/** The client in an augmented PAKE.
1008 *
1009 * Augmented PAKE algorithms need to differentiate between client and server.
1010 */
1011#define PSA_PAKE_SIDE_CLIENT ((psa_pake_side_t)0x11)
1012
1013/** The server in an augmented PAKE.
1014 *
1015 * Augmented PAKE algorithms need to differentiate between client and server.
1016 */
1017#define PSA_PAKE_SIDE_SERVER ((psa_pake_side_t)0x12)
1018
1019/** The PAKE primitive type indicating the use of elliptic curves.
1020 *
1021 * The values of the \c family and \c bits fields of the cipher suite identify a
1022 * specific elliptic curve, using the same mapping that is used for ECC
1023 * (::psa_ecc_family_t) keys.
1024 *
1025 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1026 * \c bits means the value returned by psa_pake_cs_get_bits().)
1027 *
1028 * Input and output during the operation can involve group elements and scalar
1029 * values:
1030 * -# The format for group elements is the same as for public keys on the
1031 * specific curve would be. For more information, consult the documentation of
1032 * psa_export_public_key().
1033 * -# The format for scalars is the same as for private keys on the specific
1034 * curve would be. For more information, consult the documentation of
1035 * psa_export_key().
1036 */
1037#define PSA_PAKE_PRIMITIVE_TYPE_ECC ((psa_pake_primitive_type_t)0x01)
1038
1039/** The PAKE primitive type indicating the use of Diffie-Hellman groups.
1040 *
1041 * The values of the \c family and \c bits fields of the cipher suite identify
1042 * a specific Diffie-Hellman group, using the same mapping that is used for
1043 * Diffie-Hellman (::psa_dh_family_t) keys.
1044 *
1045 * (Here \c family means the value returned by psa_pake_cs_get_family() and
1046 * \c bits means the value returned by psa_pake_cs_get_bits().)
1047 *
1048 * Input and output during the operation can involve group elements and scalar
1049 * values:
1050 * -# The format for group elements is the same as for public keys on the
1051 * specific group would be. For more information, consult the documentation of
1052 * psa_export_public_key().
1053 * -# The format for scalars is the same as for private keys on the specific
1054 * group would be. For more information, consult the documentation of
1055 * psa_export_key().
1056 */
1057#define PSA_PAKE_PRIMITIVE_TYPE_DH ((psa_pake_primitive_type_t)0x02)
1058
1059/** Construct a PAKE primitive from type, family and bit-size.
1060 *
1061 * \param pake_type The type of the primitive
1062 * (value of type ::psa_pake_primitive_type_t).
1063 * \param pake_family The family of the primitive
1064 * (the type and interpretation of this parameter depends
1065 * on \p type, for more information consult the
1066 * documentation of individual ::psa_pake_primitive_type_t
1067 * constants).
1068 * \param pake_bits The bit-size of the primitive
1069 * (Value of type \c size_t. The interpretation
1070 * of this parameter depends on \p family, for more
1071 * information consult the documentation of individual
1072 * ::psa_pake_primitive_type_t constants).
1073 *
1074 * \return The constructed primitive value of type ::psa_pake_primitive_t.
1075 * Return 0 if the requested primitive can't be encoded as
1076 * ::psa_pake_primitive_t.
1077 */
1078#define PSA_PAKE_PRIMITIVE(pake_type, pake_family, pake_bits) \
1079 ((pake_bits & 0xFFFF) != pake_bits) ? 0 : \
1080 ((psa_pake_primitive_t) (((pake_type) << 24 | \
1081 (pake_family) << 16) | (pake_bits)))
1082
1083/** The key share being sent to or received from the peer.
1084 *
1085 * The format for both input and output at this step is the same as for public
1086 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1087 * be.
1088 *
1089 * For more information on the format, consult the documentation of
1090 * psa_export_public_key().
1091 *
1092 * For information regarding how the group is determined, consult the
1093 * documentation #PSA_PAKE_PRIMITIVE.
1094 */
1095#define PSA_PAKE_STEP_KEY_SHARE ((psa_pake_step_t)0x01)
1096
1097/** A Schnorr NIZKP public key.
1098 *
1099 * The format for both input and output at this step is the same as for public
1100 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1101 * be.
1102 *
1103 * For more information on the format, consult the documentation of
1104 * psa_export_public_key().
1105 *
1106 * For information regarding how the group is determined, consult the
1107 * documentation #PSA_PAKE_PRIMITIVE.
1108 */
1109#define PSA_PAKE_STEP_ZK_PUBLIC ((psa_pake_step_t)0x02)
1110
1111/** A Schnorr NIZKP proof.
1112 *
1113 * The format for both input and output at this step is the same as for private
1114 * keys on the group determined by the primitive (::psa_pake_primitive_t) would
1115 * be.
1116 *
1117 * Some public key algorithms mask the private keys and this might be reflected
1118 * in the export format. Even if this is the case the masking is omitted at
1119 * this step.
1120 *
1121 * For more information on the format, consult the documentation of
1122 * psa_export_key().
1123 *
1124 * For information regarding how the group is determined, consult the
1125 * documentation #PSA_PAKE_PRIMITIVE.
1126 */
1127#define PSA_PAKE_STEP_ZK_PROOF ((psa_pake_step_t)0x03)
1128
1129/** The type of the data strucure for PAKE cipher suites.
1130 *
1131 * This is an implementation-defined \c struct. Applications should not
1132 * make any assumptions about the content of this structure.
1133 * Implementation details can change in future versions without notice.
1134 */
1135typedef struct psa_pake_cipher_suite_s psa_pake_cipher_suite_t;
1136
1137/** Retrieve the PAKE algorithm from a PAKE cipher suite.
1138 *
1139 * This function may be declared as `static` (i.e. without external
1140 * linkage). This function may be provided as a function-like macro,
1141 * but in this case it must evaluate its argument exactly once.
1142 *
1143 * \param[in] cipher_suite The cipher suite structure to query.
1144 *
1145 * \return The PAKE algorithm stored in the cipher suite structure.
1146 */
1147static psa_algorithm_t psa_pake_cs_get_algorithm(
1148 const psa_pake_cipher_suite_t* cipher_suite
1149 );
1150
1151/** Declare the PAKE algorithm for the cipher suite.
1152 *
1153 * This function overwrites any PAKE algorithm
1154 * previously set in \p cipher_suite.
1155 *
1156 * This function may be declared as `static` (i.e. without external
1157 * linkage). This function may be provided as a function-like macro,
1158 * but in this case it must evaluate each of its arguments exactly once.
1159 *
1160 * \param[out] cipher_suite The cipher suite structure to write to.
1161 * \param algorithm The PAKE algorithm to write.
1162 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1163 * such that #PSA_ALG_IS_PAKE(\c alg) is true.)
1164 * If this is 0, the PAKE algorithm in
1165 * \p cipher_suite becomes unspecified.
1166 */
1167static void psa_pake_cs_set_algorithm(
1168 psa_pake_cipher_suite_t* cipher_suite,
1169 psa_algorithm_t algorithm
1170 );
1171
1172/** Retrieve the primitive from a PAKE cipher suite.
1173 *
1174 * This function may be declared as `static` (i.e. without external linkage).
1175 * This function may be provided as a function-like macro, but in this case it
1176 * must evaluate its argument exactly once.
1177 *
1178 * \param[in] cipher_suite The cipher suite structure to query.
1179 *
1180 * \return The primitive stored in the cipher suite structure.
1181 */
1182static psa_pake_primitive_t psa_pake_cs_get_primitive(
1183 const psa_pake_cipher_suite_t* cipher_suite
1184 );
1185
1186/** Declare the primitive for a PAKE cipher suite.
1187 *
1188 * This function overwrites any primitive previously set in \p cipher_suite.
1189 *
1190 * This function may be declared as `static` (i.e. without external
1191 * linkage). This function may be provided as a function-like macro,
1192 * but in this case it must evaluate each of its arguments exactly once.
1193 *
1194 * \param[out] cipher_suite The cipher suite structure to write to.
1195 * \param primitive The primitive to write. If this is 0, the
1196 * primitive type in \p cipher_suite becomes
1197 * unspecified.
1198 */
1199static void psa_pake_cs_set_primitive(
1200 psa_pake_cipher_suite_t* cipher_suite,
1201 psa_pake_primitive_t primitive
1202 );
1203
1204/** Retrieve the hash algorithm from a PAKE cipher suite.
1205 *
1206 * This function may be declared as `static` (i.e. without external
1207 * linkage). This function may be provided as a function-like macro,
1208 * but in this case it must evaluate its argument exactly once.
1209 *
1210 * \param[in] cipher_suite The cipher suite structure to query.
1211 *
1212 * \return The hash algorithm stored in the cipher suite structure. The return
1213 * value is 0 if the PAKE is not parametrised by a hash algorithm or if
1214 * the hash algorithm is not set.
1215 */
1216static psa_algorithm_t psa_pake_cs_get_hash(
1217 const psa_pake_cipher_suite_t* cipher_suite
1218 );
1219
1220/** Declare the hash algorithm for a PAKE cipher suite.
1221 *
1222 * This function overwrites any hash algorithm
1223 * previously set in \p cipher_suite.
1224 *
1225 * This function may be declared as `static` (i.e. without external
1226 * linkage). This function may be provided as a function-like macro,
1227 * but in this case it must evaluate each of its arguments exactly once.
1228 *
1229 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1230 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1231 * for more information.
1232 *
1233 * \param[out] cipher_suite The cipher suite structure to write to.
1234 * \param hash The hash involved in the cipher suite.
1235 * (`PSA_ALG_XXX` values of type ::psa_algorithm_t
1236 * such that #PSA_ALG_IS_HASH(\c alg) is true.)
1237 * If this is 0, the hash algorithm in
1238 * \p cipher_suite becomes unspecified.
1239 */
1240static void psa_pake_cs_set_hash(
1241 psa_pake_cipher_suite_t* cipher_suite,
1242 psa_algorithm_t hash
1243 );
1244
1245/** The type of the state data structure for PAKE operations.
1246 *
1247 * Before calling any function on a PAKE operation object, the application
1248 * must initialize it by any of the following means:
1249 * - Set the structure to all-bits-zero, for example:
1250 * \code
1251 * psa_pake_operation_t operation;
1252 * memset(&operation, 0, sizeof(operation));
1253 * \endcode
1254 * - Initialize the structure to logical zero values, for example:
1255 * \code
1256 * psa_pake_operation_t operation = {0};
1257 * \endcode
1258 * - Initialize the structure to the initializer #PSA_PAKE_OPERATION_INIT,
1259 * for example:
1260 * \code
1261 * psa_pake_operation_t operation = PSA_PAKE_OPERATION_INIT;
1262 * \endcode
1263 * - Assign the result of the function psa_pake_operation_init()
1264 * to the structure, for example:
1265 * \code
1266 * psa_pake_operation_t operation;
1267 * operation = psa_pake_operation_init();
1268 * \endcode
1269 *
1270 * This is an implementation-defined \c struct. Applications should not
1271 * make any assumptions about the content of this structure.
1272 * Implementation details can change in future versions without notice. */
1273typedef struct psa_pake_operation_s psa_pake_operation_t;
1274
1275/** Return an initial value for an PAKE operation object.
1276 */
1277static psa_pake_operation_t psa_pake_operation_init(void);
1278
1279/** Set the session information for a password-authenticated key exchange.
1280 *
1281 * The sequence of operations to set up a password-authenticated key exchange
1282 * is as follows:
1283 * -# Allocate an operation object which will be passed to all the functions
1284 * listed here.
1285 * -# Initialize the operation object with one of the methods described in the
1286 * documentation for #psa_pake_operation_t, e.g.
1287 * #PSA_PAKE_OPERATION_INIT.
1288 * -# Call psa_pake_setup() to specify the cipher suite.
1289 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
1290 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
1291 * to be called depends on the algorithm in use.
1292 *
1293 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1294 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1295 * for more information.
1296 *
1297 * A typical sequence of calls to perform a password-authenticated key
1298 * exchange:
1299 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
1300 * key share that needs to be sent to the peer.
1301 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
1302 * the key share that was received from the peer.
1303 * -# Depending on the algorithm additional calls to psa_pake_output() and
1304 * psa_pake_input() might be necessary.
1305 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
1306 *
1307 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1308 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1309 * for more information.
1310 *
1311 * If an error occurs at any step after a call to psa_pake_setup(),
1312 * the operation will need to be reset by a call to psa_pake_abort(). The
1313 * application may call psa_pake_abort() at any time after the operation
1314 * has been initialized.
1315 *
1316 * After a successful call to psa_pake_setup(), the application must
1317 * eventually terminate the operation. The following events terminate an
1318 * operation:
1319 * - A call to psa_pake_abort().
1320 * - A successful call to psa_pake_get_implicit_key().
1321 *
1322 * \param[in,out] operation The operation object to set up. It must have
1323 * been initialized as per the documentation for
1324 * #psa_pake_operation_t and not yet in use (no
1325 * other function has been called on it since the
1326 * last initialization).
1327 * \param cipher_suite The cipher suite to use. (A cipher suite fully
1328 * characterizes a PAKE algorithm and determines
1329 * the algorithm as well.)
1330 *
1331 * \retval #PSA_SUCCESS
1332 * Success.
1333 * \retval #PSA_ERROR_BAD_STATE
1334 * The operation state is not valid.
1335 * \retval #PSA_ERROR_NOT_SUPPORTED
1336 * The \p cipher_suite is not supported or is not valid.
1337 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1338 * \retval #PSA_ERROR_HARDWARE_FAILURE
1339 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1340 * \retval #PSA_ERROR_BAD_STATE
1341 * The library has not been previously initialized by psa_crypto_init().
1342 * It is implementation-dependent whether a failure to initialize
1343 * results in this error code.
1344 */
1345psa_status_t psa_pake_setup(psa_pake_operation_t *operation,
1346 psa_pake_cipher_suite_t cipher_suite);
1347
1348/** Set the password for a password-authenticated key exchange from key ID.
1349 *
1350 * Call this function when the password, or a value derived from the password,
1351 * is already present in the key store. To calculate the password-derived value
1352 * from a password input, use the key derivation interface and
1353 * psa_pake_set_password_stretch() instead.
1354 *
1355 * \param[in,out] operation The operation object to set the password for. It
1356 * must have been set up by psa_pake_setup() and
1357 * not yet in use (neither psa_pake_output() nor
1358 * psa_pake_input() has been called yet). It must
1359 * be on operation for which the password hasn't
1360 * been set yet (neither
1361 * psa_pake_set_password_stretch() nor
1362 * psa_pake_set_password_key() has been called
1363 * yet).
1364 * \param password Identifier of the key holding the password or a
1365 * value derived from the password (eg. by a
1366 * memory-hard function). It must remain valid
1367 * until the operation terminates. It must be of
1368 * type #PSA_KEY_TYPE_PASSWORD or
1369 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
1370 * the usage #PSA_KEY_USAGE_DERIVE.
1371 *
1372 * \retval #PSA_SUCCESS
1373 * Success.
1374 * \retval #PSA_ERROR_BAD_STATE
1375 * The operation state is not valid (it must have been set up.)
1376 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1377 * \retval #PSA_ERROR_INVALID_HANDLE
1378 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1379 * \retval #PSA_ERROR_HARDWARE_FAILURE
1380 * \retval #PSA_ERROR_STORAGE_FAILURE
1381 * \retval #PSA_ERROR_NOT_PERMITTED
1382 * \retval #PSA_ERROR_INVALID_ARGUMENT
1383 * \p key is not compatible with the algorithm or the cipher suite.
1384 * \retval #PSA_ERROR_BAD_STATE
1385 * The library has not been previously initialized by psa_crypto_init().
1386 * It is implementation-dependent whether a failure to initialize
1387 * results in this error code.
1388 */
1389psa_status_t psa_pake_set_password_key(psa_pake_operation_t *operation,
1390 mbedtls_svc_key_id_t password);
1391
1392/** Set the password for a password-authenticated key exchange via a key
1393 * stretching function.
1394 *
1395 * Some protocols use values derived from passwords via key stretching
1396 * functions to mitigate dictionary attacks. Key stretching functions can be
1397 * accessed through the key derivation interface and the result can be supplied
1398 * to the PAKE operation in the form of a key derivation object.
1399 *
1400 * This function draws bytes from a key derivation algorithm and sets those
1401 * bytes as a password for the password-authenticated key exchange. If you
1402 * view the key derivation's output as a stream of bytes, this function
1403 * destructively reads the requested number of bytes from the stream.
1404 * The key derivation operation's capacity decreases by the number of bytes read.
1405 *
1406 * If this function returns anything other than #PSA_SUCCESS, both \p operation
1407 * and \p key_derivation operations enter an error state and must be aborted by
1408 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1409 *
1410 * \param[in,out] operation The operation object to set the password for.
1411 * It must have been set up by psa_pake_setup()
1412 * and not yet in use (neither psa_pake_output()
1413 * nor psa_pake_input() has been called yet). It
1414 * must be on operation for which the password
1415 * hasn't been set yet (neither
1416 * psa_pake_set_password_stretch() nor
1417 * psa_pake_set_password_key() has been called
1418 * yet).
1419 * \param[in,out] key_derivation An ongoing key derivation operation set up
1420 * from the password and in a state suitable for
1421 * calling psa_key_derivation_output_bytes().
1422 * \param input_length Number of bytes to input from the
1423 * \p key_derivation operation.
1424 *
1425 * \retval #PSA_SUCCESS
1426 * Success.
1427 * \retval #PSA_ERROR_BAD_STATE
1428 * The state of \p operation or \p key_derivation is not valid.
1429 * \retval #PSA_ERROR_INSUFFICIENT_DATA
1430 * The \p key_derivation operation's capacity was less than
1431 * \p input_length bytes.
1432 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1433 * \retval #PSA_ERROR_INVALID_HANDLE
1434 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1435 * \retval #PSA_ERROR_HARDWARE_FAILURE
1436 * \retval #PSA_ERROR_STORAGE_FAILURE
1437 * \retval #PSA_ERROR_NOT_PERMITTED
1438 * One of the inputs to \p key_derivation was a key whose policy didn't
1439 * allow #PSA_KEY_USAGE_DERIVE.
1440 * \retval #PSA_ERROR_INVALID_ARGUMENT
1441 * \p key is not compatible with the algorithm or the cipher suite.
1442 * \retval #PSA_ERROR_BAD_STATE
1443 * The library has not been previously initialized by psa_crypto_init().
1444 * It is implementation-dependent whether a failure to initialize
1445 * results in this error code.
1446 */
1447psa_status_t psa_pake_set_password_stretch(
1448 psa_pake_operation_t *operation,
1449 psa_key_derivation_operation_t *key_derivation,
1450 size_t input_length
1451 );
1452
1453/** Set the user ID for a password-authenticated key exchange.
1454 *
1455 * Call this function to set the user ID. For PAKE algorithms that associate a
1456 * user identifier with each side of the session you need to call
1457 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
1458 * user identifier with the session, call psa_pake_set_user() only.
1459 *
1460 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1461 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1462 * for more information.
1463 *
1464 * \param[in,out] operation The operation object to set the user ID for. It
1465 * must have been set up by psa_pake_setup() and
1466 * not yet in use (neither psa_pake_output() nor
1467 * psa_pake_input() has been called yet). It must
1468 * be on operation for which the user ID hasn't
1469 * been set (psa_pake_set_user() hasn't been
1470 * called yet).
1471 * \param[in] user_id The user ID to authenticate with.
1472 * \param user_id_len Size of the \p user_id buffer in bytes.
1473 *
1474 * \retval #PSA_SUCCESS
1475 * Success.
1476 * \retval #PSA_ERROR_BAD_STATE
1477 * The operation state is not valid.
1478 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1479 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1480 * \retval #PSA_ERROR_HARDWARE_FAILURE
1481 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1482 * \retval #PSA_ERROR_INVALID_ARGUMENT
1483 * \p user_id is NULL.
1484 * \retval #PSA_ERROR_BAD_STATE
1485 * The library has not been previously initialized by psa_crypto_init().
1486 * It is implementation-dependent whether a failure to initialize
1487 * results in this error code.
1488 */
1489psa_status_t psa_pake_set_user(psa_pake_operation_t *operation,
1490 const uint8_t *user_id,
1491 size_t user_id_len);
1492
1493/** Set the peer ID for a password-authenticated key exchange.
1494 *
1495 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
1496 * that associate a user identifier with each side of the session. For PAKE
1497 * algorithms that associate a single user identifier with the session, call
1498 * psa_pake_set_user() only.
1499 *
1500 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1501 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1502 * for more information.
1503 *
1504 * \param[in,out] operation The operation object to set the peer ID for. It
1505 * must have been set up by psa_pake_setup() and
1506 * not yet in use (neither psa_pake_output() nor
1507 * psa_pake_input() has been called yet). It must
1508 * be on operation for which the peer ID hasn't
1509 * been set (psa_pake_set_peer() hasn't been
1510 * called yet).
1511 * \param[in] peer_id The peer's ID to authenticate.
1512 * \param peer_id_len Size of the \p peer_id buffer in bytes.
1513 *
1514 * \retval #PSA_SUCCESS
1515 * Success.
1516 * \retval #PSA_ERROR_BAD_STATE
1517 * The operation state is not valid.
1518 * \retval #PSA_ERROR_NOT_SUPPORTED
1519 * The algorithm doesn't associate a second identity with the session.
1520 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1521 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1522 * \retval #PSA_ERROR_HARDWARE_FAILURE
1523 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1524 * \retval #PSA_ERROR_INVALID_ARGUMENT
1525 * \p user_id is NULL.
1526 * \retval #PSA_ERROR_BAD_STATE
1527 * The library has not been previously initialized by psa_crypto_init().
1528 * It is implementation-dependent whether a failure to initialize
1529 * results in this error code.
1530 */
1531psa_status_t psa_pake_set_peer(psa_pake_operation_t *operation,
1532 const uint8_t *peer_id,
1533 size_t peer_id_len);
1534
1535/** Set the side for a password-authenticated key exchange.
1536 *
1537 * Not all PAKE algorithms need to differentiate the communicating entities.
1538 * It is optional to call this function for PAKEs that don't require a side
1539 * parameter. For such PAKEs the side parameter is ignored.
1540 *
1541 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
1542 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
1543 * for more information.
1544 *
1545 * \param[in,out] operation The operation object to set the side for. It
1546 * must have been set up by psa_pake_setup() and
1547 * not yet in use (neither psa_pake_output() nor
1548 * psa_pake_input() has been called yet). It must
1549 * be on operation for which the side hasn't been
1550 * set (psa_pake_set_side() hasn't been called
1551 * yet).
1552 * \param side A value of type ::psa_pake_side_t signaling the
1553 * side of the algorithm that is being set up. For
1554 * more information see the documentation of \c
1555 * PSA_PAKE_SIDE_XXX constants.
1556 *
1557 * \retval #PSA_SUCCESS
1558 * Success.
1559 * \retval #PSA_ERROR_BAD_STATE
1560 * The operation state is not valid.
1561 * \retval #PSA_ERROR_NOT_SUPPORTED
1562 * The \p side for this algorithm is not supported or is not valid.
1563 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1564 * \retval #PSA_ERROR_HARDWARE_FAILURE
1565 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1566 * \retval #PSA_ERROR_BAD_STATE
1567 * The library has not been previously initialized by psa_crypto_init().
1568 * It is implementation-dependent whether a failure to initialize
1569 * results in this error code.
1570 */
1571psa_status_t psa_pake_set_side(psa_pake_operation_t *operation,
1572 psa_pake_side_t side);
1573
1574/** Get output for a step of a password-authenticated key exchange.
1575 *
1576 * Depending on the algorithm being executed, you might need to call this
1577 * function several times or you might not need to call this at all.
1578 *
1579 * The exact sequence of calls to perform a password-authenticated key
1580 * exchange depends on the algorithm in use. Refer to the documentation of
1581 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1582 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1583 * information.
1584 *
1585 * If this function returns an error status, the operation enters an error
1586 * state and must be aborted by calling psa_pake_abort().
1587 *
1588 * \param[in,out] operation Active PAKE operation.
1589 * \param step The step of the algorithm for which the output is
1590 * requested.
1591 * \param[out] output Buffer where the output is to be written in the
1592 * format appropriate for this \p step. Refer to
1593 * the documentation of the individual
1594 * \c PSA_PAKE_STEP_XXX constants for more
1595 * information.
1596 * \param output_size Size of the \p output buffer in bytes. This must
1597 * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \c
1598 * cipher_suite, \p type).
1599 *
1600 * \param[out] output_length On success, the number of bytes of the returned
1601 * output.
1602 *
1603 * \retval #PSA_SUCCESS
1604 * Success.
1605 * \retval #PSA_ERROR_BAD_STATE
1606 * The operation state is not valid (it must be active, but beyond that
1607 * validity is specific to the algorithm).
1608 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
1609 * The size of the \p output buffer is too small.
1610 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1611 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1612 * \retval #PSA_ERROR_HARDWARE_FAILURE
1613 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1614 * \retval #PSA_ERROR_STORAGE_FAILURE
1615 * \retval #PSA_ERROR_BAD_STATE
1616 * The library has not been previously initialized by psa_crypto_init().
1617 * It is implementation-dependent whether a failure to initialize
1618 * results in this error code.
1619 */
1620psa_status_t psa_pake_output(psa_pake_operation_t *operation,
1621 psa_pake_step_t step,
1622 uint8_t *output,
1623 size_t output_size,
1624 size_t *output_length);
1625
1626/** Provide input for a step of a password-authenticated key exchange.
1627 *
1628 * Depending on the algorithm being executed, you might need to call this
1629 * function several times or you might not need to call this at all.
1630 *
1631 * The exact sequence of calls to perform a password-authenticated key
1632 * exchange depends on the algorithm in use. Refer to the documentation of
1633 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1634 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1635 * information.
1636 *
1637 * If this function returns an error status, the operation enters an error
1638 * state and must be aborted by calling psa_pake_abort().
1639 *
1640 * \param[in,out] operation Active PAKE operation.
1641 * \param step The step for which the input is provided.
1642 * \param[out] input Buffer containing the input in the format
1643 * appropriate for this \p step. Refer to the
1644 * documentation of the individual
1645 * \c PSA_PAKE_STEP_XXX constants for more
1646 * information.
1647 * \param[out] input_length Size of the \p input buffer in bytes.
1648 *
1649 * \retval #PSA_SUCCESS
1650 * Success.
1651 * \retval #PSA_ERROR_BAD_STATE
1652 * The operation state is not valid (it must be active, but beyond that
1653 * validity is specific to the algorithm).
1654 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1655 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1656 * \retval #PSA_ERROR_HARDWARE_FAILURE
1657 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1658 * \retval #PSA_ERROR_STORAGE_FAILURE
1659 * \retval #PSA_ERROR_INVALID_ARGUMENT
1660 * The input is not valid for the algorithm, ciphersuite or \p step.
1661 * \retval #PSA_ERROR_BAD_STATE
1662 * The library has not been previously initialized by psa_crypto_init().
1663 * It is implementation-dependent whether a failure to initialize
1664 * results in this error code.
1665 */
1666psa_status_t psa_pake_input(psa_pake_operation_t *operation,
1667 psa_pake_step_t step,
1668 uint8_t *input,
1669 size_t input_length);
1670
1671/** Get implicitly confirmed shared secret from a PAKE.
1672 *
1673 * At this point there is a cryptographic guarantee that only the authenticated
1674 * party who used the same password is able to compute the key. But there is no
1675 * guarantee that the peer is the party he claims to be and was able to do so.
1676 *
1677 * That is, the authentication is only implicit (the peer is not authenticated
1678 * at this point, and no action should be taken that assume that they are - like
1679 * for example accessing restricted files).
1680 *
1681 * This function can be called after the key exchange phase of the operation
1682 * has completed. It imports the shared secret output of the PAKE into the
1683 * provided derivation operation. The input step
1684 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
1685 * material in the key derivation operation.
1686 *
1687 * The exact sequence of calls to perform a password-authenticated key
1688 * exchange depends on the algorithm in use. Refer to the documentation of
1689 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
1690 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
1691 * information.
1692 *
1693 * When this function returns successfully, \p operation becomes inactive.
1694 * If this function returns an error status, both \p operation
1695 * and \p key_derivation operations enter an error state and must be aborted by
1696 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
1697 *
1698 * \param[in,out] operation Active PAKE operation.
1699 * \param[out] output A key derivation operation that is ready
1700 * for an input step of type
1701 * #PSA_KEY_DERIVATION_INPUT_SECRET.
1702 *
1703 * \retval #PSA_SUCCESS
1704 * Success.
1705 * \retval #PSA_ERROR_BAD_STATE
1706 * The operation state is not valid (it must be active, but beyond that
1707 * validity is specific to the algorithm).
1708 * \retval #PSA_ERROR_BAD_STATE
1709 * The state of \p output is not valid for
1710 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
1711 * step is out of order or the application has done this step already
1712 * and it may not be repeated.
1713 * \retval #PSA_ERROR_INVALID_ARGUMENT
1714 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the output’s
1715 * algorithm.
1716 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1717 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
1718 * \retval #PSA_ERROR_HARDWARE_FAILURE
1719 * \retval #PSA_ERROR_CORRUPTION_DETECTED
1720 * \retval #PSA_ERROR_STORAGE_FAILURE
1721 * \retval #PSA_ERROR_BAD_STATE
1722 * The library has not been previously initialized by psa_crypto_init().
1723 * It is implementation-dependent whether a failure to initialize
1724 * results in this error code.
1725 */
1726psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
1727 psa_key_derivation_operation_t *output);
1728
1729/**@}*/
1730
1731/** A sufficient output buffer size for psa_pake_output().
1732 *
1733 * If the size of the output buffer is at least this large, it is guaranteed
1734 * that psa_pake_output() will not fail due to an insufficient output buffer
1735 * size. The actual size of the output might be smaller in any given call.
1736 *
1737 * See also #PSA_PAKE_OUTPUT_MAX_SIZE
1738 *
1739 * \param alg A PAKE algorithm (PSA_ALG_XXX value such that
1740 * #PSA_ALG_IS_PAKE(\p alg) is true).
1741 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1742 * compatible with algorithm \p alg.
1743 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1744 * algorithm \p alg.
1745 * \return A sufficient output buffer size for the specified
1746 * output, cipher suite and algorithm. If the cipher suite,
1747 * the output type or PAKE algorithm is not recognized, or
1748 * the parameters are incompatible, return 0.
1749 */
1750#define PSA_PAKE_OUTPUT_SIZE(alg, primitive, output_step) 0
1751
1752/** A sufficient input buffer size for psa_pake_input().
1753 *
1754 * If the size of the input is larger than this, it is guaranteed
1755 * that psa_pake_input() will fail with #PSA_ERROR_INVALID_ARGUMENT.
1756 *
1757 * See also #PSA_PAKE_INPUT_MAX_SIZE
1758 *
1759 * \param alg A PAKE algorithm (PSA_ALG_XXX value such that
1760 * #PSA_ALG_IS_PAKE(\p alg) is true).
1761 * \param primitive A primitive of type ::psa_pake_primitive_t that is
1762 * compatible with algorithm \p alg.
1763 * \param output_step A value of type ::psa_pake_step_t that is valid for the
1764 * algorithm \p alg.
1765 * \return A sufficient output buffer size for the specified
1766 * output, cipher suite and algorithm. If the cipher suite,
1767 * the output type or PAKE algorithm is not recognized, or
1768 * the parameters are incompatible, return 0.
1769 */
1770#define PSA_PAKE_INPUT_SIZE(alg, primitive, input_step) 0
1771
1772/** Output buffer size for psa_pake_output() for any of the supported cipher
1773 * suites and PAKE algorithms.
1774 *
1775 * This macro must expand to a compile-time constant integer.
1776 *
1777 * See also #PSA_PAKE_OUTPUT_SIZE(\p alg, \p cipher_suite, \p output).
1778 */
1779#define PSA_PAKE_OUTPUT_MAX_SIZE 0
1780
1781/** Input buffer size for psa_pake_input() for any of the supported cipher
1782 * suites and PAKE algorithms.
1783 *
1784 * This macro must expand to a compile-time constant integer.
1785 *
1786 * See also #PSA_PAKE_INPUT_SIZE(\p alg, \p cipher_suite, \p output).
1787 */
1788#define PSA_PAKE_INPUT_MAX_SIZE 0
1789
1790struct psa_pake_cipher_suite_s
1791{
1792 psa_algorithm_t algorithm;
1793 psa_pake_primitive_type_t type;
1794 psa_pake_family_t family;
1795 uint16_t bits;
1796 psa_algorithm_t hash;
1797};
1798
1799static inline psa_algorithm_t psa_pake_cs_get_algorithm(
1800 const psa_pake_cipher_suite_t *cipher_suite)
1801{
1802 return(cipher_suite->algorithm);
1803}
1804
1805static inline void psa_pake_cs_set_algorithm(
1806 psa_pake_cipher_suite_t *cipher_suite,
1807 psa_algorithm_t algorithm)
1808{
1809 if(!PSA_ALG_IS_PAKE(algorithm))
1810 cipher_suite->algorithm = 0;
1811 else
1812 cipher_suite->algorithm = algorithm;
1813}
1814
1815static inline psa_pake_primitive_t psa_pake_cs_get_primitive(
1816 const psa_pake_cipher_suite_t *cipher_suite)
1817{
1818 return(PSA_PAKE_PRIMITIVE(cipher_suite->type, cipher_suite->family,
1819 cipher_suite->bits));
1820}
1821
1822static inline void psa_pake_cs_set_primitive(
1823 psa_pake_cipher_suite_t *cipher_suite,
1824 psa_pake_primitive_t primitive)
1825{
1826 cipher_suite->type = (psa_pake_primitive_type_t) (primitive >> 24);
1827 cipher_suite->family = (psa_pake_family_t) (0xFF & (primitive >> 16));
1828 cipher_suite->bits = (uint16_t) (0xFFFF & primitive);
1829}
1830
1831static inline psa_algorithm_t psa_pake_cs_get_hash(
1832 const psa_pake_cipher_suite_t *cipher_suite)
1833{
1834 return(cipher_suite->hash);
1835}
1836
1837static inline void psa_pake_cs_set_hash(
1838 psa_pake_cipher_suite_t *cipher_suite,
1839 psa_algorithm_t hash)
1840{
1841 if(!PSA_ALG_IS_HASH(hash))
1842 cipher_suite->hash = 0;
1843 else
1844 cipher_suite->hash = hash;
1845}
1846
1847struct psa_pake_operation_s
1848{
1849 psa_algorithm_t alg;
1850 union
1851 {
1852 /* Make the union non-empty even with no supported algorithms. */
1853 uint8_t dummy;
1854 } ctx;
1855};
1856
1857/* This only zeroes out the first byte in the union, the rest is unspecified. */
1858#define PSA_PAKE_OPERATION_INIT {0, {0}}
1859static inline struct psa_pake_operation_s psa_pake_operation_init(void)
1860{
1861 const struct psa_pake_operation_s v = PSA_PAKE_OPERATION_INIT;
1862 return(v);
1863}
1864
Gilles Peskinee59236f2018-01-27 23:32:46 +01001865#ifdef __cplusplus
1866}
1867#endif
1868
1869#endif /* PSA_CRYPTO_EXTRA_H */