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