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 | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 340 | psa_status_t mbedtls_psa_aead_encrypt_setup( mbedtls_psa_aead_operation_t |
| 341 | *operation, |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 342 | const psa_key_attributes_t |
| 343 | *attributes, |
| 344 | const uint8_t *key_buffer, |
| 345 | size_t key_buffer_size, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 346 | psa_algorithm_t alg ) |
| 347 | { |
Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 348 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 349 | |
Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 350 | status = psa_aead_setup( operation, attributes, key_buffer, |
| 351 | key_buffer_size, alg ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 352 | |
| 353 | if( status == PSA_SUCCESS ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 354 | operation->is_encrypt = 1; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 355 | |
| 356 | return ( status ); |
| 357 | } |
| 358 | |
| 359 | /* Set the key and algorithm for a multipart authenticated decryption |
| 360 | * operation. */ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 361 | psa_status_t mbedtls_psa_aead_decrypt_setup( mbedtls_psa_aead_operation_t |
| 362 | *operation, |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 363 | const psa_key_attributes_t |
| 364 | *attributes, |
| 365 | const uint8_t *key_buffer, |
| 366 | size_t key_buffer_size, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 367 | psa_algorithm_t alg ) |
| 368 | { |
Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 369 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 370 | |
| 371 | (void) key_buffer_size; |
| 372 | |
Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 373 | status = psa_aead_setup( operation, attributes, key_buffer, |
| 374 | key_buffer_size, alg ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 375 | |
| 376 | if( status == PSA_SUCCESS ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 377 | operation->is_encrypt = 0; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 378 | |
| 379 | return ( status ); |
| 380 | } |
| 381 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 382 | /* Set a nonce for the multipart AEAD operation*/ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 383 | psa_status_t mbedtls_psa_aead_set_nonce( mbedtls_psa_aead_operation_t |
| 384 | *operation, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 385 | const uint8_t *nonce, |
| 386 | size_t nonce_length ) |
| 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 | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 390 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 391 | if( operation->alg == PSA_ALG_GCM ) |
| 392 | { |
| 393 | /* GCM sets nonce once additional data has been supplied */ |
| 394 | memcpy(operation->nonce, nonce, nonce_length); |
| 395 | |
| 396 | /* We know that nonce size cannot exceed the uint8_t size */ |
| 397 | operation->nonce_length = ( uint8_t ) nonce_length; |
| 398 | status = PSA_SUCCESS; |
| 399 | } |
| 400 | else |
| 401 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 402 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 403 | if( operation->alg == PSA_ALG_CCM ) |
| 404 | { |
| 405 | /* Multipart CCM not supported as yet, so CCM is basically operating |
| 406 | in oneshot mode. Store the nonce as we need this later */ |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 407 | memcpy( operation->nonce, nonce, nonce_length ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 408 | |
| 409 | /* We know that nonce size cannot exceed the uint8_t size */ |
| 410 | operation->nonce_length = ( uint8_t ) nonce_length; |
| 411 | status = PSA_SUCCESS; |
| 412 | } |
| 413 | else |
| 414 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 415 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 416 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 417 | { |
| 418 | if( nonce_length != 12 && nonce_length != 8) |
| 419 | { |
| 420 | return( PSA_ERROR_INVALID_ARGUMENT ); |
| 421 | } |
| 422 | |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 423 | status = mbedtls_to_psa_error( |
| 424 | mbedtls_chachapoly_starts( &operation->ctx.chachapoly, |
| 425 | nonce, |
| 426 | operation->is_encrypt ? |
| 427 | MBEDTLS_CHACHAPOLY_ENCRYPT : |
| 428 | MBEDTLS_CHACHAPOLY_DECRYPT ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 429 | } |
| 430 | else |
| 431 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 432 | { |
| 433 | ( void ) nonce; |
| 434 | ( void ) nonce_length; |
| 435 | |
| 436 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 437 | } |
| 438 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 439 | return( status ); |
| 440 | } |
| 441 | /* Declare the lengths of the message and additional data for AEAD. */ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 442 | psa_status_t mbedtls_psa_aead_set_lengths( mbedtls_psa_aead_operation_t |
| 443 | *operation, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 444 | size_t ad_length, |
| 445 | size_t plaintext_length ) |
| 446 | { |
| 447 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 448 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 449 | if( operation->alg == PSA_ALG_GCM ) |
| 450 | { |
Paul Elliott | ef29e17 | 2021-05-10 19:33:03 +0100 | [diff] [blame] | 451 | /* Lengths can only be too large for GCM if size_t is bigger than 32 |
Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 452 | * bits. Without th |
| 453 | e guard this code will generate warnings on 32bit builds*/ |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 454 | #if SIZE_MAX > UINT32_MAX |
| 455 | if( ( (uint64_t) ad_length ) >> 61 != 0 || |
| 456 | ( (uint64_t) plaintext_length ) > 0xFFFFFFFE0ull ) |
| 457 | { |
| 458 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
| 459 | } |
| 460 | #endif |
| 461 | } |
| 462 | else |
| 463 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 464 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 465 | if( operation->alg == PSA_ALG_CCM ) |
| 466 | { |
| 467 | if( ad_length > 0xFF00 ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 468 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 469 | } |
| 470 | else |
| 471 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 472 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 473 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 474 | { |
| 475 | /* No length restrictions for ChaChaPoly. */ |
| 476 | } |
| 477 | else |
| 478 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 479 | { |
| 480 | ( void ) ad_length; |
| 481 | ( void ) plaintext_length; |
| 482 | |
| 483 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 484 | } |
| 485 | |
| 486 | operation->ad_remaining = ad_length; |
| 487 | operation->body_remaining = plaintext_length; |
| 488 | operation->lengths_set = 1; |
| 489 | |
| 490 | return ( PSA_SUCCESS ); |
| 491 | } |
| 492 | |
| 493 | /* Pass additional data to an active multipart AEAD operation. */ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 494 | psa_status_t mbedtls_psa_aead_update_ad( mbedtls_psa_aead_operation_t |
| 495 | *operation, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 496 | const uint8_t *input, |
| 497 | size_t input_length ) |
| 498 | { |
| 499 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 500 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 501 | if( operation->lengths_set ) |
| 502 | { |
| 503 | if ( operation->ad_remaining < input_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 504 | return( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 505 | |
| 506 | operation->ad_remaining -= input_length; |
| 507 | } |
| 508 | |
| 509 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 510 | if( operation->alg == PSA_ALG_GCM ) |
| 511 | { |
Paul Elliott | c10ad21 | 2021-05-13 17:08:29 +0100 | [diff] [blame^] | 512 | /* GCM currently requires all the additional data to be passed in in |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 513 | * one contiguous buffer, so until that is re-done, we have to enforce |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 514 | * this, as we cannot allocate a buffer to collate multiple calls into. |
| 515 | */ |
Paul Elliott | c10ad21 | 2021-05-13 17:08:29 +0100 | [diff] [blame^] | 516 | if( operation->ad_started ) |
| 517 | return( PSA_ERROR_NOT_SUPPORTED ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 518 | |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 519 | status = mbedtls_to_psa_error( |
| 520 | mbedtls_gcm_starts( &operation->ctx.gcm, |
| 521 | operation->is_encrypt ? |
| 522 | MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT, |
| 523 | operation->nonce, |
| 524 | operation->nonce_length, |
| 525 | input, |
| 526 | input_length ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 527 | |
| 528 | } |
| 529 | else |
| 530 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 531 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 532 | if( operation->alg == PSA_ALG_CCM ) |
| 533 | { |
| 534 | /* CCM requires all additional data to be passed in in one go at the |
| 535 | minute, as we are basically operating in oneshot mode. */ |
Paul Elliott | 72c1008 | 2021-04-23 19:02:16 +0100 | [diff] [blame] | 536 | if( operation->ad_started ) |
Paul Elliott | c10ad21 | 2021-05-13 17:08:29 +0100 | [diff] [blame^] | 537 | return( PSA_ERROR_NOT_SUPPORTED ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 538 | |
| 539 | /* Save the additional data for later, this will be passed in |
| 540 | when we have the body. */ |
| 541 | operation->ad_buffer = ( uint8_t * ) mbedtls_calloc(1, input_length ); |
| 542 | |
| 543 | if( operation->ad_buffer ) |
| 544 | { |
| 545 | memcpy( operation->ad_buffer, input, input_length ); |
| 546 | operation->ad_length = input_length; |
Paul Elliott | 72c1008 | 2021-04-23 19:02:16 +0100 | [diff] [blame] | 547 | status = PSA_SUCCESS; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 548 | } |
| 549 | else |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 550 | return ( PSA_ERROR_INSUFFICIENT_MEMORY ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 551 | } |
| 552 | else |
| 553 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 554 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 555 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 556 | { |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 557 | status = mbedtls_to_psa_error( |
| 558 | mbedtls_chachapoly_update_aad( &operation->ctx.chachapoly, |
| 559 | input, |
| 560 | input_length ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 561 | } |
| 562 | else |
| 563 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 564 | { |
| 565 | (void) input; |
| 566 | (void) input_length; |
| 567 | |
| 568 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 569 | } |
| 570 | |
| 571 | if( status == PSA_SUCCESS ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 572 | operation->ad_started = 1; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 573 | |
| 574 | return ( status ); |
| 575 | } |
| 576 | |
| 577 | /* Encrypt or decrypt a message fragment in an active multipart AEAD |
| 578 | * operation.*/ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 579 | psa_status_t mbedtls_psa_aead_update( mbedtls_psa_aead_operation_t *operation, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 580 | const uint8_t *input, |
| 581 | size_t input_length, |
| 582 | uint8_t *output, |
| 583 | size_t output_size, |
| 584 | size_t *output_length ) |
| 585 | { |
| 586 | size_t update_output_size; |
| 587 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 588 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 589 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 590 | update_output_size = input_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 591 | |
Paul Elliott | 72c1008 | 2021-04-23 19:02:16 +0100 | [diff] [blame] | 592 | if( PSA_AEAD_UPDATE_OUTPUT_SIZE( operation->key_type, operation->alg, |
| 593 | input_length ) > output_size ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 594 | return ( PSA_ERROR_BUFFER_TOO_SMALL ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 595 | |
| 596 | if( operation->lengths_set) |
| 597 | { |
| 598 | /* Additional data length was supplied, but not all the additional |
| 599 | data was supplied.*/ |
| 600 | if( operation->ad_remaining != 0 ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 601 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 602 | |
| 603 | /* Too much data provided. */ |
| 604 | if( operation->body_remaining < input_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 605 | return ( PSA_ERROR_INVALID_ARGUMENT ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 606 | |
| 607 | operation->body_remaining -= input_length; |
| 608 | } |
| 609 | |
| 610 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 611 | if( operation->alg == PSA_ALG_GCM ) |
| 612 | { |
| 613 | /* For the time being set the requirement that all of the body data |
| 614 | * must be passed in in one update, rather than deal with the complexity |
| 615 | * of non block size aligned updates. This will be fixed in 3.0 when |
| 616 | we can change the signature of the GCM multipart functions */ |
Paul Elliott | c10ad21 | 2021-05-13 17:08:29 +0100 | [diff] [blame^] | 617 | if( operation->body_started ) |
| 618 | return( PSA_ERROR_NOT_SUPPORTED ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 619 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 620 | |
| 621 | status = mbedtls_to_psa_error( mbedtls_gcm_update( &operation->ctx.gcm, |
| 622 | input_length, |
| 623 | input, |
| 624 | output ) ); |
| 625 | } |
| 626 | else |
| 627 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 628 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 629 | if( operation->alg == PSA_ALG_CCM ) |
| 630 | { |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 631 | /* CCM does not support multipart yet, so all the input has to be |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 632 | passed in in one go. */ |
Paul Elliott | 72c1008 | 2021-04-23 19:02:16 +0100 | [diff] [blame] | 633 | if( operation->body_started ) |
Paul Elliott | c10ad21 | 2021-05-13 17:08:29 +0100 | [diff] [blame^] | 634 | return( PSA_ERROR_NOT_SUPPORTED ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 635 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 636 | /* Need to store tag for Finish() / Verify() */ |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 637 | operation->tag_buffer = |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 638 | ( uint8_t * ) mbedtls_calloc( 1, operation->tag_length ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 639 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 640 | if( operation->tag_buffer ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 641 | { |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 642 | if( operation->is_encrypt ) |
| 643 | { |
| 644 | /* Perform oneshot CCM encryption with additional data already |
| 645 | stored, as CCM does not support multipart yet.*/ |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 646 | status = mbedtls_to_psa_error( |
| 647 | mbedtls_ccm_encrypt_and_tag( &operation->ctx.ccm, |
| 648 | input_length, |
| 649 | operation->nonce, |
| 650 | operation->nonce_length, |
| 651 | operation->ad_buffer, |
| 652 | operation->ad_length, |
| 653 | input, |
| 654 | output, |
| 655 | operation->tag_buffer, |
| 656 | operation->tag_length ) ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 657 | |
| 658 | /* Even if the above operation fails, we no longer need the |
| 659 | additional data.*/ |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 660 | mbedtls_free( operation->ad_buffer ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 661 | operation->ad_buffer = NULL; |
| 662 | operation->ad_length = 0; |
| 663 | } |
| 664 | else |
| 665 | { |
| 666 | /* Need to back up the body data so we can do this again |
| 667 | later.*/ |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 668 | operation->body_buffer = |
| 669 | ( uint8_t * ) mbedtls_calloc(1, input_length ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 670 | |
| 671 | if( operation->body_buffer ) |
| 672 | { |
| 673 | memcpy( operation->body_buffer, input, input_length ); |
| 674 | operation->body_length = input_length; |
| 675 | |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 676 | /* this will fail, as the tag is clearly false, but will |
| 677 | write the decrypted data to the output buffer.*/ |
| 678 | ret = mbedtls_ccm_auth_decrypt( &operation->ctx.ccm, |
| 679 | input_length, |
| 680 | operation->nonce, |
| 681 | operation->nonce_length, |
| 682 | operation->ad_buffer, |
| 683 | operation->ad_length, |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 684 | input, output, |
| 685 | operation->tag_buffer, |
| 686 | operation->tag_length ); |
| 687 | |
| 688 | if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED ) |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 689 | status = PSA_SUCCESS; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 690 | else |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 691 | status = mbedtls_to_psa_error( ret ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 692 | } |
| 693 | else |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 694 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 695 | } |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 696 | } |
| 697 | else |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 698 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 699 | } |
| 700 | else |
| 701 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 702 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 703 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
| 704 | { |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 705 | status = mbedtls_to_psa_error( |
| 706 | mbedtls_chachapoly_update( &operation->ctx.chachapoly, |
| 707 | input_length, |
| 708 | input, |
| 709 | output ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 710 | } |
| 711 | else |
| 712 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 713 | { |
| 714 | (void) input; |
| 715 | (void) input_length; |
| 716 | |
| 717 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 718 | } |
| 719 | |
| 720 | if( status == PSA_SUCCESS ) |
| 721 | { |
| 722 | *output_length = update_output_size; |
| 723 | operation->body_started = 1; |
| 724 | } |
| 725 | |
| 726 | return( status ); |
| 727 | } |
| 728 | |
| 729 | /* Common checks for both mbedtls_psa_aead_finish() and |
| 730 | mbedtls_psa_aead_verify() */ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 731 | static psa_status_t mbedtls_psa_aead_finish_checks( mbedtls_psa_aead_operation_t |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 732 | *operation, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 733 | size_t output_size, |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 734 | size_t tag_size ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 735 | { |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 736 | size_t finish_output_size; |
| 737 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 738 | if( operation->lengths_set ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 739 | if( operation->ad_remaining != 0 || operation->body_remaining != 0 ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 740 | return( PSA_ERROR_BAD_STATE ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 741 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 742 | if( tag_size < operation->tag_length ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 743 | return ( PSA_ERROR_BUFFER_TOO_SMALL ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 744 | |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 745 | finish_output_size = operation->is_encrypt ? |
| 746 | PSA_AEAD_FINISH_OUTPUT_SIZE( operation->key_type, operation->alg ) : |
| 747 | PSA_AEAD_VERIFY_OUTPUT_SIZE( operation->key_type, operation->alg ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 748 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 749 | if( output_size < finish_output_size ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 750 | return ( PSA_ERROR_BUFFER_TOO_SMALL ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 751 | |
| 752 | return ( PSA_SUCCESS ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /* Finish encrypting a message in a multipart AEAD operation. */ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 756 | psa_status_t mbedtls_psa_aead_finish( mbedtls_psa_aead_operation_t *operation, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 757 | uint8_t *ciphertext, |
| 758 | size_t ciphertext_size, |
| 759 | size_t *ciphertext_length, |
| 760 | uint8_t *tag, |
| 761 | size_t tag_size, |
| 762 | size_t *tag_length ) |
| 763 | { |
| 764 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 765 | size_t finish_output_size = 0; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 766 | |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 767 | status = mbedtls_psa_aead_finish_checks( operation, ciphertext_size, |
| 768 | tag_size ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 769 | |
| 770 | if( status != PSA_SUCCESS ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 771 | return status; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 772 | |
| 773 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 774 | if( operation->alg == PSA_ALG_GCM ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 775 | /* We will need to do final GCM pass in here when multipart is done. */ |
| 776 | status = mbedtls_to_psa_error( mbedtls_gcm_finish( &operation->ctx.gcm, |
| 777 | tag, |
| 778 | tag_size ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 779 | else |
| 780 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 781 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 782 | if( operation->alg == PSA_ALG_CCM ) |
| 783 | { |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 784 | /* Copy the previously generated tag into place */ |
| 785 | memcpy( tag, operation->tag_buffer, operation->tag_length ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 786 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 787 | mbedtls_free(operation->tag_buffer); |
| 788 | operation->tag_buffer = NULL; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 789 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 790 | status = PSA_SUCCESS; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 791 | } |
| 792 | else |
| 793 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 794 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 795 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 796 | status = mbedtls_to_psa_error( |
| 797 | mbedtls_chachapoly_finish( &operation->ctx.chachapoly, |
| 798 | tag ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 799 | else |
| 800 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 801 | { |
| 802 | ( void ) ciphertext; |
| 803 | ( void ) ciphertext_size; |
| 804 | ( void ) ciphertext_length; |
| 805 | ( void ) tag; |
| 806 | ( void ) tag_size; |
| 807 | ( void ) tag_length; |
| 808 | |
| 809 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 810 | } |
| 811 | |
| 812 | if( status == PSA_SUCCESS ) |
| 813 | { |
| 814 | *ciphertext_length = finish_output_size; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 815 | *tag_length = operation->tag_length; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | mbedtls_psa_aead_abort(operation); |
| 819 | |
| 820 | return ( status ); |
| 821 | } |
| 822 | |
| 823 | /* Finish authenticating and decrypting a message in a multipart AEAD |
| 824 | * operation.*/ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 825 | psa_status_t mbedtls_psa_aead_verify( mbedtls_psa_aead_operation_t *operation, |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 826 | uint8_t *plaintext, |
| 827 | size_t plaintext_size, |
| 828 | size_t *plaintext_length, |
| 829 | const uint8_t *tag, |
| 830 | size_t tag_length ) |
| 831 | { |
| 832 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 833 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 834 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 835 | uint8_t * temp_buffer; |
| 836 | size_t temp_buffer_size; |
| 837 | |
| 838 | size_t finish_output_size = 0; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 839 | |
| 840 | int do_tag_check = 1; |
Paul Elliott | ccaea40 | 2021-05-13 14:22:52 +0100 | [diff] [blame] | 841 | uint8_t check_tag[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 842 | |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 843 | status = mbedtls_psa_aead_finish_checks( operation, plaintext_size, |
| 844 | tag_length ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 845 | |
| 846 | if( status != PSA_SUCCESS ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 847 | return status; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 848 | |
| 849 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 850 | if( operation->alg == PSA_ALG_GCM ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 851 | /* Call finish to get the tag for comparison */ |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 852 | status = mbedtls_to_psa_error( |
| 853 | mbedtls_gcm_finish( &operation->ctx.gcm, |
| 854 | check_tag, |
| 855 | operation->tag_length ) ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 856 | else |
| 857 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 858 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 859 | if( operation->alg == PSA_ALG_CCM ) |
| 860 | { |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 861 | if( !operation->ad_buffer || !operation->body_buffer ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 862 | return( PSA_ERROR_BAD_STATE ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 863 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 864 | /* Perform oneshot CCM decryption *again*, as its the |
| 865 | * only way to get the tag, but this time throw away the |
| 866 | results, as verify cannot write that much data. */ |
| 867 | temp_buffer_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( operation->key_type, |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 868 | operation->alg, |
| 869 | operation->body_length |
| 870 | ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 871 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 872 | temp_buffer = ( uint8_t * ) mbedtls_calloc(1, temp_buffer_size ); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 873 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 874 | if( temp_buffer ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 875 | { |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 876 | ret = mbedtls_ccm_auth_decrypt( &operation->ctx.ccm, |
| 877 | operation->body_length, |
| 878 | operation->nonce, |
| 879 | operation->nonce_length, |
| 880 | operation->ad_buffer, |
| 881 | operation->ad_length, |
| 882 | operation->body_buffer, |
| 883 | temp_buffer, tag, tag_length ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 884 | |
| 885 | if( ret == MBEDTLS_ERR_CCM_AUTH_FAILED ) |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 886 | status = PSA_ERROR_INVALID_SIGNATURE; |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 887 | else |
| 888 | { |
| 889 | status = mbedtls_to_psa_error( ret ); |
| 890 | do_tag_check = 0; |
| 891 | } |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 892 | } |
| 893 | else |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 894 | status = PSA_ERROR_INSUFFICIENT_MEMORY; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 895 | |
| 896 | /* Even if the above operation fails, we no longer need the data */ |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 897 | mbedtls_free(temp_buffer); |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 898 | |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 899 | mbedtls_free(operation->body_buffer); |
| 900 | operation->body_buffer = NULL; |
| 901 | operation->body_length = 0; |
| 902 | |
| 903 | mbedtls_free(operation->tag_buffer); |
| 904 | operation->tag_buffer = NULL; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 905 | } |
| 906 | else |
| 907 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 908 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 909 | if( operation->alg == PSA_ALG_CHACHA20_POLY1305 ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 910 | // call finish to get the tag for comparison. |
Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 911 | status = mbedtls_to_psa_error( |
| 912 | mbedtls_chachapoly_finish( &operation->ctx.chachapoly, |
| 913 | check_tag ) ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 914 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 915 | else |
| 916 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 917 | { |
| 918 | ( void ) plaintext; |
| 919 | ( void ) plaintext_size; |
| 920 | ( void ) plaintext_length; |
| 921 | ( void ) tag; |
| 922 | ( void ) tag_length; |
| 923 | |
| 924 | return ( PSA_ERROR_NOT_SUPPORTED ); |
| 925 | } |
| 926 | |
| 927 | if( status == PSA_SUCCESS ) |
| 928 | { |
Paul Elliott | 72c1008 | 2021-04-23 19:02:16 +0100 | [diff] [blame] | 929 | *plaintext_length = finish_output_size; |
| 930 | |
Paul Elliott | 6edb747 | 2021-05-10 19:29:35 +0100 | [diff] [blame] | 931 | if( do_tag_check && |
| 932 | mbedtls_psa_safer_memcmp(tag, check_tag, tag_length) != 0 ) |
Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 933 | status = PSA_ERROR_INVALID_SIGNATURE; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | mbedtls_psa_aead_abort(operation); |
| 937 | |
| 938 | return ( status ); |
| 939 | } |
| 940 | |
| 941 | /* Abort an AEAD operation */ |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 942 | psa_status_t mbedtls_psa_aead_abort( mbedtls_psa_aead_operation_t *operation ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 943 | { |
Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 944 | switch( operation->alg ) |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 945 | { |
Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 946 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 947 | case PSA_ALG_CCM: |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 948 | mbedtls_ccm_free( &operation->ctx.ccm ); |
| 949 | break; |
| 950 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 951 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 952 | case PSA_ALG_GCM: |
| 953 | mbedtls_gcm_free( &operation->ctx.gcm ); |
| 954 | break; |
| 955 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 956 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 957 | case PSA_ALG_CHACHA20_POLY1305: |
| 958 | mbedtls_chachapoly_free( &operation->ctx.chachapoly ); |
| 959 | break; |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 960 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
| 961 | } |
| 962 | |
Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 963 | operation->lengths_set = 0; |
| 964 | operation->is_encrypt = 0; |
| 965 | operation->ad_started = 0; |
| 966 | operation->body_started = 0; |
| 967 | |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 968 | mbedtls_free( operation->ad_buffer ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 969 | operation->ad_buffer = NULL; |
| 970 | operation->ad_length = 0; |
| 971 | |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 972 | mbedtls_free( operation->body_buffer ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 973 | operation->body_buffer = NULL; |
| 974 | operation->body_length = 0; |
| 975 | |
Paul Elliott | 80acb7e | 2021-05-12 12:41:33 +0100 | [diff] [blame] | 976 | mbedtls_free( operation->tag_buffer ); |
Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 977 | operation->tag_buffer = NULL; |
| 978 | |
Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 979 | return( PSA_SUCCESS ); |
| 980 | } |
| 981 | |
Ronald Cron | 7ceee8d | 2021-03-17 16:55:43 +0100 | [diff] [blame] | 982 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 983 | |