blob: d9b2ecd9add19cd27fac3c6ad7c1631002c76f85 [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 Armstrongbcd5bd92022-09-05 18:33:23 +020082/*
83 * The first PAKE step shares the same sequences of the second PAKE step
84 * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
Neil Armstrongb39833c2022-09-06 11:36:02 +020085 * It's simpler to share the same sequences numbers of the first
Neil Armstrongbcd5bd92022-09-05 18:33:23 +020086 * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
87 *
88 * State sequence with step, state & sequence enums:
89 * => Input & Output Step = PSA_PAKE_STEP_INVALID
90 * => state = PSA_PAKE_STATE_INVALID
91 * psa_pake_setup()
92 * => Input & Output Step = PSA_PAKE_STEP_X1_X2
93 * => state = PSA_PAKE_STATE_SETUP
94 * => sequence = PSA_PAKE_SEQ_INVALID
95 * |
96 * |--- In any order: (First round input before or after first round output)
97 * | | First call of psa_pake_output() or psa_pake_input() sets
98 * | | state = PSA_PAKE_STATE_READY
99 * | |
100 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
101 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
102 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
103 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
104 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
105 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
106 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
107 * | | | => state = PSA_PAKE_STATE_READY
108 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200109 * | | | => Output Step = PSA_PAKE_STEP_X2S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200110 * | |
111 * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
112 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
113 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
114 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
115 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
116 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
117 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
118 * | | | => state = PSA_PAKE_STATE_READY
119 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200120 * | | | => Output Step = PSA_PAKE_INPUT_X4S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200121 * |
122 * |--- In any order: (Second round input before or after second round output)
123 * | |
124 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
125 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
126 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
127 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
128 * | | | => state = PSA_PAKE_STATE_READY
129 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200130 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200131 * | |
132 * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
133 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
134 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
135 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
136 * | | | => state = PSA_PAKE_STATE_READY
137 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200138 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200139 * |
140 * psa_pake_get_implicit_key()
141 * => Input & Output Step = PSA_PAKE_STEP_INVALID
142 */
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200143
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200144#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200146{
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 switch (ret) {
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200148 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
149 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
150 case MBEDTLS_ERR_ECP_INVALID_KEY:
151 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return PSA_ERROR_DATA_INVALID;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200153 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
154 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_BUFFER_TOO_SMALL;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200156 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200158 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200160 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return PSA_ERROR_GENERIC_ERROR;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200162 }
163}
164#endif
165
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100166#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
Neil Armstronga557cb82022-06-10 08:58:32 +0200167#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100168static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200169{
170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200171
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100172 mbedtls_ecjpake_init(&operation->ctx.jpake);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200173
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100174 ret = mbedtls_ecjpake_setup(&operation->ctx.jpake,
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100175 operation->role,
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 MBEDTLS_MD_SHA256,
177 MBEDTLS_ECP_DP_SECP256R1,
178 operation->password,
179 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_platform_zeroize(operation->password, operation->password_len);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (ret != 0) {
184 return mbedtls_ecjpake_to_psa_error(ret);
185 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200188}
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100189#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100190
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100191/* The only two JPAKE user/peer identifiers supported in built-in implementation. */
192static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
193static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
194
Przemek Stekiele12ed362022-12-21 12:54:46 +0100195psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
196 const psa_crypto_driver_pake_inputs_t *inputs)
197{
198 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100199 size_t user_len = 0, peer_len = 0, password_len = 0;
200 uint8_t *peer = NULL, *user = NULL;
201 size_t actual_user_len = 0, actual_peer_len = 0, actual_password_len = 0;
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100202 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekiele12ed362022-12-21 12:54:46 +0100203
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100204 status = psa_crypto_driver_pake_get_password_len(inputs, &password_len);
205 if (status != PSA_SUCCESS) {
206 return status;
207 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100208
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100209 psa_crypto_driver_pake_get_user_len(inputs, &user_len);
210 if (status != PSA_SUCCESS) {
211 return status;
212 }
213
214 psa_crypto_driver_pake_get_peer_len(inputs, &peer_len);
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100215 if (status != PSA_SUCCESS) {
216 return status;
217 }
218
219 status = psa_crypto_driver_pake_get_cipher_suite(inputs, &cipher_suite);
220 if (status != PSA_SUCCESS) {
221 return status;
222 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100223
Przemek Stekiela54dc692023-02-20 10:18:10 +0100224 operation->password = mbedtls_calloc(1, password_len);
225 if (operation->password == NULL) {
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100226 status = PSA_ERROR_INSUFFICIENT_MEMORY;
227 goto error;
228 }
229
230 user = mbedtls_calloc(1, user_len);
231 if (user == NULL) {
232 status = PSA_ERROR_INSUFFICIENT_MEMORY;
233 goto error;
234 }
235
236 peer = mbedtls_calloc(1, peer_len);
237 if (peer == NULL) {
238 status = PSA_ERROR_INSUFFICIENT_MEMORY;
239 goto error;
Przemek Stekiela54dc692023-02-20 10:18:10 +0100240 }
241
242 status = psa_crypto_driver_pake_get_password(inputs, operation->password,
Przemek Stekiel083745e2023-02-23 17:28:23 +0100243 password_len, &actual_password_len);
Przemek Stekiela54dc692023-02-20 10:18:10 +0100244 if (status != PSA_SUCCESS) {
245 goto error;
246 }
247
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100248 status = psa_crypto_driver_pake_get_user(inputs, user,
249 user_len, &actual_user_len);
250 if (status != PSA_SUCCESS) {
251 goto error;
252 }
253
254 status = psa_crypto_driver_pake_get_peer(inputs, peer,
255 peer_len, &actual_peer_len);
256 if (status != PSA_SUCCESS) {
257 goto error;
258 }
259
Przemek Stekiela54dc692023-02-20 10:18:10 +0100260 operation->password_len = actual_password_len;
261 operation->alg = cipher_suite.algorithm;
262
Przemek Stekiele12ed362022-12-21 12:54:46 +0100263#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
264 if (cipher_suite.algorithm == PSA_ALG_JPAKE) {
265 if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
266 cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 ||
267 cipher_suite.bits != 256 ||
268 cipher_suite.hash != PSA_ALG_SHA_256) {
Przemek Stekiela54dc692023-02-20 10:18:10 +0100269 status = PSA_ERROR_NOT_SUPPORTED;
270 goto error;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100271 }
272
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100273 const size_t user_peer_len = sizeof(jpake_client_id); // client and server have the same length
274 if (actual_user_len != user_peer_len ||
275 actual_peer_len != user_peer_len) {
276 status = PSA_ERROR_NOT_SUPPORTED;
277 goto error;
278 }
279
280 if (memcmp(user, jpake_client_id, actual_user_len) == 0 &&
281 memcmp(peer, jpake_server_id, actual_peer_len) == 0) {
282 operation->role = MBEDTLS_ECJPAKE_CLIENT;
283 } else
284 if (memcmp(user, jpake_server_id, actual_user_len) == 0 &&
285 memcmp(peer, jpake_client_id, actual_peer_len) == 0) {
286 operation->role = MBEDTLS_ECJPAKE_SERVER;
287 } else {
288 status = PSA_ERROR_NOT_SUPPORTED;
289 goto error;
290 }
291
292 /* Role has been set, release user/peer buffers. */
293 mbedtls_free(user); mbedtls_free(peer);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100294
Przemek Stekiele12ed362022-12-21 12:54:46 +0100295 operation->buffer_length = 0;
296 operation->buffer_offset = 0;
297
298 status = psa_pake_ecjpake_setup(operation);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100299 if (status != PSA_SUCCESS) {
Przemek Stekiela54dc692023-02-20 10:18:10 +0100300 goto error;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100301 }
302
303 return PSA_SUCCESS;
304 } else
305#else
306 (void) operation;
307 (void) inputs;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200308#endif
Przemek Stekielce131bf2023-02-21 12:19:27 +0100309 { status = PSA_ERROR_NOT_SUPPORTED; }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100310
Przemek Stekiela54dc692023-02-20 10:18:10 +0100311error:
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100312 mbedtls_free(user); mbedtls_free(peer);
Przemek Stekield93de322023-02-24 08:39:04 +0100313 /* In case of failure of the setup of a multipart operation, the PSA driver interface
314 * specifies that the core does not call any other driver entry point thus does not
315 * call mbedtls_psa_pake_abort(). Therefore call it here to do the needed clean
316 * up like freeing the memory that may have been allocated to store the password.
317 */
Przemek Stekielce131bf2023-02-21 12:19:27 +0100318 mbedtls_psa_pake_abort(operation);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100319 return status;
320}
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200321
Neil Armstrong56b8d232022-06-01 18:05:57 +0200322static psa_status_t mbedtls_psa_pake_output_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100323 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100324 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 uint8_t *output,
326 size_t output_size,
327 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200328{
329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200330 size_t length;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100331 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100332
Neil Armstronga557cb82022-06-10 08:58:32 +0200333#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200334 /*
335 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
336 * handling of output sequencing.
337 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200338 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200339 * at once, on the other side the PSA CRYPTO PAKE api requires
340 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
341 * retrieved in sequence.
342 *
343 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
344 * data is stored in an intermediate buffer at first step output call,
345 * and data is sliced down by parsing the ECPoint records in order
346 * to return the right parts on each step.
347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200349 /* Initialize & write round on KEY_SHARE sequences */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100350 if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100351 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 operation->buffer,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100353 sizeof(operation->buffer),
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 &operation->buffer_length,
355 mbedtls_psa_get_random,
356 MBEDTLS_PSA_RANDOM_STATE);
357 if (ret != 0) {
358 return mbedtls_ecjpake_to_psa_error(ret);
359 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200360
361 operation->buffer_offset = 0;
Przemek Stekielb09c4872023-01-17 12:05:38 +0100362 } else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100363 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 operation->buffer,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100365 sizeof(operation->buffer),
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 &operation->buffer_length,
367 mbedtls_psa_get_random,
368 MBEDTLS_PSA_RANDOM_STATE);
369 if (ret != 0) {
370 return mbedtls_ecjpake_to_psa_error(ret);
371 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200372
373 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200374 }
375
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200376 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200377 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
378 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
379 * that the data for each step is prepended with a length byte, and
380 * then they're concatenated. Additionally, the server's second round
381 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200382 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200383 * In PSA, we output each step separately, and don't prepend the
384 * output with a length byte, even less a curve identifier, as that
385 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200386 */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100387 if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE &&
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100388 operation->role == MBEDTLS_ECJPAKE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200389 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
390 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200391 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200392
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200393 /* Read the length byte then move past it to the data */
394 length = operation->buffer[operation->buffer_offset];
395 operation->buffer_offset += 1;
396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (operation->buffer_offset + length > operation->buffer_length) {
398 return PSA_ERROR_DATA_CORRUPT;
399 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (output_size < length) {
402 return PSA_ERROR_BUFFER_TOO_SMALL;
403 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 memcpy(output,
406 operation->buffer + operation->buffer_offset,
407 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200408 *output_length = length;
409
410 operation->buffer_offset += length;
411
412 /* Reset buffer after ZK_PROOF sequence */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100413 if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) ||
414 (step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100415 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200416 operation->buffer_length = 0;
417 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return PSA_SUCCESS;
421 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200422#else
423 (void) step;
424 (void) output;
425 (void) output_size;
426 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200427#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200428 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200429}
430
Przemek Stekiel6c764412022-11-22 14:05:12 +0100431psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100432 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200433 uint8_t *output,
434 size_t output_size,
435 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200436{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200437 psa_status_t status = mbedtls_psa_pake_output_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100438 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200441}
442
Neil Armstrong56b8d232022-06-01 18:05:57 +0200443static psa_status_t mbedtls_psa_pake_input_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100444 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100445 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 const uint8_t *input,
447 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200448{
449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100450 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100451
Neil Armstronga557cb82022-06-10 08:58:32 +0200452#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200453 /*
454 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
455 * handling of input sequencing.
456 *
457 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
458 * at once as input, on the other side the PSA CRYPTO PAKE api requires
459 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
460 * given in sequence.
461 *
462 * In order to achieve API compatibility, each X1+X2 or X4S step data
463 * is stored sequentially in an intermediate buffer and given to the
464 * MbedTLS JPAKE API on the last step.
465 *
466 * This causes any input error to be only detected on the last step.
467 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (operation->alg == PSA_ALG_JPAKE) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200469 /*
470 * Copy input to local buffer and format it as the Mbed TLS API
471 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
472 * The summary is that the data for each step is prepended with a
473 * length byte, and then they're concatenated. Additionally, the
474 * server's second round output is prepended with a 3-bytes
475 * ECParameters structure - which means we have to prepend that when
476 * we're a client.
477 */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100478 if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE &&
Przemek Stekiele80ec0a2023-03-21 16:49:43 +0100479 operation->role == MBEDTLS_ECJPAKE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200480 /* We only support secp256r1. */
481 /* This is the ECParameters structure defined by RFC 8422. */
482 unsigned char ecparameters[3] = {
483 3, /* named_curve */
484 0, 23 /* secp256r1 */
485 };
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100486
Przemek Stekiel691e91a2023-03-07 16:26:37 +0100487 if (operation->buffer_length + sizeof(ecparameters) >
488 sizeof(operation->buffer)) {
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100489 return PSA_ERROR_BUFFER_TOO_SMALL;
490 }
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 memcpy(operation->buffer + operation->buffer_length,
493 ecparameters, sizeof(ecparameters));
494 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200495 }
496
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100497 /*
Przemek Stekiel691e91a2023-03-07 16:26:37 +0100498 * The core checks that input_length is smaller than
499 * PSA_PAKE_INPUT_MAX_SIZE.
500 * Thus no risk of integer overflow here.
Przemek Stekiel4dc83d42023-02-27 11:49:35 +0100501 */
502 if (operation->buffer_length + input_length + 1 > sizeof(operation->buffer)) {
503 return PSA_ERROR_BUFFER_TOO_SMALL;
504 }
505
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200506 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200507 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200508 operation->buffer_length += 1;
509
510 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 memcpy(operation->buffer + operation->buffer_length,
512 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200513 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200514
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200515 /* Load buffer at each last round ZK_PROOF */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100516 if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100517 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 operation->buffer,
519 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200520
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100521 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200522 operation->buffer_length = 0;
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (ret != 0) {
525 return mbedtls_ecjpake_to_psa_error(ret);
526 }
Przemek Stekielb09c4872023-01-17 12:05:38 +0100527 } else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100528 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.jpake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 operation->buffer,
530 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200531
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100532 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200533 operation->buffer_length = 0;
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (ret != 0) {
536 return mbedtls_ecjpake_to_psa_error(ret);
537 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return PSA_SUCCESS;
541 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200542#else
543 (void) step;
544 (void) input;
545 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200546#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200547 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200548}
549
Przemek Stekiel6c764412022-11-22 14:05:12 +0100550psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100551 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200552 const uint8_t *input,
553 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200554{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200555 psa_status_t status = mbedtls_psa_pake_input_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100556 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200559}
560
Neil Armstrong56b8d232022-06-01 18:05:57 +0200561psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100562 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100563 uint8_t *output, size_t output_size,
564 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200565{
566 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200567
Neil Armstronga557cb82022-06-10 08:58:32 +0200568#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100570 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.jpake,
571 output,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100572 output_size,
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100573 output_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_psa_get_random,
575 MBEDTLS_PSA_RANDOM_STATE);
576 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200578 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200579
Przemek Stekiel0c781802022-11-29 14:53:13 +0100580 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200582#else
583 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200584#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100585 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200586}
587
Przemek Stekiel6c764412022-11-22 14:05:12 +0100588psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200589{
Przemek Stekielce131bf2023-02-21 12:19:27 +0100590 mbedtls_platform_zeroize(operation->password, operation->password_len);
591 mbedtls_free(operation->password);
592 operation->password = NULL;
593 operation->password_len = 0;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100594
Przemek Stekielce131bf2023-02-21 12:19:27 +0100595#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6e628a42023-04-25 13:11:36 +0200597 operation->role = MBEDTLS_ECJPAKE_NONE;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100598 mbedtls_platform_zeroize(operation->buffer, sizeof(operation->buffer));
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200599 operation->buffer_length = 0;
600 operation->buffer_offset = 0;
Przemek Stekiele3ef3a12023-02-27 10:20:06 +0100601 mbedtls_ecjpake_free(&operation->ctx.jpake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200602 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200603#endif
604
Neil Armstrongcb679f22022-09-13 14:43:07 +0200605 operation->alg = PSA_ALG_NONE;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200608}
609
610#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
611
612#endif /* MBEDTLS_PSA_CRYPTO_C */