blob: 9256f5a14a51844d6fbfee93dc4577ecfe924085 [file] [log] [blame]
Neil Armstrong56b8d232022-06-01 18:05:57 +02001/*
2 * PSA PAKE layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#ifndef PSA_CRYPTO_PAKE_H
22#define PSA_CRYPTO_PAKE_H
23
24#include <psa/crypto.h>
25
26/** Set the session information for a password-authenticated key exchange.
27 *
28 * The sequence of operations to set up a password-authenticated key exchange
29 * is as follows:
30 * -# Allocate an operation object which will be passed to all the functions
31 * listed here.
32 * -# Initialize the operation object with one of the methods described in the
33 * documentation for #psa_pake_operation_t, e.g.
34 * #PSA_PAKE_OPERATION_INIT.
35 * -# Call psa_pake_setup() to specify the cipher suite.
36 * -# Call \c psa_pake_set_xxx() functions on the operation to complete the
37 * setup. The exact sequence of \c psa_pake_set_xxx() functions that needs
38 * to be called depends on the algorithm in use.
39 *
40 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
41 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
42 * for more information.
43 *
44 * A typical sequence of calls to perform a password-authenticated key
45 * exchange:
46 * -# Call psa_pake_output(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to get the
47 * key share that needs to be sent to the peer.
48 * -# Call psa_pake_input(operation, #PSA_PAKE_STEP_KEY_SHARE, ...) to provide
49 * the key share that was received from the peer.
50 * -# Depending on the algorithm additional calls to psa_pake_output() and
51 * psa_pake_input() might be necessary.
52 * -# Call psa_pake_get_implicit_key() for accessing the shared secret.
53 *
54 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
55 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
56 * for more information.
57 *
58 * If an error occurs at any step after a call to psa_pake_setup(),
59 * the operation will need to be reset by a call to psa_pake_abort(). The
60 * application may call psa_pake_abort() at any time after the operation
61 * has been initialized.
62 *
63 * After a successful call to psa_pake_setup(), the application must
64 * eventually terminate the operation. The following events terminate an
65 * operation:
66 * - A call to psa_pake_abort().
67 * - A successful call to psa_pake_get_implicit_key().
68 *
69 * \param[in,out] operation The operation object to set up. It must have
70 * been initialized but not set up yet.
71 * \param[in] cipher_suite The cipher suite to use. (A cipher suite fully
72 * characterizes a PAKE algorithm and determines
73 * the algorithm as well.)
74 *
75 * \retval #PSA_SUCCESS
76 * Success.
77 * \retval #PSA_ERROR_INVALID_ARGUMENT
78 * The algorithm in \p cipher_suite is not a PAKE algorithm, or the
79 * PAKE primitive in \p cipher_suite is not compatible with the
80 * PAKE algorithm, or the hash algorithm in \p cipher_suite is invalid
81 * or not compatible with the PAKE algorithm and primitive.
82 * \retval #PSA_ERROR_NOT_SUPPORTED
83 * The algorithm in \p cipher_suite is not a supported PAKE algorithm,
84 * or the PAKE primitive in \p cipher_suite is not supported or not
85 * compatible with the PAKE algorithm, or the hash algorithm in
86 * \p cipher_suite is not supported or not compatible with the PAKE
87 * algorithm and primitive.
88 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
89 * \retval #PSA_ERROR_CORRUPTION_DETECTED
90 * \retval #PSA_ERROR_BAD_STATE
91 * The operation state is not valid, or
92 * the library has not been previously initialized by psa_crypto_init().
93 * It is implementation-dependent whether a failure to initialize
94 * results in this error code.
95 */
Przemek Stekiel6c764412022-11-22 14:05:12 +010096psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +020097 const psa_pake_cipher_suite_t *cipher_suite);
98
99/** Set the password for a password-authenticated key exchange from key ID.
100 *
101 * Call this function when the password, or a value derived from the password,
102 * is already present in the key store.
Przemek Stekiel6c764412022-11-22 14:05:12 +0100103 * \param[in] attributes The attributes of the key to use for the
104 * operation.
Neil Armstrong56b8d232022-06-01 18:05:57 +0200105 * \param[in,out] operation The operation object to set the password for. It
106 * must have been set up by psa_pake_setup() and
107 * not yet in use (neither psa_pake_output() nor
108 * psa_pake_input() has been called yet). It must
109 * be on operation for which the password hasn't
110 * been set yet (psa_pake_set_password_key()
111 * hasn't been called yet).
Przemek Stekiel6c764412022-11-22 14:05:12 +0100112 * \param password Buffer holding the password
113 * \param password_len Password buffer size
Neil Armstrong56b8d232022-06-01 18:05:57 +0200114 *
115 * \retval #PSA_SUCCESS
116 * Success.
117 * \retval #PSA_ERROR_INVALID_HANDLE
118 * \p password is not a valid key identifier.
119 * \retval #PSA_ERROR_NOT_PERMITTED
120 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
121 * permit the \p operation's algorithm.
122 * \retval #PSA_ERROR_INVALID_ARGUMENT
123 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
124 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
125 * the \p operation's cipher suite.
126 * \retval #PSA_ERROR_NOT_SUPPORTED
127 * The key type or key size of \p password is not supported with the
128 * \p operation's cipher suite.
129 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
130 * \retval #PSA_ERROR_CORRUPTION_DETECTED
131 * \retval #PSA_ERROR_STORAGE_FAILURE
132 * \retval #PSA_ERROR_DATA_CORRUPT
133 * \retval #PSA_ERROR_DATA_INVALID
134 * \retval #PSA_ERROR_BAD_STATE
135 * The operation state is not valid (it must have been set up.), or
136 * the library has not been previously initialized by psa_crypto_init().
137 * It is implementation-dependent whether a failure to initialize
138 * results in this error code.
139 */
140psa_status_t mbedtls_psa_pake_set_password_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100141 const psa_key_attributes_t *attributes,
142 mbedtls_psa_pake_operation_t *operation,
143 uint8_t *password,
144 size_t password_len);
Neil Armstrong56b8d232022-06-01 18:05:57 +0200145
146/** Set the user ID for a password-authenticated key exchange.
147 *
148 * Call this function to set the user ID. For PAKE algorithms that associate a
149 * user identifier with each side of the session you need to call
150 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
151 * user identifier with the session, call psa_pake_set_user() only.
152 *
153 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
154 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
155 * for more information.
156 *
157 * \param[in,out] operation The operation object to set the user ID for. It
158 * must have been set up by psa_pake_setup() and
159 * not yet in use (neither psa_pake_output() nor
160 * psa_pake_input() has been called yet). It must
161 * be on operation for which the user ID hasn't
162 * been set (psa_pake_set_user() hasn't been
163 * called yet).
164 * \param[in] user_id The user ID to authenticate with.
165 * \param user_id_len Size of the \p user_id buffer in bytes.
166 *
167 * \retval #PSA_SUCCESS
168 * Success.
169 * \retval #PSA_ERROR_INVALID_ARGUMENT
170 * \p user_id is not valid for the \p operation's algorithm and cipher
171 * suite.
172 * \retval #PSA_ERROR_NOT_SUPPORTED
173 * The value of \p user_id is not supported by the implementation.
174 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
175 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
176 * \retval #PSA_ERROR_CORRUPTION_DETECTED
177 * \retval #PSA_ERROR_BAD_STATE
178 * The operation state is not valid, or
179 * the library has not been previously initialized by psa_crypto_init().
180 * It is implementation-dependent whether a failure to initialize
181 * results in this error code.
182 */
Przemek Stekiel6c764412022-11-22 14:05:12 +0100183psa_status_t mbedtls_psa_pake_set_user(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200184 const uint8_t *user_id,
185 size_t user_id_len);
186
187/** Set the peer ID for a password-authenticated key exchange.
188 *
189 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
190 * that associate a user identifier with each side of the session. For PAKE
191 * algorithms that associate a single user identifier with the session, call
192 * psa_pake_set_user() only.
193 *
194 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
195 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
196 * for more information.
197 *
198 * \param[in,out] operation The operation object to set the peer ID for. It
199 * must have been set up by psa_pake_setup() and
200 * not yet in use (neither psa_pake_output() nor
201 * psa_pake_input() has been called yet). It must
202 * be on operation for which the peer ID hasn't
203 * been set (psa_pake_set_peer() hasn't been
204 * called yet).
205 * \param[in] peer_id The peer's ID to authenticate.
206 * \param peer_id_len Size of the \p peer_id buffer in bytes.
207 *
208 * \retval #PSA_SUCCESS
209 * Success.
210 * \retval #PSA_ERROR_INVALID_ARGUMENT
211 * \p user_id is not valid for the \p operation's algorithm and cipher
212 * suite.
213 * \retval #PSA_ERROR_NOT_SUPPORTED
214 * The algorithm doesn't associate a second identity with the session.
215 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
216 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
217 * \retval #PSA_ERROR_CORRUPTION_DETECTED
218 * \retval #PSA_ERROR_BAD_STATE
219 * Calling psa_pake_set_peer() is invalid with the \p operation's
220 * algorithm, the operation state is not valid, or the library has not
221 * been previously initialized by psa_crypto_init().
222 * It is implementation-dependent whether a failure to initialize
223 * results in this error code.
224 */
Przemek Stekiel6c764412022-11-22 14:05:12 +0100225psa_status_t mbedtls_psa_pake_set_peer(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200226 const uint8_t *peer_id,
227 size_t peer_id_len);
228
229/** Set the application role for a password-authenticated key exchange.
230 *
231 * Not all PAKE algorithms need to differentiate the communicating entities.
232 * It is optional to call this function for PAKEs that don't require a role
233 * to be specified. For such PAKEs the application role parameter is ignored,
234 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
235 *
236 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
237 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
238 * for more information.
239 *
240 * \param[in,out] operation The operation object to specify the
241 * application's role for. It must have been set up
242 * by psa_pake_setup() and not yet in use (neither
243 * psa_pake_output() nor psa_pake_input() has been
244 * called yet). It must be on operation for which
245 * the application's role hasn't been specified
246 * (psa_pake_set_role() hasn't been called yet).
247 * \param role A value of type ::psa_pake_role_t indicating the
248 * application's role in the PAKE the algorithm
249 * that is being set up. For more information see
250 * the documentation of \c PSA_PAKE_ROLE_XXX
251 * constants.
252 *
253 * \retval #PSA_SUCCESS
254 * Success.
255 * \retval #PSA_ERROR_INVALID_ARGUMENT
256 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
257 * \retval #PSA_ERROR_NOT_SUPPORTED
258 * The \p role for this algorithm is not supported or is not valid.
259 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
260 * \retval #PSA_ERROR_CORRUPTION_DETECTED
261 * \retval #PSA_ERROR_BAD_STATE
262 * The operation state is not valid, or
263 * the library has not been previously initialized by psa_crypto_init().
264 * It is implementation-dependent whether a failure to initialize
265 * results in this error code.
266 */
Przemek Stekiel6c764412022-11-22 14:05:12 +0100267psa_status_t mbedtls_psa_pake_set_role(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200268 psa_pake_role_t role);
269
270/** Get output for a step of a password-authenticated key exchange.
271 *
272 * Depending on the algorithm being executed, you might need to call this
273 * function several times or you might not need to call this at all.
274 *
275 * The exact sequence of calls to perform a password-authenticated key
276 * exchange depends on the algorithm in use. Refer to the documentation of
277 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
278 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
279 * information.
280 *
281 * If this function returns an error status, the operation enters an error
282 * state and must be aborted by calling psa_pake_abort().
283 *
284 * \param[in,out] operation Active PAKE operation.
285 * \param step The step of the algorithm for which the output is
286 * requested.
287 * \param[out] output Buffer where the output is to be written in the
288 * format appropriate for this \p step. Refer to
289 * the documentation of the individual
290 * \c PSA_PAKE_STEP_XXX constants for more
291 * information.
292 * \param output_size Size of the \p output buffer in bytes. This must
293 * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p
294 * primitive, \p step) where \p alg and
295 * \p primitive are the PAKE algorithm and primitive
296 * in the operation's cipher suite, and \p step is
297 * the output step.
298 *
299 * \param[out] output_length On success, the number of bytes of the returned
300 * output.
301 *
302 * \retval #PSA_SUCCESS
303 * Success.
304 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
305 * The size of the \p output buffer is too small.
306 * \retval #PSA_ERROR_INVALID_ARGUMENT
307 * \p step is not compatible with the operation's algorithm.
308 * \retval #PSA_ERROR_NOT_SUPPORTED
309 * \p step is not supported with the operation's algorithm.
310 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
311 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
312 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
313 * \retval #PSA_ERROR_CORRUPTION_DETECTED
314 * \retval #PSA_ERROR_STORAGE_FAILURE
315 * \retval #PSA_ERROR_DATA_CORRUPT
316 * \retval #PSA_ERROR_DATA_INVALID
317 * \retval #PSA_ERROR_BAD_STATE
318 * The operation state is not valid (it must be active, and fully set
319 * up, and this call must conform to the algorithm's requirements
320 * for ordering of input and output steps), or
321 * the library has not been previously initialized by psa_crypto_init().
322 * It is implementation-dependent whether a failure to initialize
323 * results in this error code.
324 */
Przemek Stekiel6c764412022-11-22 14:05:12 +0100325psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200326 psa_pake_step_t step,
327 uint8_t *output,
328 size_t output_size,
329 size_t *output_length);
330
331/** Provide input for a step of a password-authenticated key exchange.
332 *
333 * Depending on the algorithm being executed, you might need to call this
334 * function several times or you might not need to call this at all.
335 *
336 * The exact sequence of calls to perform a password-authenticated key
337 * exchange depends on the algorithm in use. Refer to the documentation of
338 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
339 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
340 * information.
341 *
342 * If this function returns an error status, the operation enters an error
343 * state and must be aborted by calling psa_pake_abort().
344 *
345 * \param[in,out] operation Active PAKE operation.
346 * \param step The step for which the input is provided.
347 * \param[in] input Buffer containing the input in the format
348 * appropriate for this \p step. Refer to the
349 * documentation of the individual
350 * \c PSA_PAKE_STEP_XXX constants for more
351 * information.
352 * \param input_length Size of the \p input buffer in bytes.
353 *
354 * \retval #PSA_SUCCESS
355 * Success.
356 * \retval #PSA_ERROR_INVALID_SIGNATURE
357 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
358 * \retval #PSA_ERROR_INVALID_ARGUMENT
359 * \p is not compatible with the \p operation’s algorithm, or the
360 * \p input is not valid for the \p operation's algorithm, cipher suite
361 * or \p step.
362 * \retval #PSA_ERROR_NOT_SUPPORTED
363 * \p step p is not supported with the \p operation's algorithm, or the
364 * \p input is not supported for the \p operation's algorithm, cipher
365 * suite or \p step.
366 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
367 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
368 * \retval #PSA_ERROR_CORRUPTION_DETECTED
369 * \retval #PSA_ERROR_STORAGE_FAILURE
370 * \retval #PSA_ERROR_DATA_CORRUPT
371 * \retval #PSA_ERROR_DATA_INVALID
372 * \retval #PSA_ERROR_BAD_STATE
373 * The operation state is not valid (it must be active, and fully set
374 * up, and this call must conform to the algorithm's requirements
375 * for ordering of input and output steps), or
376 * the library has not been previously initialized by psa_crypto_init().
377 * It is implementation-dependent whether a failure to initialize
378 * results in this error code.
379 */
Przemek Stekiel6c764412022-11-22 14:05:12 +0100380psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200381 psa_pake_step_t step,
382 const uint8_t *input,
383 size_t input_length);
384
385/** Get implicitly confirmed shared secret from a PAKE.
386 *
387 * At this point there is a cryptographic guarantee that only the authenticated
388 * party who used the same password is able to compute the key. But there is no
389 * guarantee that the peer is the party it claims to be and was able to do so.
390 *
391 * That is, the authentication is only implicit. Since the peer is not
392 * authenticated yet, no action should be taken yet that assumes that the peer
393 * is who it claims to be. For example, do not access restricted files on the
394 * peer's behalf until an explicit authentication has succeeded.
395 *
396 * This function can be called after the key exchange phase of the operation
397 * has completed. It imports the shared secret output of the PAKE into the
398 * provided derivation operation. The input step
399 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
400 * material in the key derivation operation.
401 *
402 * The exact sequence of calls to perform a password-authenticated key
403 * exchange depends on the algorithm in use. Refer to the documentation of
404 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
405 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
406 * information.
407 *
408 * When this function returns successfully, \p operation becomes inactive.
409 * If this function returns an error status, both \p operation
410 * and \p key_derivation operations enter an error state and must be aborted by
411 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
412 *
413 * \param[in,out] operation Active PAKE operation.
414 * \param[out] output A key derivation operation that is ready
415 * for an input step of type
416 * #PSA_KEY_DERIVATION_INPUT_SECRET.
417 *
418 * \retval #PSA_SUCCESS
419 * Success.
420 * \retval #PSA_ERROR_INVALID_ARGUMENT
421 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
422 * algorithm in the \p output key derivation operation.
423 * \retval #PSA_ERROR_NOT_SUPPORTED
424 * Input from a PAKE is not supported by the algorithm in the \p output
425 * key derivation operation.
426 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
427 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
428 * \retval #PSA_ERROR_CORRUPTION_DETECTED
429 * \retval #PSA_ERROR_STORAGE_FAILURE
430 * \retval #PSA_ERROR_DATA_CORRUPT
431 * \retval #PSA_ERROR_DATA_INVALID
432 * \retval #PSA_ERROR_BAD_STATE
433 * The PAKE operation state is not valid (it must be active, but beyond
434 * that validity is specific to the algorithm), or
435 * the library has not been previously initialized by psa_crypto_init(),
436 * or the state of \p output is not valid for
437 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
438 * step is out of order or the application has done this step already
439 * and it may not be repeated.
440 * It is implementation-dependent whether a failure to initialize
441 * results in this error code.
442 */
443psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100444 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel0c781802022-11-29 14:53:13 +0100445 uint8_t *output, size_t *output_size);
Neil Armstrong56b8d232022-06-01 18:05:57 +0200446
447/** Abort a PAKE operation.
448 *
449 * Aborting an operation frees all associated resources except for the \c
450 * operation structure itself. Once aborted, the operation object can be reused
451 * for another operation by calling psa_pake_setup() again.
452 *
453 * This function may be called at any time after the operation
454 * object has been initialized as described in #psa_pake_operation_t.
455 *
456 * In particular, calling psa_pake_abort() after the operation has been
457 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
458 * is safe and has no effect.
459 *
460 * \param[in,out] operation The operation to abort.
461 *
462 * \retval #PSA_SUCCESS
463 * Success.
464 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
465 * \retval #PSA_ERROR_CORRUPTION_DETECTED
466 * \retval #PSA_ERROR_BAD_STATE
467 * The library has not been previously initialized by psa_crypto_init().
468 * It is implementation-dependent whether a failure to initialize
469 * results in this error code.
470 */
Przemek Stekiel6c764412022-11-22 14:05:12 +0100471psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation);
Neil Armstrong56b8d232022-06-01 18:05:57 +0200472
473#endif /* PSA_CRYPTO_PAKE_H */