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