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 | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 36 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 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 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 133 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
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 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 251 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 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 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 267 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 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 | |
Neil Armstrong | 0d001ef | 2022-06-08 17:42:52 +0200 | [diff] [blame] | 331 | if( output == NULL || output_size == 0 || output_length == NULL ) |
| 332 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 333 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 334 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 335 | if( operation->alg == PSA_ALG_JPAKE ) |
| 336 | { |
| 337 | if( operation->state == PSA_PAKE_STATE_SETUP ) { |
| 338 | status = psa_pake_ecjpake_setup( operation ); |
| 339 | if( status != PSA_SUCCESS ) |
| 340 | { |
| 341 | psa_pake_abort( operation ); |
| 342 | return( status ); |
| 343 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 344 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 345 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 346 | if( operation->state >= PSA_PAKE_STATE_READY && |
| 347 | ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 || |
| 348 | operation->buffer == NULL ) ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 349 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 350 | return( PSA_ERROR_BAD_STATE ); |
| 351 | } |
| 352 | |
| 353 | if( operation->state != PSA_PAKE_STATE_READY && |
| 354 | operation->state != PSA_PAKE_OUTPUT_X1_X2 && |
| 355 | operation->state != PSA_PAKE_OUTPUT_X2S ) |
| 356 | { |
| 357 | return( PSA_ERROR_BAD_STATE ); |
| 358 | } |
| 359 | |
| 360 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 361 | { |
| 362 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 363 | return( PSA_ERROR_BAD_STATE ); |
| 364 | |
| 365 | switch( operation->output_step ) |
| 366 | { |
| 367 | case PSA_PAKE_STEP_X1_X2: |
| 368 | operation->state = PSA_PAKE_OUTPUT_X1_X2; |
| 369 | break; |
| 370 | case PSA_PAKE_STEP_X2S: |
| 371 | operation->state = PSA_PAKE_OUTPUT_X2S; |
| 372 | break; |
| 373 | default: |
| 374 | return( PSA_ERROR_BAD_STATE ); |
| 375 | } |
| 376 | |
| 377 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 378 | } |
| 379 | |
| 380 | /* Check if step matches current sequence */ |
| 381 | switch( operation->sequence ) |
| 382 | { |
| 383 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 384 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 385 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 386 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 387 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 388 | |
| 389 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 390 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 391 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 392 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 393 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 394 | |
| 395 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 396 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 397 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 398 | return( PSA_ERROR_BAD_STATE ); |
| 399 | break; |
| 400 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 401 | default: |
| 402 | return( PSA_ERROR_BAD_STATE ); |
| 403 | } |
| 404 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 405 | /* Initialize & write round on KEY_SHARE sequences */ |
| 406 | if( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 407 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 408 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 409 | ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake, |
| 410 | operation->buffer, |
| 411 | PSA_PAKE_BUFFER_SIZE, |
| 412 | &operation->buffer_length, |
| 413 | mbedtls_psa_get_random, |
| 414 | MBEDTLS_PSA_RANDOM_STATE ); |
| 415 | if( ret != 0 ) |
| 416 | { |
| 417 | psa_pake_abort( operation ); |
| 418 | return( mbedtls_to_psa_error( ret ) ); |
| 419 | } |
| 420 | |
| 421 | operation->buffer_offset = 0; |
| 422 | } |
| 423 | else if( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 424 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
| 425 | { |
| 426 | ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake, |
| 427 | operation->buffer, |
| 428 | PSA_PAKE_BUFFER_SIZE, |
| 429 | &operation->buffer_length, |
| 430 | mbedtls_psa_get_random, |
| 431 | MBEDTLS_PSA_RANDOM_STATE ); |
| 432 | if( ret != 0 ) |
| 433 | { |
| 434 | psa_pake_abort( operation ); |
| 435 | return( mbedtls_to_psa_error( ret ) ); |
| 436 | } |
| 437 | |
| 438 | operation->buffer_offset = 0; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 441 | /* Load output sequence length */ |
| 442 | if( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 443 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 444 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 445 | if( operation->role == PSA_PAKE_ROLE_SERVER ) |
| 446 | /* Length is stored after 3bytes curve */ |
| 447 | length = 3 + operation->buffer[3] + 1; |
| 448 | else |
| 449 | /* Length is stored at the first byte */ |
| 450 | length = operation->buffer[0] + 1; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 451 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 452 | else |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 453 | /* Length is stored at the first byte of the next chunk */ |
| 454 | length = operation->buffer[operation->buffer_offset] + 1; |
| 455 | |
| 456 | if( length > operation->buffer_length ) |
| 457 | return( PSA_ERROR_DATA_CORRUPT ); |
| 458 | |
| 459 | if( output_size < length ) |
| 460 | { |
| 461 | psa_pake_abort( operation ); |
| 462 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 463 | } |
| 464 | |
| 465 | memcpy( output, |
| 466 | operation->buffer + operation->buffer_offset, |
| 467 | length ); |
| 468 | *output_length = length; |
| 469 | |
| 470 | operation->buffer_offset += length; |
| 471 | |
| 472 | /* Reset buffer after ZK_PROOF sequence */ |
| 473 | if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 474 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 475 | ( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 476 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 477 | { |
| 478 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 479 | operation->buffer_length = 0; |
| 480 | operation->buffer_offset = 0; |
| 481 | |
| 482 | operation->state = PSA_PAKE_STATE_READY; |
| 483 | operation->output_step++; |
| 484 | operation->sequence = 0; |
| 485 | } |
| 486 | else |
| 487 | operation->sequence++; |
| 488 | |
| 489 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 490 | } |
| 491 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 492 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 493 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | psa_status_t psa_pake_input( psa_pake_operation_t *operation, |
| 497 | psa_pake_step_t step, |
| 498 | const uint8_t *input, |
| 499 | size_t input_length ) |
| 500 | { |
| 501 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 502 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 503 | size_t buffer_remain; |
| 504 | |
| 505 | if( operation->alg == 0 || |
| 506 | operation->state == PSA_PAKE_STATE_INVALID ) |
| 507 | return( PSA_ERROR_BAD_STATE ); |
| 508 | |
| 509 | if( step != PSA_PAKE_STEP_KEY_SHARE && |
| 510 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 511 | step != PSA_PAKE_STEP_ZK_PROOF ) |
| 512 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 513 | |
Neil Armstrong | 0d001ef | 2022-06-08 17:42:52 +0200 | [diff] [blame] | 514 | if( input == NULL || input_length == 0 ) |
| 515 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 516 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 517 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 518 | if( operation->alg == PSA_ALG_JPAKE ) |
| 519 | { |
| 520 | if( operation->state == PSA_PAKE_STATE_SETUP ) { |
| 521 | status = psa_pake_ecjpake_setup( operation ); |
| 522 | if( status != PSA_SUCCESS ) |
| 523 | { |
| 524 | psa_pake_abort( operation ); |
| 525 | return( status ); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | if( operation->state >= PSA_PAKE_STATE_READY && |
| 530 | ( mbedtls_ecjpake_check( &operation->ctx.ecjpake ) != 0 || |
| 531 | operation->buffer == NULL ) ) |
| 532 | { |
| 533 | return( PSA_ERROR_BAD_STATE ); |
| 534 | } |
| 535 | |
| 536 | if( operation->state != PSA_PAKE_STATE_READY && |
| 537 | operation->state != PSA_PAKE_INPUT_X1_X2 && |
| 538 | operation->state != PSA_PAKE_INPUT_X4S ) |
| 539 | { |
| 540 | return( PSA_ERROR_BAD_STATE ); |
| 541 | } |
| 542 | |
| 543 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 544 | { |
| 545 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 546 | return( PSA_ERROR_BAD_STATE ); |
| 547 | |
| 548 | switch( operation->input_step ) |
| 549 | { |
| 550 | case PSA_PAKE_STEP_X1_X2: |
| 551 | operation->state = PSA_PAKE_INPUT_X1_X2; |
| 552 | break; |
| 553 | case PSA_PAKE_STEP_X2S: |
| 554 | operation->state = PSA_PAKE_INPUT_X4S; |
| 555 | break; |
| 556 | default: |
| 557 | return( PSA_ERROR_BAD_STATE ); |
| 558 | } |
| 559 | |
| 560 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 561 | } |
| 562 | |
| 563 | buffer_remain = PSA_PAKE_BUFFER_SIZE - operation->buffer_length; |
| 564 | |
| 565 | if( input_length == 0 || |
| 566 | input_length > buffer_remain ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 567 | { |
| 568 | psa_pake_abort( operation ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 569 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 570 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 571 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 572 | /* Check if step matches current sequence */ |
| 573 | switch( operation->sequence ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 574 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 575 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 576 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 577 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 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_PUBLIC: |
| 582 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 583 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 584 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 585 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 586 | |
| 587 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 588 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 589 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 590 | return( PSA_ERROR_BAD_STATE ); |
| 591 | break; |
| 592 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 593 | default: |
| 594 | return( PSA_ERROR_BAD_STATE ); |
| 595 | } |
| 596 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 597 | /* Copy input to local buffer */ |
| 598 | memcpy( operation->buffer + operation->buffer_length, |
| 599 | input, input_length ); |
| 600 | operation->buffer_length += input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 601 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 602 | /* Load buffer at each last round ZK_PROOF */ |
| 603 | if( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 604 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 605 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 606 | ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake, |
| 607 | operation->buffer, |
| 608 | operation->buffer_length ); |
| 609 | |
| 610 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 611 | operation->buffer_length = 0; |
| 612 | |
| 613 | if( ret != 0 ) |
| 614 | { |
| 615 | psa_pake_abort( operation ); |
| 616 | return( mbedtls_to_psa_error( ret ) ); |
| 617 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 618 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 619 | else if( operation->state == PSA_PAKE_INPUT_X4S && |
| 620 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 621 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 622 | ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake, |
| 623 | operation->buffer, |
| 624 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 625 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 626 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 627 | operation->buffer_length = 0; |
| 628 | |
| 629 | if( ret != 0 ) |
| 630 | { |
| 631 | psa_pake_abort( operation ); |
| 632 | return( mbedtls_to_psa_error( ret ) ); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | if( ( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 637 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 638 | ( operation->state == PSA_PAKE_INPUT_X4S && |
| 639 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 640 | { |
| 641 | operation->state = PSA_PAKE_STATE_READY; |
| 642 | operation->input_step++; |
| 643 | operation->sequence = 0; |
| 644 | } |
| 645 | else |
| 646 | operation->sequence++; |
| 647 | |
| 648 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 649 | } |
| 650 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 651 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 652 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation, |
| 656 | psa_key_derivation_operation_t *output) |
| 657 | { |
| 658 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 659 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 660 | |
| 661 | if( operation->alg == 0 || |
| 662 | operation->state != PSA_PAKE_STATE_READY || |
| 663 | ( operation->input_step != PSA_PAKE_STEP_DERIVE && |
| 664 | operation->output_step != PSA_PAKE_STEP_DERIVE ) ) |
| 665 | return( PSA_ERROR_BAD_STATE ); |
| 666 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 667 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 668 | if( operation->alg == PSA_ALG_JPAKE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 669 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 670 | ret = mbedtls_ecjpake_derive_secret( &operation->ctx.ecjpake, |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 671 | operation->buffer, |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 672 | PSA_PAKE_BUFFER_SIZE, |
| 673 | &operation->buffer_length, |
| 674 | mbedtls_psa_get_random, |
| 675 | MBEDTLS_PSA_RANDOM_STATE ); |
| 676 | if( ret != 0) |
| 677 | { |
| 678 | psa_pake_abort( operation ); |
| 679 | return( mbedtls_to_psa_error( ret ) ); |
| 680 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 681 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 682 | status = psa_key_derivation_input_bytes( output, |
| 683 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 684 | operation->buffer, |
| 685 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 686 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 687 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
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 | psa_pake_abort( operation ); |
| 690 | |
| 691 | return( status ); |
| 692 | } |
| 693 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 694 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 695 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | psa_status_t psa_pake_abort(psa_pake_operation_t * operation) |
| 699 | { |
| 700 | if( operation->alg == 0 ) |
| 701 | { |
| 702 | return( PSA_SUCCESS ); |
| 703 | } |
| 704 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame^] | 705 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 706 | if( operation->alg == PSA_ALG_JPAKE ) |
| 707 | { |
| 708 | operation->input_step = 0; |
| 709 | operation->output_step = 0; |
| 710 | operation->password = MBEDTLS_SVC_KEY_ID_INIT; |
| 711 | operation->role = 0; |
| 712 | mbedtls_free( operation->buffer ); |
| 713 | operation->buffer = NULL; |
| 714 | operation->buffer_length = 0; |
| 715 | operation->buffer_offset = 0; |
| 716 | mbedtls_ecjpake_free( &operation->ctx.ecjpake ); |
| 717 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 718 | #endif |
| 719 | |
Neil Armstrong | fbc4b4a | 2022-06-10 08:54:53 +0200 | [diff] [blame] | 720 | operation->alg = 0; |
| 721 | operation->state = 0; |
| 722 | operation->sequence = 0; |
| 723 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 724 | return( PSA_SUCCESS ); |
| 725 | } |
| 726 | |
| 727 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ |
| 728 | |
| 729 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |