blob: 519825084b3ad51d09104aaea03e0cfcc86e677a [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
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200104/*
105 * The first PAKE step shares the same sequences of the second PAKE step
106 * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
Neil Armstrongb39833c2022-09-06 11:36:02 +0200107 * It's simpler to share the same sequences numbers of the first
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200108 * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
109 *
110 * State sequence with step, state & sequence enums:
111 * => Input & Output Step = PSA_PAKE_STEP_INVALID
112 * => state = PSA_PAKE_STATE_INVALID
113 * psa_pake_setup()
114 * => Input & Output Step = PSA_PAKE_STEP_X1_X2
115 * => state = PSA_PAKE_STATE_SETUP
116 * => sequence = PSA_PAKE_SEQ_INVALID
117 * |
118 * |--- In any order: (First round input before or after first round output)
119 * | | First call of psa_pake_output() or psa_pake_input() sets
120 * | | state = PSA_PAKE_STATE_READY
121 * | |
122 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
123 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
124 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
125 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
126 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
127 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
128 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
129 * | | | => state = PSA_PAKE_STATE_READY
130 * | | | => sequence = PSA_PAKE_SEQ_INVALID
131 * | | \ => Output Step = PSA_PAKE_STEP_X2S
132 * | |
133 * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
134 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
135 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
136 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
137 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
138 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
139 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
140 * | | | => state = PSA_PAKE_STATE_READY
141 * | | | => sequence = PSA_PAKE_SEQ_INVALID
142 * | | \ => Output Step = PSA_PAKE_INPUT_X4S
143 * |
144 * |--- In any order: (Second round input before or after second round output)
145 * | |
146 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
147 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
148 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
149 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
150 * | | | => state = PSA_PAKE_STATE_READY
151 * | | | => sequence = PSA_PAKE_SEQ_INVALID
152 * | | \ => Output Step = PSA_PAKE_STEP_DERIVE
153 * | |
154 * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
155 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
156 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
157 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
158 * | | | => state = PSA_PAKE_STATE_READY
159 * | | | => sequence = PSA_PAKE_SEQ_INVALID
160 * | | \ => Output Step = PSA_PAKE_STEP_DERIVE
161 * |
162 * psa_pake_get_implicit_key()
163 * => Input & Output Step = PSA_PAKE_STEP_INVALID
164 */
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200165enum psa_pake_sequence
166{
167 PSA_PAKE_SEQ_INVALID = 0,
168 PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
169 PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
170 PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
171 PSA_PAKE_X2_STEP_KEY_SHARE = 4,
172 PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
173 PSA_PAKE_X2_STEP_ZK_PROOF = 6,
174 PSA_PAKE_SEQ_END = 7,
175};
176
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200177#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
178static psa_status_t mbedtls_ecjpake_to_psa_error( int ret )
179{
180 switch( ret )
181 {
182 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
183 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
184 case MBEDTLS_ERR_ECP_INVALID_KEY:
185 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
186 return( PSA_ERROR_DATA_INVALID );
187 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
188 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
189 return( PSA_ERROR_BUFFER_TOO_SMALL );
190 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
191 return( PSA_ERROR_NOT_SUPPORTED );
192 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
193 return( PSA_ERROR_CORRUPTION_DETECTED );
194 default:
195 return( PSA_ERROR_GENERIC_ERROR );
196 }
197}
198#endif
199
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200200#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
201psa_status_t psa_pake_setup( psa_pake_operation_t *operation,
202 const psa_pake_cipher_suite_t *cipher_suite)
203{
204 /* A context must be freshly initialized before it can be set up. */
Neil Armstrong5fb07c62022-06-10 09:00:00 +0200205 if( operation->alg != 0 )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200206 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200207
208 if( cipher_suite == NULL ||
209 PSA_ALG_IS_PAKE(cipher_suite->algorithm ) == 0 ||
210 ( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC &&
211 cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH ) ||
212 PSA_ALG_IS_HASH( cipher_suite->hash ) == 0 )
213 {
214 return( PSA_ERROR_INVALID_ARGUMENT );
215 }
216
Neil Armstronga557cb82022-06-10 08:58:32 +0200217#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200218 if( cipher_suite->algorithm == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200219 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200220 if( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
221 cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
222 cipher_suite->bits != 256 ||
223 cipher_suite->hash != PSA_ALG_SHA_256 )
224 {
225 return( PSA_ERROR_NOT_SUPPORTED );
226 }
227
228 operation->alg = cipher_suite->algorithm;
229
230 mbedtls_ecjpake_init( &operation->ctx.ecjpake );
231
232 operation->state = PSA_PAKE_STATE_SETUP;
233 operation->sequence = PSA_PAKE_SEQ_INVALID;
234 operation->input_step = PSA_PAKE_STEP_X1_X2;
235 operation->output_step = PSA_PAKE_STEP_X1_X2;
236
237 operation->buffer = NULL;
238 operation->buffer_length = 0;
239 operation->buffer_offset = 0;
240
241 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200242 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200243 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200244#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200245 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200246}
247
248psa_status_t psa_pake_set_password_key( psa_pake_operation_t *operation,
249 mbedtls_svc_key_id_t password )
250{
251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
252 psa_key_attributes_t attributes = psa_key_attributes_init();
253 psa_key_type_t type;
254 psa_key_usage_t usage;
255
256 if( operation->alg == 0 ||
257 operation->state != PSA_PAKE_STATE_SETUP )
258 {
259 return( PSA_ERROR_BAD_STATE );
260 }
261
262 status = psa_get_key_attributes( password, &attributes );
263 if( status != PSA_SUCCESS )
Neil Armstronge9231112022-06-10 09:03:41 +0200264 return( status );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200265
266 type = psa_get_key_type( &attributes );
267 usage = psa_get_key_usage_flags( &attributes );
268
269 psa_reset_key_attributes( &attributes );
270
271 if( type != PSA_KEY_TYPE_PASSWORD &&
272 type != PSA_KEY_TYPE_PASSWORD_HASH )
273 {
Neil Armstronge9231112022-06-10 09:03:41 +0200274 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200275 }
276
Neil Armstrongdf598ab2022-06-08 17:17:08 +0200277 if( ( usage & PSA_KEY_USAGE_DERIVE ) == 0 )
Neil Armstronge9231112022-06-10 09:03:41 +0200278 return( PSA_ERROR_NOT_PERMITTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200279
280 operation->password = password;
281
282 return( PSA_SUCCESS );
283}
284
285psa_status_t psa_pake_set_user( psa_pake_operation_t *operation,
286 const uint8_t *user_id,
287 size_t user_id_len )
288{
289 if( operation->alg == 0 ||
290 operation->state != PSA_PAKE_STATE_SETUP )
291 {
292 return( PSA_ERROR_BAD_STATE );
293 }
294
295 if( user_id_len == 0 || user_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200296 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200297
298 return( PSA_ERROR_NOT_SUPPORTED );
299}
300
301psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation,
302 const uint8_t *peer_id,
303 size_t peer_id_len )
304{
305 if( operation->alg == 0 ||
306 operation->state != PSA_PAKE_STATE_SETUP )
307 {
308 return( PSA_ERROR_BAD_STATE );
309 }
310
311 if( peer_id_len == 0 || peer_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200312 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200313
314 return( PSA_ERROR_NOT_SUPPORTED );
315}
316
317psa_status_t psa_pake_set_role( psa_pake_operation_t *operation,
318 psa_pake_role_t role )
319{
320 if( operation->alg == 0 ||
321 operation->state != PSA_PAKE_STATE_SETUP )
322 {
323 return( PSA_ERROR_BAD_STATE );
324 }
325
326 if( role != PSA_PAKE_ROLE_NONE &&
327 role != PSA_PAKE_ROLE_FIRST &&
328 role != PSA_PAKE_ROLE_SECOND &&
329 role != PSA_PAKE_ROLE_CLIENT &&
330 role != PSA_PAKE_ROLE_SERVER )
331 {
Neil Armstronge9231112022-06-10 09:03:41 +0200332 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200333 }
334
Neil Armstronga557cb82022-06-10 08:58:32 +0200335#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200336 if( operation->alg == PSA_ALG_JPAKE )
337 {
338 if( role != PSA_PAKE_ROLE_CLIENT &&
339 role != PSA_PAKE_ROLE_SERVER )
Neil Armstronge9231112022-06-10 09:03:41 +0200340 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200341
342 operation->role = role;
343
344 return( PSA_SUCCESS );
345 }
346 else
347#endif
348 return( PSA_ERROR_NOT_SUPPORTED );
349}
350
Neil Armstronga557cb82022-06-10 08:58:32 +0200351#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200352static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation )
353{
354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
356 mbedtls_ecjpake_role role;
357 psa_key_slot_t *slot = NULL;
358
359 if( operation->role == PSA_PAKE_ROLE_CLIENT )
360 role = MBEDTLS_ECJPAKE_CLIENT;
361 else if( operation->role == PSA_PAKE_ROLE_SERVER )
362 role = MBEDTLS_ECJPAKE_SERVER;
363 else
364 return( PSA_ERROR_BAD_STATE );
365
366 if( psa_is_valid_key_id( operation->password, 1 ) == 0 )
367 return( PSA_ERROR_BAD_STATE );
368
369 status = psa_get_and_lock_key_slot( operation->password, &slot );
370 if( status != PSA_SUCCESS )
371 return( status );
372
373
374 ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake,
375 role,
376 MBEDTLS_MD_SHA256,
377 MBEDTLS_ECP_DP_SECP256R1,
378 slot->key.data, slot->key.bytes );
379
380 psa_unlock_key_slot( slot );
381 slot = NULL;
382
383 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200384 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200385
Neil Armstrong6b1f99f2022-06-08 13:37:37 +0200386 operation->buffer = mbedtls_calloc( 1, PSA_PAKE_BUFFER_SIZE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200387 if( operation->buffer == NULL )
388 return( PSA_ERROR_INSUFFICIENT_MEMORY );
389
390 operation->state = PSA_PAKE_STATE_READY;
391
392 return( PSA_SUCCESS );
393}
394#endif
395
396psa_status_t psa_pake_output( psa_pake_operation_t *operation,
397 psa_pake_step_t step,
398 uint8_t *output,
399 size_t output_size,
400 size_t *output_length )
401{
402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
404 size_t length;
405
406 if( operation->alg == 0 ||
407 operation->state == PSA_PAKE_STATE_INVALID )
408 return( PSA_ERROR_BAD_STATE );
409
410 if( step != PSA_PAKE_STEP_KEY_SHARE &&
411 step != PSA_PAKE_STEP_ZK_PUBLIC &&
412 step != PSA_PAKE_STEP_ZK_PROOF )
413 return( PSA_ERROR_INVALID_ARGUMENT );
414
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200415 if( output == NULL || output_size == 0 || output_length == NULL )
416 return( PSA_ERROR_INVALID_ARGUMENT );
417
Neil Armstronga557cb82022-06-10 08:58:32 +0200418#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200419 if( operation->alg == PSA_ALG_JPAKE )
420 {
421 if( operation->state == PSA_PAKE_STATE_SETUP ) {
422 status = psa_pake_ecjpake_setup( operation );
423 if( status != PSA_SUCCESS )
424 {
425 psa_pake_abort( operation );
426 return( status );
427 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200428 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200429
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200430 if( operation->state >= PSA_PAKE_STATE_READY &&
431 ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 ||
432 operation->buffer == NULL ) )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200433 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200434 return( PSA_ERROR_BAD_STATE );
435 }
436
437 if( operation->state != PSA_PAKE_STATE_READY &&
438 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
439 operation->state != PSA_PAKE_OUTPUT_X2S )
440 {
441 return( PSA_ERROR_BAD_STATE );
442 }
443
444 if( operation->state == PSA_PAKE_STATE_READY )
445 {
446 if( step != PSA_PAKE_STEP_KEY_SHARE )
447 return( PSA_ERROR_BAD_STATE );
448
449 switch( operation->output_step )
450 {
451 case PSA_PAKE_STEP_X1_X2:
452 operation->state = PSA_PAKE_OUTPUT_X1_X2;
453 break;
454 case PSA_PAKE_STEP_X2S:
455 operation->state = PSA_PAKE_OUTPUT_X2S;
456 break;
457 default:
458 return( PSA_ERROR_BAD_STATE );
459 }
460
461 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
462 }
463
464 /* Check if step matches current sequence */
465 switch( operation->sequence )
466 {
467 case PSA_PAKE_X1_STEP_KEY_SHARE:
468 case PSA_PAKE_X2_STEP_KEY_SHARE:
469 if( step != PSA_PAKE_STEP_KEY_SHARE )
470 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200471 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200472
473 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
474 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
475 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
476 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200477 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200478
479 case PSA_PAKE_X1_STEP_ZK_PROOF:
480 case PSA_PAKE_X2_STEP_ZK_PROOF:
481 if( step != PSA_PAKE_STEP_ZK_PROOF )
482 return( PSA_ERROR_BAD_STATE );
483 break;
484
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200485 default:
486 return( PSA_ERROR_BAD_STATE );
487 }
488
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200489 /* Initialize & write round on KEY_SHARE sequences */
490 if( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
491 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200492 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200493 ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake,
494 operation->buffer,
495 PSA_PAKE_BUFFER_SIZE,
496 &operation->buffer_length,
497 mbedtls_psa_get_random,
498 MBEDTLS_PSA_RANDOM_STATE );
499 if( ret != 0 )
500 {
501 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200502 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200503 }
504
505 operation->buffer_offset = 0;
506 }
507 else if( operation->state == PSA_PAKE_OUTPUT_X2S &&
508 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
509 {
510 ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake,
511 operation->buffer,
512 PSA_PAKE_BUFFER_SIZE,
513 &operation->buffer_length,
514 mbedtls_psa_get_random,
515 MBEDTLS_PSA_RANDOM_STATE );
516 if( ret != 0 )
517 {
518 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200519 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200520 }
521
522 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200523 }
524
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200525 /* Load output sequence length */
526 if( operation->state == PSA_PAKE_OUTPUT_X2S &&
527 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200528 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200529 if( operation->role == PSA_PAKE_ROLE_SERVER )
530 /* Length is stored after 3bytes curve */
531 length = 3 + operation->buffer[3] + 1;
532 else
533 /* Length is stored at the first byte */
534 length = operation->buffer[0] + 1;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200535 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200536 else
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200537 /* Length is stored at the first byte of the next chunk */
538 length = operation->buffer[operation->buffer_offset] + 1;
539
540 if( length > operation->buffer_length )
541 return( PSA_ERROR_DATA_CORRUPT );
542
543 if( output_size < length )
544 {
545 psa_pake_abort( operation );
546 return( PSA_ERROR_BUFFER_TOO_SMALL );
547 }
548
549 memcpy( output,
550 operation->buffer + operation->buffer_offset,
551 length );
552 *output_length = length;
553
554 operation->buffer_offset += length;
555
556 /* Reset buffer after ZK_PROOF sequence */
557 if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
558 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
559 ( operation->state == PSA_PAKE_OUTPUT_X2S &&
560 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
561 {
562 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
563 operation->buffer_length = 0;
564 operation->buffer_offset = 0;
565
566 operation->state = PSA_PAKE_STATE_READY;
567 operation->output_step++;
568 operation->sequence = 0;
569 }
570 else
571 operation->sequence++;
572
573 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200574 }
575 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200576#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200577 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200578}
579
580psa_status_t psa_pake_input( psa_pake_operation_t *operation,
581 psa_pake_step_t step,
582 const uint8_t *input,
583 size_t input_length )
584{
585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
586 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
587 size_t buffer_remain;
588
589 if( operation->alg == 0 ||
590 operation->state == PSA_PAKE_STATE_INVALID )
591 return( PSA_ERROR_BAD_STATE );
592
593 if( step != PSA_PAKE_STEP_KEY_SHARE &&
594 step != PSA_PAKE_STEP_ZK_PUBLIC &&
595 step != PSA_PAKE_STEP_ZK_PROOF )
596 return( PSA_ERROR_INVALID_ARGUMENT );
597
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200598 if( input == NULL || input_length == 0 )
599 return( PSA_ERROR_INVALID_ARGUMENT );
600
Neil Armstronga557cb82022-06-10 08:58:32 +0200601#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200602 if( operation->alg == PSA_ALG_JPAKE )
603 {
Neil Armstrong5bbdb702022-09-05 17:54:15 +0200604 if( operation->state == PSA_PAKE_STATE_SETUP )
605 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200606 status = psa_pake_ecjpake_setup( operation );
607 if( status != PSA_SUCCESS )
608 {
609 psa_pake_abort( operation );
610 return( status );
611 }
612 }
613
614 if( operation->state >= PSA_PAKE_STATE_READY &&
615 ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 ||
616 operation->buffer == NULL ) )
617 {
618 return( PSA_ERROR_BAD_STATE );
619 }
620
621 if( operation->state != PSA_PAKE_STATE_READY &&
622 operation->state != PSA_PAKE_INPUT_X1_X2 &&
623 operation->state != PSA_PAKE_INPUT_X4S )
624 {
625 return( PSA_ERROR_BAD_STATE );
626 }
627
628 if( operation->state == PSA_PAKE_STATE_READY )
629 {
630 if( step != PSA_PAKE_STEP_KEY_SHARE )
631 return( PSA_ERROR_BAD_STATE );
632
633 switch( operation->input_step )
634 {
635 case PSA_PAKE_STEP_X1_X2:
636 operation->state = PSA_PAKE_INPUT_X1_X2;
637 break;
638 case PSA_PAKE_STEP_X2S:
639 operation->state = PSA_PAKE_INPUT_X4S;
640 break;
641 default:
642 return( PSA_ERROR_BAD_STATE );
643 }
644
645 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
646 }
647
648 buffer_remain = PSA_PAKE_BUFFER_SIZE - operation->buffer_length;
649
650 if( input_length == 0 ||
651 input_length > buffer_remain )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200652 {
653 psa_pake_abort( operation );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200654 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200655 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200656
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200657 /* Check if step matches current sequence */
658 switch( operation->sequence )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200659 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200660 case PSA_PAKE_X1_STEP_KEY_SHARE:
661 case PSA_PAKE_X2_STEP_KEY_SHARE:
662 if( step != PSA_PAKE_STEP_KEY_SHARE )
663 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200664 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200665
666 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
667 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
668 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
669 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200670 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200671
672 case PSA_PAKE_X1_STEP_ZK_PROOF:
673 case PSA_PAKE_X2_STEP_ZK_PROOF:
674 if( step != PSA_PAKE_STEP_ZK_PROOF )
675 return( PSA_ERROR_BAD_STATE );
676 break;
677
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200678 default:
679 return( PSA_ERROR_BAD_STATE );
680 }
681
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200682 /* Copy input to local buffer */
683 memcpy( operation->buffer + operation->buffer_length,
684 input, input_length );
685 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200686
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200687 /* Load buffer at each last round ZK_PROOF */
688 if( operation->state == PSA_PAKE_INPUT_X1_X2 &&
689 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200690 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200691 ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake,
692 operation->buffer,
693 operation->buffer_length );
694
695 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
696 operation->buffer_length = 0;
697
698 if( ret != 0 )
699 {
700 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200701 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200702 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200703 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200704 else if( operation->state == PSA_PAKE_INPUT_X4S &&
705 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200706 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200707 ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake,
708 operation->buffer,
709 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200710
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200711 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
712 operation->buffer_length = 0;
713
714 if( ret != 0 )
715 {
716 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200717 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200718 }
719 }
720
721 if( ( operation->state == PSA_PAKE_INPUT_X1_X2 &&
722 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
723 ( operation->state == PSA_PAKE_INPUT_X4S &&
724 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
725 {
726 operation->state = PSA_PAKE_STATE_READY;
727 operation->input_step++;
728 operation->sequence = 0;
729 }
730 else
731 operation->sequence++;
732
733 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200734 }
735 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200736#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200737 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200738}
739
740psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
741 psa_key_derivation_operation_t *output)
742{
743 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
744 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
745
746 if( operation->alg == 0 ||
747 operation->state != PSA_PAKE_STATE_READY ||
Neil Armstrong1e855602022-06-15 11:32:11 +0200748 operation->input_step != PSA_PAKE_STEP_DERIVE ||
749 operation->output_step != PSA_PAKE_STEP_DERIVE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200750 return( PSA_ERROR_BAD_STATE );
751
Neil Armstronga557cb82022-06-10 08:58:32 +0200752#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200753 if( operation->alg == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200754 {
Neil Armstrongf19a3cb2022-06-15 16:00:29 +0200755 ret = mbedtls_ecjpake_write_shared_key( &operation->ctx.ecjpake,
756 operation->buffer,
757 PSA_PAKE_BUFFER_SIZE,
758 &operation->buffer_length,
759 mbedtls_psa_get_random,
760 MBEDTLS_PSA_RANDOM_STATE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200761 if( ret != 0)
762 {
763 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200764 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200765 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200766
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200767 status = psa_key_derivation_input_bytes( output,
768 PSA_KEY_DERIVATION_INPUT_SECRET,
769 operation->buffer,
770 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200771
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200772 mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200773
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200774 psa_pake_abort( operation );
775
776 return( status );
777 }
778 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200779#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200780 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200781}
782
783psa_status_t psa_pake_abort(psa_pake_operation_t * operation)
784{
785 if( operation->alg == 0 )
786 {
787 return( PSA_SUCCESS );
788 }
789
Neil Armstronga557cb82022-06-10 08:58:32 +0200790#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200791 if( operation->alg == PSA_ALG_JPAKE )
792 {
793 operation->input_step = 0;
794 operation->output_step = 0;
795 operation->password = MBEDTLS_SVC_KEY_ID_INIT;
796 operation->role = 0;
797 mbedtls_free( operation->buffer );
798 operation->buffer = NULL;
799 operation->buffer_length = 0;
800 operation->buffer_offset = 0;
801 mbedtls_ecjpake_free( &operation->ctx.ecjpake );
802 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200803#endif
804
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200805 operation->alg = 0;
806 operation->state = 0;
807 operation->sequence = 0;
808
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200809 return( PSA_SUCCESS );
810}
811
812#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
813
814#endif /* MBEDTLS_PSA_CRYPTO_C */