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