Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 1 | /* |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | /** |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 8 | * \file psa/crypto_sizes.h |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 9 | * |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 10 | * \brief PSA cryptography module: Mbed TLS buffer size macros |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 11 | * |
| 12 | * \note This file may not be included directly. Applications must |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 13 | * include psa/crypto.h. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 14 | * |
| 15 | * This file contains the definitions of macros that are useful to |
| 16 | * compute buffer sizes. The signatures and semantics of these macros |
| 17 | * are standardized, but the definitions are not, because they depend on |
| 18 | * the available algorithms and, in some cases, on permitted tolerances |
| 19 | * on buffer sizes. |
| 20 | * |
| 21 | * In implementations with isolation between the application and the |
| 22 | * cryptography module, implementers should take care to ensure that |
| 23 | * the definitions that are exposed to applications match what the |
| 24 | * module implements. |
| 25 | * |
| 26 | * Macros that compute sizes whose values do not depend on the |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 27 | * implementation are in crypto.h. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
| 30 | #ifndef PSA_CRYPTO_SIZES_H |
| 31 | #define PSA_CRYPTO_SIZES_H |
| 32 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 33 | #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8) |
| 34 | #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8) |
| 35 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 36 | #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \ |
| 37 | (((length) + (block_size) - 1) / (block_size) * (block_size)) |
| 38 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 39 | /** The size of the output of psa_hash_finish(), in bytes. |
| 40 | * |
| 41 | * This is also the hash size that psa_hash_verify() expects. |
| 42 | * |
| 43 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 44 | * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm |
| 45 | * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a |
| 46 | * hash algorithm). |
| 47 | * |
| 48 | * \return The hash size for the specified hash algorithm. |
| 49 | * If the hash algorithm is not recognized, return 0. |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 50 | */ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 51 | #define PSA_HASH_LENGTH(alg) \ |
| 52 | ( \ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 53 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \ |
| 54 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \ |
| 55 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \ |
| 56 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \ |
| 57 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \ |
| 58 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \ |
| 59 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \ |
| 60 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \ |
| 61 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \ |
| 62 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \ |
| 63 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \ |
| 64 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \ |
| 65 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \ |
| 66 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \ |
| 67 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \ |
| 68 | 0) |
| 69 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 70 | /** \def PSA_HASH_MAX_SIZE |
| 71 | * |
| 72 | * Maximum size of a hash. |
| 73 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 74 | * This macro expands to a compile-time constant integer. This value |
| 75 | * is the maximum size of a hash in bytes. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 76 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 77 | /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226, |
| 78 | * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for |
| 79 | * HMAC-SHA3-512. */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 80 | #define PSA_HASH_MAX_SIZE 64 |
| 81 | #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128 |
| 82 | |
| 83 | /** \def PSA_MAC_MAX_SIZE |
| 84 | * |
| 85 | * Maximum size of a MAC. |
| 86 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 87 | * This macro expands to a compile-time constant integer. This value |
| 88 | * is the maximum size of a MAC in bytes. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 89 | */ |
| 90 | /* All non-HMAC MACs have a maximum size that's smaller than the |
| 91 | * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 92 | /* Note that the encoding of truncated MAC algorithms limits this value |
| 93 | * to 64 bytes. |
| 94 | */ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 95 | #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE |
| 96 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 97 | /** The tag size for an AEAD algorithm, in bytes. |
| 98 | * |
| 99 | * \param alg An AEAD algorithm |
| 100 | * (\c PSA_ALG_XXX value such that |
| 101 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 102 | * |
| 103 | * \return The tag size for the specified algorithm. |
| 104 | * If the AEAD algorithm does not have an identified |
| 105 | * tag that can be distinguished from the rest of |
| 106 | * the ciphertext, return 0. |
| 107 | * If the AEAD algorithm is not recognized, return 0. |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 108 | */ |
| 109 | #define PSA_AEAD_TAG_LENGTH(alg) \ |
| 110 | (PSA_ALG_IS_AEAD(alg) ? \ |
| 111 | (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \ |
| 112 | 0) |
| 113 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 114 | /** The maximum tag size for all supported AEAD algorithms, in bytes. |
| 115 | * |
| 116 | * See also #PSA_AEAD_TAG_LENGTH(\p alg). |
| 117 | */ |
| 118 | #define PSA_AEAD_TAG_MAX_SIZE 16 |
| 119 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 120 | /* The maximum size of an RSA key on this implementation, in bits. |
| 121 | * This is a vendor-specific macro. |
| 122 | * |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 123 | * Mbed TLS does not set a hard limit on the size of RSA keys: any key |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 124 | * whose parameters fit in a bignum is accepted. However large keys can |
| 125 | * induce a large memory usage and long computation times. Unlike other |
| 126 | * auxiliary macros in this file and in crypto.h, which reflect how the |
| 127 | * library is configured, this macro defines how the library is |
| 128 | * configured. This implementation refuses to import or generate an |
| 129 | * RSA key whose size is larger than the value defined here. |
| 130 | * |
| 131 | * Note that an implementation may set different size limits for different |
| 132 | * operations, and does not need to accept all key sizes up to the limit. */ |
| 133 | #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096 |
| 134 | |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 135 | /* The maximum size of an ECC key on this implementation, in bits */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 136 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521 |
| 137 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 138 | /** This macro returns the maximum supported length of the PSK for the |
| 139 | * TLS-1.2 PSK-to-MS key derivation |
| 140 | * (#PSA_ALG_TLS12_PSK_TO_MS(\p hash_alg)). |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 141 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 142 | * The maximum supported length does not depend on the chosen hash algorithm. |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 143 | * |
| 144 | * Quoting RFC 4279, Sect 5.3: |
| 145 | * TLS implementations supporting these ciphersuites MUST support |
| 146 | * arbitrary PSK identities up to 128 octets in length, and arbitrary |
| 147 | * PSKs up to 64 octets in length. Supporting longer identities and |
| 148 | * keys is RECOMMENDED. |
| 149 | * |
| 150 | * Therefore, no implementation should define a value smaller than 64 |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 151 | * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE. |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 152 | */ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 153 | #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128 |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 154 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 155 | /** The maximum size of a block cipher. */ |
| 156 | #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16 |
| 157 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 158 | |
| 159 | /** The size of the output of psa_mac_sign_finish(), in bytes. |
| 160 | * |
| 161 | * This is also the MAC size that psa_mac_verify_finish() expects. |
| 162 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 163 | * \warning This macro may evaluate its arguments multiple times or |
| 164 | * zero times, so you should not pass arguments that contain |
| 165 | * side effects. |
| 166 | * |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 167 | * \param key_type The type of the MAC key. |
| 168 | * \param key_bits The size of the MAC key in bits. |
| 169 | * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 170 | * #PSA_ALG_IS_MAC(\p alg) is true). |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 171 | * |
| 172 | * \return The MAC size for the specified algorithm with |
| 173 | * the specified key parameters. |
| 174 | * \return 0 if the MAC algorithm is not recognized. |
| 175 | * \return Either 0 or the correct size for a MAC algorithm that |
| 176 | * the implementation recognizes, but does not support. |
| 177 | * \return Unspecified if the key parameters are not consistent |
| 178 | * with the algorithm. |
| 179 | */ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 180 | #define PSA_MAC_LENGTH(key_type, key_bits, alg) \ |
| 181 | ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \ |
| 182 | PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \ |
| 183 | PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 184 | ((void)(key_type), (void)(key_bits), 0)) |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 185 | |
| 186 | /** The maximum size of the output of psa_aead_encrypt(), in bytes. |
| 187 | * |
| 188 | * If the size of the ciphertext buffer is at least this large, it is |
| 189 | * guaranteed that psa_aead_encrypt() will not fail due to an |
| 190 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 191 | * the ciphertext may be smaller. |
| 192 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 193 | * \warning This macro may evaluate its arguments multiple times or |
| 194 | * zero times, so you should not pass arguments that contain |
| 195 | * side effects. |
| 196 | * |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 197 | * \param alg An AEAD algorithm |
| 198 | * (\c PSA_ALG_XXX value such that |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 199 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 200 | * \param plaintext_length Size of the plaintext in bytes. |
| 201 | * |
| 202 | * \return The AEAD ciphertext size for the specified |
| 203 | * algorithm. |
| 204 | * If the AEAD algorithm is not recognized, return 0. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 205 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 206 | #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \ |
| 207 | (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \ |
| 208 | (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 209 | 0) |
| 210 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 211 | /** A sufficient output buffer size for psa_aead_encrypt(), for any of the |
| 212 | * supported key types and AEAD algorithms. |
| 213 | * |
| 214 | * If the size of the ciphertext buffer is at least this large, it is guaranteed |
| 215 | * that psa_aead_encrypt() will not fail due to an insufficient buffer size. |
| 216 | * |
| 217 | * \note This macro returns a compile-time constant if its arguments are |
| 218 | * compile-time constants. |
| 219 | * |
| 220 | * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p alg, \p plaintext_length). |
| 221 | * |
| 222 | * \param plaintext_length Size of the plaintext in bytes. |
| 223 | * |
| 224 | * \return A sufficient output buffer size for any of the |
| 225 | * supported key types and AEAD algorithms. |
| 226 | * |
| 227 | */ |
| 228 | #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \ |
| 229 | ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE) |
| 230 | |
| 231 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 232 | /** The maximum size of the output of psa_aead_decrypt(), in bytes. |
| 233 | * |
| 234 | * If the size of the plaintext buffer is at least this large, it is |
| 235 | * guaranteed that psa_aead_decrypt() will not fail due to an |
| 236 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 237 | * the plaintext may be smaller. |
| 238 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 239 | * \warning This macro may evaluate its arguments multiple times or |
| 240 | * zero times, so you should not pass arguments that contain |
| 241 | * side effects. |
| 242 | * |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 243 | * \param alg An AEAD algorithm |
| 244 | * (\c PSA_ALG_XXX value such that |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 245 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 246 | * \param ciphertext_length Size of the plaintext in bytes. |
| 247 | * |
| 248 | * \return The AEAD ciphertext size for the specified |
| 249 | * algorithm. |
| 250 | * If the AEAD algorithm is not recognized, return 0. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 251 | */ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 252 | #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \ |
| 253 | (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 254 | (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \ |
| 255 | 0) |
| 256 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 257 | /** A sufficient output buffer size for psa_aead_decrypt(), for any of the |
| 258 | * supported key types and AEAD algorithms. |
| 259 | * |
| 260 | * If the size of the plaintext buffer is at least this large, it is guaranteed |
| 261 | * that psa_aead_decrypt() will not fail due to an insufficient buffer size. |
| 262 | * |
| 263 | * \note This macro returns a compile-time constant if its arguments are |
| 264 | * compile-time constants. |
| 265 | * |
| 266 | * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p alg, \p ciphertext_length). |
| 267 | * |
| 268 | * \param ciphertext_length Size of the ciphertext in bytes. |
| 269 | * |
| 270 | * \return A sufficient output buffer size for any of the |
| 271 | * supported key types and AEAD algorithms. |
| 272 | * |
| 273 | */ |
| 274 | #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \ |
| 275 | (ciphertext_length) |
| 276 | |
| 277 | /** The default nonce size for an AEAD algorithm, in bytes. |
| 278 | * |
| 279 | * This macro can be used to allocate a buffer of sufficient size to |
| 280 | * store the nonce output from #psa_aead_generate_nonce(). |
| 281 | * |
| 282 | * See also #PSA_AEAD_NONCE_MAX_SIZE. |
| 283 | * |
| 284 | * \note This is not the maximum size of nonce supported as input to |
| 285 | * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), |
| 286 | * just the default size that is generated by #psa_aead_generate_nonce(). |
| 287 | * |
| 288 | * \warning This macro may evaluate its arguments multiple times or |
| 289 | * zero times, so you should not pass arguments that contain |
| 290 | * side effects. |
| 291 | * |
| 292 | * \param key_type A symmetric key type that is compatible with |
| 293 | * algorithm \p alg. |
| 294 | * |
| 295 | * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that |
| 296 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 297 | * |
| 298 | * \return The default nonce size for the specified key type and algorithm. |
| 299 | * If the key type or AEAD algorithm is not recognized, |
| 300 | * or the parameters are incompatible, return 0. |
| 301 | */ |
| 302 | #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \ |
| 303 | (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 && \ |
| 304 | (PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM || \ |
| 305 | PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_GCM) ? 12 : \ |
| 306 | (key_type) == PSA_KEY_TYPE_CHACHA20 && \ |
| 307 | PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CHACHA20_POLY1305 ? 12 : \ |
| 308 | 0) |
| 309 | |
| 310 | /** The maximum default nonce size among all supported pairs of key types and |
| 311 | * AEAD algorithms, in bytes. |
| 312 | * |
| 313 | * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH() |
| 314 | * may return. |
| 315 | * |
| 316 | * \note This is not the maximum size of nonce supported as input to |
| 317 | * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(), |
| 318 | * just the largest size that may be generated by |
| 319 | * #psa_aead_generate_nonce(). |
| 320 | */ |
| 321 | #define PSA_AEAD_NONCE_MAX_SIZE 12 |
| 322 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 323 | /** A sufficient output buffer size for psa_aead_update(). |
| 324 | * |
| 325 | * If the size of the output buffer is at least this large, it is |
| 326 | * guaranteed that psa_aead_update() will not fail due to an |
| 327 | * insufficient buffer size. The actual size of the output may be smaller |
| 328 | * in any given call. |
| 329 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 330 | * \warning This macro may evaluate its arguments multiple times or |
| 331 | * zero times, so you should not pass arguments that contain |
| 332 | * side effects. |
| 333 | * |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 334 | * \param alg An AEAD algorithm |
| 335 | * (\c PSA_ALG_XXX value such that |
| 336 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 337 | * \param input_length Size of the input in bytes. |
| 338 | * |
| 339 | * \return A sufficient output buffer size for the specified |
| 340 | * algorithm. |
| 341 | * If the AEAD algorithm is not recognized, return 0. |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 342 | */ |
| 343 | /* For all the AEAD modes defined in this specification, it is possible |
| 344 | * to emit output without delay. However, hardware may not always be |
| 345 | * capable of this. So for modes based on a block cipher, allow the |
| 346 | * implementation to delay the output until it has a full block. */ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 347 | #define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \ |
| 348 | (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ |
| 349 | PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)) : \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 350 | (input_length)) |
| 351 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 352 | /** A sufficient output buffer size for psa_aead_update(), for any of the |
| 353 | * supported key types and AEAD algorithms. |
| 354 | * |
| 355 | * If the size of the output buffer is at least this large, it is guaranteed |
| 356 | * that psa_aead_update() will not fail due to an insufficient buffer size. |
| 357 | * |
| 358 | * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p alg, \p input_length). |
| 359 | * |
| 360 | * \param input_length Size of the input in bytes. |
| 361 | */ |
| 362 | #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \ |
| 363 | (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length))) |
| 364 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 365 | /** A sufficient ciphertext buffer size for psa_aead_finish(). |
| 366 | * |
| 367 | * If the size of the ciphertext buffer is at least this large, it is |
| 368 | * guaranteed that psa_aead_finish() will not fail due to an |
| 369 | * insufficient ciphertext buffer size. The actual size of the output may |
| 370 | * be smaller in any given call. |
| 371 | * |
| 372 | * \param alg An AEAD algorithm |
| 373 | * (\c PSA_ALG_XXX value such that |
| 374 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 375 | * |
| 376 | * \return A sufficient ciphertext buffer size for the |
| 377 | * specified algorithm. |
| 378 | * If the AEAD algorithm is not recognized, return 0. |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 379 | */ |
| 380 | #define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \ |
| 381 | (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 382 | PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 383 | 0) |
| 384 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 385 | /** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the |
| 386 | * supported key types and AEAD algorithms. |
| 387 | * |
| 388 | * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p alg). |
| 389 | */ |
| 390 | #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) |
| 391 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 392 | /** A sufficient plaintext buffer size for psa_aead_verify(). |
| 393 | * |
| 394 | * If the size of the plaintext buffer is at least this large, it is |
| 395 | * guaranteed that psa_aead_verify() will not fail due to an |
| 396 | * insufficient plaintext buffer size. The actual size of the output may |
| 397 | * be smaller in any given call. |
| 398 | * |
| 399 | * \param alg An AEAD algorithm |
| 400 | * (\c PSA_ALG_XXX value such that |
| 401 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 402 | * |
| 403 | * \return A sufficient plaintext buffer size for the |
| 404 | * specified algorithm. |
| 405 | * If the AEAD algorithm is not recognized, return 0. |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 406 | */ |
| 407 | #define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \ |
| 408 | (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 409 | PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 410 | 0) |
| 411 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 412 | /** A sufficient plaintext buffer size for psa_aead_verify(), for any of the |
| 413 | * supported key types and AEAD algorithms. |
| 414 | * |
| 415 | * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p alg). |
| 416 | */ |
| 417 | #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) |
| 418 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 419 | #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ |
| 420 | (PSA_ALG_IS_RSA_OAEP(alg) ? \ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 421 | 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 422 | 11 /*PKCS#1v1.5*/) |
| 423 | |
| 424 | /** |
| 425 | * \brief ECDSA signature size for a given curve bit size |
| 426 | * |
| 427 | * \param curve_bits Curve size in bits. |
| 428 | * \return Signature size in bytes. |
| 429 | * |
| 430 | * \note This macro returns a compile-time constant if its argument is one. |
| 431 | */ |
| 432 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 433 | (PSA_BITS_TO_BYTES(curve_bits) * 2) |
| 434 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 435 | /** Sufficient signature buffer size for psa_sign_hash(). |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 436 | * |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 437 | * This macro returns a sufficient buffer size for a signature using a key |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 438 | * of the specified type and size, with the specified algorithm. |
| 439 | * Note that the actual size of the signature may be smaller |
| 440 | * (some algorithms produce a variable-size signature). |
| 441 | * |
| 442 | * \warning This function may call its arguments multiple times or |
| 443 | * zero times, so you should not pass arguments that contain |
| 444 | * side effects. |
| 445 | * |
| 446 | * \param key_type An asymmetric key type (this may indifferently be a |
| 447 | * key pair type or a public key type). |
| 448 | * \param key_bits The size of the key in bits. |
| 449 | * \param alg The signature algorithm. |
| 450 | * |
| 451 | * \return If the parameters are valid and supported, return |
| 452 | * a buffer size in bytes that guarantees that |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 453 | * psa_sign_hash() will not fail with |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 454 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 455 | * If the parameters are a valid combination that is not supported, |
| 456 | * return either a sensible size or 0. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 457 | * If the parameters are not valid, the |
| 458 | * return value is unspecified. |
| 459 | */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 460 | #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 461 | (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 462 | PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ |
| 463 | ((void)alg, 0)) |
| 464 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 465 | #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \ |
| 466 | PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) |
| 467 | |
| 468 | /** \def PSA_SIGNATURE_MAX_SIZE |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 469 | * |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 470 | * Maximum size of an asymmetric signature. |
| 471 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 472 | * This macro expands to a compile-time constant integer. This value |
| 473 | * is the maximum size of a signature in bytes. |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 474 | */ |
| 475 | #define PSA_SIGNATURE_MAX_SIZE \ |
| 476 | (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \ |
| 477 | PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \ |
| 478 | PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE) |
| 479 | |
| 480 | /** Sufficient output buffer size for psa_asymmetric_encrypt(). |
| 481 | * |
| 482 | * This macro returns a sufficient buffer size for a ciphertext produced using |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 483 | * a key of the specified type and size, with the specified algorithm. |
| 484 | * Note that the actual size of the ciphertext may be smaller, depending |
| 485 | * on the algorithm. |
| 486 | * |
| 487 | * \warning This function may call its arguments multiple times or |
| 488 | * zero times, so you should not pass arguments that contain |
| 489 | * side effects. |
| 490 | * |
| 491 | * \param key_type An asymmetric key type (this may indifferently be a |
| 492 | * key pair type or a public key type). |
| 493 | * \param key_bits The size of the key in bits. |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 494 | * \param alg The asymmetric encryption algorithm. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 495 | * |
| 496 | * \return If the parameters are valid and supported, return |
| 497 | * a buffer size in bytes that guarantees that |
| 498 | * psa_asymmetric_encrypt() will not fail with |
| 499 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 500 | * If the parameters are a valid combination that is not supported, |
| 501 | * return either a sensible size or 0. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 502 | * If the parameters are not valid, the |
| 503 | * return value is unspecified. |
| 504 | */ |
| 505 | #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 506 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 507 | ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 508 | 0) |
| 509 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 510 | /** A sufficient output buffer size for psa_asymmetric_encrypt(), for any |
| 511 | * supported asymmetric encryption. |
| 512 | * |
| 513 | * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). |
| 514 | */ |
| 515 | /* This macro assumes that RSA is the only supported asymmetric encryption. */ |
| 516 | #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \ |
| 517 | (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) |
| 518 | |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 519 | /** Sufficient output buffer size for psa_asymmetric_decrypt(). |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 520 | * |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 521 | * This macro returns a sufficient buffer size for a plaintext produced using |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 522 | * a key of the specified type and size, with the specified algorithm. |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 523 | * Note that the actual size of the plaintext may be smaller, depending |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 524 | * on the algorithm. |
| 525 | * |
| 526 | * \warning This function may call its arguments multiple times or |
| 527 | * zero times, so you should not pass arguments that contain |
| 528 | * side effects. |
| 529 | * |
| 530 | * \param key_type An asymmetric key type (this may indifferently be a |
| 531 | * key pair type or a public key type). |
| 532 | * \param key_bits The size of the key in bits. |
Soby Mathew | 07ef6e4 | 2020-07-20 21:09:23 +0100 | [diff] [blame] | 533 | * \param alg The asymmetric encryption algorithm. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 534 | * |
| 535 | * \return If the parameters are valid and supported, return |
| 536 | * a buffer size in bytes that guarantees that |
| 537 | * psa_asymmetric_decrypt() will not fail with |
| 538 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 539 | * If the parameters are a valid combination that is not supported, |
| 540 | * return either a sensible size or 0. |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 541 | * If the parameters are not valid, the |
| 542 | * return value is unspecified. |
| 543 | */ |
| 544 | #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 545 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 546 | PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ |
| 547 | 0) |
| 548 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 549 | /** A sufficient output buffer size for psa_asymmetric_decrypt(), for any |
| 550 | * supported asymmetric decryption. |
| 551 | * |
| 552 | * This macro assumes that RSA is the only supported asymmetric encryption. |
| 553 | * |
| 554 | * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg). |
| 555 | */ |
| 556 | #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \ |
| 557 | (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS)) |
| 558 | |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 559 | /* Maximum size of the ASN.1 encoding of an INTEGER with the specified |
| 560 | * number of bits. |
| 561 | * |
| 562 | * This definition assumes that bits <= 2^19 - 9 so that the length field |
| 563 | * is at most 3 bytes. The length of the encoding is the length of the |
| 564 | * bit string padded to a whole number of bytes plus: |
| 565 | * - 1 type byte; |
| 566 | * - 1 to 3 length bytes; |
| 567 | * - 0 to 1 bytes of leading 0 due to the sign bit. |
| 568 | */ |
| 569 | #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \ |
| 570 | ((bits) / 8 + 5) |
| 571 | |
| 572 | /* Maximum size of the export encoding of an RSA public key. |
| 573 | * Assumes that the public exponent is less than 2^32. |
| 574 | * |
| 575 | * RSAPublicKey ::= SEQUENCE { |
| 576 | * modulus INTEGER, -- n |
| 577 | * publicExponent INTEGER } -- e |
| 578 | * |
| 579 | * - 4 bytes of SEQUENCE overhead; |
| 580 | * - n : INTEGER; |
| 581 | * - 7 bytes for the public exponent. |
| 582 | */ |
| 583 | #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
| 584 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11) |
| 585 | |
| 586 | /* Maximum size of the export encoding of an RSA key pair. |
| 587 | * Assumes thatthe public exponent is less than 2^32 and that the size |
| 588 | * difference between the two primes is at most 1 bit. |
| 589 | * |
| 590 | * RSAPrivateKey ::= SEQUENCE { |
| 591 | * version Version, -- 0 |
| 592 | * modulus INTEGER, -- N-bit |
| 593 | * publicExponent INTEGER, -- 32-bit |
| 594 | * privateExponent INTEGER, -- N-bit |
| 595 | * prime1 INTEGER, -- N/2-bit |
| 596 | * prime2 INTEGER, -- N/2-bit |
| 597 | * exponent1 INTEGER, -- N/2-bit |
| 598 | * exponent2 INTEGER, -- N/2-bit |
| 599 | * coefficient INTEGER, -- N/2-bit |
| 600 | * } |
| 601 | * |
| 602 | * - 4 bytes of SEQUENCE overhead; |
| 603 | * - 3 bytes of version; |
| 604 | * - 7 half-size INTEGERs plus 2 full-size INTEGERs, |
| 605 | * overapproximated as 9 half-size INTEGERS; |
| 606 | * - 7 bytes for the public exponent. |
| 607 | */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 608 | #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 609 | (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14) |
| 610 | |
| 611 | /* Maximum size of the export encoding of a DSA public key. |
| 612 | * |
| 613 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 614 | * algorithm AlgorithmIdentifier, |
| 615 | * subjectPublicKey BIT STRING } -- contains DSAPublicKey |
| 616 | * AlgorithmIdentifier ::= SEQUENCE { |
| 617 | * algorithm OBJECT IDENTIFIER, |
| 618 | * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs |
| 619 | * DSAPublicKey ::= INTEGER -- public key, Y |
| 620 | * |
| 621 | * - 3 * 4 bytes of SEQUENCE overhead; |
| 622 | * - 1 + 1 + 7 bytes of algorithm (DSA OID); |
| 623 | * - 4 bytes of BIT STRING overhead; |
| 624 | * - 3 full-size INTEGERs (p, g, y); |
| 625 | * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits). |
| 626 | */ |
| 627 | #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
| 628 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59) |
| 629 | |
| 630 | /* Maximum size of the export encoding of a DSA key pair. |
| 631 | * |
| 632 | * DSAPrivateKey ::= SEQUENCE { |
| 633 | * version Version, -- 0 |
| 634 | * prime INTEGER, -- p |
| 635 | * subprime INTEGER, -- q |
| 636 | * generator INTEGER, -- g |
| 637 | * public INTEGER, -- y |
| 638 | * private INTEGER, -- x |
| 639 | * } |
| 640 | * |
| 641 | * - 4 bytes of SEQUENCE overhead; |
| 642 | * - 3 bytes of version; |
| 643 | * - 3 full-size INTEGERs (p, g, y); |
| 644 | * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits). |
| 645 | */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 646 | #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 647 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75) |
| 648 | |
| 649 | /* Maximum size of the export encoding of an ECC public key. |
| 650 | * |
| 651 | * The representation of an ECC public key is: |
| 652 | * - The byte 0x04; |
| 653 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 654 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 655 | * - where m is the bit size associated with the curve. |
| 656 | * |
| 657 | * - 1 byte + 2 * point size. |
| 658 | */ |
| 659 | #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
| 660 | (2 * PSA_BITS_TO_BYTES(key_bits) + 1) |
| 661 | |
| 662 | /* Maximum size of the export encoding of an ECC key pair. |
| 663 | * |
| 664 | * An ECC key pair is represented by the secret value. |
| 665 | */ |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 666 | #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 667 | (PSA_BITS_TO_BYTES(key_bits)) |
| 668 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 669 | /** Sufficient output buffer size for psa_export_key() or |
| 670 | * psa_export_public_key(). |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 671 | * |
| 672 | * This macro returns a compile-time constant if its arguments are |
| 673 | * compile-time constants. |
| 674 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 675 | * \warning This macro may evaluate its arguments multiple times or |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 676 | * zero times, so you should not pass arguments that contain |
| 677 | * side effects. |
| 678 | * |
| 679 | * The following code illustrates how to allocate enough memory to export |
| 680 | * a key by querying the key type and size at runtime. |
| 681 | * \code{c} |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 682 | * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 683 | * psa_status_t status; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 684 | * status = psa_get_key_attributes(key, &attributes); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 685 | * if (status != PSA_SUCCESS) handle_error(...); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 686 | * psa_key_type_t key_type = psa_get_key_type(&attributes); |
| 687 | * size_t key_bits = psa_get_key_bits(&attributes); |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 688 | * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 689 | * psa_reset_key_attributes(&attributes); |
| 690 | * uint8_t *buffer = malloc(buffer_size); |
| 691 | * if (buffer == NULL) handle_error(...); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 692 | * size_t buffer_length; |
| 693 | * status = psa_export_key(key, buffer, buffer_size, &buffer_length); |
| 694 | * if (status != PSA_SUCCESS) handle_error(...); |
| 695 | * \endcode |
| 696 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 697 | * \param key_type A supported key type. |
| 698 | * \param key_bits The size of the key in bits. |
| 699 | * |
| 700 | * \return If the parameters are valid and supported, return |
| 701 | * a buffer size in bytes that guarantees that |
| 702 | * psa_export_key() or psa_export_public_key() will not fail with |
| 703 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 704 | * If the parameters are a valid combination that is not supported, |
| 705 | * return either a sensible size or 0. |
| 706 | * If the parameters are not valid, the return value is unspecified. |
| 707 | */ |
| 708 | #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \ |
| 709 | (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ |
| 710 | (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \ |
| 711 | (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
| 712 | (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \ |
| 713 | (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
| 714 | PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \ |
| 715 | PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
| 716 | 0) |
| 717 | |
| 718 | /** Sufficient output buffer size for psa_export_public_key(). |
| 719 | * |
| 720 | * This macro returns a compile-time constant if its arguments are |
| 721 | * compile-time constants. |
| 722 | * |
| 723 | * \warning This macro may evaluate its arguments multiple times or |
| 724 | * zero times, so you should not pass arguments that contain |
| 725 | * side effects. |
| 726 | * |
| 727 | * The following code illustrates how to allocate enough memory to export |
| 728 | * a public key by querying the key type and size at runtime. |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 729 | * \code{c} |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 730 | * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 731 | * psa_status_t status; |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 732 | * status = psa_get_key_attributes(key, &attributes); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 733 | * if (status != PSA_SUCCESS) handle_error(...); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 734 | * psa_key_type_t key_type = psa_get_key_type(&attributes); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 735 | * size_t key_bits = psa_get_key_bits(&attributes); |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 736 | * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits); |
Antonio de Angelis | 04debbd | 2019-10-14 12:12:52 +0100 | [diff] [blame] | 737 | * psa_reset_key_attributes(&attributes); |
| 738 | * uint8_t *buffer = malloc(buffer_size); |
| 739 | * if (buffer == NULL) handle_error(...); |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 740 | * size_t buffer_length; |
| 741 | * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length); |
| 742 | * if (status != PSA_SUCCESS) handle_error(...); |
| 743 | * \endcode |
| 744 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 745 | * \param key_type A public key or key pair key type. |
| 746 | * \param key_bits The size of the key in bits. |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 747 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 748 | * \return If the parameters are valid and supported, return |
| 749 | * a buffer size in bytes that guarantees that |
| 750 | * psa_export_public_key() will not fail with |
| 751 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 752 | * If the parameters are a valid combination that is not |
| 753 | * supported, return either a sensible size or 0. |
| 754 | * If the parameters are not valid, |
| 755 | * the return value is unspecified. |
| 756 | * |
| 757 | * If the parameters are valid and supported, |
| 758 | * return the same result as |
| 759 | * #PSA_EXPORT_KEY_OUTPUT_SIZE( |
| 760 | * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type), |
| 761 | * \p key_bits). |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 762 | */ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 763 | #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \ |
| 764 | (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
| 765 | PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
Jamie Fox | 0e54ebc | 2019-04-09 14:21:04 +0100 | [diff] [blame] | 766 | 0) |
| 767 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 768 | /** Sufficient buffer size for exporting any asymmetric key pair. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 769 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 770 | * This macro expands to a compile-time constant integer. This value is |
| 771 | * a sufficient buffer size when calling psa_export_key() to export any |
| 772 | * asymmetric key pair, regardless of the exact key type and key size. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 773 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 774 | * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). |
| 775 | */ |
| 776 | #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \ |
| 777 | (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ |
| 778 | PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \ |
| 779 | PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \ |
| 780 | PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)) |
| 781 | |
| 782 | /** Sufficient buffer size for exporting any asymmetric public key. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 783 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 784 | * This macro expands to a compile-time constant integer. This value is |
| 785 | * a sufficient buffer size when calling psa_export_key() or |
| 786 | * psa_export_public_key() to export any asymmetric public key, |
| 787 | * regardless of the exact key type and key size. |
| 788 | * |
| 789 | * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits). |
| 790 | */ |
| 791 | #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \ |
| 792 | (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \ |
| 793 | PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \ |
| 794 | PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \ |
| 795 | PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)) |
| 796 | |
| 797 | /** Sufficient output buffer size for psa_raw_key_agreement(). |
| 798 | * |
| 799 | * This macro returns a compile-time constant if its arguments are |
| 800 | * compile-time constants. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 801 | * |
| 802 | * \warning This macro may evaluate its arguments multiple times or |
| 803 | * zero times, so you should not pass arguments that contain |
| 804 | * side effects. |
| 805 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 806 | * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 807 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 808 | * \param key_type A supported key type. |
| 809 | * \param key_bits The size of the key in bits. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 810 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 811 | * \return If the parameters are valid and supported, return |
| 812 | * a buffer size in bytes that guarantees that |
| 813 | * psa_raw_key_agreement() will not fail with |
| 814 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 815 | * If the parameters are a valid combination that |
| 816 | * is not supported, return either a sensible size or 0. |
| 817 | * If the parameters are not valid, |
| 818 | * the return value is unspecified. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 819 | */ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 820 | /* FFDH is not yet supported in PSA. */ |
| 821 | #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \ |
| 822 | (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \ |
| 823 | PSA_BITS_TO_BYTES(key_bits) : \ |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 824 | 0) |
| 825 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 826 | /** Maximum size of the output from psa_raw_key_agreement(). |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 827 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 828 | * This macro expands to a compile-time constant integer. This value is the |
| 829 | * maximum size of the output any raw key agreement algorithm, in bytes. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 830 | * |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 831 | * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits). |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 832 | */ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 833 | #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \ |
| 834 | (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)) |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 835 | |
| 836 | /** The default IV size for a cipher algorithm, in bytes. |
| 837 | * |
| 838 | * The IV that is generated as part of a call to #psa_cipher_encrypt() is always |
| 839 | * the default IV length for the algorithm. |
| 840 | * |
| 841 | * This macro can be used to allocate a buffer of sufficient size to |
| 842 | * store the IV output from #psa_cipher_generate_iv() when using |
| 843 | * a multi-part cipher operation. |
| 844 | * |
| 845 | * See also #PSA_CIPHER_IV_MAX_SIZE. |
| 846 | * |
| 847 | * \warning This macro may evaluate its arguments multiple times or |
| 848 | * zero times, so you should not pass arguments that contain |
| 849 | * side effects. |
| 850 | * |
| 851 | * \param key_type A symmetric key type that is compatible with algorithm \p alg. |
| 852 | * |
| 853 | * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that |
| 854 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 855 | * |
| 856 | * \return The default IV size for the specified key type and algorithm. |
| 857 | * If the algorithm does not use an IV, return 0. |
| 858 | * If the key type or cipher algorithm is not recognized, |
| 859 | * or the parameters are incompatible, return 0. |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 860 | */ |
| 861 | #define PSA_CIPHER_IV_LENGTH(key_type, alg) \ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 862 | (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \ |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 863 | ((alg) == PSA_ALG_CTR || \ |
| 864 | (alg) == PSA_ALG_CFB || \ |
| 865 | (alg) == PSA_ALG_OFB || \ |
| 866 | (alg) == PSA_ALG_XTS || \ |
| 867 | (alg) == PSA_ALG_CBC_NO_PADDING || \ |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 868 | (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ |
Maulik Patel | 28659c4 | 2021-01-06 14:09:22 +0000 | [diff] [blame] | 869 | (key_type) == PSA_KEY_TYPE_CHACHA20 && \ |
| 870 | (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \ |
| 871 | 0) |
| 872 | |
| 873 | /** The maximum IV size for all supported cipher algorithms, in bytes. |
| 874 | * |
| 875 | * See also #PSA_CIPHER_IV_LENGTH(). |
| 876 | */ |
| 877 | #define PSA_CIPHER_IV_MAX_SIZE 16 |
| 878 | |
Maulik Patel | 13b27cf | 2021-05-14 11:44:53 +0100 | [diff] [blame^] | 879 | /** The maximum size of the output of psa_cipher_encrypt(), in bytes. |
| 880 | * |
| 881 | * If the size of the output buffer is at least this large, it is guaranteed |
| 882 | * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. |
| 883 | * Depending on the algorithm, the actual size of the output might be smaller. |
| 884 | * |
| 885 | * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length). |
| 886 | * |
| 887 | * \warning This macro may evaluate its arguments multiple times or |
| 888 | * zero times, so you should not pass arguments that contain |
| 889 | * side effects. |
| 890 | * |
| 891 | * \param key_type A symmetric key type that is compatible with algorithm |
| 892 | * alg. |
| 893 | * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that |
| 894 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 895 | * \param input_length Size of the input in bytes. |
| 896 | * |
| 897 | * \return A sufficient output size for the specified key type and |
| 898 | * algorithm. If the key type or cipher algorithm is not |
| 899 | * recognized, or the parameters are incompatible, |
| 900 | * return 0. |
| 901 | */ |
| 902 | #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ |
| 903 | (alg == PSA_ALG_CBC_PKCS7 ? \ |
| 904 | PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ |
| 905 | (input_length) + 1) + \ |
| 906 | PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \ |
| 907 | (PSA_ALG_IS_CIPHER(alg) ? \ |
| 908 | (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \ |
| 909 | 0)) |
| 910 | |
| 911 | /** A sufficient output buffer size for psa_cipher_encrypt(), for any of the |
| 912 | * supported key types and cipher algorithms. |
| 913 | * |
| 914 | * If the size of the output buffer is at least this large, it is guaranteed |
| 915 | * that psa_cipher_encrypt() will not fail due to an insufficient buffer size. |
| 916 | * |
| 917 | * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). |
| 918 | * |
| 919 | * \param input_length Size of the input in bytes. |
| 920 | * |
| 921 | */ |
| 922 | #define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \ |
| 923 | (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \ |
| 924 | (input_length) + 1) + \ |
| 925 | PSA_CIPHER_IV_MAX_SIZE) |
| 926 | |
| 927 | /** The maximum size of the output of psa_cipher_decrypt(), in bytes. |
| 928 | * |
| 929 | * If the size of the output buffer is at least this large, it is guaranteed |
| 930 | * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. |
| 931 | * Depending on the algorithm, the actual size of the output might be smaller. |
| 932 | * |
| 933 | * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length). |
| 934 | * |
| 935 | * \param key_type A symmetric key type that is compatible with algorithm |
| 936 | * alg. |
| 937 | * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that |
| 938 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 939 | * \param input_length Size of the input in bytes. |
| 940 | * |
| 941 | * \return A sufficient output size for the specified key type and |
| 942 | * algorithm. If the key type or cipher algorithm is not |
| 943 | * recognized, or the parameters are incompatible, |
| 944 | * return 0. |
| 945 | */ |
| 946 | #define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \ |
| 947 | (PSA_ALG_IS_CIPHER(alg) && \ |
| 948 | ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \ |
| 949 | (input_length) : \ |
| 950 | 0) |
| 951 | |
| 952 | /** A sufficient output buffer size for psa_cipher_decrypt(), for any of the |
| 953 | * supported key types and cipher algorithms. |
| 954 | * |
| 955 | * If the size of the output buffer is at least this large, it is guaranteed |
| 956 | * that psa_cipher_decrypt() will not fail due to an insufficient buffer size. |
| 957 | * |
| 958 | * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). |
| 959 | * |
| 960 | * \param input_length Size of the input in bytes. |
| 961 | */ |
| 962 | #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \ |
| 963 | (input_length) |
| 964 | |
| 965 | /** A sufficient output buffer size for psa_cipher_update(). |
| 966 | * |
| 967 | * If the size of the output buffer is at least this large, it is guaranteed |
| 968 | * that psa_cipher_update() will not fail due to an insufficient buffer size. |
| 969 | * The actual size of the output might be smaller in any given call. |
| 970 | * |
| 971 | * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length). |
| 972 | * |
| 973 | * \param key_type A symmetric key type that is compatible with algorithm |
| 974 | * alg. |
| 975 | * \param alg A cipher algorithm (PSA_ALG_XXX value such that |
| 976 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 977 | * \param input_length Size of the input in bytes. |
| 978 | * |
| 979 | * \return A sufficient output size for the specified key type and |
| 980 | * algorithm. If the key type or cipher algorithm is not |
| 981 | * recognized, or the parameters are incompatible, return 0. |
| 982 | */ |
| 983 | #define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \ |
| 984 | (PSA_ALG_IS_CIPHER(alg) ? \ |
| 985 | (((alg) == PSA_ALG_CBC_PKCS7 || \ |
| 986 | (alg) == PSA_ALG_CBC_NO_PADDING || \ |
| 987 | (alg) == PSA_ALG_ECB_NO_PADDING) ? \ |
| 988 | PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \ |
| 989 | input_length) : \ |
| 990 | (input_length)) : \ |
| 991 | 0) |
| 992 | |
| 993 | /** A sufficient output buffer size for psa_cipher_update(), for any of the |
| 994 | * supported key types and cipher algorithms. |
| 995 | * |
| 996 | * If the size of the output buffer is at least this large, it is guaranteed |
| 997 | * that psa_cipher_update() will not fail due to an insufficient buffer size. |
| 998 | * |
| 999 | * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length). |
| 1000 | * |
| 1001 | * \param input_length Size of the input in bytes. |
| 1002 | */ |
| 1003 | #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \ |
| 1004 | (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length)) |
| 1005 | |
| 1006 | /** A sufficient ciphertext buffer size for psa_cipher_finish(). |
| 1007 | * |
| 1008 | * If the size of the ciphertext buffer is at least this large, it is |
| 1009 | * guaranteed that psa_cipher_finish() will not fail due to an insufficient |
| 1010 | * ciphertext buffer size. The actual size of the output might be smaller in |
| 1011 | * any given call. |
| 1012 | * |
| 1013 | * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE(). |
| 1014 | * |
| 1015 | * \param key_type A symmetric key type that is compatible with algorithm |
| 1016 | * alg. |
| 1017 | * \param alg A cipher algorithm (PSA_ALG_XXX value such that |
| 1018 | * #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 1019 | * \return A sufficient output size for the specified key type and |
| 1020 | * algorithm. If the key type or cipher algorithm is not |
| 1021 | * recognized, or the parameters are incompatible, return 0. |
| 1022 | */ |
| 1023 | #define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \ |
| 1024 | (PSA_ALG_IS_CIPHER(alg) ? \ |
| 1025 | (alg == PSA_ALG_CBC_PKCS7 ? \ |
| 1026 | PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \ |
| 1027 | 0) : \ |
| 1028 | 0) |
| 1029 | |
| 1030 | /** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the |
| 1031 | * supported key types and cipher algorithms. |
| 1032 | * |
| 1033 | * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg). |
| 1034 | */ |
| 1035 | #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \ |
| 1036 | (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE) |
| 1037 | |
Antonio de Angelis | 377a155 | 2018-11-22 17:02:40 +0000 | [diff] [blame] | 1038 | #endif /* PSA_CRYPTO_SIZES_H */ |