| 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 |  | 
| David Horstmann | 57727cd | 2023-06-20 19:40:57 +0100 | [diff] [blame] | 82 | /* | 
|  | 83 | * Possible sequence of calls to implementation: | 
|  | 84 | * | 
|  | 85 | * |--- In any order: | 
|  | 86 | * |   | | 
|  | 87 | * |   |------ In Order | 
|  | 88 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X1_STEP_KEY_SHARE) | 
|  | 89 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X1_STEP_ZK_PUBLIC) | 
|  | 90 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X1_STEP_ZK_PROOF) | 
|  | 91 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X2_STEP_KEY_SHARE) | 
|  | 92 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X2_STEP_ZK_PUBLIC) | 
|  | 93 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X2_STEP_ZK_PROOF) | 
|  | 94 | * |   | | 
|  | 95 | * |   |------ In Order: | 
|  | 96 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X1_STEP_KEY_SHARE) | 
|  | 97 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X1_STEP_ZK_PUBLIC) | 
|  | 98 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X1_STEP_ZK_PROOF) | 
|  | 99 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X2_STEP_KEY_SHARE) | 
|  | 100 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X2_STEP_ZK_PUBLIC) | 
|  | 101 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X2_STEP_ZK_PROOF) | 
|  | 102 | * | | 
|  | 103 | * |--- In any order: | 
|  | 104 | * |   | | 
|  | 105 | * |   |------ In Order | 
|  | 106 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X2S_STEP_KEY_SHARE) | 
|  | 107 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X2S_STEP_ZK_PUBLIC) | 
|  | 108 | * |   |       | mbedtls_psa_pake_output(PSA_JPAKE_X2S_STEP_ZK_PROOF) | 
|  | 109 | * |   | | 
|  | 110 | * |   |------ In Order: | 
|  | 111 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X4S_STEP_KEY_SHARE) | 
|  | 112 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X4S_STEP_ZK_PUBLIC) | 
|  | 113 | * |           | mbedtls_psa_pake_input(PSA_JPAKE_X4S_STEP_ZK_PROOF) | 
|  | 114 | */ | 
|  | 115 |  | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 116 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | static psa_status_t mbedtls_ecjpake_to_psa_error(int ret) | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 118 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | switch (ret) { | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 120 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: | 
|  | 121 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: | 
|  | 122 | case MBEDTLS_ERR_ECP_INVALID_KEY: | 
|  | 123 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | return PSA_ERROR_DATA_INVALID; | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 125 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: | 
|  | 126 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 128 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | return PSA_ERROR_NOT_SUPPORTED; | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 130 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | return PSA_ERROR_CORRUPTION_DETECTED; | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 132 | default: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | return PSA_ERROR_GENERIC_ERROR; | 
| Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 134 | } | 
|  | 135 | } | 
|  | 136 | #endif | 
|  | 137 |  | 
| Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 138 | #if defined(MBEDTLS_PSA_BUILTIN_PAKE) | 
| Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 139 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 140 | static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation) | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 141 | { | 
|  | 142 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 143 |  | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 144 | mbedtls_ecjpake_init(&operation->ctx.jpake); | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 145 |  | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 146 | ret = mbedtls_ecjpake_setup(&operation->ctx.jpake, | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 147 | operation->role, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | MBEDTLS_MD_SHA256, | 
|  | 149 | MBEDTLS_ECP_DP_SECP256R1, | 
|  | 150 | operation->password, | 
|  | 151 | operation->password_len); | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 152 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | mbedtls_platform_zeroize(operation->password, operation->password_len); | 
| Przemek Stekiel | ad0f357 | 2022-11-21 15:04:37 +0100 | [diff] [blame] | 154 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | if (ret != 0) { | 
|  | 156 | return mbedtls_ecjpake_to_psa_error(ret); | 
|  | 157 | } | 
| Przemek Stekiel | 0bdec19 | 2022-11-22 09:10:35 +0100 | [diff] [blame] | 158 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | return PSA_SUCCESS; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 160 | } | 
| Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 161 | #endif | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 162 |  | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 163 | /* The only two JPAKE user/peer identifiers supported in built-in implementation. */ | 
|  | 164 | static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' }; | 
|  | 165 | static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' }; | 
|  | 166 |  | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 167 | psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation, | 
|  | 168 | const psa_crypto_driver_pake_inputs_t *inputs) | 
|  | 169 | { | 
|  | 170 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 171 | size_t user_len = 0, peer_len = 0, password_len = 0; | 
|  | 172 | uint8_t *peer = NULL, *user = NULL; | 
|  | 173 | size_t actual_user_len = 0, actual_peer_len = 0, actual_password_len = 0; | 
| Przemek Stekiel | 5cbca79 | 2023-01-17 16:51:19 +0100 | [diff] [blame] | 174 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 175 |  | 
| Przemek Stekiel | 5cbca79 | 2023-01-17 16:51:19 +0100 | [diff] [blame] | 176 | status = psa_crypto_driver_pake_get_password_len(inputs, &password_len); | 
|  | 177 | if (status != PSA_SUCCESS) { | 
|  | 178 | return status; | 
|  | 179 | } | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 180 |  | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 181 | psa_crypto_driver_pake_get_user_len(inputs, &user_len); | 
|  | 182 | if (status != PSA_SUCCESS) { | 
|  | 183 | return status; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | psa_crypto_driver_pake_get_peer_len(inputs, &peer_len); | 
| Przemek Stekiel | 5cbca79 | 2023-01-17 16:51:19 +0100 | [diff] [blame] | 187 | if (status != PSA_SUCCESS) { | 
|  | 188 | return status; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | status = psa_crypto_driver_pake_get_cipher_suite(inputs, &cipher_suite); | 
|  | 192 | if (status != PSA_SUCCESS) { | 
|  | 193 | return status; | 
|  | 194 | } | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 195 |  | 
| Przemek Stekiel | a54dc69 | 2023-02-20 10:18:10 +0100 | [diff] [blame] | 196 | operation->password = mbedtls_calloc(1, password_len); | 
|  | 197 | if (operation->password == NULL) { | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 198 | status = PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 199 | goto error; | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | user = mbedtls_calloc(1, user_len); | 
|  | 203 | if (user == NULL) { | 
|  | 204 | status = PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 205 | goto error; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | peer = mbedtls_calloc(1, peer_len); | 
|  | 209 | if (peer == NULL) { | 
|  | 210 | status = PSA_ERROR_INSUFFICIENT_MEMORY; | 
|  | 211 | goto error; | 
| Przemek Stekiel | a54dc69 | 2023-02-20 10:18:10 +0100 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
|  | 214 | status = psa_crypto_driver_pake_get_password(inputs, operation->password, | 
| Przemek Stekiel | 083745e | 2023-02-23 17:28:23 +0100 | [diff] [blame] | 215 | password_len, &actual_password_len); | 
| Przemek Stekiel | a54dc69 | 2023-02-20 10:18:10 +0100 | [diff] [blame] | 216 | if (status != PSA_SUCCESS) { | 
|  | 217 | goto error; | 
|  | 218 | } | 
|  | 219 |  | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 220 | status = psa_crypto_driver_pake_get_user(inputs, user, | 
|  | 221 | user_len, &actual_user_len); | 
|  | 222 | if (status != PSA_SUCCESS) { | 
|  | 223 | goto error; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | status = psa_crypto_driver_pake_get_peer(inputs, peer, | 
|  | 227 | peer_len, &actual_peer_len); | 
|  | 228 | if (status != PSA_SUCCESS) { | 
|  | 229 | goto error; | 
|  | 230 | } | 
|  | 231 |  | 
| Przemek Stekiel | a54dc69 | 2023-02-20 10:18:10 +0100 | [diff] [blame] | 232 | operation->password_len = actual_password_len; | 
|  | 233 | operation->alg = cipher_suite.algorithm; | 
|  | 234 |  | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 235 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) | 
|  | 236 | if (cipher_suite.algorithm == PSA_ALG_JPAKE) { | 
|  | 237 | if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC || | 
|  | 238 | cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 || | 
|  | 239 | cipher_suite.bits != 256 || | 
|  | 240 | cipher_suite.hash != PSA_ALG_SHA_256) { | 
| Przemek Stekiel | a54dc69 | 2023-02-20 10:18:10 +0100 | [diff] [blame] | 241 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 242 | goto error; | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 245 | const size_t user_peer_len = sizeof(jpake_client_id); // client and server have the same length | 
|  | 246 | if (actual_user_len != user_peer_len || | 
|  | 247 | actual_peer_len != user_peer_len) { | 
|  | 248 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 249 | goto error; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | if (memcmp(user, jpake_client_id, actual_user_len) == 0 && | 
|  | 253 | memcmp(peer, jpake_server_id, actual_peer_len) == 0) { | 
|  | 254 | operation->role = MBEDTLS_ECJPAKE_CLIENT; | 
|  | 255 | } else | 
|  | 256 | if (memcmp(user, jpake_server_id, actual_user_len) == 0 && | 
|  | 257 | memcmp(peer, jpake_client_id, actual_peer_len) == 0) { | 
|  | 258 | operation->role = MBEDTLS_ECJPAKE_SERVER; | 
|  | 259 | } else { | 
|  | 260 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 261 | goto error; | 
|  | 262 | } | 
|  | 263 |  | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 264 | operation->buffer_length = 0; | 
|  | 265 | operation->buffer_offset = 0; | 
|  | 266 |  | 
|  | 267 | status = psa_pake_ecjpake_setup(operation); | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 268 | if (status != PSA_SUCCESS) { | 
| Przemek Stekiel | a54dc69 | 2023-02-20 10:18:10 +0100 | [diff] [blame] | 269 | goto error; | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
| Przemek Stekiel | aede2ad | 2023-04-25 14:30:34 +0200 | [diff] [blame] | 272 | /* Role has been set, release user/peer buffers. */ | 
|  | 273 | mbedtls_free(user); mbedtls_free(peer); | 
|  | 274 |  | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 275 | return PSA_SUCCESS; | 
|  | 276 | } else | 
|  | 277 | #else | 
|  | 278 | (void) operation; | 
|  | 279 | (void) inputs; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 280 | #endif | 
| Przemek Stekiel | ce131bf | 2023-02-21 12:19:27 +0100 | [diff] [blame] | 281 | { status = PSA_ERROR_NOT_SUPPORTED; } | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 282 |  | 
| Przemek Stekiel | a54dc69 | 2023-02-20 10:18:10 +0100 | [diff] [blame] | 283 | error: | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 284 | mbedtls_free(user); mbedtls_free(peer); | 
| Przemek Stekiel | d93de32 | 2023-02-24 08:39:04 +0100 | [diff] [blame] | 285 | /* In case of failure of the setup of a multipart operation, the PSA driver interface | 
|  | 286 | * specifies that the core does not call any other driver entry point thus does not | 
|  | 287 | * call mbedtls_psa_pake_abort(). Therefore call it here to do the needed clean | 
|  | 288 | * up like freeing the memory that may have been allocated to store the password. | 
|  | 289 | */ | 
| Przemek Stekiel | ce131bf | 2023-02-21 12:19:27 +0100 | [diff] [blame] | 290 | mbedtls_psa_pake_abort(operation); | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 291 | return status; | 
|  | 292 | } | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 293 |  | 
| Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 294 | static psa_status_t mbedtls_psa_pake_output_internal( | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 295 | mbedtls_psa_pake_operation_t *operation, | 
| Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 296 | psa_crypto_driver_pake_step_t step, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | uint8_t *output, | 
|  | 298 | size_t output_size, | 
|  | 299 | size_t *output_length) | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 300 | { | 
|  | 301 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 302 | size_t length; | 
| Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 303 | (void) step; // Unused parameter | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 304 |  | 
| Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 305 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) | 
| Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 306 | /* | 
|  | 307 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different | 
|  | 308 | * handling of output sequencing. | 
|  | 309 | * | 
| Neil Armstrong | 6a12a77 | 2022-09-14 12:17:42 +0200 | [diff] [blame] | 310 | * 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] | 311 | * at once, on the other side the PSA CRYPTO PAKE api requires | 
|  | 312 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be | 
|  | 313 | * retrieved in sequence. | 
|  | 314 | * | 
|  | 315 | * In order to achieve API compatibility, the whole X1+X2 or X2S steps | 
|  | 316 | * data is stored in an intermediate buffer at first step output call, | 
|  | 317 | * and data is sliced down by parsing the ECPoint records in order | 
|  | 318 | * to return the right parts on each step. | 
|  | 319 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | if (operation->alg == PSA_ALG_JPAKE) { | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 321 | /* Initialize & write round on KEY_SHARE sequences */ | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 322 | if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) { | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 323 | ret = mbedtls_ecjpake_write_round_one(&operation->ctx.jpake, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 324 | operation->buffer, | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 325 | sizeof(operation->buffer), | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | &operation->buffer_length, | 
|  | 327 | mbedtls_psa_get_random, | 
|  | 328 | MBEDTLS_PSA_RANDOM_STATE); | 
|  | 329 | if (ret != 0) { | 
|  | 330 | return mbedtls_ecjpake_to_psa_error(ret); | 
|  | 331 | } | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 332 |  | 
|  | 333 | operation->buffer_offset = 0; | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 334 | } else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) { | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 335 | ret = mbedtls_ecjpake_write_round_two(&operation->ctx.jpake, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | operation->buffer, | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 337 | sizeof(operation->buffer), | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 338 | &operation->buffer_length, | 
|  | 339 | mbedtls_psa_get_random, | 
|  | 340 | MBEDTLS_PSA_RANDOM_STATE); | 
|  | 341 | if (ret != 0) { | 
|  | 342 | return mbedtls_ecjpake_to_psa_error(ret); | 
|  | 343 | } | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 344 |  | 
|  | 345 | operation->buffer_offset = 0; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
| Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 348 | /* | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 349 | * mbedtls_ecjpake_write_round_xxx() outputs thing in the format | 
|  | 350 | * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is | 
|  | 351 | * that the data for each step is prepended with a length byte, and | 
|  | 352 | * then they're concatenated. Additionally, the server's second round | 
|  | 353 | * output is prepended with a 3-bytes ECParameters structure. | 
| Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 354 | * | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 355 | * In PSA, we output each step separately, and don't prepend the | 
|  | 356 | * output with a length byte, even less a curve identifier, as that | 
|  | 357 | * information is already available. | 
| Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 358 | */ | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 359 | if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE && | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 360 | operation->role == MBEDTLS_ECJPAKE_SERVER) { | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 361 | /* Skip ECParameters, with is 3 bytes (RFC 8422) */ | 
|  | 362 | operation->buffer_offset += 3; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 363 | } | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 364 |  | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 365 | /* Read the length byte then move past it to the data */ | 
|  | 366 | length = operation->buffer[operation->buffer_offset]; | 
|  | 367 | operation->buffer_offset += 1; | 
|  | 368 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 369 | if (operation->buffer_offset + length > operation->buffer_length) { | 
|  | 370 | return PSA_ERROR_DATA_CORRUPT; | 
|  | 371 | } | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 372 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | if (output_size < length) { | 
|  | 374 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 375 | } | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 376 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | memcpy(output, | 
|  | 378 | operation->buffer + operation->buffer_offset, | 
|  | 379 | length); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 380 | *output_length = length; | 
|  | 381 |  | 
|  | 382 | operation->buffer_offset += length; | 
|  | 383 |  | 
|  | 384 | /* Reset buffer after ZK_PROOF sequence */ | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 385 | if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) || | 
|  | 386 | (step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) { | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 387 | mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer)); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 388 | operation->buffer_length = 0; | 
|  | 389 | operation->buffer_offset = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | } | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 391 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | return PSA_SUCCESS; | 
|  | 393 | } else | 
| Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 394 | #else | 
|  | 395 | (void) step; | 
|  | 396 | (void) output; | 
|  | 397 | (void) output_size; | 
|  | 398 | (void) output_length; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 399 | #endif | 
| Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 400 | { return PSA_ERROR_NOT_SUPPORTED; } | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 401 | } | 
|  | 402 |  | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 403 | psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation, | 
| Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 404 | psa_crypto_driver_pake_step_t step, | 
| Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 405 | uint8_t *output, | 
|  | 406 | size_t output_size, | 
|  | 407 | size_t *output_length) | 
| Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 408 | { | 
| Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 409 | psa_status_t status = mbedtls_psa_pake_output_internal( | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 410 | operation, step, output, output_size, output_length); | 
| Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 411 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | return status; | 
| Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 413 | } | 
|  | 414 |  | 
| Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 415 | static psa_status_t mbedtls_psa_pake_input_internal( | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 416 | mbedtls_psa_pake_operation_t *operation, | 
| Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 417 | psa_crypto_driver_pake_step_t step, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | const uint8_t *input, | 
|  | 419 | size_t input_length) | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 420 | { | 
|  | 421 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Przemek Stekiel | 9a5b812 | 2022-12-22 13:34:47 +0100 | [diff] [blame] | 422 | (void) step; // Unused parameter | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 423 |  | 
| Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 424 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) | 
| Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 425 | /* | 
|  | 426 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different | 
|  | 427 | * handling of input sequencing. | 
|  | 428 | * | 
|  | 429 | * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data | 
|  | 430 | * at once as input, on the other side the PSA CRYPTO PAKE api requires | 
|  | 431 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be | 
|  | 432 | * given in sequence. | 
|  | 433 | * | 
|  | 434 | * In order to achieve API compatibility, each X1+X2 or X4S step data | 
|  | 435 | * is stored sequentially in an intermediate buffer and given to the | 
|  | 436 | * MbedTLS JPAKE API on the last step. | 
|  | 437 | * | 
|  | 438 | * This causes any input error to be only detected on the last step. | 
|  | 439 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | if (operation->alg == PSA_ALG_JPAKE) { | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 441 | /* | 
|  | 442 | * Copy input to local buffer and format it as the Mbed TLS API | 
|  | 443 | * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7. | 
|  | 444 | * The summary is that the data for each step is prepended with a | 
|  | 445 | * length byte, and then they're concatenated. Additionally, the | 
|  | 446 | * server's second round output is prepended with a 3-bytes | 
|  | 447 | * ECParameters structure - which means we have to prepend that when | 
|  | 448 | * we're a client. | 
|  | 449 | */ | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 450 | if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE && | 
| Przemek Stekiel | e80ec0a | 2023-03-21 16:49:43 +0100 | [diff] [blame] | 451 | operation->role == MBEDTLS_ECJPAKE_CLIENT) { | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 452 | /* We only support secp256r1. */ | 
|  | 453 | /* This is the ECParameters structure defined by RFC 8422. */ | 
|  | 454 | unsigned char ecparameters[3] = { | 
|  | 455 | 3, /* named_curve */ | 
|  | 456 | 0, 23 /* secp256r1 */ | 
|  | 457 | }; | 
| Przemek Stekiel | 4dc83d4 | 2023-02-27 11:49:35 +0100 | [diff] [blame] | 458 |  | 
| Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 459 | if (operation->buffer_length + sizeof(ecparameters) > | 
|  | 460 | sizeof(operation->buffer)) { | 
| Przemek Stekiel | 4dc83d4 | 2023-02-27 11:49:35 +0100 | [diff] [blame] | 461 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 462 | } | 
|  | 463 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | memcpy(operation->buffer + operation->buffer_length, | 
|  | 465 | ecparameters, sizeof(ecparameters)); | 
|  | 466 | operation->buffer_length += sizeof(ecparameters); | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 467 | } | 
|  | 468 |  | 
| Przemek Stekiel | 4dc83d4 | 2023-02-27 11:49:35 +0100 | [diff] [blame] | 469 | /* | 
| Przemek Stekiel | 691e91a | 2023-03-07 16:26:37 +0100 | [diff] [blame] | 470 | * The core checks that input_length is smaller than | 
|  | 471 | * PSA_PAKE_INPUT_MAX_SIZE. | 
|  | 472 | * Thus no risk of integer overflow here. | 
| Przemek Stekiel | 4dc83d4 | 2023-02-27 11:49:35 +0100 | [diff] [blame] | 473 | */ | 
|  | 474 | if (operation->buffer_length + input_length + 1 > sizeof(operation->buffer)) { | 
|  | 475 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 476 | } | 
|  | 477 |  | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 478 | /* Write the length byte */ | 
| Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 479 | operation->buffer[operation->buffer_length] = (uint8_t) input_length; | 
| Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 480 | operation->buffer_length += 1; | 
|  | 481 |  | 
|  | 482 | /* Finally copy the data */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | memcpy(operation->buffer + operation->buffer_length, | 
|  | 484 | input, input_length); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 485 | operation->buffer_length += input_length; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 486 |  | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 487 | /* Load buffer at each last round ZK_PROOF */ | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 488 | if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) { | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 489 | ret = mbedtls_ecjpake_read_round_one(&operation->ctx.jpake, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | operation->buffer, | 
|  | 491 | operation->buffer_length); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 492 |  | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 493 | mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer)); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 494 | operation->buffer_length = 0; | 
|  | 495 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | if (ret != 0) { | 
|  | 497 | return mbedtls_ecjpake_to_psa_error(ret); | 
|  | 498 | } | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 499 | } else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) { | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 500 | ret = mbedtls_ecjpake_read_round_two(&operation->ctx.jpake, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | operation->buffer, | 
|  | 502 | operation->buffer_length); | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 503 |  | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 504 | mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer)); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 505 | operation->buffer_length = 0; | 
|  | 506 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | if (ret != 0) { | 
|  | 508 | return mbedtls_ecjpake_to_psa_error(ret); | 
|  | 509 | } | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 510 | } | 
|  | 511 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | return PSA_SUCCESS; | 
|  | 513 | } else | 
| Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 514 | #else | 
|  | 515 | (void) step; | 
|  | 516 | (void) input; | 
|  | 517 | (void) input_length; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 518 | #endif | 
| Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 519 | { return PSA_ERROR_NOT_SUPPORTED; } | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 522 | psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation, | 
| Przemek Stekiel | 251e86a | 2023-02-17 14:30:50 +0100 | [diff] [blame] | 523 | psa_crypto_driver_pake_step_t step, | 
| Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 524 | const uint8_t *input, | 
|  | 525 | size_t input_length) | 
| Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 526 | { | 
| Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 527 | psa_status_t status = mbedtls_psa_pake_input_internal( | 
| Przemek Stekiel | b09c487 | 2023-01-17 12:05:38 +0100 | [diff] [blame] | 528 | operation, step, input, input_length); | 
| Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 529 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | return status; | 
| Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 531 | } | 
|  | 532 |  | 
| Neil Armstrong | 56b8d23 | 2022-06-01 18:05:57 +0200 | [diff] [blame] | 533 | psa_status_t mbedtls_psa_pake_get_implicit_key( | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 534 | mbedtls_psa_pake_operation_t *operation, | 
| Przemek Stekiel | 6b64862 | 2023-02-19 22:55:33 +0100 | [diff] [blame] | 535 | uint8_t *output, size_t output_size, | 
|  | 536 | size_t *output_length) | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 537 | { | 
|  | 538 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 539 |  | 
| Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 540 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | if (operation->alg == PSA_ALG_JPAKE) { | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 542 | ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.jpake, | 
|  | 543 | output, | 
| Przemek Stekiel | 6b64862 | 2023-02-19 22:55:33 +0100 | [diff] [blame] | 544 | output_size, | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 545 | output_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | mbedtls_psa_get_random, | 
|  | 547 | MBEDTLS_PSA_RANDOM_STATE); | 
|  | 548 | if (ret != 0) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | return mbedtls_ecjpake_to_psa_error(ret); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 550 | } | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 551 |  | 
| Przemek Stekiel | 0c78180 | 2022-11-29 14:53:13 +0100 | [diff] [blame] | 552 | return PSA_SUCCESS; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | } else | 
| Neil Armstrong | 5ae6096 | 2022-09-15 11:29:46 +0200 | [diff] [blame] | 554 | #else | 
|  | 555 | (void) output; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 556 | #endif | 
| Przemek Stekiel | e12ed36 | 2022-12-21 12:54:46 +0100 | [diff] [blame] | 557 | { return PSA_ERROR_NOT_SUPPORTED; } | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 560 | psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation) | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 561 | { | 
| Przemek Stekiel | ce131bf | 2023-02-21 12:19:27 +0100 | [diff] [blame] | 562 | mbedtls_platform_zeroize(operation->password, operation->password_len); | 
|  | 563 | mbedtls_free(operation->password); | 
|  | 564 | operation->password = NULL; | 
|  | 565 | operation->password_len = 0; | 
| Przemek Stekiel | 6c76441 | 2022-11-22 14:05:12 +0100 | [diff] [blame] | 566 |  | 
| Przemek Stekiel | ce131bf | 2023-02-21 12:19:27 +0100 | [diff] [blame] | 567 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | if (operation->alg == PSA_ALG_JPAKE) { | 
| Przemek Stekiel | 6e628a4 | 2023-04-25 13:11:36 +0200 | [diff] [blame] | 569 | operation->role = MBEDTLS_ECJPAKE_NONE; | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 570 | mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer)); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 571 | operation->buffer_length = 0; | 
|  | 572 | operation->buffer_offset = 0; | 
| Przemek Stekiel | e3ef3a1 | 2023-02-27 10:20:06 +0100 | [diff] [blame] | 573 | mbedtls_ecjpake_free(&operation->ctx.jpake); | 
| Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 574 | } | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 575 | #endif | 
|  | 576 |  | 
| Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 577 | operation->alg = PSA_ALG_NONE; | 
| Neil Armstrong | fbc4b4a | 2022-06-10 08:54:53 +0200 | [diff] [blame] | 578 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 579 | return PSA_SUCCESS; | 
| Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 580 | } | 
|  | 581 |  | 
|  | 582 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ | 
|  | 583 |  | 
|  | 584 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |