| 1 | /* |
| 2 | * PSA AEAD entry points |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
| 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "common.h" |
| 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 12 | |
| 13 | #include "psa_crypto_aead.h" |
| 14 | #include "psa_crypto_core.h" |
| 15 | #include "psa_crypto_cipher.h" |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include "mbedtls/platform.h" |
| 19 | |
| 20 | #include "mbedtls/ccm.h" |
| 21 | #include "mbedtls/chachapoly.h" |
| 22 | #include "mbedtls/cipher.h" |
| 23 | #include "mbedtls/gcm.h" |
| 24 | #include "mbedtls/error.h" |
| 25 | |
| 26 | static psa_status_t psa_aead_setup( |
| 27 | mbedtls_psa_aead_operation_t *operation, |
| 28 | const psa_key_attributes_t *attributes, |
| 29 | const uint8_t *key_buffer, |
| 30 | size_t key_buffer_size, |
| 31 | psa_algorithm_t alg) |
| 32 | { |
| 33 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 34 | mbedtls_cipher_id_t cipher_id; |
| 35 | mbedtls_cipher_mode_t mode; |
| 36 | size_t key_bits = attributes->bits; |
| 37 | (void) key_buffer_size; |
| 38 | |
| 39 | status = mbedtls_cipher_values_from_psa(alg, attributes->type, |
| 40 | &key_bits, &mode, &cipher_id); |
| 41 | if (status != PSA_SUCCESS) { |
| 42 | return status; |
| 43 | } |
| 44 | |
| 45 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
| 46 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 47 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
| 48 | operation->alg = PSA_ALG_CCM; |
| 49 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 50 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 51 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
| 52 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->type) != 16) { |
| 53 | return PSA_ERROR_INVALID_ARGUMENT; |
| 54 | } |
| 55 | |
| 56 | mbedtls_ccm_init(&operation->ctx.ccm); |
| 57 | status = mbedtls_to_psa_error( |
| 58 | mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id, |
| 59 | key_buffer, (unsigned int) key_bits)); |
| 60 | if (status != PSA_SUCCESS) { |
| 61 | return status; |
| 62 | } |
| 63 | break; |
| 64 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 65 | |
| 66 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 67 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
| 68 | operation->alg = PSA_ALG_GCM; |
| 69 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 70 | * The call to mbedtls_gcm_crypt_and_tag or |
| 71 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
| 72 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->type) != 16) { |
| 73 | return PSA_ERROR_INVALID_ARGUMENT; |
| 74 | } |
| 75 | |
| 76 | mbedtls_gcm_init(&operation->ctx.gcm); |
| 77 | status = mbedtls_to_psa_error( |
| 78 | mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id, |
| 79 | key_buffer, (unsigned int) key_bits)); |
| 80 | if (status != PSA_SUCCESS) { |
| 81 | return status; |
| 82 | } |
| 83 | break; |
| 84 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 85 | |
| 86 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 87 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
| 88 | operation->alg = PSA_ALG_CHACHA20_POLY1305; |
| 89 | /* We only support the default tag length. */ |
| 90 | if (alg != PSA_ALG_CHACHA20_POLY1305) { |
| 91 | return PSA_ERROR_NOT_SUPPORTED; |
| 92 | } |
| 93 | |
| 94 | mbedtls_chachapoly_init(&operation->ctx.chachapoly); |
| 95 | status = mbedtls_to_psa_error( |
| 96 | mbedtls_chachapoly_setkey(&operation->ctx.chachapoly, |
| 97 | key_buffer)); |
| 98 | if (status != PSA_SUCCESS) { |
| 99 | return status; |
| 100 | } |
| 101 | break; |
| 102 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 103 | |
| 104 | default: |
| 105 | (void) status; |
| 106 | (void) key_buffer; |
| 107 | return PSA_ERROR_NOT_SUPPORTED; |
| 108 | } |
| 109 | |
| 110 | operation->key_type = psa_get_key_type(attributes); |
| 111 | |
| 112 | operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); |
| 113 | |
| 114 | return PSA_SUCCESS; |
| 115 | } |
| 116 | |
| 117 | psa_status_t mbedtls_psa_aead_encrypt( |
| 118 | const psa_key_attributes_t *attributes, |
| 119 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 120 | psa_algorithm_t alg, |
| 121 | const uint8_t *nonce, size_t nonce_length, |
| 122 | const uint8_t *additional_data, size_t additional_data_length, |
| 123 | const uint8_t *plaintext, size_t plaintext_length, |
| 124 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length) |
| 125 | { |
| 126 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 127 | mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT; |
| 128 | uint8_t *tag; |
| 129 | |
| 130 | status = psa_aead_setup(&operation, attributes, key_buffer, |
| 131 | key_buffer_size, alg); |
| 132 | |
| 133 | if (status != PSA_SUCCESS) { |
| 134 | goto exit; |
| 135 | } |
| 136 | |
| 137 | /* For all currently supported modes, the tag is at the end of the |
| 138 | * ciphertext. */ |
| 139 | if (ciphertext_size < (plaintext_length + operation.tag_length)) { |
| 140 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 141 | goto exit; |
| 142 | } |
| 143 | tag = ciphertext + plaintext_length; |
| 144 | |
| 145 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 146 | if (operation.alg == PSA_ALG_CCM) { |
| 147 | status = mbedtls_to_psa_error( |
| 148 | mbedtls_ccm_encrypt_and_tag(&operation.ctx.ccm, |
| 149 | plaintext_length, |
| 150 | nonce, nonce_length, |
| 151 | additional_data, |
| 152 | additional_data_length, |
| 153 | plaintext, ciphertext, |
| 154 | tag, operation.tag_length)); |
| 155 | } else |
| 156 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 157 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 158 | if (operation.alg == PSA_ALG_GCM) { |
| 159 | status = mbedtls_to_psa_error( |
| 160 | mbedtls_gcm_crypt_and_tag(&operation.ctx.gcm, |
| 161 | MBEDTLS_GCM_ENCRYPT, |
| 162 | plaintext_length, |
| 163 | nonce, nonce_length, |
| 164 | additional_data, additional_data_length, |
| 165 | plaintext, ciphertext, |
| 166 | operation.tag_length, tag)); |
| 167 | } else |
| 168 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 169 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 170 | if (operation.alg == PSA_ALG_CHACHA20_POLY1305) { |
| 171 | if (operation.tag_length != 16) { |
| 172 | status = PSA_ERROR_NOT_SUPPORTED; |
| 173 | goto exit; |
| 174 | } |
| 175 | status = mbedtls_to_psa_error( |
| 176 | mbedtls_chachapoly_encrypt_and_tag(&operation.ctx.chachapoly, |
| 177 | plaintext_length, |
| 178 | nonce, |
| 179 | additional_data, |
| 180 | additional_data_length, |
| 181 | plaintext, |
| 182 | ciphertext, |
| 183 | tag)); |
| 184 | } else |
| 185 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 186 | { |
| 187 | (void) tag; |
| 188 | (void) nonce; |
| 189 | (void) nonce_length; |
| 190 | (void) additional_data; |
| 191 | (void) additional_data_length; |
| 192 | (void) plaintext; |
| 193 | return PSA_ERROR_NOT_SUPPORTED; |
| 194 | } |
| 195 | |
| 196 | if (status == PSA_SUCCESS) { |
| 197 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 198 | } |
| 199 | |
| 200 | exit: |
| 201 | mbedtls_psa_aead_abort(&operation); |
| 202 | |
| 203 | return status; |
| 204 | } |
| 205 | |
| 206 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 207 | * followed by the tag. Return the length of the part preceding the tag in |
| 208 | * *plaintext_length. This is the size of the plaintext in modes where |
| 209 | * the encrypted data has the same size as the plaintext, such as |
| 210 | * CCM and GCM. */ |
| 211 | static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length, |
| 212 | const uint8_t *ciphertext, |
| 213 | size_t ciphertext_length, |
| 214 | size_t plaintext_size, |
| 215 | const uint8_t **p_tag) |
| 216 | { |
| 217 | size_t payload_length; |
| 218 | if (tag_length > ciphertext_length) { |
| 219 | return PSA_ERROR_INVALID_ARGUMENT; |
| 220 | } |
| 221 | payload_length = ciphertext_length - tag_length; |
| 222 | if (payload_length > plaintext_size) { |
| 223 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 224 | } |
| 225 | *p_tag = ciphertext + payload_length; |
| 226 | return PSA_SUCCESS; |
| 227 | } |
| 228 | |
| 229 | psa_status_t mbedtls_psa_aead_decrypt( |
| 230 | const psa_key_attributes_t *attributes, |
| 231 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 232 | psa_algorithm_t alg, |
| 233 | const uint8_t *nonce, size_t nonce_length, |
| 234 | const uint8_t *additional_data, size_t additional_data_length, |
| 235 | const uint8_t *ciphertext, size_t ciphertext_length, |
| 236 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length) |
| 237 | { |
| 238 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 239 | mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT; |
| 240 | const uint8_t *tag = NULL; |
| 241 | |
| 242 | status = psa_aead_setup(&operation, attributes, key_buffer, |
| 243 | key_buffer_size, alg); |
| 244 | |
| 245 | if (status != PSA_SUCCESS) { |
| 246 | goto exit; |
| 247 | } |
| 248 | |
| 249 | status = psa_aead_unpadded_locate_tag(operation.tag_length, |
| 250 | ciphertext, ciphertext_length, |
| 251 | plaintext_size, &tag); |
| 252 | if (status != PSA_SUCCESS) { |
| 253 | goto exit; |
| 254 | } |
| 255 | |
| 256 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 257 | if (operation.alg == PSA_ALG_CCM) { |
| 258 | status = mbedtls_to_psa_error( |
| 259 | mbedtls_ccm_auth_decrypt(&operation.ctx.ccm, |
| 260 | ciphertext_length - operation.tag_length, |
| 261 | nonce, nonce_length, |
| 262 | additional_data, |
| 263 | additional_data_length, |
| 264 | ciphertext, plaintext, |
| 265 | tag, operation.tag_length)); |
| 266 | } else |
| 267 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 268 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 269 | if (operation.alg == PSA_ALG_GCM) { |
| 270 | status = mbedtls_to_psa_error( |
| 271 | mbedtls_gcm_auth_decrypt(&operation.ctx.gcm, |
| 272 | ciphertext_length - operation.tag_length, |
| 273 | nonce, nonce_length, |
| 274 | additional_data, |
| 275 | additional_data_length, |
| 276 | tag, operation.tag_length, |
| 277 | ciphertext, plaintext)); |
| 278 | } else |
| 279 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 280 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 281 | if (operation.alg == PSA_ALG_CHACHA20_POLY1305) { |
| 282 | if (operation.tag_length != 16) { |
| 283 | status = PSA_ERROR_NOT_SUPPORTED; |
| 284 | goto exit; |
| 285 | } |
| 286 | status = mbedtls_to_psa_error( |
| 287 | mbedtls_chachapoly_auth_decrypt(&operation.ctx.chachapoly, |
| 288 | ciphertext_length - operation.tag_length, |
| 289 | nonce, |
| 290 | additional_data, |
| 291 | additional_data_length, |
| 292 | tag, |
| 293 | ciphertext, |
| 294 | plaintext)); |
| 295 | } else |
| 296 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 297 | { |
| 298 | (void) nonce; |
| 299 | (void) nonce_length; |
| 300 | (void) additional_data; |
| 301 | (void) additional_data_length; |
| 302 | (void) plaintext; |
| 303 | return PSA_ERROR_NOT_SUPPORTED; |
| 304 | } |
| 305 | |
| 306 | if (status == PSA_SUCCESS) { |
| 307 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 308 | } |
| 309 | |
| 310 | exit: |
| 311 | mbedtls_psa_aead_abort(&operation); |
| 312 | |
| 313 | return status; |
| 314 | } |
| 315 | |
| 316 | /* Set the key and algorithm for a multipart authenticated encryption |
| 317 | * operation. */ |
| 318 | psa_status_t mbedtls_psa_aead_encrypt_setup( |
| 319 | mbedtls_psa_aead_operation_t *operation, |
| 320 | const psa_key_attributes_t *attributes, |
| 321 | const uint8_t *key_buffer, |
| 322 | size_t key_buffer_size, |
| 323 | psa_algorithm_t alg) |
| 324 | { |
| 325 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 326 | |
| 327 | status = psa_aead_setup(operation, attributes, key_buffer, |
| 328 | key_buffer_size, alg); |
| 329 | |
| 330 | if (status == PSA_SUCCESS) { |
| 331 | operation->is_encrypt = 1; |
| 332 | } |
| 333 | |
| 334 | return status; |
| 335 | } |
| 336 | |
| 337 | /* Set the key and algorithm for a multipart authenticated decryption |
| 338 | * operation. */ |
| 339 | psa_status_t mbedtls_psa_aead_decrypt_setup( |
| 340 | mbedtls_psa_aead_operation_t *operation, |
| 341 | const psa_key_attributes_t *attributes, |
| 342 | const uint8_t *key_buffer, |
| 343 | size_t key_buffer_size, |
| 344 | psa_algorithm_t alg) |
| 345 | { |
| 346 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 347 | |
| 348 | status = psa_aead_setup(operation, attributes, key_buffer, |
| 349 | key_buffer_size, alg); |
| 350 | |
| 351 | if (status == PSA_SUCCESS) { |
| 352 | operation->is_encrypt = 0; |
| 353 | } |
| 354 | |
| 355 | return status; |
| 356 | } |
| 357 | |
| 358 | /* Set a nonce for the multipart AEAD operation*/ |
| 359 | psa_status_t mbedtls_psa_aead_set_nonce( |
| 360 | mbedtls_psa_aead_operation_t *operation, |
| 361 | const uint8_t *nonce, |
| 362 | size_t nonce_length) |
| 363 | { |
| 364 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 365 | |
| 366 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 367 | if (operation->alg == PSA_ALG_GCM) { |
| 368 | status = mbedtls_to_psa_error( |
| 369 | mbedtls_gcm_starts(&operation->ctx.gcm, |
| 370 | operation->is_encrypt ? |
| 371 | MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT, |
| 372 | nonce, |
| 373 | nonce_length)); |
| 374 | } else |
| 375 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 376 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 377 | if (operation->alg == PSA_ALG_CCM) { |
| 378 | status = mbedtls_to_psa_error( |
| 379 | mbedtls_ccm_starts(&operation->ctx.ccm, |
| 380 | operation->is_encrypt ? |
| 381 | MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT, |
| 382 | nonce, |
| 383 | nonce_length)); |
| 384 | } else |
| 385 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 386 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 387 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { |
| 388 | /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to |
| 389 | * allocate a buffer in the operation, copy the nonce to it and pad |
| 390 | * it, so for now check the nonce is 12 bytes, as |
| 391 | * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the |
| 392 | * passed in buffer. */ |
| 393 | if (nonce_length != 12) { |
| 394 | return PSA_ERROR_INVALID_ARGUMENT; |
| 395 | } |
| 396 | |
| 397 | status = mbedtls_to_psa_error( |
| 398 | mbedtls_chachapoly_starts(&operation->ctx.chachapoly, |
| 399 | nonce, |
| 400 | operation->is_encrypt ? |
| 401 | MBEDTLS_CHACHAPOLY_ENCRYPT : |
| 402 | MBEDTLS_CHACHAPOLY_DECRYPT)); |
| 403 | } else |
| 404 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 405 | { |
| 406 | (void) operation; |
| 407 | (void) nonce; |
| 408 | (void) nonce_length; |
| 409 | |
| 410 | return PSA_ERROR_NOT_SUPPORTED; |
| 411 | } |
| 412 | |
| 413 | return status; |
| 414 | } |
| 415 | |
| 416 | /* Declare the lengths of the message and additional data for AEAD. */ |
| 417 | psa_status_t mbedtls_psa_aead_set_lengths( |
| 418 | mbedtls_psa_aead_operation_t *operation, |
| 419 | size_t ad_length, |
| 420 | size_t plaintext_length) |
| 421 | { |
| 422 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 423 | if (operation->alg == PSA_ALG_CCM) { |
| 424 | return mbedtls_to_psa_error( |
| 425 | mbedtls_ccm_set_lengths(&operation->ctx.ccm, |
| 426 | ad_length, |
| 427 | plaintext_length, |
| 428 | operation->tag_length)); |
| 429 | |
| 430 | } |
| 431 | #else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 432 | (void) operation; |
| 433 | (void) ad_length; |
| 434 | (void) plaintext_length; |
| 435 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 436 | |
| 437 | return PSA_SUCCESS; |
| 438 | } |
| 439 | |
| 440 | /* Pass additional data to an active multipart AEAD operation. */ |
| 441 | psa_status_t mbedtls_psa_aead_update_ad( |
| 442 | mbedtls_psa_aead_operation_t *operation, |
| 443 | const uint8_t *input, |
| 444 | size_t input_length) |
| 445 | { |
| 446 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 447 | |
| 448 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 449 | if (operation->alg == PSA_ALG_GCM) { |
| 450 | status = mbedtls_to_psa_error( |
| 451 | mbedtls_gcm_update_ad(&operation->ctx.gcm, input, input_length)); |
| 452 | } else |
| 453 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 454 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 455 | if (operation->alg == PSA_ALG_CCM) { |
| 456 | status = mbedtls_to_psa_error( |
| 457 | mbedtls_ccm_update_ad(&operation->ctx.ccm, input, input_length)); |
| 458 | } else |
| 459 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 460 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 461 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { |
| 462 | status = mbedtls_to_psa_error( |
| 463 | mbedtls_chachapoly_update_aad(&operation->ctx.chachapoly, |
| 464 | input, |
| 465 | input_length)); |
| 466 | } else |
| 467 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 468 | { |
| 469 | (void) operation; |
| 470 | (void) input; |
| 471 | (void) input_length; |
| 472 | |
| 473 | return PSA_ERROR_NOT_SUPPORTED; |
| 474 | } |
| 475 | |
| 476 | return status; |
| 477 | } |
| 478 | |
| 479 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 480 | * operation.*/ |
| 481 | psa_status_t mbedtls_psa_aead_update( |
| 482 | mbedtls_psa_aead_operation_t *operation, |
| 483 | const uint8_t *input, |
| 484 | size_t input_length, |
| 485 | uint8_t *output, |
| 486 | size_t output_size, |
| 487 | size_t *output_length) |
| 488 | { |
| 489 | size_t update_output_length; |
| 490 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 491 | |
| 492 | update_output_length = input_length; |
| 493 | |
| 494 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 495 | if (operation->alg == PSA_ALG_GCM) { |
| 496 | status = mbedtls_to_psa_error( |
| 497 | mbedtls_gcm_update(&operation->ctx.gcm, |
| 498 | input, input_length, |
| 499 | output, output_size, |
| 500 | &update_output_length)); |
| 501 | } else |
| 502 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 503 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 504 | if (operation->alg == PSA_ALG_CCM) { |
| 505 | if (output_size < input_length) { |
| 506 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 507 | } |
| 508 | |
| 509 | status = mbedtls_to_psa_error( |
| 510 | mbedtls_ccm_update(&operation->ctx.ccm, |
| 511 | input, input_length, |
| 512 | output, output_size, |
| 513 | &update_output_length)); |
| 514 | } else |
| 515 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 516 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 517 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { |
| 518 | if (output_size < input_length) { |
| 519 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 520 | } |
| 521 | |
| 522 | status = mbedtls_to_psa_error( |
| 523 | mbedtls_chachapoly_update(&operation->ctx.chachapoly, |
| 524 | input_length, |
| 525 | input, |
| 526 | output)); |
| 527 | } else |
| 528 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 529 | { |
| 530 | (void) operation; |
| 531 | (void) input; |
| 532 | (void) output; |
| 533 | (void) output_size; |
| 534 | |
| 535 | return PSA_ERROR_NOT_SUPPORTED; |
| 536 | } |
| 537 | |
| 538 | if (status == PSA_SUCCESS) { |
| 539 | *output_length = update_output_length; |
| 540 | } |
| 541 | |
| 542 | return status; |
| 543 | } |
| 544 | |
| 545 | /* Finish encrypting a message in a multipart AEAD operation. */ |
| 546 | psa_status_t mbedtls_psa_aead_finish( |
| 547 | mbedtls_psa_aead_operation_t *operation, |
| 548 | uint8_t *ciphertext, |
| 549 | size_t ciphertext_size, |
| 550 | size_t *ciphertext_length, |
| 551 | uint8_t *tag, |
| 552 | size_t tag_size, |
| 553 | size_t *tag_length) |
| 554 | { |
| 555 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 556 | size_t finish_output_size = 0; |
| 557 | |
| 558 | if (tag_size < operation->tag_length) { |
| 559 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 560 | } |
| 561 | |
| 562 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 563 | if (operation->alg == PSA_ALG_GCM) { |
| 564 | status = mbedtls_to_psa_error( |
| 565 | mbedtls_gcm_finish(&operation->ctx.gcm, |
| 566 | ciphertext, ciphertext_size, ciphertext_length, |
| 567 | tag, operation->tag_length)); |
| 568 | } else |
| 569 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 570 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 571 | if (operation->alg == PSA_ALG_CCM) { |
| 572 | /* tag must be big enough to store a tag of size passed into set |
| 573 | * lengths. */ |
| 574 | if (tag_size < operation->tag_length) { |
| 575 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 576 | } |
| 577 | |
| 578 | status = mbedtls_to_psa_error( |
| 579 | mbedtls_ccm_finish(&operation->ctx.ccm, |
| 580 | tag, operation->tag_length)); |
| 581 | } else |
| 582 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 583 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 584 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { |
| 585 | /* Belt and braces. Although the above tag_size check should have |
| 586 | * already done this, if we later start supporting smaller tag sizes |
| 587 | * for chachapoly, then passing a tag buffer smaller than 16 into here |
| 588 | * could cause a buffer overflow, so better safe than sorry. */ |
| 589 | if (tag_size < 16) { |
| 590 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 591 | } |
| 592 | |
| 593 | status = mbedtls_to_psa_error( |
| 594 | mbedtls_chachapoly_finish(&operation->ctx.chachapoly, |
| 595 | tag)); |
| 596 | } else |
| 597 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 598 | { |
| 599 | (void) ciphertext; |
| 600 | (void) ciphertext_size; |
| 601 | (void) ciphertext_length; |
| 602 | (void) tag; |
| 603 | (void) tag_size; |
| 604 | (void) tag_length; |
| 605 | |
| 606 | return PSA_ERROR_NOT_SUPPORTED; |
| 607 | } |
| 608 | |
| 609 | if (status == PSA_SUCCESS) { |
| 610 | /* This will be zero for all supported algorithms currently, but left |
| 611 | * here for future support. */ |
| 612 | *ciphertext_length = finish_output_size; |
| 613 | *tag_length = operation->tag_length; |
| 614 | } |
| 615 | |
| 616 | return status; |
| 617 | } |
| 618 | |
| 619 | /* Abort an AEAD operation */ |
| 620 | psa_status_t mbedtls_psa_aead_abort( |
| 621 | mbedtls_psa_aead_operation_t *operation) |
| 622 | { |
| 623 | switch (operation->alg) { |
| 624 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 625 | case PSA_ALG_CCM: |
| 626 | mbedtls_ccm_free(&operation->ctx.ccm); |
| 627 | break; |
| 628 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 629 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 630 | case PSA_ALG_GCM: |
| 631 | mbedtls_gcm_free(&operation->ctx.gcm); |
| 632 | break; |
| 633 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 634 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 635 | case PSA_ALG_CHACHA20_POLY1305: |
| 636 | mbedtls_chachapoly_free(&operation->ctx.chachapoly); |
| 637 | break; |
| 638 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 639 | } |
| 640 | |
| 641 | operation->is_encrypt = 0; |
| 642 | |
| 643 | return PSA_SUCCESS; |
| 644 | } |
| 645 | |
| 646 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 647 | |