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