blob: 3d5b57d29ec1115e54e658f6ebd1f4de378daebe [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
Neil Armstronga557cb82022-06-10 08:58:32 +0200166#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100167static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200168{
169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200170 mbedtls_ecjpake_role role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200171
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 if (operation->role == PSA_PAKE_ROLE_CLIENT) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200173 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 } else if (operation->role == PSA_PAKE_ROLE_SERVER) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200175 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 } else {
177 return PSA_ERROR_BAD_STATE;
178 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (operation->password_len == 0) {
181 return PSA_ERROR_BAD_STATE;
182 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200183
Przemek Stekiel6c764412022-11-22 14:05:12 +0100184 ret = mbedtls_ecjpake_setup(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 role,
186 MBEDTLS_MD_SHA256,
187 MBEDTLS_ECP_DP_SECP256R1,
188 operation->password,
189 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 mbedtls_platform_zeroize(operation->password, operation->password_len);
192 mbedtls_free(operation->password);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100193 operation->password = NULL;
194 operation->password_len = 0;
195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (ret != 0) {
197 return mbedtls_ecjpake_to_psa_error(ret);
198 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200201}
Przemek Stekiele12ed362022-12-21 12:54:46 +0100202
203psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
204 const psa_crypto_driver_pake_inputs_t *inputs)
205{
206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
207
208 uint8_t *password = inputs->password;
209 size_t password_len = inputs->password_len;
210 psa_pake_role_t role = inputs->role;
211 psa_pake_cipher_suite_t cipher_suite = inputs->cipher_suite;
212
213 memset(operation, 0, sizeof(mbedtls_psa_pake_operation_t));
214
215#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
216 if (cipher_suite.algorithm == PSA_ALG_JPAKE) {
217 if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
218 cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 ||
219 cipher_suite.bits != 256 ||
220 cipher_suite.hash != PSA_ALG_SHA_256) {
221 status = PSA_ERROR_NOT_SUPPORTED;
222 goto error;
223 }
224
225 if (role != PSA_PAKE_ROLE_CLIENT &&
226 role != PSA_PAKE_ROLE_SERVER) {
227 status = PSA_ERROR_NOT_SUPPORTED;
228 goto error;
229 }
230
231 mbedtls_ecjpake_init(&operation->ctx.pake);
232
233 operation->password_len = password_len;
234 operation->password = password;
235 operation->role = role;
236 operation->alg = cipher_suite.algorithm;
237
238 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
239 operation->buffer_length = 0;
240 operation->buffer_offset = 0;
241
242 status = psa_pake_ecjpake_setup(operation);
243
244 if (status != PSA_SUCCESS) {
245 goto error;
246 }
247
248 return PSA_SUCCESS;
249 } else
250#else
251 (void) operation;
252 (void) inputs;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200253#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100254 { status = PSA_ERROR_NOT_SUPPORTED; }
255
256error:
257 mbedtls_free(password);
258 mbedtls_psa_pake_abort(operation);
259 return status;
260}
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200261
Neil Armstrong56b8d232022-06-01 18:05:57 +0200262static psa_status_t mbedtls_psa_pake_output_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100263 mbedtls_psa_pake_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 psa_pake_step_t step,
Przemek Stekiele12ed362022-12-21 12:54:46 +0100265 const psa_pake_computation_stage_t *computation_stage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 uint8_t *output,
267 size_t output_size,
268 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200269{
270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200271 size_t length;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100272 (void) step;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200273
Przemek Stekiel6c764412022-11-22 14:05:12 +0100274 if (operation->alg == PSA_ALG_NONE) {
275 return PSA_ERROR_BAD_STATE;
276 }
277
Neil Armstronga557cb82022-06-10 08:58:32 +0200278#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200279 /*
280 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
281 * handling of output sequencing.
282 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200283 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200284 * at once, on the other side the PSA CRYPTO PAKE api requires
285 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
286 * retrieved in sequence.
287 *
288 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
289 * data is stored in an intermediate buffer at first step output call,
290 * and data is sliced down by parsing the ECPoint records in order
291 * to return the right parts on each step.
292 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +0100294 const psa_jpake_computation_stage_t *jpake_computation_stage =
295 &computation_stage->data.jpake_computation_stage;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200296
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200297 /* Initialize & write round on KEY_SHARE sequences */
Przemek Stekiele12ed362022-12-21 12:54:46 +0100298 if (jpake_computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
299 jpake_computation_stage->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100300 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 operation->buffer,
302 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
303 &operation->buffer_length,
304 mbedtls_psa_get_random,
305 MBEDTLS_PSA_RANDOM_STATE);
306 if (ret != 0) {
307 return mbedtls_ecjpake_to_psa_error(ret);
308 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200309
310 operation->buffer_offset = 0;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100311 } else if (jpake_computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
312 jpake_computation_stage->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100313 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 operation->buffer,
315 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
316 &operation->buffer_length,
317 mbedtls_psa_get_random,
318 MBEDTLS_PSA_RANDOM_STATE);
319 if (ret != 0) {
320 return mbedtls_ecjpake_to_psa_error(ret);
321 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200322
323 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200324 }
325
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200326 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200327 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
328 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
329 * that the data for each step is prepended with a length byte, and
330 * then they're concatenated. Additionally, the server's second round
331 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200332 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200333 * In PSA, we output each step separately, and don't prepend the
334 * output with a length byte, even less a curve identifier, as that
335 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200336 */
Przemek Stekiele12ed362022-12-21 12:54:46 +0100337 if (jpake_computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
338 jpake_computation_stage->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 operation->role == PSA_PAKE_ROLE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200340 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
341 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200342 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200343
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200344 /* Read the length byte then move past it to the data */
345 length = operation->buffer[operation->buffer_offset];
346 operation->buffer_offset += 1;
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (operation->buffer_offset + length > operation->buffer_length) {
349 return PSA_ERROR_DATA_CORRUPT;
350 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (output_size < length) {
353 return PSA_ERROR_BUFFER_TOO_SMALL;
354 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 memcpy(output,
357 operation->buffer + operation->buffer_offset,
358 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200359 *output_length = length;
360
361 operation->buffer_offset += length;
362
363 /* Reset buffer after ZK_PROOF sequence */
Przemek Stekiele12ed362022-12-21 12:54:46 +0100364 if ((jpake_computation_stage->state == PSA_PAKE_OUTPUT_X1_X2 &&
365 jpake_computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
366 (jpake_computation_stage->state == PSA_PAKE_OUTPUT_X2S &&
367 jpake_computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200369 operation->buffer_length = 0;
370 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 return PSA_SUCCESS;
374 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200375#else
376 (void) step;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100377 (void) computation_stage;
Neil Armstrong5ae60962022-09-15 11:29:46 +0200378 (void) output;
379 (void) output_size;
380 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200381#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200382 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200383}
384
Przemek Stekiel6c764412022-11-22 14:05:12 +0100385psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200386 psa_pake_step_t step,
Przemek Stekiele12ed362022-12-21 12:54:46 +0100387 const psa_pake_computation_stage_t *computation_stage,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200388 uint8_t *output,
389 size_t output_size,
390 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200391{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200392 psa_status_t status = mbedtls_psa_pake_output_internal(
Przemek Stekiele12ed362022-12-21 12:54:46 +0100393 operation, step, computation_stage, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (status != PSA_SUCCESS) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100396 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200400}
401
Neil Armstrong56b8d232022-06-01 18:05:57 +0200402static psa_status_t mbedtls_psa_pake_input_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100403 mbedtls_psa_pake_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 psa_pake_step_t step,
Przemek Stekiele12ed362022-12-21 12:54:46 +0100405 const psa_pake_computation_stage_t *computation_stage,
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 const uint8_t *input,
407 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200408{
409 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100410 (void) step;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100411 if (operation->alg == PSA_ALG_NONE) {
412 return PSA_ERROR_BAD_STATE;
413 }
414
Neil Armstronga557cb82022-06-10 08:58:32 +0200415#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200416 /*
417 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
418 * handling of input sequencing.
419 *
420 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
421 * at once as input, on the other side the PSA CRYPTO PAKE api requires
422 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
423 * given in sequence.
424 *
425 * In order to achieve API compatibility, each X1+X2 or X4S step data
426 * is stored sequentially in an intermediate buffer and given to the
427 * MbedTLS JPAKE API on the last step.
428 *
429 * This causes any input error to be only detected on the last step.
430 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiele12ed362022-12-21 12:54:46 +0100432 const psa_jpake_computation_stage_t *jpake_computation_stage =
433 &computation_stage->data.jpake_computation_stage;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200434 /*
435 * Copy input to local buffer and format it as the Mbed TLS API
436 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
437 * The summary is that the data for each step is prepended with a
438 * length byte, and then they're concatenated. Additionally, the
439 * server's second round output is prepended with a 3-bytes
440 * ECParameters structure - which means we have to prepend that when
441 * we're a client.
442 */
Przemek Stekiele12ed362022-12-21 12:54:46 +0100443 if (jpake_computation_stage->state == PSA_PAKE_INPUT_X4S &&
444 jpake_computation_stage->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 operation->role == PSA_PAKE_ROLE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200446 /* We only support secp256r1. */
447 /* This is the ECParameters structure defined by RFC 8422. */
448 unsigned char ecparameters[3] = {
449 3, /* named_curve */
450 0, 23 /* secp256r1 */
451 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 memcpy(operation->buffer + operation->buffer_length,
453 ecparameters, sizeof(ecparameters));
454 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200455 }
456
457 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200458 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200459 operation->buffer_length += 1;
460
461 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 memcpy(operation->buffer + operation->buffer_length,
463 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200464 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200465
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200466 /* Load buffer at each last round ZK_PROOF */
Przemek Stekiele12ed362022-12-21 12:54:46 +0100467 if (jpake_computation_stage->state == PSA_PAKE_INPUT_X1_X2 &&
468 jpake_computation_stage->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100469 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 operation->buffer,
471 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200474 operation->buffer_length = 0;
475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (ret != 0) {
477 return mbedtls_ecjpake_to_psa_error(ret);
478 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100479 } else if (jpake_computation_stage->state == PSA_PAKE_INPUT_X4S &&
480 jpake_computation_stage->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100481 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 operation->buffer,
483 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200486 operation->buffer_length = 0;
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (ret != 0) {
489 return mbedtls_ecjpake_to_psa_error(ret);
490 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return PSA_SUCCESS;
494 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200495#else
496 (void) step;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100497 (void) computation_stage;
Neil Armstrong5ae60962022-09-15 11:29:46 +0200498 (void) input;
499 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200500#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200501 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200502}
503
Przemek Stekiel6c764412022-11-22 14:05:12 +0100504psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200505 psa_pake_step_t step,
Przemek Stekiele12ed362022-12-21 12:54:46 +0100506 const psa_pake_computation_stage_t *computation_stage,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200507 const uint8_t *input,
508 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200509{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200510 psa_status_t status = mbedtls_psa_pake_input_internal(
Przemek Stekiele12ed362022-12-21 12:54:46 +0100511 operation, step, computation_stage, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 if (status != PSA_SUCCESS) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100514 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200518}
519
Neil Armstrong56b8d232022-06-01 18:05:57 +0200520psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100521 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel0c781802022-11-29 14:53:13 +0100522 uint8_t *output, size_t *output_size)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200523{
524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200525
Przemek Stekiel6c764412022-11-22 14:05:12 +0100526 if (operation->alg == PSA_ALG_NONE) {
527 return PSA_ERROR_BAD_STATE;
528 }
529
Neil Armstronga557cb82022-06-10 08:58:32 +0200530#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100532 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 operation->buffer,
534 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
535 &operation->buffer_length,
536 mbedtls_psa_get_random,
537 MBEDTLS_PSA_RANDOM_STATE);
538 if (ret != 0) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100539 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200541 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200542
Przemek Stekiel0c781802022-11-29 14:53:13 +0100543 memcpy(output, operation->buffer, operation->buffer_length);
544 *output_size = operation->buffer_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200547
Przemek Stekiel6c764412022-11-22 14:05:12 +0100548 mbedtls_psa_pake_abort(operation);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200549
Przemek Stekiel0c781802022-11-29 14:53:13 +0100550 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200552#else
553 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200554#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100555 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200556}
557
Przemek Stekiel6c764412022-11-22 14:05:12 +0100558psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200559{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100560 if (operation->alg == PSA_ALG_NONE) {
561 return PSA_SUCCESS;
562 }
563
Neil Armstronga557cb82022-06-10 08:58:32 +0200564#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if (operation->alg == PSA_ALG_JPAKE) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (operation->password_len > 0) {
568 mbedtls_platform_zeroize(operation->password, operation->password_len);
569 }
570 mbedtls_free(operation->password);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100571 operation->password = NULL;
572 operation->password_len = 0;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200573 operation->role = PSA_PAKE_ROLE_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200575 operation->buffer_length = 0;
576 operation->buffer_offset = 0;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100577 mbedtls_ecjpake_free(&operation->ctx.pake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200578 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200579#endif
580
Neil Armstrongcb679f22022-09-13 14:43:07 +0200581 operation->alg = PSA_ALG_NONE;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200584}
585
586#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
587
588#endif /* MBEDTLS_PSA_CRYPTO_C */