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 | eac6c75 | 2021-09-15 19:08:27 +0100 | [diff] [blame] | 198 | status = mbedtls_aead_check_nonce_length( &operation, nonce_length ); |
| 199 | |
| 200 | if( status != PSA_SUCCESS ) |
Paul Elliott | 96b0173 | 2021-07-16 17:00:26 +0100 | [diff] [blame] | 201 | goto exit; |
Paul Elliott | 96b0173 | 2021-07-16 17:00:26 +0100 | [diff] [blame] | 202 | |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 203 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 204 | if( operation.alg == PSA_ALG_CCM ) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 205 | { |
| 206 | status = mbedtls_to_psa_error( |
| 207 | mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm, |
| 208 | plaintext_length, |
| 209 | nonce, nonce_length, |
| 210 | additional_data, |
| 211 | additional_data_length, |
| 212 | plaintext, ciphertext, |
| 213 | tag, operation.tag_length ) ); |
| 214 | } |
| 215 | else |
| 216 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 217 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 218 | if( operation.alg == PSA_ALG_GCM ) |
Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 219 | { |
| 220 | status = mbedtls_to_psa_error( |
| 221 | mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm, |
| 222 | MBEDTLS_GCM_ENCRYPT, |
| 223 | plaintext_length, |
| 224 | nonce, nonce_length, |
| 225 | additional_data, additional_data_length, |
| 226 | plaintext, ciphertext, |
| 227 | operation.tag_length, tag ) ); |
| 228 | } |
| 229 | else |
| 230 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 231 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 232 | if( operation.alg == PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 233 | { |
Paul Elliott | 96b0173 | 2021-07-16 17:00:26 +0100 | [diff] [blame] | 234 | if( operation.tag_length != 16 ) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 235 | { |
| 236 | status = PSA_ERROR_NOT_SUPPORTED; |
| 237 | goto exit; |
| 238 | } |
| 239 | status = mbedtls_to_psa_error( |
| 240 | mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly, |
| 241 | plaintext_length, |
| 242 | nonce, |
| 243 | additional_data, |
| 244 | additional_data_length, |
| 245 | plaintext, |
| 246 | ciphertext, |
| 247 | tag ) ); |
| 248 | } |
| 249 | else |
| 250 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 251 | { |
| 252 | (void) tag; |
| 253 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 254 | } |
| 255 | |
| 256 | if( status == PSA_SUCCESS ) |
| 257 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 258 | |
| 259 | exit: |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 260 | mbedtls_psa_aead_abort( &operation ); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 261 | |
| 262 | return( status ); |
| 263 | } |
| 264 | |
| 265 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 266 | * followed by the tag. Return the length of the part preceding the tag in |
| 267 | * *plaintext_length. This is the size of the plaintext in modes where |
| 268 | * the encrypted data has the same size as the plaintext, such as |
| 269 | * CCM and GCM. */ |
| 270 | static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length, |
| 271 | const uint8_t *ciphertext, |
| 272 | size_t ciphertext_length, |
| 273 | size_t plaintext_size, |
| 274 | const uint8_t **p_tag ) |
| 275 | { |
| 276 | size_t payload_length; |
| 277 | if( tag_length > ciphertext_length ) |
| 278 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 279 | payload_length = ciphertext_length - tag_length; |
| 280 | if( payload_length > plaintext_size ) |
| 281 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 282 | *p_tag = ciphertext + payload_length; |
| 283 | return( PSA_SUCCESS ); |
| 284 | } |
| 285 | |
| 286 | psa_status_t mbedtls_psa_aead_decrypt( |
| 287 | const psa_key_attributes_t *attributes, |
| 288 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 289 | psa_algorithm_t alg, |
| 290 | const uint8_t *nonce, size_t nonce_length, |
| 291 | const uint8_t *additional_data, size_t additional_data_length, |
| 292 | const uint8_t *ciphertext, size_t ciphertext_length, |
| 293 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length ) |
| 294 | { |
| 295 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 296 | mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 297 | const uint8_t *tag = NULL; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 298 | |
Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 299 | status = psa_aead_setup( &operation, attributes, key_buffer, |
| 300 | key_buffer_size, alg ); |
| 301 | |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 302 | if( status != PSA_SUCCESS ) |
| 303 | goto exit; |
| 304 | |
| 305 | status = psa_aead_unpadded_locate_tag( operation.tag_length, |
| 306 | ciphertext, ciphertext_length, |
| 307 | plaintext_size, &tag ); |
| 308 | if( status != PSA_SUCCESS ) |
| 309 | goto exit; |
| 310 | |
Paul Elliott | eac6c75 | 2021-09-15 19:08:27 +0100 | [diff] [blame] | 311 | status = mbedtls_aead_check_nonce_length( &operation, nonce_length ); |
| 312 | |
| 313 | if( status != PSA_SUCCESS ) |
Paul Elliott | cf2d66e | 2021-06-23 18:49:56 +0100 | [diff] [blame] | 314 | goto exit; |
Paul Elliott | cf2d66e | 2021-06-23 18:49:56 +0100 | [diff] [blame] | 315 | |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 316 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 317 | if( operation.alg == PSA_ALG_CCM ) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 318 | { |
| 319 | status = mbedtls_to_psa_error( |
| 320 | mbedtls_ccm_auth_decrypt( &operation.ctx.ccm, |
| 321 | ciphertext_length - operation.tag_length, |
| 322 | nonce, nonce_length, |
| 323 | additional_data, |
| 324 | additional_data_length, |
| 325 | ciphertext, plaintext, |
| 326 | tag, operation.tag_length ) ); |
| 327 | } |
| 328 | else |
| 329 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 330 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 331 | if( operation.alg == PSA_ALG_GCM ) |
Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 332 | { |
| 333 | status = mbedtls_to_psa_error( |
| 334 | mbedtls_gcm_auth_decrypt( &operation.ctx.gcm, |
| 335 | ciphertext_length - operation.tag_length, |
| 336 | nonce, nonce_length, |
| 337 | additional_data, |
| 338 | additional_data_length, |
| 339 | tag, operation.tag_length, |
| 340 | ciphertext, plaintext ) ); |
| 341 | } |
| 342 | else |
| 343 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 344 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 345 | if( operation.alg == PSA_ALG_CHACHA20_POLY1305 ) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 346 | { |
Paul Elliott | cf2d66e | 2021-06-23 18:49:56 +0100 | [diff] [blame] | 347 | if( operation.tag_length != 16 ) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 348 | { |
| 349 | status = PSA_ERROR_NOT_SUPPORTED; |
| 350 | goto exit; |
| 351 | } |
| 352 | status = mbedtls_to_psa_error( |
| 353 | mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly, |
| 354 | ciphertext_length - operation.tag_length, |
| 355 | nonce, |
| 356 | additional_data, |
| 357 | additional_data_length, |
| 358 | tag, |
| 359 | ciphertext, |
| 360 | plaintext ) ); |
| 361 | } |
| 362 | else |
| 363 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 364 | { |
| 365 | return( PSA_ERROR_NOT_SUPPORTED ); |
| 366 | } |
| 367 | |
| 368 | if( status == PSA_SUCCESS ) |
| 369 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 370 | |
| 371 | exit: |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 372 | mbedtls_psa_aead_abort( &operation ); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 373 | |
| 374 | if( status == PSA_SUCCESS ) |
| 375 | *plaintext_length = ciphertext_length - operation.tag_length; |
| 376 | return( status ); |
| 377 | } |
Ronald Cron | 7ceee8d | 2021-03-17 16:55:43 +0100 | [diff] [blame] | 378 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 379 | /* Set the key and algorithm for a multipart authenticated encryption |
| 380 | * operation. */ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 381 | psa_status_t mbedtls_psa_aead_encrypt_setup( |
| 382 | mbedtls_psa_aead_operation_t *operation, |
| 383 | const psa_key_attributes_t *attributes, |
| 384 | const uint8_t *key_buffer, |
| 385 | size_t key_buffer_size, |
| 386 | psa_algorithm_t alg ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 387 | { |
Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 388 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 389 | |
Paul Elliott | 60aa203 | 2021-05-20 18:57:02 +0100 | [diff] [blame] | 390 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 391 | if( operation->alg == PSA_ALG_CCM ) |
| 392 | { |
Paul Elliott | e95259f | 2021-05-21 17:09:21 +0100 | [diff] [blame] | 393 | return( PSA_ERROR_NOT_SUPPORTED ); |
Paul Elliott | 60aa203 | 2021-05-20 18:57:02 +0100 | [diff] [blame] | 394 | } |
| 395 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 396 | |
Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 397 | status = psa_aead_setup( operation, attributes, key_buffer, |
| 398 | key_buffer_size, alg ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 399 | |
| 400 | if( status == PSA_SUCCESS ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 401 | operation->is_encrypt = 1; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 402 | |
| 403 | return ( status ); |
| 404 | } |
| 405 | |
| 406 | /* Set the key and algorithm for a multipart authenticated decryption |
| 407 | * operation. */ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 408 | psa_status_t mbedtls_psa_aead_decrypt_setup( |
| 409 | mbedtls_psa_aead_operation_t *operation, |
| 410 | const psa_key_attributes_t *attributes, |
| 411 | const uint8_t *key_buffer, |
| 412 | size_t key_buffer_size, |
| 413 | psa_algorithm_t alg ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 414 | { |
Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 415 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 416 | |
Paul Elliott | e95259f | 2021-05-21 17:09:21 +0100 | [diff] [blame] | 417 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
Paul Elliott | 60aa203 | 2021-05-20 18:57:02 +0100 | [diff] [blame] | 418 | if( operation->alg == PSA_ALG_CCM ) |
| 419 | { |
Paul Elliott | e95259f | 2021-05-21 17:09:21 +0100 | [diff] [blame] | 420 | return( PSA_ERROR_NOT_SUPPORTED ); |
Paul Elliott | 60aa203 | 2021-05-20 18:57:02 +0100 | [diff] [blame] | 421 | } |
| 422 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 423 | |
Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 424 | status = psa_aead_setup( operation, attributes, key_buffer, |
| 425 | key_buffer_size, alg ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 426 | |
| 427 | if( status == PSA_SUCCESS ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 428 | operation->is_encrypt = 0; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 429 | |
| 430 | return ( status ); |
| 431 | } |
| 432 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 433 | /* Set a nonce for the multipart AEAD operation*/ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 434 | psa_status_t mbedtls_psa_aead_set_nonce( |
| 435 | mbedtls_psa_aead_operation_t *operation, |
| 436 | const uint8_t *nonce, |
| 437 | size_t nonce_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 438 | { |
Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 439 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 440 | |
Paul Elliott | cf2d66e | 2021-06-23 18:49:56 +0100 | [diff] [blame] | 441 | if( mbedtls_aead_check_nonce_length( operation, nonce_length ) |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 442 | != PSA_SUCCESS ) |
Paul Elliott | cf2d66e | 2021-06-23 18:49:56 +0100 | [diff] [blame] | 443 | { |
| 444 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 445 | } |
| 446 | |
Paul Elliott | bc94978 | 2021-06-03 15:29:00 +0100 | [diff] [blame] | 447 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 448 | if( operation->alg == PSA_ALG_GCM ) |
| 449 | { |
Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 450 | status = mbedtls_to_psa_error( |
| 451 | mbedtls_gcm_starts( &operation->ctx.gcm, |
| 452 | operation->is_encrypt ? |
| 453 | MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT, |
| 454 | nonce, |
| 455 | nonce_length ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 456 | } |
| 457 | else |
| 458 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 459 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 460 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 461 | { |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 462 | status = mbedtls_to_psa_error( |
| 463 | mbedtls_chachapoly_starts( &operation->ctx.chachapoly, |
| 464 | nonce, |
| 465 | operation->is_encrypt ? |
| 466 | MBEDTLS_CHACHAPOLY_ENCRYPT : |
| 467 | MBEDTLS_CHACHAPOLY_DECRYPT ) ); |
Paul Elliott | efda340 | 2021-08-25 17:16:52 +0100 | [diff] [blame] | 468 | } |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 469 | else |
| 470 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 471 | { |
| 472 | ( void ) nonce; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 473 | |
| 474 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 475 | } |
| 476 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 477 | return( status ); |
| 478 | } |
Paul Elliott | efda340 | 2021-08-25 17:16:52 +0100 | [diff] [blame] | 479 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 480 | /* Declare the lengths of the message and additional data for AEAD. */ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 481 | psa_status_t mbedtls_psa_aead_set_lengths( |
| 482 | mbedtls_psa_aead_operation_t *operation, |
| 483 | size_t ad_length, |
| 484 | size_t plaintext_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 485 | { |
| 486 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 487 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 488 | if( operation->alg == PSA_ALG_GCM ) |
| 489 | { |
Paul Elliott | ef29e17 | 2021-05-10 19:33:03 +0100 | [diff] [blame] | 490 | /* 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] | 491 | * bits. Without the guard this code will generate warnings on 32bit |
Paul Elliott | e95259f | 2021-05-21 17:09:21 +0100 | [diff] [blame] | 492 | * builds */ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 493 | #if SIZE_MAX > UINT32_MAX |
| 494 | if( ( (uint64_t) ad_length ) >> 61 != 0 || |
| 495 | ( (uint64_t) plaintext_length ) > 0xFFFFFFFE0ull ) |
| 496 | { |
| 497 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
| 498 | } |
| 499 | #endif |
| 500 | } |
| 501 | else |
| 502 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 503 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 504 | if( operation->alg == PSA_ALG_CCM ) |
| 505 | { |
| 506 | if( ad_length > 0xFF00 ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 507 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 508 | } |
| 509 | else |
| 510 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 511 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 512 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 513 | { |
| 514 | /* No length restrictions for ChaChaPoly. */ |
| 515 | } |
| 516 | else |
| 517 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 518 | { |
Paul Elliott | bc94978 | 2021-06-03 15:29:00 +0100 | [diff] [blame] | 519 | ( void ) operation; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 520 | ( void ) ad_length; |
| 521 | ( void ) plaintext_length; |
| 522 | |
| 523 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 524 | } |
| 525 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 526 | return ( PSA_SUCCESS ); |
| 527 | } |
| 528 | |
| 529 | /* Pass additional data to an active multipart AEAD operation. */ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 530 | psa_status_t mbedtls_psa_aead_update_ad( |
| 531 | mbedtls_psa_aead_operation_t *operation, |
| 532 | const uint8_t *input, |
| 533 | size_t input_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 534 | { |
| 535 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 536 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 537 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 538 | if( operation->alg == PSA_ALG_GCM ) |
| 539 | { |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 540 | status = mbedtls_to_psa_error( |
Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 541 | mbedtls_gcm_update_ad( &operation->ctx.gcm, input, input_length ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 542 | } |
| 543 | else |
| 544 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 545 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 546 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 547 | { |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 548 | status = mbedtls_to_psa_error( |
| 549 | mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly, |
| 550 | input, |
| 551 | input_length ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 552 | } |
| 553 | else |
| 554 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 555 | { |
Paul Elliott | bc94978 | 2021-06-03 15:29:00 +0100 | [diff] [blame] | 556 | ( void ) operation; |
| 557 | ( void ) input; |
| 558 | ( void ) input_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 559 | |
| 560 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 561 | } |
| 562 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 563 | return ( status ); |
| 564 | } |
| 565 | |
| 566 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 567 | * operation.*/ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 568 | psa_status_t mbedtls_psa_aead_update( |
| 569 | mbedtls_psa_aead_operation_t *operation, |
| 570 | const uint8_t *input, |
| 571 | size_t input_length, |
| 572 | uint8_t *output, |
| 573 | size_t output_size, |
| 574 | size_t *output_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 575 | { |
Paul Elliott | e2c788d | 2021-05-13 17:16:01 +0100 | [diff] [blame] | 576 | size_t update_output_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 577 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 578 | |
Paul Elliott | e2c788d | 2021-05-13 17:16:01 +0100 | [diff] [blame] | 579 | update_output_length = input_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 580 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 581 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 582 | if( operation->alg == PSA_ALG_GCM ) |
| 583 | { |
Paul Elliott | ecce901 | 2021-07-23 15:44:11 +0100 | [diff] [blame] | 584 | if( output_size < input_length ) |
| 585 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 586 | |
Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 587 | status = mbedtls_to_psa_error( |
| 588 | mbedtls_gcm_update( &operation->ctx.gcm, |
| 589 | input, input_length, |
| 590 | output, output_size, |
| 591 | &update_output_length ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 592 | } |
| 593 | else |
| 594 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 595 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 596 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 597 | { |
Paul Elliott | ecce901 | 2021-07-23 15:44:11 +0100 | [diff] [blame] | 598 | if( output_size < input_length ) |
| 599 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 600 | |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 601 | status = mbedtls_to_psa_error( |
| 602 | mbedtls_chachapoly_update( &operation->ctx.chachapoly, |
| 603 | input_length, |
| 604 | input, |
| 605 | output ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 606 | } |
| 607 | else |
| 608 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 609 | { |
Paul Elliott | bc94978 | 2021-06-03 15:29:00 +0100 | [diff] [blame] | 610 | ( void ) input; |
| 611 | ( void ) input_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 612 | |
| 613 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 614 | } |
| 615 | |
| 616 | if( status == PSA_SUCCESS ) |
Paul Elliott | e2c788d | 2021-05-13 17:16:01 +0100 | [diff] [blame] | 617 | *output_length = update_output_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 618 | |
| 619 | return( status ); |
| 620 | } |
| 621 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 622 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 623 | psa_status_t mbedtls_psa_aead_finish( |
| 624 | mbedtls_psa_aead_operation_t *operation, |
| 625 | uint8_t *ciphertext, |
| 626 | size_t ciphertext_size, |
| 627 | size_t *ciphertext_length, |
| 628 | uint8_t *tag, |
| 629 | size_t tag_size, |
| 630 | size_t *tag_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 631 | { |
| 632 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 633 | size_t finish_output_size = 0; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 634 | |
Paul Elliott | 315628d | 2021-07-20 18:25:54 +0100 | [diff] [blame] | 635 | if( tag_size < operation->tag_length ) |
| 636 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 637 | |
| 638 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 639 | if( operation->alg == PSA_ALG_GCM ) |
Paul Elliott | ecce901 | 2021-07-23 15:44:11 +0100 | [diff] [blame] | 640 | { |
| 641 | if( ciphertext_size < 15 ) |
| 642 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 643 | |
Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 644 | status = mbedtls_to_psa_error( |
| 645 | mbedtls_gcm_finish( &operation->ctx.gcm, |
| 646 | ciphertext, ciphertext_size, |
Paul Elliott | 2fe5db8 | 2021-07-22 18:10:43 +0100 | [diff] [blame] | 647 | tag, operation->tag_length ) ); |
Paul Elliott | ecce901 | 2021-07-23 15:44:11 +0100 | [diff] [blame] | 648 | } |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 649 | else |
| 650 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 651 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 652 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
Paul Elliott | ed08cf8 | 2021-07-22 18:48:24 +0100 | [diff] [blame] | 653 | { |
| 654 | /* Belt and braces. Although the above tag_size check should have |
| 655 | * already done this, if we later start supporting smaller tag sizes |
| 656 | * for chachapoly, then passing a tag buffer smaller than 16 into here |
| 657 | * could cause a buffer overflow, so better safe than sorry. */ |
| 658 | if( tag_size < 16 ) |
| 659 | return( PSA_ERROR_BUFFER_TOO_SMALL ); |
| 660 | |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 661 | status = mbedtls_to_psa_error( |
Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 662 | mbedtls_chachapoly_finish( &operation->ctx.chachapoly, |
| 663 | tag ) ); |
Paul Elliott | ed08cf8 | 2021-07-22 18:48:24 +0100 | [diff] [blame] | 664 | } |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 665 | else |
| 666 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 667 | { |
| 668 | ( void ) ciphertext; |
| 669 | ( void ) ciphertext_size; |
| 670 | ( void ) ciphertext_length; |
| 671 | ( void ) tag; |
| 672 | ( void ) tag_size; |
| 673 | ( void ) tag_length; |
| 674 | |
| 675 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 676 | } |
| 677 | |
| 678 | if( status == PSA_SUCCESS ) |
| 679 | { |
Paul Elliott | 8ff7421 | 2021-09-19 18:39:23 +0100 | [diff] [blame^] | 680 | /* This will be zero for all supported algorithms currently, but left |
| 681 | * here for future support. */ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 682 | *ciphertext_length = finish_output_size; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 683 | *tag_length = operation->tag_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 686 | return ( status ); |
| 687 | } |
| 688 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 689 | /* Abort an AEAD operation */ |
Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 690 | psa_status_t mbedtls_psa_aead_abort( |
| 691 | mbedtls_psa_aead_operation_t *operation ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 692 | { |
Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 693 | switch( operation->alg ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 694 | { |
Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 695 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 696 | case PSA_ALG_CCM: |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 697 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 698 | break; |
| 699 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 700 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 701 | case PSA_ALG_GCM: |
| 702 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 703 | break; |
| 704 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 705 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 706 | case PSA_ALG_CHACHA20_POLY1305: |
| 707 | mbedtls_chachapoly_free( &operation->ctx.chachapoly ); |
| 708 | break; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 709 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 710 | } |
| 711 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 712 | operation->is_encrypt = 0; |
Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 713 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 714 | return( PSA_SUCCESS ); |
| 715 | } |
| 716 | |
Ronald Cron | 7ceee8d | 2021-03-17 16:55:43 +0100 | [diff] [blame] | 717 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 718 | |