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