Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +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 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa/crypto.h> |
| 26 | #include "psa_crypto_core.h" |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 27 | #include "psa_crypto_pake.h" |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 28 | #include "psa_crypto_slot_management.h" |
| 29 | |
| 30 | #include <mbedtls/ecjpake.h> |
| 31 | #include <mbedtls/psa_util.h> |
| 32 | |
| 33 | #include <mbedtls/platform.h> |
| 34 | #include <mbedtls/error.h> |
| 35 | #include <string.h> |
| 36 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 37 | /* |
| 38 | * State sequence: |
| 39 | * |
| 40 | * psa_pake_setup() |
| 41 | * | |
| 42 | * |-- In any order: |
| 43 | * | | psa_pake_set_password_key() |
| 44 | * | | psa_pake_set_user() |
| 45 | * | | psa_pake_set_peer() |
Neil Armstrong | c29f847 | 2022-06-08 13:34:49 +0200 | [diff] [blame] | 46 | * | | psa_pake_set_role() |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 47 | * | |
| 48 | * |--- In any order: (First round input before or after first round output) |
| 49 | * | | |
| 50 | * | |------ In Order |
| 51 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 52 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 53 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 54 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 55 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 56 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 57 | * | | |
| 58 | * | |------ In Order: |
| 59 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 60 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 61 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 62 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 63 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 64 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 65 | * | |
| 66 | * |--- In any order: (Second round input before or after second round output) |
| 67 | * | | |
| 68 | * | |------ In Order |
| 69 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 70 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 71 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 72 | * | | |
| 73 | * | |------ In Order: |
| 74 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 75 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 76 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 77 | * | |
| 78 | * psa_pake_get_implicit_key() |
| 79 | * psa_pake_abort() |
| 80 | */ |
| 81 | |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 82 | /* |
| 83 | * The first PAKE step shares the same sequences of the second PAKE step |
| 84 | * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs. |
Neil Armstrong | b39833c | 2022-09-06 11:36:02 +0200 | [diff] [blame] | 85 | * It's simpler to share the same sequences numbers of the first |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 86 | * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps. |
| 87 | * |
| 88 | * State sequence with step, state & sequence enums: |
| 89 | * => Input & Output Step = PSA_PAKE_STEP_INVALID |
| 90 | * => state = PSA_PAKE_STATE_INVALID |
| 91 | * psa_pake_setup() |
| 92 | * => Input & Output Step = PSA_PAKE_STEP_X1_X2 |
| 93 | * => state = PSA_PAKE_STATE_SETUP |
| 94 | * => sequence = PSA_PAKE_SEQ_INVALID |
| 95 | * | |
| 96 | * |--- In any order: (First round input before or after first round output) |
| 97 | * | | First call of psa_pake_output() or psa_pake_input() sets |
| 98 | * | | state = PSA_PAKE_STATE_READY |
| 99 | * | | |
| 100 | * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2 |
| 101 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 102 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 103 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 104 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE |
| 105 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC |
| 106 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF |
| 107 | * | | | => state = PSA_PAKE_STATE_READY |
| 108 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 109 | * | | | => Output Step = PSA_PAKE_STEP_X2S |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 110 | * | | |
| 111 | * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2 |
| 112 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 113 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 114 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 115 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE |
| 116 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC |
| 117 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF |
| 118 | * | | | => state = PSA_PAKE_STATE_READY |
| 119 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 120 | * | | | => Output Step = PSA_PAKE_INPUT_X4S |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 121 | * | |
| 122 | * |--- In any order: (Second round input before or after second round output) |
| 123 | * | | |
| 124 | * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S |
| 125 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 126 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 127 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 128 | * | | | => state = PSA_PAKE_STATE_READY |
| 129 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 130 | * | | | => Output Step = PSA_PAKE_STEP_DERIVE |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 131 | * | | |
| 132 | * | |------ In Order: => state = PSA_PAKE_INPUT_X4S |
| 133 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 134 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 135 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 136 | * | | | => state = PSA_PAKE_STATE_READY |
| 137 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 138 | * | | | => Output Step = PSA_PAKE_STEP_DERIVE |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 139 | * | |
| 140 | * psa_pake_get_implicit_key() |
| 141 | * => Input & Output Step = PSA_PAKE_STEP_INVALID |
| 142 | */ |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 143 | |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 144 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 145 | static psa_status_t mbedtls_ecjpake_to_psa_error(int ret) |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 146 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | switch (ret) { |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 148 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 149 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
| 150 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
| 151 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | return PSA_ERROR_DATA_INVALID; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 153 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 154 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 156 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 157 | return PSA_ERROR_NOT_SUPPORTED; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 158 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | return PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 160 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | return PSA_ERROR_GENERIC_ERROR; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | #endif |
| 165 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 166 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 167 | static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 168 | { |
| 169 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 170 | mbedtls_ecjpake_role role; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 171 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | if (operation->role == PSA_PAKE_ROLE_CLIENT) { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 173 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | } else if (operation->role == PSA_PAKE_ROLE_SERVER) { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 175 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | } else { |
| 177 | return PSA_ERROR_BAD_STATE; |
| 178 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | if (operation->password_len == 0) { |
| 181 | return PSA_ERROR_BAD_STATE; |
| 182 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 183 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 184 | ret = mbedtls_ecjpake_setup(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | role, |
| 186 | MBEDTLS_MD_SHA256, |
| 187 | MBEDTLS_ECP_DP_SECP256R1, |
| 188 | operation->password, |
| 189 | operation->password_len); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | mbedtls_platform_zeroize(operation->password, operation->password_len); |
| 192 | mbedtls_free(operation->password); |
Przemek Stekiel | ad0f357 | 2022-11-21 15:04:37 +0100 | [diff] [blame] | 193 | operation->password = NULL; |
| 194 | operation->password_len = 0; |
| 195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | if (ret != 0) { |
| 197 | return mbedtls_ecjpake_to_psa_error(ret); |
| 198 | } |
Przemek Stekiel | 0bdec19 | 2022-11-22 09:10:35 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | return PSA_SUCCESS; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 201 | } |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 202 | |
| 203 | psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation, |
| 204 | const psa_crypto_driver_pake_inputs_t *inputs) |
| 205 | { |
| 206 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 5cbca79 | 2023-01-17 16:51:19 +0100 | [diff] [blame] | 207 | size_t password_len = 0; |
| 208 | psa_pake_role_t role = PSA_PAKE_ROLE_NONE; |
| 209 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 210 | size_t actual_password_len = 0; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 211 | |
Przemek Stekiel | 5cbca79 | 2023-01-17 16:51:19 +0100 | [diff] [blame] | 212 | status = psa_crypto_driver_pake_get_password_len(inputs, &password_len); |
| 213 | if (status != PSA_SUCCESS) { |
| 214 | return status; |
| 215 | } |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 216 | |
Przemek Stekiel | 5cbca79 | 2023-01-17 16:51:19 +0100 | [diff] [blame] | 217 | status = psa_crypto_driver_pake_get_role(inputs, &role); |
| 218 | if (status != PSA_SUCCESS) { |
| 219 | return status; |
| 220 | } |
| 221 | |
| 222 | status = psa_crypto_driver_pake_get_cipher_suite(inputs, &cipher_suite); |
| 223 | if (status != PSA_SUCCESS) { |
| 224 | return status; |
| 225 | } |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 226 | |
| 227 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
| 228 | if (cipher_suite.algorithm == PSA_ALG_JPAKE) { |
| 229 | if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC || |
| 230 | cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 || |
| 231 | cipher_suite.bits != 256 || |
| 232 | cipher_suite.hash != PSA_ALG_SHA_256) { |
| 233 | status = PSA_ERROR_NOT_SUPPORTED; |
| 234 | goto error; |
| 235 | } |
| 236 | |
| 237 | if (role != PSA_PAKE_ROLE_CLIENT && |
| 238 | role != PSA_PAKE_ROLE_SERVER) { |
| 239 | status = PSA_ERROR_NOT_SUPPORTED; |
| 240 | goto error; |
| 241 | } |
| 242 | |
| 243 | mbedtls_ecjpake_init(&operation->ctx.pake); |
| 244 | |
Przemek Stekiel | 2797d37 | 2022-12-22 11:19:22 +0100 | [diff] [blame] | 245 | operation->password = mbedtls_calloc(1, password_len); |
| 246 | if (operation->password == NULL) { |
| 247 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
| 248 | goto error; |
| 249 | } |
| 250 | |
Przemek Stekiel | 5cbca79 | 2023-01-17 16:51:19 +0100 | [diff] [blame] | 251 | status = psa_crypto_driver_pake_get_password(inputs, operation->password, |
| 252 | password_len, &actual_password_len); |
| 253 | if (status != PSA_SUCCESS) { |
| 254 | goto error; |
| 255 | } |
| 256 | |
| 257 | operation->password_len = actual_password_len; |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 258 | operation->role = role; |
| 259 | operation->alg = cipher_suite.algorithm; |
| 260 | |
| 261 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
| 262 | operation->buffer_length = 0; |
| 263 | operation->buffer_offset = 0; |
| 264 | |
| 265 | status = psa_pake_ecjpake_setup(operation); |
| 266 | |
| 267 | if (status != PSA_SUCCESS) { |
| 268 | goto error; |
| 269 | } |
| 270 | |
| 271 | return PSA_SUCCESS; |
| 272 | } else |
| 273 | #else |
| 274 | (void) operation; |
| 275 | (void) inputs; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 276 | #endif |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 277 | { status = PSA_ERROR_NOT_SUPPORTED; } |
| 278 | |
| 279 | error: |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 280 | mbedtls_psa_pake_abort(operation); |
| 281 | return status; |
| 282 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 283 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 284 | static psa_status_t mbedtls_psa_pake_output_internal( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 285 | mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 286 | psa_pake_driver_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | uint8_t *output, |
| 288 | size_t output_size, |
| 289 | size_t *output_length) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 290 | { |
| 291 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 292 | size_t length; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 293 | (void) step; // Unused parameter |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 294 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 295 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 296 | /* |
| 297 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 298 | * handling of output sequencing. |
| 299 | * |
Neil Armstrong | 6a12a77 | 2022-09-14 12:17:42 +0200 | [diff] [blame] | 300 | * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 301 | * at once, on the other side the PSA CRYPTO PAKE api requires |
| 302 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be |
| 303 | * retrieved in sequence. |
| 304 | * |
| 305 | * In order to achieve API compatibility, the whole X1+X2 or X2S steps |
| 306 | * data is stored in an intermediate buffer at first step output call, |
| 307 | * and data is sliced down by parsing the ECPoint records in order |
| 308 | * to return the right parts on each step. |
| 309 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | if (operation->alg == PSA_ALG_JPAKE) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 311 | /* Initialize & write round on KEY_SHARE sequences */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 312 | if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 313 | ret = mbedtls_ecjpake_write_round_one(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | operation->buffer, |
| 315 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 316 | &operation->buffer_length, |
| 317 | mbedtls_psa_get_random, |
| 318 | MBEDTLS_PSA_RANDOM_STATE); |
| 319 | if (ret != 0) { |
| 320 | return mbedtls_ecjpake_to_psa_error(ret); |
| 321 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 322 | |
| 323 | operation->buffer_offset = 0; |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 324 | } else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 325 | ret = mbedtls_ecjpake_write_round_two(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | operation->buffer, |
| 327 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 328 | &operation->buffer_length, |
| 329 | mbedtls_psa_get_random, |
| 330 | MBEDTLS_PSA_RANDOM_STATE); |
| 331 | if (ret != 0) { |
| 332 | return mbedtls_ecjpake_to_psa_error(ret); |
| 333 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 334 | |
| 335 | operation->buffer_offset = 0; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 336 | } |
| 337 | |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 338 | /* |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 339 | * mbedtls_ecjpake_write_round_xxx() outputs thing in the format |
| 340 | * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is |
| 341 | * that the data for each step is prepended with a length byte, and |
| 342 | * then they're concatenated. Additionally, the server's second round |
| 343 | * output is prepended with a 3-bytes ECParameters structure. |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 344 | * |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 345 | * In PSA, we output each step separately, and don't prepend the |
| 346 | * output with a length byte, even less a curve identifier, as that |
| 347 | * information is already available. |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 348 | */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 349 | if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | operation->role == PSA_PAKE_ROLE_SERVER) { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 351 | /* Skip ECParameters, with is 3 bytes (RFC 8422) */ |
| 352 | operation->buffer_offset += 3; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 353 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 354 | |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 355 | /* Read the length byte then move past it to the data */ |
| 356 | length = operation->buffer[operation->buffer_offset]; |
| 357 | operation->buffer_offset += 1; |
| 358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | if (operation->buffer_offset + length > operation->buffer_length) { |
| 360 | return PSA_ERROR_DATA_CORRUPT; |
| 361 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | if (output_size < length) { |
| 364 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 365 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | memcpy(output, |
| 368 | operation->buffer + operation->buffer_offset, |
| 369 | length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 370 | *output_length = length; |
| 371 | |
| 372 | operation->buffer_offset += length; |
| 373 | |
| 374 | /* Reset buffer after ZK_PROOF sequence */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 375 | if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) || |
| 376 | (step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 378 | operation->buffer_length = 0; |
| 379 | operation->buffer_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | return PSA_SUCCESS; |
| 383 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 384 | #else |
| 385 | (void) step; |
| 386 | (void) output; |
| 387 | (void) output_size; |
| 388 | (void) output_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 389 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 390 | { return PSA_ERROR_NOT_SUPPORTED; } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 393 | psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 394 | psa_pake_driver_step_t step, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 395 | uint8_t *output, |
| 396 | size_t output_size, |
| 397 | size_t *output_length) |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 398 | { |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 399 | psa_status_t status = mbedtls_psa_pake_output_internal( |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 400 | operation, step, output, output_size, output_length); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 403 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | } |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 405 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | return status; |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 409 | static psa_status_t mbedtls_psa_pake_input_internal( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 410 | mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 411 | psa_pake_driver_step_t step, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | const uint8_t *input, |
| 413 | size_t input_length) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 414 | { |
| 415 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 416 | (void) step; // Unused parameter |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 417 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 418 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 419 | /* |
| 420 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 421 | * handling of input sequencing. |
| 422 | * |
| 423 | * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data |
| 424 | * at once as input, on the other side the PSA CRYPTO PAKE api requires |
| 425 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be |
| 426 | * given in sequence. |
| 427 | * |
| 428 | * In order to achieve API compatibility, each X1+X2 or X4S step data |
| 429 | * is stored sequentially in an intermediate buffer and given to the |
| 430 | * MbedTLS JPAKE API on the last step. |
| 431 | * |
| 432 | * This causes any input error to be only detected on the last step. |
| 433 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | if (operation->alg == PSA_ALG_JPAKE) { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 435 | /* |
| 436 | * Copy input to local buffer and format it as the Mbed TLS API |
| 437 | * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7. |
| 438 | * The summary is that the data for each step is prepended with a |
| 439 | * length byte, and then they're concatenated. Additionally, the |
| 440 | * server's second round output is prepended with a 3-bytes |
| 441 | * ECParameters structure - which means we have to prepend that when |
| 442 | * we're a client. |
| 443 | */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 444 | if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | operation->role == PSA_PAKE_ROLE_CLIENT) { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 446 | /* We only support secp256r1. */ |
| 447 | /* This is the ECParameters structure defined by RFC 8422. */ |
| 448 | unsigned char ecparameters[3] = { |
| 449 | 3, /* named_curve */ |
| 450 | 0, 23 /* secp256r1 */ |
| 451 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | memcpy(operation->buffer + operation->buffer_length, |
| 453 | ecparameters, sizeof(ecparameters)); |
| 454 | operation->buffer_length += sizeof(ecparameters); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* Write the length byte */ |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 458 | operation->buffer[operation->buffer_length] = (uint8_t) input_length; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 459 | operation->buffer_length += 1; |
| 460 | |
| 461 | /* Finally copy the data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | memcpy(operation->buffer + operation->buffer_length, |
| 463 | input, input_length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 464 | operation->buffer_length += input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 465 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 466 | /* Load buffer at each last round ZK_PROOF */ |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 467 | if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 468 | ret = mbedtls_ecjpake_read_round_one(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 469 | operation->buffer, |
| 470 | operation->buffer_length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 473 | operation->buffer_length = 0; |
| 474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | if (ret != 0) { |
| 476 | return mbedtls_ecjpake_to_psa_error(ret); |
| 477 | } |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 478 | } else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 479 | ret = mbedtls_ecjpake_read_round_two(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 480 | operation->buffer, |
| 481 | operation->buffer_length); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 484 | operation->buffer_length = 0; |
| 485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | if (ret != 0) { |
| 487 | return mbedtls_ecjpake_to_psa_error(ret); |
| 488 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | return PSA_SUCCESS; |
| 492 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 493 | #else |
| 494 | (void) step; |
| 495 | (void) input; |
| 496 | (void) input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 497 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 498 | { return PSA_ERROR_NOT_SUPPORTED; } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 501 | psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 502 | psa_pake_driver_step_t step, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 503 | const uint8_t *input, |
| 504 | size_t input_length) |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 505 | { |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 506 | psa_status_t status = mbedtls_psa_pake_input_internal( |
Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 507 | operation, step, input, input_length); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 510 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | } |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | return status; |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 516 | psa_status_t mbedtls_psa_pake_get_implicit_key( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 517 | mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 518 | uint8_t *output, size_t *output_size) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 519 | { |
| 520 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 521 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 522 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 524 | ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | operation->buffer, |
| 526 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 527 | &operation->buffer_length, |
| 528 | mbedtls_psa_get_random, |
| 529 | MBEDTLS_PSA_RANDOM_STATE); |
| 530 | if (ret != 0) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 531 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | return mbedtls_ecjpake_to_psa_error(ret); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 533 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 534 | |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 535 | memcpy(output, operation->buffer, operation->buffer_length); |
| 536 | *output_size = operation->buffer_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 539 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 540 | mbedtls_psa_pake_abort(operation); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 541 | |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 542 | return PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 544 | #else |
| 545 | (void) output; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 546 | #endif |
Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 547 | { return PSA_ERROR_NOT_SUPPORTED; } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 550 | psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 551 | { |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 552 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | if (operation->alg == PSA_ALG_JPAKE) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | if (operation->password_len > 0) { |
| 556 | mbedtls_platform_zeroize(operation->password, operation->password_len); |
| 557 | } |
| 558 | mbedtls_free(operation->password); |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 559 | operation->password = NULL; |
| 560 | operation->password_len = 0; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 561 | operation->role = PSA_PAKE_ROLE_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 563 | operation->buffer_length = 0; |
| 564 | operation->buffer_offset = 0; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 565 | mbedtls_ecjpake_free(&operation->ctx.pake); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 566 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 567 | #endif |
| 568 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 569 | operation->alg = PSA_ALG_NONE; |
Neil Armstrong | fbc4b4a | 2022-06-10 08:54:53 +0200 | [diff] [blame] | 570 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | return PSA_SUCCESS; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ |
| 575 | |
| 576 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |