|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 TU Dresden |
| 3 | + * Copyright (C) 2021 HAW Hamburg |
| 4 | + * |
| 5 | + * This file is subject to the terms and conditions of the GNU Lesser |
| 6 | + * General Public License v2.1. See the file LICENSE in the top level |
| 7 | + * directory for more details. |
| 8 | + */ |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +/** |
| 13 | + * @ingroup sys_psa_crypto |
| 14 | + * @{ |
| 15 | + * |
| 16 | + * @file |
| 17 | + * @brief AEAD size definitions for the PSA Crypto API |
| 18 | + * |
| 19 | + * @author Armin Wolf <[email protected]> |
| 20 | + * @author Lena Boeckmann <[email protected]> |
| 21 | + * |
| 22 | + */ |
| 23 | + |
| 24 | +#ifdef __cplusplus |
| 25 | +extern "C" { |
| 26 | +#endif |
| 27 | + |
| 28 | +#include "algorithm.h" |
| 29 | + |
| 30 | +/** |
| 31 | + * @brief A sufficient plaintext buffer size for @ref psa_aead_decrypt(), |
| 32 | + * for any of the supported key types and AEAD algorithms. |
| 33 | + * |
| 34 | + * @details If the size of the plaintext buffer is at least this large, |
| 35 | + * it is guaranteed that @ref psa_aead_decrypt() will not fail due |
| 36 | + * to an insufficient buffer size. |
| 37 | + * |
| 38 | + * See also @ref PSA_AEAD_DECRYPT_OUTPUT_SIZE(). |
| 39 | + * |
| 40 | + * @param ciphertext_length Size of the ciphertext in bytes. |
| 41 | + */ |
| 42 | +#define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \ |
| 43 | +/* implementation-defined value */ |
| 44 | + |
| 45 | +/** |
| 46 | + * @brief The length of a tag for an AEAD algorithm, in bytes. |
| 47 | + * |
| 48 | + * @details This is the size of the tag output from @ref psa_aead_finish(). |
| 49 | + * If the size of the tag buffer is at least this large, it is guaranteed that |
| 50 | + * @ref psa_aead_finish() will not fail due to an insufficient tag buffer size. |
| 51 | + * |
| 52 | + * See also @ref PSA_AEAD_TAG_MAX_SIZE. |
| 53 | + * |
| 54 | + * @param key_type The type of the AEAD key. |
| 55 | + * @param key_bits The size of the AEAD key in bits. |
| 56 | + * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such that |
| 57 | + * @ref PSA_ALG_IS_AEAD(@p alg) is true. |
| 58 | + * |
| 59 | + * @return The tag length for the specified algorithm and key. |
| 60 | + * 0 if the AEAD algorithm does not have an identified tag that can be distinguished from |
| 61 | + * the rest of the ciphertext. |
| 62 | + * 0 if the AEAD algorithm is not recognized or not supported. |
| 63 | + */ |
| 64 | +#define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \ |
| 65 | + (PSA_ALG_IS_AEAD(alg) ? \ |
| 66 | + (((alg) & 0x003f0000) >> 16) : \ |
| 67 | + ((void) (key_type), (void) (key_bits), 0)) |
| 68 | + |
| 69 | +/** |
| 70 | + * @brief A sufficient buffer size for storing the tag output by @ref psa_aead_finish(), |
| 71 | + * for any of the supported key types and AEAD algorithms. |
| 72 | + * |
| 73 | + * @details If the size of the tag buffer is at least this large, it is guaranteed that |
| 74 | + * @ref psa_aead_finish() will not fail due to an insufficient buffer size. |
| 75 | + * |
| 76 | + * See also @ref PSA_AEAD_TAG_LENGTH(). |
| 77 | + */ |
| 78 | +#define PSA_AEAD_TAG_MAX_SIZE (16) |
| 79 | + |
| 80 | +/** |
| 81 | + * @brief A sufficient buffer size for storing the tag output by @ref psa_aead_finish(), |
| 82 | + * for AES key types and CCM algorithms. |
| 83 | + * |
| 84 | + * @details If the size of the tag buffer is at least this large, it is guaranteed that |
| 85 | + * @ref psa_aead_finish() will not fail due to an insufficient buffer size. |
| 86 | + * |
| 87 | + * See also @ref PSA_AEAD_TAG_LENGTH(). |
| 88 | + */ |
| 89 | +#define PSA_AES_CCM_TAG_MAX_SIZE (16) |
| 90 | + |
| 91 | +/** |
| 92 | + * @brief A sufficient plaintext buffer size for @ref psa_aead_decrypt(), in bytes. |
| 93 | + * |
| 94 | + * @details If the size of the plaintext buffer is at least this large, it is guaranteed that |
| 95 | + * @ref psa_aead_decrypt() will not fail due to an insufficient buffer size. Depending on |
| 96 | + * the algorithm, the actual size of the plaintext might be smaller. |
| 97 | + * |
| 98 | + * See also @ref PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE. |
| 99 | + * |
| 100 | + * @param key_type A symmetric key type that is compatible with algorithm alg. |
| 101 | + * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t |
| 102 | + * such that @ref PSA_ALG_IS_AEAD(@p alg) is true. |
| 103 | + * @param ciphertext_length Size of the ciphertext in bytes. |
| 104 | + * |
| 105 | + * @return The AEAD plaintext size for the specified key type and algorithm. |
| 106 | + * 0 if the key type or AEAD algorithm is not recognized, not supported or the parameters |
| 107 | + * are incompatible. |
| 108 | + */ |
| 109 | +#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \ |
| 110 | + (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \ |
| 111 | + ((ciphertext_length) > PSA_AEAD_TAG_LENGTH(key_type, 0, alg)) ? \ |
| 112 | + (ciphertext_length) - PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : 0) |
| 113 | + |
| 114 | +/** |
| 115 | + * @brief A sufficient ciphertext buffer size for @ref psa_aead_encrypt(), |
| 116 | + * for any of the supported key types and AEAD algorithms. |
| 117 | + * |
| 118 | + * @details If the size of the ciphertext buffer is at least this large, |
| 119 | + * it is guaranteed that @ref psa_aead_encrypt() will not fail due to an insufficient |
| 120 | + * buffer size. |
| 121 | + * |
| 122 | + * See also @ref PSA_AEAD_ENCRYPT_OUTPUT_SIZE(). |
| 123 | + * |
| 124 | + * @param plaintext_length Size of the plaintext in bytes. |
| 125 | + */ |
| 126 | +#define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \ |
| 127 | + ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE) |
| 128 | + |
| 129 | +/** |
| 130 | + * @brief A sufficient ciphertext buffer size for @ref psa_aead_encrypt(), in bytes. |
| 131 | + * |
| 132 | + * @details If the size of the ciphertext buffer is at least this large, it is guaranteed that |
| 133 | + * @ref psa_aead_encrypt() will not fail due to an insufficient buffer size. Depending on |
| 134 | + * the algorithm, the actual size of the ciphertext might be smaller. |
| 135 | + * |
| 136 | + * See also @ref PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE. |
| 137 | + * |
| 138 | + * @param key_type A symmetric key type that is compatible with algorithm alg. |
| 139 | + * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such |
| 140 | + * that @ref PSA_ALG_IS_AEAD(alg) is true. |
| 141 | + * @param plaintext_length Size of the plaintext in bytes. |
| 142 | + * |
| 143 | + * @return The AEAD ciphertext size for the specified key type and algorithm. |
| 144 | + * 0 if the key type or AEAD algorithm is not recognized, not supported or the parameters |
| 145 | + * are incompatible. |
| 146 | + */ |
| 147 | +#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \ |
| 148 | + (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \ |
| 149 | + (plaintext_length) + PSA_AEAD_TAG_LENGTH(key_type, 0, alg) : 0) |
| 150 | + |
| 151 | +/** |
| 152 | + * @brief A sufficient ciphertext buffer size for @ref psa_aead_finish(), |
| 153 | + * for any of the supported key types and AEAD algorithms. |
| 154 | + * |
| 155 | + * @details If the size of the ciphertext buffer is at least this large, it is guaranteed that |
| 156 | + * @ref psa_aead_finish() will not fail due to an insufficient ciphertext buffer size. |
| 157 | + * |
| 158 | + * See also @ref PSA_AEAD_FINISH_OUTPUT_SIZE(). |
| 159 | + */ |
| 160 | +#define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE /* implementation-defined value */ |
| 161 | + |
| 162 | +/** |
| 163 | + * @brief A sufficient ciphertext buffer size for @ref psa_aead_finish(). |
| 164 | + * |
| 165 | + * @details If the size of the ciphertext buffer is at least this large, it is guaranteed that |
| 166 | + * @ref psa_aead_finish() will not fail due to an insufficient ciphertext buffer size. The |
| 167 | + * actual size of the output might be smaller in any given call. |
| 168 | + * |
| 169 | + * See also @ref PSA_AEAD_FINISH_OUTPUT_MAX_SIZE. |
| 170 | + * |
| 171 | + * @param key_type A symmetric key type that is compatible with algorithm alg. |
| 172 | + * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such that |
| 173 | + * @ref PSA_ALG_IS_AEAD(@p alg) is true. |
| 174 | + * |
| 175 | + * @return A sufficient ciphertext buffer size for the specified key type and algorithm. |
| 176 | + * If the key type or AEAD algorithm is not recognized, or the parameters are incompatible, |
| 177 | + * return 0. An implementation can return either 0 or a correct size for a key type and |
| 178 | + * AEAD algorithm that it recognizes, but does not support. |
| 179 | + */ |
| 180 | +#define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \ |
| 181 | +/* implementation-defined value */ |
| 182 | + |
| 183 | +/** |
| 184 | + * @brief The default nonce size for an AEAD algorithm, in bytes. |
| 185 | + * |
| 186 | + * @details If the size of the nonce buffer is at least this large, it is guaranteed that |
| 187 | + * @ref psa_aead_generate_nonce() will not fail due to an insufficient buffer size. |
| 188 | + * |
| 189 | + * For most AEAD algorithms, @ref PSA_AEAD_NONCE_LENGTH() evaluates to the exact size of |
| 190 | + * the nonce generated by @ref psa_aead_generate_nonce(). |
| 191 | + * |
| 192 | + * See also @ref PSA_AEAD_NONCE_MAX_SIZE. |
| 193 | + * |
| 194 | + * @param key_type A symmetric key type that is compatible with algorithm alg. |
| 195 | + * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such that |
| 196 | + * @ref PSA_ALG_IS_AEAD(@p alg) is true. |
| 197 | + * |
| 198 | + * @return The default nonce size for the specified key type and algorithm. |
| 199 | + * 0 if the key type or AEAD algorithm is not recognized, not supported or the parameters |
| 200 | + * are incompatible. |
| 201 | + */ |
| 202 | +#define PSA_AEAD_NONCE_LENGTH(key_type, alg) \ |
| 203 | + ((PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 && \ |
| 204 | + ((PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM) || \ |
| 205 | + (PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM))) || \ |
| 206 | + (key_type == PSA_KEY_TYPE_CHACHA20 && \ |
| 207 | + PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CHACHA20_POLY1305) ? \ |
| 208 | + 12 : 0) |
| 209 | + |
| 210 | +/** |
| 211 | + * @brief A sufficient buffer size for storing the nonce generated by |
| 212 | + * @ref psa_aead_generate_nonce(), for any of the supported key types and AEAD algorithms. |
| 213 | + * |
| 214 | + * @details If the size of the nonce buffer is at least this large, it is guaranteed that |
| 215 | + * @ref psa_aead_generate_nonce() will not fail due to an insufficient buffer size. |
| 216 | + * |
| 217 | + * See also @ref PSA_AEAD_NONCE_LENGTH(). |
| 218 | + */ |
| 219 | +#define PSA_AEAD_NONCE_MAX_SIZE (13) |
| 220 | + |
| 221 | +/** |
| 222 | + * @brief A sufficient output buffer size for @ref psa_aead_update(), for any of the supported key |
| 223 | + * types and AEAD algorithms. |
| 224 | + * |
| 225 | + * @details If the size of the output buffer is at least this large, it is guaranteed that |
| 226 | + * @ref psa_aead_update() will not fail due to an insufficient buffer size. |
| 227 | + * |
| 228 | + * See also @ref PSA_AEAD_UPDATE_OUTPUT_SIZE(). |
| 229 | + * |
| 230 | + * @param input_length Size of the input in bytes. |
| 231 | + */ |
| 232 | +#define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \ |
| 233 | +/* implementation-defined value */ |
| 234 | + |
| 235 | +/** |
| 236 | + * @brief A sufficient output buffer size for @ref psa_aead_update(). |
| 237 | + * |
| 238 | + * @details If the size of the output buffer is at least this large, it is guaranteed that |
| 239 | + * @ref psa_aead_update() will not fail due to an insufficient buffer size. The actual |
| 240 | + * size of the output might be smaller in any given call. |
| 241 | + * |
| 242 | + * See also @ref PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE. |
| 243 | + * |
| 244 | + * @param key_type A symmetric key type that is compatible with algorithm alg. |
| 245 | + * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such that |
| 246 | + * @ref PSA_ALG_IS_AEAD(@p alg) is true. |
| 247 | + * @param input_length Size of the input in bytes. |
| 248 | + * |
| 249 | + * @return A sufficient output buffer size for the specified key type and algorithm. |
| 250 | + * 0 if the key type or AEAD algorithm is not recognized, not supported or the parameters |
| 251 | + * are incompatible. |
| 252 | + */ |
| 253 | +#define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ |
| 254 | +/* implementation-defined value */ |
| 255 | + |
| 256 | +/** |
| 257 | + * @brief A sufficient output buffer size for @ref psa_aead_update(), for any of the supported key |
| 258 | + * types and AEAD algorithms. |
| 259 | + * |
| 260 | + * @details If the size of the output buffer is at least this large, it is guaranteed that |
| 261 | + * @ref psa_aead_update() will not fail due to an insufficient buffer size. |
| 262 | + * |
| 263 | + * See also @ref PSA_AEAD_UPDATE_OUTPUT_SIZE(). |
| 264 | + * |
| 265 | + * @param input_length Size of the input in bytes. |
| 266 | + */ |
| 267 | +#define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE /* implementation-defined value */ |
| 268 | + |
| 269 | +/** |
| 270 | + * @brief A sufficient plaintext buffer size for @ref psa_aead_verify(), in bytes. |
| 271 | + * |
| 272 | + * @details If the size of the plaintext buffer is at least this large, it is guaranteed that |
| 273 | + * @ref psa_aead_verify() will not fail due to an insufficient plaintext buffer size. The |
| 274 | + * actual size of the output might be smaller in any given call. |
| 275 | + * |
| 276 | + * See also @ref PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE. |
| 277 | + * |
| 278 | + * @param key_type A symmetric key type that is compatible with algorithm alg. |
| 279 | + * @param alg An AEAD algorithm: a value of type @ref psa_algorithm_t such that |
| 280 | + * @ref PSA_ALG_IS_AEAD(@p alg) is true. |
| 281 | + * |
| 282 | + * @return A sufficient plaintext buffer size for the specified key type and algorithm. |
| 283 | + * 0 if the key type or AEAD algorithm is not recognized, not supported or the parameters |
| 284 | + * are incompatible. |
| 285 | + */ |
| 286 | +#define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \ |
| 287 | +/* implementation-defined value */ |
| 288 | + |
| 289 | +#ifdef __cplusplus |
| 290 | +} |
| 291 | +#endif |
| 292 | + |
| 293 | +/** @} */ |
0 commit comments