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