| 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 | 
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 6 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Ronald Cron | 7ceee8d | 2021-03-17 16:55:43 +0100 | [diff] [blame] | 7 | */ | 
|  | 8 |  | 
|  | 9 | #include "common.h" | 
|  | 10 |  | 
|  | 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) | 
|  | 12 |  | 
|  | 13 | #include "psa_crypto_aead.h" | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 14 | #include "psa_crypto_core.h" | 
| Dave Rodgman | 1630447 | 2022-11-02 09:25:38 +0000 | [diff] [blame] | 15 | #include "psa_crypto_cipher.h" | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 16 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 17 | #include <string.h> | 
|  | 18 | #include "mbedtls/platform.h" | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 19 |  | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 20 | #include "mbedtls/ccm.h" | 
|  | 21 | #include "mbedtls/chachapoly.h" | 
|  | 22 | #include "mbedtls/cipher.h" | 
|  | 23 | #include "mbedtls/gcm.h" | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 24 | #include "mbedtls/error.h" | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 25 |  | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 26 | static psa_status_t psa_aead_setup( | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 27 | mbedtls_psa_aead_operation_t *operation, | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 28 | const psa_key_attributes_t *attributes, | 
|  | 29 | const uint8_t *key_buffer, | 
| Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 30 | size_t key_buffer_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 31 | psa_algorithm_t alg) | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 32 | { | 
|  | 33 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Valerio Setti | 4a24982 | 2023-10-18 12:34:54 +0200 | [diff] [blame] | 34 | mbedtls_cipher_id_t cipher_id; | 
|  | 35 | mbedtls_cipher_mode_t mode; | 
|  | 36 | size_t key_bits = attributes->core.bits; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | (void) key_buffer_size; | 
| Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 38 |  | 
| Valerio Setti | 4a24982 | 2023-10-18 12:34:54 +0200 | [diff] [blame] | 39 | status = mbedtls_cipher_values_from_psa(alg, attributes->core.type, | 
|  | 40 | &key_bits, &mode, &cipher_id); | 
|  | 41 | if (status != PSA_SUCCESS) { | 
|  | 42 | return status; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 44 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 46 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): | 
| Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 48 | operation->alg = PSA_ALG_CCM; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 49 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. | 
|  | 50 | * The call to mbedtls_ccm_encrypt_and_tag or | 
|  | 51 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) { | 
|  | 53 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 54 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 55 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | mbedtls_ccm_init(&operation->ctx.ccm); | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 57 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id, | 
|  | 59 | key_buffer, (unsigned int) key_bits)); | 
|  | 60 | if (status != PSA_SUCCESS) { | 
|  | 61 | return status; | 
|  | 62 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 63 | break; | 
|  | 64 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
|  | 65 |  | 
|  | 66 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): | 
| Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 68 | operation->alg = PSA_ALG_GCM; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 69 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. | 
|  | 70 | * The call to mbedtls_gcm_crypt_and_tag or | 
|  | 71 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) { | 
|  | 73 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 74 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 75 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 76 | mbedtls_gcm_init(&operation->ctx.gcm); | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 77 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id, | 
|  | 79 | key_buffer, (unsigned int) key_bits)); | 
|  | 80 | if (status != PSA_SUCCESS) { | 
|  | 81 | return status; | 
|  | 82 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 83 | break; | 
|  | 84 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
|  | 85 |  | 
|  | 86 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): | 
| Paul Elliott | 07a30c4 | 2021-04-20 14:13:23 +0100 | [diff] [blame] | 88 | operation->alg = PSA_ALG_CHACHA20_POLY1305; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 89 | /* We only support the default tag length. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 90 | if (alg != PSA_ALG_CHACHA20_POLY1305) { | 
|  | 91 | return PSA_ERROR_NOT_SUPPORTED; | 
|  | 92 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 93 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 94 | mbedtls_chachapoly_init(&operation->ctx.chachapoly); | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 95 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | mbedtls_chachapoly_setkey(&operation->ctx.chachapoly, | 
|  | 97 | key_buffer)); | 
|  | 98 | if (status != PSA_SUCCESS) { | 
|  | 99 | return status; | 
|  | 100 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 101 | break; | 
|  | 102 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 103 |  | 
|  | 104 | default: | 
| Ronald Cron | 7a55deb | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 105 | (void) status; | 
|  | 106 | (void) key_buffer; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | return PSA_ERROR_NOT_SUPPORTED; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | operation->key_type = psa_get_key_type(attributes); | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 111 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | operation->tag_length = PSA_ALG_AEAD_GET_TAG_LENGTH(alg); | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 113 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | return PSA_SUCCESS; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 | psa_status_t mbedtls_psa_aead_encrypt( | 
|  | 118 | const psa_key_attributes_t *attributes, | 
|  | 119 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 120 | psa_algorithm_t alg, | 
|  | 121 | const uint8_t *nonce, size_t nonce_length, | 
|  | 122 | const uint8_t *additional_data, size_t additional_data_length, | 
|  | 123 | const uint8_t *plaintext, size_t plaintext_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | uint8_t *ciphertext, size_t ciphertext_size, size_t *ciphertext_length) | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 125 | { | 
|  | 126 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 127 | mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 128 | uint8_t *tag; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 129 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | status = psa_aead_setup(&operation, attributes, key_buffer, | 
|  | 131 | key_buffer_size, alg); | 
| Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 134 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 136 |  | 
|  | 137 | /* For all currently supported modes, the tag is at the end of the | 
|  | 138 | * ciphertext. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 139 | if (ciphertext_size < (plaintext_length + operation.tag_length)) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 140 | status = PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 141 | goto exit; | 
|  | 142 | } | 
|  | 143 | tag = ciphertext + plaintext_length; | 
|  | 144 |  | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 145 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | if (operation.alg == PSA_ALG_CCM) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 147 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | mbedtls_ccm_encrypt_and_tag(&operation.ctx.ccm, | 
|  | 149 | plaintext_length, | 
|  | 150 | nonce, nonce_length, | 
|  | 151 | additional_data, | 
|  | 152 | additional_data_length, | 
|  | 153 | plaintext, ciphertext, | 
|  | 154 | tag, operation.tag_length)); | 
|  | 155 | } else | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 156 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
| Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 157 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | if (operation.alg == PSA_ALG_GCM) { | 
| Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 159 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | mbedtls_gcm_crypt_and_tag(&operation.ctx.gcm, | 
|  | 161 | MBEDTLS_GCM_ENCRYPT, | 
|  | 162 | plaintext_length, | 
|  | 163 | nonce, nonce_length, | 
|  | 164 | additional_data, additional_data_length, | 
|  | 165 | plaintext, ciphertext, | 
|  | 166 | operation.tag_length, tag)); | 
|  | 167 | } else | 
| Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 168 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 169 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | if (operation.alg == PSA_ALG_CHACHA20_POLY1305) { | 
|  | 171 | if (operation.tag_length != 16) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 172 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 173 | goto exit; | 
|  | 174 | } | 
|  | 175 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | mbedtls_chachapoly_encrypt_and_tag(&operation.ctx.chachapoly, | 
|  | 177 | plaintext_length, | 
|  | 178 | nonce, | 
|  | 179 | additional_data, | 
|  | 180 | additional_data_length, | 
|  | 181 | plaintext, | 
|  | 182 | ciphertext, | 
|  | 183 | tag)); | 
|  | 184 | } else | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 185 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 186 | { | 
|  | 187 | (void) tag; | 
| Ronald Cron | 7a55deb | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 188 | (void) nonce; | 
|  | 189 | (void) nonce_length; | 
|  | 190 | (void) additional_data; | 
|  | 191 | (void) additional_data_length; | 
|  | 192 | (void) plaintext; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | return PSA_ERROR_NOT_SUPPORTED; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | if (status == PSA_SUCCESS) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 197 | *ciphertext_length = plaintext_length + operation.tag_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 199 |  | 
|  | 200 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | mbedtls_psa_aead_abort(&operation); | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 202 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | return status; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
|  | 206 | /* Locate the tag in a ciphertext buffer containing the encrypted data | 
|  | 207 | * followed by the tag. Return the length of the part preceding the tag in | 
|  | 208 | * *plaintext_length. This is the size of the plaintext in modes where | 
|  | 209 | * the encrypted data has the same size as the plaintext, such as | 
|  | 210 | * CCM and GCM. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length, | 
|  | 212 | const uint8_t *ciphertext, | 
|  | 213 | size_t ciphertext_length, | 
|  | 214 | size_t plaintext_size, | 
|  | 215 | const uint8_t **p_tag) | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 216 | { | 
|  | 217 | size_t payload_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | if (tag_length > ciphertext_length) { | 
|  | 219 | return PSA_ERROR_INVALID_ARGUMENT; | 
|  | 220 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 221 | payload_length = ciphertext_length - tag_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | if (payload_length > plaintext_size) { | 
|  | 223 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 224 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 225 | *p_tag = ciphertext + payload_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | return PSA_SUCCESS; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 227 | } | 
|  | 228 |  | 
|  | 229 | psa_status_t mbedtls_psa_aead_decrypt( | 
|  | 230 | const psa_key_attributes_t *attributes, | 
|  | 231 | const uint8_t *key_buffer, size_t key_buffer_size, | 
|  | 232 | psa_algorithm_t alg, | 
|  | 233 | const uint8_t *nonce, size_t nonce_length, | 
|  | 234 | const uint8_t *additional_data, size_t additional_data_length, | 
|  | 235 | const uint8_t *ciphertext, size_t ciphertext_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | uint8_t *plaintext, size_t plaintext_size, size_t *plaintext_length) | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 237 | { | 
|  | 238 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 239 | mbedtls_psa_aead_operation_t operation = MBEDTLS_PSA_AEAD_OPERATION_INIT; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 240 | const uint8_t *tag = NULL; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 241 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | status = psa_aead_setup(&operation, attributes, key_buffer, | 
|  | 243 | key_buffer_size, alg); | 
| Paul Elliott | cc35859 | 2021-05-12 12:22:28 +0100 | [diff] [blame] | 244 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 246 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 248 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | status = psa_aead_unpadded_locate_tag(operation.tag_length, | 
|  | 250 | ciphertext, ciphertext_length, | 
|  | 251 | plaintext_size, &tag); | 
|  | 252 | if (status != PSA_SUCCESS) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 253 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 255 |  | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 256 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | if (operation.alg == PSA_ALG_CCM) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 258 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | mbedtls_ccm_auth_decrypt(&operation.ctx.ccm, | 
|  | 260 | ciphertext_length - operation.tag_length, | 
|  | 261 | nonce, nonce_length, | 
|  | 262 | additional_data, | 
|  | 263 | additional_data_length, | 
|  | 264 | ciphertext, plaintext, | 
|  | 265 | tag, operation.tag_length)); | 
|  | 266 | } else | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 267 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
| Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 268 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | if (operation.alg == PSA_ALG_GCM) { | 
| Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 270 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | mbedtls_gcm_auth_decrypt(&operation.ctx.gcm, | 
|  | 272 | ciphertext_length - operation.tag_length, | 
|  | 273 | nonce, nonce_length, | 
|  | 274 | additional_data, | 
|  | 275 | additional_data_length, | 
|  | 276 | tag, operation.tag_length, | 
|  | 277 | ciphertext, plaintext)); | 
|  | 278 | } else | 
| Ronald Cron | 810eb16 | 2021-04-06 09:01:39 +0200 | [diff] [blame] | 279 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 280 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | if (operation.alg == PSA_ALG_CHACHA20_POLY1305) { | 
|  | 282 | if (operation.tag_length != 16) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 283 | status = PSA_ERROR_NOT_SUPPORTED; | 
|  | 284 | goto exit; | 
|  | 285 | } | 
|  | 286 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | mbedtls_chachapoly_auth_decrypt(&operation.ctx.chachapoly, | 
|  | 288 | ciphertext_length - operation.tag_length, | 
|  | 289 | nonce, | 
|  | 290 | additional_data, | 
|  | 291 | additional_data_length, | 
|  | 292 | tag, | 
|  | 293 | ciphertext, | 
|  | 294 | plaintext)); | 
|  | 295 | } else | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 296 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 297 | { | 
| Ronald Cron | 7a55deb | 2021-04-28 14:29:00 +0200 | [diff] [blame] | 298 | (void) nonce; | 
|  | 299 | (void) nonce_length; | 
|  | 300 | (void) additional_data; | 
|  | 301 | (void) additional_data_length; | 
|  | 302 | (void) plaintext; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | return PSA_ERROR_NOT_SUPPORTED; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | if (status == PSA_SUCCESS) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 307 | *plaintext_length = ciphertext_length - operation.tag_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 308 | } | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 309 |  | 
|  | 310 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | mbedtls_psa_aead_abort(&operation); | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 312 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | if (status == PSA_SUCCESS) { | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 314 | *plaintext_length = ciphertext_length - operation.tag_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | } | 
|  | 316 | return status; | 
| Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 317 | } | 
| Ronald Cron | 7ceee8d | 2021-03-17 16:55:43 +0100 | [diff] [blame] | 318 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 319 | /* Set the key and algorithm for a multipart authenticated encryption | 
|  | 320 | * operation. */ | 
| Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 321 | psa_status_t mbedtls_psa_aead_encrypt_setup( | 
|  | 322 | mbedtls_psa_aead_operation_t *operation, | 
|  | 323 | const psa_key_attributes_t *attributes, | 
|  | 324 | const uint8_t *key_buffer, | 
|  | 325 | size_t key_buffer_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 326 | psa_algorithm_t alg) | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 327 | { | 
| Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 328 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 329 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | status = psa_aead_setup(operation, attributes, key_buffer, | 
|  | 331 | key_buffer_size, alg); | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 332 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 334 | operation->is_encrypt = 1; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | } | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 336 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | return status; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
|  | 340 | /* Set the key and algorithm for a multipart authenticated decryption | 
|  | 341 | * operation. */ | 
| Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 342 | psa_status_t mbedtls_psa_aead_decrypt_setup( | 
|  | 343 | mbedtls_psa_aead_operation_t *operation, | 
|  | 344 | const psa_key_attributes_t *attributes, | 
|  | 345 | const uint8_t *key_buffer, | 
|  | 346 | size_t key_buffer_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | psa_algorithm_t alg) | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 348 | { | 
| Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 349 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 350 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | status = psa_aead_setup(operation, attributes, key_buffer, | 
|  | 352 | key_buffer_size, alg); | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 353 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 355 | operation->is_encrypt = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | } | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 357 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | return status; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 361 | /* Set a nonce for the multipart AEAD operation*/ | 
| Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 362 | psa_status_t mbedtls_psa_aead_set_nonce( | 
|  | 363 | mbedtls_psa_aead_operation_t *operation, | 
|  | 364 | const uint8_t *nonce, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | size_t nonce_length) | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 366 | { | 
| Paul Elliott | 9e8ccd7 | 2021-05-13 14:30:53 +0100 | [diff] [blame] | 367 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 368 |  | 
| Paul Elliott | bc94978 | 2021-06-03 15:29:00 +0100 | [diff] [blame] | 369 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | if (operation->alg == PSA_ALG_GCM) { | 
| Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 371 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | mbedtls_gcm_starts(&operation->ctx.gcm, | 
|  | 373 | operation->is_encrypt ? | 
|  | 374 | MBEDTLS_GCM_ENCRYPT : MBEDTLS_GCM_DECRYPT, | 
|  | 375 | nonce, | 
|  | 376 | nonce_length)); | 
|  | 377 | } else | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 378 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 379 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 380 | if (operation->alg == PSA_ALG_CCM) { | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 381 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 382 | mbedtls_ccm_starts(&operation->ctx.ccm, | 
|  | 383 | operation->is_encrypt ? | 
|  | 384 | MBEDTLS_CCM_ENCRYPT : MBEDTLS_CCM_DECRYPT, | 
|  | 385 | nonce, | 
|  | 386 | nonce_length)); | 
|  | 387 | } else | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 388 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 389 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { | 
| Paul Elliott | 946c920 | 2021-09-28 14:32:55 +0100 | [diff] [blame] | 391 | /* Note - ChaChaPoly allows an 8 byte nonce, but we would have to | 
|  | 392 | * allocate a buffer in the operation, copy the nonce to it and pad | 
|  | 393 | * it, so for now check the nonce is 12 bytes, as | 
|  | 394 | * mbedtls_chachapoly_starts() assumes it can read 12 bytes from the | 
|  | 395 | * passed in buffer. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | if (nonce_length != 12) { | 
|  | 397 | return PSA_ERROR_INVALID_ARGUMENT; | 
| Paul Elliott | 946c920 | 2021-09-28 14:32:55 +0100 | [diff] [blame] | 398 | } | 
|  | 399 |  | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 400 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | mbedtls_chachapoly_starts(&operation->ctx.chachapoly, | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 402 | nonce, | 
|  | 403 | operation->is_encrypt ? | 
|  | 404 | MBEDTLS_CHACHAPOLY_ENCRYPT : | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | MBEDTLS_CHACHAPOLY_DECRYPT)); | 
|  | 406 | } else | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 407 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 408 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 409 | (void) operation; | 
|  | 410 | (void) nonce; | 
|  | 411 | (void) nonce_length; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 412 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | return PSA_ERROR_NOT_SUPPORTED; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 414 | } | 
|  | 415 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | return status; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 417 | } | 
| Paul Elliott | efda340 | 2021-08-25 17:16:52 +0100 | [diff] [blame] | 418 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | /* Declare the lengths of the message and additional data for AEAD. */ | 
| Paul Elliott | dff6c5d | 2021-09-28 11:00:20 +0100 | [diff] [blame] | 420 | psa_status_t mbedtls_psa_aead_set_lengths( | 
|  | 421 | mbedtls_psa_aead_operation_t *operation, | 
|  | 422 | size_t ad_length, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | size_t plaintext_length) | 
| Paul Elliott | dff6c5d | 2021-09-28 11:00:20 +0100 | [diff] [blame] | 424 | { | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 425 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | if (operation->alg == PSA_ALG_CCM) { | 
|  | 427 | return mbedtls_to_psa_error( | 
|  | 428 | mbedtls_ccm_set_lengths(&operation->ctx.ccm, | 
|  | 429 | ad_length, | 
|  | 430 | plaintext_length, | 
|  | 431 | operation->tag_length)); | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 432 |  | 
|  | 433 | } | 
|  | 434 | #else /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | (void) operation; | 
|  | 436 | (void) ad_length; | 
|  | 437 | (void) plaintext_length; | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 438 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
| Paul Elliott | dff6c5d | 2021-09-28 11:00:20 +0100 | [diff] [blame] | 439 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | return PSA_SUCCESS; | 
| Paul Elliott | dff6c5d | 2021-09-28 11:00:20 +0100 | [diff] [blame] | 441 | } | 
|  | 442 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 443 | /* Pass additional data to an active multipart AEAD operation. */ | 
| Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 444 | psa_status_t mbedtls_psa_aead_update_ad( | 
|  | 445 | mbedtls_psa_aead_operation_t *operation, | 
|  | 446 | const uint8_t *input, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | size_t input_length) | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 448 | { | 
|  | 449 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 450 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 451 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | if (operation->alg == PSA_ALG_GCM) { | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 453 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | mbedtls_gcm_update_ad(&operation->ctx.gcm, input, input_length)); | 
|  | 455 | } else | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 456 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 457 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | if (operation->alg == PSA_ALG_CCM) { | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 459 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 460 | mbedtls_ccm_update_ad(&operation->ctx.ccm, input, input_length)); | 
|  | 461 | } else | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 462 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 463 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 465 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | mbedtls_chachapoly_update_aad(&operation->ctx.chachapoly, | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 467 | input, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | input_length)); | 
|  | 469 | } else | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 470 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 471 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | (void) operation; | 
|  | 473 | (void) input; | 
|  | 474 | (void) input_length; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 475 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | return PSA_ERROR_NOT_SUPPORTED; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 477 | } | 
|  | 478 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | return status; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 480 | } | 
|  | 481 |  | 
|  | 482 | /* Encrypt or decrypt a message fragment in an active multipart AEAD | 
|  | 483 | * operation.*/ | 
| Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 484 | psa_status_t mbedtls_psa_aead_update( | 
|  | 485 | mbedtls_psa_aead_operation_t *operation, | 
|  | 486 | const uint8_t *input, | 
|  | 487 | size_t input_length, | 
|  | 488 | uint8_t *output, | 
|  | 489 | size_t output_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | size_t *output_length) | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 491 | { | 
| Paul Elliott | e2c788d | 2021-05-13 17:16:01 +0100 | [diff] [blame] | 492 | size_t update_output_length; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 493 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 494 |  | 
| Paul Elliott | e2c788d | 2021-05-13 17:16:01 +0100 | [diff] [blame] | 495 | update_output_length = input_length; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 496 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 497 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | if (operation->alg == PSA_ALG_GCM) { | 
| Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 499 | status =  mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | mbedtls_gcm_update(&operation->ctx.gcm, | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 501 | input, input_length, | 
|  | 502 | output, output_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | &update_output_length)); | 
|  | 504 | } else | 
|  | 505 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
|  | 506 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
|  | 507 | if (operation->alg == PSA_ALG_CCM) { | 
|  | 508 | if (output_size < input_length) { | 
|  | 509 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 510 | } | 
| Paul Elliott | ecce901 | 2021-07-23 15:44:11 +0100 | [diff] [blame] | 511 |  | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 512 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | mbedtls_ccm_update(&operation->ctx.ccm, | 
|  | 514 | input, input_length, | 
|  | 515 | output, output_size, | 
|  | 516 | &update_output_length)); | 
|  | 517 | } else | 
|  | 518 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
|  | 519 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
|  | 520 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { | 
|  | 521 | if (output_size < input_length) { | 
|  | 522 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 523 | } | 
|  | 524 |  | 
|  | 525 | status = mbedtls_to_psa_error( | 
|  | 526 | mbedtls_chachapoly_update(&operation->ctx.chachapoly, | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 527 | input_length, | 
|  | 528 | input, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | output)); | 
|  | 530 | } else | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 531 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 532 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | (void) operation; | 
|  | 534 | (void) input; | 
|  | 535 | (void) output; | 
|  | 536 | (void) output_size; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 537 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | return PSA_ERROR_NOT_SUPPORTED; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 539 | } | 
|  | 540 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | e2c788d | 2021-05-13 17:16:01 +0100 | [diff] [blame] | 542 | *output_length = update_output_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | } | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 544 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 545 | return status; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 546 | } | 
|  | 547 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 548 | /* Finish encrypting a message in a multipart AEAD operation. */ | 
| Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 549 | psa_status_t mbedtls_psa_aead_finish( | 
|  | 550 | mbedtls_psa_aead_operation_t *operation, | 
|  | 551 | uint8_t *ciphertext, | 
|  | 552 | size_t ciphertext_size, | 
|  | 553 | size_t *ciphertext_length, | 
|  | 554 | uint8_t *tag, | 
|  | 555 | size_t tag_size, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 556 | size_t *tag_length) | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 557 | { | 
|  | 558 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
| Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 559 | size_t finish_output_size = 0; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 560 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 561 | if (tag_size < operation->tag_length) { | 
|  | 562 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 563 | } | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 564 |  | 
|  | 565 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | if (operation->alg == PSA_ALG_GCM) { | 
| Paul Elliott | 83f09ef | 2021-05-21 19:28:26 +0100 | [diff] [blame] | 567 | status =  mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | mbedtls_gcm_finish(&operation->ctx.gcm, | 
|  | 569 | ciphertext, ciphertext_size, ciphertext_length, | 
|  | 570 | tag, operation->tag_length)); | 
|  | 571 | } else | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 572 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 573 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | if (operation->alg == PSA_ALG_CCM) { | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 575 | /* tag must be big enough to store a tag of size passed into set | 
|  | 576 | * lengths. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | if (tag_size < operation->tag_length) { | 
|  | 578 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 579 | } | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 580 |  | 
|  | 581 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | mbedtls_ccm_finish(&operation->ctx.ccm, | 
|  | 583 | tag, operation->tag_length)); | 
|  | 584 | } else | 
| Paul Elliott | e193ea8 | 2021-10-01 13:00:16 +0100 | [diff] [blame] | 585 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 586 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | if (operation->alg == PSA_ALG_CHACHA20_POLY1305) { | 
| Paul Elliott | ed08cf8 | 2021-07-22 18:48:24 +0100 | [diff] [blame] | 588 | /* Belt and braces. Although the above tag_size check should have | 
|  | 589 | * already done this, if we later start supporting smaller tag sizes | 
|  | 590 | * for chachapoly, then passing a tag buffer smaller than 16 into here | 
|  | 591 | * could cause a buffer overflow, so better safe than sorry. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | if (tag_size < 16) { | 
|  | 593 | return PSA_ERROR_BUFFER_TOO_SMALL; | 
|  | 594 | } | 
| Paul Elliott | ed08cf8 | 2021-07-22 18:48:24 +0100 | [diff] [blame] | 595 |  | 
| Paul Elliott | 2df4005 | 2021-05-07 17:52:18 +0100 | [diff] [blame] | 596 | status = mbedtls_to_psa_error( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 597 | mbedtls_chachapoly_finish(&operation->ctx.chachapoly, | 
|  | 598 | tag)); | 
|  | 599 | } else | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 600 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 601 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | (void) ciphertext; | 
|  | 603 | (void) ciphertext_size; | 
|  | 604 | (void) ciphertext_length; | 
|  | 605 | (void) tag; | 
|  | 606 | (void) tag_size; | 
|  | 607 | (void) tag_length; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 608 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 609 | return PSA_ERROR_NOT_SUPPORTED; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 610 | } | 
|  | 611 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | if (status == PSA_SUCCESS) { | 
| Paul Elliott | 8ff7421 | 2021-09-19 18:39:23 +0100 | [diff] [blame] | 613 | /* This will be zero for all supported algorithms currently, but left | 
|  | 614 | * here for future support. */ | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 615 | *ciphertext_length = finish_output_size; | 
| Paul Elliott | fd3ca24 | 2021-04-25 18:10:42 +0100 | [diff] [blame] | 616 | *tag_length = operation->tag_length; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 617 | } | 
|  | 618 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | return status; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 620 | } | 
|  | 621 |  | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 622 | /* Abort an AEAD operation */ | 
| Paul Elliott | bb8bf66 | 2021-05-19 17:29:42 +0100 | [diff] [blame] | 623 | psa_status_t mbedtls_psa_aead_abort( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | mbedtls_psa_aead_operation_t *operation) | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 625 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | switch (operation->alg) { | 
| Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 627 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) | 
|  | 628 | case PSA_ALG_CCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | mbedtls_ccm_free(&operation->ctx.ccm); | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 630 | break; | 
|  | 631 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ | 
|  | 632 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) | 
|  | 633 | case PSA_ALG_GCM: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 634 | mbedtls_gcm_free(&operation->ctx.gcm); | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 635 | break; | 
|  | 636 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ | 
|  | 637 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) | 
| Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 638 | case PSA_ALG_CHACHA20_POLY1305: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | mbedtls_chachapoly_free(&operation->ctx.chachapoly); | 
| Paul Elliott | 811d8d4 | 2021-04-22 11:31:14 +0100 | [diff] [blame] | 640 | break; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 641 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ | 
|  | 642 | } | 
|  | 643 |  | 
| Paul Elliott | cbbde5f | 2021-05-10 18:19:46 +0100 | [diff] [blame] | 644 | operation->is_encrypt = 0; | 
| Paul Elliott | 1a98aca | 2021-05-20 18:24:07 +0100 | [diff] [blame] | 645 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | return PSA_SUCCESS; | 
| Paul Elliott | adb8b16 | 2021-04-20 16:06:57 +0100 | [diff] [blame] | 647 | } | 
|  | 648 |  | 
| Ronald Cron | 7ceee8d | 2021-03-17 16:55:43 +0100 | [diff] [blame] | 649 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |