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