blob: db00cbd28d4db59bec0dbfcfe8235cdbd3178386 [file] [log] [blame]
Neil Armstronga4cc7d62022-05-25 11:30:48 +02001/*
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 Armstrong56b8d232022-06-01 18:05:57 +020027#include "psa_crypto_pake.h"
Neil Armstronga4cc7d62022-05-25 11:30:48 +020028#include "psa_crypto_slot_management.h"
29
30#include <mbedtls/ecjpake.h>
Kristian Larssona1aeff42023-09-04 10:19:27 +020031#include "psa_util_internal.h"
Neil Armstronga4cc7d62022-05-25 11:30:48 +020032
33#include <mbedtls/platform.h>
34#include <mbedtls/error.h>
35#include <string.h>
36
Neil Armstronga4cc7d62022-05-25 11:30:48 +020037/*
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 Armstrongc29f8472022-06-08 13:34:49 +020046 * | | psa_pake_set_role()
Neil Armstronga4cc7d62022-05-25 11:30:48 +020047 * |
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 Horstmann57727cd2023-06-20 19:40:57 +010082/*
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 Armstrongdb05cbf2022-06-15 15:25:45 +0200116#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100117static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200118{
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 switch (ret) {
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200120 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 Peskine449bd832023-01-11 14:50:10 +0100124 return PSA_ERROR_DATA_INVALID;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200125 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
126 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 return PSA_ERROR_BUFFER_TOO_SMALL;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200128 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200130 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200132 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return PSA_ERROR_GENERIC_ERROR;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200134 }
135}
136#endif
137
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100138#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
Neil Armstronga557cb82022-06-10 08:58:32 +0200139#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100140static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200141{
142 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200143
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100144 mbedtls_ecjpake_init(&operation->ctx.jpake);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200145
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100146 ret = mbedtls_ecjpake_setup(&operation->ctx.jpake,
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100147 operation->role,
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 MBEDTLS_MD_SHA256,
149 MBEDTLS_ECP_DP_SECP256R1,
150 operation->password,
151 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_platform_zeroize(operation->password, operation->password_len);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (ret != 0) {
156 return mbedtls_ecjpake_to_psa_error(ret);
157 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200160}
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100161#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100162
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100163/* The only two JPAKE user/peer identifiers supported in built-in implementation. */
164static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
165static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
166
Przemek Stekiele12ed362022-12-21 12:54:46 +0100167psa_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 Stekiele80ec0a2023-03-21 16:49:43 +0100171 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 Stekiel5cbca792023-01-17 16:51:19 +0100174 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekiele12ed362022-12-21 12:54:46 +0100175
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100176 status = psa_crypto_driver_pake_get_password_len(inputs, &password_len);
177 if (status != PSA_SUCCESS) {
178 return status;
179 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100180
Paul Elliott2a12fc22023-08-11 17:45:20 +0100181 status = psa_crypto_driver_pake_get_user_len(inputs, &user_len);
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100182 if (status != PSA_SUCCESS) {
183 return status;
184 }
185
Paul Elliott2a12fc22023-08-11 17:45:20 +0100186 status = psa_crypto_driver_pake_get_peer_len(inputs, &peer_len);
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100187 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 Stekiele12ed362022-12-21 12:54:46 +0100195
Przemek Stekiela54dc692023-02-20 10:18:10 +0100196 operation->password = mbedtls_calloc(1, password_len);
197 if (operation->password == NULL) {
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100198 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 Stekiela54dc692023-02-20 10:18:10 +0100212 }
213
214 status = psa_crypto_driver_pake_get_password(inputs, operation->password,
Przemek Stekiel083745e2023-02-23 17:28:23 +0100215 password_len, &actual_password_len);
Przemek Stekiela54dc692023-02-20 10:18:10 +0100216 if (status != PSA_SUCCESS) {
217 goto error;
218 }
219
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100220 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 Stekiela54dc692023-02-20 10:18:10 +0100232 operation->password_len = actual_password_len;
233 operation->alg = cipher_suite.algorithm;
234
Przemek Stekiele12ed362022-12-21 12:54:46 +0100235#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 Stekiela54dc692023-02-20 10:18:10 +0100241 status = PSA_ERROR_NOT_SUPPORTED;
242 goto error;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100243 }
244
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100245 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 Stekiele12ed362022-12-21 12:54:46 +0100264 operation->buffer_length = 0;
265 operation->buffer_offset = 0;
266
267 status = psa_pake_ecjpake_setup(operation);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100268 if (status != PSA_SUCCESS) {
Przemek Stekiela54dc692023-02-20 10:18:10 +0100269 goto error;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100270 }
271
Przemek Stekielaede2ad2023-04-25 14:30:34 +0200272 /* Role has been set, release user/peer buffers. */
273 mbedtls_free(user); mbedtls_free(peer);
274
Przemek Stekiele12ed362022-12-21 12:54:46 +0100275 return PSA_SUCCESS;
276 } else
277#else
278 (void) operation;
279 (void) inputs;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200280#endif
Przemek Stekielce131bf2023-02-21 12:19:27 +0100281 { status = PSA_ERROR_NOT_SUPPORTED; }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100282
Przemek Stekiela54dc692023-02-20 10:18:10 +0100283error:
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100284 mbedtls_free(user); mbedtls_free(peer);
Przemek Stekield93de322023-02-24 08:39:04 +0100285 /* 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 Stekielce131bf2023-02-21 12:19:27 +0100290 mbedtls_psa_pake_abort(operation);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100291 return status;
292}
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200293
Neil Armstrong56b8d232022-06-01 18:05:57 +0200294static psa_status_t mbedtls_psa_pake_output_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100295 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100296 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 uint8_t *output,
298 size_t output_size,
299 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200300{
301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200302 size_t length;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100303 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100304
Neil Armstronga557cb82022-06-10 08:58:32 +0200305#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200306 /*
Thomas Daubney540324c2023-10-06 17:07:24 +0100307 * The PSA CRYPTO PAKE and Mbed TLS JPAKE API have a different
Neil Armstrongfa849622022-09-13 15:10:46 +0200308 * handling of output sequencing.
309 *
Thomas Daubney540324c2023-10-06 17:07:24 +0100310 * The Mbed TLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200311 * 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 Peskine449bd832023-01-11 14:50:10 +0100320 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200321 /* Initialize & write round on KEY_SHARE sequences */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100322 if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100323 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 operation->buffer,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100325 sizeof(operation->buffer),
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 &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 Armstrong4efd7a42022-06-08 17:18:31 +0200332
333 operation->buffer_offset = 0;
Przemek Stekielb09c4872023-01-17 12:05:38 +0100334 } else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100335 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 operation->buffer,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100337 sizeof(operation->buffer),
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 &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 Armstrong4efd7a42022-06-08 17:18:31 +0200344
345 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200346 }
347
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200348 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200349 * 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 Armstrong1d0294f2022-09-13 14:49:24 +0200354 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200355 * 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 Armstrong1d0294f2022-09-13 14:49:24 +0200358 */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100359 if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE &&
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100360 operation->role == MBEDTLS_ECJPAKE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200361 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
362 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200363 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200364
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200365 /* 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 Peskine449bd832023-01-11 14:50:10 +0100369 if (operation->buffer_offset + length > operation->buffer_length) {
370 return PSA_ERROR_DATA_CORRUPT;
371 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (output_size < length) {
374 return PSA_ERROR_BUFFER_TOO_SMALL;
375 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 memcpy(output,
378 operation->buffer + operation->buffer_offset,
379 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200380 *output_length = length;
381
382 operation->buffer_offset += length;
383
384 /* Reset buffer after ZK_PROOF sequence */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100385 if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) ||
386 (step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100387 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200388 operation->buffer_length = 0;
389 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 return PSA_SUCCESS;
393 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200394#else
395 (void) step;
396 (void) output;
397 (void) output_size;
398 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200399#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200400 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200401}
402
Przemek Stekiel6c764412022-11-22 14:05:12 +0100403psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100404 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200405 uint8_t *output,
406 size_t output_size,
407 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200408{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200409 psa_status_t status = mbedtls_psa_pake_output_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100410 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200413}
414
Neil Armstrong56b8d232022-06-01 18:05:57 +0200415static psa_status_t mbedtls_psa_pake_input_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100416 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100417 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 const uint8_t *input,
419 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200420{
421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100422 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100423
Neil Armstronga557cb82022-06-10 08:58:32 +0200424#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200425 /*
Thomas Daubney540324c2023-10-06 17:07:24 +0100426 * The PSA CRYPTO PAKE and Mbed TLS JPAKE API have a different
Neil Armstrongfa849622022-09-13 15:10:46 +0200427 * handling of input sequencing.
428 *
Thomas Daubney540324c2023-10-06 17:07:24 +0100429 * The Mbed TLS JPAKE API takes the whole X1+X2 or X4S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200430 * 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
Thomas Daubney540324c2023-10-06 17:07:24 +0100436 * Mbed TLS JPAKE API on the last step.
Neil Armstrongfa849622022-09-13 15:10:46 +0200437 *
438 * This causes any input error to be only detected on the last step.
439 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 if (operation->alg == PSA_ALG_JPAKE) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200441 /*
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 Stekielb09c4872023-01-17 12:05:38 +0100450 if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE &&
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100451 operation->role == MBEDTLS_ECJPAKE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200452 /* 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 Stekiel4dc83d42023-02-27 11:49:35 +0100458
Przemek Stekiel691e91a2023-03-07 16:26:37 +0100459 if (operation->buffer_length + sizeof(ecparameters) >
460 sizeof(operation->buffer)) {
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100461 return PSA_ERROR_BUFFER_TOO_SMALL;
462 }
463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 memcpy(operation->buffer + operation->buffer_length,
465 ecparameters, sizeof(ecparameters));
466 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200467 }
468
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100469 /*
Przemek Stekiel691e91a2023-03-07 16:26:37 +0100470 * The core checks that input_length is smaller than
471 * PSA_PAKE_INPUT_MAX_SIZE.
472 * Thus no risk of integer overflow here.
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100473 */
474 if (operation->buffer_length + input_length + 1 > sizeof(operation->buffer)) {
475 return PSA_ERROR_BUFFER_TOO_SMALL;
476 }
477
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200478 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200479 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200480 operation->buffer_length += 1;
481
482 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 memcpy(operation->buffer + operation->buffer_length,
484 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200485 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200486
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200487 /* Load buffer at each last round ZK_PROOF */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100488 if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100489 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 operation->buffer,
491 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200492
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100493 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200494 operation->buffer_length = 0;
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (ret != 0) {
497 return mbedtls_ecjpake_to_psa_error(ret);
498 }
Przemek Stekielb09c4872023-01-17 12:05:38 +0100499 } else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100500 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 operation->buffer,
502 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200503
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100504 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200505 operation->buffer_length = 0;
506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (ret != 0) {
508 return mbedtls_ecjpake_to_psa_error(ret);
509 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200510 }
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return PSA_SUCCESS;
513 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200514#else
515 (void) step;
516 (void) input;
517 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200518#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200519 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200520}
521
Przemek Stekiel6c764412022-11-22 14:05:12 +0100522psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100523 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200524 const uint8_t *input,
525 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200526{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200527 psa_status_t status = mbedtls_psa_pake_input_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100528 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200531}
532
Neil Armstrong56b8d232022-06-01 18:05:57 +0200533psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100534 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100535 uint8_t *output, size_t output_size,
536 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200537{
538 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200539
Neil Armstronga557cb82022-06-10 08:58:32 +0200540#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100542 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.jpake,
543 output,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100544 output_size,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100545 output_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 mbedtls_psa_get_random,
547 MBEDTLS_PSA_RANDOM_STATE);
548 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200550 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200551
Przemek Stekiel0c781802022-11-29 14:53:13 +0100552 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200554#else
555 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200556#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100557 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200558}
559
Przemek Stekiel6c764412022-11-22 14:05:12 +0100560psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200561{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +0100562 mbedtls_zeroize_and_free(operation->password, operation->password_len);
Przemek Stekielce131bf2023-02-21 12:19:27 +0100563 operation->password = NULL;
564 operation->password_len = 0;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100565
Przemek Stekielce131bf2023-02-21 12:19:27 +0100566#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6e628a42023-04-25 13:11:36 +0200568 operation->role = MBEDTLS_ECJPAKE_NONE;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100569 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200570 operation->buffer_length = 0;
571 operation->buffer_offset = 0;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100572 mbedtls_ecjpake_free(&operation->ctx.jpake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200573 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200574#endif
575
Neil Armstrongcb679f22022-09-13 14:43:07 +0200576 operation->alg = PSA_ALG_NONE;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200579}
580
581#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
582
583#endif /* MBEDTLS_PSA_CRYPTO_C */