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