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