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 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 25 | # include "psa_crypto_aead.h" |
| 26 | # include "psa_crypto_core.h" |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 27 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 28 | # include "mbedtls/ccm.h" |
| 29 | # include "mbedtls/chachapoly.h" |
| 30 | # include "mbedtls/cipher.h" |
| 31 | # include "mbedtls/gcm.h" |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 32 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 33 | typedef struct { |
| 34 | union { |
| 35 | unsigned dummy; /* Make the union non-empty even with no supported |
| 36 | algorithms. */ |
| 37 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 38 | mbedtls_ccm_context ccm; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 39 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 40 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 41 | mbedtls_gcm_context gcm; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 42 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 43 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 44 | mbedtls_chachapoly_context chachapoly; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 45 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 46 | } ctx; |
| 47 | psa_algorithm_t core_alg; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 48 | uint8_t tag_length; |
| 49 | } aead_operation_t; |
| 50 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 51 | # define AEAD_OPERATION_INIT \ |
| 52 | { \ |
| 53 | { 0 }, 0, 0 \ |
| 54 | } |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 55 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 56 | static void psa_aead_abort_internal(aead_operation_t *operation) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 57 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 58 | switch (operation->core_alg) { |
| 59 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 60 | case PSA_ALG_CCM: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 61 | mbedtls_ccm_free(&operation->ctx.ccm); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 62 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 63 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 64 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 65 | case PSA_ALG_GCM: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 66 | mbedtls_gcm_free(&operation->ctx.gcm); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 67 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 68 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 69 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
Ronald Cron | b9349a6 | 2021-03-26 13:32:29 +0100 | [diff] [blame] | 70 | case PSA_ALG_CHACHA20_POLY1305: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 71 | mbedtls_chachapoly_free(&operation->ctx.chachapoly); |
Ronald Cron | b9349a6 | 2021-03-26 13:32:29 +0100 | [diff] [blame] | 72 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 73 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 77 | static psa_status_t psa_aead_setup(aead_operation_t *operation, |
| 78 | const psa_key_attributes_t *attributes, |
| 79 | const uint8_t *key_buffer, |
| 80 | psa_algorithm_t alg) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 81 | { |
| 82 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 83 | size_t key_bits; |
Ronald Cron | ecbc068 | 2021-03-26 13:25:17 +0100 | [diff] [blame] | 84 | const mbedtls_cipher_info_t *cipher_info; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 85 | mbedtls_cipher_id_t cipher_id; |
Ronald Cron | ecbc068 | 2021-03-26 13:25:17 +0100 | [diff] [blame] | 86 | size_t full_tag_length = 0; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 87 | |
| 88 | key_bits = attributes->core.bits; |
| 89 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 90 | cipher_info = mbedtls_cipher_info_from_psa(alg, attributes->core.type, |
| 91 | key_bits, &cipher_id); |
| 92 | if (cipher_info == NULL) |
| 93 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 94 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 95 | switch (PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0)) { |
| 96 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 97 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 98 | operation->core_alg = PSA_ALG_CCM; |
Ronald Cron | ecbc068 | 2021-03-26 13:25:17 +0100 | [diff] [blame] | 99 | full_tag_length = 16; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 100 | /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16. |
| 101 | * The call to mbedtls_ccm_encrypt_and_tag or |
| 102 | * mbedtls_ccm_auth_decrypt will validate the tag length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 103 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) |
| 104 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 105 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 106 | mbedtls_ccm_init(&operation->ctx.ccm); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 107 | status = mbedtls_to_psa_error( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 108 | mbedtls_ccm_setkey(&operation->ctx.ccm, cipher_id, key_buffer, |
| 109 | (unsigned int)key_bits)); |
| 110 | if (status != PSA_SUCCESS) |
| 111 | return status; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 112 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 113 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 114 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 115 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 116 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 117 | operation->core_alg = PSA_ALG_GCM; |
Ronald Cron | ecbc068 | 2021-03-26 13:25:17 +0100 | [diff] [blame] | 118 | full_tag_length = 16; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 119 | /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16. |
| 120 | * The call to mbedtls_gcm_crypt_and_tag or |
| 121 | * mbedtls_gcm_auth_decrypt will validate the tag length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 122 | if (PSA_BLOCK_CIPHER_BLOCK_LENGTH(attributes->core.type) != 16) |
| 123 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 124 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 125 | mbedtls_gcm_init(&operation->ctx.gcm); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 126 | status = mbedtls_to_psa_error( |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 127 | mbedtls_gcm_setkey(&operation->ctx.gcm, cipher_id, key_buffer, |
| 128 | (unsigned int)key_bits)); |
| 129 | if (status != PSA_SUCCESS) |
| 130 | return status; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 131 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 132 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 133 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 134 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 135 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 136 | operation->core_alg = PSA_ALG_CHACHA20_POLY1305; |
Ronald Cron | ecbc068 | 2021-03-26 13:25:17 +0100 | [diff] [blame] | 137 | full_tag_length = 16; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 138 | /* We only support the default tag length. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 139 | if (alg != PSA_ALG_CHACHA20_POLY1305) |
| 140 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 141 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 142 | mbedtls_chachapoly_init(&operation->ctx.chachapoly); |
| 143 | status = mbedtls_to_psa_error(mbedtls_chachapoly_setkey( |
| 144 | &operation->ctx.chachapoly, key_buffer)); |
| 145 | if (status != PSA_SUCCESS) |
| 146 | return status; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 147 | break; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 148 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 149 | |
| 150 | default: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 151 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 154 | if (PSA_AEAD_TAG_LENGTH(attributes->core.type, key_bits, alg) > |
| 155 | full_tag_length) |
| 156 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 157 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 158 | operation->tag_length = |
| 159 | PSA_AEAD_TAG_LENGTH(attributes->core.type, key_bits, alg); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 160 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 161 | return PSA_SUCCESS; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 164 | psa_status_t mbedtls_psa_aead_encrypt(const psa_key_attributes_t *attributes, |
| 165 | const uint8_t *key_buffer, |
| 166 | size_t key_buffer_size, |
| 167 | psa_algorithm_t alg, |
| 168 | const uint8_t *nonce, |
| 169 | size_t nonce_length, |
| 170 | const uint8_t *additional_data, |
| 171 | size_t additional_data_length, |
| 172 | const uint8_t *plaintext, |
| 173 | size_t plaintext_length, |
| 174 | uint8_t *ciphertext, |
| 175 | size_t ciphertext_size, |
| 176 | size_t *ciphertext_length) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 177 | { |
| 178 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 179 | aead_operation_t operation = AEAD_OPERATION_INIT; |
| 180 | uint8_t *tag; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 181 | (void)key_buffer_size; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 182 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 183 | status = psa_aead_setup(&operation, attributes, key_buffer, alg); |
| 184 | if (status != PSA_SUCCESS) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 185 | goto exit; |
| 186 | |
| 187 | /* For all currently supported modes, the tag is at the end of the |
| 188 | * ciphertext. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 189 | if (ciphertext_size < (plaintext_length + operation.tag_length)) { |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 190 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
| 191 | goto exit; |
| 192 | } |
| 193 | tag = ciphertext + plaintext_length; |
| 194 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 195 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 196 | if (operation.core_alg == PSA_ALG_CCM) { |
| 197 | status = mbedtls_to_psa_error(mbedtls_ccm_encrypt_and_tag( |
| 198 | &operation.ctx.ccm, plaintext_length, nonce, nonce_length, |
| 199 | additional_data, additional_data_length, plaintext, ciphertext, tag, |
| 200 | operation.tag_length)); |
| 201 | } else |
| 202 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 203 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 204 | if (operation.core_alg == PSA_ALG_GCM) { |
| 205 | status = mbedtls_to_psa_error(mbedtls_gcm_crypt_and_tag( |
| 206 | &operation.ctx.gcm, MBEDTLS_GCM_ENCRYPT, plaintext_length, nonce, |
| 207 | nonce_length, additional_data, additional_data_length, plaintext, |
| 208 | ciphertext, operation.tag_length, tag)); |
| 209 | } else |
| 210 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 211 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 212 | if (operation.core_alg == PSA_ALG_CHACHA20_POLY1305) { |
| 213 | if (nonce_length != 12 || operation.tag_length != 16) { |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 214 | status = PSA_ERROR_NOT_SUPPORTED; |
| 215 | goto exit; |
| 216 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 217 | status = mbedtls_to_psa_error(mbedtls_chachapoly_encrypt_and_tag( |
| 218 | &operation.ctx.chachapoly, plaintext_length, nonce, additional_data, |
| 219 | additional_data_length, plaintext, ciphertext, tag)); |
| 220 | } else |
| 221 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 222 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 223 | (void)tag; |
| 224 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 227 | if (status == PSA_SUCCESS) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 228 | *ciphertext_length = plaintext_length + operation.tag_length; |
| 229 | |
| 230 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 231 | psa_aead_abort_internal(&operation); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 232 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 233 | return status; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* Locate the tag in a ciphertext buffer containing the encrypted data |
| 237 | * followed by the tag. Return the length of the part preceding the tag in |
| 238 | * *plaintext_length. This is the size of the plaintext in modes where |
| 239 | * the encrypted data has the same size as the plaintext, such as |
| 240 | * CCM and GCM. */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 241 | static psa_status_t psa_aead_unpadded_locate_tag(size_t tag_length, |
| 242 | const uint8_t *ciphertext, |
| 243 | size_t ciphertext_length, |
| 244 | size_t plaintext_size, |
| 245 | const uint8_t **p_tag) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 246 | { |
| 247 | size_t payload_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 248 | if (tag_length > ciphertext_length) |
| 249 | return PSA_ERROR_INVALID_ARGUMENT; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 250 | payload_length = ciphertext_length - tag_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 251 | if (payload_length > plaintext_size) |
| 252 | return PSA_ERROR_BUFFER_TOO_SMALL; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 253 | *p_tag = ciphertext + payload_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 254 | return PSA_SUCCESS; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 257 | psa_status_t mbedtls_psa_aead_decrypt(const psa_key_attributes_t *attributes, |
| 258 | const uint8_t *key_buffer, |
| 259 | size_t key_buffer_size, |
| 260 | psa_algorithm_t alg, |
| 261 | const uint8_t *nonce, |
| 262 | size_t nonce_length, |
| 263 | const uint8_t *additional_data, |
| 264 | size_t additional_data_length, |
| 265 | const uint8_t *ciphertext, |
| 266 | size_t ciphertext_length, |
| 267 | uint8_t *plaintext, |
| 268 | size_t plaintext_size, |
| 269 | size_t *plaintext_length) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 270 | { |
| 271 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 272 | aead_operation_t operation = AEAD_OPERATION_INIT; |
| 273 | const uint8_t *tag = NULL; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 274 | (void)key_buffer_size; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 275 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 276 | status = psa_aead_setup(&operation, attributes, key_buffer, alg); |
| 277 | if (status != PSA_SUCCESS) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 278 | goto exit; |
| 279 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 280 | status = psa_aead_unpadded_locate_tag(operation.tag_length, ciphertext, |
| 281 | ciphertext_length, plaintext_size, |
| 282 | &tag); |
| 283 | if (status != PSA_SUCCESS) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 284 | goto exit; |
| 285 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 286 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
| 287 | if (operation.core_alg == PSA_ALG_CCM) { |
| 288 | status = mbedtls_to_psa_error(mbedtls_ccm_auth_decrypt( |
| 289 | &operation.ctx.ccm, ciphertext_length - operation.tag_length, nonce, |
| 290 | nonce_length, additional_data, additional_data_length, ciphertext, |
| 291 | plaintext, tag, operation.tag_length)); |
| 292 | } else |
| 293 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CCM */ |
| 294 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
| 295 | if (operation.core_alg == PSA_ALG_GCM) { |
| 296 | status = mbedtls_to_psa_error(mbedtls_gcm_auth_decrypt( |
| 297 | &operation.ctx.gcm, ciphertext_length - operation.tag_length, nonce, |
| 298 | nonce_length, additional_data, additional_data_length, tag, |
| 299 | operation.tag_length, ciphertext, plaintext)); |
| 300 | } else |
| 301 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_GCM */ |
| 302 | # if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
| 303 | if (operation.core_alg == PSA_ALG_CHACHA20_POLY1305) { |
| 304 | if (nonce_length != 12 || operation.tag_length != 16) { |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 305 | status = PSA_ERROR_NOT_SUPPORTED; |
| 306 | goto exit; |
| 307 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 308 | status = mbedtls_to_psa_error(mbedtls_chachapoly_auth_decrypt( |
| 309 | &operation.ctx.chachapoly, ciphertext_length - operation.tag_length, |
| 310 | nonce, additional_data, additional_data_length, tag, ciphertext, |
| 311 | plaintext)); |
| 312 | } else |
| 313 | # endif /* MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305 */ |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 314 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 315 | return PSA_ERROR_NOT_SUPPORTED; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 316 | } |
| 317 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [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; |
| 320 | |
| 321 | exit: |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 322 | psa_aead_abort_internal(&operation); |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 323 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 324 | if (status == PSA_SUCCESS) |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 325 | *plaintext_length = ciphertext_length - operation.tag_length; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 326 | return status; |
Ronald Cron | 46f9178 | 2021-03-17 08:16:34 +0100 | [diff] [blame] | 327 | } |
Ronald Cron | 7ceee8d | 2021-03-17 16:55:43 +0100 | [diff] [blame] | 328 | |
| 329 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |