blob: 150270c6c3e6aa43416a436944b4a3fd158dc018 [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
Neil Armstrongbcd5bd92022-09-05 18:33:23 +020082/*
83 * The first PAKE step shares the same sequences of the second PAKE step
84 * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs.
Neil Armstrongb39833c2022-09-06 11:36:02 +020085 * It's simpler to share the same sequences numbers of the first
Neil Armstrongbcd5bd92022-09-05 18:33:23 +020086 * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps.
87 *
88 * State sequence with step, state & sequence enums:
89 * => Input & Output Step = PSA_PAKE_STEP_INVALID
90 * => state = PSA_PAKE_STATE_INVALID
91 * psa_pake_setup()
92 * => Input & Output Step = PSA_PAKE_STEP_X1_X2
93 * => state = PSA_PAKE_STATE_SETUP
94 * => sequence = PSA_PAKE_SEQ_INVALID
95 * |
96 * |--- In any order: (First round input before or after first round output)
97 * | | First call of psa_pake_output() or psa_pake_input() sets
98 * | | state = PSA_PAKE_STATE_READY
99 * | |
100 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2
101 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
102 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
103 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
104 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
105 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
106 * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
107 * | | | => state = PSA_PAKE_STATE_READY
108 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200109 * | | | => Output Step = PSA_PAKE_STEP_X2S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200110 * | |
111 * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2
112 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
113 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
114 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
115 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE
116 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC
117 * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF
118 * | | | => state = PSA_PAKE_STATE_READY
119 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200120 * | | | => Output Step = PSA_PAKE_INPUT_X4S
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200121 * |
122 * |--- In any order: (Second round input before or after second round output)
123 * | |
124 * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S
125 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
126 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
127 * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
128 * | | | => state = PSA_PAKE_STATE_READY
129 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200130 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200131 * | |
132 * | |------ In Order: => state = PSA_PAKE_INPUT_X4S
133 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE
134 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC
135 * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF
136 * | | | => state = PSA_PAKE_STATE_READY
137 * | | | => sequence = PSA_PAKE_SEQ_INVALID
Neil Armstrong9720b882022-09-06 11:39:21 +0200138 * | | | => Output Step = PSA_PAKE_STEP_DERIVE
Neil Armstrongbcd5bd92022-09-05 18:33:23 +0200139 * |
140 * psa_pake_get_implicit_key()
141 * => Input & Output Step = PSA_PAKE_STEP_INVALID
142 */
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200143
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200144#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145static psa_status_t mbedtls_ecjpake_to_psa_error(int ret)
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200146{
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 switch (ret) {
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200148 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
149 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
150 case MBEDTLS_ERR_ECP_INVALID_KEY:
151 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return PSA_ERROR_DATA_INVALID;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200153 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
154 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return PSA_ERROR_BUFFER_TOO_SMALL;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200156 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return PSA_ERROR_NOT_SUPPORTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200158 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return PSA_ERROR_CORRUPTION_DETECTED;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200160 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return PSA_ERROR_GENERIC_ERROR;
Neil Armstrongdb05cbf2022-06-15 15:25:45 +0200162 }
163}
164#endif
165
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100166#if defined(MBEDTLS_PSA_BUILTIN_PAKE)
Neil Armstronga557cb82022-06-10 08:58:32 +0200167#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100168static psa_status_t psa_pake_ecjpake_setup(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200169{
170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel4fcc61e2023-02-14 20:05:43 +0100171 mbedtls_ecjpake_role role = (operation->role == PSA_PAKE_ROLE_CLIENT) ?
172 MBEDTLS_ECJPAKE_CLIENT : MBEDTLS_ECJPAKE_SERVER;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200173
Przemek Stekiel4fcc61e2023-02-14 20:05:43 +0100174 mbedtls_ecjpake_init(&operation->ctx.pake);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200175
Przemek Stekiel6c764412022-11-22 14:05:12 +0100176 ret = mbedtls_ecjpake_setup(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 role,
178 MBEDTLS_MD_SHA256,
179 MBEDTLS_ECP_DP_SECP256R1,
180 operation->password,
181 operation->password_len);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_platform_zeroize(operation->password, operation->password_len);
Przemek Stekielad0f3572022-11-21 15:04:37 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (ret != 0) {
186 return mbedtls_ecjpake_to_psa_error(ret);
187 }
Przemek Stekiel0bdec192022-11-22 09:10:35 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200190}
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100191#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100192
193psa_status_t mbedtls_psa_pake_setup(mbedtls_psa_pake_operation_t *operation,
194 const psa_crypto_driver_pake_inputs_t *inputs)
195{
196 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100197 size_t password_len = 0;
198 psa_pake_role_t role = PSA_PAKE_ROLE_NONE;
199 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
200 size_t actual_password_len = 0;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100201
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100202 status = psa_crypto_driver_pake_get_password_len(inputs, &password_len);
203 if (status != PSA_SUCCESS) {
204 return status;
205 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100206
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100207 status = psa_crypto_driver_pake_get_role(inputs, &role);
208 if (status != PSA_SUCCESS) {
209 return status;
210 }
211
212 status = psa_crypto_driver_pake_get_cipher_suite(inputs, &cipher_suite);
213 if (status != PSA_SUCCESS) {
214 return status;
215 }
Przemek Stekiele12ed362022-12-21 12:54:46 +0100216
217#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
218 if (cipher_suite.algorithm == PSA_ALG_JPAKE) {
219 if (cipher_suite.type != PSA_PAKE_PRIMITIVE_TYPE_ECC ||
220 cipher_suite.family != PSA_ECC_FAMILY_SECP_R1 ||
221 cipher_suite.bits != 256 ||
222 cipher_suite.hash != PSA_ALG_SHA_256) {
Przemek Stekiel849c35f2023-02-14 15:11:40 +0100223 return PSA_ERROR_NOT_SUPPORTED;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100224 }
225
Przemek Stekiel2797d372022-12-22 11:19:22 +0100226 operation->password = mbedtls_calloc(1, password_len);
227 if (operation->password == NULL) {
228 status = PSA_ERROR_INSUFFICIENT_MEMORY;
Przemek Stekiel849c35f2023-02-14 15:11:40 +0100229 return status;
Przemek Stekiel2797d372022-12-22 11:19:22 +0100230 }
231
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100232 status = psa_crypto_driver_pake_get_password(inputs, operation->password,
233 password_len, &actual_password_len);
234 if (status != PSA_SUCCESS) {
Przemek Stekiel849c35f2023-02-14 15:11:40 +0100235 return status;
Przemek Stekiel5cbca792023-01-17 16:51:19 +0100236 }
237
238 operation->password_len = actual_password_len;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100239 operation->role = role;
240 operation->alg = cipher_suite.algorithm;
241
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100242 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_JPAKE_BUFFER_SIZE);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100243 operation->buffer_length = 0;
244 operation->buffer_offset = 0;
245
246 status = psa_pake_ecjpake_setup(operation);
Przemek Stekiele12ed362022-12-21 12:54:46 +0100247 if (status != PSA_SUCCESS) {
Przemek Stekiel849c35f2023-02-14 15:11:40 +0100248 return status;
Przemek Stekiele12ed362022-12-21 12:54:46 +0100249 }
250
251 return PSA_SUCCESS;
252 } else
253#else
254 (void) operation;
255 (void) inputs;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200256#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100257 { status = PSA_ERROR_NOT_SUPPORTED; }
258
Przemek Stekiele12ed362022-12-21 12:54:46 +0100259 return status;
260}
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200261
Neil Armstrong56b8d232022-06-01 18:05:57 +0200262static psa_status_t mbedtls_psa_pake_output_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100263 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100264 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 uint8_t *output,
266 size_t output_size,
267 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200268{
269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200270 size_t length;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100271 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100272
Neil Armstronga557cb82022-06-10 08:58:32 +0200273#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200274 /*
275 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
276 * handling of output sequencing.
277 *
Neil Armstrong6a12a772022-09-14 12:17:42 +0200278 * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data
Neil Armstrongfa849622022-09-13 15:10:46 +0200279 * at once, on the other side the PSA CRYPTO PAKE api requires
280 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be
281 * retrieved in sequence.
282 *
283 * In order to achieve API compatibility, the whole X1+X2 or X2S steps
284 * data is stored in an intermediate buffer at first step output call,
285 * and data is sliced down by parsing the ECPoint records in order
286 * to return the right parts on each step.
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (operation->alg == PSA_ALG_JPAKE) {
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200289 /* Initialize & write round on KEY_SHARE sequences */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100290 if (step == PSA_JPAKE_X1_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100291 ret = mbedtls_ecjpake_write_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 operation->buffer,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100293 MBEDTLS_PSA_JPAKE_BUFFER_SIZE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 &operation->buffer_length,
295 mbedtls_psa_get_random,
296 MBEDTLS_PSA_RANDOM_STATE);
297 if (ret != 0) {
298 return mbedtls_ecjpake_to_psa_error(ret);
299 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200300
301 operation->buffer_offset = 0;
Przemek Stekielb09c4872023-01-17 12:05:38 +0100302 } else if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100303 ret = mbedtls_ecjpake_write_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 operation->buffer,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100305 MBEDTLS_PSA_JPAKE_BUFFER_SIZE,
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 &operation->buffer_length,
307 mbedtls_psa_get_random,
308 MBEDTLS_PSA_RANDOM_STATE);
309 if (ret != 0) {
310 return mbedtls_ecjpake_to_psa_error(ret);
311 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200312
313 operation->buffer_offset = 0;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200314 }
315
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200316 /*
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200317 * mbedtls_ecjpake_write_round_xxx() outputs thing in the format
318 * defined by draft-cragie-tls-ecjpake-01 section 7. The summary is
319 * that the data for each step is prepended with a length byte, and
320 * then they're concatenated. Additionally, the server's second round
321 * output is prepended with a 3-bytes ECParameters structure.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200322 *
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200323 * In PSA, we output each step separately, and don't prepend the
324 * output with a length byte, even less a curve identifier, as that
325 * information is already available.
Neil Armstrong1d0294f2022-09-13 14:49:24 +0200326 */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100327 if (step == PSA_JPAKE_X2S_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 operation->role == PSA_PAKE_ROLE_SERVER) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200329 /* Skip ECParameters, with is 3 bytes (RFC 8422) */
330 operation->buffer_offset += 3;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200331 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200332
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200333 /* Read the length byte then move past it to the data */
334 length = operation->buffer[operation->buffer_offset];
335 operation->buffer_offset += 1;
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (operation->buffer_offset + length > operation->buffer_length) {
338 return PSA_ERROR_DATA_CORRUPT;
339 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (output_size < length) {
342 return PSA_ERROR_BUFFER_TOO_SMALL;
343 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 memcpy(output,
346 operation->buffer + operation->buffer_offset,
347 length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200348 *output_length = length;
349
350 operation->buffer_offset += length;
351
352 /* Reset buffer after ZK_PROOF sequence */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100353 if ((step == PSA_JPAKE_X2_STEP_ZK_PROOF) ||
354 (step == PSA_JPAKE_X2S_STEP_ZK_PROOF)) {
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100355 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_JPAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200356 operation->buffer_length = 0;
357 operation->buffer_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return PSA_SUCCESS;
361 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200362#else
363 (void) step;
364 (void) output;
365 (void) output_size;
366 (void) output_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200367#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200368 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200369}
370
Przemek Stekiel6c764412022-11-22 14:05:12 +0100371psa_status_t mbedtls_psa_pake_output(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100372 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200373 uint8_t *output,
374 size_t output_size,
375 size_t *output_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200376{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200377 psa_status_t status = mbedtls_psa_pake_output_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100378 operation, step, output, output_size, output_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200381}
382
Neil Armstrong56b8d232022-06-01 18:05:57 +0200383static psa_status_t mbedtls_psa_pake_input_internal(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100384 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100385 psa_crypto_driver_pake_step_t step,
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 const uint8_t *input,
387 size_t input_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200388{
389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Przemek Stekiel9a5b8122022-12-22 13:34:47 +0100390 (void) step; // Unused parameter
Przemek Stekiel6c764412022-11-22 14:05:12 +0100391
Neil Armstronga557cb82022-06-10 08:58:32 +0200392#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Neil Armstrongfa849622022-09-13 15:10:46 +0200393 /*
394 * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different
395 * handling of input sequencing.
396 *
397 * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data
398 * at once as input, on the other side the PSA CRYPTO PAKE api requires
399 * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be
400 * given in sequence.
401 *
402 * In order to achieve API compatibility, each X1+X2 or X4S step data
403 * is stored sequentially in an intermediate buffer and given to the
404 * MbedTLS JPAKE API on the last step.
405 *
406 * This causes any input error to be only detected on the last step.
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (operation->alg == PSA_ALG_JPAKE) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200409 /*
410 * Copy input to local buffer and format it as the Mbed TLS API
411 * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7.
412 * The summary is that the data for each step is prepended with a
413 * length byte, and then they're concatenated. Additionally, the
414 * server's second round output is prepended with a 3-bytes
415 * ECParameters structure - which means we have to prepend that when
416 * we're a client.
417 */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100418 if (step == PSA_JPAKE_X4S_STEP_KEY_SHARE &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 operation->role == PSA_PAKE_ROLE_CLIENT) {
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200420 /* We only support secp256r1. */
421 /* This is the ECParameters structure defined by RFC 8422. */
422 unsigned char ecparameters[3] = {
423 3, /* named_curve */
424 0, 23 /* secp256r1 */
425 };
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 memcpy(operation->buffer + operation->buffer_length,
427 ecparameters, sizeof(ecparameters));
428 operation->buffer_length += sizeof(ecparameters);
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200429 }
430
431 /* Write the length byte */
Manuel Pégourié-Gonnard0771d412022-10-06 09:30:34 +0200432 operation->buffer[operation->buffer_length] = (uint8_t) input_length;
Manuel Pégourié-Gonnardec7012d2022-10-05 12:17:34 +0200433 operation->buffer_length += 1;
434
435 /* Finally copy the data */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 memcpy(operation->buffer + operation->buffer_length,
437 input, input_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200438 operation->buffer_length += input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200439
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200440 /* Load buffer at each last round ZK_PROOF */
Przemek Stekielb09c4872023-01-17 12:05:38 +0100441 if (step == PSA_JPAKE_X2_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100442 ret = mbedtls_ecjpake_read_round_one(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 operation->buffer,
444 operation->buffer_length);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200445
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100446 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_JPAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200447 operation->buffer_length = 0;
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (ret != 0) {
450 return mbedtls_ecjpake_to_psa_error(ret);
451 }
Przemek Stekielb09c4872023-01-17 12:05:38 +0100452 } else if (step == PSA_JPAKE_X4S_STEP_ZK_PROOF) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100453 ret = mbedtls_ecjpake_read_round_two(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 operation->buffer,
455 operation->buffer_length);
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200456
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100457 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_JPAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200458 operation->buffer_length = 0;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (ret != 0) {
461 return mbedtls_ecjpake_to_psa_error(ret);
462 }
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200463 }
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 return PSA_SUCCESS;
466 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200467#else
468 (void) step;
469 (void) input;
470 (void) input_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200471#endif
Neil Armstrong5ae60962022-09-15 11:29:46 +0200472 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200473}
474
Przemek Stekiel6c764412022-11-22 14:05:12 +0100475psa_status_t mbedtls_psa_pake_input(mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100476 psa_crypto_driver_pake_step_t step,
Neil Armstrong56b8d232022-06-01 18:05:57 +0200477 const uint8_t *input,
478 size_t input_length)
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200479{
Neil Armstrong56b8d232022-06-01 18:05:57 +0200480 psa_status_t status = mbedtls_psa_pake_input_internal(
Przemek Stekielb09c4872023-01-17 12:05:38 +0100481 operation, step, input, input_length);
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return status;
Manuel Pégourié-Gonnardf155ab92022-10-13 13:11:52 +0200484}
485
Neil Armstrong56b8d232022-06-01 18:05:57 +0200486psa_status_t mbedtls_psa_pake_get_implicit_key(
Przemek Stekiel6c764412022-11-22 14:05:12 +0100487 mbedtls_psa_pake_operation_t *operation,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100488 uint8_t *output, size_t output_size,
489 size_t *output_length)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200490{
491 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200492
Neil Armstronga557cb82022-06-10 08:58:32 +0200493#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6c764412022-11-22 14:05:12 +0100495 ret = mbedtls_ecjpake_write_shared_key(&operation->ctx.pake,
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 operation->buffer,
Przemek Stekiel6b648622023-02-19 22:55:33 +0100497 output_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 &operation->buffer_length,
499 mbedtls_psa_get_random,
500 MBEDTLS_PSA_RANDOM_STATE);
501 if (ret != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 return mbedtls_ecjpake_to_psa_error(ret);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200503 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200504
Przemek Stekiel0c781802022-11-29 14:53:13 +0100505 memcpy(output, operation->buffer, operation->buffer_length);
Przemek Stekiel6b648622023-02-19 22:55:33 +0100506 *output_length = operation->buffer_length;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200507
Przemek Stekiel0c781802022-11-29 14:53:13 +0100508 return PSA_SUCCESS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 } else
Neil Armstrong5ae60962022-09-15 11:29:46 +0200510#else
511 (void) output;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200512#endif
Przemek Stekiele12ed362022-12-21 12:54:46 +0100513 { return PSA_ERROR_NOT_SUPPORTED; }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200514}
515
Przemek Stekiel6c764412022-11-22 14:05:12 +0100516psa_status_t mbedtls_psa_pake_abort(mbedtls_psa_pake_operation_t *operation)
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200517{
Neil Armstronga557cb82022-06-10 08:58:32 +0200518#if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE)
Przemek Stekiel6c764412022-11-22 14:05:12 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 if (operation->alg == PSA_ALG_JPAKE) {
Przemek Stekiel6d778302023-02-14 20:24:32 +0100521 mbedtls_platform_zeroize(operation->password, operation->password_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_free(operation->password);
Przemek Stekiel152ae072022-11-17 13:24:36 +0100523 operation->password = NULL;
524 operation->password_len = 0;
Neil Armstrongcb679f22022-09-13 14:43:07 +0200525 operation->role = PSA_PAKE_ROLE_NONE;
Przemek Stekiel251e86a2023-02-17 14:30:50 +0100526 mbedtls_platform_zeroize(operation->buffer, MBEDTLS_PSA_JPAKE_BUFFER_SIZE);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200527 operation->buffer_length = 0;
528 operation->buffer_offset = 0;
Przemek Stekiel6c764412022-11-22 14:05:12 +0100529 mbedtls_ecjpake_free(&operation->ctx.pake);
Neil Armstrong4efd7a42022-06-08 17:18:31 +0200530 }
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200531#endif
532
Neil Armstrongcb679f22022-09-13 14:43:07 +0200533 operation->alg = PSA_ALG_NONE;
Neil Armstrongfbc4b4a2022-06-10 08:54:53 +0200534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return PSA_SUCCESS;
Neil Armstronga4cc7d62022-05-25 11:30:48 +0200536}
537
538#endif /* MBEDTLS_PSA_BUILTIN_PAKE */
539
540#endif /* MBEDTLS_PSA_CRYPTO_C */