blob: 1fd91290e628c2049ccbe80548c7bdfd791355ed [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 Armstrong5fb07c62022-06-10 09:00:00 +0200201 if( operation->alg != 0 )
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
Neil Armstrongecb221b2022-09-08 11:21:07 +0200233 mbedtls_platform_zeroize( operation->buffer, 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
252 if( operation->alg == 0 ||
253 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{
285 if( operation->alg == 0 ||
286 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{
301 if( operation->alg == 0 ||
302 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{
316 if( operation->alg == 0 ||
317 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
388psa_status_t psa_pake_output( psa_pake_operation_t *operation,
389 psa_pake_step_t step,
390 uint8_t *output,
391 size_t output_size,
392 size_t *output_length )
393{
394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
395 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
396 size_t length;
397
398 if( operation->alg == 0 ||
399 operation->state == PSA_PAKE_STATE_INVALID )
400 return( PSA_ERROR_BAD_STATE );
401
402 if( step != PSA_PAKE_STEP_KEY_SHARE &&
403 step != PSA_PAKE_STEP_ZK_PUBLIC &&
404 step != PSA_PAKE_STEP_ZK_PROOF )
405 return( PSA_ERROR_INVALID_ARGUMENT );
406
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200407 if( output == NULL || output_size == 0 || output_length == NULL )
408 return( PSA_ERROR_INVALID_ARGUMENT );
409
Neil Armstronga557cb82022-06-10 08:58:32 +0200410#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200411 if( operation->alg == PSA_ALG_JPAKE )
412 {
413 if( operation->state == PSA_PAKE_STATE_SETUP ) {
414 status = psa_pake_ecjpake_setup( operation );
415 if( status != PSA_SUCCESS )
416 {
417 psa_pake_abort( operation );
418 return( status );
419 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200420 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200421
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200422 if( operation->state >= PSA_PAKE_STATE_READY &&
Neil Armstrongecb221b2022-09-08 11:21:07 +0200423 mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200424 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200425 return( PSA_ERROR_BAD_STATE );
426 }
427
428 if( operation->state != PSA_PAKE_STATE_READY &&
429 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
430 operation->state != PSA_PAKE_OUTPUT_X2S )
431 {
432 return( PSA_ERROR_BAD_STATE );
433 }
434
435 if( operation->state == PSA_PAKE_STATE_READY )
436 {
437 if( step != PSA_PAKE_STEP_KEY_SHARE )
438 return( PSA_ERROR_BAD_STATE );
439
440 switch( operation->output_step )
441 {
442 case PSA_PAKE_STEP_X1_X2:
443 operation->state = PSA_PAKE_OUTPUT_X1_X2;
444 break;
445 case PSA_PAKE_STEP_X2S:
446 operation->state = PSA_PAKE_OUTPUT_X2S;
447 break;
448 default:
449 return( PSA_ERROR_BAD_STATE );
450 }
451
452 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
453 }
454
455 /* Check if step matches current sequence */
456 switch( operation->sequence )
457 {
458 case PSA_PAKE_X1_STEP_KEY_SHARE:
459 case PSA_PAKE_X2_STEP_KEY_SHARE:
460 if( step != PSA_PAKE_STEP_KEY_SHARE )
461 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200462 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200463
464 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
465 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
466 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
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_PROOF:
471 case PSA_PAKE_X2_STEP_ZK_PROOF:
472 if( step != PSA_PAKE_STEP_ZK_PROOF )
473 return( PSA_ERROR_BAD_STATE );
474 break;
475
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200476 default:
477 return( PSA_ERROR_BAD_STATE );
478 }
479
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200480 /* Initialize & write round on KEY_SHARE sequences */
481 if( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
482 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200483 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200484 ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake,
485 operation->buffer,
486 PSA_PAKE_BUFFER_SIZE,
487 &operation->buffer_length,
488 mbedtls_psa_get_random,
489 MBEDTLS_PSA_RANDOM_STATE );
490 if( ret != 0 )
491 {
492 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200493 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200494 }
495
496 operation->buffer_offset = 0;
497 }
498 else if( operation->state == PSA_PAKE_OUTPUT_X2S &&
499 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
500 {
501 ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake,
502 operation->buffer,
503 PSA_PAKE_BUFFER_SIZE,
504 &operation->buffer_length,
505 mbedtls_psa_get_random,
506 MBEDTLS_PSA_RANDOM_STATE );
507 if( ret != 0 )
508 {
509 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200510 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200511 }
512
513 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200514 }
515
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200516 /* Load output sequence length */
517 if( operation->state == PSA_PAKE_OUTPUT_X2S &&
518 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200519 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200520 if( operation->role == PSA_PAKE_ROLE_SERVER )
521 /* Length is stored after 3bytes curve */
522 length = 3 + operation->buffer[3] + 1;
523 else
524 /* Length is stored at the first byte */
525 length = operation->buffer[0] + 1;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200526 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200527 else
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200528 /* Length is stored at the first byte of the next chunk */
529 length = operation->buffer[operation->buffer_offset] + 1;
530
531 if( length > operation->buffer_length )
532 return( PSA_ERROR_DATA_CORRUPT );
533
534 if( output_size < length )
535 {
536 psa_pake_abort( operation );
537 return( PSA_ERROR_BUFFER_TOO_SMALL );
538 }
539
540 memcpy( output,
541 operation->buffer + operation->buffer_offset,
542 length );
543 *output_length = length;
544
545 operation->buffer_offset += length;
546
547 /* Reset buffer after ZK_PROOF sequence */
548 if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
549 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
550 ( operation->state == PSA_PAKE_OUTPUT_X2S &&
551 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
552 {
553 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
554 operation->buffer_length = 0;
555 operation->buffer_offset = 0;
556
557 operation->state = PSA_PAKE_STATE_READY;
558 operation->output_step++;
559 operation->sequence = 0;
560 }
561 else
562 operation->sequence++;
563
564 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200565 }
566 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200567#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200568 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200569}
570
571psa_status_t psa_pake_input( psa_pake_operation_t *operation,
572 psa_pake_step_t step,
573 const uint8_t *input,
574 size_t input_length )
575{
576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
577 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
578 size_t buffer_remain;
579
580 if( operation->alg == 0 ||
581 operation->state == PSA_PAKE_STATE_INVALID )
582 return( PSA_ERROR_BAD_STATE );
583
584 if( step != PSA_PAKE_STEP_KEY_SHARE &&
585 step != PSA_PAKE_STEP_ZK_PUBLIC &&
586 step != PSA_PAKE_STEP_ZK_PROOF )
587 return( PSA_ERROR_INVALID_ARGUMENT );
588
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200589 if( input == NULL || input_length == 0 )
590 return( PSA_ERROR_INVALID_ARGUMENT );
591
Neil Armstronga557cb82022-06-10 08:58:32 +0200592#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200593 if( operation->alg == PSA_ALG_JPAKE )
594 {
Neil Armstrong5bbdb702022-09-05 17:54:15 +0200595 if( operation->state == PSA_PAKE_STATE_SETUP )
596 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200597 status = psa_pake_ecjpake_setup( operation );
598 if( status != PSA_SUCCESS )
599 {
600 psa_pake_abort( operation );
601 return( status );
602 }
603 }
604
605 if( operation->state >= PSA_PAKE_STATE_READY &&
Neil Armstrongecb221b2022-09-08 11:21:07 +0200606 mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200607 {
608 return( PSA_ERROR_BAD_STATE );
609 }
610
611 if( operation->state != PSA_PAKE_STATE_READY &&
612 operation->state != PSA_PAKE_INPUT_X1_X2 &&
613 operation->state != PSA_PAKE_INPUT_X4S )
614 {
615 return( PSA_ERROR_BAD_STATE );
616 }
617
618 if( operation->state == PSA_PAKE_STATE_READY )
619 {
620 if( step != PSA_PAKE_STEP_KEY_SHARE )
621 return( PSA_ERROR_BAD_STATE );
622
623 switch( operation->input_step )
624 {
625 case PSA_PAKE_STEP_X1_X2:
626 operation->state = PSA_PAKE_INPUT_X1_X2;
627 break;
628 case PSA_PAKE_STEP_X2S:
629 operation->state = PSA_PAKE_INPUT_X4S;
630 break;
631 default:
632 return( PSA_ERROR_BAD_STATE );
633 }
634
635 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
636 }
637
638 buffer_remain = PSA_PAKE_BUFFER_SIZE - operation->buffer_length;
639
640 if( input_length == 0 ||
641 input_length > buffer_remain )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200642 {
643 psa_pake_abort( operation );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200644 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200645 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200646
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200647 /* Check if step matches current sequence */
648 switch( operation->sequence )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200649 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200650 case PSA_PAKE_X1_STEP_KEY_SHARE:
651 case PSA_PAKE_X2_STEP_KEY_SHARE:
652 if( step != PSA_PAKE_STEP_KEY_SHARE )
653 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200654 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200655
656 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
657 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
658 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
659 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200660 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200661
662 case PSA_PAKE_X1_STEP_ZK_PROOF:
663 case PSA_PAKE_X2_STEP_ZK_PROOF:
664 if( step != PSA_PAKE_STEP_ZK_PROOF )
665 return( PSA_ERROR_BAD_STATE );
666 break;
667
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200668 default:
669 return( PSA_ERROR_BAD_STATE );
670 }
671
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200672 /* Copy input to local buffer */
673 memcpy( operation->buffer + operation->buffer_length,
674 input, input_length );
675 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200676
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200677 /* Load buffer at each last round ZK_PROOF */
678 if( operation->state == PSA_PAKE_INPUT_X1_X2 &&
679 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200680 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200681 ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake,
682 operation->buffer,
683 operation->buffer_length );
684
685 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
686 operation->buffer_length = 0;
687
688 if( ret != 0 )
689 {
690 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200691 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200692 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200693 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200694 else if( operation->state == PSA_PAKE_INPUT_X4S &&
695 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200696 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200697 ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake,
698 operation->buffer,
699 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200700
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200701 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
702 operation->buffer_length = 0;
703
704 if( ret != 0 )
705 {
706 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200707 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200708 }
709 }
710
711 if( ( operation->state == PSA_PAKE_INPUT_X1_X2 &&
712 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
713 ( operation->state == PSA_PAKE_INPUT_X4S &&
714 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
715 {
716 operation->state = PSA_PAKE_STATE_READY;
717 operation->input_step++;
718 operation->sequence = 0;
719 }
720 else
721 operation->sequence++;
722
723 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200724 }
725 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200726#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200727 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200728}
729
730psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
731 psa_key_derivation_operation_t *output)
732{
733 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
734 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
735
736 if( operation->alg == 0 ||
737 operation->state != PSA_PAKE_STATE_READY ||
Neil Armstrong1e855602022-06-15 11:32:11 +0200738 operation->input_step != PSA_PAKE_STEP_DERIVE ||
739 operation->output_step != PSA_PAKE_STEP_DERIVE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200740 return( PSA_ERROR_BAD_STATE );
741
Neil Armstronga557cb82022-06-10 08:58:32 +0200742#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200743 if( operation->alg == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200744 {
Neil Armstrongf19a3cb2022-06-15 16:00:29 +0200745 ret = mbedtls_ecjpake_write_shared_key( &operation->ctx.ecjpake,
746 operation->buffer,
747 PSA_PAKE_BUFFER_SIZE,
748 &operation->buffer_length,
749 mbedtls_psa_get_random,
750 MBEDTLS_PSA_RANDOM_STATE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200751 if( ret != 0)
752 {
753 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200754 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200755 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200756
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200757 status = psa_key_derivation_input_bytes( output,
758 PSA_KEY_DERIVATION_INPUT_SECRET,
759 operation->buffer,
760 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200761
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200762 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200763
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200764 psa_pake_abort( operation );
765
766 return( status );
767 }
768 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200769#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200770 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200771}
772
773psa_status_t psa_pake_abort(psa_pake_operation_t * operation)
774{
775 if( operation->alg == 0 )
776 {
777 return( PSA_SUCCESS );
778 }
779
Neil Armstronga557cb82022-06-10 08:58:32 +0200780#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200781 if( operation->alg == PSA_ALG_JPAKE )
782 {
783 operation->input_step = 0;
784 operation->output_step = 0;
785 operation->password = MBEDTLS_SVC_KEY_ID_INIT;
786 operation->role = 0;
Neil Armstrongecb221b2022-09-08 11:21:07 +0200787 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200788 operation->buffer_length = 0;
789 operation->buffer_offset = 0;
790 mbedtls_ecjpake_free( &operation->ctx.ecjpake );
791 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200792#endif
793
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200794 operation->alg = 0;
795 operation->state = 0;
796 operation->sequence = 0;
797
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200798 return( PSA_SUCCESS );
799}
800
801#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
802
803#endif /* MBEDTLS_PSA_CRYPTO_C */