blob: 556acd99b793bcb9374bab26916bafdfec2cbeb0 [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 Armstronga557cb82022-06-10 08:58:32 +020036#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +020037#define PSA_PAKE_BUFFER_SIZE ( ( 69 + 66 + 33 ) * 2 )
38#endif
39
40/*
41 * State sequence:
42 *
43 * psa_pake_setup()
44 * |
45 * |-- In any order:
46 * | | psa_pake_set_password_key()
47 * | | psa_pake_set_user()
48 * | | psa_pake_set_peer()
Neil Armstrongc29f8472022-06-08 13:34:49 +020049 * | | psa_pake_set_role()
Neil Armstronga4cc7d62022-05-25 11:30:48 +020050 * |
51 * |--- In any order: (First round input before or after first round output)
52 * | |
53 * | |------ In Order
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 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
58 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
59 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
60 * | |
61 * | |------ In Order:
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 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
66 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
67 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
68 * |
69 * |--- In any order: (Second round input before or after second round output)
70 * | |
71 * | |------ In Order
72 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
73 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
74 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
75 * | |
76 * | |------ In Order:
77 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
78 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
79 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
80 * |
81 * psa_pake_get_implicit_key()
82 * psa_pake_abort()
83 */
84
85enum psa_pake_step
86{
87 PSA_PAKE_STEP_INVALID = 0,
88 PSA_PAKE_STEP_X1_X2 = 1,
89 PSA_PAKE_STEP_X2S = 2,
90 PSA_PAKE_STEP_DERIVE = 3,
91};
92
93enum psa_pake_state
94{
95 PSA_PAKE_STATE_INVALID = 0,
96 PSA_PAKE_STATE_SETUP = 1,
97 PSA_PAKE_STATE_READY = 2,
98 PSA_PAKE_OUTPUT_X1_X2 = 3,
99 PSA_PAKE_OUTPUT_X2S = 4,
100 PSA_PAKE_INPUT_X1_X2 = 5,
101 PSA_PAKE_INPUT_X4S = 6,
102};
103
104enum psa_pake_sequence
105{
106 PSA_PAKE_SEQ_INVALID = 0,
107 PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
108 PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
109 PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
110 PSA_PAKE_X2_STEP_KEY_SHARE = 4,
111 PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
112 PSA_PAKE_X2_STEP_ZK_PROOF = 6,
113 PSA_PAKE_SEQ_END = 7,
114};
115
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200116#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
117static psa_status_t mbedtls_ecjpake_to_psa_error( int ret )
118{
119 switch( ret )
120 {
121 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
122 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
123 case MBEDTLS_ERR_ECP_INVALID_KEY:
124 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
125 return( PSA_ERROR_DATA_INVALID );
126 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
127 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
128 return( PSA_ERROR_BUFFER_TOO_SMALL );
129 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
130 return( PSA_ERROR_NOT_SUPPORTED );
131 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
132 return( PSA_ERROR_CORRUPTION_DETECTED );
133 default:
134 return( PSA_ERROR_GENERIC_ERROR );
135 }
136}
137#endif
138
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200139#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
140psa_status_t psa_pake_setup( psa_pake_operation_t *operation,
141 const psa_pake_cipher_suite_t *cipher_suite)
142{
143 /* A context must be freshly initialized before it can be set up. */
Neil Armstrong5fb07c62022-06-10 09:00:00 +0200144 if( operation->alg != 0 )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200145 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200146
147 if( cipher_suite == NULL ||
148 PSA_ALG_IS_PAKE(cipher_suite->algorithm ) == 0 ||
149 ( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC &&
150 cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH ) ||
151 PSA_ALG_IS_HASH( cipher_suite->hash ) == 0 )
152 {
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 }
155
Neil Armstronga557cb82022-06-10 08:58:32 +0200156#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200157 if( cipher_suite->algorithm == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200158 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200159 if( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
160 cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
161 cipher_suite->bits != 256 ||
162 cipher_suite->hash != PSA_ALG_SHA_256 )
163 {
164 return( PSA_ERROR_NOT_SUPPORTED );
165 }
166
167 operation->alg = cipher_suite->algorithm;
168
169 mbedtls_ecjpake_init( &operation->ctx.ecjpake );
170
171 operation->state = PSA_PAKE_STATE_SETUP;
172 operation->sequence = PSA_PAKE_SEQ_INVALID;
173 operation->input_step = PSA_PAKE_STEP_X1_X2;
174 operation->output_step = PSA_PAKE_STEP_X1_X2;
175
176 operation->buffer = NULL;
177 operation->buffer_length = 0;
178 operation->buffer_offset = 0;
179
180 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200181 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200182 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200183#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200184 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200185}
186
187psa_status_t psa_pake_set_password_key( psa_pake_operation_t *operation,
188 mbedtls_svc_key_id_t password )
189{
190 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
191 psa_key_attributes_t attributes = psa_key_attributes_init();
192 psa_key_type_t type;
193 psa_key_usage_t usage;
194
195 if( operation->alg == 0 ||
196 operation->state != PSA_PAKE_STATE_SETUP )
197 {
198 return( PSA_ERROR_BAD_STATE );
199 }
200
201 status = psa_get_key_attributes( password, &attributes );
202 if( status != PSA_SUCCESS )
Neil Armstronge9231112022-06-10 09:03:41 +0200203 return( status );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200204
205 type = psa_get_key_type( &attributes );
206 usage = psa_get_key_usage_flags( &attributes );
207
208 psa_reset_key_attributes( &attributes );
209
210 if( type != PSA_KEY_TYPE_PASSWORD &&
211 type != PSA_KEY_TYPE_PASSWORD_HASH )
212 {
Neil Armstronge9231112022-06-10 09:03:41 +0200213 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200214 }
215
Neil Armstrongdf598ab2022-06-08 17:17:08 +0200216 if( ( usage & PSA_KEY_USAGE_DERIVE ) == 0 )
Neil Armstronge9231112022-06-10 09:03:41 +0200217 return( PSA_ERROR_NOT_PERMITTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200218
219 operation->password = password;
220
221 return( PSA_SUCCESS );
222}
223
224psa_status_t psa_pake_set_user( psa_pake_operation_t *operation,
225 const uint8_t *user_id,
226 size_t user_id_len )
227{
228 if( operation->alg == 0 ||
229 operation->state != PSA_PAKE_STATE_SETUP )
230 {
231 return( PSA_ERROR_BAD_STATE );
232 }
233
234 if( user_id_len == 0 || user_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200236
237 return( PSA_ERROR_NOT_SUPPORTED );
238}
239
240psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation,
241 const uint8_t *peer_id,
242 size_t peer_id_len )
243{
244 if( operation->alg == 0 ||
245 operation->state != PSA_PAKE_STATE_SETUP )
246 {
247 return( PSA_ERROR_BAD_STATE );
248 }
249
250 if( peer_id_len == 0 || peer_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200251 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200252
253 return( PSA_ERROR_NOT_SUPPORTED );
254}
255
256psa_status_t psa_pake_set_role( psa_pake_operation_t *operation,
257 psa_pake_role_t role )
258{
259 if( operation->alg == 0 ||
260 operation->state != PSA_PAKE_STATE_SETUP )
261 {
262 return( PSA_ERROR_BAD_STATE );
263 }
264
265 if( role != PSA_PAKE_ROLE_NONE &&
266 role != PSA_PAKE_ROLE_FIRST &&
267 role != PSA_PAKE_ROLE_SECOND &&
268 role != PSA_PAKE_ROLE_CLIENT &&
269 role != PSA_PAKE_ROLE_SERVER )
270 {
Neil Armstronge9231112022-06-10 09:03:41 +0200271 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200272 }
273
Neil Armstronga557cb82022-06-10 08:58:32 +0200274#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200275 if( operation->alg == PSA_ALG_JPAKE )
276 {
277 if( role != PSA_PAKE_ROLE_CLIENT &&
278 role != PSA_PAKE_ROLE_SERVER )
Neil Armstronge9231112022-06-10 09:03:41 +0200279 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200280
281 operation->role = role;
282
283 return( PSA_SUCCESS );
284 }
285 else
286#endif
287 return( PSA_ERROR_NOT_SUPPORTED );
288}
289
Neil Armstronga557cb82022-06-10 08:58:32 +0200290#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200291static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation )
292{
293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
294 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
295 mbedtls_ecjpake_role role;
296 psa_key_slot_t *slot = NULL;
297
298 if( operation->role == PSA_PAKE_ROLE_CLIENT )
299 role = MBEDTLS_ECJPAKE_CLIENT;
300 else if( operation->role == PSA_PAKE_ROLE_SERVER )
301 role = MBEDTLS_ECJPAKE_SERVER;
302 else
303 return( PSA_ERROR_BAD_STATE );
304
305 if( psa_is_valid_key_id( operation->password, 1 ) == 0 )
306 return( PSA_ERROR_BAD_STATE );
307
308 status = psa_get_and_lock_key_slot( operation->password, &slot );
309 if( status != PSA_SUCCESS )
310 return( status );
311
312
313 ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake,
314 role,
315 MBEDTLS_MD_SHA256,
316 MBEDTLS_ECP_DP_SECP256R1,
317 slot->key.data, slot->key.bytes );
318
319 psa_unlock_key_slot( slot );
320 slot = NULL;
321
322 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200323 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200324
Neil Armstrong6b1f99f2022-06-08 13:37:37 +0200325 operation->buffer = mbedtls_calloc( 1, PSA_PAKE_BUFFER_SIZE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200326 if( operation->buffer == NULL )
327 return( PSA_ERROR_INSUFFICIENT_MEMORY );
328
329 operation->state = PSA_PAKE_STATE_READY;
330
331 return( PSA_SUCCESS );
332}
333#endif
334
335psa_status_t psa_pake_output( psa_pake_operation_t *operation,
336 psa_pake_step_t step,
337 uint8_t *output,
338 size_t output_size,
339 size_t *output_length )
340{
341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
343 size_t length;
344
345 if( operation->alg == 0 ||
346 operation->state == PSA_PAKE_STATE_INVALID )
347 return( PSA_ERROR_BAD_STATE );
348
349 if( step != PSA_PAKE_STEP_KEY_SHARE &&
350 step != PSA_PAKE_STEP_ZK_PUBLIC &&
351 step != PSA_PAKE_STEP_ZK_PROOF )
352 return( PSA_ERROR_INVALID_ARGUMENT );
353
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200354 if( output == NULL || output_size == 0 || output_length == NULL )
355 return( PSA_ERROR_INVALID_ARGUMENT );
356
Neil Armstronga557cb82022-06-10 08:58:32 +0200357#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200358 if( operation->alg == PSA_ALG_JPAKE )
359 {
360 if( operation->state == PSA_PAKE_STATE_SETUP ) {
361 status = psa_pake_ecjpake_setup( operation );
362 if( status != PSA_SUCCESS )
363 {
364 psa_pake_abort( operation );
365 return( status );
366 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200367 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200368
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200369 if( operation->state >= PSA_PAKE_STATE_READY &&
370 ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 ||
371 operation->buffer == NULL ) )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200372 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200373 return( PSA_ERROR_BAD_STATE );
374 }
375
376 if( operation->state != PSA_PAKE_STATE_READY &&
377 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
378 operation->state != PSA_PAKE_OUTPUT_X2S )
379 {
380 return( PSA_ERROR_BAD_STATE );
381 }
382
383 if( operation->state == PSA_PAKE_STATE_READY )
384 {
385 if( step != PSA_PAKE_STEP_KEY_SHARE )
386 return( PSA_ERROR_BAD_STATE );
387
388 switch( operation->output_step )
389 {
390 case PSA_PAKE_STEP_X1_X2:
391 operation->state = PSA_PAKE_OUTPUT_X1_X2;
392 break;
393 case PSA_PAKE_STEP_X2S:
394 operation->state = PSA_PAKE_OUTPUT_X2S;
395 break;
396 default:
397 return( PSA_ERROR_BAD_STATE );
398 }
399
400 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
401 }
402
403 /* Check if step matches current sequence */
404 switch( operation->sequence )
405 {
406 case PSA_PAKE_X1_STEP_KEY_SHARE:
407 case PSA_PAKE_X2_STEP_KEY_SHARE:
408 if( step != PSA_PAKE_STEP_KEY_SHARE )
409 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200410 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200411
412 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
413 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
414 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
415 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200416 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200417
418 case PSA_PAKE_X1_STEP_ZK_PROOF:
419 case PSA_PAKE_X2_STEP_ZK_PROOF:
420 if( step != PSA_PAKE_STEP_ZK_PROOF )
421 return( PSA_ERROR_BAD_STATE );
422 break;
423
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200424 default:
425 return( PSA_ERROR_BAD_STATE );
426 }
427
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200428 /* Initialize & write round on KEY_SHARE sequences */
429 if( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
430 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200431 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200432 ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake,
433 operation->buffer,
434 PSA_PAKE_BUFFER_SIZE,
435 &operation->buffer_length,
436 mbedtls_psa_get_random,
437 MBEDTLS_PSA_RANDOM_STATE );
438 if( ret != 0 )
439 {
440 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200441 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200442 }
443
444 operation->buffer_offset = 0;
445 }
446 else if( operation->state == PSA_PAKE_OUTPUT_X2S &&
447 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
448 {
449 ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake,
450 operation->buffer,
451 PSA_PAKE_BUFFER_SIZE,
452 &operation->buffer_length,
453 mbedtls_psa_get_random,
454 MBEDTLS_PSA_RANDOM_STATE );
455 if( ret != 0 )
456 {
457 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200458 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200459 }
460
461 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200462 }
463
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200464 /* Load output sequence length */
465 if( operation->state == PSA_PAKE_OUTPUT_X2S &&
466 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200467 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200468 if( operation->role == PSA_PAKE_ROLE_SERVER )
469 /* Length is stored after 3bytes curve */
470 length = 3 + operation->buffer[3] + 1;
471 else
472 /* Length is stored at the first byte */
473 length = operation->buffer[0] + 1;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200474 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200475 else
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200476 /* Length is stored at the first byte of the next chunk */
477 length = operation->buffer[operation->buffer_offset] + 1;
478
479 if( length > operation->buffer_length )
480 return( PSA_ERROR_DATA_CORRUPT );
481
482 if( output_size < length )
483 {
484 psa_pake_abort( operation );
485 return( PSA_ERROR_BUFFER_TOO_SMALL );
486 }
487
488 memcpy( output,
489 operation->buffer + operation->buffer_offset,
490 length );
491 *output_length = length;
492
493 operation->buffer_offset += length;
494
495 /* Reset buffer after ZK_PROOF sequence */
496 if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
497 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
498 ( operation->state == PSA_PAKE_OUTPUT_X2S &&
499 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
500 {
501 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
502 operation->buffer_length = 0;
503 operation->buffer_offset = 0;
504
505 operation->state = PSA_PAKE_STATE_READY;
506 operation->output_step++;
507 operation->sequence = 0;
508 }
509 else
510 operation->sequence++;
511
512 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200513 }
514 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200515#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200516 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200517}
518
519psa_status_t psa_pake_input( psa_pake_operation_t *operation,
520 psa_pake_step_t step,
521 const uint8_t *input,
522 size_t input_length )
523{
524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
525 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
526 size_t buffer_remain;
527
528 if( operation->alg == 0 ||
529 operation->state == PSA_PAKE_STATE_INVALID )
530 return( PSA_ERROR_BAD_STATE );
531
532 if( step != PSA_PAKE_STEP_KEY_SHARE &&
533 step != PSA_PAKE_STEP_ZK_PUBLIC &&
534 step != PSA_PAKE_STEP_ZK_PROOF )
535 return( PSA_ERROR_INVALID_ARGUMENT );
536
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200537 if( input == NULL || input_length == 0 )
538 return( PSA_ERROR_INVALID_ARGUMENT );
539
Neil Armstronga557cb82022-06-10 08:58:32 +0200540#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200541 if( operation->alg == PSA_ALG_JPAKE )
542 {
543 if( operation->state == PSA_PAKE_STATE_SETUP ) {
544 status = psa_pake_ecjpake_setup( operation );
545 if( status != PSA_SUCCESS )
546 {
547 psa_pake_abort( operation );
548 return( status );
549 }
550 }
551
552 if( operation->state >= PSA_PAKE_STATE_READY &&
553 ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 ||
554 operation->buffer == NULL ) )
555 {
556 return( PSA_ERROR_BAD_STATE );
557 }
558
559 if( operation->state != PSA_PAKE_STATE_READY &&
560 operation->state != PSA_PAKE_INPUT_X1_X2 &&
561 operation->state != PSA_PAKE_INPUT_X4S )
562 {
563 return( PSA_ERROR_BAD_STATE );
564 }
565
566 if( operation->state == PSA_PAKE_STATE_READY )
567 {
568 if( step != PSA_PAKE_STEP_KEY_SHARE )
569 return( PSA_ERROR_BAD_STATE );
570
571 switch( operation->input_step )
572 {
573 case PSA_PAKE_STEP_X1_X2:
574 operation->state = PSA_PAKE_INPUT_X1_X2;
575 break;
576 case PSA_PAKE_STEP_X2S:
577 operation->state = PSA_PAKE_INPUT_X4S;
578 break;
579 default:
580 return( PSA_ERROR_BAD_STATE );
581 }
582
583 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
584 }
585
586 buffer_remain = PSA_PAKE_BUFFER_SIZE - operation->buffer_length;
587
588 if( input_length == 0 ||
589 input_length > buffer_remain )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200590 {
591 psa_pake_abort( operation );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200592 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200593 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200594
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200595 /* Check if step matches current sequence */
596 switch( operation->sequence )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200597 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200598 case PSA_PAKE_X1_STEP_KEY_SHARE:
599 case PSA_PAKE_X2_STEP_KEY_SHARE:
600 if( step != PSA_PAKE_STEP_KEY_SHARE )
601 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200602 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200603
604 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
605 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
606 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
607 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200608 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200609
610 case PSA_PAKE_X1_STEP_ZK_PROOF:
611 case PSA_PAKE_X2_STEP_ZK_PROOF:
612 if( step != PSA_PAKE_STEP_ZK_PROOF )
613 return( PSA_ERROR_BAD_STATE );
614 break;
615
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200616 default:
617 return( PSA_ERROR_BAD_STATE );
618 }
619
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200620 /* Copy input to local buffer */
621 memcpy( operation->buffer + operation->buffer_length,
622 input, input_length );
623 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200624
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200625 /* Load buffer at each last round ZK_PROOF */
626 if( operation->state == PSA_PAKE_INPUT_X1_X2 &&
627 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200628 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200629 ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake,
630 operation->buffer,
631 operation->buffer_length );
632
633 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
634 operation->buffer_length = 0;
635
636 if( ret != 0 )
637 {
638 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200639 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200640 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200641 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200642 else if( operation->state == PSA_PAKE_INPUT_X4S &&
643 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200644 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200645 ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake,
646 operation->buffer,
647 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200648
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200649 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
650 operation->buffer_length = 0;
651
652 if( ret != 0 )
653 {
654 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200655 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200656 }
657 }
658
659 if( ( operation->state == PSA_PAKE_INPUT_X1_X2 &&
660 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
661 ( operation->state == PSA_PAKE_INPUT_X4S &&
662 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
663 {
664 operation->state = PSA_PAKE_STATE_READY;
665 operation->input_step++;
666 operation->sequence = 0;
667 }
668 else
669 operation->sequence++;
670
671 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200672 }
673 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200674#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200675 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200676}
677
678psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
679 psa_key_derivation_operation_t *output)
680{
681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
682 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
683
684 if( operation->alg == 0 ||
685 operation->state != PSA_PAKE_STATE_READY ||
Neil Armstrong1e855602022-06-15 11:32:11 +0200686 operation->input_step != PSA_PAKE_STEP_DERIVE ||
687 operation->output_step != PSA_PAKE_STEP_DERIVE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200688 return( PSA_ERROR_BAD_STATE );
689
Neil Armstronga557cb82022-06-10 08:58:32 +0200690#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200691 if( operation->alg == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200692 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200693 ret = mbedtls_ecjpake_derive_secret( &operation->ctx.ecjpake,
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200694 operation->buffer,
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200695 PSA_PAKE_BUFFER_SIZE,
696 &operation->buffer_length,
697 mbedtls_psa_get_random,
698 MBEDTLS_PSA_RANDOM_STATE );
699 if( ret != 0)
700 {
701 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200702 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200703 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200704
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200705 status = psa_key_derivation_input_bytes( output,
706 PSA_KEY_DERIVATION_INPUT_SECRET,
707 operation->buffer,
708 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200709
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200710 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200711
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200712 psa_pake_abort( operation );
713
714 return( status );
715 }
716 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200717#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200718 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200719}
720
721psa_status_t psa_pake_abort(psa_pake_operation_t * operation)
722{
723 if( operation->alg == 0 )
724 {
725 return( PSA_SUCCESS );
726 }
727
Neil Armstronga557cb82022-06-10 08:58:32 +0200728#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200729 if( operation->alg == PSA_ALG_JPAKE )
730 {
731 operation->input_step = 0;
732 operation->output_step = 0;
733 operation->password = MBEDTLS_SVC_KEY_ID_INIT;
734 operation->role = 0;
735 mbedtls_free( operation->buffer );
736 operation->buffer = NULL;
737 operation->buffer_length = 0;
738 operation->buffer_offset = 0;
739 mbedtls_ecjpake_free( &operation->ctx.ecjpake );
740 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200741#endif
742
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200743 operation->alg = 0;
744 operation->state = 0;
745 operation->sequence = 0;
746
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200747 return( PSA_SUCCESS );
748}
749
750#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
751
752#endif /* MBEDTLS_PSA_CRYPTO_C */