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