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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | enum psa_pake_step { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 83 | PSA_PAKE_STEP_INVALID = 0, |
| 84 | PSA_PAKE_STEP_X1_X2 = 1, |
| 85 | PSA_PAKE_STEP_X2S = 2, |
| 86 | PSA_PAKE_STEP_DERIVE = 3, |
| 87 | }; |
| 88 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | enum psa_pake_state { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 90 | PSA_PAKE_STATE_INVALID = 0, |
| 91 | PSA_PAKE_STATE_SETUP = 1, |
| 92 | PSA_PAKE_STATE_READY = 2, |
| 93 | PSA_PAKE_OUTPUT_X1_X2 = 3, |
| 94 | PSA_PAKE_OUTPUT_X2S = 4, |
| 95 | PSA_PAKE_INPUT_X1_X2 = 5, |
| 96 | PSA_PAKE_INPUT_X4S = 6, |
| 97 | }; |
| 98 | |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 99 | /* |
| 100 | * The first PAKE step shares the same sequences of the second PAKE step |
| 101 | * 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] | 102 | * It's simpler to share the same sequences numbers of the first |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 103 | * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps. |
| 104 | * |
| 105 | * State sequence with step, state & sequence enums: |
| 106 | * => Input & Output Step = PSA_PAKE_STEP_INVALID |
| 107 | * => state = PSA_PAKE_STATE_INVALID |
| 108 | * psa_pake_setup() |
| 109 | * => Input & Output Step = PSA_PAKE_STEP_X1_X2 |
| 110 | * => state = PSA_PAKE_STATE_SETUP |
| 111 | * => sequence = PSA_PAKE_SEQ_INVALID |
| 112 | * | |
| 113 | * |--- In any order: (First round input before or after first round output) |
| 114 | * | | First call of psa_pake_output() or psa_pake_input() sets |
| 115 | * | | state = PSA_PAKE_STATE_READY |
| 116 | * | | |
| 117 | * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2 |
| 118 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 119 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 120 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 121 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE |
| 122 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC |
| 123 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF |
| 124 | * | | | => state = PSA_PAKE_STATE_READY |
| 125 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 126 | * | | | => Output Step = PSA_PAKE_STEP_X2S |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 127 | * | | |
| 128 | * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2 |
| 129 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 130 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 131 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 132 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE |
| 133 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC |
| 134 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF |
| 135 | * | | | => state = PSA_PAKE_STATE_READY |
| 136 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 137 | * | | | => Output Step = PSA_PAKE_INPUT_X4S |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 138 | * | |
| 139 | * |--- In any order: (Second round input before or after second round output) |
| 140 | * | | |
| 141 | * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S |
| 142 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 143 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 144 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 145 | * | | | => state = PSA_PAKE_STATE_READY |
| 146 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 147 | * | | | => Output Step = PSA_PAKE_STEP_DERIVE |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 148 | * | | |
| 149 | * | |------ In Order: => state = PSA_PAKE_INPUT_X4S |
| 150 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 151 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 152 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 153 | * | | | => state = PSA_PAKE_STATE_READY |
| 154 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 155 | * | | | => Output Step = PSA_PAKE_STEP_DERIVE |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 156 | * | |
| 157 | * psa_pake_get_implicit_key() |
| 158 | * => Input & Output Step = PSA_PAKE_STEP_INVALID |
| 159 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | enum psa_pake_sequence { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 161 | PSA_PAKE_SEQ_INVALID = 0, |
| 162 | PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */ |
| 163 | PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */ |
| 164 | PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */ |
| 165 | PSA_PAKE_X2_STEP_KEY_SHARE = 4, |
| 166 | PSA_PAKE_X2_STEP_ZK_PUBLIC = 5, |
| 167 | PSA_PAKE_X2_STEP_ZK_PROOF = 6, |
| 168 | PSA_PAKE_SEQ_END = 7, |
| 169 | }; |
| 170 | |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 171 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | static psa_status_t mbedtls_ecjpake_to_psa_error(int ret) |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 173 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | switch (ret) { |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 175 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 176 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
| 177 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
| 178 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | return PSA_ERROR_DATA_INVALID; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 180 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 181 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 183 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | return PSA_ERROR_NOT_SUPPORTED; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 185 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | return PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 187 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 188 | return PSA_ERROR_GENERIC_ERROR; |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | #endif |
| 192 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 193 | #if defined(MBEDTLS_PSA_BUILTIN_PAKE) |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 194 | 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] | 195 | const psa_crypto_driver_pake_inputs_t *inputs) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 196 | { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 197 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 198 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 199 | uint8_t *password = inputs->password; |
| 200 | size_t password_len = inputs->password_len; |
| 201 | psa_pake_role_t role = inputs->role; |
| 202 | psa_pake_cipher_suite_t cipher_suite = inputs->cipher_suite; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 203 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 204 | memset(operation, 0, sizeof(mbedtls_psa_pake_operation_t)); |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 205 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 206 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 207 | if (cipher_suite.algorithm == PSA_ALG_JPAKE) { |
| 208 | if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC || |
| 209 | cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 || |
| 210 | cipher_suite.bits != 256 || |
| 211 | cipher_suite.hash != PSA_ALG_SHA_256) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 212 | status = PSA_ERROR_NOT_SUPPORTED; |
| 213 | goto error; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 216 | if (role != PSA_PAKE_ROLE_CLIENT && |
| 217 | role != PSA_PAKE_ROLE_SERVER) { |
| 218 | status = PSA_ERROR_NOT_SUPPORTED; |
| 219 | goto error; |
| 220 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 221 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 222 | mbedtls_ecjpake_init(&operation->ctx.pake); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 223 | |
| 224 | operation->state = PSA_PAKE_STATE_SETUP; |
| 225 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
| 226 | operation->input_step = PSA_PAKE_STEP_X1_X2; |
| 227 | operation->output_step = PSA_PAKE_STEP_X1_X2; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 228 | operation->password_len = password_len; |
| 229 | operation->password = password; |
| 230 | operation->role = role; |
| 231 | operation->alg = cipher_suite.algorithm; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 234 | operation->buffer_length = 0; |
| 235 | operation->buffer_offset = 0; |
| 236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | return PSA_SUCCESS; |
| 238 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 239 | #else |
| 240 | (void) operation; |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 241 | (void) inputs; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 242 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 243 | { status = PSA_ERROR_NOT_SUPPORTED; } |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 244 | |
| 245 | error: |
Przemek Stekiel | 51eac53 | 2022-12-07 11:04:51 +0100 | [diff] [blame] | 246 | mbedtls_free(password); |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 247 | mbedtls_psa_pake_abort(operation); |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 248 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 251 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 252 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 253 | 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] | 254 | { |
| 255 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 256 | mbedtls_ecjpake_role role; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 257 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | if (operation->role == PSA_PAKE_ROLE_CLIENT) { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 259 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | } else if (operation->role == PSA_PAKE_ROLE_SERVER) { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 261 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | } else { |
| 263 | return PSA_ERROR_BAD_STATE; |
| 264 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | if (operation->password_len == 0) { |
| 267 | return PSA_ERROR_BAD_STATE; |
| 268 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 269 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 270 | ret = mbedtls_ecjpake_setup(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | role, |
| 272 | MBEDTLS_MD_SHA256, |
| 273 | MBEDTLS_ECP_DP_SECP256R1, |
| 274 | operation->password, |
| 275 | operation->password_len); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | mbedtls_platform_zeroize(operation->password, operation->password_len); |
| 278 | mbedtls_free(operation->password); |
Przemek Stekiel | ad0f357 | 2022-11-21 15:04:37 +0100 | [diff] [blame] | 279 | operation->password = NULL; |
| 280 | operation->password_len = 0; |
| 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | if (ret != 0) { |
| 283 | return mbedtls_ecjpake_to_psa_error(ret); |
| 284 | } |
Przemek Stekiel | 0bdec19 | 2022-11-22 09:10:35 +0100 | [diff] [blame] | 285 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 286 | operation->state = PSA_PAKE_STATE_READY; |
| 287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | return PSA_SUCCESS; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 289 | } |
| 290 | #endif |
| 291 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 292 | static psa_status_t mbedtls_psa_pake_output_internal( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 293 | mbedtls_psa_pake_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | psa_pake_step_t step, |
| 295 | uint8_t *output, |
| 296 | size_t output_size, |
| 297 | size_t *output_length) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 298 | { |
| 299 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 300 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 301 | size_t length; |
| 302 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 303 | if (operation->alg == PSA_ALG_NONE) { |
| 304 | return PSA_ERROR_BAD_STATE; |
| 305 | } |
| 306 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 307 | if (operation->state == PSA_PAKE_STATE_INVALID) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | return PSA_ERROR_BAD_STATE; |
| 309 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 310 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 311 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 312 | /* |
| 313 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 314 | * handling of output sequencing. |
| 315 | * |
Neil Armstrong | 6a12a77 | 2022-09-14 12:17:42 +0200 | [diff] [blame] | 316 | * 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] | 317 | * at once, on the other side the PSA CRYPTO PAKE api requires |
| 318 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be |
| 319 | * retrieved in sequence. |
| 320 | * |
| 321 | * In order to achieve API compatibility, the whole X1+X2 or X2S steps |
| 322 | * data is stored in an intermediate buffer at first step output call, |
| 323 | * and data is sliced down by parsing the ECPoint records in order |
| 324 | * to return the right parts on each step. |
| 325 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | if (operation->alg == PSA_ALG_JPAKE) { |
| 327 | if (step != PSA_PAKE_STEP_KEY_SHARE && |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 328 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 329 | step != PSA_PAKE_STEP_ZK_PROOF) { |
| 330 | return PSA_ERROR_INVALID_ARGUMENT; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 331 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | if (operation->state == PSA_PAKE_STATE_SETUP) { |
| 334 | status = psa_pake_ecjpake_setup(operation); |
| 335 | if (status != PSA_SUCCESS) { |
| 336 | return status; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if (operation->state != PSA_PAKE_STATE_READY && |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 341 | operation->state != PSA_PAKE_OUTPUT_X1_X2 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 342 | operation->state != PSA_PAKE_OUTPUT_X2S) { |
| 343 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 346 | if (operation->state == PSA_PAKE_STATE_READY) { |
| 347 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 348 | return PSA_ERROR_BAD_STATE; |
| 349 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | switch (operation->output_step) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 352 | case PSA_PAKE_STEP_X1_X2: |
| 353 | operation->state = PSA_PAKE_OUTPUT_X1_X2; |
| 354 | break; |
| 355 | case PSA_PAKE_STEP_X2S: |
| 356 | operation->state = PSA_PAKE_OUTPUT_X2S; |
| 357 | break; |
| 358 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 363 | } |
| 364 | |
| 365 | /* Check if step matches current sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | switch (operation->sequence) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 367 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 368 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 370 | return PSA_ERROR_BAD_STATE; |
| 371 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 372 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 373 | |
| 374 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 375 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | if (step != PSA_PAKE_STEP_ZK_PUBLIC) { |
| 377 | return PSA_ERROR_BAD_STATE; |
| 378 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 379 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 380 | |
| 381 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 382 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | if (step != PSA_PAKE_STEP_ZK_PROOF) { |
| 384 | return PSA_ERROR_BAD_STATE; |
| 385 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 386 | break; |
| 387 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 388 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 392 | /* Initialize & write round on KEY_SHARE sequences */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | if (operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 394 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 395 | ret = mbedtls_ecjpake_write_round_one(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | operation->buffer, |
| 397 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 398 | &operation->buffer_length, |
| 399 | mbedtls_psa_get_random, |
| 400 | MBEDTLS_PSA_RANDOM_STATE); |
| 401 | if (ret != 0) { |
| 402 | return mbedtls_ecjpake_to_psa_error(ret); |
| 403 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 404 | |
| 405 | operation->buffer_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | } else if (operation->state == PSA_PAKE_OUTPUT_X2S && |
| 407 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 408 | ret = mbedtls_ecjpake_write_round_two(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | operation->buffer, |
| 410 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 411 | &operation->buffer_length, |
| 412 | mbedtls_psa_get_random, |
| 413 | MBEDTLS_PSA_RANDOM_STATE); |
| 414 | if (ret != 0) { |
| 415 | return mbedtls_ecjpake_to_psa_error(ret); |
| 416 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 417 | |
| 418 | operation->buffer_offset = 0; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 419 | } |
| 420 | |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 421 | /* |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 422 | * mbedtls_ecjpake_write_round_xxx() outputs thing in the format |
| 423 | * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is |
| 424 | * that the data for each step is prepended with a length byte, and |
| 425 | * then they're concatenated. Additionally, the server's second round |
| 426 | * output is prepended with a 3-bytes ECParameters structure. |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 427 | * |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 428 | * In PSA, we output each step separately, and don't prepend the |
| 429 | * output with a length byte, even less a curve identifier, as that |
| 430 | * information is already available. |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 431 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | if (operation->state == PSA_PAKE_OUTPUT_X2S && |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 433 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | operation->role == PSA_PAKE_ROLE_SERVER) { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 435 | /* Skip ECParameters, with is 3 bytes (RFC 8422) */ |
| 436 | operation->buffer_offset += 3; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 437 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 438 | |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 439 | /* Read the length byte then move past it to the data */ |
| 440 | length = operation->buffer[operation->buffer_offset]; |
| 441 | operation->buffer_offset += 1; |
| 442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | if (operation->buffer_offset + length > operation->buffer_length) { |
| 444 | return PSA_ERROR_DATA_CORRUPT; |
| 445 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | if (output_size < length) { |
| 448 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 449 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | memcpy(output, |
| 452 | operation->buffer + operation->buffer_offset, |
| 453 | length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 454 | *output_length = length; |
| 455 | |
| 456 | operation->buffer_offset += length; |
| 457 | |
| 458 | /* Reset buffer after ZK_PROOF sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | if ((operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 460 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) || |
| 461 | (operation->state == PSA_PAKE_OUTPUT_X2S && |
| 462 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) { |
| 463 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 464 | operation->buffer_length = 0; |
| 465 | operation->buffer_offset = 0; |
| 466 | |
| 467 | operation->state = PSA_PAKE_STATE_READY; |
| 468 | operation->output_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 469 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | } else { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 471 | operation->sequence++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | return PSA_SUCCESS; |
| 475 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 476 | #else |
| 477 | (void) step; |
| 478 | (void) output; |
| 479 | (void) output_size; |
| 480 | (void) output_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 481 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 482 | { return PSA_ERROR_NOT_SUPPORTED; } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 485 | 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] | 486 | psa_pake_step_t step, |
| 487 | uint8_t *output, |
| 488 | size_t output_size, |
| 489 | size_t *output_length) |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 490 | { |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 491 | psa_status_t status = mbedtls_psa_pake_output_internal( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | operation, step, output, output_size, output_length); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 495 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | } |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | return status; |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 501 | static psa_status_t mbedtls_psa_pake_input_internal( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 502 | mbedtls_psa_pake_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | psa_pake_step_t step, |
| 504 | const uint8_t *input, |
| 505 | size_t input_length) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 506 | { |
| 507 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 508 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 509 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 510 | if (operation->alg == PSA_ALG_NONE) { |
| 511 | return PSA_ERROR_BAD_STATE; |
| 512 | } |
| 513 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 514 | if (operation->state == PSA_PAKE_STATE_INVALID) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 515 | return PSA_ERROR_BAD_STATE; |
| 516 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 517 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 518 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 519 | /* |
| 520 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 521 | * handling of input sequencing. |
| 522 | * |
| 523 | * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data |
| 524 | * at once as input, on the other side the PSA CRYPTO PAKE api requires |
| 525 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be |
| 526 | * given in sequence. |
| 527 | * |
| 528 | * In order to achieve API compatibility, each X1+X2 or X4S step data |
| 529 | * is stored sequentially in an intermediate buffer and given to the |
| 530 | * MbedTLS JPAKE API on the last step. |
| 531 | * |
| 532 | * This causes any input error to be only detected on the last step. |
| 533 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | if (operation->alg == PSA_ALG_JPAKE) { |
| 535 | if (step != PSA_PAKE_STEP_KEY_SHARE && |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 536 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | step != PSA_PAKE_STEP_ZK_PROOF) { |
| 538 | return PSA_ERROR_INVALID_ARGUMENT; |
| 539 | } |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 540 | |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 541 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256); |
| 543 | if (input_length > (size_t) PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, prim, step)) { |
| 544 | return PSA_ERROR_INVALID_ARGUMENT; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 545 | } |
| 546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | if (operation->state == PSA_PAKE_STATE_SETUP) { |
| 548 | status = psa_pake_ecjpake_setup(operation); |
| 549 | if (status != PSA_SUCCESS) { |
| 550 | return status; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (operation->state != PSA_PAKE_STATE_READY && |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 555 | operation->state != PSA_PAKE_INPUT_X1_X2 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | operation->state != PSA_PAKE_INPUT_X4S) { |
| 557 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | if (operation->state == PSA_PAKE_STATE_READY) { |
| 561 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 562 | return PSA_ERROR_BAD_STATE; |
| 563 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | switch (operation->input_step) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 566 | case PSA_PAKE_STEP_X1_X2: |
| 567 | operation->state = PSA_PAKE_INPUT_X1_X2; |
| 568 | break; |
| 569 | case PSA_PAKE_STEP_X2S: |
| 570 | operation->state = PSA_PAKE_INPUT_X4S; |
| 571 | break; |
| 572 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 577 | } |
| 578 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 579 | /* Check if step matches current sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | switch (operation->sequence) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 581 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 582 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 583 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 584 | return PSA_ERROR_BAD_STATE; |
| 585 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 586 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 587 | |
| 588 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 589 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 590 | if (step != PSA_PAKE_STEP_ZK_PUBLIC) { |
| 591 | return PSA_ERROR_BAD_STATE; |
| 592 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 593 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 594 | |
| 595 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 596 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 597 | if (step != PSA_PAKE_STEP_ZK_PROOF) { |
| 598 | return PSA_ERROR_BAD_STATE; |
| 599 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 600 | break; |
| 601 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 602 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 603 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 604 | } |
| 605 | |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 606 | /* |
| 607 | * Copy input to local buffer and format it as the Mbed TLS API |
| 608 | * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7. |
| 609 | * The summary is that the data for each step is prepended with a |
| 610 | * length byte, and then they're concatenated. Additionally, the |
| 611 | * server's second round output is prepended with a 3-bytes |
| 612 | * ECParameters structure - which means we have to prepend that when |
| 613 | * we're a client. |
| 614 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 615 | if (operation->state == PSA_PAKE_INPUT_X4S && |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 616 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 617 | operation->role == PSA_PAKE_ROLE_CLIENT) { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 618 | /* We only support secp256r1. */ |
| 619 | /* This is the ECParameters structure defined by RFC 8422. */ |
| 620 | unsigned char ecparameters[3] = { |
| 621 | 3, /* named_curve */ |
| 622 | 0, 23 /* secp256r1 */ |
| 623 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | memcpy(operation->buffer + operation->buffer_length, |
| 625 | ecparameters, sizeof(ecparameters)); |
| 626 | operation->buffer_length += sizeof(ecparameters); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* Write the length byte */ |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 630 | operation->buffer[operation->buffer_length] = (uint8_t) input_length; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 631 | operation->buffer_length += 1; |
| 632 | |
| 633 | /* Finally copy the data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 634 | memcpy(operation->buffer + operation->buffer_length, |
| 635 | input, input_length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 636 | operation->buffer_length += input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 637 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 638 | /* Load buffer at each last round ZK_PROOF */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | if (operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 640 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 641 | ret = mbedtls_ecjpake_read_round_one(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 642 | operation->buffer, |
| 643 | operation->buffer_length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 646 | operation->buffer_length = 0; |
| 647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | if (ret != 0) { |
| 649 | return mbedtls_ecjpake_to_psa_error(ret); |
| 650 | } |
| 651 | } else if (operation->state == PSA_PAKE_INPUT_X4S && |
| 652 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 653 | ret = mbedtls_ecjpake_read_round_two(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 654 | operation->buffer, |
| 655 | operation->buffer_length); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 657 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 658 | operation->buffer_length = 0; |
| 659 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | if (ret != 0) { |
| 661 | return mbedtls_ecjpake_to_psa_error(ret); |
| 662 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 663 | } |
| 664 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | if ((operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 666 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) || |
| 667 | (operation->state == PSA_PAKE_INPUT_X4S && |
| 668 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 669 | operation->state = PSA_PAKE_STATE_READY; |
| 670 | operation->input_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 671 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | } else { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 673 | operation->sequence++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | return PSA_SUCCESS; |
| 677 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 678 | #else |
| 679 | (void) step; |
| 680 | (void) input; |
| 681 | (void) input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 682 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 683 | { return PSA_ERROR_NOT_SUPPORTED; } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 684 | } |
| 685 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 686 | 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] | 687 | psa_pake_step_t step, |
| 688 | const uint8_t *input, |
| 689 | size_t input_length) |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 690 | { |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 691 | psa_status_t status = mbedtls_psa_pake_input_internal( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 692 | operation, step, input, input_length); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 694 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 695 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | } |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | return status; |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 699 | } |
| 700 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 701 | psa_status_t mbedtls_psa_pake_get_implicit_key( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 702 | mbedtls_psa_pake_operation_t *operation, |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 703 | uint8_t *output, size_t *output_size) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 704 | { |
| 705 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 706 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 707 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 708 | if (operation->alg == PSA_ALG_NONE) { |
| 709 | return PSA_ERROR_BAD_STATE; |
| 710 | } |
| 711 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 712 | if (operation->input_step != PSA_PAKE_STEP_DERIVE || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | operation->output_step != PSA_PAKE_STEP_DERIVE) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 714 | status = PSA_ERROR_BAD_STATE; |
| 715 | goto error; |
| 716 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 717 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 718 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 720 | ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 721 | operation->buffer, |
| 722 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 723 | &operation->buffer_length, |
| 724 | mbedtls_psa_get_random, |
| 725 | MBEDTLS_PSA_RANDOM_STATE); |
| 726 | if (ret != 0) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 727 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 728 | return mbedtls_ecjpake_to_psa_error(ret); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 729 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 730 | |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 731 | memcpy(output, operation->buffer, operation->buffer_length); |
| 732 | *output_size = operation->buffer_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 735 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 736 | mbedtls_psa_pake_abort(operation); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 737 | |
Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 738 | return PSA_SUCCESS; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 739 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 740 | #else |
| 741 | (void) output; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 742 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 743 | { status = PSA_ERROR_NOT_SUPPORTED; } |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 744 | |
| 745 | error: |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 746 | mbedtls_psa_pake_abort(operation); |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 749 | } |
| 750 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 751 | 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] | 752 | { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 753 | if (operation->alg == PSA_ALG_NONE) { |
| 754 | return PSA_SUCCESS; |
| 755 | } |
| 756 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 757 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | if (operation->alg == PSA_ALG_JPAKE) { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 760 | operation->input_step = PSA_PAKE_STEP_INVALID; |
| 761 | operation->output_step = PSA_PAKE_STEP_INVALID; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | if (operation->password_len > 0) { |
| 763 | mbedtls_platform_zeroize(operation->password, operation->password_len); |
| 764 | } |
| 765 | mbedtls_free(operation->password); |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 766 | operation->password = NULL; |
| 767 | operation->password_len = 0; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 768 | operation->role = PSA_PAKE_ROLE_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 770 | operation->buffer_length = 0; |
| 771 | operation->buffer_offset = 0; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 772 | mbedtls_ecjpake_free(&operation->ctx.pake); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 773 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 774 | #endif |
| 775 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 776 | operation->alg = PSA_ALG_NONE; |
| 777 | operation->state = PSA_PAKE_STATE_INVALID; |
| 778 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | fbc4b4a | 2022-06-10 08:54:53 +0200 | [diff] [blame] | 779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | return PSA_SUCCESS; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ |
| 784 | |
| 785 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |