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