blob: 7140faf83779ee348704765251d75c1de3a0ecd9 [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>
31#include <mbedtls/psa_util.h>
32
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
Neil Armstrongdb05cbf2022-06-15 15:25:45 +020082#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +010083static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
Neil Armstrongdb05cbf2022-06-15 15:25:45 +020084{
Gilles Peskine449bd832023-01-11 14:50:10 +010085 switch (ret) {
Neil Armstrongdb05cbf2022-06-15 15:25:45 +020086 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
87 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
88 case MBEDTLS_ERR_ECP_INVALID_KEY:
89 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return PSA_ERROR_DATA_INVALID;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +020091 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
92 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return PSA_ERROR_BUFFER_TOO_SMALL;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +020094 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +020096 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +020098 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return PSA_ERROR_GENERIC_ERROR;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200100 }
101}
102#endif
103
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100104#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
Neil Armstronga557cb82022-06-10 08:58:32 +0200105#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100106static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200107{
108 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200109
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100110 mbedtls_ecjpake_init(&operation->ctx.jpake);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200111
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100112 ret = mbedtls_ecjpake_setup(&operation->ctx.jpake,
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100113 operation->role,
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 MBEDTLS_MD_SHA256,
115 MBEDTLS_ECP_DP_SECP256R1,
116 operation->password,
117 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_platform_zeroize(operation->password, operation->password_len);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (ret != 0) {
122 return mbedtls_ecjpake_to_psa_error(ret);
123 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200126}
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100127#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100128
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100129/* The only two JPAKE user/peer identifiers supported in built-in implementation. */
130static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
131static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
132
Przemek Stekiele12ed362022-12-21 12:54:46 +0100133psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
134 const psa_crypto_driver_pake_inputs_t *inputs)
135{
136 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100137 size_t user_len = 0, peer_len = 0, password_len = 0;
138 uint8_t *peer = NULL, *user = NULL;
139 size_t actual_user_len = 0, actual_peer_len = 0, actual_password_len = 0;
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100140 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekiele12ed362022-12-21 12:54:46 +0100141
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100142 status = psa_crypto_driver_pake_get_password_len(inputs, &password_len);
143 if (status != PSA_SUCCESS) {
144 return status;
145 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100146
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100147 psa_crypto_driver_pake_get_user_len(inputs, &user_len);
148 if (status != PSA_SUCCESS) {
149 return status;
150 }
151
152 psa_crypto_driver_pake_get_peer_len(inputs, &peer_len);
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100153 if (status != PSA_SUCCESS) {
154 return status;
155 }
156
157 status = psa_crypto_driver_pake_get_cipher_suite(inputs, &cipher_suite);
158 if (status != PSA_SUCCESS) {
159 return status;
160 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100161
Przemek Stekiela54dc692023-02-20 10:18:10 +0100162 operation->password = mbedtls_calloc(1, password_len);
163 if (operation->password == NULL) {
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100164 status = PSA_ERROR_INSUFFICIENT_MEMORY;
165 goto error;
166 }
167
168 user = mbedtls_calloc(1, user_len);
169 if (user == NULL) {
170 status = PSA_ERROR_INSUFFICIENT_MEMORY;
171 goto error;
172 }
173
174 peer = mbedtls_calloc(1, peer_len);
175 if (peer == NULL) {
176 status = PSA_ERROR_INSUFFICIENT_MEMORY;
177 goto error;
Przemek Stekiela54dc692023-02-20 10:18:10 +0100178 }
179
180 status = psa_crypto_driver_pake_get_password(inputs, operation->password,
Przemek Stekiel083745e2023-02-23 17:28:23 +0100181 password_len, &actual_password_len);
Przemek Stekiela54dc692023-02-20 10:18:10 +0100182 if (status != PSA_SUCCESS) {
183 goto error;
184 }
185
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100186 status = psa_crypto_driver_pake_get_user(inputs, user,
187 user_len, &actual_user_len);
188 if (status != PSA_SUCCESS) {
189 goto error;
190 }
191
192 status = psa_crypto_driver_pake_get_peer(inputs, peer,
193 peer_len, &actual_peer_len);
194 if (status != PSA_SUCCESS) {
195 goto error;
196 }
197
Przemek Stekiela54dc692023-02-20 10:18:10 +0100198 operation->password_len = actual_password_len;
199 operation->alg = cipher_suite.algorithm;
200
Przemek Stekiele12ed362022-12-21 12:54:46 +0100201#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
202 if (cipher_suite.algorithm == PSA_ALG_JPAKE) {
203 if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
204 cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 ||
205 cipher_suite.bits != 256 ||
206 cipher_suite.hash != PSA_ALG_SHA_256) {
Przemek Stekiela54dc692023-02-20 10:18:10 +0100207 status = PSA_ERROR_NOT_SUPPORTED;
208 goto error;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100209 }
210
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100211 const size_t user_peer_len = sizeof(jpake_client_id); // client and server have the same length
212 if (actual_user_len != user_peer_len ||
213 actual_peer_len != user_peer_len) {
214 status = PSA_ERROR_NOT_SUPPORTED;
215 goto error;
216 }
217
218 if (memcmp(user, jpake_client_id, actual_user_len) == 0 &&
219 memcmp(peer, jpake_server_id, actual_peer_len) == 0) {
220 operation->role = MBEDTLS_ECJPAKE_CLIENT;
221 } else
222 if (memcmp(user, jpake_server_id, actual_user_len) == 0 &&
223 memcmp(peer, jpake_client_id, actual_peer_len) == 0) {
224 operation->role = MBEDTLS_ECJPAKE_SERVER;
225 } else {
226 status = PSA_ERROR_NOT_SUPPORTED;
227 goto error;
228 }
229
Przemek Stekiele12ed362022-12-21 12:54:46 +0100230 operation->buffer_length = 0;
231 operation->buffer_offset = 0;
232
233 status = psa_pake_ecjpake_setup(operation);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100234 if (status != PSA_SUCCESS) {
Przemek Stekiela54dc692023-02-20 10:18:10 +0100235 goto error;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100236 }
237
Przemek Stekielaede2ad2023-04-25 14:30:34 +0200238 /* Role has been set, release user/peer buffers. */
239 mbedtls_free(user); mbedtls_free(peer);
240
Przemek Stekiele12ed362022-12-21 12:54:46 +0100241 return PSA_SUCCESS;
242 } else
243#else
244 (void) operation;
245 (void) inputs;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200246#endif
Przemek Stekielce131bf2023-02-21 12:19:27 +0100247 { status = PSA_ERROR_NOT_SUPPORTED; }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100248
Przemek Stekiela54dc692023-02-20 10:18:10 +0100249error:
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100250 mbedtls_free(user); mbedtls_free(peer);
Przemek Stekield93de322023-02-24 08:39:04 +0100251 /* In case of failure of the setup of a multipart operation, the PSA driver interface
252 * specifies that the core does not call any other driver entry point thus does not
253 * call mbedtls_psa_pake_abort(). Therefore call it here to do the needed clean
254 * up like freeing the memory that may have been allocated to store the password.
255 */
Przemek Stekielce131bf2023-02-21 12:19:27 +0100256 mbedtls_psa_pake_abort(operation);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100257 return status;
258}
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200259
Neil Armstrong56b8d232022-06-01 18:05:57 +0200260static psa_status_t mbedtls_psa_pake_output_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100261 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100262 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 uint8_t *output,
264 size_t output_size,
265 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200266{
267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200268 size_t length;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100269 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100270
Neil Armstronga557cb82022-06-10 08:58:32 +0200271#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200272 /*
273 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
274 * handling of output sequencing.
275 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200276 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200277 * at once, on the other side the PSA CRYPTO PAKE api requires
278 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
279 * retrieved in sequence.
280 *
281 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
282 * data is stored in an intermediate buffer at first step output call,
283 * and data is sliced down by parsing the ECPoint records in order
284 * to return the right parts on each step.
285 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200287 /* Initialize & write round on KEY_SHARE sequences */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100288 if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100289 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 operation->buffer,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100291 sizeof(operation->buffer),
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 &operation->buffer_length,
293 mbedtls_psa_get_random,
294 MBEDTLS_PSA_RANDOM_STATE);
295 if (ret != 0) {
296 return mbedtls_ecjpake_to_psa_error(ret);
297 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200298
299 operation->buffer_offset = 0;
Przemek Stekielb09c4872023-01-17 12:05:38 +0100300 } else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100301 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 operation->buffer,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100303 sizeof(operation->buffer),
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 &operation->buffer_length,
305 mbedtls_psa_get_random,
306 MBEDTLS_PSA_RANDOM_STATE);
307 if (ret != 0) {
308 return mbedtls_ecjpake_to_psa_error(ret);
309 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200310
311 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200312 }
313
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200314 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200315 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
316 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
317 * that the data for each step is prepended with a length byte, and
318 * then they're concatenated. Additionally, the server's second round
319 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200320 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200321 * In PSA, we output each step separately, and don't prepend the
322 * output with a length byte, even less a curve identifier, as that
323 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200324 */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100325 if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE &&
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100326 operation->role == MBEDTLS_ECJPAKE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200327 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
328 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200329 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200330
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200331 /* Read the length byte then move past it to the data */
332 length = operation->buffer[operation->buffer_offset];
333 operation->buffer_offset += 1;
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (operation->buffer_offset + length > operation->buffer_length) {
336 return PSA_ERROR_DATA_CORRUPT;
337 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (output_size < length) {
340 return PSA_ERROR_BUFFER_TOO_SMALL;
341 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 memcpy(output,
344 operation->buffer + operation->buffer_offset,
345 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200346 *output_length = length;
347
348 operation->buffer_offset += length;
349
350 /* Reset buffer after ZK_PROOF sequence */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100351 if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) ||
352 (step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100353 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200354 operation->buffer_length = 0;
355 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 return PSA_SUCCESS;
359 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200360#else
361 (void) step;
362 (void) output;
363 (void) output_size;
364 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200365#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200366 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200367}
368
Przemek Stekiel6c764412022-11-22 14:05:12 +0100369psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100370 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200371 uint8_t *output,
372 size_t output_size,
373 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200374{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200375 psa_status_t status = mbedtls_psa_pake_output_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100376 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200379}
380
Neil Armstrong56b8d232022-06-01 18:05:57 +0200381static psa_status_t mbedtls_psa_pake_input_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100382 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100383 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 const uint8_t *input,
385 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200386{
387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100388 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100389
Neil Armstronga557cb82022-06-10 08:58:32 +0200390#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200391 /*
392 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
393 * handling of input sequencing.
394 *
395 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
396 * at once as input, on the other side the PSA CRYPTO PAKE api requires
397 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
398 * given in sequence.
399 *
400 * In order to achieve API compatibility, each X1+X2 or X4S step data
401 * is stored sequentially in an intermediate buffer and given to the
402 * MbedTLS JPAKE API on the last step.
403 *
404 * This causes any input error to be only detected on the last step.
405 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (operation->alg == PSA_ALG_JPAKE) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200407 /*
408 * Copy input to local buffer and format it as the Mbed TLS API
409 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
410 * The summary is that the data for each step is prepended with a
411 * length byte, and then they're concatenated. Additionally, the
412 * server's second round output is prepended with a 3-bytes
413 * ECParameters structure - which means we have to prepend that when
414 * we're a client.
415 */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100416 if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE &&
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100417 operation->role == MBEDTLS_ECJPAKE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200418 /* We only support secp256r1. */
419 /* This is the ECParameters structure defined by RFC 8422. */
420 unsigned char ecparameters[3] = {
421 3, /* named_curve */
422 0, 23 /* secp256r1 */
423 };
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100424
Przemek Stekiel691e91a2023-03-07 16:26:37 +0100425 if (operation->buffer_length + sizeof(ecparameters) >
426 sizeof(operation->buffer)) {
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100427 return PSA_ERROR_BUFFER_TOO_SMALL;
428 }
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 memcpy(operation->buffer + operation->buffer_length,
431 ecparameters, sizeof(ecparameters));
432 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200433 }
434
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100435 /*
Przemek Stekiel691e91a2023-03-07 16:26:37 +0100436 * The core checks that input_length is smaller than
437 * PSA_PAKE_INPUT_MAX_SIZE.
438 * Thus no risk of integer overflow here.
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100439 */
440 if (operation->buffer_length + input_length + 1 > sizeof(operation->buffer)) {
441 return PSA_ERROR_BUFFER_TOO_SMALL;
442 }
443
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200444 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200445 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200446 operation->buffer_length += 1;
447
448 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 memcpy(operation->buffer + operation->buffer_length,
450 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200451 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200452
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200453 /* Load buffer at each last round ZK_PROOF */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100454 if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100455 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 operation->buffer,
457 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200458
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100459 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200460 operation->buffer_length = 0;
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (ret != 0) {
463 return mbedtls_ecjpake_to_psa_error(ret);
464 }
Przemek Stekielb09c4872023-01-17 12:05:38 +0100465 } else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100466 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 operation->buffer,
468 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200469
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100470 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200471 operation->buffer_length = 0;
472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (ret != 0) {
474 return mbedtls_ecjpake_to_psa_error(ret);
475 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200476 }
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 return PSA_SUCCESS;
479 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200480#else
481 (void) step;
482 (void) input;
483 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200484#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200485 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200486}
487
Przemek Stekiel6c764412022-11-22 14:05:12 +0100488psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100489 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200490 const uint8_t *input,
491 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200492{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200493 psa_status_t status = mbedtls_psa_pake_input_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100494 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200497}
498
Neil Armstrong56b8d232022-06-01 18:05:57 +0200499psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100500 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100501 uint8_t *output, size_t output_size,
502 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200503{
504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200505
Neil Armstronga557cb82022-06-10 08:58:32 +0200506#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100508 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.jpake,
509 output,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100510 output_size,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100511 output_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 mbedtls_psa_get_random,
513 MBEDTLS_PSA_RANDOM_STATE);
514 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200516 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200517
Przemek Stekiel0c781802022-11-29 14:53:13 +0100518 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200520#else
521 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200522#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100523 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200524}
525
Przemek Stekiel6c764412022-11-22 14:05:12 +0100526psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200527{
Przemek Stekielce131bf2023-02-21 12:19:27 +0100528 mbedtls_platform_zeroize(operation->password, operation->password_len);
529 mbedtls_free(operation->password);
530 operation->password = NULL;
531 operation->password_len = 0;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100532
Przemek Stekielce131bf2023-02-21 12:19:27 +0100533#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6e628a42023-04-25 13:11:36 +0200535 operation->role = MBEDTLS_ECJPAKE_NONE;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100536 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200537 operation->buffer_length = 0;
538 operation->buffer_offset = 0;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100539 mbedtls_ecjpake_free(&operation->ctx.jpake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200540 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200541#endif
542
Neil Armstrongcb679f22022-09-13 14:43:07 +0200543 operation->alg = PSA_ALG_NONE;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200546}
547
548#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
549
550#endif /* MBEDTLS_PSA_CRYPTO_C */