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