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 | |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 144 | /* The maximum size of an RSA key on this implementation, in bits. |
| 145 | * This is a vendor-specific macro. |
| 146 | * |
| 147 | * Mbed TLS does not set a hard limit on the size of RSA keys: any key |
| 148 | * whose parameters fit in a bignum is accepted. However large keys can |
| 149 | * induce a large memory usage and long computation times. Unlike other |
| 150 | * auxiliary macros in this file and in crypto.h, which reflect how the |
| 151 | * library is configured, this macro defines how the library is |
| 152 | * configured. This implementation refuses to import or generate an |
| 153 | * RSA key whose size is larger than the value defined here. |
| 154 | * |
| 155 | * Note that an implementation may set different size limits for different |
| 156 | * operations, and does not need to accept all key sizes up to the limit. */ |
| 157 | #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096 |
| 158 | |
| 159 | /* The maximum size of an ECC key on this implementation, in bits. |
| 160 | * This is a vendor-specific macro. */ |
| 161 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 162 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521 |
| 163 | #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
| 164 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512 |
| 165 | #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 166 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448 |
| 167 | #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 168 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384 |
| 169 | #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
| 170 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384 |
| 171 | #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 172 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256 |
| 173 | #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
| 174 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256 |
| 175 | #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
| 176 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256 |
| 177 | #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 178 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255 |
| 179 | #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
| 180 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224 |
| 181 | #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
| 182 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224 |
| 183 | #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
| 184 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192 |
| 185 | #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
| 186 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192 |
| 187 | #else |
| 188 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0 |
| 189 | #endif |
| 190 | |
gabor-mezei-arm | bdae918 | 2021-01-28 14:33:10 +0100 | [diff] [blame] | 191 | /** This macro returns the maximum supported length of the PSK for the |
| 192 | * TLS-1.2 PSK-to-MS key derivation |
Gilles Peskine | 364d12c | 2021-03-08 17:23:47 +0100 | [diff] [blame^] | 193 | * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)). |
gabor-mezei-arm | bdae918 | 2021-01-28 14:33:10 +0100 | [diff] [blame] | 194 | * |
| 195 | * The maximum supported length does not depend on the chosen hash algorithm. |
Hanno Becker | 8dbfca4 | 2018-10-12 11:56:55 +0100 | [diff] [blame] | 196 | * |
| 197 | * Quoting RFC 4279, Sect 5.3: |
| 198 | * TLS implementations supporting these ciphersuites MUST support |
| 199 | * arbitrary PSK identities up to 128 octets in length, and arbitrary |
| 200 | * PSKs up to 64 octets in length. Supporting longer identities and |
| 201 | * keys is RECOMMENDED. |
| 202 | * |
| 203 | * Therefore, no implementation should define a value smaller than 64 |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 204 | * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE. |
Hanno Becker | 8dbfca4 | 2018-10-12 11:56:55 +0100 | [diff] [blame] | 205 | */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 206 | #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128 |
Hanno Becker | 8dbfca4 | 2018-10-12 11:56:55 +0100 | [diff] [blame] | 207 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 208 | /** The maximum size of a block cipher supported by the implementation. */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 209 | #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16 |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 210 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 211 | /** The size of the output of psa_mac_sign_finish(), in bytes. |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 212 | * |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 213 | * This is also the MAC size that psa_mac_verify_finish() expects. |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 214 | * |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 215 | * \warning This macro may evaluate its arguments multiple times or |
| 216 | * zero times, so you should not pass arguments that contain |
| 217 | * side effects. |
| 218 | * |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 219 | * \param key_type The type of the MAC key. |
| 220 | * \param key_bits The size of the MAC key in bits. |
| 221 | * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 63f7930 | 2019-02-15 13:01:17 +0100 | [diff] [blame] | 222 | * #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 223 | * |
| 224 | * \return The MAC size for the specified algorithm with |
| 225 | * the specified key parameters. |
| 226 | * \return 0 if the MAC algorithm is not recognized. |
| 227 | * \return Either 0 or the correct size for a MAC algorithm that |
| 228 | * the implementation recognizes, but does not support. |
| 229 | * \return Unspecified if the key parameters are not consistent |
| 230 | * with the algorithm. |
| 231 | */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 232 | #define PSA_MAC_LENGTH(key_type, key_bits, alg) \ |
| 233 | ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \ |
| 234 | PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \ |
| 235 | 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] | 236 | ((void)(key_type), (void)(key_bits), 0)) |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 237 | |
| 238 | /** The maximum size of the output of psa_aead_encrypt(), in bytes. |
| 239 | * |
| 240 | * If the size of the ciphertext buffer is at least this large, it is |
| 241 | * guaranteed that psa_aead_encrypt() will not fail due to an |
| 242 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 243 | * the ciphertext may be smaller. |
| 244 | * |
| 245 | * \param alg An AEAD algorithm |
| 246 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 63f7930 | 2019-02-15 13:01:17 +0100 | [diff] [blame] | 247 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 248 | * \param plaintext_length Size of the plaintext in bytes. |
| 249 | * |
| 250 | * \return The AEAD ciphertext size for the specified |
| 251 | * algorithm. |
| 252 | * If the AEAD algorithm is not recognized, return 0. |
| 253 | * An implementation may return either 0 or a |
| 254 | * correct size for an AEAD algorithm that it |
| 255 | * recognizes, but does not support. |
| 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 | |
| 262 | /** The maximum size of the output of psa_aead_decrypt(), in bytes. |
| 263 | * |
| 264 | * If the size of the plaintext buffer is at least this large, it is |
| 265 | * guaranteed that psa_aead_decrypt() will not fail due to an |
| 266 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 267 | * the plaintext may be smaller. |
| 268 | * |
| 269 | * \param alg An AEAD algorithm |
| 270 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 63f7930 | 2019-02-15 13:01:17 +0100 | [diff] [blame] | 271 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 272 | * \param ciphertext_length Size of the plaintext in bytes. |
| 273 | * |
| 274 | * \return The AEAD ciphertext size for the specified |
| 275 | * algorithm. |
| 276 | * If the AEAD algorithm is not recognized, return 0. |
| 277 | * An implementation may return either 0 or a |
| 278 | * correct size for an AEAD algorithm that it |
| 279 | * recognizes, but does not support. |
| 280 | */ |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 281 | #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \ |
| 282 | (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \ |
Gilles Peskine | 36d477d | 2019-05-14 16:09:22 +0200 | [diff] [blame] | 283 | (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 284 | 0) |
| 285 | |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 286 | /** A sufficient output buffer size for psa_aead_update(). |
| 287 | * |
| 288 | * 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] | 289 | * guaranteed that psa_aead_update() will not fail due to an |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 290 | * insufficient buffer size. The actual size of the output may be smaller |
| 291 | * in any given call. |
| 292 | * |
| 293 | * \param alg An AEAD algorithm |
| 294 | * (\c PSA_ALG_XXX value such that |
| 295 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 296 | * \param input_length Size of the input in bytes. |
| 297 | * |
| 298 | * \return A sufficient output buffer size for the specified |
| 299 | * algorithm. |
| 300 | * If the AEAD algorithm is not recognized, return 0. |
| 301 | * An implementation may return either 0 or a |
| 302 | * correct size for an AEAD algorithm that it |
| 303 | * recognizes, but does not support. |
| 304 | */ |
| 305 | /* For all the AEAD modes defined in this specification, it is possible |
| 306 | * to emit output without delay. However, hardware may not always be |
| 307 | * capable of this. So for modes based on a block cipher, allow the |
| 308 | * implementation to delay the output until it has a full block. */ |
Gilles Peskine | 248010c | 2019-05-14 16:08:59 +0200 | [diff] [blame] | 309 | #define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \ |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 310 | (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ |
gabor-mezei-arm | d25ea72 | 2021-01-21 12:20:08 +0100 | [diff] [blame] | 311 | PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)) : \ |
Gilles Peskine | 248010c | 2019-05-14 16:08:59 +0200 | [diff] [blame] | 312 | (input_length)) |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 313 | |
| 314 | /** A sufficient ciphertext buffer size for psa_aead_finish(). |
Gilles Peskine | bdc2786 | 2019-05-06 15:45:16 +0200 | [diff] [blame] | 315 | * |
| 316 | * If the size of the ciphertext buffer is at least this large, it is |
| 317 | * guaranteed that psa_aead_finish() will not fail due to an |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 318 | * insufficient ciphertext buffer size. The actual size of the output may |
| 319 | * be smaller in any given call. |
Gilles Peskine | bdc2786 | 2019-05-06 15:45:16 +0200 | [diff] [blame] | 320 | * |
| 321 | * \param alg An AEAD algorithm |
| 322 | * (\c PSA_ALG_XXX value such that |
| 323 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 324 | * |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 325 | * \return A sufficient ciphertext buffer size for the |
Gilles Peskine | bdc2786 | 2019-05-06 15:45:16 +0200 | [diff] [blame] | 326 | * specified algorithm. |
| 327 | * If the AEAD algorithm is not recognized, return 0. |
| 328 | * An implementation may return either 0 or a |
| 329 | * correct size for an AEAD algorithm that it |
| 330 | * recognizes, but does not support. |
| 331 | */ |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 332 | #define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \ |
| 333 | (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 334 | PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \ |
Gilles Peskine | 49dd8d8 | 2019-05-06 15:16:19 +0200 | [diff] [blame] | 335 | 0) |
| 336 | |
| 337 | /** A sufficient plaintext buffer size for psa_aead_verify(). |
| 338 | * |
| 339 | * If the size of the plaintext buffer is at least this large, it is |
| 340 | * guaranteed that psa_aead_verify() will not fail due to an |
| 341 | * insufficient plaintext buffer size. The actual size of the output may |
| 342 | * be smaller in any given call. |
| 343 | * |
| 344 | * \param alg An AEAD algorithm |
| 345 | * (\c PSA_ALG_XXX value such that |
| 346 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 347 | * |
| 348 | * \return A sufficient plaintext buffer size for the |
| 349 | * specified algorithm. |
| 350 | * If the AEAD algorithm is not recognized, return 0. |
| 351 | * An implementation may return either 0 or a |
| 352 | * correct size for an AEAD algorithm that it |
| 353 | * recognizes, but does not support. |
| 354 | */ |
| 355 | #define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \ |
| 356 | (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 357 | PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 358 | 0) |
| 359 | |
Jaeden Amero | 7f04214 | 2019-02-07 10:44:38 +0000 | [diff] [blame] | 360 | #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ |
| 361 | (PSA_ALG_IS_RSA_OAEP(alg) ? \ |
gabor-mezei-arm | d25ea72 | 2021-01-21 12:20:08 +0100 | [diff] [blame] | 362 | 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \ |
Gilles Peskine | a7c26db | 2018-12-12 13:42:25 +0100 | [diff] [blame] | 363 | 11 /*PKCS#1v1.5*/) |
| 364 | |
| 365 | /** |
| 366 | * \brief ECDSA signature size for a given curve bit size |
| 367 | * |
| 368 | * \param curve_bits Curve size in bits. |
| 369 | * \return Signature size in bytes. |
| 370 | * |
| 371 | * \note This macro returns a compile-time constant if its argument is one. |
| 372 | */ |
| 373 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 374 | (PSA_BITS_TO_BYTES(curve_bits) * 2) |
| 375 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 376 | /** Sufficient signature buffer size for psa_sign_hash(). |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 377 | * |
Gilles Peskine | 56e2dc8 | 2019-05-21 15:59:56 +0200 | [diff] [blame] | 378 | * 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] | 379 | * of the specified type and size, with the specified algorithm. |
| 380 | * Note that the actual size of the signature may be smaller |
| 381 | * (some algorithms produce a variable-size signature). |
| 382 | * |
| 383 | * \warning This function may call its arguments multiple times or |
| 384 | * zero times, so you should not pass arguments that contain |
| 385 | * side effects. |
| 386 | * |
| 387 | * \param key_type An asymmetric key type (this may indifferently be a |
| 388 | * key pair type or a public key type). |
| 389 | * \param key_bits The size of the key in bits. |
| 390 | * \param alg The signature algorithm. |
| 391 | * |
| 392 | * \return If the parameters are valid and supported, return |
| 393 | * a buffer size in bytes that guarantees that |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 394 | * psa_sign_hash() will not fail with |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 395 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 396 | * If the parameters are a valid combination that is not supported |
Gilles Peskine | 27a983d | 2019-05-16 17:24:53 +0200 | [diff] [blame] | 397 | * by the implementation, this macro shall return either a |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 398 | * sensible size or 0. |
| 399 | * If the parameters are not valid, the |
| 400 | * return value is unspecified. |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 401 | */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 402 | #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 403 | (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 404 | PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ |
| 405 | ((void)alg, 0)) |
| 406 | |
Gilles Peskine | 2975571 | 2019-11-08 15:49:40 +0100 | [diff] [blame] | 407 | #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \ |
| 408 | PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) |
| 409 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 410 | /** \def PSA_SIGNATURE_MAX_SIZE |
Gilles Peskine | 2975571 | 2019-11-08 15:49:40 +0100 | [diff] [blame] | 411 | * |
| 412 | * Maximum size of an asymmetric signature. |
| 413 | * |
| 414 | * This macro must expand to a compile-time constant integer. This value |
| 415 | * should be the maximum size of a signature supported by the implementation, |
| 416 | * in bytes, and must be no smaller than this maximum. |
| 417 | */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 418 | #define PSA_SIGNATURE_MAX_SIZE \ |
Gilles Peskine | 2975571 | 2019-11-08 15:49:40 +0100 | [diff] [blame] | 419 | (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \ |
| 420 | PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \ |
| 421 | PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE) |
| 422 | |
Gilles Peskine | 56e2dc8 | 2019-05-21 15:59:56 +0200 | [diff] [blame] | 423 | /** Sufficient output buffer size for psa_asymmetric_encrypt(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 424 | * |
Gilles Peskine | 56e2dc8 | 2019-05-21 15:59:56 +0200 | [diff] [blame] | 425 | * This macro returns a sufficient buffer size for a ciphertext produced using |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 426 | * a key of the specified type and size, with the specified algorithm. |
| 427 | * Note that the actual size of the ciphertext may be smaller, depending |
| 428 | * on the algorithm. |
| 429 | * |
| 430 | * \warning This function may call its arguments multiple times or |
| 431 | * zero times, so you should not pass arguments that contain |
| 432 | * side effects. |
| 433 | * |
| 434 | * \param key_type An asymmetric key type (this may indifferently be a |
| 435 | * key pair type or a public key type). |
| 436 | * \param key_bits The size of the key in bits. |
Gilles Peskine | 9ff8d1f | 2020-05-05 16:00:17 +0200 | [diff] [blame] | 437 | * \param alg The asymmetric encryption algorithm. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 438 | * |
| 439 | * \return If the parameters are valid and supported, return |
| 440 | * a buffer size in bytes that guarantees that |
| 441 | * psa_asymmetric_encrypt() will not fail with |
| 442 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 443 | * If the parameters are a valid combination that is not supported |
Gilles Peskine | 27a983d | 2019-05-16 17:24:53 +0200 | [diff] [blame] | 444 | * by the implementation, this macro shall return either a |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 445 | * sensible size or 0. |
| 446 | * If the parameters are not valid, the |
| 447 | * return value is unspecified. |
| 448 | */ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 449 | #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 450 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 451 | ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 452 | 0) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 453 | |
Gilles Peskine | 56e2dc8 | 2019-05-21 15:59:56 +0200 | [diff] [blame] | 454 | /** Sufficient output buffer size for psa_asymmetric_decrypt(). |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 455 | * |
Gilles Peskine | 7668960 | 2020-05-05 16:01:22 +0200 | [diff] [blame] | 456 | * This macro returns a sufficient buffer size for a plaintext produced using |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 457 | * a key of the specified type and size, with the specified algorithm. |
Gilles Peskine | 7668960 | 2020-05-05 16:01:22 +0200 | [diff] [blame] | 458 | * Note that the actual size of the plaintext may be smaller, depending |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 459 | * on the algorithm. |
| 460 | * |
| 461 | * \warning This function may call its arguments multiple times or |
| 462 | * zero times, so you should not pass arguments that contain |
| 463 | * side effects. |
| 464 | * |
| 465 | * \param key_type An asymmetric key type (this may indifferently be a |
| 466 | * key pair type or a public key type). |
| 467 | * \param key_bits The size of the key in bits. |
Gilles Peskine | 9ff8d1f | 2020-05-05 16:00:17 +0200 | [diff] [blame] | 468 | * \param alg The asymmetric encryption algorithm. |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 469 | * |
| 470 | * \return If the parameters are valid and supported, return |
| 471 | * a buffer size in bytes that guarantees that |
| 472 | * psa_asymmetric_decrypt() will not fail with |
| 473 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 474 | * If the parameters are a valid combination that is not supported |
Gilles Peskine | 27a983d | 2019-05-16 17:24:53 +0200 | [diff] [blame] | 475 | * by the implementation, this macro shall return either a |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 476 | * sensible size or 0. |
| 477 | * If the parameters are not valid, the |
| 478 | * return value is unspecified. |
| 479 | */ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 480 | #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 481 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 482 | PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ |
| 483 | 0) |
| 484 | |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 485 | /* Maximum size of the ASN.1 encoding of an INTEGER with the specified |
| 486 | * number of bits. |
| 487 | * |
| 488 | * This definition assumes that bits <= 2^19 - 9 so that the length field |
| 489 | * is at most 3 bytes. The length of the encoding is the length of the |
| 490 | * bit string padded to a whole number of bytes plus: |
| 491 | * - 1 type byte; |
| 492 | * - 1 to 3 length bytes; |
| 493 | * - 0 to 1 bytes of leading 0 due to the sign bit. |
| 494 | */ |
| 495 | #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \ |
| 496 | ((bits) / 8 + 5) |
| 497 | |
| 498 | /* Maximum size of the export encoding of an RSA public key. |
| 499 | * Assumes that the public exponent is less than 2^32. |
| 500 | * |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 501 | * RSAPublicKey ::= SEQUENCE { |
| 502 | * modulus INTEGER, -- n |
| 503 | * publicExponent INTEGER } -- e |
| 504 | * |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 505 | * - 4 bytes of SEQUENCE overhead; |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 506 | * - n : INTEGER; |
| 507 | * - 7 bytes for the public exponent. |
| 508 | */ |
| 509 | #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 510 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11) |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 511 | |
| 512 | /* Maximum size of the export encoding of an RSA key pair. |
| 513 | * Assumes thatthe public exponent is less than 2^32 and that the size |
| 514 | * difference between the two primes is at most 1 bit. |
| 515 | * |
| 516 | * RSAPrivateKey ::= SEQUENCE { |
| 517 | * version Version, -- 0 |
| 518 | * modulus INTEGER, -- N-bit |
| 519 | * publicExponent INTEGER, -- 32-bit |
| 520 | * privateExponent INTEGER, -- N-bit |
| 521 | * prime1 INTEGER, -- N/2-bit |
| 522 | * prime2 INTEGER, -- N/2-bit |
| 523 | * exponent1 INTEGER, -- N/2-bit |
| 524 | * exponent2 INTEGER, -- N/2-bit |
| 525 | * coefficient INTEGER, -- N/2-bit |
| 526 | * } |
| 527 | * |
| 528 | * - 4 bytes of SEQUENCE overhead; |
| 529 | * - 3 bytes of version; |
| 530 | * - 7 half-size INTEGERs plus 2 full-size INTEGERs, |
| 531 | * overapproximated as 9 half-size INTEGERS; |
| 532 | * - 7 bytes for the public exponent. |
| 533 | */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 534 | #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \ |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 535 | (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14) |
| 536 | |
| 537 | /* Maximum size of the export encoding of a DSA public key. |
| 538 | * |
| 539 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 540 | * algorithm AlgorithmIdentifier, |
| 541 | * subjectPublicKey BIT STRING } -- contains DSAPublicKey |
| 542 | * AlgorithmIdentifier ::= SEQUENCE { |
| 543 | * algorithm OBJECT IDENTIFIER, |
| 544 | * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs |
| 545 | * DSAPublicKey ::= INTEGER -- public key, Y |
| 546 | * |
| 547 | * - 3 * 4 bytes of SEQUENCE overhead; |
| 548 | * - 1 + 1 + 7 bytes of algorithm (DSA OID); |
| 549 | * - 4 bytes of BIT STRING overhead; |
| 550 | * - 3 full-size INTEGERs (p, g, y); |
| 551 | * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits). |
| 552 | */ |
| 553 | #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
| 554 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59) |
| 555 | |
| 556 | /* Maximum size of the export encoding of a DSA key pair. |
| 557 | * |
| 558 | * DSAPrivateKey ::= SEQUENCE { |
| 559 | * version Version, -- 0 |
| 560 | * prime INTEGER, -- p |
| 561 | * subprime INTEGER, -- q |
| 562 | * generator INTEGER, -- g |
| 563 | * public INTEGER, -- y |
| 564 | * private INTEGER, -- x |
| 565 | * } |
| 566 | * |
| 567 | * - 4 bytes of SEQUENCE overhead; |
| 568 | * - 3 bytes of version; |
| 569 | * - 3 full-size INTEGERs (p, g, y); |
| 570 | * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits). |
| 571 | */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 572 | #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \ |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 573 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75) |
| 574 | |
| 575 | /* Maximum size of the export encoding of an ECC public key. |
| 576 | * |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 577 | * The representation of an ECC public key is: |
| 578 | * - The byte 0x04; |
| 579 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 580 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 581 | * - where m is the bit size associated with the curve. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 582 | * |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 583 | * - 1 byte + 2 * point size. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 584 | */ |
| 585 | #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 586 | (2 * PSA_BITS_TO_BYTES(key_bits) + 1) |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 587 | |
| 588 | /* Maximum size of the export encoding of an ECC key pair. |
| 589 | * |
Gilles Peskine | 5eb1521 | 2018-10-31 13:24:35 +0100 | [diff] [blame] | 590 | * An ECC key pair is represented by the secret value. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 591 | */ |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 592 | #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \ |
Gilles Peskine | 5eb1521 | 2018-10-31 13:24:35 +0100 | [diff] [blame] | 593 | (PSA_BITS_TO_BYTES(key_bits)) |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 594 | |
gabor-mezei-arm | bdae918 | 2021-01-28 14:33:10 +0100 | [diff] [blame] | 595 | /** Sufficient output buffer size for psa_export_key() or |
| 596 | * psa_export_public_key(). |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 597 | * |
| 598 | * This macro returns a compile-time constant if its arguments are |
| 599 | * compile-time constants. |
| 600 | * |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 601 | * \warning This macro may evaluate its arguments multiple times or |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 602 | * zero times, so you should not pass arguments that contain |
| 603 | * side effects. |
| 604 | * |
| 605 | * The following code illustrates how to allocate enough memory to export |
| 606 | * a key by querying the key type and size at runtime. |
| 607 | * \code{c} |
Gilles Peskine | d7d43b9 | 2019-05-21 15:56:03 +0200 | [diff] [blame] | 608 | * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 609 | * psa_status_t status; |
Gilles Peskine | d7d43b9 | 2019-05-21 15:56:03 +0200 | [diff] [blame] | 610 | * status = psa_get_key_attributes(key, &attributes); |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 611 | * if (status != PSA_SUCCESS) handle_error(...); |
Gilles Peskine | d7d43b9 | 2019-05-21 15:56:03 +0200 | [diff] [blame] | 612 | * psa_key_type_t key_type = psa_get_key_type(&attributes); |
| 613 | * size_t key_bits = psa_get_key_bits(&attributes); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 614 | * 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] | 615 | * psa_reset_key_attributes(&attributes); |
Gilles Peskine | f82088a | 2019-07-15 11:07:38 +0200 | [diff] [blame] | 616 | * uint8_t *buffer = malloc(buffer_size); |
Gilles Peskine | d7d43b9 | 2019-05-21 15:56:03 +0200 | [diff] [blame] | 617 | * if (buffer == NULL) handle_error(...); |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 618 | * size_t buffer_length; |
| 619 | * status = psa_export_key(key, buffer, buffer_size, &buffer_length); |
| 620 | * if (status != PSA_SUCCESS) handle_error(...); |
| 621 | * \endcode |
| 622 | * |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 623 | * \param key_type A supported key type. |
| 624 | * \param key_bits The size of the key in bits. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 625 | * |
| 626 | * \return If the parameters are valid and supported, return |
| 627 | * a buffer size in bytes that guarantees that |
gabor-mezei-arm | bdae918 | 2021-01-28 14:33:10 +0100 | [diff] [blame] | 628 | * psa_export_key() or psa_export_public_key() will not fail with |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 629 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 630 | * If the parameters are a valid combination that is not supported |
Gilles Peskine | 27a983d | 2019-05-16 17:24:53 +0200 | [diff] [blame] | 631 | * by the implementation, this macro shall return either a |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 632 | * sensible size or 0. |
| 633 | * If the parameters are not valid, the |
| 634 | * return value is unspecified. |
| 635 | */ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 636 | #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \ |
| 637 | (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ |
| 638 | (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] | 639 | (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] | 640 | (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] | 641 | (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] | 642 | PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \ |
| 643 | 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] | 644 | 0) |
| 645 | |
Bence Szépkúti | 423d3e7 | 2020-10-29 11:07:39 +0100 | [diff] [blame] | 646 | /** The default nonce size for an AEAD algorithm, in bytes. |
| 647 | * |
| 648 | * This macro can be used to allocate a buffer of sufficient size to |
| 649 | * store the nonce output from #psa_aead_generate_nonce(). |
| 650 | * |
| 651 | * See also #PSA_AEAD_NONCE_MAX_SIZE. |
| 652 | * |
| 653 | * \note This is not the maximum size of nonce supported as input to #psa_aead_set_nonce(), |
| 654 | * #psa_aead_encrypt() or #psa_aead_decrypt(), just the default size that is generated by |
| 655 | * #psa_aead_generate_nonce(). |
| 656 | * |
| 657 | * \warning This macro may evaluate its arguments multiple times or |
| 658 | * zero times, so you should not pass arguments that contain |
| 659 | * side effects. |
| 660 | * |
| 661 | * \param key_type A symmetric key type that is compatible with algorithm \p alg. |
| 662 | * |
| 663 | * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_AEAD(\p alg) is true). |
| 664 | * |
| 665 | * \return The default nonce size for the specified key type and algorithm. |
| 666 | * If the key type or AEAD algorithm is not recognized, |
| 667 | * or the parameters are incompatible, return 0. |
| 668 | * An implementation can return either 0 or a correct size for a key type |
| 669 | * and AEAD algorithm that it recognizes, but does not support. |
| 670 | */ |
| 671 | #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \ |
gabor-mezei-arm | d25ea72 | 2021-01-21 12:20:08 +0100 | [diff] [blame] | 672 | (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 && \ |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 673 | (PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CCM || \ |
| 674 | PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_GCM) ? 12 : \ |
Bence Szépkúti | 423d3e7 | 2020-10-29 11:07:39 +0100 | [diff] [blame] | 675 | (key_type) == PSA_KEY_TYPE_CHACHA20 && \ |
Bence Szépkúti | a63b20d | 2020-12-16 11:36:46 +0100 | [diff] [blame] | 676 | PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG(alg) == PSA_ALG_CHACHA20_POLY1305 ? 12 : \ |
Bence Szépkúti | 423d3e7 | 2020-10-29 11:07:39 +0100 | [diff] [blame] | 677 | 0) |
| 678 | |
| 679 | /** The maximum default nonce size among all supported pairs of key types and AEAD algorithms, in bytes. |
| 680 | * |
| 681 | * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH() may return. |
| 682 | * |
| 683 | * \note This is not the maximum size of nonce supported as input to #psa_aead_set_nonce(), |
| 684 | * #psa_aead_encrypt() or #psa_aead_decrypt(), just the largest size that may be generated by |
| 685 | * #psa_aead_generate_nonce(). |
| 686 | */ |
| 687 | #define PSA_AEAD_NONCE_MAX_SIZE 12 |
| 688 | |
| 689 | /** The default IV size for a cipher algorithm, in bytes. |
| 690 | * |
| 691 | * The IV that is generated as part of a call to #psa_cipher_encrypt() is always |
| 692 | * the default IV length for the algorithm. |
| 693 | * |
| 694 | * This macro can be used to allocate a buffer of sufficient size to |
| 695 | * store the IV output from #psa_cipher_generate_iv() when using |
| 696 | * a multi-part cipher operation. |
| 697 | * |
| 698 | * See also #PSA_CIPHER_IV_MAX_SIZE. |
| 699 | * |
| 700 | * \warning This macro may evaluate its arguments multiple times or |
| 701 | * zero times, so you should not pass arguments that contain |
| 702 | * side effects. |
| 703 | * |
| 704 | * \param key_type A symmetric key type that is compatible with algorithm \p alg. |
| 705 | * |
| 706 | * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true). |
| 707 | * |
| 708 | * \return The default IV size for the specified key type and algorithm. |
| 709 | * If the algorithm does not use an IV, return 0. |
| 710 | * If the key type or cipher algorithm is not recognized, |
| 711 | * or the parameters are incompatible, return 0. |
| 712 | * An implementation can return either 0 or a correct size for a key type |
| 713 | * and cipher algorithm that it recognizes, but does not support. |
| 714 | */ |
| 715 | #define PSA_CIPHER_IV_LENGTH(key_type, alg) \ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 716 | (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \ |
Bence Szépkúti | 423d3e7 | 2020-10-29 11:07:39 +0100 | [diff] [blame] | 717 | ((alg) == PSA_ALG_CTR || \ |
| 718 | (alg) == PSA_ALG_CFB || \ |
| 719 | (alg) == PSA_ALG_OFB || \ |
| 720 | (alg) == PSA_ALG_XTS || \ |
| 721 | (alg) == PSA_ALG_CBC_NO_PADDING || \ |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 722 | (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] | 723 | (key_type) == PSA_KEY_TYPE_CHACHA20 && \ |
Bence Szépkúti | cbe3953 | 2020-12-08 00:01:31 +0100 | [diff] [blame] | 724 | (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \ |
Bence Szépkúti | 423d3e7 | 2020-10-29 11:07:39 +0100 | [diff] [blame] | 725 | 0) |
| 726 | |
| 727 | /** The maximum IV size for all supported cipher algorithms, in bytes. |
| 728 | * |
| 729 | * See also #PSA_CIPHER_IV_LENGTH(). |
| 730 | */ |
| 731 | #define PSA_CIPHER_IV_MAX_SIZE 16 |
| 732 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 733 | #endif /* PSA_CRYPTO_SIZES_H */ |