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