blob: 6c4db6f2d7751908206edcca153911eb490a3aee [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"
Neil Armstrong56b8d232022-06-01 18:05:57 +020027#include "psa_crypto_pake.h"
Neil Armstronga4cc7d62022-05-25 11:30:48 +020028#include "psa_crypto_slot_management.h"
29
30#include <mbedtls/ecjpake.h>
31#include <mbedtls/psa_util.h>
32
33#include <mbedtls/platform.h>
34#include <mbedtls/error.h>
35#include <string.h>
36
Neil Armstronga4cc7d62022-05-25 11:30:48 +020037/*
38 * State sequence:
39 *
40 * psa_pake_setup()
41 * |
42 * |-- In any order:
43 * | | psa_pake_set_password_key()
44 * | | psa_pake_set_user()
45 * | | psa_pake_set_peer()
Neil Armstrongc29f8472022-06-08 13:34:49 +020046 * | | psa_pake_set_role()
Neil Armstronga4cc7d62022-05-25 11:30:48 +020047 * |
48 * |--- In any order: (First round input before or after first round output)
49 * | |
50 * | |------ In Order
51 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
52 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
53 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
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 * | |
58 * | |------ In Order:
59 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
60 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
61 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
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 * |
66 * |--- In any order: (Second round input before or after second round output)
67 * | |
68 * | |------ In Order
69 * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE)
70 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC)
71 * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF)
72 * | |
73 * | |------ In Order:
74 * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE)
75 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC)
76 * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF)
77 * |
78 * psa_pake_get_implicit_key()
79 * psa_pake_abort()
80 */
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082enum psa_pake_step {
Neil Armstronga4cc7d62022-05-25 11:30:48 +020083 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
Gilles Peskine449bd832023-01-11 14:50:10 +010089enum psa_pake_state {
Neil Armstronga4cc7d62022-05-25 11:30:48 +020090 PSA_PAKE_STATE_INVALID = 0,
91 PSA_PAKE_STATE_SETUP = 1,
92 PSA_PAKE_STATE_READY = 2,
93 PSA_PAKE_OUTPUT_X1_X2 = 3,
94 PSA_PAKE_OUTPUT_X2S = 4,
95 PSA_PAKE_INPUT_X1_X2 = 5,
96 PSA_PAKE_INPUT_X4S = 6,
97};
98
Neil Armstrongbcd5bd92022-09-05 18:33:23 +020099/*
100 * The first PAKE step shares the same sequences of the second PAKE step
101 * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
Neil Armstrongb39833c2022-09-06 11:36:02 +0200102 * It's simpler to share the same sequences numbers of the first
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200103 * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
104 *
105 * State sequence with step, state & sequence enums:
106 * => Input & Output Step = PSA_PAKE_STEP_INVALID
107 * => state = PSA_PAKE_STATE_INVALID
108 * psa_pake_setup()
109 * => Input & Output Step = PSA_PAKE_STEP_X1_X2
110 * => state = PSA_PAKE_STATE_SETUP
111 * => sequence = PSA_PAKE_SEQ_INVALID
112 * |
113 * |--- In any order: (First round input before or after first round output)
114 * | | First call of psa_pake_output() or psa_pake_input() sets
115 * | | state = PSA_PAKE_STATE_READY
116 * | |
117 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
118 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
119 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
120 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
121 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
122 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
123 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
124 * | | | => state = PSA_PAKE_STATE_READY
125 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200126 * | | | => Output Step = PSA_PAKE_STEP_X2S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200127 * | |
128 * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
129 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
130 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
131 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
132 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
133 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
134 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
135 * | | | => state = PSA_PAKE_STATE_READY
136 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200137 * | | | => Output Step = PSA_PAKE_INPUT_X4S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200138 * |
139 * |--- In any order: (Second round input before or after second round output)
140 * | |
141 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
142 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
143 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
144 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
145 * | | | => state = PSA_PAKE_STATE_READY
146 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200147 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200148 * | |
149 * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
150 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
151 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
152 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
153 * | | | => state = PSA_PAKE_STATE_READY
154 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200155 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200156 * |
157 * psa_pake_get_implicit_key()
158 * => Input & Output Step = PSA_PAKE_STEP_INVALID
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160enum psa_pake_sequence {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200161 PSA_PAKE_SEQ_INVALID = 0,
162 PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */
163 PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */
164 PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */
165 PSA_PAKE_X2_STEP_KEY_SHARE = 4,
166 PSA_PAKE_X2_STEP_ZK_PUBLIC = 5,
167 PSA_PAKE_X2_STEP_ZK_PROOF = 6,
168 PSA_PAKE_SEQ_END = 7,
169};
170
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200171#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100172static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200173{
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 switch (ret) {
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200175 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
176 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
177 case MBEDTLS_ERR_ECP_INVALID_KEY:
178 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return PSA_ERROR_DATA_INVALID;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200180 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
181 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 return PSA_ERROR_BUFFER_TOO_SMALL;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200183 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200185 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200187 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return PSA_ERROR_GENERIC_ERROR;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200189 }
190}
191#endif
192
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200193#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100194psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200195 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200196{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
198
199 /* A context must be freshly initialized before it can be set up. */
200 if (operation->alg != PSA_ALG_NONE) {
201 status = PSA_ERROR_BAD_STATE;
202 goto error;
203 }
204
205 if (cipher_suite == NULL ||
206 PSA_ALG_IS_PAKE(cipher_suite->algorithm) == 0 ||
207 (cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC &&
208 cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH) ||
209 PSA_ALG_IS_HASH(cipher_suite->hash) == 0) {
210 status = PSA_ERROR_INVALID_ARGUMENT;
211 goto error;
212 }
213
Neil Armstronga557cb82022-06-10 08:58:32 +0200214#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (cipher_suite->algorithm == PSA_ALG_JPAKE) {
216 if (cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200217 cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
218 cipher_suite->bits != 256 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 cipher_suite->hash != PSA_ALG_SHA_256) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100220 status = PSA_ERROR_NOT_SUPPORTED;
221 goto error;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200222 }
223
224 operation->alg = cipher_suite->algorithm;
225
Przemek Stekiel6c764412022-11-22 14:05:12 +0100226 mbedtls_ecjpake_init(&operation->ctx.pake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200227
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;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100232 operation->password_len = 0;
233 operation->password = NULL;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200236 operation->buffer_length = 0;
237 operation->buffer_offset = 0;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_SUCCESS;
240 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200241#else
242 (void) operation;
243 (void) cipher_suite;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200244#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200245 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100246
247error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200248 mbedtls_psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100249 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200250}
251
Przemek Stekiel6c764412022-11-22 14:05:12 +0100252psa_status_t mbedtls_psa_pake_set_password_key(const psa_key_attributes_t *attributes,
253 mbedtls_psa_pake_operation_t *operation,
254 uint8_t *password,
255 size_t password_len)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200256{
257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100258 psa_key_type_t type = psa_get_key_type(attributes);
259 psa_key_usage_t usage = psa_get_key_usage_flags(attributes);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (type != PSA_KEY_TYPE_PASSWORD &&
262 type != PSA_KEY_TYPE_PASSWORD_HASH) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100263 status = PSA_ERROR_INVALID_ARGUMENT;
264 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200265 }
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if ((usage & PSA_KEY_USAGE_DERIVE) == 0) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100268 status = PSA_ERROR_NOT_PERMITTED;
269 goto error;
270 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200271
Przemek Stekiel6c764412022-11-22 14:05:12 +0100272 if (operation->alg == PSA_ALG_NONE) {
273 status = PSA_ERROR_BAD_STATE;
274 goto error;
275 }
276
277 if (operation->state != PSA_PAKE_STATE_SETUP) {
278 status = PSA_ERROR_BAD_STATE;
279 goto error;
280 }
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (operation->password != NULL) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100283 status = PSA_ERROR_BAD_STATE;
284 goto error;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100285 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100286
Przemek Stekiel6c764412022-11-22 14:05:12 +0100287 operation->password = mbedtls_calloc(1, password_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (operation->password == NULL) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 return PSA_ERROR_INSUFFICIENT_MEMORY;
290 }
Przemek Stekiel348410f2022-11-15 22:22:07 +0100291
Przemek Stekiel6c764412022-11-22 14:05:12 +0100292 memcpy(operation->password, password, password_len);
293 operation->password_len = password_len;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return PSA_SUCCESS;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100296
297error:
Przemek Stekiel6c764412022-11-22 14:05:12 +0100298 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200300}
301
Przemek Stekiel6c764412022-11-22 14:05:12 +0100302psa_status_t mbedtls_psa_pake_set_user(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200303 const uint8_t *user_id,
304 size_t user_id_len)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200305{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100306 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong5ae60962022-09-15 11:29:46 +0200307 (void) user_id;
308 (void) user_id_len;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100309
Przemek Stekiel6c764412022-11-22 14:05:12 +0100310 if (operation->alg == PSA_ALG_NONE) {
311 return PSA_ERROR_BAD_STATE;
312 }
313
Neil Armstrong5ae60962022-09-15 11:29:46 +0200314 if (operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100315 status = PSA_ERROR_BAD_STATE;
316 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200317 }
318
Valerio Settifdb77cd2022-11-11 12:02:24 +0100319 status = PSA_ERROR_NOT_SUPPORTED;
320
321error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200322 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200324}
325
Przemek Stekiel6c764412022-11-22 14:05:12 +0100326psa_status_t mbedtls_psa_pake_set_peer(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200327 const uint8_t *peer_id,
328 size_t peer_id_len)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200329{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100330 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrong5ae60962022-09-15 11:29:46 +0200331 (void) peer_id;
332 (void) peer_id_len;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100333
Przemek Stekiel6c764412022-11-22 14:05:12 +0100334 if (operation->alg == PSA_ALG_NONE) {
335 status = PSA_ERROR_BAD_STATE;
336 goto error;
337 }
338
Neil Armstrong5ae60962022-09-15 11:29:46 +0200339 if (operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100340 status = PSA_ERROR_BAD_STATE;
341 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200342 }
343
Valerio Settifdb77cd2022-11-11 12:02:24 +0100344 status = PSA_ERROR_NOT_SUPPORTED;
345
346error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200347 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200349}
350
Przemek Stekiel6c764412022-11-22 14:05:12 +0100351psa_status_t mbedtls_psa_pake_set_role(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200352 psa_pake_role_t role)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200353{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
355 if (operation->alg == PSA_ALG_NONE) {
356 status = PSA_ERROR_BAD_STATE;
357 goto error;
358 }
359
Neil Armstrong5ae60962022-09-15 11:29:46 +0200360 if (operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100361 status = PSA_ERROR_BAD_STATE;
362 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200363 }
364
Neil Armstronga557cb82022-06-10 08:58:32 +0200365#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (operation->alg == PSA_ALG_JPAKE) {
367 if (role != PSA_PAKE_ROLE_CLIENT &&
368 role != PSA_PAKE_ROLE_SERVER) {
369 return PSA_ERROR_NOT_SUPPORTED;
370 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200371
372 operation->role = role;
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return PSA_SUCCESS;
375 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200376#else
377 (void) role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200378#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200379
380 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100381
382error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200383 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200385}
386
Neil Armstronga557cb82022-06-10 08:58:32 +0200387#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100388static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200389{
390 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200391 mbedtls_ecjpake_role role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (operation->role == PSA_PAKE_ROLE_CLIENT) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200394 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 } else if (operation->role == PSA_PAKE_ROLE_SERVER) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200396 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 } else {
398 return PSA_ERROR_BAD_STATE;
399 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (operation->password_len == 0) {
402 return PSA_ERROR_BAD_STATE;
403 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200404
Przemek Stekiel6c764412022-11-22 14:05:12 +0100405 ret = mbedtls_ecjpake_setup(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 role,
407 MBEDTLS_MD_SHA256,
408 MBEDTLS_ECP_DP_SECP256R1,
409 operation->password,
410 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 mbedtls_platform_zeroize(operation->password, operation->password_len);
413 mbedtls_free(operation->password);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100414 operation->password = NULL;
415 operation->password_len = 0;
416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (ret != 0) {
418 return mbedtls_ecjpake_to_psa_error(ret);
419 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100420
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200421 operation->state = PSA_PAKE_STATE_READY;
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200424}
425#endif
426
Neil Armstrong56b8d232022-06-01 18:05:57 +0200427static psa_status_t mbedtls_psa_pake_output_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100428 mbedtls_psa_pake_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 psa_pake_step_t step,
430 uint8_t *output,
431 size_t output_size,
432 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200433{
434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
435 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
436 size_t length;
437
Przemek Stekiel6c764412022-11-22 14:05:12 +0100438 if (operation->alg == PSA_ALG_NONE) {
439 return PSA_ERROR_BAD_STATE;
440 }
441
Neil Armstrong5ae60962022-09-15 11:29:46 +0200442 if (operation->state == PSA_PAKE_STATE_INVALID) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 return PSA_ERROR_BAD_STATE;
444 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200445
Neil Armstronga557cb82022-06-10 08:58:32 +0200446#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200447 /*
448 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
449 * handling of output sequencing.
450 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200451 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200452 * at once, on the other side the PSA CRYPTO PAKE api requires
453 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
454 * retrieved in sequence.
455 *
456 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
457 * data is stored in an intermediate buffer at first step output call,
458 * and data is sliced down by parsing the ECPoint records in order
459 * to return the right parts on each step.
460 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (operation->alg == PSA_ALG_JPAKE) {
462 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200463 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 step != PSA_PAKE_STEP_ZK_PROOF) {
465 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200466 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (operation->state == PSA_PAKE_STATE_SETUP) {
469 status = psa_pake_ecjpake_setup(operation);
470 if (status != PSA_SUCCESS) {
471 return status;
472 }
473 }
474
475 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200476 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 operation->state != PSA_PAKE_OUTPUT_X2S) {
478 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200479 }
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (operation->state == PSA_PAKE_STATE_READY) {
482 if (step != PSA_PAKE_STEP_KEY_SHARE) {
483 return PSA_ERROR_BAD_STATE;
484 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 switch (operation->output_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200487 case PSA_PAKE_STEP_X1_X2:
488 operation->state = PSA_PAKE_OUTPUT_X1_X2;
489 break;
490 case PSA_PAKE_STEP_X2S:
491 operation->state = PSA_PAKE_OUTPUT_X2S;
492 break;
493 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200495 }
496
497 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
498 }
499
500 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200502 case PSA_PAKE_X1_STEP_KEY_SHARE:
503 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (step != PSA_PAKE_STEP_KEY_SHARE) {
505 return PSA_ERROR_BAD_STATE;
506 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200507 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200508
509 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
510 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
512 return PSA_ERROR_BAD_STATE;
513 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200514 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200515
516 case PSA_PAKE_X1_STEP_ZK_PROOF:
517 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (step != PSA_PAKE_STEP_ZK_PROOF) {
519 return PSA_ERROR_BAD_STATE;
520 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200521 break;
522
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200523 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200525 }
526
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200527 /* Initialize & write round on KEY_SHARE sequences */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
529 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100530 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 operation->buffer,
532 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
533 &operation->buffer_length,
534 mbedtls_psa_get_random,
535 MBEDTLS_PSA_RANDOM_STATE);
536 if (ret != 0) {
537 return mbedtls_ecjpake_to_psa_error(ret);
538 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200539
540 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 } else if (operation->state == PSA_PAKE_OUTPUT_X2S &&
542 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100543 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 operation->buffer,
545 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
546 &operation->buffer_length,
547 mbedtls_psa_get_random,
548 MBEDTLS_PSA_RANDOM_STATE);
549 if (ret != 0) {
550 return mbedtls_ecjpake_to_psa_error(ret);
551 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200552
553 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200554 }
555
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200556 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200557 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
558 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
559 * that the data for each step is prepended with a length byte, and
560 * then they're concatenated. Additionally, the server's second round
561 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200562 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200563 * In PSA, we output each step separately, and don't prepend the
564 * output with a length byte, even less a curve identifier, as that
565 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200566 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 if (operation->state == PSA_PAKE_OUTPUT_X2S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200568 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 operation->role == PSA_PAKE_ROLE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200570 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
571 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200572 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200573
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200574 /* Read the length byte then move past it to the data */
575 length = operation->buffer[operation->buffer_offset];
576 operation->buffer_offset += 1;
577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (operation->buffer_offset + length > operation->buffer_length) {
579 return PSA_ERROR_DATA_CORRUPT;
580 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (output_size < length) {
583 return PSA_ERROR_BUFFER_TOO_SMALL;
584 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 memcpy(output,
587 operation->buffer + operation->buffer_offset,
588 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200589 *output_length = length;
590
591 operation->buffer_offset += length;
592
593 /* Reset buffer after ZK_PROOF sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if ((operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
595 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
596 (operation->state == PSA_PAKE_OUTPUT_X2S &&
597 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
598 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200599 operation->buffer_length = 0;
600 operation->buffer_offset = 0;
601
602 operation->state = PSA_PAKE_STATE_READY;
603 operation->output_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200604 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200606 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return PSA_SUCCESS;
610 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200611#else
612 (void) step;
613 (void) output;
614 (void) output_size;
615 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200616#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200617 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200618}
619
Przemek Stekiel6c764412022-11-22 14:05:12 +0100620psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200621 psa_pake_step_t step,
622 uint8_t *output,
623 size_t output_size,
624 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200625{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200626 psa_status_t status = mbedtls_psa_pake_output_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (status != PSA_SUCCESS) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100630 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200634}
635
Neil Armstrong56b8d232022-06-01 18:05:57 +0200636static psa_status_t mbedtls_psa_pake_input_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100637 mbedtls_psa_pake_operation_t *operation,
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 psa_pake_step_t step,
639 const uint8_t *input,
640 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200641{
642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200644
Przemek Stekiel6c764412022-11-22 14:05:12 +0100645 if (operation->alg == PSA_ALG_NONE) {
646 return PSA_ERROR_BAD_STATE;
647 }
648
Neil Armstrong5ae60962022-09-15 11:29:46 +0200649 if (operation->state == PSA_PAKE_STATE_INVALID) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return PSA_ERROR_BAD_STATE;
651 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200652
Neil Armstronga557cb82022-06-10 08:58:32 +0200653#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200654 /*
655 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
656 * handling of input sequencing.
657 *
658 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
659 * at once as input, on the other side the PSA CRYPTO PAKE api requires
660 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
661 * given in sequence.
662 *
663 * In order to achieve API compatibility, each X1+X2 or X4S step data
664 * is stored sequentially in an intermediate buffer and given to the
665 * MbedTLS JPAKE API on the last step.
666 *
667 * This causes any input error to be only detected on the last step.
668 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if (operation->alg == PSA_ALG_JPAKE) {
670 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200671 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 step != PSA_PAKE_STEP_ZK_PROOF) {
673 return PSA_ERROR_INVALID_ARGUMENT;
674 }
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200675
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200676 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256);
678 if (input_length > (size_t) PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, prim, step)) {
679 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200680 }
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (operation->state == PSA_PAKE_STATE_SETUP) {
683 status = psa_pake_ecjpake_setup(operation);
684 if (status != PSA_SUCCESS) {
685 return status;
686 }
687 }
688
689 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200690 operation->state != PSA_PAKE_INPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 operation->state != PSA_PAKE_INPUT_X4S) {
692 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200693 }
694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 if (operation->state == PSA_PAKE_STATE_READY) {
696 if (step != PSA_PAKE_STEP_KEY_SHARE) {
697 return PSA_ERROR_BAD_STATE;
698 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 switch (operation->input_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200701 case PSA_PAKE_STEP_X1_X2:
702 operation->state = PSA_PAKE_INPUT_X1_X2;
703 break;
704 case PSA_PAKE_STEP_X2S:
705 operation->state = PSA_PAKE_INPUT_X4S;
706 break;
707 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200709 }
710
711 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
712 }
713
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200714 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200716 case PSA_PAKE_X1_STEP_KEY_SHARE:
717 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if (step != PSA_PAKE_STEP_KEY_SHARE) {
719 return PSA_ERROR_BAD_STATE;
720 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200721 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200722
723 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
724 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
726 return PSA_ERROR_BAD_STATE;
727 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200728 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200729
730 case PSA_PAKE_X1_STEP_ZK_PROOF:
731 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (step != PSA_PAKE_STEP_ZK_PROOF) {
733 return PSA_ERROR_BAD_STATE;
734 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200735 break;
736
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200737 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200739 }
740
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200741 /*
742 * Copy input to local buffer and format it as the Mbed TLS API
743 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
744 * The summary is that the data for each step is prepended with a
745 * length byte, and then they're concatenated. Additionally, the
746 * server's second round output is prepended with a 3-bytes
747 * ECParameters structure - which means we have to prepend that when
748 * we're a client.
749 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (operation->state == PSA_PAKE_INPUT_X4S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200751 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 operation->role == PSA_PAKE_ROLE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200753 /* We only support secp256r1. */
754 /* This is the ECParameters structure defined by RFC 8422. */
755 unsigned char ecparameters[3] = {
756 3, /* named_curve */
757 0, 23 /* secp256r1 */
758 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 memcpy(operation->buffer + operation->buffer_length,
760 ecparameters, sizeof(ecparameters));
761 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200762 }
763
764 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200765 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200766 operation->buffer_length += 1;
767
768 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 memcpy(operation->buffer + operation->buffer_length,
770 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200771 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200772
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200773 /* Load buffer at each last round ZK_PROOF */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (operation->state == PSA_PAKE_INPUT_X1_X2 &&
775 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100776 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 operation->buffer,
778 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200781 operation->buffer_length = 0;
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (ret != 0) {
784 return mbedtls_ecjpake_to_psa_error(ret);
785 }
786 } else if (operation->state == PSA_PAKE_INPUT_X4S &&
787 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100788 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 operation->buffer,
790 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200793 operation->buffer_length = 0;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (ret != 0) {
796 return mbedtls_ecjpake_to_psa_error(ret);
797 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200798 }
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((operation->state == PSA_PAKE_INPUT_X1_X2 &&
801 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
802 (operation->state == PSA_PAKE_INPUT_X4S &&
803 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200804 operation->state = PSA_PAKE_STATE_READY;
805 operation->input_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200806 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200808 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 return PSA_SUCCESS;
812 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200813#else
814 (void) step;
815 (void) input;
816 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200817#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200818 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200819}
820
Przemek Stekiel6c764412022-11-22 14:05:12 +0100821psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200822 psa_pake_step_t step,
823 const uint8_t *input,
824 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200825{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200826 psa_status_t status = mbedtls_psa_pake_input_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200828
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (status != PSA_SUCCESS) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100830 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200834}
835
Neil Armstrong56b8d232022-06-01 18:05:57 +0200836psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100837 mbedtls_psa_pake_operation_t *operation,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200838 psa_key_derivation_operation_t *output)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200839{
840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
841 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
842
Przemek Stekiel6c764412022-11-22 14:05:12 +0100843 if (operation->alg == PSA_ALG_NONE) {
844 return PSA_ERROR_BAD_STATE;
845 }
846
Neil Armstrong5ae60962022-09-15 11:29:46 +0200847 if (operation->input_step != PSA_PAKE_STEP_DERIVE ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 operation->output_step != PSA_PAKE_STEP_DERIVE) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100849 status = PSA_ERROR_BAD_STATE;
850 goto error;
851 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200852
Neil Armstronga557cb82022-06-10 08:58:32 +0200853#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100855 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 operation->buffer,
857 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
858 &operation->buffer_length,
859 mbedtls_psa_get_random,
860 MBEDTLS_PSA_RANDOM_STATE);
861 if (ret != 0) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100862 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200864 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 status = psa_key_derivation_input_bytes(output,
867 PSA_KEY_DERIVATION_INPUT_SECRET,
868 operation->buffer,
869 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200872
Przemek Stekiel6c764412022-11-22 14:05:12 +0100873 mbedtls_psa_pake_abort(operation);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 return status;
876 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200877#else
878 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200879#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200880 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100881
882error:
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 psa_key_derivation_abort(output);
Przemek Stekiel6c764412022-11-22 14:05:12 +0100884 mbedtls_psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200887}
888
Przemek Stekiel6c764412022-11-22 14:05:12 +0100889psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200890{
Przemek Stekiel6c764412022-11-22 14:05:12 +0100891 if (operation->alg == PSA_ALG_NONE) {
892 return PSA_SUCCESS;
893 }
894
Neil Armstronga557cb82022-06-10 08:58:32 +0200895#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrongcb679f22022-09-13 14:43:07 +0200898 operation->input_step = PSA_PAKE_STEP_INVALID;
899 operation->output_step = PSA_PAKE_STEP_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (operation->password_len > 0) {
901 mbedtls_platform_zeroize(operation->password, operation->password_len);
902 }
903 mbedtls_free(operation->password);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100904 operation->password = NULL;
905 operation->password_len = 0;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200906 operation->role = PSA_PAKE_ROLE_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200908 operation->buffer_length = 0;
909 operation->buffer_offset = 0;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100910 mbedtls_ecjpake_free(&operation->ctx.pake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200911 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200912#endif
913
Neil Armstrongcb679f22022-09-13 14:43:07 +0200914 operation->alg = PSA_ALG_NONE;
915 operation->state = PSA_PAKE_STATE_INVALID;
916 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200917
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200919}
920
921#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
922
923#endif /* MBEDTLS_PSA_CRYPTO_C */