blob: ef31af420418d2a600e50c840bda94ba35a52dc8 [file] [log] [blame]
Neil Armstronga4cc7d62022-05-25 11:30:48 +02001/*
2 * PSA PAKE layer on top of Mbed TLS software crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
26#include "psa_crypto_core.h"
27#include "psa_crypto_slot_management.h"
28
29#include <mbedtls/ecjpake.h>
30#include <mbedtls/psa_util.h>
31
32#include <mbedtls/platform.h>
33#include <mbedtls/error.h>
34#include <string.h>
35
Neil Armstronga4cc7d62022-05-25 11:30:48 +020036/*
37 * State sequence:
38 *
39 * psa_pake_setup()
40 * |
41 * |-- In any order:
42 * | | psa_pake_set_password_key()
43 * | | psa_pake_set_user()
44 * | | psa_pake_set_peer()
Neil Armstrongc29f8472022-06-08 13:34:49 +020045 * | | psa_pake_set_role()
Neil Armstronga4cc7d62022-05-25 11:30:48 +020046 * |
47 * |--- In any order: (First round input before or after first round output)
48 * | |
49 * | |------ In Order
50 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
51 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
52 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
53 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
54 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
55 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
56 * | |
57 * | |------ In Order:
58 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
59 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
60 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
61 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
62 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
63 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
64 * |
65 * |--- In any order: (Second round input before or after second round output)
66 * | |
67 * | |------ In Order
68 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
69 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
70 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
71 * | |
72 * | |------ In Order:
73 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
74 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
75 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
76 * |
77 * psa_pake_get_implicit_key()
78 * psa_pake_abort()
79 */
80
81enum psa_pake_step
82{
83 PSA_PAKE_STEP_INVALID = 0,
84 PSA_PAKE_STEP_X1_X2 = 1,
85 PSA_PAKE_STEP_X2S = 2,
86 PSA_PAKE_STEP_DERIVE = 3,
87};
88
89enum psa_pake_state
90{
91 PSA_PAKE_STATE_INVALID = 0,
92 PSA_PAKE_STATE_SETUP = 1,
93 PSA_PAKE_STATE_READY = 2,
94 PSA_PAKE_OUTPUT_X1_X2 = 3,
95 PSA_PAKE_OUTPUT_X2S = 4,
96 PSA_PAKE_INPUT_X1_X2 = 5,
97 PSA_PAKE_INPUT_X4S = 6,
98};
99
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200100/*
101 * The first PAKE step shares the same sequences of the second PAKE step
102 * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
Neil Armstrongb39833c2022-09-06 11:36:02 +0200103 * It's simpler to share the same sequences numbers of the first
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200104 * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
105 *
106 * State sequence with step, state & sequence enums:
107 * => Input & Output Step = PSA_PAKE_STEP_INVALID
108 * => state = PSA_PAKE_STATE_INVALID
109 * psa_pake_setup()
110 * => Input & Output Step = PSA_PAKE_STEP_X1_X2
111 * => state = PSA_PAKE_STATE_SETUP
112 * => sequence = PSA_PAKE_SEQ_INVALID
113 * |
114 * |--- In any order: (First round input before or after first round output)
115 * | | First call of psa_pake_output() or psa_pake_input() sets
116 * | | state = PSA_PAKE_STATE_READY
117 * | |
118 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
119 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
120 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
121 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
122 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
123 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
124 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
125 * | | | => state = PSA_PAKE_STATE_READY
126 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200127 * | | | => Output Step = PSA_PAKE_STEP_X2S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200128 * | |
129 * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
130 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
131 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
132 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
133 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
134 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
135 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
136 * | | | => state = PSA_PAKE_STATE_READY
137 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200138 * | | | => Output Step = PSA_PAKE_INPUT_X4S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200139 * |
140 * |--- In any order: (Second round input before or after second round output)
141 * | |
142 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
143 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
144 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
145 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
146 * | | | => state = PSA_PAKE_STATE_READY
147 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200148 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200149 * | |
150 * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
151 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
152 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
153 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
154 * | | | => state = PSA_PAKE_STATE_READY
155 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200156 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200157 * |
158 * psa_pake_get_implicit_key()
159 * => Input & Output Step = PSA_PAKE_STEP_INVALID
160 */
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200161enum psa_pake_sequence
162{
163 PSA_PAKE_SEQ_INVALID = 0,
164 PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
165 PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
166 PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
167 PSA_PAKE_X2_STEP_KEY_SHARE = 4,
168 PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
169 PSA_PAKE_X2_STEP_ZK_PROOF = 6,
170 PSA_PAKE_SEQ_END = 7,
171};
172
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200173#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
174static psa_status_t mbedtls_ecjpake_to_psa_error( int ret )
175{
176 switch( ret )
177 {
178 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
179 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
180 case MBEDTLS_ERR_ECP_INVALID_KEY:
181 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
182 return( PSA_ERROR_DATA_INVALID );
183 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
184 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
185 return( PSA_ERROR_BUFFER_TOO_SMALL );
186 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
189 return( PSA_ERROR_CORRUPTION_DETECTED );
190 default:
191 return( PSA_ERROR_GENERIC_ERROR );
192 }
193}
194#endif
195
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200196#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
197psa_status_t psa_pake_setup( psa_pake_operation_t *operation,
198 const psa_pake_cipher_suite_t *cipher_suite)
199{
200 /* A context must be freshly initialized before it can be set up. */
Neil Armstrongcb679f22022-09-13 14:43:07 +0200201 if( operation->alg != PSA_ALG_NONE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200202 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200203
204 if( cipher_suite == NULL ||
205 PSA_ALG_IS_PAKE(cipher_suite->algorithm ) == 0 ||
206 ( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC &&
207 cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH ) ||
208 PSA_ALG_IS_HASH( cipher_suite->hash ) == 0 )
209 {
210 return( PSA_ERROR_INVALID_ARGUMENT );
211 }
212
Neil Armstronga557cb82022-06-10 08:58:32 +0200213#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200214 if( cipher_suite->algorithm == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200215 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200216 if( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
217 cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
218 cipher_suite->bits != 256 ||
219 cipher_suite->hash != PSA_ALG_SHA_256 )
220 {
221 return( PSA_ERROR_NOT_SUPPORTED );
222 }
223
224 operation->alg = cipher_suite->algorithm;
225
226 mbedtls_ecjpake_init( &operation->ctx.ecjpake );
227
228 operation->state = PSA_PAKE_STATE_SETUP;
229 operation->sequence = PSA_PAKE_SEQ_INVALID;
230 operation->input_step = PSA_PAKE_STEP_X1_X2;
231 operation->output_step = PSA_PAKE_STEP_X1_X2;
232
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200233 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200234 operation->buffer_length = 0;
235 operation->buffer_offset = 0;
236
237 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200238 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200239 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200240#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200241 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200242}
243
244psa_status_t psa_pake_set_password_key( psa_pake_operation_t *operation,
245 mbedtls_svc_key_id_t password )
246{
247 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
248 psa_key_attributes_t attributes = psa_key_attributes_init();
249 psa_key_type_t type;
250 psa_key_usage_t usage;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100251 psa_key_slot_t *slot = NULL;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200252
Neil Armstrongcb679f22022-09-13 14:43:07 +0200253 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200254 operation->state != PSA_PAKE_STATE_SETUP )
255 {
256 return( PSA_ERROR_BAD_STATE );
257 }
258
Przemek Stekiel348410f2022-11-15 22:22:07 +0100259 if( psa_is_valid_key_id( password, 1 ) == 0 )
260 return( PSA_ERROR_BAD_STATE );
261
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200262 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
Przemek Stekiel348410f2022-11-15 22:22:07 +0100280 status = psa_get_and_lock_key_slot_with_policy( password, &slot,
281 PSA_KEY_USAGE_DERIVE,
282 PSA_ALG_JPAKE );
283 if( status != PSA_SUCCESS )
284 return( status );
285
286 if( slot->key.data == NULL || slot->key.bytes == 0 )
287 return( PSA_ERROR_INVALID_ARGUMENT );
288
Przemek Stekiel152ae072022-11-17 13:24:36 +0100289 if( operation->password != NULL )
Przemek Stekiel348410f2022-11-15 22:22:07 +0100290 {
Przemek Stekiel369ae0a2022-11-17 14:14:31 +0100291 mbedtls_platform_zeroize( operation->password, operation->password_len );
Przemek Stekiel152ae072022-11-17 13:24:36 +0100292 mbedtls_free( operation->password );
293 operation->password_len = 0;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100294 }
295
Przemek Stekiel152ae072022-11-17 13:24:36 +0100296 operation->password = mbedtls_calloc( 1, slot->key.bytes );
297 if( operation->password == NULL )
Przemek Stekiel348410f2022-11-15 22:22:07 +0100298 {
299 status = psa_unlock_key_slot( slot );
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 }
Przemek Stekiel152ae072022-11-17 13:24:36 +0100302 memcpy( operation->password, slot->key.data, slot->key.bytes );
303 operation->password_len = slot->key.bytes;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100304
305 status = psa_unlock_key_slot( slot );
306 if( status != PSA_SUCCESS )
307 return( status );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200308
309 return( PSA_SUCCESS );
310}
311
312psa_status_t psa_pake_set_user( psa_pake_operation_t *operation,
313 const uint8_t *user_id,
314 size_t user_id_len )
315{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200316 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200317 operation->state != PSA_PAKE_STATE_SETUP )
318 {
319 return( PSA_ERROR_BAD_STATE );
320 }
321
322 if( user_id_len == 0 || user_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200323 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200324
325 return( PSA_ERROR_NOT_SUPPORTED );
326}
327
328psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation,
329 const uint8_t *peer_id,
330 size_t peer_id_len )
331{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200332 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200333 operation->state != PSA_PAKE_STATE_SETUP )
334 {
335 return( PSA_ERROR_BAD_STATE );
336 }
337
338 if( peer_id_len == 0 || peer_id == NULL )
Neil Armstronge9231112022-06-10 09:03:41 +0200339 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200340
341 return( PSA_ERROR_NOT_SUPPORTED );
342}
343
344psa_status_t psa_pake_set_role( psa_pake_operation_t *operation,
345 psa_pake_role_t role )
346{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200347 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200348 operation->state != PSA_PAKE_STATE_SETUP )
349 {
350 return( PSA_ERROR_BAD_STATE );
351 }
352
353 if( role != PSA_PAKE_ROLE_NONE &&
354 role != PSA_PAKE_ROLE_FIRST &&
355 role != PSA_PAKE_ROLE_SECOND &&
356 role != PSA_PAKE_ROLE_CLIENT &&
357 role != PSA_PAKE_ROLE_SERVER )
358 {
Neil Armstronge9231112022-06-10 09:03:41 +0200359 return( PSA_ERROR_INVALID_ARGUMENT );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200360 }
361
Neil Armstronga557cb82022-06-10 08:58:32 +0200362#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200363 if( operation->alg == PSA_ALG_JPAKE )
364 {
365 if( role != PSA_PAKE_ROLE_CLIENT &&
366 role != PSA_PAKE_ROLE_SERVER )
Neil Armstronge9231112022-06-10 09:03:41 +0200367 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200368
369 operation->role = role;
370
371 return( PSA_SUCCESS );
372 }
373 else
374#endif
375 return( PSA_ERROR_NOT_SUPPORTED );
376}
377
Neil Armstronga557cb82022-06-10 08:58:32 +0200378#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200379static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation )
380{
381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200382 mbedtls_ecjpake_role role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200383
384 if( operation->role == PSA_PAKE_ROLE_CLIENT )
385 role = MBEDTLS_ECJPAKE_CLIENT;
386 else if( operation->role == PSA_PAKE_ROLE_SERVER )
387 role = MBEDTLS_ECJPAKE_SERVER;
388 else
389 return( PSA_ERROR_BAD_STATE );
390
Przemek Stekiel152ae072022-11-17 13:24:36 +0100391 if (operation->password == NULL ||
392 operation->password_len == 0 )
Przemek Stekiel348410f2022-11-15 22:22:07 +0100393 {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200394 return( PSA_ERROR_BAD_STATE );
Przemek Stekiel348410f2022-11-15 22:22:07 +0100395 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200396
397 ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake,
398 role,
399 MBEDTLS_MD_SHA256,
400 MBEDTLS_ECP_DP_SECP256R1,
Przemek Stekiel152ae072022-11-17 13:24:36 +0100401 operation->password,
402 operation->password_len );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200403
404 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200405 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200406
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200407 operation->state = PSA_PAKE_STATE_READY;
408
409 return( PSA_SUCCESS );
410}
411#endif
412
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200413static psa_status_t psa_pake_output_internal(
414 psa_pake_operation_t *operation,
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200415 psa_pake_step_t step,
416 uint8_t *output,
417 size_t output_size,
418 size_t *output_length )
419{
420 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
422 size_t length;
423
Neil Armstrongcb679f22022-09-13 14:43:07 +0200424 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200425 operation->state == PSA_PAKE_STATE_INVALID )
426 return( PSA_ERROR_BAD_STATE );
427
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200428 if( output == NULL || output_size == 0 || output_length == NULL )
429 return( PSA_ERROR_INVALID_ARGUMENT );
430
Neil Armstronga557cb82022-06-10 08:58:32 +0200431#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200432 /*
433 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
434 * handling of output sequencing.
435 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200436 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200437 * at once, on the other side the PSA CRYPTO PAKE api requires
438 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
439 * retrieved in sequence.
440 *
441 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
442 * data is stored in an intermediate buffer at first step output call,
443 * and data is sliced down by parsing the ECPoint records in order
444 * to return the right parts on each step.
445 */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200446 if( operation->alg == PSA_ALG_JPAKE )
447 {
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200448 if( step != PSA_PAKE_STEP_KEY_SHARE &&
449 step != PSA_PAKE_STEP_ZK_PUBLIC &&
450 step != PSA_PAKE_STEP_ZK_PROOF )
451 return( PSA_ERROR_INVALID_ARGUMENT );
452
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200453 if( operation->state == PSA_PAKE_STATE_SETUP ) {
454 status = psa_pake_ecjpake_setup( operation );
455 if( status != PSA_SUCCESS )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200456 return( status );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200457 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200458
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200459 if( operation->state != PSA_PAKE_STATE_READY &&
460 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
461 operation->state != PSA_PAKE_OUTPUT_X2S )
462 {
463 return( PSA_ERROR_BAD_STATE );
464 }
465
466 if( operation->state == PSA_PAKE_STATE_READY )
467 {
468 if( step != PSA_PAKE_STEP_KEY_SHARE )
469 return( PSA_ERROR_BAD_STATE );
470
471 switch( operation->output_step )
472 {
473 case PSA_PAKE_STEP_X1_X2:
474 operation->state = PSA_PAKE_OUTPUT_X1_X2;
475 break;
476 case PSA_PAKE_STEP_X2S:
477 operation->state = PSA_PAKE_OUTPUT_X2S;
478 break;
479 default:
480 return( PSA_ERROR_BAD_STATE );
481 }
482
483 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
484 }
485
486 /* Check if step matches current sequence */
487 switch( operation->sequence )
488 {
489 case PSA_PAKE_X1_STEP_KEY_SHARE:
490 case PSA_PAKE_X2_STEP_KEY_SHARE:
491 if( step != PSA_PAKE_STEP_KEY_SHARE )
492 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200493 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200494
495 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
496 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
497 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
498 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200499 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200500
501 case PSA_PAKE_X1_STEP_ZK_PROOF:
502 case PSA_PAKE_X2_STEP_ZK_PROOF:
503 if( step != PSA_PAKE_STEP_ZK_PROOF )
504 return( PSA_ERROR_BAD_STATE );
505 break;
506
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200507 default:
508 return( PSA_ERROR_BAD_STATE );
509 }
510
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200511 /* Initialize & write round on KEY_SHARE sequences */
512 if( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
513 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200514 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200515 ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake,
516 operation->buffer,
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200517 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200518 &operation->buffer_length,
519 mbedtls_psa_get_random,
520 MBEDTLS_PSA_RANDOM_STATE );
521 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200522 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200523
524 operation->buffer_offset = 0;
525 }
526 else if( operation->state == PSA_PAKE_OUTPUT_X2S &&
527 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE )
528 {
529 ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake,
530 operation->buffer,
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200531 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200532 &operation->buffer_length,
533 mbedtls_psa_get_random,
534 MBEDTLS_PSA_RANDOM_STATE );
535 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200536 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200537
538 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200539 }
540
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200541 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200542 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
543 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
544 * that the data for each step is prepended with a length byte, and
545 * then they're concatenated. Additionally, the server's second round
546 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200547 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200548 * In PSA, we output each step separately, and don't prepend the
549 * output with a length byte, even less a curve identifier, as that
550 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200551 */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200552 if( operation->state == PSA_PAKE_OUTPUT_X2S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200553 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
554 operation->role == PSA_PAKE_ROLE_SERVER )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200555 {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200556 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
557 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200558 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200559
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200560 /* Read the length byte then move past it to the data */
561 length = operation->buffer[operation->buffer_offset];
562 operation->buffer_offset += 1;
563
564 if( operation->buffer_offset + length > operation->buffer_length )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200565 return( PSA_ERROR_DATA_CORRUPT );
566
567 if( output_size < length )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200568 return( PSA_ERROR_BUFFER_TOO_SMALL );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200569
570 memcpy( output,
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200571 operation->buffer + operation->buffer_offset,
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200572 length );
573 *output_length = length;
574
575 operation->buffer_offset += length;
576
577 /* Reset buffer after ZK_PROOF sequence */
578 if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
579 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
580 ( operation->state == PSA_PAKE_OUTPUT_X2S &&
581 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
582 {
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200583 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200584 operation->buffer_length = 0;
585 operation->buffer_offset = 0;
586
587 operation->state = PSA_PAKE_STATE_READY;
588 operation->output_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200589 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200590 }
591 else
592 operation->sequence++;
593
594 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200595 }
596 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200597#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200598 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200599}
600
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200601psa_status_t psa_pake_output( psa_pake_operation_t *operation,
602 psa_pake_step_t step,
603 uint8_t *output,
604 size_t output_size,
605 size_t *output_length )
606{
607 psa_status_t status = psa_pake_output_internal(
608 operation, step, output, output_size, output_length );
609
610 if( status != PSA_SUCCESS )
611 psa_pake_abort( operation );
612
613 return( status );
614}
615
616static psa_status_t psa_pake_input_internal(
617 psa_pake_operation_t *operation,
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200618 psa_pake_step_t step,
619 const uint8_t *input,
620 size_t input_length )
621{
622 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200624
Neil Armstrongcb679f22022-09-13 14:43:07 +0200625 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200626 operation->state == PSA_PAKE_STATE_INVALID )
627 return( PSA_ERROR_BAD_STATE );
628
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200629 if( input == NULL || input_length == 0 )
630 return( PSA_ERROR_INVALID_ARGUMENT );
631
Neil Armstronga557cb82022-06-10 08:58:32 +0200632#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200633 /*
634 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
635 * handling of input sequencing.
636 *
637 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
638 * at once as input, on the other side the PSA CRYPTO PAKE api requires
639 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
640 * given in sequence.
641 *
642 * In order to achieve API compatibility, each X1+X2 or X4S step data
643 * is stored sequentially in an intermediate buffer and given to the
644 * MbedTLS JPAKE API on the last step.
645 *
646 * This causes any input error to be only detected on the last step.
647 */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200648 if( operation->alg == PSA_ALG_JPAKE )
649 {
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200650 if( step != PSA_PAKE_STEP_KEY_SHARE &&
651 step != PSA_PAKE_STEP_ZK_PUBLIC &&
652 step != PSA_PAKE_STEP_ZK_PROOF )
653 return( PSA_ERROR_INVALID_ARGUMENT );
654
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200655 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
656 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256 );
Manuel Pégourié-Gonnard02f82bb2022-10-13 13:32:02 +0200657 if( input_length > (size_t) PSA_PAKE_INPUT_SIZE( PSA_ALG_JPAKE, prim, step ) )
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200658 return( PSA_ERROR_INVALID_ARGUMENT );
659
Neil Armstrong5bbdb702022-09-05 17:54:15 +0200660 if( operation->state == PSA_PAKE_STATE_SETUP )
661 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200662 status = psa_pake_ecjpake_setup( operation );
663 if( status != PSA_SUCCESS )
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200664 return( status );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200665 }
666
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200667 if( operation->state != PSA_PAKE_STATE_READY &&
668 operation->state != PSA_PAKE_INPUT_X1_X2 &&
669 operation->state != PSA_PAKE_INPUT_X4S )
670 {
671 return( PSA_ERROR_BAD_STATE );
672 }
673
674 if( operation->state == PSA_PAKE_STATE_READY )
675 {
676 if( step != PSA_PAKE_STEP_KEY_SHARE )
677 return( PSA_ERROR_BAD_STATE );
678
679 switch( operation->input_step )
680 {
681 case PSA_PAKE_STEP_X1_X2:
682 operation->state = PSA_PAKE_INPUT_X1_X2;
683 break;
684 case PSA_PAKE_STEP_X2S:
685 operation->state = PSA_PAKE_INPUT_X4S;
686 break;
687 default:
688 return( PSA_ERROR_BAD_STATE );
689 }
690
691 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
692 }
693
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200694 /* Check if step matches current sequence */
695 switch( operation->sequence )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200696 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200697 case PSA_PAKE_X1_STEP_KEY_SHARE:
698 case PSA_PAKE_X2_STEP_KEY_SHARE:
699 if( step != PSA_PAKE_STEP_KEY_SHARE )
700 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200701 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200702
703 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
704 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
705 if( step != PSA_PAKE_STEP_ZK_PUBLIC )
706 return( PSA_ERROR_BAD_STATE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200707 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200708
709 case PSA_PAKE_X1_STEP_ZK_PROOF:
710 case PSA_PAKE_X2_STEP_ZK_PROOF:
711 if( step != PSA_PAKE_STEP_ZK_PROOF )
712 return( PSA_ERROR_BAD_STATE );
713 break;
714
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200715 default:
716 return( PSA_ERROR_BAD_STATE );
717 }
718
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200719 /*
720 * Copy input to local buffer and format it as the Mbed TLS API
721 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
722 * The summary is that the data for each step is prepended with a
723 * length byte, and then they're concatenated. Additionally, the
724 * server's second round output is prepended with a 3-bytes
725 * ECParameters structure - which means we have to prepend that when
726 * we're a client.
727 */
728 if( operation->state == PSA_PAKE_INPUT_X4S &&
729 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
730 operation->role == PSA_PAKE_ROLE_CLIENT )
731 {
732 /* We only support secp256r1. */
733 /* This is the ECParameters structure defined by RFC 8422. */
734 unsigned char ecparameters[3] = {
735 3, /* named_curve */
736 0, 23 /* secp256r1 */
737 };
738 memcpy( operation->buffer + operation->buffer_length,
739 ecparameters, sizeof( ecparameters ) );
740 operation->buffer_length += sizeof( ecparameters );
741 }
742
743 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200744 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200745 operation->buffer_length += 1;
746
747 /* Finally copy the data */
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200748 memcpy( operation->buffer + operation->buffer_length,
749 input, input_length );
750 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200751
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200752 /* Load buffer at each last round ZK_PROOF */
753 if( operation->state == PSA_PAKE_INPUT_X1_X2 &&
754 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200755 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200756 ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake,
757 operation->buffer,
758 operation->buffer_length );
759
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200760 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200761 operation->buffer_length = 0;
762
763 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200764 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200765 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200766 else if( operation->state == PSA_PAKE_INPUT_X4S &&
767 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200768 {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200769 ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake,
770 operation->buffer,
771 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200772
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200773 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200774 operation->buffer_length = 0;
775
776 if( ret != 0 )
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200777 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200778 }
779
780 if( ( operation->state == PSA_PAKE_INPUT_X1_X2 &&
781 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) ||
782 ( operation->state == PSA_PAKE_INPUT_X4S &&
783 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) )
784 {
785 operation->state = PSA_PAKE_STATE_READY;
786 operation->input_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200787 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200788 }
789 else
790 operation->sequence++;
791
792 return( PSA_SUCCESS );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200793 }
794 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200795#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200796 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200797}
798
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200799psa_status_t psa_pake_input( psa_pake_operation_t *operation,
800 psa_pake_step_t step,
801 const uint8_t *input,
802 size_t input_length )
803{
804 psa_status_t status = psa_pake_input_internal(
805 operation, step, input, input_length );
806
807 if( status != PSA_SUCCESS )
808 psa_pake_abort( operation );
809
810 return( status );
811}
812
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200813psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation,
814 psa_key_derivation_operation_t *output)
815{
816 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
817 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
818
Neil Armstrongcb679f22022-09-13 14:43:07 +0200819 if( operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200820 operation->state != PSA_PAKE_STATE_READY ||
Neil Armstrong1e855602022-06-15 11:32:11 +0200821 operation->input_step != PSA_PAKE_STEP_DERIVE ||
822 operation->output_step != PSA_PAKE_STEP_DERIVE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200823 return( PSA_ERROR_BAD_STATE );
824
Neil Armstronga557cb82022-06-10 08:58:32 +0200825#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200826 if( operation->alg == PSA_ALG_JPAKE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200827 {
Neil Armstrongf19a3cb2022-06-15 16:00:29 +0200828 ret = mbedtls_ecjpake_write_shared_key( &operation->ctx.ecjpake,
829 operation->buffer,
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200830 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
Neil Armstrongf19a3cb2022-06-15 16:00:29 +0200831 &operation->buffer_length,
832 mbedtls_psa_get_random,
833 MBEDTLS_PSA_RANDOM_STATE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200834 if( ret != 0)
835 {
836 psa_pake_abort( operation );
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200837 return( mbedtls_ecjpake_to_psa_error( ret ) );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200838 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200839
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200840 status = psa_key_derivation_input_bytes( output,
841 PSA_KEY_DERIVATION_INPUT_SECRET,
842 operation->buffer,
843 operation->buffer_length );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200844
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200845 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200846
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200847 psa_pake_abort( operation );
848
849 return( status );
850 }
851 else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200852#endif
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200853 return( PSA_ERROR_NOT_SUPPORTED );
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200854}
855
856psa_status_t psa_pake_abort(psa_pake_operation_t * operation)
857{
Neil Armstrongcb679f22022-09-13 14:43:07 +0200858 if( operation->alg == PSA_ALG_NONE )
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200859 {
860 return( PSA_SUCCESS );
861 }
862
Neil Armstronga557cb82022-06-10 08:58:32 +0200863#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200864 if( operation->alg == PSA_ALG_JPAKE )
865 {
Neil Armstrongcb679f22022-09-13 14:43:07 +0200866 operation->input_step = PSA_PAKE_STEP_INVALID;
867 operation->output_step = PSA_PAKE_STEP_INVALID;
Przemek Stekiel369ae0a2022-11-17 14:14:31 +0100868 mbedtls_platform_zeroize( operation->password, operation->password_len );
Przemek Stekiel152ae072022-11-17 13:24:36 +0100869 mbedtls_free( operation->password );
870 operation->password = NULL;
871 operation->password_len = 0;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200872 operation->role = PSA_PAKE_ROLE_NONE;
Manuel Pégourié-Gonnard79617d92022-10-05 12:55:50 +0200873 mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE );
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200874 operation->buffer_length = 0;
875 operation->buffer_offset = 0;
876 mbedtls_ecjpake_free( &operation->ctx.ecjpake );
877 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200878#endif
879
Neil Armstrongcb679f22022-09-13 14:43:07 +0200880 operation->alg = PSA_ALG_NONE;
881 operation->state = PSA_PAKE_STATE_INVALID;
882 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200883
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200884 return( PSA_SUCCESS );
885}
886
887#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
888
889#endif /* MBEDTLS_PSA_CRYPTO_C */