blob: 594def2ba64148b0f2b693eb9bc53ffde4d6c076 [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
36#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
37#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
116#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
117psa_status_t psa_pake_setup( psa_pake_operation_t *operation,
118 const psa_pake_cipher_suite_t *cipher_suite)
119{
120 /* A context must be freshly initialized before it can be set up. */
121 if( operation->alg != 0 || operation->state != PSA_PAKE_STATE_INVALID )
122 {
123 return( PSA_ERROR_BAD_STATE );
124 }
125
126 if( cipher_suite == NULL ||
127 PSA_ALG_IS_PAKE(cipher_suite->algorithm ) == 0 ||
128 ( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC &&
129 cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH ) ||
130 PSA_ALG_IS_HASH( cipher_suite->hash ) == 0 )
131 {
132 return( PSA_ERROR_INVALID_ARGUMENT );
133 }
134
135#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
136 if( cipher_suite->algorithm != PSA_ALG_JPAKE ||
137 cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
138 cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
139 cipher_suite->bits != 256 ||
140 cipher_suite->hash != PSA_ALG_SHA_256 )
141 {
142 return( PSA_ERROR_NOT_SUPPORTED );
143 }
144
145 operation->alg = cipher_suite->algorithm;
146
147 mbedtls_ecjpake_init( &operation->ctx.ecjpake );
148
149 operation->state = PSA_PAKE_STATE_SETUP;
150 operation->sequence = PSA_PAKE_SEQ_INVALID;
151 operation->input_step = PSA_PAKE_STEP_X1_X2;
152 operation->output_step = PSA_PAKE_STEP_X1_X2;
153
154 operation->buffer = NULL;
155 operation->buffer_length = 0;
156 operation->buffer_offset = 0;
157
158 return( PSA_SUCCESS );
159#else
160 return( PSA_ERROR_NOT_SUPPORTED );
161#endif
162}
163
164psa_status_t psa_pake_set_password_key( psa_pake_operation_t *operation,
165 mbedtls_svc_key_id_t password )
166{
167 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
168 psa_key_attributes_t attributes = psa_key_attributes_init();
169 psa_key_type_t type;
170 psa_key_usage_t usage;
171
172 if( operation->alg == 0 ||
173 operation->state != PSA_PAKE_STATE_SETUP )
174 {
175 return( PSA_ERROR_BAD_STATE );
176 }
177
178 status = psa_get_key_attributes( password, &attributes );
179 if( status != PSA_SUCCESS )
180 return status;
181
182 type = psa_get_key_type( &attributes );
183 usage = psa_get_key_usage_flags( &attributes );
184
185 psa_reset_key_attributes( &attributes );
186
187 if( type != PSA_KEY_TYPE_PASSWORD &&
188 type != PSA_KEY_TYPE_PASSWORD_HASH )
189 {
190 return PSA_ERROR_INVALID_ARGUMENT;
191 }
192
193 if( usage == 0 ||
194 ( usage & PSA_KEY_USAGE_DERIVE ) == 0 )
195 {
196 return PSA_ERROR_NOT_PERMITTED;
197 }
198
199 operation->password = password;
200
201 return( PSA_SUCCESS );
202}
203
204psa_status_t psa_pake_set_user( psa_pake_operation_t *operation,
205 const uint8_t *user_id,
206 size_t user_id_len )
207{
208 if( operation->alg == 0 ||
209 operation->state != PSA_PAKE_STATE_SETUP )
210 {
211 return( PSA_ERROR_BAD_STATE );
212 }
213
214 if( user_id_len == 0 || user_id == NULL )
215 return PSA_ERROR_INVALID_ARGUMENT;
216
217 return( PSA_ERROR_NOT_SUPPORTED );
218}
219
220psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation,
221 const uint8_t *peer_id,
222 size_t peer_id_len )
223{
224 if( operation->alg == 0 ||
225 operation->state != PSA_PAKE_STATE_SETUP )
226 {
227 return( PSA_ERROR_BAD_STATE );
228 }
229
230 if( peer_id_len == 0 || peer_id == NULL )
231 return PSA_ERROR_INVALID_ARGUMENT;
232
233 return( PSA_ERROR_NOT_SUPPORTED );
234}
235
236psa_status_t psa_pake_set_role( psa_pake_operation_t *operation,
237 psa_pake_role_t role )
238{
239 if( operation->alg == 0 ||
240 operation->state != PSA_PAKE_STATE_SETUP )
241 {
242 return( PSA_ERROR_BAD_STATE );
243 }
244
245 if( role != PSA_PAKE_ROLE_NONE &&
246 role != PSA_PAKE_ROLE_FIRST &&
247 role != PSA_PAKE_ROLE_SECOND &&
248 role != PSA_PAKE_ROLE_CLIENT &&
249 role != PSA_PAKE_ROLE_SERVER )
250 {
251 return PSA_ERROR_INVALID_ARGUMENT;
252 }
253
254#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
255 if( operation->alg == PSA_ALG_JPAKE )
256 {
257 if( role != PSA_PAKE_ROLE_CLIENT &&
258 role != PSA_PAKE_ROLE_SERVER )
259 return PSA_ERROR_NOT_SUPPORTED;
260
261 operation->role = role;
262
263 return( PSA_SUCCESS );
264 }
265 else
266#endif
267 return( PSA_ERROR_NOT_SUPPORTED );
268}
269
270#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
271static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation )
272{
273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
274 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
275 mbedtls_ecjpake_role role;
276 psa_key_slot_t *slot = NULL;
277
278 if( operation->role == PSA_PAKE_ROLE_CLIENT )
279 role = MBEDTLS_ECJPAKE_CLIENT;
280 else if( operation->role == PSA_PAKE_ROLE_SERVER )
281 role = MBEDTLS_ECJPAKE_SERVER;
282 else
283 return( PSA_ERROR_BAD_STATE );
284
285 if( psa_is_valid_key_id( operation->password, 1 ) == 0 )
286 return( PSA_ERROR_BAD_STATE );
287
288 status = psa_get_and_lock_key_slot( operation->password, &slot );
289 if( status != PSA_SUCCESS )
290 return( status );
291
292
293 ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake,
294 role,
295 MBEDTLS_MD_SHA256,
296 MBEDTLS_ECP_DP_SECP256R1,
297 slot->key.data, slot->key.bytes );
298
299 psa_unlock_key_slot( slot );
300 slot = NULL;
301
302 if( ret != 0 )
303 return( mbedtls_to_psa_error( ret ) );
304
305 operation->buffer = mbedtls_calloc( 1, 512 );
306 if( operation->buffer == NULL )
307 return( PSA_ERROR_INSUFFICIENT_MEMORY );
308
309 operation->state = PSA_PAKE_STATE_READY;
310
311 return( PSA_SUCCESS );
312}
313#endif
314
315psa_status_t psa_pake_output( psa_pake_operation_t *operation,
316 psa_pake_step_t step,
317 uint8_t *output,
318 size_t output_size,
319 size_t *output_length )
320{
321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
323 size_t length;
324
325 if( operation->alg == 0 ||
326 operation->state == PSA_PAKE_STATE_INVALID )
327 return( PSA_ERROR_BAD_STATE );
328
329 if( step != PSA_PAKE_STEP_KEY_SHARE &&
330 step != PSA_PAKE_STEP_ZK_PUBLIC &&
331 step != PSA_PAKE_STEP_ZK_PROOF )
332 return( PSA_ERROR_INVALID_ARGUMENT );
333
334#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
335 if( operation->state == PSA_PAKE_STATE_SETUP ) {
336 status = psa_pake_ecjpake_setup( operation );
337 if( status != PSA_SUCCESS )
338 {
339 psa_pake_abort( operation );
340 return( status );
341 }
342 }
343
344 if( operation->state >= PSA_PAKE_STATE_READY &&
345 ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 ||
346 operation->buffer == NULL ) )
347 {
348 return( PSA_ERROR_BAD_STATE );
349 }
350
351 if( operation->state != PSA_PAKE_STATE_READY &&
352 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
353 operation->state != PSA_PAKE_OUTPUT_X2S )
354 {
355 return( PSA_ERROR_BAD_STATE );
356 }
357
358 if( operation->state == PSA_PAKE_STATE_READY )
359 {
360 if( step != PSA_PAKE_STEP_KEY_SHARE )
361 return( PSA_ERROR_BAD_STATE );
362
363 switch( operation->output_step )
364 {
365 case PSA_PAKE_STEP_X1_X2:
366 operation->state = PSA_PAKE_OUTPUT_X1_X2;
367 break;
368 case PSA_PAKE_STEP_X2S:
369 operation->state = PSA_PAKE_OUTPUT_X2S;
370 break;
371 default:
372 return( PSA_ERROR_BAD_STATE );
373 }
374
375 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
376 }
377
378 /* Check if step matches current sequence */
379 switch( operation->sequence )
380 {
381 case PSA_PAKE_X1_STEP_KEY_SHARE:
382 case PSA_PAKE_X2_STEP_KEY_SHARE:
383 if( step != PSA_PAKE_STEP_KEY_SHARE )
384 return( PSA_ERROR_BAD_STATE );
385 break;
386
387 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
388 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
389 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
390 return( PSA_ERROR_BAD_STATE );
391 break;
392
393 case PSA_PAKE_X1_STEP_ZK_PROOF:
394 case PSA_PAKE_X2_STEP_ZK_PROOF:
395 if( step != PSA_PAKE_STEP_ZK_PROOF )
396 return( PSA_ERROR_BAD_STATE );
397 break;
398
399 default:
400 return( PSA_ERROR_BAD_STATE );
401 }
402
403 /* Initialize & write round on KEY_SHARE sequences */
404 if( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
405 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
406 {
407 ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake,
408 operation->buffer,
409 PSA_PAKE_BUFFER_SIZE,
410 &operation->buffer_length,
411 mbedtls_psa_get_random,
412 MBEDTLS_PSA_RANDOM_STATE );
413 if( ret != 0 )
414 {
415 psa_pake_abort( operation );
416 return( mbedtls_to_psa_error( ret ) );
417 }
418
419 operation->buffer_offset = 0;
420 }
421 else if( operation->state == PSA_PAKE_OUTPUT_X2S &&
422 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
423 {
424 ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake,
425 operation->buffer,
426 PSA_PAKE_BUFFER_SIZE,
427 &operation->buffer_length,
428 mbedtls_psa_get_random,
429 MBEDTLS_PSA_RANDOM_STATE );
430 if( ret != 0 )
431 {
432 psa_pake_abort( operation );
433 return( mbedtls_to_psa_error( ret ) );
434 }
435
436 operation->buffer_offset = 0;
437 }
438
439 /* Load output sequence length */
440 if( operation->state == PSA_PAKE_OUTPUT_X2S &&
441 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
442 {
443 if( operation->role == PSA_PAKE_ROLE_SERVER )
444 /* Length is stored after 3bytes curve */
445 length = 3 + operation->buffer[3] + 1;
446 else
447 /* Length is stored at the first byte */
448 length = operation->buffer[0] + 1;
449 }
450 else
451 {
Neil Armstrongc29f8472022-06-08 13:34:49 +0200452 /* Length is stored at the first byte of the next chunk */
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200453 length = operation->buffer[operation->buffer_offset] + 1;
454 }
455
456 if( length > operation->buffer_length )
457 return( PSA_ERROR_DATA_CORRUPT );
458
459 if( output_size < length )
460 {
461 psa_pake_abort( operation );
462 return( PSA_ERROR_BUFFER_TOO_SMALL );
463 }
464
465 memcpy( output,
466 operation->buffer + operation->buffer_offset,
467 length );
468 *output_length = length;
469
470 operation->buffer_offset += length;
471
472 /* Reset buffer after ZK_PROOF sequence */
473 if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
474 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
475 ( operation->state == PSA_PAKE_OUTPUT_X2S &&
476 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
477 {
478 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
479 operation->buffer_length = 0;
480 operation->buffer_offset = 0;
481
482 operation->state = PSA_PAKE_STATE_READY;
483 operation->output_step++;
484 operation->sequence = 0;
485 }
486 else
487 {
488 operation->sequence++;
489 }
490
491 return( PSA_SUCCESS );
492#else
493 return( PSA_ERROR_NOT_SUPPORTED );
494#endif
495}
496
497psa_status_t psa_pake_input( psa_pake_operation_t *operation,
498 psa_pake_step_t step,
499 const uint8_t *input,
500 size_t input_length )
501{
502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
503 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
504 size_t buffer_remain;
505
506 if( operation->alg == 0 ||
507 operation->state == PSA_PAKE_STATE_INVALID )
508 return( PSA_ERROR_BAD_STATE );
509
510 if( step != PSA_PAKE_STEP_KEY_SHARE &&
511 step != PSA_PAKE_STEP_ZK_PUBLIC &&
512 step != PSA_PAKE_STEP_ZK_PROOF )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514
515#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
516 if( operation->state == PSA_PAKE_STATE_SETUP ) {
517 status = psa_pake_ecjpake_setup( operation );
518 if( status != PSA_SUCCESS )
519 {
520 psa_pake_abort( operation );
521 return( status );
522 }
523 }
524
525 if( operation->state >= PSA_PAKE_STATE_READY &&
526 ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 ||
527 operation->buffer == NULL ) )
528 {
529 return( PSA_ERROR_BAD_STATE );
530 }
531
532 if( operation->state != PSA_PAKE_STATE_READY &&
533 operation->state != PSA_PAKE_INPUT_X1_X2 &&
534 operation->state != PSA_PAKE_INPUT_X4S )
535 {
536 return( PSA_ERROR_BAD_STATE );
537 }
538
539 if( operation->state == PSA_PAKE_STATE_READY )
540 {
541 if( step != PSA_PAKE_STEP_KEY_SHARE )
542 return( PSA_ERROR_BAD_STATE );
543
544 switch( operation->input_step )
545 {
546 case PSA_PAKE_STEP_X1_X2:
547 operation->state = PSA_PAKE_INPUT_X1_X2;
548 break;
549 case PSA_PAKE_STEP_X2S:
550 operation->state = PSA_PAKE_INPUT_X4S;
551 break;
552 default:
553 return( PSA_ERROR_BAD_STATE );
554 }
555
556 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
557 }
558
559 buffer_remain = PSA_PAKE_BUFFER_SIZE - operation->buffer_length;
560
561 if( input_length == 0 ||
562 input_length > buffer_remain )
563 {
564 psa_pake_abort( operation );
565 return( PSA_ERROR_INSUFFICIENT_MEMORY );
566 }
567
568 /* Check if step matches current sequence */
569 switch( operation->sequence )
570 {
571 case PSA_PAKE_X1_STEP_KEY_SHARE:
572 case PSA_PAKE_X2_STEP_KEY_SHARE:
573 if( step != PSA_PAKE_STEP_KEY_SHARE )
574 return( PSA_ERROR_BAD_STATE );
575 break;
576
577 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
578 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
579 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
580 return( PSA_ERROR_BAD_STATE );
581 break;
582
583 case PSA_PAKE_X1_STEP_ZK_PROOF:
584 case PSA_PAKE_X2_STEP_ZK_PROOF:
585 if( step != PSA_PAKE_STEP_ZK_PROOF )
586 return( PSA_ERROR_BAD_STATE );
587 break;
588
589 default:
590 return( PSA_ERROR_BAD_STATE );
591 }
592
593 /* Copy input to local buffer */
594 memcpy( operation->buffer + operation->buffer_length,
595 input, input_length );
596 operation->buffer_length += input_length;
597
598 /* Load buffer at each last round ZK_PROOF */
599 if( operation->state == PSA_PAKE_INPUT_X1_X2 &&
600 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF )
601 {
602 ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake,
603 operation->buffer,
604 operation->buffer_length );
605
606 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
607 operation->buffer_length = 0;
608
609 if( ret != 0 )
610 {
611 psa_pake_abort( operation );
612 return( mbedtls_to_psa_error( ret ) );
613 }
614 }
615 else if( operation->state == PSA_PAKE_INPUT_X4S &&
616 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF )
617 {
618 ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake,
619 operation->buffer,
620 operation->buffer_length );
621
622 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
623 operation->buffer_length = 0;
624
625 if( ret != 0 )
626 {
627 psa_pake_abort( operation );
628 return( mbedtls_to_psa_error( ret ) );
629 }
630 }
631
632 if( ( operation->state == PSA_PAKE_INPUT_X1_X2 &&
633 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
634 ( operation->state == PSA_PAKE_INPUT_X4S &&
635 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
636 {
637 operation->state = PSA_PAKE_STATE_READY;
638 operation->input_step++;
639 operation->sequence = 0;
640 }
641 else
642 {
643 operation->sequence++;
644 }
645
646 return( PSA_SUCCESS );
647#else
648 return( PSA_ERROR_NOT_SUPPORTED );
649#endif
650}
651
652psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
653 psa_key_derivation_operation_t *output)
654{
655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
656 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
657
658 if( operation->alg == 0 ||
659 operation->state != PSA_PAKE_STATE_READY ||
660 ( operation->input_step != PSA_PAKE_STEP_DERIVE &&
661 operation->output_step != PSA_PAKE_STEP_DERIVE ) )
662 return( PSA_ERROR_BAD_STATE );
663
664#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
665 ret = mbedtls_ecjpake_derive_secret( &operation->ctx.ecjpake,
666 operation->buffer,
667 PSA_PAKE_BUFFER_SIZE,
668 &operation->buffer_length,
669 mbedtls_psa_get_random,
670 MBEDTLS_PSA_RANDOM_STATE );
671 if( ret != 0)
672 {
673 psa_pake_abort( operation );
674 return( mbedtls_to_psa_error( ret ) );
675 }
676
677 status = psa_key_derivation_input_bytes( output,
678 PSA_KEY_DERIVATION_INPUT_SECRET,
679 operation->buffer,
680 operation->buffer_length );
681
682 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
683
684 psa_pake_abort( operation );
685
686 return( status );
687#else
688 return( PSA_ERROR_NOT_SUPPORTED );
689#endif
690}
691
692psa_status_t psa_pake_abort(psa_pake_operation_t * operation)
693{
694 if( operation->alg == 0 )
695 {
696 return( PSA_SUCCESS );
697 }
698
699 operation->alg = 0;
700 operation->state = 0;
701 operation->sequence = 0;
702
703#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE)
704 operation->input_step = 0;
705 operation->output_step = 0;
706 operation->password = MBEDTLS_SVC_KEY_ID_INIT;
707 operation->role = 0;
708 mbedtls_free( operation->buffer );
709 operation->buffer = NULL;
710 operation->buffer_length = 0;
711 operation->buffer_offset = 0;
712 mbedtls_ecjpake_free( &operation->ctx.ecjpake );
713#endif
714
715 return( PSA_SUCCESS );
716}
717
718#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
719
720#endif /* MBEDTLS_PSA_CRYPTO_C */