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