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, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 195 | const psa_pake_cipher_suite_t *cipher_suite) |
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 | |
| 199 | /* A context must be freshly initialized before it can be set up. */ |
| 200 | if (operation->alg != PSA_ALG_NONE) { |
| 201 | status = PSA_ERROR_BAD_STATE; |
| 202 | goto error; |
| 203 | } |
| 204 | |
| 205 | if (cipher_suite == NULL || |
| 206 | PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 || |
| 207 | (cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC && |
| 208 | cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH) || |
| 209 | PSA_ALG_IS_HASH(cipher_suite->hash) == 0) { |
| 210 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 211 | goto error; |
| 212 | } |
| 213 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 214 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | if (cipher_suite->algorithm == PSA_ALG_JPAKE) { |
| 216 | if (cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC || |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 217 | cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 || |
| 218 | cipher_suite->bits != 256 || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 219 | cipher_suite->hash != PSA_ALG_SHA_256) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 220 | status = PSA_ERROR_NOT_SUPPORTED; |
| 221 | goto error; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | operation->alg = cipher_suite->algorithm; |
| 225 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 226 | mbedtls_ecjpake_init(&operation->ctx.pake); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 227 | |
| 228 | operation->state = PSA_PAKE_STATE_SETUP; |
| 229 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
| 230 | operation->input_step = PSA_PAKE_STEP_X1_X2; |
| 231 | operation->output_step = PSA_PAKE_STEP_X1_X2; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 232 | operation->password_len = 0; |
| 233 | operation->password = NULL; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 236 | operation->buffer_length = 0; |
| 237 | operation->buffer_offset = 0; |
| 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | return PSA_SUCCESS; |
| 240 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 241 | #else |
| 242 | (void) operation; |
| 243 | (void) cipher_suite; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 244 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 245 | { status = PSA_ERROR_NOT_SUPPORTED; } |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 246 | |
| 247 | error: |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 248 | mbedtls_psa_pake_abort(operation); |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 249 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 252 | psa_status_t mbedtls_psa_pake_set_password_key(const psa_key_attributes_t *attributes, |
| 253 | mbedtls_psa_pake_operation_t *operation, |
| 254 | uint8_t *password, |
| 255 | size_t password_len) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 256 | { |
| 257 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 258 | psa_key_type_t type = psa_get_key_type(attributes); |
| 259 | psa_key_usage_t usage = psa_get_key_usage_flags(attributes); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | if (type != PSA_KEY_TYPE_PASSWORD && |
| 262 | type != PSA_KEY_TYPE_PASSWORD_HASH) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 263 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 264 | goto error; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | if ((usage & PSA_KEY_USAGE_DERIVE) == 0) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 268 | status = PSA_ERROR_NOT_PERMITTED; |
| 269 | goto error; |
| 270 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 271 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 272 | if (operation->alg == PSA_ALG_NONE) { |
| 273 | status = PSA_ERROR_BAD_STATE; |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | if (operation->state != PSA_PAKE_STATE_SETUP) { |
| 278 | status = PSA_ERROR_BAD_STATE; |
| 279 | goto error; |
| 280 | } |
| 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | if (operation->password != NULL) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 283 | status = PSA_ERROR_BAD_STATE; |
| 284 | goto error; |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 285 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 286 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 287 | operation->password = mbedtls_calloc(1, password_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | if (operation->password == NULL) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | return PSA_ERROR_INSUFFICIENT_MEMORY; |
| 290 | } |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 291 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 292 | memcpy(operation->password, password, password_len); |
| 293 | operation->password_len = password_len; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 294 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | return PSA_SUCCESS; |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 296 | |
| 297 | error: |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 298 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 302 | psa_status_t mbedtls_psa_pake_set_user(mbedtls_psa_pake_operation_t *operation, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 303 | const uint8_t *user_id, |
| 304 | size_t user_id_len) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 305 | { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 306 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 307 | (void) user_id; |
| 308 | (void) user_id_len; |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 309 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 310 | if (operation->alg == PSA_ALG_NONE) { |
| 311 | return PSA_ERROR_BAD_STATE; |
| 312 | } |
| 313 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 314 | if (operation->state != PSA_PAKE_STATE_SETUP) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 315 | status = PSA_ERROR_BAD_STATE; |
| 316 | goto error; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 317 | } |
| 318 | |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 319 | status = PSA_ERROR_NOT_SUPPORTED; |
| 320 | |
| 321 | error: |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 322 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 326 | psa_status_t mbedtls_psa_pake_set_peer(mbedtls_psa_pake_operation_t *operation, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 327 | const uint8_t *peer_id, |
| 328 | size_t peer_id_len) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 329 | { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 330 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 331 | (void) peer_id; |
| 332 | (void) peer_id_len; |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 333 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 334 | if (operation->alg == PSA_ALG_NONE) { |
| 335 | status = PSA_ERROR_BAD_STATE; |
| 336 | goto error; |
| 337 | } |
| 338 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 339 | if (operation->state != PSA_PAKE_STATE_SETUP) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 340 | status = PSA_ERROR_BAD_STATE; |
| 341 | goto error; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 344 | status = PSA_ERROR_NOT_SUPPORTED; |
| 345 | |
| 346 | error: |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 347 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 348 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 351 | psa_status_t mbedtls_psa_pake_set_role(mbedtls_psa_pake_operation_t *operation, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 352 | psa_pake_role_t role) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 353 | { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 354 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 355 | if (operation->alg == PSA_ALG_NONE) { |
| 356 | status = PSA_ERROR_BAD_STATE; |
| 357 | goto error; |
| 358 | } |
| 359 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 360 | if (operation->state != PSA_PAKE_STATE_SETUP) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 361 | status = PSA_ERROR_BAD_STATE; |
| 362 | goto error; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 365 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | if (operation->alg == PSA_ALG_JPAKE) { |
| 367 | if (role != PSA_PAKE_ROLE_CLIENT && |
| 368 | role != PSA_PAKE_ROLE_SERVER) { |
| 369 | return PSA_ERROR_NOT_SUPPORTED; |
| 370 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 371 | |
| 372 | operation->role = role; |
| 373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | return PSA_SUCCESS; |
| 375 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 376 | #else |
| 377 | (void) role; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 378 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 379 | |
| 380 | { status = PSA_ERROR_NOT_SUPPORTED; } |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 381 | |
| 382 | error: |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 383 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 387 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 388 | 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] | 389 | { |
| 390 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 391 | mbedtls_ecjpake_role role; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | if (operation->role == PSA_PAKE_ROLE_CLIENT) { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 394 | role = MBEDTLS_ECJPAKE_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | } else if (operation->role == PSA_PAKE_ROLE_SERVER) { |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 396 | role = MBEDTLS_ECJPAKE_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | } else { |
| 398 | return PSA_ERROR_BAD_STATE; |
| 399 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | if (operation->password_len == 0) { |
| 402 | return PSA_ERROR_BAD_STATE; |
| 403 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 404 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 405 | ret = mbedtls_ecjpake_setup(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | role, |
| 407 | MBEDTLS_MD_SHA256, |
| 408 | MBEDTLS_ECP_DP_SECP256R1, |
| 409 | operation->password, |
| 410 | operation->password_len); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | mbedtls_platform_zeroize(operation->password, operation->password_len); |
| 413 | mbedtls_free(operation->password); |
Przemek Stekiel | ad0f357 | 2022-11-21 15:04:37 +0100 | [diff] [blame] | 414 | operation->password = NULL; |
| 415 | operation->password_len = 0; |
| 416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | if (ret != 0) { |
| 418 | return mbedtls_ecjpake_to_psa_error(ret); |
| 419 | } |
Przemek Stekiel | 0bdec19 | 2022-11-22 09:10:35 +0100 | [diff] [blame] | 420 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 421 | operation->state = PSA_PAKE_STATE_READY; |
| 422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | return PSA_SUCCESS; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 424 | } |
| 425 | #endif |
| 426 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 427 | static psa_status_t mbedtls_psa_pake_output_internal( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 428 | mbedtls_psa_pake_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | psa_pake_step_t step, |
| 430 | uint8_t *output, |
| 431 | size_t output_size, |
| 432 | size_t *output_length) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 433 | { |
| 434 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 435 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 436 | size_t length; |
| 437 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 438 | if (operation->alg == PSA_ALG_NONE) { |
| 439 | return PSA_ERROR_BAD_STATE; |
| 440 | } |
| 441 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 442 | if (operation->state == PSA_PAKE_STATE_INVALID) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | return PSA_ERROR_BAD_STATE; |
| 444 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 445 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 446 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 447 | /* |
| 448 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 449 | * handling of output sequencing. |
| 450 | * |
Neil Armstrong | 6a12a77 | 2022-09-14 12:17:42 +0200 | [diff] [blame] | 451 | * 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] | 452 | * at once, on the other side the PSA CRYPTO PAKE api requires |
| 453 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be |
| 454 | * retrieved in sequence. |
| 455 | * |
| 456 | * In order to achieve API compatibility, the whole X1+X2 or X2S steps |
| 457 | * data is stored in an intermediate buffer at first step output call, |
| 458 | * and data is sliced down by parsing the ECPoint records in order |
| 459 | * to return the right parts on each step. |
| 460 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 461 | if (operation->alg == PSA_ALG_JPAKE) { |
| 462 | if (step != PSA_PAKE_STEP_KEY_SHARE && |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 463 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | step != PSA_PAKE_STEP_ZK_PROOF) { |
| 465 | return PSA_ERROR_INVALID_ARGUMENT; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 466 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | if (operation->state == PSA_PAKE_STATE_SETUP) { |
| 469 | status = psa_pake_ecjpake_setup(operation); |
| 470 | if (status != PSA_SUCCESS) { |
| 471 | return status; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (operation->state != PSA_PAKE_STATE_READY && |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 476 | operation->state != PSA_PAKE_OUTPUT_X1_X2 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | operation->state != PSA_PAKE_OUTPUT_X2S) { |
| 478 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | if (operation->state == PSA_PAKE_STATE_READY) { |
| 482 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 483 | return PSA_ERROR_BAD_STATE; |
| 484 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | switch (operation->output_step) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 487 | case PSA_PAKE_STEP_X1_X2: |
| 488 | operation->state = PSA_PAKE_OUTPUT_X1_X2; |
| 489 | break; |
| 490 | case PSA_PAKE_STEP_X2S: |
| 491 | operation->state = PSA_PAKE_OUTPUT_X2S; |
| 492 | break; |
| 493 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 498 | } |
| 499 | |
| 500 | /* Check if step matches current sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | switch (operation->sequence) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 502 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 503 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 505 | return PSA_ERROR_BAD_STATE; |
| 506 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 507 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 508 | |
| 509 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 510 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | if (step != PSA_PAKE_STEP_ZK_PUBLIC) { |
| 512 | return PSA_ERROR_BAD_STATE; |
| 513 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 514 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 515 | |
| 516 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 517 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | if (step != PSA_PAKE_STEP_ZK_PROOF) { |
| 519 | return PSA_ERROR_BAD_STATE; |
| 520 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 521 | break; |
| 522 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 523 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 524 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 527 | /* Initialize & write round on KEY_SHARE sequences */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 528 | if (operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 529 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 530 | ret = mbedtls_ecjpake_write_round_one(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 531 | operation->buffer, |
| 532 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 533 | &operation->buffer_length, |
| 534 | mbedtls_psa_get_random, |
| 535 | MBEDTLS_PSA_RANDOM_STATE); |
| 536 | if (ret != 0) { |
| 537 | return mbedtls_ecjpake_to_psa_error(ret); |
| 538 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 539 | |
| 540 | operation->buffer_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | } else if (operation->state == PSA_PAKE_OUTPUT_X2S && |
| 542 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 543 | ret = mbedtls_ecjpake_write_round_two(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | operation->buffer, |
| 545 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 546 | &operation->buffer_length, |
| 547 | mbedtls_psa_get_random, |
| 548 | MBEDTLS_PSA_RANDOM_STATE); |
| 549 | if (ret != 0) { |
| 550 | return mbedtls_ecjpake_to_psa_error(ret); |
| 551 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 552 | |
| 553 | operation->buffer_offset = 0; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 556 | /* |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 557 | * mbedtls_ecjpake_write_round_xxx() outputs thing in the format |
| 558 | * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is |
| 559 | * that the data for each step is prepended with a length byte, and |
| 560 | * then they're concatenated. Additionally, the server's second round |
| 561 | * output is prepended with a 3-bytes ECParameters structure. |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 562 | * |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 563 | * In PSA, we output each step separately, and don't prepend the |
| 564 | * output with a length byte, even less a curve identifier, as that |
| 565 | * information is already available. |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 566 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | if (operation->state == PSA_PAKE_OUTPUT_X2S && |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 568 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 569 | operation->role == PSA_PAKE_ROLE_SERVER) { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 570 | /* Skip ECParameters, with is 3 bytes (RFC 8422) */ |
| 571 | operation->buffer_offset += 3; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 572 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 573 | |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 574 | /* Read the length byte then move past it to the data */ |
| 575 | length = operation->buffer[operation->buffer_offset]; |
| 576 | operation->buffer_offset += 1; |
| 577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | if (operation->buffer_offset + length > operation->buffer_length) { |
| 579 | return PSA_ERROR_DATA_CORRUPT; |
| 580 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | if (output_size < length) { |
| 583 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 584 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | memcpy(output, |
| 587 | operation->buffer + operation->buffer_offset, |
| 588 | length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 589 | *output_length = length; |
| 590 | |
| 591 | operation->buffer_offset += length; |
| 592 | |
| 593 | /* Reset buffer after ZK_PROOF sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | if ((operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 595 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) || |
| 596 | (operation->state == PSA_PAKE_OUTPUT_X2S && |
| 597 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) { |
| 598 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 599 | operation->buffer_length = 0; |
| 600 | operation->buffer_offset = 0; |
| 601 | |
| 602 | operation->state = PSA_PAKE_STATE_READY; |
| 603 | operation->output_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 604 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 605 | } else { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 606 | operation->sequence++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 608 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 609 | return PSA_SUCCESS; |
| 610 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 611 | #else |
| 612 | (void) step; |
| 613 | (void) output; |
| 614 | (void) output_size; |
| 615 | (void) output_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 616 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 617 | { return PSA_ERROR_NOT_SUPPORTED; } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 620 | 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] | 621 | psa_pake_step_t step, |
| 622 | uint8_t *output, |
| 623 | size_t output_size, |
| 624 | size_t *output_length) |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 625 | { |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 626 | psa_status_t status = mbedtls_psa_pake_output_internal( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | operation, step, output, output_size, output_length); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 630 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | } |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | return status; |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 634 | } |
| 635 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 636 | static psa_status_t mbedtls_psa_pake_input_internal( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 637 | mbedtls_psa_pake_operation_t *operation, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | psa_pake_step_t step, |
| 639 | const uint8_t *input, |
| 640 | size_t input_length) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 641 | { |
| 642 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 643 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 644 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 645 | if (operation->alg == PSA_ALG_NONE) { |
| 646 | return PSA_ERROR_BAD_STATE; |
| 647 | } |
| 648 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 649 | if (operation->state == PSA_PAKE_STATE_INVALID) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | return PSA_ERROR_BAD_STATE; |
| 651 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 652 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 653 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 654 | /* |
| 655 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 656 | * handling of input sequencing. |
| 657 | * |
| 658 | * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data |
| 659 | * at once as input, on the other side the PSA CRYPTO PAKE api requires |
| 660 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be |
| 661 | * given in sequence. |
| 662 | * |
| 663 | * In order to achieve API compatibility, each X1+X2 or X4S step data |
| 664 | * is stored sequentially in an intermediate buffer and given to the |
| 665 | * MbedTLS JPAKE API on the last step. |
| 666 | * |
| 667 | * This causes any input error to be only detected on the last step. |
| 668 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 669 | if (operation->alg == PSA_ALG_JPAKE) { |
| 670 | if (step != PSA_PAKE_STEP_KEY_SHARE && |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 671 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | step != PSA_PAKE_STEP_ZK_PROOF) { |
| 673 | return PSA_ERROR_INVALID_ARGUMENT; |
| 674 | } |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 675 | |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 676 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 677 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256); |
| 678 | if (input_length > (size_t) PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, prim, step)) { |
| 679 | return PSA_ERROR_INVALID_ARGUMENT; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 682 | if (operation->state == PSA_PAKE_STATE_SETUP) { |
| 683 | status = psa_pake_ecjpake_setup(operation); |
| 684 | if (status != PSA_SUCCESS) { |
| 685 | return status; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if (operation->state != PSA_PAKE_STATE_READY && |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 690 | operation->state != PSA_PAKE_INPUT_X1_X2 && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 691 | operation->state != PSA_PAKE_INPUT_X4S) { |
| 692 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 695 | if (operation->state == PSA_PAKE_STATE_READY) { |
| 696 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 697 | return PSA_ERROR_BAD_STATE; |
| 698 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 699 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | switch (operation->input_step) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 701 | case PSA_PAKE_STEP_X1_X2: |
| 702 | operation->state = PSA_PAKE_INPUT_X1_X2; |
| 703 | break; |
| 704 | case PSA_PAKE_STEP_X2S: |
| 705 | operation->state = PSA_PAKE_INPUT_X4S; |
| 706 | break; |
| 707 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 712 | } |
| 713 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 714 | /* Check if step matches current sequence */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 715 | switch (operation->sequence) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 716 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 717 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | if (step != PSA_PAKE_STEP_KEY_SHARE) { |
| 719 | return PSA_ERROR_BAD_STATE; |
| 720 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 721 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 722 | |
| 723 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 724 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | if (step != PSA_PAKE_STEP_ZK_PUBLIC) { |
| 726 | return PSA_ERROR_BAD_STATE; |
| 727 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 728 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 729 | |
| 730 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 731 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | if (step != PSA_PAKE_STEP_ZK_PROOF) { |
| 733 | return PSA_ERROR_BAD_STATE; |
| 734 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 735 | break; |
| 736 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 737 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | return PSA_ERROR_BAD_STATE; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 741 | /* |
| 742 | * Copy input to local buffer and format it as the Mbed TLS API |
| 743 | * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7. |
| 744 | * The summary is that the data for each step is prepended with a |
| 745 | * length byte, and then they're concatenated. Additionally, the |
| 746 | * server's second round output is prepended with a 3-bytes |
| 747 | * ECParameters structure - which means we have to prepend that when |
| 748 | * we're a client. |
| 749 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | if (operation->state == PSA_PAKE_INPUT_X4S && |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 751 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 752 | operation->role == PSA_PAKE_ROLE_CLIENT) { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 753 | /* We only support secp256r1. */ |
| 754 | /* This is the ECParameters structure defined by RFC 8422. */ |
| 755 | unsigned char ecparameters[3] = { |
| 756 | 3, /* named_curve */ |
| 757 | 0, 23 /* secp256r1 */ |
| 758 | }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 759 | memcpy(operation->buffer + operation->buffer_length, |
| 760 | ecparameters, sizeof(ecparameters)); |
| 761 | operation->buffer_length += sizeof(ecparameters); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /* Write the length byte */ |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 765 | operation->buffer[operation->buffer_length] = (uint8_t) input_length; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 766 | operation->buffer_length += 1; |
| 767 | |
| 768 | /* Finally copy the data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | memcpy(operation->buffer + operation->buffer_length, |
| 770 | input, input_length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 771 | operation->buffer_length += input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 772 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 773 | /* Load buffer at each last round ZK_PROOF */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | if (operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 775 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 776 | ret = mbedtls_ecjpake_read_round_one(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | operation->buffer, |
| 778 | operation->buffer_length); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 781 | operation->buffer_length = 0; |
| 782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | if (ret != 0) { |
| 784 | return mbedtls_ecjpake_to_psa_error(ret); |
| 785 | } |
| 786 | } else if (operation->state == PSA_PAKE_INPUT_X4S && |
| 787 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 788 | ret = mbedtls_ecjpake_read_round_two(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 789 | operation->buffer, |
| 790 | operation->buffer_length); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 793 | operation->buffer_length = 0; |
| 794 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | if (ret != 0) { |
| 796 | return mbedtls_ecjpake_to_psa_error(ret); |
| 797 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | if ((operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 801 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) || |
| 802 | (operation->state == PSA_PAKE_INPUT_X4S && |
| 803 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 804 | operation->state = PSA_PAKE_STATE_READY; |
| 805 | operation->input_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 806 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | } else { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 808 | operation->sequence++; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | return PSA_SUCCESS; |
| 812 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 813 | #else |
| 814 | (void) step; |
| 815 | (void) input; |
| 816 | (void) input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 817 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 818 | { return PSA_ERROR_NOT_SUPPORTED; } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 819 | } |
| 820 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 821 | 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] | 822 | psa_pake_step_t step, |
| 823 | const uint8_t *input, |
| 824 | size_t input_length) |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 825 | { |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 826 | psa_status_t status = mbedtls_psa_pake_input_internal( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | operation, step, input, input_length); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | if (status != PSA_SUCCESS) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 830 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 831 | } |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | return status; |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 836 | psa_status_t mbedtls_psa_pake_get_implicit_key( |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 837 | mbedtls_psa_pake_operation_t *operation, |
Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 838 | psa_key_derivation_operation_t *output) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 839 | { |
| 840 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 841 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 842 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 843 | if (operation->alg == PSA_ALG_NONE) { |
| 844 | return PSA_ERROR_BAD_STATE; |
| 845 | } |
| 846 | |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 847 | if (operation->input_step != PSA_PAKE_STEP_DERIVE || |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 848 | operation->output_step != PSA_PAKE_STEP_DERIVE) { |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 849 | status = PSA_ERROR_BAD_STATE; |
| 850 | goto error; |
| 851 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 852 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 853 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 854 | if (operation->alg == PSA_ALG_JPAKE) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 855 | ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.pake, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | operation->buffer, |
| 857 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
| 858 | &operation->buffer_length, |
| 859 | mbedtls_psa_get_random, |
| 860 | MBEDTLS_PSA_RANDOM_STATE); |
| 861 | if (ret != 0) { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 862 | mbedtls_psa_pake_abort(operation); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | return mbedtls_ecjpake_to_psa_error(ret); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 864 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | status = psa_key_derivation_input_bytes(output, |
| 867 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 868 | operation->buffer, |
| 869 | operation->buffer_length); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 872 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 873 | mbedtls_psa_pake_abort(operation); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 875 | return status; |
| 876 | } else |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 877 | #else |
| 878 | (void) output; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 879 | #endif |
Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 880 | { status = PSA_ERROR_NOT_SUPPORTED; } |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 881 | |
| 882 | error: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 883 | psa_key_derivation_abort(output); |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 884 | mbedtls_psa_pake_abort(operation); |
Valerio Setti | fdb77cd | 2022-11-11 12:02:24 +0100 | [diff] [blame] | 885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | return status; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 887 | } |
| 888 | |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 889 | 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] | 890 | { |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 891 | if (operation->alg == PSA_ALG_NONE) { |
| 892 | return PSA_SUCCESS; |
| 893 | } |
| 894 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 895 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | if (operation->alg == PSA_ALG_JPAKE) { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 898 | operation->input_step = PSA_PAKE_STEP_INVALID; |
| 899 | operation->output_step = PSA_PAKE_STEP_INVALID; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | if (operation->password_len > 0) { |
| 901 | mbedtls_platform_zeroize(operation->password, operation->password_len); |
| 902 | } |
| 903 | mbedtls_free(operation->password); |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 904 | operation->password = NULL; |
| 905 | operation->password_len = 0; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 906 | operation->role = PSA_PAKE_ROLE_NONE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 907 | mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 908 | operation->buffer_length = 0; |
| 909 | operation->buffer_offset = 0; |
Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame^] | 910 | mbedtls_ecjpake_free(&operation->ctx.pake); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 911 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 912 | #endif |
| 913 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 914 | operation->alg = PSA_ALG_NONE; |
| 915 | operation->state = PSA_PAKE_STATE_INVALID; |
| 916 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | fbc4b4a | 2022-06-10 08:54:53 +0200 | [diff] [blame] | 917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 918 | return PSA_SUCCESS; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ |
| 922 | |
| 923 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |