blob: 7171f75662d31f45bf832718d76dfe862f83e748 [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)
Neil Armstrong56b8d232022-06-01 18:05:57 +0200194psa_status_t mbedtls_psa_pake_setup(psa_pake_operation_t *operation,
195 const psa_pake_cipher_suite_t *cipher_suite)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200196{
Valerio Setti6d4e75f2022-11-21 14:56:56 +0100197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100198
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200199 /* A context must be freshly initialized before it can be set up. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (operation->alg != PSA_ALG_NONE) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100201 status = PSA_ERROR_BAD_STATE;
202 goto error;
203 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 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) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100210 status = PSA_ERROR_INVALID_ARGUMENT;
211 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200212 }
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
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 mbedtls_ecjpake_init(&operation->ctx.ecjpake);
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;
232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_SUCCESS;
238 } else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200239#endif
Valerio Settifdb77cd2022-11-11 12:02:24 +0100240 status = PSA_ERROR_NOT_SUPPORTED;
241
242error:
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100244 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200245}
246
Neil Armstrong56b8d232022-06-01 18:05:57 +0200247psa_status_t mbedtls_psa_pake_set_password_key(psa_pake_operation_t *operation,
248 mbedtls_svc_key_id_t password)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200249{
250 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
251 psa_key_attributes_t attributes = psa_key_attributes_init();
252 psa_key_type_t type;
253 psa_key_usage_t usage;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100254 psa_key_slot_t *slot = NULL;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (operation->alg == PSA_ALG_NONE ||
257 operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100258 status = PSA_ERROR_BAD_STATE;
259 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200260 }
261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 status = psa_get_key_attributes(password, &attributes);
263 if (status != PSA_SUCCESS) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100264 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 type = psa_get_key_type(&attributes);
268 usage = psa_get_key_usage_flags(&attributes);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 psa_reset_key_attributes(&attributes);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (type != PSA_KEY_TYPE_PASSWORD &&
273 type != PSA_KEY_TYPE_PASSWORD_HASH) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100274 status = PSA_ERROR_INVALID_ARGUMENT;
275 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200276 }
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if ((usage & PSA_KEY_USAGE_DERIVE) == 0) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100279 status = PSA_ERROR_NOT_PERMITTED;
280 goto error;
281 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (operation->password != NULL) {
284 return PSA_ERROR_BAD_STATE;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100285 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100286
287 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
288 PSA_KEY_USAGE_DERIVE,
289 PSA_ALG_JPAKE);
290 if (status != PSA_SUCCESS) {
291 return status;
292 }
293
294 operation->password = mbedtls_calloc(1, slot->key.bytes);
295 if (operation->password == NULL) {
296 psa_unlock_key_slot(slot);
297 return PSA_ERROR_INSUFFICIENT_MEMORY;
298 }
299 memcpy(operation->password, slot->key.data, slot->key.bytes);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100300 operation->password_len = slot->key.bytes;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 status = psa_unlock_key_slot(slot);
303 if (status != PSA_SUCCESS) {
304 return status;
305 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return PSA_SUCCESS;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100308
309error:
310 psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200312}
313
Neil Armstrong56b8d232022-06-01 18:05:57 +0200314psa_status_t mbedtls_psa_pake_set_user(psa_pake_operation_t *operation,
315 const uint8_t *user_id,
316 size_t user_id_len)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200317{
Valerio Setti6d4e75f2022-11-21 14:56:56 +0100318 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (operation->alg == PSA_ALG_NONE ||
321 operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100322 status = PSA_ERROR_BAD_STATE;
323 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200324 }
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (user_id_len == 0 || user_id == NULL) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100327 status = PSA_ERROR_INVALID_ARGUMENT;
328 goto error;
329 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200330
Valerio Settifdb77cd2022-11-11 12:02:24 +0100331 status = PSA_ERROR_NOT_SUPPORTED;
332
333error:
334 psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200336}
337
Neil Armstrong56b8d232022-06-01 18:05:57 +0200338psa_status_t mbedtls_psa_pake_set_peer(psa_pake_operation_t *operation,
339 const uint8_t *peer_id,
340 size_t peer_id_len)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200341{
Valerio Setti6d4e75f2022-11-21 14:56:56 +0100342 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (operation->alg == PSA_ALG_NONE ||
345 operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100346 status = PSA_ERROR_BAD_STATE;
347 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200348 }
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (peer_id_len == 0 || peer_id == NULL) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100351 status = PSA_ERROR_INVALID_ARGUMENT;
352 goto error;
353 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200354
Valerio Settifdb77cd2022-11-11 12:02:24 +0100355 status = PSA_ERROR_NOT_SUPPORTED;
356
357error:
358 psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200360}
361
Neil Armstrong56b8d232022-06-01 18:05:57 +0200362psa_status_t mbedtls_psa_pake_set_role(psa_pake_operation_t *operation,
363 psa_pake_role_t role)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200364{
Valerio Setti6d4e75f2022-11-21 14:56:56 +0100365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (operation->alg == PSA_ALG_NONE ||
368 operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100369 status = PSA_ERROR_BAD_STATE;
370 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200371 }
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (role != PSA_PAKE_ROLE_NONE &&
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200374 role != PSA_PAKE_ROLE_FIRST &&
375 role != PSA_PAKE_ROLE_SECOND &&
376 role != PSA_PAKE_ROLE_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 role != PSA_PAKE_ROLE_SERVER) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100378 status = PSA_ERROR_INVALID_ARGUMENT;
379 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200380 }
381
Neil Armstronga557cb82022-06-10 08:58:32 +0200382#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (operation->alg == PSA_ALG_JPAKE) {
384 if (role != PSA_PAKE_ROLE_CLIENT &&
385 role != PSA_PAKE_ROLE_SERVER) {
386 return PSA_ERROR_NOT_SUPPORTED;
387 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200388
389 operation->role = role;
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 return PSA_SUCCESS;
392 } else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200393#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 status = PSA_ERROR_NOT_SUPPORTED;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100395
396error:
397 psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200399}
400
Neil Armstronga557cb82022-06-10 08:58:32 +0200401#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100402static psa_status_t psa_pake_ecjpake_setup(psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200403{
404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200405 mbedtls_ecjpake_role role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (operation->role == PSA_PAKE_ROLE_CLIENT) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200408 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 } else if (operation->role == PSA_PAKE_ROLE_SERVER) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200410 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 } else {
412 return PSA_ERROR_BAD_STATE;
413 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (operation->password_len == 0) {
416 return PSA_ERROR_BAD_STATE;
417 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 ret = mbedtls_ecjpake_setup(&operation->ctx.ecjpake,
420 role,
421 MBEDTLS_MD_SHA256,
422 MBEDTLS_ECP_DP_SECP256R1,
423 operation->password,
424 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_platform_zeroize(operation->password, operation->password_len);
427 mbedtls_free(operation->password);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100428 operation->password = NULL;
429 operation->password_len = 0;
430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (ret != 0) {
432 return mbedtls_ecjpake_to_psa_error(ret);
433 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100434
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200435 operation->state = PSA_PAKE_STATE_READY;
436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200438}
439#endif
440
Neil Armstrong56b8d232022-06-01 18:05:57 +0200441static psa_status_t mbedtls_psa_pake_output_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 psa_pake_operation_t *operation,
443 psa_pake_step_t step,
444 uint8_t *output,
445 size_t output_size,
446 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200447{
448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
450 size_t length;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (operation->alg == PSA_ALG_NONE ||
453 operation->state == PSA_PAKE_STATE_INVALID) {
454 return PSA_ERROR_BAD_STATE;
455 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (output == NULL || output_size == 0 || output_length == NULL) {
458 return PSA_ERROR_INVALID_ARGUMENT;
459 }
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200460
Neil Armstronga557cb82022-06-10 08:58:32 +0200461#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200462 /*
463 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
464 * handling of output sequencing.
465 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200466 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200467 * at once, on the other side the PSA CRYPTO PAKE api requires
468 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
469 * retrieved in sequence.
470 *
471 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
472 * data is stored in an intermediate buffer at first step output call,
473 * and data is sliced down by parsing the ECPoint records in order
474 * to return the right parts on each step.
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (operation->alg == PSA_ALG_JPAKE) {
477 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200478 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 step != PSA_PAKE_STEP_ZK_PROOF) {
480 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200481 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (operation->state == PSA_PAKE_STATE_SETUP) {
484 status = psa_pake_ecjpake_setup(operation);
485 if (status != PSA_SUCCESS) {
486 return status;
487 }
488 }
489
490 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200491 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 operation->state != PSA_PAKE_OUTPUT_X2S) {
493 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (operation->state == PSA_PAKE_STATE_READY) {
497 if (step != PSA_PAKE_STEP_KEY_SHARE) {
498 return PSA_ERROR_BAD_STATE;
499 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 switch (operation->output_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200502 case PSA_PAKE_STEP_X1_X2:
503 operation->state = PSA_PAKE_OUTPUT_X1_X2;
504 break;
505 case PSA_PAKE_STEP_X2S:
506 operation->state = PSA_PAKE_OUTPUT_X2S;
507 break;
508 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200510 }
511
512 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
513 }
514
515 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200517 case PSA_PAKE_X1_STEP_KEY_SHARE:
518 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (step != PSA_PAKE_STEP_KEY_SHARE) {
520 return PSA_ERROR_BAD_STATE;
521 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200522 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200523
524 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
525 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
527 return PSA_ERROR_BAD_STATE;
528 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200529 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200530
531 case PSA_PAKE_X1_STEP_ZK_PROOF:
532 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (step != PSA_PAKE_STEP_ZK_PROOF) {
534 return PSA_ERROR_BAD_STATE;
535 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200536 break;
537
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200538 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200540 }
541
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200542 /* Initialize & write round on KEY_SHARE sequences */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
544 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
545 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.ecjpake,
546 operation->buffer,
547 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
548 &operation->buffer_length,
549 mbedtls_psa_get_random,
550 MBEDTLS_PSA_RANDOM_STATE);
551 if (ret != 0) {
552 return mbedtls_ecjpake_to_psa_error(ret);
553 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200554
555 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 } else if (operation->state == PSA_PAKE_OUTPUT_X2S &&
557 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
558 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.ecjpake,
559 operation->buffer,
560 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
561 &operation->buffer_length,
562 mbedtls_psa_get_random,
563 MBEDTLS_PSA_RANDOM_STATE);
564 if (ret != 0) {
565 return mbedtls_ecjpake_to_psa_error(ret);
566 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200567
568 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200569 }
570
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200571 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200572 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
573 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
574 * that the data for each step is prepended with a length byte, and
575 * then they're concatenated. Additionally, the server's second round
576 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200577 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200578 * In PSA, we output each step separately, and don't prepend the
579 * output with a length byte, even less a curve identifier, as that
580 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200581 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (operation->state == PSA_PAKE_OUTPUT_X2S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200583 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 operation->role == PSA_PAKE_ROLE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200585 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
586 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200587 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200588
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200589 /* Read the length byte then move past it to the data */
590 length = operation->buffer[operation->buffer_offset];
591 operation->buffer_offset += 1;
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 if (operation->buffer_offset + length > operation->buffer_length) {
594 return PSA_ERROR_DATA_CORRUPT;
595 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (output_size < length) {
598 return PSA_ERROR_BUFFER_TOO_SMALL;
599 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 memcpy(output,
602 operation->buffer + operation->buffer_offset,
603 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200604 *output_length = length;
605
606 operation->buffer_offset += length;
607
608 /* Reset buffer after ZK_PROOF sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if ((operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
610 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
611 (operation->state == PSA_PAKE_OUTPUT_X2S &&
612 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
613 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200614 operation->buffer_length = 0;
615 operation->buffer_offset = 0;
616
617 operation->state = PSA_PAKE_STATE_READY;
618 operation->output_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200619 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200621 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return PSA_SUCCESS;
625 } else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200626#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200628}
629
Neil Armstrong56b8d232022-06-01 18:05:57 +0200630psa_status_t mbedtls_psa_pake_output(psa_pake_operation_t *operation,
631 psa_pake_step_t step,
632 uint8_t *output,
633 size_t output_size,
634 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200635{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200636 psa_status_t status = mbedtls_psa_pake_output_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (status != PSA_SUCCESS) {
640 psa_pake_abort(operation);
641 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200644}
645
Neil Armstrong56b8d232022-06-01 18:05:57 +0200646static psa_status_t mbedtls_psa_pake_input_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 psa_pake_operation_t *operation,
648 psa_pake_step_t step,
649 const uint8_t *input,
650 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200651{
652 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
653 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (operation->alg == PSA_ALG_NONE ||
656 operation->state == PSA_PAKE_STATE_INVALID) {
657 return PSA_ERROR_BAD_STATE;
658 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (input == NULL || input_length == 0) {
661 return PSA_ERROR_INVALID_ARGUMENT;
662 }
Neil Armstrong0d001ef2022-06-08 17:42:52 +0200663
Neil Armstronga557cb82022-06-10 08:58:32 +0200664#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200665 /*
666 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
667 * handling of input sequencing.
668 *
669 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
670 * at once as input, on the other side the PSA CRYPTO PAKE api requires
671 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
672 * given in sequence.
673 *
674 * In order to achieve API compatibility, each X1+X2 or X4S step data
675 * is stored sequentially in an intermediate buffer and given to the
676 * MbedTLS JPAKE API on the last step.
677 *
678 * This causes any input error to be only detected on the last step.
679 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (operation->alg == PSA_ALG_JPAKE) {
681 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200682 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 step != PSA_PAKE_STEP_ZK_PROOF) {
684 return PSA_ERROR_INVALID_ARGUMENT;
685 }
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200686
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200687 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256);
689 if (input_length > (size_t) PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, prim, step)) {
690 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200691 }
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (operation->state == PSA_PAKE_STATE_SETUP) {
694 status = psa_pake_ecjpake_setup(operation);
695 if (status != PSA_SUCCESS) {
696 return status;
697 }
698 }
699
700 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200701 operation->state != PSA_PAKE_INPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 operation->state != PSA_PAKE_INPUT_X4S) {
703 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if (operation->state == PSA_PAKE_STATE_READY) {
707 if (step != PSA_PAKE_STEP_KEY_SHARE) {
708 return PSA_ERROR_BAD_STATE;
709 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 switch (operation->input_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200712 case PSA_PAKE_STEP_X1_X2:
713 operation->state = PSA_PAKE_INPUT_X1_X2;
714 break;
715 case PSA_PAKE_STEP_X2S:
716 operation->state = PSA_PAKE_INPUT_X4S;
717 break;
718 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200720 }
721
722 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
723 }
724
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200725 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200727 case PSA_PAKE_X1_STEP_KEY_SHARE:
728 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (step != PSA_PAKE_STEP_KEY_SHARE) {
730 return PSA_ERROR_BAD_STATE;
731 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200732 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200733
734 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
735 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
737 return PSA_ERROR_BAD_STATE;
738 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200739 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200740
741 case PSA_PAKE_X1_STEP_ZK_PROOF:
742 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (step != PSA_PAKE_STEP_ZK_PROOF) {
744 return PSA_ERROR_BAD_STATE;
745 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200746 break;
747
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200748 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200750 }
751
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200752 /*
753 * Copy input to local buffer and format it as the Mbed TLS API
754 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
755 * The summary is that the data for each step is prepended with a
756 * length byte, and then they're concatenated. Additionally, the
757 * server's second round output is prepended with a 3-bytes
758 * ECParameters structure - which means we have to prepend that when
759 * we're a client.
760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 if (operation->state == PSA_PAKE_INPUT_X4S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200762 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 operation->role == PSA_PAKE_ROLE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200764 /* We only support secp256r1. */
765 /* This is the ECParameters structure defined by RFC 8422. */
766 unsigned char ecparameters[3] = {
767 3, /* named_curve */
768 0, 23 /* secp256r1 */
769 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 memcpy(operation->buffer + operation->buffer_length,
771 ecparameters, sizeof(ecparameters));
772 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200773 }
774
775 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200776 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200777 operation->buffer_length += 1;
778
779 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 memcpy(operation->buffer + operation->buffer_length,
781 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200782 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200783
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200784 /* Load buffer at each last round ZK_PROOF */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (operation->state == PSA_PAKE_INPUT_X1_X2 &&
786 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) {
787 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.ecjpake,
788 operation->buffer,
789 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200792 operation->buffer_length = 0;
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (ret != 0) {
795 return mbedtls_ecjpake_to_psa_error(ret);
796 }
797 } else if (operation->state == PSA_PAKE_INPUT_X4S &&
798 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) {
799 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.ecjpake,
800 operation->buffer,
801 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200804 operation->buffer_length = 0;
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 if (ret != 0) {
807 return mbedtls_ecjpake_to_psa_error(ret);
808 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200809 }
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if ((operation->state == PSA_PAKE_INPUT_X1_X2 &&
812 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
813 (operation->state == PSA_PAKE_INPUT_X4S &&
814 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200815 operation->state = PSA_PAKE_STATE_READY;
816 operation->input_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200817 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200819 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 return PSA_SUCCESS;
823 } else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200824#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200826}
827
Neil Armstrong56b8d232022-06-01 18:05:57 +0200828psa_status_t mbedtls_psa_pake_input(psa_pake_operation_t *operation,
829 psa_pake_step_t step,
830 const uint8_t *input,
831 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200832{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200833 psa_status_t status = mbedtls_psa_pake_input_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 if (status != PSA_SUCCESS) {
837 psa_pake_abort(operation);
838 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200841}
842
Neil Armstrong56b8d232022-06-01 18:05:57 +0200843psa_status_t mbedtls_psa_pake_get_implicit_key(
844 psa_pake_operation_t *operation,
845 psa_key_derivation_operation_t *output)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200846{
847 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
848 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 if (operation->alg == PSA_ALG_NONE ||
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200851 operation->state != PSA_PAKE_STATE_READY ||
Neil Armstrong1e855602022-06-15 11:32:11 +0200852 operation->input_step != PSA_PAKE_STEP_DERIVE ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 operation->output_step != PSA_PAKE_STEP_DERIVE) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100854 status = PSA_ERROR_BAD_STATE;
855 goto error;
856 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200857
Neil Armstronga557cb82022-06-10 08:58:32 +0200858#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (operation->alg == PSA_ALG_JPAKE) {
860 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.ecjpake,
861 operation->buffer,
862 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
863 &operation->buffer_length,
864 mbedtls_psa_get_random,
865 MBEDTLS_PSA_RANDOM_STATE);
866 if (ret != 0) {
867 psa_pake_abort(operation);
868 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200869 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200870
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 status = psa_key_derivation_input_bytes(output,
872 PSA_KEY_DERIVATION_INPUT_SECRET,
873 operation->buffer,
874 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 psa_pake_abort(operation);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return status;
881 } else
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200882#endif
Valerio Settifdb77cd2022-11-11 12:02:24 +0100883 status = PSA_ERROR_NOT_SUPPORTED;
884
885error:
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 psa_key_derivation_abort(output);
887 psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200890}
891
Neil Armstrong56b8d232022-06-01 18:05:57 +0200892psa_status_t mbedtls_psa_pake_abort(psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200893{
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if (operation->alg == PSA_ALG_NONE) {
895 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200896 }
897
Neil Armstronga557cb82022-06-10 08:58:32 +0200898#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrongcb679f22022-09-13 14:43:07 +0200900 operation->input_step = PSA_PAKE_STEP_INVALID;
901 operation->output_step = PSA_PAKE_STEP_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 if (operation->password_len > 0) {
903 mbedtls_platform_zeroize(operation->password, operation->password_len);
904 }
905 mbedtls_free(operation->password);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100906 operation->password = NULL;
907 operation->password_len = 0;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200908 operation->role = PSA_PAKE_ROLE_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200910 operation->buffer_length = 0;
911 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 mbedtls_ecjpake_free(&operation->ctx.ecjpake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200913 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200914#endif
915
Neil Armstrongcb679f22022-09-13 14:43:07 +0200916 operation->alg = PSA_ALG_NONE;
917 operation->state = PSA_PAKE_STATE_INVALID;
918 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200921}
922
923#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
924
925#endif /* MBEDTLS_PSA_CRYPTO_C */