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 | |
| 36 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
| 37 | #define PSA_PAKE_BUFFER_SIZE ( ( 69 + 66 + 33 ) * 2 ) |
| 38 | #endif |
| 39 | |
| 40 | /* |
| 41 | * State sequence: |
| 42 | * |
| 43 | * psa_pake_setup() |
| 44 | * | |
| 45 | * |-- In any order: |
| 46 | * | | psa_pake_set_password_key() |
| 47 | * | | psa_pake_set_user() |
| 48 | * | | psa_pake_set_peer() |
Neil Armstrong | c29f847 | 2022-06-08 13:34:49 +0200 | [diff] [blame] | 49 | * | | psa_pake_set_role() |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 50 | * | |
| 51 | * |--- In any order: (First round input before or after first round output) |
| 52 | * | | |
| 53 | * | |------ In Order |
| 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 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 58 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 59 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 60 | * | | |
| 61 | * | |------ In Order: |
| 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 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 66 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 67 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 68 | * | |
| 69 | * |--- In any order: (Second round input before or after second round output) |
| 70 | * | | |
| 71 | * | |------ In Order |
| 72 | * | | | psa_pake_output(PSA_PAKE_STEP_KEY_SHARE) |
| 73 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PUBLIC) |
| 74 | * | | | psa_pake_output(PSA_PAKE_STEP_ZK_PROOF) |
| 75 | * | | |
| 76 | * | |------ In Order: |
| 77 | * | | psa_pake_input(PSA_PAKE_STEP_KEY_SHARE) |
| 78 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PUBLIC) |
| 79 | * | | psa_pake_input(PSA_PAKE_STEP_ZK_PROOF) |
| 80 | * | |
| 81 | * psa_pake_get_implicit_key() |
| 82 | * psa_pake_abort() |
| 83 | */ |
| 84 | |
| 85 | enum psa_pake_step |
| 86 | { |
| 87 | PSA_PAKE_STEP_INVALID = 0, |
| 88 | PSA_PAKE_STEP_X1_X2 = 1, |
| 89 | PSA_PAKE_STEP_X2S = 2, |
| 90 | PSA_PAKE_STEP_DERIVE = 3, |
| 91 | }; |
| 92 | |
| 93 | enum psa_pake_state |
| 94 | { |
| 95 | PSA_PAKE_STATE_INVALID = 0, |
| 96 | PSA_PAKE_STATE_SETUP = 1, |
| 97 | PSA_PAKE_STATE_READY = 2, |
| 98 | PSA_PAKE_OUTPUT_X1_X2 = 3, |
| 99 | PSA_PAKE_OUTPUT_X2S = 4, |
| 100 | PSA_PAKE_INPUT_X1_X2 = 5, |
| 101 | PSA_PAKE_INPUT_X4S = 6, |
| 102 | }; |
| 103 | |
| 104 | enum psa_pake_sequence |
| 105 | { |
| 106 | PSA_PAKE_SEQ_INVALID = 0, |
| 107 | PSA_PAKE_X1_STEP_KEY_SHARE = 1, /* also X2S & X4S KEY_SHARE */ |
| 108 | PSA_PAKE_X1_STEP_ZK_PUBLIC = 2, /* also X2S & X4S ZK_PUBLIC */ |
| 109 | PSA_PAKE_X1_STEP_ZK_PROOF = 3, /* also X2S & X4S ZK_PROOF */ |
| 110 | PSA_PAKE_X2_STEP_KEY_SHARE = 4, |
| 111 | PSA_PAKE_X2_STEP_ZK_PUBLIC = 5, |
| 112 | PSA_PAKE_X2_STEP_ZK_PROOF = 6, |
| 113 | PSA_PAKE_SEQ_END = 7, |
| 114 | }; |
| 115 | |
| 116 | #if defined(MBEDTLS_PSA_BUILTIN_PAKE) |
| 117 | psa_status_t psa_pake_setup( psa_pake_operation_t *operation, |
| 118 | const psa_pake_cipher_suite_t *cipher_suite) |
| 119 | { |
| 120 | /* A context must be freshly initialized before it can be set up. */ |
| 121 | if( operation->alg != 0 || operation->state != PSA_PAKE_STATE_INVALID ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 122 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 123 | |
| 124 | if( cipher_suite == NULL || |
| 125 | PSA_ALG_IS_PAKE(cipher_suite->algorithm ) == 0 || |
| 126 | ( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC && |
| 127 | cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_DH ) || |
| 128 | PSA_ALG_IS_HASH( cipher_suite->hash ) == 0 ) |
| 129 | { |
| 130 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 131 | } |
| 132 | |
| 133 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 134 | if( cipher_suite->algorithm == PSA_ALG_JPAKE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 135 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 136 | if( cipher_suite->type != PSA_PAKE_PRIMITIVE_TYPE_ECC || |
| 137 | cipher_suite->family != PSA_ECC_FAMILY_SECP_R1 || |
| 138 | cipher_suite->bits != 256 || |
| 139 | cipher_suite->hash != PSA_ALG_SHA_256 ) |
| 140 | { |
| 141 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 142 | } |
| 143 | |
| 144 | operation->alg = cipher_suite->algorithm; |
| 145 | |
| 146 | mbedtls_ecjpake_init( &operation->ctx.ecjpake ); |
| 147 | |
| 148 | operation->state = PSA_PAKE_STATE_SETUP; |
| 149 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
| 150 | operation->input_step = PSA_PAKE_STEP_X1_X2; |
| 151 | operation->output_step = PSA_PAKE_STEP_X1_X2; |
| 152 | |
| 153 | operation->buffer = NULL; |
| 154 | operation->buffer_length = 0; |
| 155 | operation->buffer_offset = 0; |
| 156 | |
| 157 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 158 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 159 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 160 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 161 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | psa_status_t psa_pake_set_password_key( psa_pake_operation_t *operation, |
| 165 | mbedtls_svc_key_id_t password ) |
| 166 | { |
| 167 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 168 | psa_key_attributes_t attributes = psa_key_attributes_init(); |
| 169 | psa_key_type_t type; |
| 170 | psa_key_usage_t usage; |
| 171 | |
| 172 | if( operation->alg == 0 || |
| 173 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 174 | { |
| 175 | return( PSA_ERROR_BAD_STATE ); |
| 176 | } |
| 177 | |
| 178 | status = psa_get_key_attributes( password, &attributes ); |
| 179 | if( status != PSA_SUCCESS ) |
| 180 | return status; |
| 181 | |
| 182 | type = psa_get_key_type( &attributes ); |
| 183 | usage = psa_get_key_usage_flags( &attributes ); |
| 184 | |
| 185 | psa_reset_key_attributes( &attributes ); |
| 186 | |
| 187 | if( type != PSA_KEY_TYPE_PASSWORD && |
| 188 | type != PSA_KEY_TYPE_PASSWORD_HASH ) |
| 189 | { |
| 190 | return PSA_ERROR_INVALID_ARGUMENT; |
| 191 | } |
| 192 | |
Neil Armstrong | df598ab | 2022-06-08 17:17:08 +0200 | [diff] [blame] | 193 | if( ( usage & PSA_KEY_USAGE_DERIVE ) == 0 ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 194 | return PSA_ERROR_NOT_PERMITTED; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 195 | |
| 196 | operation->password = password; |
| 197 | |
| 198 | return( PSA_SUCCESS ); |
| 199 | } |
| 200 | |
| 201 | psa_status_t psa_pake_set_user( psa_pake_operation_t *operation, |
| 202 | const uint8_t *user_id, |
| 203 | size_t user_id_len ) |
| 204 | { |
| 205 | if( operation->alg == 0 || |
| 206 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 207 | { |
| 208 | return( PSA_ERROR_BAD_STATE ); |
| 209 | } |
| 210 | |
| 211 | if( user_id_len == 0 || user_id == NULL ) |
| 212 | return PSA_ERROR_INVALID_ARGUMENT; |
| 213 | |
| 214 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 215 | } |
| 216 | |
| 217 | psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation, |
| 218 | const uint8_t *peer_id, |
| 219 | size_t peer_id_len ) |
| 220 | { |
| 221 | if( operation->alg == 0 || |
| 222 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 223 | { |
| 224 | return( PSA_ERROR_BAD_STATE ); |
| 225 | } |
| 226 | |
| 227 | if( peer_id_len == 0 || peer_id == NULL ) |
| 228 | return PSA_ERROR_INVALID_ARGUMENT; |
| 229 | |
| 230 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 231 | } |
| 232 | |
| 233 | psa_status_t psa_pake_set_role( psa_pake_operation_t *operation, |
| 234 | psa_pake_role_t role ) |
| 235 | { |
| 236 | if( operation->alg == 0 || |
| 237 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 238 | { |
| 239 | return( PSA_ERROR_BAD_STATE ); |
| 240 | } |
| 241 | |
| 242 | if( role != PSA_PAKE_ROLE_NONE && |
| 243 | role != PSA_PAKE_ROLE_FIRST && |
| 244 | role != PSA_PAKE_ROLE_SECOND && |
| 245 | role != PSA_PAKE_ROLE_CLIENT && |
| 246 | role != PSA_PAKE_ROLE_SERVER ) |
| 247 | { |
| 248 | return PSA_ERROR_INVALID_ARGUMENT; |
| 249 | } |
| 250 | |
| 251 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
| 252 | if( operation->alg == PSA_ALG_JPAKE ) |
| 253 | { |
| 254 | if( role != PSA_PAKE_ROLE_CLIENT && |
| 255 | role != PSA_PAKE_ROLE_SERVER ) |
| 256 | return PSA_ERROR_NOT_SUPPORTED; |
| 257 | |
| 258 | operation->role = role; |
| 259 | |
| 260 | return( PSA_SUCCESS ); |
| 261 | } |
| 262 | else |
| 263 | #endif |
| 264 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 265 | } |
| 266 | |
| 267 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
| 268 | static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation ) |
| 269 | { |
| 270 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 271 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 272 | mbedtls_ecjpake_role role; |
| 273 | psa_key_slot_t *slot = NULL; |
| 274 | |
| 275 | if( operation->role == PSA_PAKE_ROLE_CLIENT ) |
| 276 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 277 | else if( operation->role == PSA_PAKE_ROLE_SERVER ) |
| 278 | role = MBEDTLS_ECJPAKE_SERVER; |
| 279 | else |
| 280 | return( PSA_ERROR_BAD_STATE ); |
| 281 | |
| 282 | if( psa_is_valid_key_id( operation->password, 1 ) == 0 ) |
| 283 | return( PSA_ERROR_BAD_STATE ); |
| 284 | |
| 285 | status = psa_get_and_lock_key_slot( operation->password, &slot ); |
| 286 | if( status != PSA_SUCCESS ) |
| 287 | return( status ); |
| 288 | |
| 289 | |
| 290 | ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake, |
| 291 | role, |
| 292 | MBEDTLS_MD_SHA256, |
| 293 | MBEDTLS_ECP_DP_SECP256R1, |
| 294 | slot->key.data, slot->key.bytes ); |
| 295 | |
| 296 | psa_unlock_key_slot( slot ); |
| 297 | slot = NULL; |
| 298 | |
| 299 | if( ret != 0 ) |
| 300 | return( mbedtls_to_psa_error( ret ) ); |
| 301 | |
Neil Armstrong | 6b1f99f | 2022-06-08 13:37:37 +0200 | [diff] [blame] | 302 | operation->buffer = mbedtls_calloc( 1, PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 303 | if( operation->buffer == NULL ) |
| 304 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 305 | |
| 306 | operation->state = PSA_PAKE_STATE_READY; |
| 307 | |
| 308 | return( PSA_SUCCESS ); |
| 309 | } |
| 310 | #endif |
| 311 | |
| 312 | psa_status_t psa_pake_output( psa_pake_operation_t *operation, |
| 313 | psa_pake_step_t step, |
| 314 | uint8_t *output, |
| 315 | size_t output_size, |
| 316 | size_t *output_length ) |
| 317 | { |
| 318 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 319 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 320 | size_t length; |
| 321 | |
| 322 | if( operation->alg == 0 || |
| 323 | operation->state == PSA_PAKE_STATE_INVALID ) |
| 324 | return( PSA_ERROR_BAD_STATE ); |
| 325 | |
| 326 | if( step != PSA_PAKE_STEP_KEY_SHARE && |
| 327 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 328 | step != PSA_PAKE_STEP_ZK_PROOF ) |
| 329 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 330 | |
| 331 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 332 | if( operation->alg == PSA_ALG_JPAKE ) |
| 333 | { |
| 334 | if( operation->state == PSA_PAKE_STATE_SETUP ) { |
| 335 | status = psa_pake_ecjpake_setup( operation ); |
| 336 | if( status != PSA_SUCCESS ) |
| 337 | { |
| 338 | psa_pake_abort( operation ); |
| 339 | return( status ); |
| 340 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 341 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 342 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 343 | if( operation->state >= PSA_PAKE_STATE_READY && |
| 344 | ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 || |
| 345 | operation->buffer == NULL ) ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 346 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 347 | return( PSA_ERROR_BAD_STATE ); |
| 348 | } |
| 349 | |
| 350 | if( operation->state != PSA_PAKE_STATE_READY && |
| 351 | operation->state != PSA_PAKE_OUTPUT_X1_X2 && |
| 352 | operation->state != PSA_PAKE_OUTPUT_X2S ) |
| 353 | { |
| 354 | return( PSA_ERROR_BAD_STATE ); |
| 355 | } |
| 356 | |
| 357 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 358 | { |
| 359 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 360 | return( PSA_ERROR_BAD_STATE ); |
| 361 | |
| 362 | switch( operation->output_step ) |
| 363 | { |
| 364 | case PSA_PAKE_STEP_X1_X2: |
| 365 | operation->state = PSA_PAKE_OUTPUT_X1_X2; |
| 366 | break; |
| 367 | case PSA_PAKE_STEP_X2S: |
| 368 | operation->state = PSA_PAKE_OUTPUT_X2S; |
| 369 | break; |
| 370 | default: |
| 371 | return( PSA_ERROR_BAD_STATE ); |
| 372 | } |
| 373 | |
| 374 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 375 | } |
| 376 | |
| 377 | /* Check if step matches current sequence */ |
| 378 | switch( operation->sequence ) |
| 379 | { |
| 380 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 381 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 382 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 383 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 384 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 385 | |
| 386 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 387 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 388 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 389 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 390 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 391 | |
| 392 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 393 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 394 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 395 | return( PSA_ERROR_BAD_STATE ); |
| 396 | break; |
| 397 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 398 | default: |
| 399 | return( PSA_ERROR_BAD_STATE ); |
| 400 | } |
| 401 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 402 | /* Initialize & write round on KEY_SHARE sequences */ |
| 403 | if( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 404 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 405 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 406 | ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake, |
| 407 | operation->buffer, |
| 408 | PSA_PAKE_BUFFER_SIZE, |
| 409 | &operation->buffer_length, |
| 410 | mbedtls_psa_get_random, |
| 411 | MBEDTLS_PSA_RANDOM_STATE ); |
| 412 | if( ret != 0 ) |
| 413 | { |
| 414 | psa_pake_abort( operation ); |
| 415 | return( mbedtls_to_psa_error( ret ) ); |
| 416 | } |
| 417 | |
| 418 | operation->buffer_offset = 0; |
| 419 | } |
| 420 | else if( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 421 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
| 422 | { |
| 423 | ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake, |
| 424 | operation->buffer, |
| 425 | PSA_PAKE_BUFFER_SIZE, |
| 426 | &operation->buffer_length, |
| 427 | mbedtls_psa_get_random, |
| 428 | MBEDTLS_PSA_RANDOM_STATE ); |
| 429 | if( ret != 0 ) |
| 430 | { |
| 431 | psa_pake_abort( operation ); |
| 432 | return( mbedtls_to_psa_error( ret ) ); |
| 433 | } |
| 434 | |
| 435 | operation->buffer_offset = 0; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 438 | /* Load output sequence length */ |
| 439 | if( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 440 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 441 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 442 | if( operation->role == PSA_PAKE_ROLE_SERVER ) |
| 443 | /* Length is stored after 3bytes curve */ |
| 444 | length = 3 + operation->buffer[3] + 1; |
| 445 | else |
| 446 | /* Length is stored at the first byte */ |
| 447 | length = operation->buffer[0] + 1; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 448 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 449 | else |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 450 | /* Length is stored at the first byte of the next chunk */ |
| 451 | length = operation->buffer[operation->buffer_offset] + 1; |
| 452 | |
| 453 | if( length > operation->buffer_length ) |
| 454 | return( PSA_ERROR_DATA_CORRUPT ); |
| 455 | |
| 456 | if( output_size < length ) |
| 457 | { |
| 458 | psa_pake_abort( operation ); |
| 459 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 460 | } |
| 461 | |
| 462 | memcpy( output, |
| 463 | operation->buffer + operation->buffer_offset, |
| 464 | length ); |
| 465 | *output_length = length; |
| 466 | |
| 467 | operation->buffer_offset += length; |
| 468 | |
| 469 | /* Reset buffer after ZK_PROOF sequence */ |
| 470 | if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 471 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 472 | ( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 473 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 474 | { |
| 475 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 476 | operation->buffer_length = 0; |
| 477 | operation->buffer_offset = 0; |
| 478 | |
| 479 | operation->state = PSA_PAKE_STATE_READY; |
| 480 | operation->output_step++; |
| 481 | operation->sequence = 0; |
| 482 | } |
| 483 | else |
| 484 | operation->sequence++; |
| 485 | |
| 486 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 487 | } |
| 488 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 489 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 490 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | psa_status_t psa_pake_input( psa_pake_operation_t *operation, |
| 494 | psa_pake_step_t step, |
| 495 | const uint8_t *input, |
| 496 | size_t input_length ) |
| 497 | { |
| 498 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 499 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 500 | size_t buffer_remain; |
| 501 | |
| 502 | if( operation->alg == 0 || |
| 503 | operation->state == PSA_PAKE_STATE_INVALID ) |
| 504 | return( PSA_ERROR_BAD_STATE ); |
| 505 | |
| 506 | if( step != PSA_PAKE_STEP_KEY_SHARE && |
| 507 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 508 | step != PSA_PAKE_STEP_ZK_PROOF ) |
| 509 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 510 | |
| 511 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 512 | if( operation->alg == PSA_ALG_JPAKE ) |
| 513 | { |
| 514 | if( operation->state == PSA_PAKE_STATE_SETUP ) { |
| 515 | status = psa_pake_ecjpake_setup( operation ); |
| 516 | if( status != PSA_SUCCESS ) |
| 517 | { |
| 518 | psa_pake_abort( operation ); |
| 519 | return( status ); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | if( operation->state >= PSA_PAKE_STATE_READY && |
| 524 | ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 || |
| 525 | operation->buffer == NULL ) ) |
| 526 | { |
| 527 | return( PSA_ERROR_BAD_STATE ); |
| 528 | } |
| 529 | |
| 530 | if( operation->state != PSA_PAKE_STATE_READY && |
| 531 | operation->state != PSA_PAKE_INPUT_X1_X2 && |
| 532 | operation->state != PSA_PAKE_INPUT_X4S ) |
| 533 | { |
| 534 | return( PSA_ERROR_BAD_STATE ); |
| 535 | } |
| 536 | |
| 537 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 538 | { |
| 539 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 540 | return( PSA_ERROR_BAD_STATE ); |
| 541 | |
| 542 | switch( operation->input_step ) |
| 543 | { |
| 544 | case PSA_PAKE_STEP_X1_X2: |
| 545 | operation->state = PSA_PAKE_INPUT_X1_X2; |
| 546 | break; |
| 547 | case PSA_PAKE_STEP_X2S: |
| 548 | operation->state = PSA_PAKE_INPUT_X4S; |
| 549 | break; |
| 550 | default: |
| 551 | return( PSA_ERROR_BAD_STATE ); |
| 552 | } |
| 553 | |
| 554 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 555 | } |
| 556 | |
| 557 | buffer_remain = PSA_PAKE_BUFFER_SIZE - operation->buffer_length; |
| 558 | |
| 559 | if( input_length == 0 || |
| 560 | input_length > buffer_remain ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 561 | { |
| 562 | psa_pake_abort( operation ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 563 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 564 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 565 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 566 | /* Check if step matches current sequence */ |
| 567 | switch( operation->sequence ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 568 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 569 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 570 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 571 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 572 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 573 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 574 | |
| 575 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 576 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 577 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 578 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 579 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 580 | |
| 581 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 582 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 583 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 584 | return( PSA_ERROR_BAD_STATE ); |
| 585 | break; |
| 586 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 587 | default: |
| 588 | return( PSA_ERROR_BAD_STATE ); |
| 589 | } |
| 590 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 591 | /* Copy input to local buffer */ |
| 592 | memcpy( operation->buffer + operation->buffer_length, |
| 593 | input, input_length ); |
| 594 | operation->buffer_length += input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 595 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 596 | /* Load buffer at each last round ZK_PROOF */ |
| 597 | if( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 598 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 599 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 600 | ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake, |
| 601 | operation->buffer, |
| 602 | operation->buffer_length ); |
| 603 | |
| 604 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 605 | operation->buffer_length = 0; |
| 606 | |
| 607 | if( ret != 0 ) |
| 608 | { |
| 609 | psa_pake_abort( operation ); |
| 610 | return( mbedtls_to_psa_error( ret ) ); |
| 611 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 612 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 613 | else if( operation->state == PSA_PAKE_INPUT_X4S && |
| 614 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 615 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 616 | ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake, |
| 617 | operation->buffer, |
| 618 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 619 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 620 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 621 | operation->buffer_length = 0; |
| 622 | |
| 623 | if( ret != 0 ) |
| 624 | { |
| 625 | psa_pake_abort( operation ); |
| 626 | return( mbedtls_to_psa_error( ret ) ); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if( ( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 631 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 632 | ( operation->state == PSA_PAKE_INPUT_X4S && |
| 633 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 634 | { |
| 635 | operation->state = PSA_PAKE_STATE_READY; |
| 636 | operation->input_step++; |
| 637 | operation->sequence = 0; |
| 638 | } |
| 639 | else |
| 640 | operation->sequence++; |
| 641 | |
| 642 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 643 | } |
| 644 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 645 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 646 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation, |
| 650 | psa_key_derivation_operation_t *output) |
| 651 | { |
| 652 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 653 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 654 | |
| 655 | if( operation->alg == 0 || |
| 656 | operation->state != PSA_PAKE_STATE_READY || |
| 657 | ( operation->input_step != PSA_PAKE_STEP_DERIVE && |
| 658 | operation->output_step != PSA_PAKE_STEP_DERIVE ) ) |
| 659 | return( PSA_ERROR_BAD_STATE ); |
| 660 | |
| 661 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 662 | if( operation->alg == PSA_ALG_JPAKE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 663 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 664 | ret = mbedtls_ecjpake_derive_secret( &operation->ctx.ecjpake, |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 665 | operation->buffer, |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 666 | PSA_PAKE_BUFFER_SIZE, |
| 667 | &operation->buffer_length, |
| 668 | mbedtls_psa_get_random, |
| 669 | MBEDTLS_PSA_RANDOM_STATE ); |
| 670 | if( ret != 0) |
| 671 | { |
| 672 | psa_pake_abort( operation ); |
| 673 | return( mbedtls_to_psa_error( ret ) ); |
| 674 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 675 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 676 | status = psa_key_derivation_input_bytes( output, |
| 677 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 678 | operation->buffer, |
| 679 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 680 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 681 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 682 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 683 | psa_pake_abort( operation ); |
| 684 | |
| 685 | return( status ); |
| 686 | } |
| 687 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 688 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 689 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | psa_status_t psa_pake_abort(psa_pake_operation_t * operation) |
| 693 | { |
| 694 | if( operation->alg == 0 ) |
| 695 | { |
| 696 | return( PSA_SUCCESS ); |
| 697 | } |
| 698 | |
| 699 | operation->alg = 0; |
| 700 | operation->state = 0; |
| 701 | operation->sequence = 0; |
| 702 | |
| 703 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECJPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame^] | 704 | if( operation->alg == PSA_ALG_JPAKE ) |
| 705 | { |
| 706 | operation->input_step = 0; |
| 707 | operation->output_step = 0; |
| 708 | operation->password = MBEDTLS_SVC_KEY_ID_INIT; |
| 709 | operation->role = 0; |
| 710 | mbedtls_free( operation->buffer ); |
| 711 | operation->buffer = NULL; |
| 712 | operation->buffer_length = 0; |
| 713 | operation->buffer_offset = 0; |
| 714 | mbedtls_ecjpake_free( &operation->ctx.ecjpake ); |
| 715 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 716 | #endif |
| 717 | |
| 718 | return( PSA_SUCCESS ); |
| 719 | } |
| 720 | |
| 721 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ |
| 722 | |
| 723 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |