Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PSA PAKE layer on top of Mbed TLS software crypto |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | * not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
| 23 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 24 | |
| 25 | #include <psa/crypto.h> |
| 26 | #include "psa_crypto_core.h" |
| 27 | #include "psa_crypto_slot_management.h" |
| 28 | |
| 29 | #include <mbedtls/ecjpake.h> |
| 30 | #include <mbedtls/psa_util.h> |
| 31 | |
| 32 | #include <mbedtls/platform.h> |
| 33 | #include <mbedtls/error.h> |
| 34 | #include <string.h> |
| 35 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 36 | /* |
| 37 | * State sequence: |
| 38 | * |
| 39 | * psa_pake_setup() |
| 40 | * | |
| 41 | * |-- In any order: |
| 42 | * | | psa_pake_set_password_key() |
| 43 | * | | psa_pake_set_user() |
| 44 | * | | psa_pake_set_peer() |
Neil Armstrong | c29f847 | 2022-06-08 13:34:49 +0200 | [diff] [blame] | 45 | * | | psa_pake_set_role() |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 46 | * | |
| 47 | * |--- In any order: (First round input before or after first round output) |
| 48 | * | | |
| 49 | * | |------ In Order |
| 50 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 51 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 52 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 53 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 54 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 55 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 56 | * | | |
| 57 | * | |------ In Order: |
| 58 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 59 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 60 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 61 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 62 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 63 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 64 | * | |
| 65 | * |--- In any order: (Second round input before or after second round output) |
| 66 | * | | |
| 67 | * | |------ In Order |
| 68 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 69 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 70 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 71 | * | | |
| 72 | * | |------ In Order: |
| 73 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 74 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 75 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 76 | * | |
| 77 | * psa_pake_get_implicit_key() |
| 78 | * psa_pake_abort() |
| 79 | */ |
| 80 | |
| 81 | enum psa_pake_step |
| 82 | { |
| 83 | PSA_PAKE_STEP_INVALID = 0, |
| 84 | PSA_PAKE_STEP_X1_X2 = 1, |
| 85 | PSA_PAKE_STEP_X2S = 2, |
| 86 | PSA_PAKE_STEP_DERIVE = 3, |
| 87 | }; |
| 88 | |
| 89 | enum psa_pake_state |
| 90 | { |
| 91 | PSA_PAKE_STATE_INVALID = 0, |
| 92 | PSA_PAKE_STATE_SETUP = 1, |
| 93 | PSA_PAKE_STATE_READY = 2, |
| 94 | PSA_PAKE_OUTPUT_X1_X2 = 3, |
| 95 | PSA_PAKE_OUTPUT_X2S = 4, |
| 96 | PSA_PAKE_INPUT_X1_X2 = 5, |
| 97 | PSA_PAKE_INPUT_X4S = 6, |
| 98 | }; |
| 99 | |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 100 | /* |
| 101 | * The first PAKE step shares the same sequences of the second PAKE step |
| 102 | * but with a second set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs. |
Neil Armstrong | b39833c | 2022-09-06 11:36:02 +0200 | [diff] [blame] | 103 | * It's simpler to share the same sequences numbers of the first |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 104 | * set of KEY_SHARE/ZK_PUBLIC/ZK_PROOF outputs/inputs in both PAKE steps. |
| 105 | * |
| 106 | * State sequence with step, state & sequence enums: |
| 107 | * => Input & Output Step = PSA_PAKE_STEP_INVALID |
| 108 | * => state = PSA_PAKE_STATE_INVALID |
| 109 | * psa_pake_setup() |
| 110 | * => Input & Output Step = PSA_PAKE_STEP_X1_X2 |
| 111 | * => state = PSA_PAKE_STATE_SETUP |
| 112 | * => sequence = PSA_PAKE_SEQ_INVALID |
| 113 | * | |
| 114 | * |--- In any order: (First round input before or after first round output) |
| 115 | * | | First call of psa_pake_output() or psa_pake_input() sets |
| 116 | * | | state = PSA_PAKE_STATE_READY |
| 117 | * | | |
| 118 | * | |------ In Order: => state = PSA_PAKE_OUTPUT_X1_X2 |
| 119 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 120 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 121 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 122 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE |
| 123 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC |
| 124 | * | | | psa_pake_output() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF |
| 125 | * | | | => state = PSA_PAKE_STATE_READY |
| 126 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 127 | * | | | => Output Step = PSA_PAKE_STEP_X2S |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 128 | * | | |
| 129 | * | |------ In Order: => state = PSA_PAKE_INPUT_X1_X2 |
| 130 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 131 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 132 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 133 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_KEY_SHARE |
| 134 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PUBLIC |
| 135 | * | | | psa_pake_input() => sequence = PSA_PAKE_X2_STEP_ZK_PROOF |
| 136 | * | | | => state = PSA_PAKE_STATE_READY |
| 137 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 138 | * | | | => Output Step = PSA_PAKE_INPUT_X4S |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 139 | * | |
| 140 | * |--- In any order: (Second round input before or after second round output) |
| 141 | * | | |
| 142 | * | |------ In Order: => state = PSA_PAKE_OUTPUT_X2S |
| 143 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 144 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 145 | * | | | psa_pake_output() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 146 | * | | | => state = PSA_PAKE_STATE_READY |
| 147 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 148 | * | | | => Output Step = PSA_PAKE_STEP_DERIVE |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 149 | * | | |
| 150 | * | |------ In Order: => state = PSA_PAKE_INPUT_X4S |
| 151 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_KEY_SHARE |
| 152 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PUBLIC |
| 153 | * | | | psa_pake_input() => sequence = PSA_PAKE_X1_STEP_ZK_PROOF |
| 154 | * | | | => state = PSA_PAKE_STATE_READY |
| 155 | * | | | => sequence = PSA_PAKE_SEQ_INVALID |
Neil Armstrong | 9720b88 | 2022-09-06 11:39:21 +0200 | [diff] [blame] | 156 | * | | | => Output Step = PSA_PAKE_STEP_DERIVE |
Neil Armstrong | bcd5bd9 | 2022-09-05 18:33:23 +0200 | [diff] [blame] | 157 | * | |
| 158 | * psa_pake_get_implicit_key() |
| 159 | * => Input & Output Step = PSA_PAKE_STEP_INVALID |
| 160 | */ |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 161 | enum psa_pake_sequence |
| 162 | { |
| 163 | PSA_PAKE_SEQ_INVALID = 0, |
| 164 | PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */ |
| 165 | PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */ |
| 166 | PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */ |
| 167 | PSA_PAKE_X2_STEP_KEY_SHARE = 4, |
| 168 | PSA_PAKE_X2_STEP_ZK_PUBLIC = 5, |
| 169 | PSA_PAKE_X2_STEP_ZK_PROOF = 6, |
| 170 | PSA_PAKE_SEQ_END = 7, |
| 171 | }; |
| 172 | |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 173 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
| 174 | static psa_status_t mbedtls_ecjpake_to_psa_error( int ret ) |
| 175 | { |
| 176 | switch( ret ) |
| 177 | { |
| 178 | case MBEDTLS_ERR_MPI_BAD_INPUT_DATA: |
| 179 | case MBEDTLS_ERR_ECP_BAD_INPUT_DATA: |
| 180 | case MBEDTLS_ERR_ECP_INVALID_KEY: |
| 181 | case MBEDTLS_ERR_ECP_VERIFY_FAILED: |
| 182 | return( PSA_ERROR_DATA_INVALID ); |
| 183 | case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL: |
| 184 | case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL: |
| 185 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 186 | case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE: |
| 187 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 188 | case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED: |
| 189 | return( PSA_ERROR_CORRUPTION_DETECTED ); |
| 190 | default: |
| 191 | return( PSA_ERROR_GENERIC_ERROR ); |
| 192 | } |
| 193 | } |
| 194 | #endif |
| 195 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 196 | #if defined(MBEDTLS_PSA_BUILTIN_PAKE) |
| 197 | psa_status_t psa_pake_setup( psa_pake_operation_t *operation, |
| 198 | const psa_pake_cipher_suite_t *cipher_suite) |
| 199 | { |
| 200 | /* A context must be freshly initialized before it can be set up. */ |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 201 | if( operation->alg != PSA_ALG_NONE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 202 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 203 | |
| 204 | if( cipher_suite == NULL || |
| 205 | PSA_ALG_IS_PAKE(cipher_suite->algorithm ) == 0 || |
| 206 | ( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC && |
| 207 | cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH ) || |
| 208 | PSA_ALG_IS_HASH( cipher_suite->hash ) == 0 ) |
| 209 | { |
| 210 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 211 | } |
| 212 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 213 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 214 | if( cipher_suite->algorithm == PSA_ALG_JPAKE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 215 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 216 | if( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC || |
| 217 | cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 || |
| 218 | cipher_suite->bits != 256 || |
| 219 | cipher_suite->hash != PSA_ALG_SHA_256 ) |
| 220 | { |
| 221 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 222 | } |
| 223 | |
| 224 | operation->alg = cipher_suite->algorithm; |
| 225 | |
| 226 | mbedtls_ecjpake_init( &operation->ctx.ecjpake ); |
| 227 | |
| 228 | operation->state = PSA_PAKE_STATE_SETUP; |
| 229 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
| 230 | operation->input_step = PSA_PAKE_STEP_X1_X2; |
| 231 | operation->output_step = PSA_PAKE_STEP_X1_X2; |
| 232 | |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 233 | mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 234 | operation->buffer_length = 0; |
| 235 | operation->buffer_offset = 0; |
| 236 | |
| 237 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 238 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 239 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 240 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 241 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | psa_status_t psa_pake_set_password_key( psa_pake_operation_t *operation, |
| 245 | mbedtls_svc_key_id_t password ) |
| 246 | { |
| 247 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 248 | psa_key_attributes_t attributes = psa_key_attributes_init(); |
| 249 | psa_key_type_t type; |
| 250 | psa_key_usage_t usage; |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 251 | psa_key_slot_t *slot = NULL; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 252 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 253 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 254 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 255 | { |
| 256 | return( PSA_ERROR_BAD_STATE ); |
| 257 | } |
| 258 | |
| 259 | status = psa_get_key_attributes( password, &attributes ); |
| 260 | if( status != PSA_SUCCESS ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 261 | return( status ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 262 | |
| 263 | type = psa_get_key_type( &attributes ); |
| 264 | usage = psa_get_key_usage_flags( &attributes ); |
| 265 | |
| 266 | psa_reset_key_attributes( &attributes ); |
| 267 | |
| 268 | if( type != PSA_KEY_TYPE_PASSWORD && |
| 269 | type != PSA_KEY_TYPE_PASSWORD_HASH ) |
| 270 | { |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 271 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Neil Armstrong | df598ab | 2022-06-08 17:17:08 +0200 | [diff] [blame] | 274 | if( ( usage & PSA_KEY_USAGE_DERIVE ) == 0 ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 275 | return( PSA_ERROR_NOT_PERMITTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 276 | |
Przemek Stekiel | 0bdec19 | 2022-11-22 09:10:35 +0100 | [diff] [blame^] | 277 | if( operation->password != NULL ) |
| 278 | return( PSA_ERROR_BAD_STATE ); |
| 279 | |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 280 | status = psa_get_and_lock_key_slot_with_policy( password, &slot, |
| 281 | PSA_KEY_USAGE_DERIVE, |
| 282 | PSA_ALG_JPAKE ); |
| 283 | if( status != PSA_SUCCESS ) |
| 284 | return( status ); |
| 285 | |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 286 | operation->password = mbedtls_calloc( 1, slot->key.bytes ); |
| 287 | if( operation->password == NULL ) |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 288 | { |
Przemek Stekiel | 0bdec19 | 2022-11-22 09:10:35 +0100 | [diff] [blame^] | 289 | psa_unlock_key_slot( slot ); |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 290 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 291 | } |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 292 | memcpy( operation->password, slot->key.data, slot->key.bytes ); |
| 293 | operation->password_len = slot->key.bytes; |
Przemek Stekiel | 348410f | 2022-11-15 22:22:07 +0100 | [diff] [blame] | 294 | |
| 295 | status = psa_unlock_key_slot( slot ); |
| 296 | if( status != PSA_SUCCESS ) |
| 297 | return( status ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 298 | |
| 299 | return( PSA_SUCCESS ); |
| 300 | } |
| 301 | |
| 302 | psa_status_t psa_pake_set_user( psa_pake_operation_t *operation, |
| 303 | const uint8_t *user_id, |
| 304 | size_t user_id_len ) |
| 305 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 306 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 307 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 308 | { |
| 309 | return( PSA_ERROR_BAD_STATE ); |
| 310 | } |
| 311 | |
| 312 | if( user_id_len == 0 || user_id == NULL ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 313 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 314 | |
| 315 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 316 | } |
| 317 | |
| 318 | psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation, |
| 319 | const uint8_t *peer_id, |
| 320 | size_t peer_id_len ) |
| 321 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 322 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 323 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 324 | { |
| 325 | return( PSA_ERROR_BAD_STATE ); |
| 326 | } |
| 327 | |
| 328 | if( peer_id_len == 0 || peer_id == NULL ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 329 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 330 | |
| 331 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 332 | } |
| 333 | |
| 334 | psa_status_t psa_pake_set_role( psa_pake_operation_t *operation, |
| 335 | psa_pake_role_t role ) |
| 336 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 337 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 338 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 339 | { |
| 340 | return( PSA_ERROR_BAD_STATE ); |
| 341 | } |
| 342 | |
| 343 | if( role != PSA_PAKE_ROLE_NONE && |
| 344 | role != PSA_PAKE_ROLE_FIRST && |
| 345 | role != PSA_PAKE_ROLE_SECOND && |
| 346 | role != PSA_PAKE_ROLE_CLIENT && |
| 347 | role != PSA_PAKE_ROLE_SERVER ) |
| 348 | { |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 349 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 350 | } |
| 351 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 352 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 353 | if( operation->alg == PSA_ALG_JPAKE ) |
| 354 | { |
| 355 | if( role != PSA_PAKE_ROLE_CLIENT && |
| 356 | role != PSA_PAKE_ROLE_SERVER ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 357 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 358 | |
| 359 | operation->role = role; |
| 360 | |
| 361 | return( PSA_SUCCESS ); |
| 362 | } |
| 363 | else |
| 364 | #endif |
| 365 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 366 | } |
| 367 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 368 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 369 | static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation ) |
| 370 | { |
| 371 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 372 | mbedtls_ecjpake_role role; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 373 | |
| 374 | if( operation->role == PSA_PAKE_ROLE_CLIENT ) |
| 375 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 376 | else if( operation->role == PSA_PAKE_ROLE_SERVER ) |
| 377 | role = MBEDTLS_ECJPAKE_SERVER; |
| 378 | else |
| 379 | return( PSA_ERROR_BAD_STATE ); |
| 380 | |
Przemek Stekiel | ad0f357 | 2022-11-21 15:04:37 +0100 | [diff] [blame] | 381 | if( operation->password_len == 0 ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 382 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 383 | |
| 384 | ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake, |
| 385 | role, |
| 386 | MBEDTLS_MD_SHA256, |
| 387 | MBEDTLS_ECP_DP_SECP256R1, |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 388 | operation->password, |
| 389 | operation->password_len ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 390 | |
Przemek Stekiel | ad0f357 | 2022-11-21 15:04:37 +0100 | [diff] [blame] | 391 | mbedtls_platform_zeroize( operation->password, operation->password_len ); |
| 392 | mbedtls_free( operation->password ); |
| 393 | operation->password = NULL; |
| 394 | operation->password_len = 0; |
| 395 | |
Przemek Stekiel | 0bdec19 | 2022-11-22 09:10:35 +0100 | [diff] [blame^] | 396 | if( ret != 0 ) |
| 397 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
| 398 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 399 | operation->state = PSA_PAKE_STATE_READY; |
| 400 | |
| 401 | return( PSA_SUCCESS ); |
| 402 | } |
| 403 | #endif |
| 404 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 405 | static psa_status_t psa_pake_output_internal( |
| 406 | psa_pake_operation_t *operation, |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 407 | psa_pake_step_t step, |
| 408 | uint8_t *output, |
| 409 | size_t output_size, |
| 410 | size_t *output_length ) |
| 411 | { |
| 412 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 413 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 414 | size_t length; |
| 415 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 416 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 417 | operation->state == PSA_PAKE_STATE_INVALID ) |
| 418 | return( PSA_ERROR_BAD_STATE ); |
| 419 | |
Neil Armstrong | 0d001ef | 2022-06-08 17:42:52 +0200 | [diff] [blame] | 420 | if( output == NULL || output_size == 0 || output_length == NULL ) |
| 421 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 422 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 423 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 424 | /* |
| 425 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 426 | * handling of output sequencing. |
| 427 | * |
Neil Armstrong | 6a12a77 | 2022-09-14 12:17:42 +0200 | [diff] [blame] | 428 | * The MbedTLS JPAKE API outputs the whole X1+X2 and X2S steps data |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 429 | * 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 | */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 438 | if( operation->alg == PSA_ALG_JPAKE ) |
| 439 | { |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 440 | if( step != PSA_PAKE_STEP_KEY_SHARE && |
| 441 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 442 | step != PSA_PAKE_STEP_ZK_PROOF ) |
| 443 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 444 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 445 | if( operation->state == PSA_PAKE_STATE_SETUP ) { |
| 446 | status = psa_pake_ecjpake_setup( operation ); |
| 447 | if( status != PSA_SUCCESS ) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 448 | return( status ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 449 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 450 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 451 | if( operation->state != PSA_PAKE_STATE_READY && |
| 452 | operation->state != PSA_PAKE_OUTPUT_X1_X2 && |
| 453 | operation->state != PSA_PAKE_OUTPUT_X2S ) |
| 454 | { |
| 455 | return( PSA_ERROR_BAD_STATE ); |
| 456 | } |
| 457 | |
| 458 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 459 | { |
| 460 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 461 | return( PSA_ERROR_BAD_STATE ); |
| 462 | |
| 463 | switch( operation->output_step ) |
| 464 | { |
| 465 | case PSA_PAKE_STEP_X1_X2: |
| 466 | operation->state = PSA_PAKE_OUTPUT_X1_X2; |
| 467 | break; |
| 468 | case PSA_PAKE_STEP_X2S: |
| 469 | operation->state = PSA_PAKE_OUTPUT_X2S; |
| 470 | break; |
| 471 | default: |
| 472 | return( PSA_ERROR_BAD_STATE ); |
| 473 | } |
| 474 | |
| 475 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 476 | } |
| 477 | |
| 478 | /* Check if step matches current sequence */ |
| 479 | switch( operation->sequence ) |
| 480 | { |
| 481 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 482 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 483 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 484 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 485 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 486 | |
| 487 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 488 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 489 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 490 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 491 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 492 | |
| 493 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 494 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 495 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 496 | return( PSA_ERROR_BAD_STATE ); |
| 497 | break; |
| 498 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 499 | default: |
| 500 | return( PSA_ERROR_BAD_STATE ); |
| 501 | } |
| 502 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 503 | /* Initialize & write round on KEY_SHARE sequences */ |
| 504 | if( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 505 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 506 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 507 | ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake, |
| 508 | operation->buffer, |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 509 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 510 | &operation->buffer_length, |
| 511 | mbedtls_psa_get_random, |
| 512 | MBEDTLS_PSA_RANDOM_STATE ); |
| 513 | if( ret != 0 ) |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 514 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 515 | |
| 516 | operation->buffer_offset = 0; |
| 517 | } |
| 518 | else if( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 519 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
| 520 | { |
| 521 | ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake, |
| 522 | operation->buffer, |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 523 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 524 | &operation->buffer_length, |
| 525 | mbedtls_psa_get_random, |
| 526 | MBEDTLS_PSA_RANDOM_STATE ); |
| 527 | if( ret != 0 ) |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 528 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 529 | |
| 530 | operation->buffer_offset = 0; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 531 | } |
| 532 | |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 533 | /* |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 534 | * 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 Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 539 | * |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 540 | * 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 Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 543 | */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 544 | if( operation->state == PSA_PAKE_OUTPUT_X2S && |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 545 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE && |
| 546 | operation->role == PSA_PAKE_ROLE_SERVER ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 547 | { |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 548 | /* Skip ECParameters, with is 3 bytes (RFC 8422) */ |
| 549 | operation->buffer_offset += 3; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 550 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 551 | |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 552 | /* Read the length byte then move past it to the data */ |
| 553 | length = operation->buffer[operation->buffer_offset]; |
| 554 | operation->buffer_offset += 1; |
| 555 | |
| 556 | if( operation->buffer_offset + length > operation->buffer_length ) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 557 | return( PSA_ERROR_DATA_CORRUPT ); |
| 558 | |
| 559 | if( output_size < length ) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 560 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 561 | |
| 562 | memcpy( output, |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 563 | operation->buffer + operation->buffer_offset, |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 564 | length ); |
| 565 | *output_length = length; |
| 566 | |
| 567 | operation->buffer_offset += length; |
| 568 | |
| 569 | /* Reset buffer after ZK_PROOF sequence */ |
| 570 | if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 571 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 572 | ( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 573 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 574 | { |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 575 | mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 576 | operation->buffer_length = 0; |
| 577 | operation->buffer_offset = 0; |
| 578 | |
| 579 | operation->state = PSA_PAKE_STATE_READY; |
| 580 | operation->output_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 581 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 582 | } |
| 583 | else |
| 584 | operation->sequence++; |
| 585 | |
| 586 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 587 | } |
| 588 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 589 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 590 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 593 | psa_status_t psa_pake_output( psa_pake_operation_t *operation, |
| 594 | psa_pake_step_t step, |
| 595 | uint8_t *output, |
| 596 | size_t output_size, |
| 597 | size_t *output_length ) |
| 598 | { |
| 599 | psa_status_t status = psa_pake_output_internal( |
| 600 | operation, step, output, output_size, output_length ); |
| 601 | |
| 602 | if( status != PSA_SUCCESS ) |
| 603 | psa_pake_abort( operation ); |
| 604 | |
| 605 | return( status ); |
| 606 | } |
| 607 | |
| 608 | static psa_status_t psa_pake_input_internal( |
| 609 | psa_pake_operation_t *operation, |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 610 | psa_pake_step_t step, |
| 611 | const uint8_t *input, |
| 612 | size_t input_length ) |
| 613 | { |
| 614 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 615 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 616 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 617 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 618 | operation->state == PSA_PAKE_STATE_INVALID ) |
| 619 | return( PSA_ERROR_BAD_STATE ); |
| 620 | |
Neil Armstrong | 0d001ef | 2022-06-08 17:42:52 +0200 | [diff] [blame] | 621 | if( input == NULL || input_length == 0 ) |
| 622 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 623 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 624 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame] | 625 | /* |
| 626 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 627 | * handling of input sequencing. |
| 628 | * |
| 629 | * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data |
| 630 | * at once as input, on the other side the PSA CRYPTO PAKE api requires |
| 631 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be |
| 632 | * given in sequence. |
| 633 | * |
| 634 | * In order to achieve API compatibility, each X1+X2 or X4S step data |
| 635 | * is stored sequentially in an intermediate buffer and given to the |
| 636 | * MbedTLS JPAKE API on the last step. |
| 637 | * |
| 638 | * This causes any input error to be only detected on the last step. |
| 639 | */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 640 | if( operation->alg == PSA_ALG_JPAKE ) |
| 641 | { |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 642 | if( step != PSA_PAKE_STEP_KEY_SHARE && |
| 643 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 644 | step != PSA_PAKE_STEP_ZK_PROOF ) |
| 645 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 646 | |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 647 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
| 648 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, 256 ); |
Manuel Pégourié-Gonnard | 02f82bb | 2022-10-13 13:32:02 +0200 | [diff] [blame] | 649 | if( input_length > (size_t) PSA_PAKE_INPUT_SIZE( PSA_ALG_JPAKE, prim, step ) ) |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 650 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 651 | |
Neil Armstrong | 5bbdb70 | 2022-09-05 17:54:15 +0200 | [diff] [blame] | 652 | if( operation->state == PSA_PAKE_STATE_SETUP ) |
| 653 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 654 | status = psa_pake_ecjpake_setup( operation ); |
| 655 | if( status != PSA_SUCCESS ) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 656 | return( status ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 657 | } |
| 658 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 659 | if( operation->state != PSA_PAKE_STATE_READY && |
| 660 | operation->state != PSA_PAKE_INPUT_X1_X2 && |
| 661 | operation->state != PSA_PAKE_INPUT_X4S ) |
| 662 | { |
| 663 | return( PSA_ERROR_BAD_STATE ); |
| 664 | } |
| 665 | |
| 666 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 667 | { |
| 668 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 669 | return( PSA_ERROR_BAD_STATE ); |
| 670 | |
| 671 | switch( operation->input_step ) |
| 672 | { |
| 673 | case PSA_PAKE_STEP_X1_X2: |
| 674 | operation->state = PSA_PAKE_INPUT_X1_X2; |
| 675 | break; |
| 676 | case PSA_PAKE_STEP_X2S: |
| 677 | operation->state = PSA_PAKE_INPUT_X4S; |
| 678 | break; |
| 679 | default: |
| 680 | return( PSA_ERROR_BAD_STATE ); |
| 681 | } |
| 682 | |
| 683 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 684 | } |
| 685 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 686 | /* Check if step matches current sequence */ |
| 687 | switch( operation->sequence ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 688 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 689 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 690 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 691 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 692 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 693 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 694 | |
| 695 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 696 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 697 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 698 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 699 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 700 | |
| 701 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 702 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 703 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 704 | return( PSA_ERROR_BAD_STATE ); |
| 705 | break; |
| 706 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 707 | default: |
| 708 | return( PSA_ERROR_BAD_STATE ); |
| 709 | } |
| 710 | |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 711 | /* |
| 712 | * Copy input to local buffer and format it as the Mbed TLS API |
| 713 | * expects, i.e. as defined by draft-cragie-tls-ecjpake-01 section 7. |
| 714 | * The summary is that the data for each step is prepended with a |
| 715 | * length byte, and then they're concatenated. Additionally, the |
| 716 | * server's second round output is prepended with a 3-bytes |
| 717 | * ECParameters structure - which means we have to prepend that when |
| 718 | * we're a client. |
| 719 | */ |
| 720 | if( operation->state == PSA_PAKE_INPUT_X4S && |
| 721 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE && |
| 722 | operation->role == PSA_PAKE_ROLE_CLIENT ) |
| 723 | { |
| 724 | /* We only support secp256r1. */ |
| 725 | /* This is the ECParameters structure defined by RFC 8422. */ |
| 726 | unsigned char ecparameters[3] = { |
| 727 | 3, /* named_curve */ |
| 728 | 0, 23 /* secp256r1 */ |
| 729 | }; |
| 730 | memcpy( operation->buffer + operation->buffer_length, |
| 731 | ecparameters, sizeof( ecparameters ) ); |
| 732 | operation->buffer_length += sizeof( ecparameters ); |
| 733 | } |
| 734 | |
| 735 | /* Write the length byte */ |
Manuel Pégourié-Gonnard | 0771d41 | 2022-10-06 09:30:34 +0200 | [diff] [blame] | 736 | operation->buffer[operation->buffer_length] = (uint8_t) input_length; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 737 | operation->buffer_length += 1; |
| 738 | |
| 739 | /* Finally copy the data */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 740 | memcpy( operation->buffer + operation->buffer_length, |
| 741 | input, input_length ); |
| 742 | operation->buffer_length += input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 743 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 744 | /* Load buffer at each last round ZK_PROOF */ |
| 745 | if( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 746 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 747 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 748 | ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake, |
| 749 | operation->buffer, |
| 750 | operation->buffer_length ); |
| 751 | |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 752 | mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 753 | operation->buffer_length = 0; |
| 754 | |
| 755 | if( ret != 0 ) |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 756 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 757 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 758 | else if( operation->state == PSA_PAKE_INPUT_X4S && |
| 759 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 760 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 761 | ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake, |
| 762 | operation->buffer, |
| 763 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 764 | |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 765 | mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 766 | operation->buffer_length = 0; |
| 767 | |
| 768 | if( ret != 0 ) |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 769 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | if( ( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 773 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 774 | ( operation->state == PSA_PAKE_INPUT_X4S && |
| 775 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 776 | { |
| 777 | operation->state = PSA_PAKE_STATE_READY; |
| 778 | operation->input_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 779 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 780 | } |
| 781 | else |
| 782 | operation->sequence++; |
| 783 | |
| 784 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 785 | } |
| 786 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 787 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 788 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 789 | } |
| 790 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 791 | psa_status_t psa_pake_input( psa_pake_operation_t *operation, |
| 792 | psa_pake_step_t step, |
| 793 | const uint8_t *input, |
| 794 | size_t input_length ) |
| 795 | { |
| 796 | psa_status_t status = psa_pake_input_internal( |
| 797 | operation, step, input, input_length ); |
| 798 | |
| 799 | if( status != PSA_SUCCESS ) |
| 800 | psa_pake_abort( operation ); |
| 801 | |
| 802 | return( status ); |
| 803 | } |
| 804 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 805 | psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation, |
| 806 | psa_key_derivation_operation_t *output) |
| 807 | { |
| 808 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 809 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 810 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 811 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 812 | operation->state != PSA_PAKE_STATE_READY || |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 813 | operation->input_step != PSA_PAKE_STEP_DERIVE || |
| 814 | operation->output_step != PSA_PAKE_STEP_DERIVE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 815 | return( PSA_ERROR_BAD_STATE ); |
| 816 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 817 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 818 | if( operation->alg == PSA_ALG_JPAKE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 819 | { |
Neil Armstrong | f19a3cb | 2022-06-15 16:00:29 +0200 | [diff] [blame] | 820 | ret = mbedtls_ecjpake_write_shared_key( &operation->ctx.ecjpake, |
| 821 | operation->buffer, |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 822 | MBEDTLS_PSA_PAKE_BUFFER_SIZE, |
Neil Armstrong | f19a3cb | 2022-06-15 16:00:29 +0200 | [diff] [blame] | 823 | &operation->buffer_length, |
| 824 | mbedtls_psa_get_random, |
| 825 | MBEDTLS_PSA_RANDOM_STATE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 826 | if( ret != 0) |
| 827 | { |
| 828 | psa_pake_abort( operation ); |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 829 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 830 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 831 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 832 | status = psa_key_derivation_input_bytes( output, |
| 833 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 834 | operation->buffer, |
| 835 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 836 | |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 837 | mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 838 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 839 | psa_pake_abort( operation ); |
| 840 | |
| 841 | return( status ); |
| 842 | } |
| 843 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 844 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 845 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | psa_status_t psa_pake_abort(psa_pake_operation_t * operation) |
| 849 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 850 | if( operation->alg == PSA_ALG_NONE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 851 | { |
| 852 | return( PSA_SUCCESS ); |
| 853 | } |
| 854 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 855 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 856 | if( operation->alg == PSA_ALG_JPAKE ) |
| 857 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 858 | operation->input_step = PSA_PAKE_STEP_INVALID; |
| 859 | operation->output_step = PSA_PAKE_STEP_INVALID; |
Przemek Stekiel | ad0f357 | 2022-11-21 15:04:37 +0100 | [diff] [blame] | 860 | if( operation->password_len > 0 ) |
| 861 | mbedtls_platform_zeroize( operation->password, operation->password_len ); |
Przemek Stekiel | 152ae07 | 2022-11-17 13:24:36 +0100 | [diff] [blame] | 862 | mbedtls_free( operation->password ); |
| 863 | operation->password = NULL; |
| 864 | operation->password_len = 0; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 865 | operation->role = PSA_PAKE_ROLE_NONE; |
Manuel Pégourié-Gonnard | 79617d9 | 2022-10-05 12:55:50 +0200 | [diff] [blame] | 866 | mbedtls_platform_zeroize( operation->buffer, MBEDTLS_PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 867 | operation->buffer_length = 0; |
| 868 | operation->buffer_offset = 0; |
| 869 | mbedtls_ecjpake_free( &operation->ctx.ecjpake ); |
| 870 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 871 | #endif |
| 872 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 873 | operation->alg = PSA_ALG_NONE; |
| 874 | operation->state = PSA_PAKE_STATE_INVALID; |
| 875 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | fbc4b4a | 2022-06-10 08:54:53 +0200 | [diff] [blame] | 876 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 877 | return( PSA_SUCCESS ); |
| 878 | } |
| 879 | |
| 880 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ |
| 881 | |
| 882 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |