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