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