blob: 0dafe786d94caa33e945f380f8af79196b23096f [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{
Neil Armstronga557cb82022-06-10 08:58:32 +0200197#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (cipher_suite->algorithm == PSA_ALG_JPAKE) {
199 if (cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200200 cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 ||
201 cipher_suite->bits != 256 ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 cipher_suite->hash != PSA_ALG_SHA_256) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100203 status = PSA_ERROR_NOT_SUPPORTED;
204 goto error;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200205 }
206
207 operation->alg = cipher_suite->algorithm;
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_ecjpake_init(&operation->ctx.ecjpake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200210
211 operation->state = PSA_PAKE_STATE_SETUP;
212 operation->sequence = PSA_PAKE_SEQ_INVALID;
213 operation->input_step = PSA_PAKE_STEP_X1_X2;
214 operation->output_step = PSA_PAKE_STEP_X1_X2;
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200217 operation->buffer_length = 0;
218 operation->buffer_offset = 0;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return PSA_SUCCESS;
221 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200222#else
223 (void) operation;
224 (void) cipher_suite;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200225#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200226 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100227
228error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200229 mbedtls_psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100230 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200231}
232
Neil Armstrong56b8d232022-06-01 18:05:57 +0200233psa_status_t mbedtls_psa_pake_set_password_key(psa_pake_operation_t *operation,
234 mbedtls_svc_key_id_t password)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200235{
236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
237 psa_key_attributes_t attributes = psa_key_attributes_init();
238 psa_key_type_t type;
239 psa_key_usage_t usage;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100240 psa_key_slot_t *slot = NULL;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (operation->alg == PSA_ALG_NONE ||
243 operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100244 status = PSA_ERROR_BAD_STATE;
245 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200246 }
247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 status = psa_get_key_attributes(password, &attributes);
249 if (status != PSA_SUCCESS) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100250 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 type = psa_get_key_type(&attributes);
254 usage = psa_get_key_usage_flags(&attributes);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 psa_reset_key_attributes(&attributes);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (type != PSA_KEY_TYPE_PASSWORD &&
259 type != PSA_KEY_TYPE_PASSWORD_HASH) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100260 status = PSA_ERROR_INVALID_ARGUMENT;
261 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200262 }
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if ((usage & PSA_KEY_USAGE_DERIVE) == 0) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100265 status = PSA_ERROR_NOT_PERMITTED;
266 goto error;
267 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (operation->password != NULL) {
270 return PSA_ERROR_BAD_STATE;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100271 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100272
273 status = psa_get_and_lock_key_slot_with_policy(password, &slot,
274 PSA_KEY_USAGE_DERIVE,
275 PSA_ALG_JPAKE);
276 if (status != PSA_SUCCESS) {
277 return status;
278 }
279
280 operation->password = mbedtls_calloc(1, slot->key.bytes);
281 if (operation->password == NULL) {
282 psa_unlock_key_slot(slot);
283 return PSA_ERROR_INSUFFICIENT_MEMORY;
284 }
285 memcpy(operation->password, slot->key.data, slot->key.bytes);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100286 operation->password_len = slot->key.bytes;
Przemek Stekiel348410f2022-11-15 22:22:07 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 status = psa_unlock_key_slot(slot);
289 if (status != PSA_SUCCESS) {
290 return status;
291 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 return PSA_SUCCESS;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100294
295error:
296 psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200298}
299
Neil Armstrong56b8d232022-06-01 18:05:57 +0200300psa_status_t mbedtls_psa_pake_set_user(psa_pake_operation_t *operation,
301 const uint8_t *user_id,
302 size_t user_id_len)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200303{
Neil Armstrong5ae60962022-09-15 11:29:46 +0200304 (void) user_id;
305 (void) user_id_len;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100306
Neil Armstrong5ae60962022-09-15 11:29:46 +0200307 if (operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100308 status = PSA_ERROR_BAD_STATE;
309 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200310 }
311
Valerio Settifdb77cd2022-11-11 12:02:24 +0100312 status = PSA_ERROR_NOT_SUPPORTED;
313
314error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200315 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200317}
318
Neil Armstrong56b8d232022-06-01 18:05:57 +0200319psa_status_t mbedtls_psa_pake_set_peer(psa_pake_operation_t *operation,
320 const uint8_t *peer_id,
321 size_t peer_id_len)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200322{
Neil Armstrong5ae60962022-09-15 11:29:46 +0200323 (void) peer_id;
324 (void) peer_id_len;
Valerio Settifdb77cd2022-11-11 12:02:24 +0100325
Neil Armstrong5ae60962022-09-15 11:29:46 +0200326 if (operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100327 status = PSA_ERROR_BAD_STATE;
328 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200329 }
330
Valerio Settifdb77cd2022-11-11 12:02:24 +0100331 status = PSA_ERROR_NOT_SUPPORTED;
332
333error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200334 mbedtls_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_role(psa_pake_operation_t *operation,
339 psa_pake_role_t role)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200340{
Neil Armstrong5ae60962022-09-15 11:29:46 +0200341 if (operation->state != PSA_PAKE_STATE_SETUP) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100342 status = PSA_ERROR_BAD_STATE;
343 goto error;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200344 }
345
Neil Armstronga557cb82022-06-10 08:58:32 +0200346#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (operation->alg == PSA_ALG_JPAKE) {
348 if (role != PSA_PAKE_ROLE_CLIENT &&
349 role != PSA_PAKE_ROLE_SERVER) {
350 return PSA_ERROR_NOT_SUPPORTED;
351 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200352
353 operation->role = role;
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return PSA_SUCCESS;
356 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200357#else
358 (void) role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200359#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200360
361 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100362
363error:
Neil Armstrong5ae60962022-09-15 11:29:46 +0200364 mbedtls_psa_pake_abort(operation);
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200366}
367
Neil Armstronga557cb82022-06-10 08:58:32 +0200368#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100369static psa_status_t psa_pake_ecjpake_setup(psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200370{
371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200372 mbedtls_ecjpake_role role;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (operation->role == PSA_PAKE_ROLE_CLIENT) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200375 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 } else if (operation->role == PSA_PAKE_ROLE_SERVER) {
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200377 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 } else {
379 return PSA_ERROR_BAD_STATE;
380 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (operation->password_len == 0) {
383 return PSA_ERROR_BAD_STATE;
384 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 ret = mbedtls_ecjpake_setup(&operation->ctx.ecjpake,
387 role,
388 MBEDTLS_MD_SHA256,
389 MBEDTLS_ECP_DP_SECP256R1,
390 operation->password,
391 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 mbedtls_platform_zeroize(operation->password, operation->password_len);
394 mbedtls_free(operation->password);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100395 operation->password = NULL;
396 operation->password_len = 0;
397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (ret != 0) {
399 return mbedtls_ecjpake_to_psa_error(ret);
400 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100401
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200402 operation->state = PSA_PAKE_STATE_READY;
403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200405}
406#endif
407
Neil Armstrong56b8d232022-06-01 18:05:57 +0200408static psa_status_t mbedtls_psa_pake_output_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 psa_pake_operation_t *operation,
410 psa_pake_step_t step,
411 uint8_t *output,
412 size_t output_size,
413 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200414{
415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
417 size_t length;
418
Neil Armstrong5ae60962022-09-15 11:29:46 +0200419 if (operation->state == PSA_PAKE_STATE_INVALID) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 return PSA_ERROR_BAD_STATE;
421 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200422
Neil Armstronga557cb82022-06-10 08:58:32 +0200423#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200424 /*
425 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
426 * handling of output sequencing.
427 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200428 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200429 * at once, on the other side the PSA CRYPTO PAKE api requires
430 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
431 * retrieved in sequence.
432 *
433 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
434 * data is stored in an intermediate buffer at first step output call,
435 * and data is sliced down by parsing the ECPoint records in order
436 * to return the right parts on each step.
437 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (operation->alg == PSA_ALG_JPAKE) {
439 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200440 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 step != PSA_PAKE_STEP_ZK_PROOF) {
442 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200443 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (operation->state == PSA_PAKE_STATE_SETUP) {
446 status = psa_pake_ecjpake_setup(operation);
447 if (status != PSA_SUCCESS) {
448 return status;
449 }
450 }
451
452 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200453 operation->state != PSA_PAKE_OUTPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 operation->state != PSA_PAKE_OUTPUT_X2S) {
455 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200456 }
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (operation->state == PSA_PAKE_STATE_READY) {
459 if (step != PSA_PAKE_STEP_KEY_SHARE) {
460 return PSA_ERROR_BAD_STATE;
461 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 switch (operation->output_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200464 case PSA_PAKE_STEP_X1_X2:
465 operation->state = PSA_PAKE_OUTPUT_X1_X2;
466 break;
467 case PSA_PAKE_STEP_X2S:
468 operation->state = PSA_PAKE_OUTPUT_X2S;
469 break;
470 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200472 }
473
474 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
475 }
476
477 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200479 case PSA_PAKE_X1_STEP_KEY_SHARE:
480 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (step != PSA_PAKE_STEP_KEY_SHARE) {
482 return PSA_ERROR_BAD_STATE;
483 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200484 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200485
486 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
487 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
489 return PSA_ERROR_BAD_STATE;
490 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200491 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200492
493 case PSA_PAKE_X1_STEP_ZK_PROOF:
494 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (step != PSA_PAKE_STEP_ZK_PROOF) {
496 return PSA_ERROR_BAD_STATE;
497 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200498 break;
499
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200500 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200502 }
503
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200504 /* Initialize & write round on KEY_SHARE sequences */
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
506 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
507 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.ecjpake,
508 operation->buffer,
509 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
510 &operation->buffer_length,
511 mbedtls_psa_get_random,
512 MBEDTLS_PSA_RANDOM_STATE);
513 if (ret != 0) {
514 return mbedtls_ecjpake_to_psa_error(ret);
515 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200516
517 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 } else if (operation->state == PSA_PAKE_OUTPUT_X2S &&
519 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE) {
520 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.ecjpake,
521 operation->buffer,
522 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
523 &operation->buffer_length,
524 mbedtls_psa_get_random,
525 MBEDTLS_PSA_RANDOM_STATE);
526 if (ret != 0) {
527 return mbedtls_ecjpake_to_psa_error(ret);
528 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200529
530 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200531 }
532
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200533 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200534 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
535 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
536 * that the data for each step is prepended with a length byte, and
537 * then they're concatenated. Additionally, the server's second round
538 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200539 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200540 * In PSA, we output each step separately, and don't prepend the
541 * output with a length byte, even less a curve identifier, as that
542 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200543 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (operation->state == PSA_PAKE_OUTPUT_X2S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200545 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 operation->role == PSA_PAKE_ROLE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200547 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
548 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200549 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200550
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200551 /* Read the length byte then move past it to the data */
552 length = operation->buffer[operation->buffer_offset];
553 operation->buffer_offset += 1;
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (operation->buffer_offset + length > operation->buffer_length) {
556 return PSA_ERROR_DATA_CORRUPT;
557 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (output_size < length) {
560 return PSA_ERROR_BUFFER_TOO_SMALL;
561 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 memcpy(output,
564 operation->buffer + operation->buffer_offset,
565 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200566 *output_length = length;
567
568 operation->buffer_offset += length;
569
570 /* Reset buffer after ZK_PROOF sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if ((operation->state == PSA_PAKE_OUTPUT_X1_X2 &&
572 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
573 (operation->state == PSA_PAKE_OUTPUT_X2S &&
574 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
575 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200576 operation->buffer_length = 0;
577 operation->buffer_offset = 0;
578
579 operation->state = PSA_PAKE_STATE_READY;
580 operation->output_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200581 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200583 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return PSA_SUCCESS;
587 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200588#else
589 (void) step;
590 (void) output;
591 (void) output_size;
592 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200593#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200594 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200595}
596
Neil Armstrong56b8d232022-06-01 18:05:57 +0200597psa_status_t mbedtls_psa_pake_output(psa_pake_operation_t *operation,
598 psa_pake_step_t step,
599 uint8_t *output,
600 size_t output_size,
601 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200602{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200603 psa_status_t status = mbedtls_psa_pake_output_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (status != PSA_SUCCESS) {
607 psa_pake_abort(operation);
608 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200611}
612
Neil Armstrong56b8d232022-06-01 18:05:57 +0200613static psa_status_t mbedtls_psa_pake_input_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 psa_pake_operation_t *operation,
615 psa_pake_step_t step,
616 const uint8_t *input,
617 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200618{
619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
620 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200621
Neil Armstrong5ae60962022-09-15 11:29:46 +0200622 if (operation->state == PSA_PAKE_STATE_INVALID) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return PSA_ERROR_BAD_STATE;
624 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200625
Neil Armstronga557cb82022-06-10 08:58:32 +0200626#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200627 /*
628 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
629 * handling of input sequencing.
630 *
631 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
632 * at once as input, on the other side the PSA CRYPTO PAKE api requires
633 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
634 * given in sequence.
635 *
636 * In order to achieve API compatibility, each X1+X2 or X4S step data
637 * is stored sequentially in an intermediate buffer and given to the
638 * MbedTLS JPAKE API on the last step.
639 *
640 * This causes any input error to be only detected on the last step.
641 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (operation->alg == PSA_ALG_JPAKE) {
643 if (step != PSA_PAKE_STEP_KEY_SHARE &&
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200644 step != PSA_PAKE_STEP_ZK_PUBLIC &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 step != PSA_PAKE_STEP_ZK_PROOF) {
646 return PSA_ERROR_INVALID_ARGUMENT;
647 }
Neil Armstrong3d4966a2022-09-13 14:54:15 +0200648
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200649 const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE(
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256);
651 if (input_length > (size_t) PSA_PAKE_INPUT_SIZE(PSA_ALG_JPAKE, prim, step)) {
652 return PSA_ERROR_INVALID_ARGUMENT;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200653 }
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (operation->state == PSA_PAKE_STATE_SETUP) {
656 status = psa_pake_ecjpake_setup(operation);
657 if (status != PSA_SUCCESS) {
658 return status;
659 }
660 }
661
662 if (operation->state != PSA_PAKE_STATE_READY &&
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200663 operation->state != PSA_PAKE_INPUT_X1_X2 &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 operation->state != PSA_PAKE_INPUT_X4S) {
665 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200666 }
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (operation->state == PSA_PAKE_STATE_READY) {
669 if (step != PSA_PAKE_STEP_KEY_SHARE) {
670 return PSA_ERROR_BAD_STATE;
671 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 switch (operation->input_step) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200674 case PSA_PAKE_STEP_X1_X2:
675 operation->state = PSA_PAKE_INPUT_X1_X2;
676 break;
677 case PSA_PAKE_STEP_X2S:
678 operation->state = PSA_PAKE_INPUT_X4S;
679 break;
680 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return PSA_ERROR_BAD_STATE;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200682 }
683
684 operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE;
685 }
686
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200687 /* Check if step matches current sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 switch (operation->sequence) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200689 case PSA_PAKE_X1_STEP_KEY_SHARE:
690 case PSA_PAKE_X2_STEP_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (step != PSA_PAKE_STEP_KEY_SHARE) {
692 return PSA_ERROR_BAD_STATE;
693 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200694 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200695
696 case PSA_PAKE_X1_STEP_ZK_PUBLIC:
697 case PSA_PAKE_X2_STEP_ZK_PUBLIC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (step != PSA_PAKE_STEP_ZK_PUBLIC) {
699 return PSA_ERROR_BAD_STATE;
700 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200701 break;
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200702
703 case PSA_PAKE_X1_STEP_ZK_PROOF:
704 case PSA_PAKE_X2_STEP_ZK_PROOF:
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (step != PSA_PAKE_STEP_ZK_PROOF) {
706 return PSA_ERROR_BAD_STATE;
707 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200708 break;
709
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200710 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return PSA_ERROR_BAD_STATE;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200712 }
713
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200714 /*
715 * Copy input to local buffer and format it as the Mbed TLS API
716 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
717 * The summary is that the data for each step is prepended with a
718 * length byte, and then they're concatenated. Additionally, the
719 * server's second round output is prepended with a 3-bytes
720 * ECParameters structure - which means we have to prepend that when
721 * we're a client.
722 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (operation->state == PSA_PAKE_INPUT_X4S &&
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200724 operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 operation->role == PSA_PAKE_ROLE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200726 /* We only support secp256r1. */
727 /* This is the ECParameters structure defined by RFC 8422. */
728 unsigned char ecparameters[3] = {
729 3, /* named_curve */
730 0, 23 /* secp256r1 */
731 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 memcpy(operation->buffer + operation->buffer_length,
733 ecparameters, sizeof(ecparameters));
734 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200735 }
736
737 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200738 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200739 operation->buffer_length += 1;
740
741 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 memcpy(operation->buffer + operation->buffer_length,
743 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200744 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200745
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200746 /* Load buffer at each last round ZK_PROOF */
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (operation->state == PSA_PAKE_INPUT_X1_X2 &&
748 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) {
749 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.ecjpake,
750 operation->buffer,
751 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200754 operation->buffer_length = 0;
755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (ret != 0) {
757 return mbedtls_ecjpake_to_psa_error(ret);
758 }
759 } else if (operation->state == PSA_PAKE_INPUT_X4S &&
760 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF) {
761 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.ecjpake,
762 operation->buffer,
763 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200766 operation->buffer_length = 0;
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (ret != 0) {
769 return mbedtls_ecjpake_to_psa_error(ret);
770 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200771 }
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if ((operation->state == PSA_PAKE_INPUT_X1_X2 &&
774 operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF) ||
775 (operation->state == PSA_PAKE_INPUT_X4S &&
776 operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF)) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200777 operation->state = PSA_PAKE_STATE_READY;
778 operation->input_step++;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200779 operation->sequence = PSA_PAKE_SEQ_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 } else {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200781 operation->sequence++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 return PSA_SUCCESS;
785 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200786#else
787 (void) step;
788 (void) input;
789 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200790#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200791 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200792}
793
Neil Armstrong56b8d232022-06-01 18:05:57 +0200794psa_status_t mbedtls_psa_pake_input(psa_pake_operation_t *operation,
795 psa_pake_step_t step,
796 const uint8_t *input,
797 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200798{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200799 psa_status_t status = mbedtls_psa_pake_input_internal(
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (status != PSA_SUCCESS) {
803 psa_pake_abort(operation);
804 }
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200807}
808
Neil Armstrong56b8d232022-06-01 18:05:57 +0200809psa_status_t mbedtls_psa_pake_get_implicit_key(
810 psa_pake_operation_t *operation,
811 psa_key_derivation_operation_t *output)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200812{
813 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
814 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
815
Neil Armstrong5ae60962022-09-15 11:29:46 +0200816 if (operation->input_step != PSA_PAKE_STEP_DERIVE ||
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 operation->output_step != PSA_PAKE_STEP_DERIVE) {
Valerio Settifdb77cd2022-11-11 12:02:24 +0100818 status = PSA_ERROR_BAD_STATE;
819 goto error;
820 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200821
Neil Armstronga557cb82022-06-10 08:58:32 +0200822#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (operation->alg == PSA_ALG_JPAKE) {
824 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.ecjpake,
825 operation->buffer,
826 MBEDTLS_PSA_PAKE_BUFFER_SIZE,
827 &operation->buffer_length,
828 mbedtls_psa_get_random,
829 MBEDTLS_PSA_RANDOM_STATE);
830 if (ret != 0) {
831 psa_pake_abort(operation);
832 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200833 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 status = psa_key_derivation_input_bytes(output,
836 PSA_KEY_DERIVATION_INPUT_SECRET,
837 operation->buffer,
838 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 psa_pake_abort(operation);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 return status;
845 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200846#else
847 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200848#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200849 { status = PSA_ERROR_NOT_SUPPORTED; }
Valerio Settifdb77cd2022-11-11 12:02:24 +0100850
851error:
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 psa_key_derivation_abort(output);
853 psa_pake_abort(operation);
Valerio Settifdb77cd2022-11-11 12:02:24 +0100854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 return status;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200856}
857
Neil Armstrong56b8d232022-06-01 18:05:57 +0200858psa_status_t mbedtls_psa_pake_abort(psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200859{
Neil Armstronga557cb82022-06-10 08:58:32 +0200860#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrongcb679f22022-09-13 14:43:07 +0200862 operation->input_step = PSA_PAKE_STEP_INVALID;
863 operation->output_step = PSA_PAKE_STEP_INVALID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (operation->password_len > 0) {
865 mbedtls_platform_zeroize(operation->password, operation->password_len);
866 }
867 mbedtls_free(operation->password);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100868 operation->password = NULL;
869 operation->password_len = 0;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200870 operation->role = PSA_PAKE_ROLE_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200872 operation->buffer_length = 0;
873 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_ecjpake_free(&operation->ctx.ecjpake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200875 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200876#endif
877
Neil Armstrongcb679f22022-09-13 14:43:07 +0200878 operation->alg = PSA_ALG_NONE;
879 operation->state = PSA_PAKE_STATE_INVALID;
880 operation->sequence = PSA_PAKE_SEQ_INVALID;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200883}
884
885#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
886
887#endif /* MBEDTLS_PSA_CRYPTO_C */