blob: 3a710dc60e5b43e33f3204b7a87474f596473f42 [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
Gilles Peskine449bd832023-01-11 14:50:10 +010082enum psa_pake_step {
Neil Armstronga4cc7d62022-05-25 11:30:48 +020083 PSA_PAKE_STEP_INVALID = 0,
84 PSA_PAKE_STEP_X1_X2 = 1,
85 PSA_PAKE_STEP_X2S = 2,
86 PSA_PAKE_STEP_DERIVE = 3,
87};
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089enum psa_pake_state {
Neil Armstronga4cc7d62022-05-25 11:30:48 +020090 PSA_PAKE_STATE_INVALID = 0,
91 PSA_PAKE_STATE_SETUP = 1,
92 PSA_PAKE_STATE_READY = 2,
93 PSA_PAKE_OUTPUT_X1_X2 = 3,
94 PSA_PAKE_OUTPUT_X2S = 4,
95 PSA_PAKE_INPUT_X1_X2 = 5,
96 PSA_PAKE_INPUT_X4S = 6,
97};
98
Neil Armstrongbcd5bd92022-09-05 18:33:23 +020099/*
100 * The first PAKE step shares the same sequences of the second PAKE step
101 * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
Neil Armstrongb39833c2022-09-06 11:36:02 +0200102 * It's simpler to share the same sequences numbers of the first
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200103 * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
104 *
105 * State sequence with step, state & sequence enums:
106 * => Input & Output Step = PSA_PAKE_STEP_INVALID
107 * => state = PSA_PAKE_STATE_INVALID
108 * psa_pake_setup()
109 * => Input & Output Step = PSA_PAKE_STEP_X1_X2
110 * => state = PSA_PAKE_STATE_SETUP
111 * => sequence = PSA_PAKE_SEQ_INVALID
112 * |
113 * |--- In any order: (First round input before or after first round output)
114 * | | First call of psa_pake_output() or psa_pake_input() sets
115 * | | state = PSA_PAKE_STATE_READY
116 * | |
117 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
118 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
119 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
120 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
121 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
122 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
123 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
124 * | | | => state = PSA_PAKE_STATE_READY
125 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200126 * | | | => Output Step = PSA_PAKE_STEP_X2S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200127 * | |
128 * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
129 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
130 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
131 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
132 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
133 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
134 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
135 * | | | => state = PSA_PAKE_STATE_READY
136 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200137 * | | | => Output Step = PSA_PAKE_INPUT_X4S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200138 * |
139 * |--- In any order: (Second round input before or after second round output)
140 * | |
141 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
142 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
143 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
144 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
145 * | | | => state = PSA_PAKE_STATE_READY
146 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200147 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200148 * | |
149 * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
150 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
151 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
152 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
153 * | | | => state = PSA_PAKE_STATE_READY
154 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200155 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200156 * |
157 * psa_pake_get_implicit_key()
158 * => Input & Output Step = PSA_PAKE_STEP_INVALID
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160enum psa_pake_sequence {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200161 PSA_PAKE_SEQ_INVALID = 0,
162 PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
163 PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
164 PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
165 PSA_PAKE_X2_STEP_KEY_SHARE = 4,
166 PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
167 PSA_PAKE_X2_STEP_ZK_PROOF = 6,
168 PSA_PAKE_SEQ_END = 7,
169};
170
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200171#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200173{
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 switch (ret) {
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200175 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
176 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
177 case MBEDTLS_ERR_ECP_INVALID_KEY:
178 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_DATA_INVALID;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200180 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
181 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_BUFFER_TOO_SMALL;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200183 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200185 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200187 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_GENERIC_ERROR;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200189 }
190}
191#endif
192
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200193#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100194psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel51eac532022-12-07 11:04:51 +0100195 const psa_crypto_driver_pake_inputs_t *inputs)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200196{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
198
Przemek Stekiel51eac532022-12-07 11:04:51 +0100199 uint8_t *password = inputs->password;
200 size_t password_len = inputs->password_len;
201 psa_pake_role_t role = inputs->role;
202 psa_pake_cipher_suite_t cipher_suite = inputs->cipher_suite;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100203
Przemek Stekiel51eac532022-12-07 11:04:51 +0100204 memset(operation, 0, sizeof(mbedtls_psa_pake_operation_t));
Przemek Stekiel6c764412022-11-22 14:05:12 +0100205
Neil Armstronga557cb82022-06-10 08:58:32 +0200206#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel51eac532022-12-07 11:04:51 +0100207 if (cipher_suite.algorithm == PSA_ALG_JPAKE) {
208 if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
209 cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 ||
210 cipher_suite.bits != 256 ||
211 cipher_suite.hash != PSA_ALG_SHA_256) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100212 status = PSA_ERROR_NOT_SUPPORTED;
213 goto error;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200214 }
215
Przemek Stekiel51eac532022-12-07 11:04:51 +0100216 if (role != PSA_PAKE_ROLE_CLIENT &&
217 role != PSA_PAKE_ROLE_SERVER) {
218 status = PSA_ERROR_NOT_SUPPORTED;
219 goto error;
220 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200221
Przemek Stekiel6c764412022-11-22 14:05:12 +0100222 mbedtls_ecjpake_init(&operation->ctx.pake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200223
224 operation->state = PSA_PAKE_STATE_SETUP;
225 operation->sequence = PSA_PAKE_SEQ_INVALID;
226 operation->input_step = PSA_PAKE_STEP_X1_X2;
227 operation->output_step = PSA_PAKE_STEP_X1_X2;
Przemek Stekiel51eac532022-12-07 11:04:51 +0100228 operation->password_len = password_len;
229 operation->password = password;
230 operation->role = role;
231 operation->alg = cipher_suite.algorithm;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200234 operation->buffer_length = 0;
235 operation->buffer_offset = 0;
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_SUCCESS;
238 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200239#else
240 (void) operation;
Przemek Stekiel51eac532022-12-07 11:04:51 +0100241 (void) inputs;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200242#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200243 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100244
245error:
Przemek Stekiel51eac532022-12-07 11:04:51 +0100246 mbedtls_free(password);
Neil Armstrong5ae60962022-09-15 11:29:46 +0200247 mbedtls_psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100248 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200249}
250
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200251
Neil Armstronga557cb82022-06-10 08:58:32 +0200252#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100253static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200254{
255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200256 mbedtls_ecjpake_role role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (operation->role == PSA_PAKE_ROLE_CLIENT) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200259 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 } else if (operation->role == PSA_PAKE_ROLE_SERVER) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200261 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 } else {
263 return PSA_ERROR_BAD_STATE;
264 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (operation->password_len == 0) {
267 return PSA_ERROR_BAD_STATE;
268 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200269
Przemek Stekiel6c764412022-11-22 14:05:12 +0100270 ret = mbedtls_ecjpake_setup(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 role,
272 MBEDTLS_MD_SHA256,
273 MBEDTLS_ECP_DP_SECP256R1,
274 operation->password,
275 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_platform_zeroize(operation->password, operation->password_len);
278 mbedtls_free(operation->password);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100279 operation->password = NULL;
280 operation->password_len = 0;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (ret != 0) {
283 return mbedtls_ecjpake_to_psa_error(ret);
284 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100285
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200286 operation->state = PSA_PAKE_STATE_READY;
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200289}
290#endif
291
Neil Armstrong56b8d232022-06-01 18:05:57 +0200292static psa_status_t mbedtls_psa_pake_output_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100293 mbedtls_psa_pake_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 psa_pake_step_t step,
295 uint8_t *output,
296 size_t output_size,
297 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200298{
299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
301 size_t length;
302
Przemek Stekiel6c764412022-11-22 14:05:12 +0100303 if (operation->alg == PSA_ALG_NONE) {
304 return PSA_ERROR_BAD_STATE;
305 }
306
Neil Armstrong5ae60962022-09-15 11:29:46 +0200307 if (operation->state == PSA_PAKE_STATE_INVALID) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return PSA_ERROR_BAD_STATE;
309 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200310
Neil Armstronga557cb82022-06-10 08:58:32 +0200311#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200312 /*
313 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
314 * handling of output sequencing.
315 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200316 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200317 * at once, on the other side the PSA CRYPTO PAKE api requires
318 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
319 * retrieved in sequence.
320 *
321 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
322 * data is stored in an intermediate buffer at first step output call,
323 * and data is sliced down by parsing the ECPoint records in order
324 * to return the right parts on each step.
325 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (operation->alg == PSA_ALG_JPAKE) {
327 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200328 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 step != PSA_PAKE_STEP_ZK_PROOF) {
330 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200331 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if (operation->state == PSA_PAKE_STATE_SETUP) {
334 status = psa_pake_ecjpake_setup(operation);
335 if (status != PSA_SUCCESS) {
336 return status;
337 }
338 }
339
340 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200341 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 operation->state != PSA_PAKE_OUTPUT_X2S) {
343 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (operation->state == PSA_PAKE_STATE_READY) {
347 if (step != PSA_PAKE_STEP_KEY_SHARE) {
348 return PSA_ERROR_BAD_STATE;
349 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 switch (operation->output_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200352 case PSA_PAKE_STEP_X1_X2:
353 operation->state = PSA_PAKE_OUTPUT_X1_X2;
354 break;
355 case PSA_PAKE_STEP_X2S:
356 operation->state = PSA_PAKE_OUTPUT_X2S;
357 break;
358 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200360 }
361
362 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
363 }
364
365 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200367 case PSA_PAKE_X1_STEP_KEY_SHARE:
368 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (step != PSA_PAKE_STEP_KEY_SHARE) {
370 return PSA_ERROR_BAD_STATE;
371 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200372 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200373
374 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
375 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
377 return PSA_ERROR_BAD_STATE;
378 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200379 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200380
381 case PSA_PAKE_X1_STEP_ZK_PROOF:
382 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (step != PSA_PAKE_STEP_ZK_PROOF) {
384 return PSA_ERROR_BAD_STATE;
385 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200386 break;
387
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200388 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200390 }
391
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200392 /* Initialize & write round on KEY_SHARE sequences */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
394 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100395 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 operation->buffer,
397 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
398 &operation->buffer_length,
399 mbedtls_psa_get_random,
400 MBEDTLS_PSA_RANDOM_STATE);
401 if (ret != 0) {
402 return mbedtls_ecjpake_to_psa_error(ret);
403 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200404
405 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 } else if (operation->state == PSA_PAKE_OUTPUT_X2S &&
407 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100408 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 operation->buffer,
410 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
411 &operation->buffer_length,
412 mbedtls_psa_get_random,
413 MBEDTLS_PSA_RANDOM_STATE);
414 if (ret != 0) {
415 return mbedtls_ecjpake_to_psa_error(ret);
416 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200417
418 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200419 }
420
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200421 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200422 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
423 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
424 * that the data for each step is prepended with a length byte, and
425 * then they're concatenated. Additionally, the server's second round
426 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200427 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200428 * In PSA, we output each step separately, and don't prepend the
429 * output with a length byte, even less a curve identifier, as that
430 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200431 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (operation->state == PSA_PAKE_OUTPUT_X2S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200433 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 operation->role == PSA_PAKE_ROLE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200435 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
436 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200437 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200438
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200439 /* Read the length byte then move past it to the data */
440 length = operation->buffer[operation->buffer_offset];
441 operation->buffer_offset += 1;
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (operation->buffer_offset + length > operation->buffer_length) {
444 return PSA_ERROR_DATA_CORRUPT;
445 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (output_size < length) {
448 return PSA_ERROR_BUFFER_TOO_SMALL;
449 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 memcpy(output,
452 operation->buffer + operation->buffer_offset,
453 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200454 *output_length = length;
455
456 operation->buffer_offset += length;
457
458 /* Reset buffer after ZK_PROOF sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if ((operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
460 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
461 (operation->state == PSA_PAKE_OUTPUT_X2S &&
462 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
463 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200464 operation->buffer_length = 0;
465 operation->buffer_offset = 0;
466
467 operation->state = PSA_PAKE_STATE_READY;
468 operation->output_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200469 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200471 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return PSA_SUCCESS;
475 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200476#else
477 (void) step;
478 (void) output;
479 (void) output_size;
480 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200481#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200482 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200483}
484
Przemek Stekiel6c764412022-11-22 14:05:12 +0100485psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200486 psa_pake_step_t step,
487 uint8_t *output,
488 size_t output_size,
489 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200490{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200491 psa_status_t status = mbedtls_psa_pake_output_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (status != PSA_SUCCESS) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100495 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200499}
500
Neil Armstrong56b8d232022-06-01 18:05:57 +0200501static psa_status_t mbedtls_psa_pake_input_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100502 mbedtls_psa_pake_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 psa_pake_step_t step,
504 const uint8_t *input,
505 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200506{
507 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
508 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200509
Przemek Stekiel6c764412022-11-22 14:05:12 +0100510 if (operation->alg == PSA_ALG_NONE) {
511 return PSA_ERROR_BAD_STATE;
512 }
513
Neil Armstrong5ae60962022-09-15 11:29:46 +0200514 if (operation->state == PSA_PAKE_STATE_INVALID) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return PSA_ERROR_BAD_STATE;
516 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200517
Neil Armstronga557cb82022-06-10 08:58:32 +0200518#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200519 /*
520 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
521 * handling of input sequencing.
522 *
523 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
524 * at once as input, on the other side the PSA CRYPTO PAKE api requires
525 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
526 * given in sequence.
527 *
528 * In order to achieve API compatibility, each X1+X2 or X4S step data
529 * is stored sequentially in an intermediate buffer and given to the
530 * MbedTLS JPAKE API on the last step.
531 *
532 * This causes any input error to be only detected on the last step.
533 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (operation->alg == PSA_ALG_JPAKE) {
535 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200536 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 step != PSA_PAKE_STEP_ZK_PROOF) {
538 return PSA_ERROR_INVALID_ARGUMENT;
539 }
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200540
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200541 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256);
543 if (input_length > (size_t) PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, prim, step)) {
544 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200545 }
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (operation->state == PSA_PAKE_STATE_SETUP) {
548 status = psa_pake_ecjpake_setup(operation);
549 if (status != PSA_SUCCESS) {
550 return status;
551 }
552 }
553
554 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200555 operation->state != PSA_PAKE_INPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 operation->state != PSA_PAKE_INPUT_X4S) {
557 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200558 }
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (operation->state == PSA_PAKE_STATE_READY) {
561 if (step != PSA_PAKE_STEP_KEY_SHARE) {
562 return PSA_ERROR_BAD_STATE;
563 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 switch (operation->input_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200566 case PSA_PAKE_STEP_X1_X2:
567 operation->state = PSA_PAKE_INPUT_X1_X2;
568 break;
569 case PSA_PAKE_STEP_X2S:
570 operation->state = PSA_PAKE_INPUT_X4S;
571 break;
572 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200574 }
575
576 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
577 }
578
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200579 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200581 case PSA_PAKE_X1_STEP_KEY_SHARE:
582 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (step != PSA_PAKE_STEP_KEY_SHARE) {
584 return PSA_ERROR_BAD_STATE;
585 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200586 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200587
588 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
589 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
591 return PSA_ERROR_BAD_STATE;
592 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200593 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200594
595 case PSA_PAKE_X1_STEP_ZK_PROOF:
596 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (step != PSA_PAKE_STEP_ZK_PROOF) {
598 return PSA_ERROR_BAD_STATE;
599 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200600 break;
601
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200602 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200604 }
605
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200606 /*
607 * Copy input to local buffer and format it as the Mbed TLS API
608 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
609 * The summary is that the data for each step is prepended with a
610 * length byte, and then they're concatenated. Additionally, the
611 * server's second round output is prepended with a 3-bytes
612 * ECParameters structure - which means we have to prepend that when
613 * we're a client.
614 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (operation->state == PSA_PAKE_INPUT_X4S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200616 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 operation->role == PSA_PAKE_ROLE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200618 /* We only support secp256r1. */
619 /* This is the ECParameters structure defined by RFC 8422. */
620 unsigned char ecparameters[3] = {
621 3, /* named_curve */
622 0, 23 /* secp256r1 */
623 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 memcpy(operation->buffer + operation->buffer_length,
625 ecparameters, sizeof(ecparameters));
626 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200627 }
628
629 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200630 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200631 operation->buffer_length += 1;
632
633 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 memcpy(operation->buffer + operation->buffer_length,
635 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200636 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200637
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200638 /* Load buffer at each last round ZK_PROOF */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (operation->state == PSA_PAKE_INPUT_X1_X2 &&
640 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100641 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 operation->buffer,
643 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200646 operation->buffer_length = 0;
647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 if (ret != 0) {
649 return mbedtls_ecjpake_to_psa_error(ret);
650 }
651 } else if (operation->state == PSA_PAKE_INPUT_X4S &&
652 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100653 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 operation->buffer,
655 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200658 operation->buffer_length = 0;
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (ret != 0) {
661 return mbedtls_ecjpake_to_psa_error(ret);
662 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200663 }
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if ((operation->state == PSA_PAKE_INPUT_X1_X2 &&
666 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
667 (operation->state == PSA_PAKE_INPUT_X4S &&
668 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200669 operation->state = PSA_PAKE_STATE_READY;
670 operation->input_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200671 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200673 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return PSA_SUCCESS;
677 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200678#else
679 (void) step;
680 (void) input;
681 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200682#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200683 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200684}
685
Przemek Stekiel6c764412022-11-22 14:05:12 +0100686psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200687 psa_pake_step_t step,
688 const uint8_t *input,
689 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200690{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200691 psa_status_t status = mbedtls_psa_pake_input_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (status != PSA_SUCCESS) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100695 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200699}
700
Neil Armstrong56b8d232022-06-01 18:05:57 +0200701psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100702 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel0c781802022-11-29 14:53:13 +0100703 uint8_t *output, size_t *output_size)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200704{
705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
706 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
707
Przemek Stekiel6c764412022-11-22 14:05:12 +0100708 if (operation->alg == PSA_ALG_NONE) {
709 return PSA_ERROR_BAD_STATE;
710 }
711
Neil Armstrong5ae60962022-09-15 11:29:46 +0200712 if (operation->input_step != PSA_PAKE_STEP_DERIVE ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 operation->output_step != PSA_PAKE_STEP_DERIVE) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100714 status = PSA_ERROR_BAD_STATE;
715 goto error;
716 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200717
Neil Armstronga557cb82022-06-10 08:58:32 +0200718#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100720 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 operation->buffer,
722 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
723 &operation->buffer_length,
724 mbedtls_psa_get_random,
725 MBEDTLS_PSA_RANDOM_STATE);
726 if (ret != 0) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100727 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200729 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200730
Przemek Stekiel0c781802022-11-29 14:53:13 +0100731 memcpy(output, operation->buffer, operation->buffer_length);
732 *output_size = operation->buffer_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200735
Przemek Stekiel6c764412022-11-22 14:05:12 +0100736 mbedtls_psa_pake_abort(operation);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200737
Przemek Stekiel0c781802022-11-29 14:53:13 +0100738 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200740#else
741 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200742#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200743 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100744
745error:
Przemek Stekiel6c764412022-11-22 14:05:12 +0100746 mbedtls_psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200749}
750
Przemek Stekiel6c764412022-11-22 14:05:12 +0100751psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200752{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100753 if (operation->alg == PSA_ALG_NONE) {
754 return PSA_SUCCESS;
755 }
756
Neil Armstronga557cb82022-06-10 08:58:32 +0200757#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrongcb679f22022-09-13 14:43:07 +0200760 operation->input_step = PSA_PAKE_STEP_INVALID;
761 operation->output_step = PSA_PAKE_STEP_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (operation->password_len > 0) {
763 mbedtls_platform_zeroize(operation->password, operation->password_len);
764 }
765 mbedtls_free(operation->password);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100766 operation->password = NULL;
767 operation->password_len = 0;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200768 operation->role = PSA_PAKE_ROLE_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200770 operation->buffer_length = 0;
771 operation->buffer_offset = 0;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100772 mbedtls_ecjpake_free(&operation->ctx.pake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200773 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200774#endif
775
Neil Armstrongcb679f22022-09-13 14:43:07 +0200776 operation->alg = PSA_ALG_NONE;
777 operation->state = PSA_PAKE_STATE_INVALID;
778 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200781}
782
783#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
784
785#endif /* MBEDTLS_PSA_CRYPTO_C */