Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 1 | /* |
| 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 Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 96 | psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 97 | const psa_crypto_driver_pake_inputs_t *inputs); |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 98 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 99 | |
| 100 | /** Get output for a step of a password-authenticated key exchange. |
| 101 | * |
| 102 | * Depending on the algorithm being executed, you might need to call this |
| 103 | * function several times or you might not need to call this at all. |
| 104 | * |
| 105 | * The exact sequence of calls to perform a password-authenticated key |
| 106 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 107 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 108 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 109 | * information. |
| 110 | * |
| 111 | * If this function returns an error status, the operation enters an error |
| 112 | * state and must be aborted by calling psa_pake_abort(). |
| 113 | * |
| 114 | * \param[in,out] operation Active PAKE operation. |
| 115 | * \param step The step of the algorithm for which the output is |
| 116 | * requested. |
| 117 | * \param[out] output Buffer where the output is to be written in the |
| 118 | * format appropriate for this \p step. Refer to |
| 119 | * the documentation of the individual |
| 120 | * \c PSA_PAKE_STEP_XXX constants for more |
| 121 | * information. |
| 122 | * \param output_size Size of the \p output buffer in bytes. This must |
| 123 | * be at least #PSA_PAKE_OUTPUT_SIZE(\p alg, \p |
| 124 | * primitive, \p step) where \p alg and |
| 125 | * \p primitive are the PAKE algorithm and primitive |
| 126 | * in the operation's cipher suite, and \p step is |
| 127 | * the output step. |
| 128 | * |
| 129 | * \param[out] output_length On success, the number of bytes of the returned |
| 130 | * output. |
| 131 | * |
| 132 | * \retval #PSA_SUCCESS |
| 133 | * Success. |
| 134 | * \retval #PSA_ERROR_BUFFER_TOO_SMALL |
| 135 | * The size of the \p output buffer is too small. |
| 136 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 137 | * \p step is not compatible with the operation's algorithm. |
| 138 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 139 | * \p step is not supported with the operation's algorithm. |
| 140 | * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY |
| 141 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 142 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 143 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 144 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 145 | * \retval #PSA_ERROR_DATA_CORRUPT |
| 146 | * \retval #PSA_ERROR_DATA_INVALID |
| 147 | * \retval #PSA_ERROR_BAD_STATE |
| 148 | * The operation state is not valid (it must be active, and fully set |
| 149 | * up, and this call must conform to the algorithm's requirements |
| 150 | * for ordering of input and output steps), or |
| 151 | * the library has not been previously initialized by psa_crypto_init(). |
| 152 | * It is implementation-dependent whether a failure to initialize |
| 153 | * results in this error code. |
| 154 | */ |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 155 | psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 156 | psa_pake_step_t step, |
| 157 | uint8_t *output, |
| 158 | size_t output_size, |
| 159 | size_t *output_length); |
| 160 | |
| 161 | /** Provide input for a step of a password-authenticated key exchange. |
| 162 | * |
| 163 | * Depending on the algorithm being executed, you might need to call this |
| 164 | * function several times or you might not need to call this at all. |
| 165 | * |
| 166 | * The exact sequence of calls to perform a password-authenticated key |
| 167 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 168 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 169 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 170 | * information. |
| 171 | * |
| 172 | * If this function returns an error status, the operation enters an error |
| 173 | * state and must be aborted by calling psa_pake_abort(). |
| 174 | * |
| 175 | * \param[in,out] operation Active PAKE operation. |
| 176 | * \param step The step for which the input is provided. |
| 177 | * \param[in] input Buffer containing the input in the format |
| 178 | * appropriate for this \p step. Refer to the |
| 179 | * documentation of the individual |
| 180 | * \c PSA_PAKE_STEP_XXX constants for more |
| 181 | * information. |
| 182 | * \param input_length Size of the \p input buffer in bytes. |
| 183 | * |
| 184 | * \retval #PSA_SUCCESS |
| 185 | * Success. |
| 186 | * \retval #PSA_ERROR_INVALID_SIGNATURE |
| 187 | * The verification fails for a #PSA_PAKE_STEP_ZK_PROOF input step. |
| 188 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 189 | * \p is not compatible with the \p operation’s algorithm, or the |
| 190 | * \p input is not valid for the \p operation's algorithm, cipher suite |
| 191 | * or \p step. |
| 192 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 193 | * \p step p is not supported with the \p operation's algorithm, or the |
| 194 | * \p input is not supported for the \p operation's algorithm, cipher |
| 195 | * suite or \p step. |
| 196 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 197 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 198 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 199 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 200 | * \retval #PSA_ERROR_DATA_CORRUPT |
| 201 | * \retval #PSA_ERROR_DATA_INVALID |
| 202 | * \retval #PSA_ERROR_BAD_STATE |
| 203 | * The operation state is not valid (it must be active, and fully set |
| 204 | * up, and this call must conform to the algorithm's requirements |
| 205 | * for ordering of input and output steps), or |
| 206 | * the library has not been previously initialized by psa_crypto_init(). |
| 207 | * It is implementation-dependent whether a failure to initialize |
| 208 | * results in this error code. |
| 209 | */ |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 210 | psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 211 | psa_pake_step_t step, |
| 212 | const uint8_t *input, |
| 213 | size_t input_length); |
| 214 | |
| 215 | /** Get implicitly confirmed shared secret from a PAKE. |
| 216 | * |
| 217 | * At this point there is a cryptographic guarantee that only the authenticated |
| 218 | * party who used the same password is able to compute the key. But there is no |
| 219 | * guarantee that the peer is the party it claims to be and was able to do so. |
| 220 | * |
| 221 | * That is, the authentication is only implicit. Since the peer is not |
| 222 | * authenticated yet, no action should be taken yet that assumes that the peer |
| 223 | * is who it claims to be. For example, do not access restricted files on the |
| 224 | * peer's behalf until an explicit authentication has succeeded. |
| 225 | * |
| 226 | * This function can be called after the key exchange phase of the operation |
| 227 | * has completed. It imports the shared secret output of the PAKE into the |
| 228 | * provided derivation operation. The input step |
| 229 | * #PSA_KEY_DERIVATION_INPUT_SECRET is used when placing the shared key |
| 230 | * material in the key derivation operation. |
| 231 | * |
| 232 | * The exact sequence of calls to perform a password-authenticated key |
| 233 | * exchange depends on the algorithm in use. Refer to the documentation of |
| 234 | * individual PAKE algorithm types (`PSA_ALG_XXX` values of type |
| 235 | * ::psa_algorithm_t such that #PSA_ALG_IS_PAKE(\c alg) is true) for more |
| 236 | * information. |
| 237 | * |
| 238 | * When this function returns successfully, \p operation becomes inactive. |
| 239 | * If this function returns an error status, both \p operation |
| 240 | * and \p key_derivation operations enter an error state and must be aborted by |
| 241 | * calling psa_pake_abort() and psa_key_derivation_abort() respectively. |
| 242 | * |
| 243 | * \param[in,out] operation Active PAKE operation. |
| 244 | * \param[out] output A key derivation operation that is ready |
| 245 | * for an input step of type |
| 246 | * #PSA_KEY_DERIVATION_INPUT_SECRET. |
| 247 | * |
| 248 | * \retval #PSA_SUCCESS |
| 249 | * Success. |
| 250 | * \retval #PSA_ERROR_INVALID_ARGUMENT |
| 251 | * #PSA_KEY_DERIVATION_INPUT_SECRET is not compatible with the |
| 252 | * algorithm in the \p output key derivation operation. |
| 253 | * \retval #PSA_ERROR_NOT_SUPPORTED |
| 254 | * Input from a PAKE is not supported by the algorithm in the \p output |
| 255 | * key derivation operation. |
| 256 | * \retval #PSA_ERROR_INSUFFICIENT_MEMORY |
| 257 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 258 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 259 | * \retval #PSA_ERROR_STORAGE_FAILURE |
| 260 | * \retval #PSA_ERROR_DATA_CORRUPT |
| 261 | * \retval #PSA_ERROR_DATA_INVALID |
| 262 | * \retval #PSA_ERROR_BAD_STATE |
| 263 | * The PAKE operation state is not valid (it must be active, but beyond |
| 264 | * that validity is specific to the algorithm), or |
| 265 | * the library has not been previously initialized by psa_crypto_init(), |
| 266 | * or the state of \p output is not valid for |
| 267 | * the #PSA_KEY_DERIVATION_INPUT_SECRET step. This can happen if the |
| 268 | * step is out of order or the application has done this step already |
| 269 | * and it may not be repeated. |
| 270 | * It is implementation-dependent whether a failure to initialize |
| 271 | * results in this error code. |
| 272 | */ |
| 273 | psa_status_t mbedtls_psa_pake_get_implicit_key( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 274 | mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 275 | uint8_t *output, size_t *output_size); |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 276 | |
| 277 | /** Abort a PAKE operation. |
| 278 | * |
| 279 | * Aborting an operation frees all associated resources except for the \c |
| 280 | * operation structure itself. Once aborted, the operation object can be reused |
| 281 | * for another operation by calling psa_pake_setup() again. |
| 282 | * |
| 283 | * This function may be called at any time after the operation |
| 284 | * object has been initialized as described in #psa_pake_operation_t. |
| 285 | * |
| 286 | * In particular, calling psa_pake_abort() after the operation has been |
| 287 | * terminated by a call to psa_pake_abort() or psa_pake_get_implicit_key() |
| 288 | * is safe and has no effect. |
| 289 | * |
| 290 | * \param[in,out] operation The operation to abort. |
| 291 | * |
| 292 | * \retval #PSA_SUCCESS |
| 293 | * Success. |
| 294 | * \retval #PSA_ERROR_COMMUNICATION_FAILURE |
| 295 | * \retval #PSA_ERROR_CORRUPTION_DETECTED |
| 296 | * \retval #PSA_ERROR_BAD_STATE |
| 297 | * The library has not been previously initialized by psa_crypto_init(). |
| 298 | * It is implementation-dependent whether a failure to initialize |
| 299 | * results in this error code. |
| 300 | */ |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 301 | psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation); |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 302 | |
| 303 | #endif /* PSA_CRYPTO_PAKE_H */ |