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