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 | |
Neil Armstrong | ecb221b | 2022-09-08 11:21:07 +0200 | [diff] [blame] | 233 | mbedtls_platform_zeroize( operation->buffer, 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; |
| 251 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 252 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 253 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 254 | { |
| 255 | return( PSA_ERROR_BAD_STATE ); |
| 256 | } |
| 257 | |
| 258 | status = psa_get_key_attributes( password, &attributes ); |
| 259 | if( status != PSA_SUCCESS ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 260 | return( status ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 261 | |
| 262 | type = psa_get_key_type( &attributes ); |
| 263 | usage = psa_get_key_usage_flags( &attributes ); |
| 264 | |
| 265 | psa_reset_key_attributes( &attributes ); |
| 266 | |
| 267 | if( type != PSA_KEY_TYPE_PASSWORD && |
| 268 | type != PSA_KEY_TYPE_PASSWORD_HASH ) |
| 269 | { |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 270 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Neil Armstrong | df598ab | 2022-06-08 17:17:08 +0200 | [diff] [blame] | 273 | if( ( usage & PSA_KEY_USAGE_DERIVE ) == 0 ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 274 | return( PSA_ERROR_NOT_PERMITTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 275 | |
| 276 | operation->password = password; |
| 277 | |
| 278 | return( PSA_SUCCESS ); |
| 279 | } |
| 280 | |
| 281 | psa_status_t psa_pake_set_user( psa_pake_operation_t *operation, |
| 282 | const uint8_t *user_id, |
| 283 | size_t user_id_len ) |
| 284 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 285 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 286 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 287 | { |
| 288 | return( PSA_ERROR_BAD_STATE ); |
| 289 | } |
| 290 | |
| 291 | if( user_id_len == 0 || user_id == NULL ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 292 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 293 | |
| 294 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 295 | } |
| 296 | |
| 297 | psa_status_t psa_pake_set_peer( psa_pake_operation_t *operation, |
| 298 | const uint8_t *peer_id, |
| 299 | size_t peer_id_len ) |
| 300 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 301 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 302 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 303 | { |
| 304 | return( PSA_ERROR_BAD_STATE ); |
| 305 | } |
| 306 | |
| 307 | if( peer_id_len == 0 || peer_id == NULL ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 308 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 309 | |
| 310 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 311 | } |
| 312 | |
| 313 | psa_status_t psa_pake_set_role( psa_pake_operation_t *operation, |
| 314 | psa_pake_role_t role ) |
| 315 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 316 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 317 | operation->state != PSA_PAKE_STATE_SETUP ) |
| 318 | { |
| 319 | return( PSA_ERROR_BAD_STATE ); |
| 320 | } |
| 321 | |
| 322 | if( role != PSA_PAKE_ROLE_NONE && |
| 323 | role != PSA_PAKE_ROLE_FIRST && |
| 324 | role != PSA_PAKE_ROLE_SECOND && |
| 325 | role != PSA_PAKE_ROLE_CLIENT && |
| 326 | role != PSA_PAKE_ROLE_SERVER ) |
| 327 | { |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 328 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 329 | } |
| 330 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 331 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 332 | if( operation->alg == PSA_ALG_JPAKE ) |
| 333 | { |
| 334 | if( role != PSA_PAKE_ROLE_CLIENT && |
| 335 | role != PSA_PAKE_ROLE_SERVER ) |
Neil Armstrong | e923111 | 2022-06-10 09:03:41 +0200 | [diff] [blame] | 336 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 337 | |
| 338 | operation->role = role; |
| 339 | |
| 340 | return( PSA_SUCCESS ); |
| 341 | } |
| 342 | else |
| 343 | #endif |
| 344 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 345 | } |
| 346 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 347 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 348 | static psa_status_t psa_pake_ecjpake_setup( psa_pake_operation_t *operation ) |
| 349 | { |
| 350 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 351 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 352 | mbedtls_ecjpake_role role; |
| 353 | psa_key_slot_t *slot = NULL; |
| 354 | |
| 355 | if( operation->role == PSA_PAKE_ROLE_CLIENT ) |
| 356 | role = MBEDTLS_ECJPAKE_CLIENT; |
| 357 | else if( operation->role == PSA_PAKE_ROLE_SERVER ) |
| 358 | role = MBEDTLS_ECJPAKE_SERVER; |
| 359 | else |
| 360 | return( PSA_ERROR_BAD_STATE ); |
| 361 | |
| 362 | if( psa_is_valid_key_id( operation->password, 1 ) == 0 ) |
| 363 | return( PSA_ERROR_BAD_STATE ); |
| 364 | |
| 365 | status = psa_get_and_lock_key_slot( operation->password, &slot ); |
| 366 | if( status != PSA_SUCCESS ) |
| 367 | return( status ); |
| 368 | |
| 369 | |
| 370 | ret = mbedtls_ecjpake_setup( &operation->ctx.ecjpake, |
| 371 | role, |
| 372 | MBEDTLS_MD_SHA256, |
| 373 | MBEDTLS_ECP_DP_SECP256R1, |
| 374 | slot->key.data, slot->key.bytes ); |
| 375 | |
| 376 | psa_unlock_key_slot( slot ); |
| 377 | slot = NULL; |
| 378 | |
| 379 | if( ret != 0 ) |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 380 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 381 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 382 | operation->state = PSA_PAKE_STATE_READY; |
| 383 | |
| 384 | return( PSA_SUCCESS ); |
| 385 | } |
| 386 | #endif |
| 387 | |
| 388 | psa_status_t psa_pake_output( psa_pake_operation_t *operation, |
| 389 | psa_pake_step_t step, |
| 390 | uint8_t *output, |
| 391 | size_t output_size, |
| 392 | size_t *output_length ) |
| 393 | { |
| 394 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 395 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 396 | size_t length; |
| 397 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 398 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 399 | operation->state == PSA_PAKE_STATE_INVALID ) |
| 400 | return( PSA_ERROR_BAD_STATE ); |
| 401 | |
Neil Armstrong | 0d001ef | 2022-06-08 17:42:52 +0200 | [diff] [blame] | 402 | if( output == NULL || output_size == 0 || output_length == NULL ) |
| 403 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 404 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 405 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame^] | 406 | /* |
| 407 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 408 | * handling of output sequencing. |
| 409 | * |
| 410 | * The MbedTLS JPAKE API outputs the whole X1+X2 anf X2S steps data |
| 411 | * at once, on the other side the PSA CRYPTO PAKE api requires |
| 412 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X2S to be |
| 413 | * retrieved in sequence. |
| 414 | * |
| 415 | * In order to achieve API compatibility, the whole X1+X2 or X2S steps |
| 416 | * data is stored in an intermediate buffer at first step output call, |
| 417 | * and data is sliced down by parsing the ECPoint records in order |
| 418 | * to return the right parts on each step. |
| 419 | */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 420 | if( operation->alg == PSA_ALG_JPAKE ) |
| 421 | { |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 422 | if( step != PSA_PAKE_STEP_KEY_SHARE && |
| 423 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 424 | step != PSA_PAKE_STEP_ZK_PROOF ) |
| 425 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 426 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 427 | if( operation->state == PSA_PAKE_STATE_SETUP ) { |
| 428 | status = psa_pake_ecjpake_setup( operation ); |
| 429 | if( status != PSA_SUCCESS ) |
| 430 | { |
| 431 | psa_pake_abort( operation ); |
| 432 | return( status ); |
| 433 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 434 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 435 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 436 | if( operation->state != PSA_PAKE_STATE_READY && |
| 437 | operation->state != PSA_PAKE_OUTPUT_X1_X2 && |
| 438 | operation->state != PSA_PAKE_OUTPUT_X2S ) |
| 439 | { |
| 440 | return( PSA_ERROR_BAD_STATE ); |
| 441 | } |
| 442 | |
| 443 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 444 | { |
| 445 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 446 | return( PSA_ERROR_BAD_STATE ); |
| 447 | |
| 448 | switch( operation->output_step ) |
| 449 | { |
| 450 | case PSA_PAKE_STEP_X1_X2: |
| 451 | operation->state = PSA_PAKE_OUTPUT_X1_X2; |
| 452 | break; |
| 453 | case PSA_PAKE_STEP_X2S: |
| 454 | operation->state = PSA_PAKE_OUTPUT_X2S; |
| 455 | break; |
| 456 | default: |
| 457 | return( PSA_ERROR_BAD_STATE ); |
| 458 | } |
| 459 | |
| 460 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 461 | } |
| 462 | |
| 463 | /* Check if step matches current sequence */ |
| 464 | switch( operation->sequence ) |
| 465 | { |
| 466 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 467 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 468 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 469 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 470 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 471 | |
| 472 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 473 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 474 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 475 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 476 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 477 | |
| 478 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 479 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 480 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 481 | return( PSA_ERROR_BAD_STATE ); |
| 482 | break; |
| 483 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 484 | default: |
| 485 | return( PSA_ERROR_BAD_STATE ); |
| 486 | } |
| 487 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 488 | /* Initialize & write round on KEY_SHARE sequences */ |
| 489 | if( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 490 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 491 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 492 | ret = mbedtls_ecjpake_write_round_one( &operation->ctx.ecjpake, |
| 493 | operation->buffer, |
| 494 | PSA_PAKE_BUFFER_SIZE, |
| 495 | &operation->buffer_length, |
| 496 | mbedtls_psa_get_random, |
| 497 | MBEDTLS_PSA_RANDOM_STATE ); |
| 498 | if( ret != 0 ) |
| 499 | { |
| 500 | psa_pake_abort( operation ); |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 501 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | operation->buffer_offset = 0; |
| 505 | } |
| 506 | else if( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 507 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
| 508 | { |
| 509 | ret = mbedtls_ecjpake_write_round_two( &operation->ctx.ecjpake, |
| 510 | operation->buffer, |
| 511 | PSA_PAKE_BUFFER_SIZE, |
| 512 | &operation->buffer_length, |
| 513 | mbedtls_psa_get_random, |
| 514 | MBEDTLS_PSA_RANDOM_STATE ); |
| 515 | if( ret != 0 ) |
| 516 | { |
| 517 | psa_pake_abort( operation ); |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 518 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | operation->buffer_offset = 0; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 524 | /* |
| 525 | * Steps sequences are stored as: |
| 526 | * struct { |
| 527 | * opaque point <1..2^8-1>; |
| 528 | * } ECPoint; |
| 529 | * |
| 530 | * Where byte 0 stores the ECPoint curve point length. |
| 531 | * |
| 532 | * The sequence length is equal to: |
| 533 | * - data length extracted from byte 0 |
| 534 | * - byte 0 size (1) |
| 535 | */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 536 | if( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 537 | operation->sequence == PSA_PAKE_X1_STEP_KEY_SHARE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 538 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 539 | if( operation->role == PSA_PAKE_ROLE_SERVER ) |
Neil Armstrong | 1d0294f | 2022-09-13 14:49:24 +0200 | [diff] [blame] | 540 | /* |
| 541 | * The X2S KEY SHARE Server steps sequence is stored as: |
| 542 | * struct { |
| 543 | * ECPoint X; |
| 544 | * opaque r <1..2^8-1>; |
| 545 | * } ECSchnorrZKP; |
| 546 | * |
| 547 | * And MbedTLS uses a 3 bytes Ephemeral public key ECPoint, |
| 548 | * so byte 3 stores the r Schnorr signature length. |
| 549 | * |
| 550 | * The sequence length is equal to: |
| 551 | * - curve storage size (3) |
| 552 | * - data length extracted from byte 3 |
| 553 | * - byte 3 size (1) |
| 554 | */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 555 | length = 3 + operation->buffer[3] + 1; |
| 556 | else |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 557 | length = operation->buffer[0] + 1; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 558 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 559 | else |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 560 | length = operation->buffer[operation->buffer_offset] + 1; |
| 561 | |
| 562 | if( length > operation->buffer_length ) |
| 563 | return( PSA_ERROR_DATA_CORRUPT ); |
| 564 | |
| 565 | if( output_size < length ) |
| 566 | { |
| 567 | psa_pake_abort( operation ); |
| 568 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 569 | } |
| 570 | |
| 571 | memcpy( output, |
| 572 | operation->buffer + operation->buffer_offset, |
| 573 | length ); |
| 574 | *output_length = length; |
| 575 | |
| 576 | operation->buffer_offset += length; |
| 577 | |
| 578 | /* Reset buffer after ZK_PROOF sequence */ |
| 579 | if( ( operation->state == PSA_PAKE_OUTPUT_X1_X2 && |
| 580 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 581 | ( operation->state == PSA_PAKE_OUTPUT_X2S && |
| 582 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 583 | { |
| 584 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 585 | operation->buffer_length = 0; |
| 586 | operation->buffer_offset = 0; |
| 587 | |
| 588 | operation->state = PSA_PAKE_STATE_READY; |
| 589 | operation->output_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 590 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 591 | } |
| 592 | else |
| 593 | operation->sequence++; |
| 594 | |
| 595 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 596 | } |
| 597 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 598 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 599 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | psa_status_t psa_pake_input( psa_pake_operation_t *operation, |
| 603 | psa_pake_step_t step, |
| 604 | const uint8_t *input, |
| 605 | size_t input_length ) |
| 606 | { |
| 607 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 608 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 609 | size_t buffer_remain; |
| 610 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 611 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 612 | operation->state == PSA_PAKE_STATE_INVALID ) |
| 613 | return( PSA_ERROR_BAD_STATE ); |
| 614 | |
Neil Armstrong | 0d001ef | 2022-06-08 17:42:52 +0200 | [diff] [blame] | 615 | if( input == NULL || input_length == 0 ) |
| 616 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 617 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 618 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | fa84962 | 2022-09-13 15:10:46 +0200 | [diff] [blame^] | 619 | /* |
| 620 | * The PSA CRYPTO PAKE and MbedTLS JPAKE API have a different |
| 621 | * handling of input sequencing. |
| 622 | * |
| 623 | * The MbedTLS JPAKE API takes the whole X1+X2 or X4S steps data |
| 624 | * at once as input, on the other side the PSA CRYPTO PAKE api requires |
| 625 | * the KEY_SHARE/ZP_PUBLIC/ZK_PROOF parts of X1, X2 & X4S to be |
| 626 | * given in sequence. |
| 627 | * |
| 628 | * In order to achieve API compatibility, each X1+X2 or X4S step data |
| 629 | * is stored sequentially in an intermediate buffer and given to the |
| 630 | * MbedTLS JPAKE API on the last step. |
| 631 | * |
| 632 | * This causes any input error to be only detected on the last step. |
| 633 | */ |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 634 | if( operation->alg == PSA_ALG_JPAKE ) |
| 635 | { |
Neil Armstrong | 3d4966a | 2022-09-13 14:54:15 +0200 | [diff] [blame] | 636 | if( step != PSA_PAKE_STEP_KEY_SHARE && |
| 637 | step != PSA_PAKE_STEP_ZK_PUBLIC && |
| 638 | step != PSA_PAKE_STEP_ZK_PROOF ) |
| 639 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 640 | |
Neil Armstrong | 5bbdb70 | 2022-09-05 17:54:15 +0200 | [diff] [blame] | 641 | if( operation->state == PSA_PAKE_STATE_SETUP ) |
| 642 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 643 | status = psa_pake_ecjpake_setup( operation ); |
| 644 | if( status != PSA_SUCCESS ) |
| 645 | { |
| 646 | psa_pake_abort( operation ); |
| 647 | return( status ); |
| 648 | } |
| 649 | } |
| 650 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 651 | if( operation->state != PSA_PAKE_STATE_READY && |
| 652 | operation->state != PSA_PAKE_INPUT_X1_X2 && |
| 653 | operation->state != PSA_PAKE_INPUT_X4S ) |
| 654 | { |
| 655 | return( PSA_ERROR_BAD_STATE ); |
| 656 | } |
| 657 | |
| 658 | if( operation->state == PSA_PAKE_STATE_READY ) |
| 659 | { |
| 660 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 661 | return( PSA_ERROR_BAD_STATE ); |
| 662 | |
| 663 | switch( operation->input_step ) |
| 664 | { |
| 665 | case PSA_PAKE_STEP_X1_X2: |
| 666 | operation->state = PSA_PAKE_INPUT_X1_X2; |
| 667 | break; |
| 668 | case PSA_PAKE_STEP_X2S: |
| 669 | operation->state = PSA_PAKE_INPUT_X4S; |
| 670 | break; |
| 671 | default: |
| 672 | return( PSA_ERROR_BAD_STATE ); |
| 673 | } |
| 674 | |
| 675 | operation->sequence = PSA_PAKE_X1_STEP_KEY_SHARE; |
| 676 | } |
| 677 | |
| 678 | buffer_remain = PSA_PAKE_BUFFER_SIZE - operation->buffer_length; |
| 679 | |
| 680 | if( input_length == 0 || |
| 681 | input_length > buffer_remain ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 682 | { |
| 683 | psa_pake_abort( operation ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 684 | return( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 685 | } |
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 | /* Check if step matches current sequence */ |
| 688 | switch( operation->sequence ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 689 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 690 | case PSA_PAKE_X1_STEP_KEY_SHARE: |
| 691 | case PSA_PAKE_X2_STEP_KEY_SHARE: |
| 692 | if( step != PSA_PAKE_STEP_KEY_SHARE ) |
| 693 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 694 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 695 | |
| 696 | case PSA_PAKE_X1_STEP_ZK_PUBLIC: |
| 697 | case PSA_PAKE_X2_STEP_ZK_PUBLIC: |
| 698 | if( step != PSA_PAKE_STEP_ZK_PUBLIC ) |
| 699 | return( PSA_ERROR_BAD_STATE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 700 | break; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 701 | |
| 702 | case PSA_PAKE_X1_STEP_ZK_PROOF: |
| 703 | case PSA_PAKE_X2_STEP_ZK_PROOF: |
| 704 | if( step != PSA_PAKE_STEP_ZK_PROOF ) |
| 705 | return( PSA_ERROR_BAD_STATE ); |
| 706 | break; |
| 707 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 708 | default: |
| 709 | return( PSA_ERROR_BAD_STATE ); |
| 710 | } |
| 711 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 712 | /* Copy input to local buffer */ |
| 713 | memcpy( operation->buffer + operation->buffer_length, |
| 714 | input, input_length ); |
| 715 | operation->buffer_length += input_length; |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 716 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 717 | /* Load buffer at each last round ZK_PROOF */ |
| 718 | if( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 719 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 720 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 721 | ret = mbedtls_ecjpake_read_round_one( &operation->ctx.ecjpake, |
| 722 | operation->buffer, |
| 723 | operation->buffer_length ); |
| 724 | |
| 725 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 726 | operation->buffer_length = 0; |
| 727 | |
| 728 | if( ret != 0 ) |
| 729 | { |
| 730 | psa_pake_abort( operation ); |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 731 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 732 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 733 | } |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 734 | else if( operation->state == PSA_PAKE_INPUT_X4S && |
| 735 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 736 | { |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 737 | ret = mbedtls_ecjpake_read_round_two( &operation->ctx.ecjpake, |
| 738 | operation->buffer, |
| 739 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 740 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 741 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
| 742 | operation->buffer_length = 0; |
| 743 | |
| 744 | if( ret != 0 ) |
| 745 | { |
| 746 | psa_pake_abort( operation ); |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 747 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | if( ( operation->state == PSA_PAKE_INPUT_X1_X2 && |
| 752 | operation->sequence == PSA_PAKE_X2_STEP_ZK_PROOF ) || |
| 753 | ( operation->state == PSA_PAKE_INPUT_X4S && |
| 754 | operation->sequence == PSA_PAKE_X1_STEP_ZK_PROOF ) ) |
| 755 | { |
| 756 | operation->state = PSA_PAKE_STATE_READY; |
| 757 | operation->input_step++; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 758 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 759 | } |
| 760 | else |
| 761 | operation->sequence++; |
| 762 | |
| 763 | return( PSA_SUCCESS ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 764 | } |
| 765 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 766 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 767 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | psa_status_t psa_pake_get_implicit_key(psa_pake_operation_t *operation, |
| 771 | psa_key_derivation_operation_t *output) |
| 772 | { |
| 773 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 774 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 775 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 776 | if( operation->alg == PSA_ALG_NONE || |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 777 | operation->state != PSA_PAKE_STATE_READY || |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 778 | operation->input_step != PSA_PAKE_STEP_DERIVE || |
| 779 | operation->output_step != PSA_PAKE_STEP_DERIVE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 780 | return( PSA_ERROR_BAD_STATE ); |
| 781 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 782 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 783 | if( operation->alg == PSA_ALG_JPAKE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 784 | { |
Neil Armstrong | f19a3cb | 2022-06-15 16:00:29 +0200 | [diff] [blame] | 785 | ret = mbedtls_ecjpake_write_shared_key( &operation->ctx.ecjpake, |
| 786 | operation->buffer, |
| 787 | PSA_PAKE_BUFFER_SIZE, |
| 788 | &operation->buffer_length, |
| 789 | mbedtls_psa_get_random, |
| 790 | MBEDTLS_PSA_RANDOM_STATE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 791 | if( ret != 0) |
| 792 | { |
| 793 | psa_pake_abort( operation ); |
Neil Armstrong | db05cbf | 2022-06-15 15:25:45 +0200 | [diff] [blame] | 794 | return( mbedtls_ecjpake_to_psa_error( ret ) ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 795 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 796 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 797 | status = psa_key_derivation_input_bytes( output, |
| 798 | PSA_KEY_DERIVATION_INPUT_SECRET, |
| 799 | operation->buffer, |
| 800 | operation->buffer_length ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 801 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 802 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 803 | |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 804 | psa_pake_abort( operation ); |
| 805 | |
| 806 | return( status ); |
| 807 | } |
| 808 | else |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 809 | #endif |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 810 | return( PSA_ERROR_NOT_SUPPORTED ); |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | psa_status_t psa_pake_abort(psa_pake_operation_t * operation) |
| 814 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 815 | if( operation->alg == PSA_ALG_NONE ) |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 816 | { |
| 817 | return( PSA_SUCCESS ); |
| 818 | } |
| 819 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 820 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_JPAKE) |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 821 | if( operation->alg == PSA_ALG_JPAKE ) |
| 822 | { |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 823 | operation->input_step = PSA_PAKE_STEP_INVALID; |
| 824 | operation->output_step = PSA_PAKE_STEP_INVALID; |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 825 | operation->password = MBEDTLS_SVC_KEY_ID_INIT; |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 826 | operation->role = PSA_PAKE_ROLE_NONE; |
Neil Armstrong | ecb221b | 2022-09-08 11:21:07 +0200 | [diff] [blame] | 827 | mbedtls_platform_zeroize( operation->buffer, PSA_PAKE_BUFFER_SIZE ); |
Neil Armstrong | 4efd7a4 | 2022-06-08 17:18:31 +0200 | [diff] [blame] | 828 | operation->buffer_length = 0; |
| 829 | operation->buffer_offset = 0; |
| 830 | mbedtls_ecjpake_free( &operation->ctx.ecjpake ); |
| 831 | } |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 832 | #endif |
| 833 | |
Neil Armstrong | cb679f2 | 2022-09-13 14:43:07 +0200 | [diff] [blame] | 834 | operation->alg = PSA_ALG_NONE; |
| 835 | operation->state = PSA_PAKE_STATE_INVALID; |
| 836 | operation->sequence = PSA_PAKE_SEQ_INVALID; |
Neil Armstrong | fbc4b4a | 2022-06-10 08:54:53 +0200 | [diff] [blame] | 837 | |
Neil Armstrong | a4cc7d6 | 2022-05-25 11:30:48 +0200 | [diff] [blame] | 838 | return( PSA_SUCCESS ); |
| 839 | } |
| 840 | |
| 841 | #endif /* MBEDTLS_PSA_BUILTIN_PAKE */ |
| 842 | |
| 843 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |