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