blob: 541e6c44b68a4a38f97a6b254702b0747fc4a631 [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"
27#include "psa_crypto_slot_management.h"
28
29#include <mbedtls/ecjpake.h>
30#include <mbedtls/psa_util.h>
31
32#include <mbedtls/platform.h>
33#include <mbedtls/error.h>
34#include <string.h>
35
Neil Armstronga4cc7d62022-05-25 11:30:48 +020036/*
37 * State sequence:
38 *
39 * psa_pake_setup()
40 * |
41 * |-- In any order:
42 * | | psa_pake_set_password_key()
43 * | | psa_pake_set_user()
44 * | | psa_pake_set_peer()
Neil Armstrongc29f8472022-06-08 13:34:49 +020045 * | | psa_pake_set_role()
Neil Armstronga4cc7d62022-05-25 11:30:48 +020046 * |
47 * |--- In any order: (First round input before or after first round output)
48 * | |
49 * | |------ In Order
50 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
51 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
52 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
53 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
54 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
55 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
56 * | |
57 * | |------ In Order:
58 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
59 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
60 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
61 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
62 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
63 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
64 * |
65 * |--- In any order: (Second round input before or after second round output)
66 * | |
67 * | |------ In Order
68 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
69 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
70 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
71 * | |
72 * | |------ In Order:
73 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
74 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
75 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
76 * |
77 * psa_pake_get_implicit_key()
78 * psa_pake_abort()
79 */
80
81enum psa_pake_step
82{
83 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
89enum psa_pake_state
90{
91 PSA_PAKE_STATE_INVALID = 0,
92 PSA_PAKE_STATE_SETUP = 1,
93 PSA_PAKE_STATE_READY = 2,
94 PSA_PAKE_OUTPUT_X1_X2 = 3,
95 PSA_PAKE_OUTPUT_X2S = 4,
96 PSA_PAKE_INPUT_X1_X2 = 5,
97 PSA_PAKE_INPUT_X4S = 6,
98};
99
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200100/*
101 * The first PAKE step shares the same sequences of the second PAKE step
102 * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
Neil Armstrongb39833c2022-09-06 11:36:02 +0200103 * It's simpler to share the same sequences numbers of the first
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200104 * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
105 *
106 * State sequence with step, state & sequence enums:
107 * => Input & Output Step = PSA_PAKE_STEP_INVALID
108 * => state = PSA_PAKE_STATE_INVALID
109 * psa_pake_setup()
110 * => Input & Output Step = PSA_PAKE_STEP_X1_X2
111 * => state = PSA_PAKE_STATE_SETUP
112 * => sequence = PSA_PAKE_SEQ_INVALID
113 * |
114 * |--- In any order: (First round input before or after first round output)
115 * | | First call of psa_pake_output() or psa_pake_input() sets
116 * | | state = PSA_PAKE_STATE_READY
117 * | |
118 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
119 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
120 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
121 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
122 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
123 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
124 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
125 * | | | => state = PSA_PAKE_STATE_READY
126 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200127 * | | | => Output Step = PSA_PAKE_STEP_X2S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200128 * | |
129 * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
130 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
131 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
132 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
133 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
134 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
135 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_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_INPUT_X4S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200139 * |
140 * |--- In any order: (Second round input before or after second round output)
141 * | |
142 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
143 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
144 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
145 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
146 * | | | => state = PSA_PAKE_STATE_READY
147 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200148 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200149 * | |
150 * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
151 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
152 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
153 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
154 * | | | => state = PSA_PAKE_STATE_READY
155 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200156 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200157 * |
158 * psa_pake_get_implicit_key()
159 * => Input & Output Step = PSA_PAKE_STEP_INVALID
160 */
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200161enum psa_pake_sequence
162{
163 PSA_PAKE_SEQ_INVALID = 0,
164 PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
165 PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
166 PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
167 PSA_PAKE_X2_STEP_KEY_SHARE = 4,
168 PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
169 PSA_PAKE_X2_STEP_ZK_PROOF = 6,
170 PSA_PAKE_SEQ_END = 7,
171};
172
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200173#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
174static psa_status_t mbedtls_ecjpake_to_psa_error( int ret )
175{
176 switch( ret )
177 {
178 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
179 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
180 case MBEDTLS_ERR_ECP_INVALID_KEY:
181 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
182 return( PSA_ERROR_DATA_INVALID );
183 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
184 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
185 return( PSA_ERROR_BUFFER_TOO_SMALL );
186 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
189 return( PSA_ERROR_CORRUPTION_DETECTED );
190 default:
191 return( PSA_ERROR_GENERIC_ERROR );
192 }
193}
194#endif
195
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200196#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
197psa_status_t psa_pake_setup( psa_pake_operation_t *operation,
198 const psa_pake_cipher_suite_t *cipher_suite)
199{
200 /* A context must be freshly initialized before it can be set up. */
Neil Armstrongcb679f22022-09-13 14:43:07 +0200201 if( operation->alg != PSA_ALG_NONE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200202 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200203
204 if( cipher_suite == NULL ||
205 PSA_ALG_IS_PAKE(cipher_suite->algorithm ) == 0 ||
206 ( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC &&
207 cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH ) ||
208 PSA_ALG_IS_HASH( cipher_suite->hash ) == 0 )
209 {
210 return( PSA_ERROR_INVALID_ARGUMENT );
211 }
212
Neil Armstronga557cb82022-06-10 08:58:32 +0200213#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200214 if( cipher_suite->algorithm == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200215 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200216 if( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
217 cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
218 cipher_suite->bits != 256 ||
219 cipher_suite->hash != PSA_ALG_SHA_256 )
220 {
221 return( PSA_ERROR_NOT_SUPPORTED );
222 }
223
224 operation->alg = cipher_suite->algorithm;
225
226 mbedtls_ecjpake_init( &operation->ctx.ecjpake );
227
228 operation->state = PSA_PAKE_STATE_SETUP;
229 operation->sequence = PSA_PAKE_SEQ_INVALID;
230 operation->input_step = PSA_PAKE_STEP_X1_X2;
231 operation->output_step = PSA_PAKE_STEP_X1_X2;
232
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200233 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
237 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200238 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200239 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200240#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200241 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200242}
243
244psa_status_t psa_pake_set_password_key( psa_pake_operation_t *operation,
245 mbedtls_svc_key_id_t password )
246{
247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
248 psa_key_attributes_t attributes = psa_key_attributes_init();
249 psa_key_type_t type;
250 psa_key_usage_t usage;
251
Neil Armstrongcb679f22022-09-13 14:43:07 +0200252 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200253 operation->state != PSA_PAKE_STATE_SETUP )
254 {
255 return( PSA_ERROR_BAD_STATE );
256 }
257
258 status = psa_get_key_attributes( password, &attributes );
259 if( status != PSA_SUCCESS )
Neil Armstronge9231112022-06-10 09:03:41 +0200260 return( status );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200261
262 type = psa_get_key_type( &attributes );
263 usage = psa_get_key_usage_flags( &attributes );
264
265 psa_reset_key_attributes( &attributes );
266
267 if( type != PSA_KEY_TYPE_PASSWORD &&
268 type != PSA_KEY_TYPE_PASSWORD_HASH )
269 {
Neil Armstronge9231112022-06-10 09:03:41 +0200270 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200271 }
272
Neil Armstrongdf598ab2022-06-08 17:17:08 +0200273 if( ( usage & PSA_KEY_USAGE_DERIVE ) == 0 )
Neil Armstronge9231112022-06-10 09:03:41 +0200274 return( PSA_ERROR_NOT_PERMITTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200275
276 operation->password = password;
277
278 return( PSA_SUCCESS );
279}
280
281psa_status_t psa_pake_set_user( psa_pake_operation_t *operation,
282 const uint8_t *user_id,
283 size_t user_id_len )
284{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200285 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200286 operation->state != PSA_PAKE_STATE_SETUP )
287 {
288 return( PSA_ERROR_BAD_STATE );
289 }
290
291 if( user_id_len == 0 || user_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200292 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200293
294 return( PSA_ERROR_NOT_SUPPORTED );
295}
296
297psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation,
298 const uint8_t *peer_id,
299 size_t peer_id_len )
300{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200301 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200302 operation->state != PSA_PAKE_STATE_SETUP )
303 {
304 return( PSA_ERROR_BAD_STATE );
305 }
306
307 if( peer_id_len == 0 || peer_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200308 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200309
310 return( PSA_ERROR_NOT_SUPPORTED );
311}
312
313psa_status_t psa_pake_set_role( psa_pake_operation_t *operation,
314 psa_pake_role_t role )
315{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200316 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200317 operation->state != PSA_PAKE_STATE_SETUP )
318 {
319 return( PSA_ERROR_BAD_STATE );
320 }
321
322 if( role != PSA_PAKE_ROLE_NONE &&
323 role != PSA_PAKE_ROLE_FIRST &&
324 role != PSA_PAKE_ROLE_SECOND &&
325 role != PSA_PAKE_ROLE_CLIENT &&
326 role != PSA_PAKE_ROLE_SERVER )
327 {
Neil Armstronge9231112022-06-10 09:03:41 +0200328 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200329 }
330
Neil Armstronga557cb82022-06-10 08:58:32 +0200331#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200332 if( operation->alg == PSA_ALG_JPAKE )
333 {
334 if( role != PSA_PAKE_ROLE_CLIENT &&
335 role != PSA_PAKE_ROLE_SERVER )
Neil Armstronge9231112022-06-10 09:03:41 +0200336 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200337
338 operation->role = role;
339
340 return( PSA_SUCCESS );
341 }
342 else
343#endif
344 return( PSA_ERROR_NOT_SUPPORTED );
345}
346
Neil Armstronga557cb82022-06-10 08:58:32 +0200347#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200348static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation )
349{
350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
352 mbedtls_ecjpake_role role;
353 psa_key_slot_t *slot = NULL;
354
355 if( operation->role == PSA_PAKE_ROLE_CLIENT )
356 role = MBEDTLS_ECJPAKE_CLIENT;
357 else if( operation->role == PSA_PAKE_ROLE_SERVER )
358 role = MBEDTLS_ECJPAKE_SERVER;
359 else
360 return( PSA_ERROR_BAD_STATE );
361
362 if( psa_is_valid_key_id( operation->password, 1 ) == 0 )
363 return( PSA_ERROR_BAD_STATE );
364
365 status = psa_get_and_lock_key_slot( operation->password, &slot );
366 if( status != PSA_SUCCESS )
367 return( status );
368
369
370 ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake,
371 role,
372 MBEDTLS_MD_SHA256,
373 MBEDTLS_ECP_DP_SECP256R1,
374 slot->key.data, slot->key.bytes );
375
376 psa_unlock_key_slot( slot );
377 slot = NULL;
378
379 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200380 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200381
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200382 operation->state = PSA_PAKE_STATE_READY;
383
384 return( PSA_SUCCESS );
385}
386#endif
387
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200388static psa_status_t psa_pake_output_internal(
389 psa_pake_operation_t *operation,
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200390 psa_pake_step_t step,
391 uint8_t *output,
392 size_t output_size,
393 size_t *output_length )
394{
395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
397 size_t length;
398
Neil Armstrongcb679f22022-09-13 14:43:07 +0200399 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200400 operation->state == PSA_PAKE_STATE_INVALID )
401 return( PSA_ERROR_BAD_STATE );
402
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200403 if( output == NULL || output_size == 0 || output_length == NULL )
404 return( PSA_ERROR_INVALID_ARGUMENT );
405
Neil Armstronga557cb82022-06-10 08:58:32 +0200406#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200407 /*
408 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
409 * handling of output sequencing.
410 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200411 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200412 * at once, on the other side the PSA CRYPTO PAKE api requires
413 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
414 * retrieved in sequence.
415 *
416 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
417 * data is stored in an intermediate buffer at first step output call,
418 * and data is sliced down by parsing the ECPoint records in order
419 * to return the right parts on each step.
420 */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200421 if( operation->alg == PSA_ALG_JPAKE )
422 {
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200423 if( step != PSA_PAKE_STEP_KEY_SHARE &&
424 step != PSA_PAKE_STEP_ZK_PUBLIC &&
425 step != PSA_PAKE_STEP_ZK_PROOF )
426 return( PSA_ERROR_INVALID_ARGUMENT );
427
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200428 if( operation->state == PSA_PAKE_STATE_SETUP ) {
429 status = psa_pake_ecjpake_setup( operation );
430 if( status != PSA_SUCCESS )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200431 return( status );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200432 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200433
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200434 if( operation->state != PSA_PAKE_STATE_READY &&
435 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
436 operation->state != PSA_PAKE_OUTPUT_X2S )
437 {
438 return( PSA_ERROR_BAD_STATE );
439 }
440
441 if( operation->state == PSA_PAKE_STATE_READY )
442 {
443 if( step != PSA_PAKE_STEP_KEY_SHARE )
444 return( PSA_ERROR_BAD_STATE );
445
446 switch( operation->output_step )
447 {
448 case PSA_PAKE_STEP_X1_X2:
449 operation->state = PSA_PAKE_OUTPUT_X1_X2;
450 break;
451 case PSA_PAKE_STEP_X2S:
452 operation->state = PSA_PAKE_OUTPUT_X2S;
453 break;
454 default:
455 return( PSA_ERROR_BAD_STATE );
456 }
457
458 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
459 }
460
461 /* Check if step matches current sequence */
462 switch( operation->sequence )
463 {
464 case PSA_PAKE_X1_STEP_KEY_SHARE:
465 case PSA_PAKE_X2_STEP_KEY_SHARE:
466 if( step != PSA_PAKE_STEP_KEY_SHARE )
467 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200468 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200469
470 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
471 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
472 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
473 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200474 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200475
476 case PSA_PAKE_X1_STEP_ZK_PROOF:
477 case PSA_PAKE_X2_STEP_ZK_PROOF:
478 if( step != PSA_PAKE_STEP_ZK_PROOF )
479 return( PSA_ERROR_BAD_STATE );
480 break;
481
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200482 default:
483 return( PSA_ERROR_BAD_STATE );
484 }
485
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200486 /* Initialize & write round on KEY_SHARE sequences */
487 if( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
488 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200489 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200490 ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake,
491 operation->buffer,
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200492 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200493 &operation->buffer_length,
494 mbedtls_psa_get_random,
495 MBEDTLS_PSA_RANDOM_STATE );
496 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200497 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200498
499 operation->buffer_offset = 0;
500 }
501 else if( operation->state == PSA_PAKE_OUTPUT_X2S &&
502 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
503 {
504 ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake,
505 operation->buffer,
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200506 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200507 &operation->buffer_length,
508 mbedtls_psa_get_random,
509 MBEDTLS_PSA_RANDOM_STATE );
510 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200511 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200512
513 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200514 }
515
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200516 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200517 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
518 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
519 * that the data for each step is prepended with a length byte, and
520 * then they're concatenated. Additionally, the server's second round
521 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200522 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200523 * In PSA, we output each step separately, and don't prepend the
524 * output with a length byte, even less a curve identifier, as that
525 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200526 */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200527 if( operation->state == PSA_PAKE_OUTPUT_X2S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200528 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
529 operation->role == PSA_PAKE_ROLE_SERVER )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200530 {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200531 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
532 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200533 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200534
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200535 /* Read the length byte then move past it to the data */
536 length = operation->buffer[operation->buffer_offset];
537 operation->buffer_offset += 1;
538
539 if( operation->buffer_offset + length > operation->buffer_length )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200540 return( PSA_ERROR_DATA_CORRUPT );
541
542 if( output_size < length )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200543 return( PSA_ERROR_BUFFER_TOO_SMALL );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200544
545 memcpy( output,
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200546 operation->buffer + operation->buffer_offset,
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200547 length );
548 *output_length = length;
549
550 operation->buffer_offset += length;
551
552 /* Reset buffer after ZK_PROOF sequence */
553 if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
554 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
555 ( operation->state == PSA_PAKE_OUTPUT_X2S &&
556 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
557 {
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200558 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200559 operation->buffer_length = 0;
560 operation->buffer_offset = 0;
561
562 operation->state = PSA_PAKE_STATE_READY;
563 operation->output_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200564 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200565 }
566 else
567 operation->sequence++;
568
569 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200570 }
571 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200572#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200573 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200574}
575
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200576psa_status_t psa_pake_output( psa_pake_operation_t *operation,
577 psa_pake_step_t step,
578 uint8_t *output,
579 size_t output_size,
580 size_t *output_length )
581{
582 psa_status_t status = psa_pake_output_internal(
583 operation, step, output, output_size, output_length );
584
585 if( status != PSA_SUCCESS )
586 psa_pake_abort( operation );
587
588 return( status );
589}
590
591static psa_status_t psa_pake_input_internal(
592 psa_pake_operation_t *operation,
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200593 psa_pake_step_t step,
594 const uint8_t *input,
595 size_t input_length )
596{
597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
598 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200599
Neil Armstrongcb679f22022-09-13 14:43:07 +0200600 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200601 operation->state == PSA_PAKE_STATE_INVALID )
602 return( PSA_ERROR_BAD_STATE );
603
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200604 if( input == NULL || input_length == 0 )
605 return( PSA_ERROR_INVALID_ARGUMENT );
606
Neil Armstronga557cb82022-06-10 08:58:32 +0200607#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200608 /*
609 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
610 * handling of input sequencing.
611 *
612 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
613 * at once as input, on the other side the PSA CRYPTO PAKE api requires
614 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
615 * given in sequence.
616 *
617 * In order to achieve API compatibility, each X1+X2 or X4S step data
618 * is stored sequentially in an intermediate buffer and given to the
619 * MbedTLS JPAKE API on the last step.
620 *
621 * This causes any input error to be only detected on the last step.
622 */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200623 if( operation->alg == PSA_ALG_JPAKE )
624 {
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200625 if( step != PSA_PAKE_STEP_KEY_SHARE &&
626 step != PSA_PAKE_STEP_ZK_PUBLIC &&
627 step != PSA_PAKE_STEP_ZK_PROOF )
628 return( PSA_ERROR_INVALID_ARGUMENT );
629
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200630 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
631 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256 );
632 if( input_length > PSA_PAKE_INPUT_SIZE( PSA_ALG_JPAKE, prim, step ) )
633 return( PSA_ERROR_INVALID_ARGUMENT );
634
Neil Armstrong5bbdb702022-09-05 17:54:15 +0200635 if( operation->state == PSA_PAKE_STATE_SETUP )
636 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200637 status = psa_pake_ecjpake_setup( operation );
638 if( status != PSA_SUCCESS )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200639 return( status );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200640 }
641
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200642 if( operation->state != PSA_PAKE_STATE_READY &&
643 operation->state != PSA_PAKE_INPUT_X1_X2 &&
644 operation->state != PSA_PAKE_INPUT_X4S )
645 {
646 return( PSA_ERROR_BAD_STATE );
647 }
648
649 if( operation->state == PSA_PAKE_STATE_READY )
650 {
651 if( step != PSA_PAKE_STEP_KEY_SHARE )
652 return( PSA_ERROR_BAD_STATE );
653
654 switch( operation->input_step )
655 {
656 case PSA_PAKE_STEP_X1_X2:
657 operation->state = PSA_PAKE_INPUT_X1_X2;
658 break;
659 case PSA_PAKE_STEP_X2S:
660 operation->state = PSA_PAKE_INPUT_X4S;
661 break;
662 default:
663 return( PSA_ERROR_BAD_STATE );
664 }
665
666 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
667 }
668
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200669 /* Check if step matches current sequence */
670 switch( operation->sequence )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200671 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200672 case PSA_PAKE_X1_STEP_KEY_SHARE:
673 case PSA_PAKE_X2_STEP_KEY_SHARE:
674 if( step != PSA_PAKE_STEP_KEY_SHARE )
675 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200676 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200677
678 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
679 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
680 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
681 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200682 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200683
684 case PSA_PAKE_X1_STEP_ZK_PROOF:
685 case PSA_PAKE_X2_STEP_ZK_PROOF:
686 if( step != PSA_PAKE_STEP_ZK_PROOF )
687 return( PSA_ERROR_BAD_STATE );
688 break;
689
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200690 default:
691 return( PSA_ERROR_BAD_STATE );
692 }
693
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200694 /*
695 * Copy input to local buffer and format it as the Mbed TLS API
696 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
697 * The summary is that the data for each step is prepended with a
698 * length byte, and then they're concatenated. Additionally, the
699 * server's second round output is prepended with a 3-bytes
700 * ECParameters structure - which means we have to prepend that when
701 * we're a client.
702 */
703 if( operation->state == PSA_PAKE_INPUT_X4S &&
704 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
705 operation->role == PSA_PAKE_ROLE_CLIENT )
706 {
707 /* We only support secp256r1. */
708 /* This is the ECParameters structure defined by RFC 8422. */
709 unsigned char ecparameters[3] = {
710 3, /* named_curve */
711 0, 23 /* secp256r1 */
712 };
713 memcpy( operation->buffer + operation->buffer_length,
714 ecparameters, sizeof( ecparameters ) );
715 operation->buffer_length += sizeof( ecparameters );
716 }
717
718 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200719 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200720 operation->buffer_length += 1;
721
722 /* Finally copy the data */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200723 memcpy( operation->buffer + operation->buffer_length,
724 input, input_length );
725 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200726
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200727 /* Load buffer at each last round ZK_PROOF */
728 if( operation->state == PSA_PAKE_INPUT_X1_X2 &&
729 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200730 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200731 ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake,
732 operation->buffer,
733 operation->buffer_length );
734
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200735 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200736 operation->buffer_length = 0;
737
738 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200739 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200740 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200741 else if( operation->state == PSA_PAKE_INPUT_X4S &&
742 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200743 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200744 ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake,
745 operation->buffer,
746 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200747
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200748 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200749 operation->buffer_length = 0;
750
751 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200752 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200753 }
754
755 if( ( operation->state == PSA_PAKE_INPUT_X1_X2 &&
756 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
757 ( operation->state == PSA_PAKE_INPUT_X4S &&
758 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
759 {
760 operation->state = PSA_PAKE_STATE_READY;
761 operation->input_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200762 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200763 }
764 else
765 operation->sequence++;
766
767 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200768 }
769 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200770#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200771 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200772}
773
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200774psa_status_t psa_pake_input( psa_pake_operation_t *operation,
775 psa_pake_step_t step,
776 const uint8_t *input,
777 size_t input_length )
778{
779 psa_status_t status = psa_pake_input_internal(
780 operation, step, input, input_length );
781
782 if( status != PSA_SUCCESS )
783 psa_pake_abort( operation );
784
785 return( status );
786}
787
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200788psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
789 psa_key_derivation_operation_t *output)
790{
791 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
793
Neil Armstrongcb679f22022-09-13 14:43:07 +0200794 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200795 operation->state != PSA_PAKE_STATE_READY ||
Neil Armstrong1e855602022-06-15 11:32:11 +0200796 operation->input_step != PSA_PAKE_STEP_DERIVE ||
797 operation->output_step != PSA_PAKE_STEP_DERIVE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200798 return( PSA_ERROR_BAD_STATE );
799
Neil Armstronga557cb82022-06-10 08:58:32 +0200800#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200801 if( operation->alg == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200802 {
Neil Armstrongf19a3cb2022-06-15 16:00:29 +0200803 ret = mbedtls_ecjpake_write_shared_key( &operation->ctx.ecjpake,
804 operation->buffer,
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200805 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
Neil Armstrongf19a3cb2022-06-15 16:00:29 +0200806 &operation->buffer_length,
807 mbedtls_psa_get_random,
808 MBEDTLS_PSA_RANDOM_STATE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200809 if( ret != 0)
810 {
811 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200812 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200813 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200814
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200815 status = psa_key_derivation_input_bytes( output,
816 PSA_KEY_DERIVATION_INPUT_SECRET,
817 operation->buffer,
818 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200819
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200820 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200821
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200822 psa_pake_abort( operation );
823
824 return( status );
825 }
826 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200827#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200828 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200829}
830
831psa_status_t psa_pake_abort(psa_pake_operation_t * operation)
832{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200833 if( operation->alg == PSA_ALG_NONE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200834 {
835 return( PSA_SUCCESS );
836 }
837
Neil Armstronga557cb82022-06-10 08:58:32 +0200838#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200839 if( operation->alg == PSA_ALG_JPAKE )
840 {
Neil Armstrongcb679f22022-09-13 14:43:07 +0200841 operation->input_step = PSA_PAKE_STEP_INVALID;
842 operation->output_step = PSA_PAKE_STEP_INVALID;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200843 operation->password = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200844 operation->role = PSA_PAKE_ROLE_NONE;
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200845 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200846 operation->buffer_length = 0;
847 operation->buffer_offset = 0;
848 mbedtls_ecjpake_free( &operation->ctx.ecjpake );
849 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200850#endif
851
Neil Armstrongcb679f22022-09-13 14:43:07 +0200852 operation->alg = PSA_ALG_NONE;
853 operation->state = PSA_PAKE_STATE_INVALID;
854 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200855
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200856 return( PSA_SUCCESS );
857}
858
859#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
860
861#endif /* MBEDTLS_PSA_CRYPTO_C */