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