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