blob: b61ddde100625dfae9773eb8c7687b3c7f47bacd [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 */
96psa_status_t mbedtls_psa_pake_setup(psa_pake_operation_t *operation,
97 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.
103 *
104 * \param[in,out] operation The operation object to set the password for. It
105 * must have been set up by psa_pake_setup() and
106 * not yet in use (neither psa_pake_output() nor
107 * psa_pake_input() has been called yet). It must
108 * be on operation for which the password hasn't
109 * been set yet (psa_pake_set_password_key()
110 * hasn't been called yet).
111 * \param password Identifier of the key holding the password or a
112 * value derived from the password (eg. by a
113 * memory-hard function). It must remain valid
114 * until the operation terminates. It must be of
115 * type #PSA_KEY_TYPE_PASSWORD or
116 * #PSA_KEY_TYPE_PASSWORD_HASH. It has to allow
117 * the usage #PSA_KEY_USAGE_DERIVE.
118 *
119 * \retval #PSA_SUCCESS
120 * Success.
121 * \retval #PSA_ERROR_INVALID_HANDLE
122 * \p password is not a valid key identifier.
123 * \retval #PSA_ERROR_NOT_PERMITTED
124 * The key does not have the #PSA_KEY_USAGE_DERIVE flag, or it does not
125 * permit the \p operation's algorithm.
126 * \retval #PSA_ERROR_INVALID_ARGUMENT
127 * The key type for \p password is not #PSA_KEY_TYPE_PASSWORD or
128 * #PSA_KEY_TYPE_PASSWORD_HASH, or \p password is not compatible with
129 * the \p operation's cipher suite.
130 * \retval #PSA_ERROR_NOT_SUPPORTED
131 * The key type or key size of \p password is not supported with the
132 * \p operation's cipher suite.
133 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
134 * \retval #PSA_ERROR_CORRUPTION_DETECTED
135 * \retval #PSA_ERROR_STORAGE_FAILURE
136 * \retval #PSA_ERROR_DATA_CORRUPT
137 * \retval #PSA_ERROR_DATA_INVALID
138 * \retval #PSA_ERROR_BAD_STATE
139 * The operation state is not valid (it must have been set up.), or
140 * the library has not been previously initialized by psa_crypto_init().
141 * It is implementation-dependent whether a failure to initialize
142 * results in this error code.
143 */
144psa_status_t mbedtls_psa_pake_set_password_key(
145 psa_pake_operation_t *operation,
146 mbedtls_svc_key_id_t password);
147
148/** Set the user ID for a password-authenticated key exchange.
149 *
150 * Call this function to set the user ID. For PAKE algorithms that associate a
151 * user identifier with each side of the session you need to call
152 * psa_pake_set_peer() as well. For PAKE algorithms that associate a single
153 * user identifier with the session, call psa_pake_set_user() only.
154 *
155 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
156 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
157 * for more information.
158 *
159 * \param[in,out] operation The operation object to set the user ID for. It
160 * must have been set up by psa_pake_setup() and
161 * not yet in use (neither psa_pake_output() nor
162 * psa_pake_input() has been called yet). It must
163 * be on operation for which the user ID hasn't
164 * been set (psa_pake_set_user() hasn't been
165 * called yet).
166 * \param[in] user_id The user ID to authenticate with.
167 * \param user_id_len Size of the \p user_id buffer in bytes.
168 *
169 * \retval #PSA_SUCCESS
170 * Success.
171 * \retval #PSA_ERROR_INVALID_ARGUMENT
172 * \p user_id is not valid for the \p operation's algorithm and cipher
173 * suite.
174 * \retval #PSA_ERROR_NOT_SUPPORTED
175 * The value of \p user_id is not supported by the implementation.
176 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
177 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
178 * \retval #PSA_ERROR_CORRUPTION_DETECTED
179 * \retval #PSA_ERROR_BAD_STATE
180 * The operation state is not valid, or
181 * the library has not been previously initialized by psa_crypto_init().
182 * It is implementation-dependent whether a failure to initialize
183 * results in this error code.
184 */
185psa_status_t mbedtls_psa_pake_set_user(psa_pake_operation_t *operation,
186 const uint8_t *user_id,
187 size_t user_id_len);
188
189/** Set the peer ID for a password-authenticated key exchange.
190 *
191 * Call this function in addition to psa_pake_set_user() for PAKE algorithms
192 * that associate a user identifier with each side of the session. For PAKE
193 * algorithms that associate a single user identifier with the session, call
194 * psa_pake_set_user() only.
195 *
196 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
197 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
198 * for more information.
199 *
200 * \param[in,out] operation The operation object to set the peer ID for. It
201 * must have been set up by psa_pake_setup() and
202 * not yet in use (neither psa_pake_output() nor
203 * psa_pake_input() has been called yet). It must
204 * be on operation for which the peer ID hasn't
205 * been set (psa_pake_set_peer() hasn't been
206 * called yet).
207 * \param[in] peer_id The peer's ID to authenticate.
208 * \param peer_id_len Size of the \p peer_id buffer in bytes.
209 *
210 * \retval #PSA_SUCCESS
211 * Success.
212 * \retval #PSA_ERROR_INVALID_ARGUMENT
213 * \p user_id is not valid for the \p operation's algorithm and cipher
214 * suite.
215 * \retval #PSA_ERROR_NOT_SUPPORTED
216 * The algorithm doesn't associate a second identity with the session.
217 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
218 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
219 * \retval #PSA_ERROR_CORRUPTION_DETECTED
220 * \retval #PSA_ERROR_BAD_STATE
221 * Calling psa_pake_set_peer() is invalid with the \p operation's
222 * algorithm, the operation state is not valid, or the library has not
223 * been previously initialized by psa_crypto_init().
224 * It is implementation-dependent whether a failure to initialize
225 * results in this error code.
226 */
227psa_status_t mbedtls_psa_pake_set_peer(psa_pake_operation_t *operation,
228 const uint8_t *peer_id,
229 size_t peer_id_len);
230
231/** Set the application role for a password-authenticated key exchange.
232 *
233 * Not all PAKE algorithms need to differentiate the communicating entities.
234 * It is optional to call this function for PAKEs that don't require a role
235 * to be specified. For such PAKEs the application role parameter is ignored,
236 * or #PSA_PAKE_ROLE_NONE can be passed as \c role.
237 *
238 * Refer to the documentation of individual PAKE algorithm types (`PSA_ALG_XXX`
239 * values of type ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true)
240 * for more information.
241 *
242 * \param[in,out] operation The operation object to specify the
243 * application's role for. It must have been set up
244 * by psa_pake_setup() and not yet in use (neither
245 * psa_pake_output() nor psa_pake_input() has been
246 * called yet). It must be on operation for which
247 * the application's role hasn't been specified
248 * (psa_pake_set_role() hasn't been called yet).
249 * \param role A value of type ::psa_pake_role_t indicating the
250 * application's role in the PAKE the algorithm
251 * that is being set up. For more information see
252 * the documentation of \c PSA_PAKE_ROLE_XXX
253 * constants.
254 *
255 * \retval #PSA_SUCCESS
256 * Success.
257 * \retval #PSA_ERROR_INVALID_ARGUMENT
258 * The \p role is not a valid PAKE role in the \p operation’s algorithm.
259 * \retval #PSA_ERROR_NOT_SUPPORTED
260 * The \p role for this algorithm is not supported or is not valid.
261 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
262 * \retval #PSA_ERROR_CORRUPTION_DETECTED
263 * \retval #PSA_ERROR_BAD_STATE
264 * The operation state is not valid, or
265 * the library has not been previously initialized by psa_crypto_init().
266 * It is implementation-dependent whether a failure to initialize
267 * results in this error code.
268 */
269psa_status_t mbedtls_psa_pake_set_role(psa_pake_operation_t *operation,
270 psa_pake_role_t role);
271
272/** Get output for a step of a password-authenticated key exchange.
273 *
274 * Depending on the algorithm being executed, you might need to call this
275 * function several times or you might not need to call this at all.
276 *
277 * The exact sequence of calls to perform a password-authenticated key
278 * exchange depends on the algorithm in use. Refer to the documentation of
279 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
280 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
281 * information.
282 *
283 * If this function returns an error status, the operation enters an error
284 * state and must be aborted by calling psa_pake_abort().
285 *
286 * \param[in,out] operation Active PAKE operation.
287 * \param step The step of the algorithm for which the output is
288 * requested.
289 * \param[out] output Buffer where the output is to be written in the
290 * format appropriate for this \p step. Refer to
291 * the documentation of the individual
292 * \c PSA_PAKE_STEP_XXX constants for more
293 * information.
294 * \param output_size Size of the \p output buffer in bytes. This must
295 * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p
296 * primitive, \p step) where \p alg and
297 * \p primitive are the PAKE algorithm and primitive
298 * in the operation's cipher suite, and \p step is
299 * the output step.
300 *
301 * \param[out] output_length On success, the number of bytes of the returned
302 * output.
303 *
304 * \retval #PSA_SUCCESS
305 * Success.
306 * \retval #PSA_ERROR_BUFFER_TOO_SMALL
307 * The size of the \p output buffer is too small.
308 * \retval #PSA_ERROR_INVALID_ARGUMENT
309 * \p step is not compatible with the operation's algorithm.
310 * \retval #PSA_ERROR_NOT_SUPPORTED
311 * \p step is not supported with the operation's algorithm.
312 * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
313 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
314 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
315 * \retval #PSA_ERROR_CORRUPTION_DETECTED
316 * \retval #PSA_ERROR_STORAGE_FAILURE
317 * \retval #PSA_ERROR_DATA_CORRUPT
318 * \retval #PSA_ERROR_DATA_INVALID
319 * \retval #PSA_ERROR_BAD_STATE
320 * The operation state is not valid (it must be active, and fully set
321 * up, and this call must conform to the algorithm's requirements
322 * for ordering of input and output steps), or
323 * the library has not been previously initialized by psa_crypto_init().
324 * It is implementation-dependent whether a failure to initialize
325 * results in this error code.
326 */
327psa_status_t mbedtls_psa_pake_output(psa_pake_operation_t *operation,
328 psa_pake_step_t step,
329 uint8_t *output,
330 size_t output_size,
331 size_t *output_length);
332
333/** Provide input for a step of a password-authenticated key exchange.
334 *
335 * Depending on the algorithm being executed, you might need to call this
336 * function several times or you might not need to call this at all.
337 *
338 * The exact sequence of calls to perform a password-authenticated key
339 * exchange depends on the algorithm in use. Refer to the documentation of
340 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
341 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
342 * information.
343 *
344 * If this function returns an error status, the operation enters an error
345 * state and must be aborted by calling psa_pake_abort().
346 *
347 * \param[in,out] operation Active PAKE operation.
348 * \param step The step for which the input is provided.
349 * \param[in] input Buffer containing the input in the format
350 * appropriate for this \p step. Refer to the
351 * documentation of the individual
352 * \c PSA_PAKE_STEP_XXX constants for more
353 * information.
354 * \param input_length Size of the \p input buffer in bytes.
355 *
356 * \retval #PSA_SUCCESS
357 * Success.
358 * \retval #PSA_ERROR_INVALID_SIGNATURE
359 * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step.
360 * \retval #PSA_ERROR_INVALID_ARGUMENT
361 * \p is not compatible with the \p operation’s algorithm, or the
362 * \p input is not valid for the \p operation's algorithm, cipher suite
363 * or \p step.
364 * \retval #PSA_ERROR_NOT_SUPPORTED
365 * \p step p is not supported with the \p operation's algorithm, or the
366 * \p input is not supported for the \p operation's algorithm, cipher
367 * suite or \p step.
368 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
369 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
370 * \retval #PSA_ERROR_CORRUPTION_DETECTED
371 * \retval #PSA_ERROR_STORAGE_FAILURE
372 * \retval #PSA_ERROR_DATA_CORRUPT
373 * \retval #PSA_ERROR_DATA_INVALID
374 * \retval #PSA_ERROR_BAD_STATE
375 * The operation state is not valid (it must be active, and fully set
376 * up, and this call must conform to the algorithm's requirements
377 * for ordering of input and output steps), or
378 * the library has not been previously initialized by psa_crypto_init().
379 * It is implementation-dependent whether a failure to initialize
380 * results in this error code.
381 */
382psa_status_t mbedtls_psa_pake_input(psa_pake_operation_t *operation,
383 psa_pake_step_t step,
384 const uint8_t *input,
385 size_t input_length);
386
387/** Get implicitly confirmed shared secret from a PAKE.
388 *
389 * At this point there is a cryptographic guarantee that only the authenticated
390 * party who used the same password is able to compute the key. But there is no
391 * guarantee that the peer is the party it claims to be and was able to do so.
392 *
393 * That is, the authentication is only implicit. Since the peer is not
394 * authenticated yet, no action should be taken yet that assumes that the peer
395 * is who it claims to be. For example, do not access restricted files on the
396 * peer's behalf until an explicit authentication has succeeded.
397 *
398 * This function can be called after the key exchange phase of the operation
399 * has completed. It imports the shared secret output of the PAKE into the
400 * provided derivation operation. The input step
401 * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key
402 * material in the key derivation operation.
403 *
404 * The exact sequence of calls to perform a password-authenticated key
405 * exchange depends on the algorithm in use. Refer to the documentation of
406 * individual PAKE algorithm types (`PSA_ALG_XXX` values of type
407 * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more
408 * information.
409 *
410 * When this function returns successfully, \p operation becomes inactive.
411 * If this function returns an error status, both \p operation
412 * and \p key_derivation operations enter an error state and must be aborted by
413 * calling psa_pake_abort() and psa_key_derivation_abort() respectively.
414 *
415 * \param[in,out] operation Active PAKE operation.
416 * \param[out] output A key derivation operation that is ready
417 * for an input step of type
418 * #PSA_KEY_DERIVATION_INPUT_SECRET.
419 *
420 * \retval #PSA_SUCCESS
421 * Success.
422 * \retval #PSA_ERROR_INVALID_ARGUMENT
423 * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the
424 * algorithm in the \p output key derivation operation.
425 * \retval #PSA_ERROR_NOT_SUPPORTED
426 * Input from a PAKE is not supported by the algorithm in the \p output
427 * key derivation operation.
428 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
429 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
430 * \retval #PSA_ERROR_CORRUPTION_DETECTED
431 * \retval #PSA_ERROR_STORAGE_FAILURE
432 * \retval #PSA_ERROR_DATA_CORRUPT
433 * \retval #PSA_ERROR_DATA_INVALID
434 * \retval #PSA_ERROR_BAD_STATE
435 * The PAKE operation state is not valid (it must be active, but beyond
436 * that validity is specific to the algorithm), or
437 * the library has not been previously initialized by psa_crypto_init(),
438 * or the state of \p output is not valid for
439 * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the
440 * step is out of order or the application has done this step already
441 * and it may not be repeated.
442 * It is implementation-dependent whether a failure to initialize
443 * results in this error code.
444 */
445psa_status_t mbedtls_psa_pake_get_implicit_key(
446 psa_pake_operation_t *operation,
447 psa_key_derivation_operation_t *output);
448
449/** Abort a PAKE operation.
450 *
451 * Aborting an operation frees all associated resources except for the \c
452 * operation structure itself. Once aborted, the operation object can be reused
453 * for another operation by calling psa_pake_setup() again.
454 *
455 * This function may be called at any time after the operation
456 * object has been initialized as described in #psa_pake_operation_t.
457 *
458 * In particular, calling psa_pake_abort() after the operation has been
459 * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key()
460 * is safe and has no effect.
461 *
462 * \param[in,out] operation The operation to abort.
463 *
464 * \retval #PSA_SUCCESS
465 * Success.
466 * \retval #PSA_ERROR_COMMUNICATION_FAILURE
467 * \retval #PSA_ERROR_CORRUPTION_DETECTED
468 * \retval #PSA_ERROR_BAD_STATE
469 * The library has not been previously initialized by psa_crypto_init().
470 * It is implementation-dependent whether a failure to initialize
471 * results in this error code.
472 */
473psa_status_t mbedtls_psa_pake_abort(psa_pake_operation_t *operation);
474
475#endif /* PSA_CRYPTO_PAKE_H */