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 | /* |
| 24 | * Copyright (C) 2018, ARM Limited, All Rights Reserved |
| 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. |
| 38 | * |
| 39 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 40 | */ |
| 41 | |
| 42 | #ifndef PSA_CRYPTO_SIZES_H |
| 43 | #define PSA_CRYPTO_SIZES_H |
| 44 | |
| 45 | /* Include the Mbed TLS configuration file, the way Mbed TLS does it |
| 46 | * in each of its header files. */ |
| 47 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 48 | #include "../mbedtls/config.h" |
| 49 | #else |
| 50 | #include MBEDTLS_CONFIG_FILE |
| 51 | #endif |
| 52 | |
Gilles Peskine | a7c26db | 2018-12-12 13:42:25 +0100 | [diff] [blame] | 53 | #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8) |
| 54 | #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8) |
| 55 | |
| 56 | /** The size of the output of psa_hash_finish(), in bytes. |
| 57 | * |
| 58 | * This is also the hash size that psa_hash_verify() expects. |
| 59 | * |
| 60 | * \param alg A hash algorithm (\c PSA_ALG_XXX value such that |
| 61 | * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm |
| 62 | * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a |
| 63 | * hash algorithm). |
| 64 | * |
| 65 | * \return The hash size for the specified hash algorithm. |
| 66 | * If the hash algorithm is not recognized, return 0. |
| 67 | * An implementation may return either 0 or the correct size |
| 68 | * for a hash algorithm that it recognizes, but does not support. |
| 69 | */ |
| 70 | #define PSA_HASH_SIZE(alg) \ |
| 71 | ( \ |
| 72 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \ |
| 73 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \ |
| 74 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \ |
| 75 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \ |
| 76 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \ |
| 77 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \ |
| 78 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \ |
| 79 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \ |
| 80 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \ |
| 81 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \ |
| 82 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \ |
| 83 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \ |
| 84 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \ |
| 85 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \ |
| 86 | PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \ |
| 87 | 0) |
| 88 | |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 89 | /** \def PSA_HASH_MAX_SIZE |
| 90 | * |
| 91 | * Maximum size of a hash. |
| 92 | * |
| 93 | * This macro must expand to a compile-time constant integer. This value |
| 94 | * should be the maximum size of a hash supported by the implementation, |
| 95 | * in bytes, and must be no smaller than this maximum. |
| 96 | */ |
Gilles Peskine | 3052f53 | 2018-09-17 14:13:26 +0200 | [diff] [blame] | 97 | /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226, |
| 98 | * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for |
| 99 | * HMAC-SHA3-512. */ |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 100 | #if defined(MBEDTLS_SHA512_C) |
| 101 | #define PSA_HASH_MAX_SIZE 64 |
| 102 | #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128 |
| 103 | #else |
| 104 | #define PSA_HASH_MAX_SIZE 32 |
| 105 | #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64 |
| 106 | #endif |
| 107 | |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 108 | /** \def PSA_MAC_MAX_SIZE |
| 109 | * |
| 110 | * Maximum size of a MAC. |
| 111 | * |
| 112 | * This macro must expand to a compile-time constant integer. This value |
| 113 | * should be the maximum size of a MAC supported by the implementation, |
| 114 | * in bytes, and must be no smaller than this maximum. |
| 115 | */ |
| 116 | /* All non-HMAC MACs have a maximum size that's smaller than the |
| 117 | * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 118 | /* Note that the encoding of truncated MAC algorithms limits this value |
| 119 | * to 64 bytes. |
| 120 | */ |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 121 | #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE |
| 122 | |
Gilles Peskine | a7c26db | 2018-12-12 13:42:25 +0100 | [diff] [blame] | 123 | /** The tag size for an AEAD algorithm, in bytes. |
| 124 | * |
| 125 | * \param alg An AEAD algorithm |
| 126 | * (\c PSA_ALG_XXX value such that |
| 127 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 128 | * |
| 129 | * \return The tag size for the specified algorithm. |
| 130 | * If the AEAD algorithm does not have an identified |
| 131 | * tag that can be distinguished from the rest of |
| 132 | * the ciphertext, return 0. |
| 133 | * If the AEAD algorithm is not recognized, return 0. |
| 134 | * An implementation may return either 0 or a |
| 135 | * correct size for an AEAD algorithm that it |
| 136 | * recognizes, but does not support. |
| 137 | */ |
| 138 | #define PSA_AEAD_TAG_LENGTH(alg) \ |
| 139 | (PSA_ALG_IS_AEAD(alg) ? \ |
| 140 | (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \ |
| 141 | 0) |
| 142 | |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 143 | /* The maximum size of an RSA key on this implementation, in bits. |
| 144 | * This is a vendor-specific macro. |
| 145 | * |
| 146 | * Mbed TLS does not set a hard limit on the size of RSA keys: any key |
| 147 | * whose parameters fit in a bignum is accepted. However large keys can |
| 148 | * induce a large memory usage and long computation times. Unlike other |
| 149 | * auxiliary macros in this file and in crypto.h, which reflect how the |
| 150 | * library is configured, this macro defines how the library is |
| 151 | * configured. This implementation refuses to import or generate an |
| 152 | * RSA key whose size is larger than the value defined here. |
| 153 | * |
| 154 | * Note that an implementation may set different size limits for different |
| 155 | * operations, and does not need to accept all key sizes up to the limit. */ |
| 156 | #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096 |
| 157 | |
| 158 | /* The maximum size of an ECC key on this implementation, in bits. |
| 159 | * This is a vendor-specific macro. */ |
| 160 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) |
| 161 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521 |
| 162 | #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) |
| 163 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512 |
| 164 | #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) |
| 165 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448 |
| 166 | #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) |
| 167 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384 |
| 168 | #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) |
| 169 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384 |
| 170 | #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) |
| 171 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256 |
| 172 | #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) |
| 173 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256 |
| 174 | #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) |
| 175 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256 |
| 176 | #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) |
| 177 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255 |
| 178 | #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) |
| 179 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224 |
| 180 | #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) |
| 181 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224 |
| 182 | #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) |
| 183 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192 |
| 184 | #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) |
| 185 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192 |
| 186 | #else |
| 187 | #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0 |
| 188 | #endif |
| 189 | |
Hanno Becker | 8dbfca4 | 2018-10-12 11:56:55 +0100 | [diff] [blame] | 190 | /** \def PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN |
| 191 | * |
| 192 | * This macro returns the maximum length of the PSK supported |
| 193 | * by the TLS-1.2 PSK-to-MS key derivation. |
| 194 | * |
| 195 | * Quoting RFC 4279, Sect 5.3: |
| 196 | * TLS implementations supporting these ciphersuites MUST support |
| 197 | * arbitrary PSK identities up to 128 octets in length, and arbitrary |
| 198 | * PSKs up to 64 octets in length. Supporting longer identities and |
| 199 | * keys is RECOMMENDED. |
| 200 | * |
| 201 | * Therefore, no implementation should define a value smaller than 64 |
| 202 | * for #PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN. |
| 203 | */ |
| 204 | #define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN 128 |
| 205 | |
Gilles Peskine | af3baab | 2018-06-27 22:55:52 +0200 | [diff] [blame] | 206 | /** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE |
| 207 | * |
| 208 | * Maximum size of an asymmetric signature. |
| 209 | * |
| 210 | * This macro must expand to a compile-time constant integer. This value |
| 211 | * should be the maximum size of a MAC supported by the implementation, |
| 212 | * in bytes, and must be no smaller than this maximum. |
| 213 | */ |
| 214 | #define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \ |
| 215 | PSA_BITS_TO_BYTES( \ |
| 216 | PSA_VENDOR_RSA_MAX_KEY_BITS > PSA_VENDOR_ECC_MAX_CURVE_BITS ? \ |
| 217 | PSA_VENDOR_RSA_MAX_KEY_BITS : \ |
| 218 | PSA_VENDOR_ECC_MAX_CURVE_BITS \ |
| 219 | ) |
| 220 | |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 221 | /** The maximum size of a block cipher supported by the implementation. */ |
| 222 | #define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16 |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 223 | |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 224 | /** The size of the output of psa_mac_sign_finish(), in bytes. |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 225 | * |
Gilles Peskine | acd4be3 | 2018-07-08 19:56:25 +0200 | [diff] [blame] | 226 | * This is also the MAC size that psa_mac_verify_finish() expects. |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 227 | * |
| 228 | * \param key_type The type of the MAC key. |
| 229 | * \param key_bits The size of the MAC key in bits. |
| 230 | * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that |
Gilles Peskine | 63f7930 | 2019-02-15 13:01:17 +0100 | [diff] [blame] | 231 | * #PSA_ALG_IS_MAC(\p alg) is true). |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 232 | * |
| 233 | * \return The MAC size for the specified algorithm with |
| 234 | * the specified key parameters. |
| 235 | * \return 0 if the MAC algorithm is not recognized. |
| 236 | * \return Either 0 or the correct size for a MAC algorithm that |
| 237 | * the implementation recognizes, but does not support. |
| 238 | * \return Unspecified if the key parameters are not consistent |
| 239 | * with the algorithm. |
| 240 | */ |
| 241 | #define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \ |
Gilles Peskine | d911eb7 | 2018-08-14 15:18:45 +0200 | [diff] [blame] | 242 | ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \ |
| 243 | PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 244 | PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \ |
Gilles Peskine | 35fe203 | 2018-08-22 18:26:02 +0200 | [diff] [blame] | 245 | ((void)(key_type), (void)(key_bits), 0)) |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 246 | |
| 247 | /** The maximum size of the output of psa_aead_encrypt(), in bytes. |
| 248 | * |
| 249 | * If the size of the ciphertext buffer is at least this large, it is |
| 250 | * guaranteed that psa_aead_encrypt() will not fail due to an |
| 251 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 252 | * the ciphertext may be smaller. |
| 253 | * |
| 254 | * \param alg An AEAD algorithm |
| 255 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 63f7930 | 2019-02-15 13:01:17 +0100 | [diff] [blame] | 256 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 257 | * \param plaintext_length Size of the plaintext in bytes. |
| 258 | * |
| 259 | * \return The AEAD ciphertext size for the specified |
| 260 | * algorithm. |
| 261 | * If the AEAD algorithm is not recognized, return 0. |
| 262 | * An implementation may return either 0 or a |
| 263 | * correct size for an AEAD algorithm that it |
| 264 | * recognizes, but does not support. |
| 265 | */ |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 266 | #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \ |
| 267 | (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \ |
| 268 | (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 269 | 0) |
| 270 | |
| 271 | /** The maximum size of the output of psa_aead_decrypt(), in bytes. |
| 272 | * |
| 273 | * If the size of the plaintext buffer is at least this large, it is |
| 274 | * guaranteed that psa_aead_decrypt() will not fail due to an |
| 275 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 276 | * the plaintext may be smaller. |
| 277 | * |
| 278 | * \param alg An AEAD algorithm |
| 279 | * (\c PSA_ALG_XXX value such that |
Gilles Peskine | 63f7930 | 2019-02-15 13:01:17 +0100 | [diff] [blame] | 280 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 281 | * \param ciphertext_length Size of the plaintext in bytes. |
| 282 | * |
| 283 | * \return The AEAD ciphertext size for the specified |
| 284 | * algorithm. |
| 285 | * If the AEAD algorithm is not recognized, return 0. |
| 286 | * An implementation may return either 0 or a |
| 287 | * correct size for an AEAD algorithm that it |
| 288 | * recognizes, but does not support. |
| 289 | */ |
Gilles Peskine | 23cc2ff | 2018-08-17 19:47:52 +0200 | [diff] [blame] | 290 | #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \ |
| 291 | (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \ |
| 292 | (plaintext_length) - PSA_AEAD_TAG_LENGTH(alg) : \ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 293 | 0) |
| 294 | |
Gilles Peskine | bdc2786 | 2019-05-06 15:45:16 +0200 | [diff] [blame^] | 295 | /** The maximum size of the output of psa_aead_finish(), in bytes. |
| 296 | * |
| 297 | * If the size of the ciphertext buffer is at least this large, it is |
| 298 | * guaranteed that psa_aead_finish() will not fail due to an |
| 299 | * insufficient buffer size. Depending on the algorithm, the actual size of |
| 300 | * the ciphertext may be smaller. |
| 301 | * |
| 302 | * \param alg An AEAD algorithm |
| 303 | * (\c PSA_ALG_XXX value such that |
| 304 | * #PSA_ALG_IS_AEAD(\p alg) is true). |
| 305 | * |
| 306 | * \return The maximum trailing ciphertext size for the |
| 307 | * specified algorithm. |
| 308 | * If the AEAD algorithm is not recognized, return 0. |
| 309 | * An implementation may return either 0 or a |
| 310 | * correct size for an AEAD algorithm that it |
| 311 | * recognizes, but does not support. |
| 312 | */ |
| 313 | #define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \ |
| 314 | ((size_t)0) |
| 315 | |
Jaeden Amero | 7f04214 | 2019-02-07 10:44:38 +0000 | [diff] [blame] | 316 | #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \ |
| 317 | (PSA_ALG_IS_RSA_OAEP(alg) ? \ |
| 318 | 2 * PSA_HASH_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \ |
Gilles Peskine | a7c26db | 2018-12-12 13:42:25 +0100 | [diff] [blame] | 319 | 11 /*PKCS#1v1.5*/) |
| 320 | |
| 321 | /** |
| 322 | * \brief ECDSA signature size for a given curve bit size |
| 323 | * |
| 324 | * \param curve_bits Curve size in bits. |
| 325 | * \return Signature size in bytes. |
| 326 | * |
| 327 | * \note This macro returns a compile-time constant if its argument is one. |
| 328 | */ |
| 329 | #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \ |
| 330 | (PSA_BITS_TO_BYTES(curve_bits) * 2) |
| 331 | |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 332 | /** Safe signature buffer size for psa_asymmetric_sign(). |
| 333 | * |
| 334 | * This macro returns a safe buffer size for a signature using a key |
| 335 | * of the specified type and size, with the specified algorithm. |
| 336 | * Note that the actual size of the signature may be smaller |
| 337 | * (some algorithms produce a variable-size signature). |
| 338 | * |
| 339 | * \warning This function may call its arguments multiple times or |
| 340 | * zero times, so you should not pass arguments that contain |
| 341 | * side effects. |
| 342 | * |
| 343 | * \param key_type An asymmetric key type (this may indifferently be a |
| 344 | * key pair type or a public key type). |
| 345 | * \param key_bits The size of the key in bits. |
| 346 | * \param alg The signature algorithm. |
| 347 | * |
| 348 | * \return If the parameters are valid and supported, return |
| 349 | * a buffer size in bytes that guarantees that |
| 350 | * psa_asymmetric_sign() will not fail with |
| 351 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 352 | * If the parameters are a valid combination that is not supported |
| 353 | * by the implementation, this macro either shall return either a |
| 354 | * sensible size or 0. |
| 355 | * If the parameters are not valid, the |
| 356 | * return value is unspecified. |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 357 | */ |
| 358 | #define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 359 | (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 360 | PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \ |
| 361 | ((void)alg, 0)) |
| 362 | |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 363 | /** Safe output buffer size for psa_asymmetric_encrypt(). |
| 364 | * |
| 365 | * This macro returns a safe buffer size for a ciphertext produced using |
| 366 | * a key of the specified type and size, with the specified algorithm. |
| 367 | * Note that the actual size of the ciphertext may be smaller, depending |
| 368 | * on the algorithm. |
| 369 | * |
| 370 | * \warning This function may call its arguments multiple times or |
| 371 | * zero times, so you should not pass arguments that contain |
| 372 | * side effects. |
| 373 | * |
| 374 | * \param key_type An asymmetric key type (this may indifferently be a |
| 375 | * key pair type or a public key type). |
| 376 | * \param key_bits The size of the key in bits. |
| 377 | * \param alg The signature algorithm. |
| 378 | * |
| 379 | * \return If the parameters are valid and supported, return |
| 380 | * a buffer size in bytes that guarantees that |
| 381 | * psa_asymmetric_encrypt() will not fail with |
| 382 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 383 | * If the parameters are a valid combination that is not supported |
| 384 | * by the implementation, this macro either shall return either a |
| 385 | * sensible size or 0. |
| 386 | * If the parameters are not valid, the |
| 387 | * return value is unspecified. |
| 388 | */ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 389 | #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 390 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 391 | ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \ |
| 392 | 0) |
Gilles Peskine | dcd1494 | 2018-07-12 00:30:52 +0200 | [diff] [blame] | 393 | |
| 394 | /** Safe output buffer size for psa_asymmetric_decrypt(). |
| 395 | * |
| 396 | * This macro returns a safe buffer size for a ciphertext produced using |
| 397 | * a key of the specified type and size, with the specified algorithm. |
| 398 | * Note that the actual size of the ciphertext may be smaller, depending |
| 399 | * on the algorithm. |
| 400 | * |
| 401 | * \warning This function may call its arguments multiple times or |
| 402 | * zero times, so you should not pass arguments that contain |
| 403 | * side effects. |
| 404 | * |
| 405 | * \param key_type An asymmetric key type (this may indifferently be a |
| 406 | * key pair type or a public key type). |
| 407 | * \param key_bits The size of the key in bits. |
| 408 | * \param alg The signature algorithm. |
| 409 | * |
| 410 | * \return If the parameters are valid and supported, return |
| 411 | * a buffer size in bytes that guarantees that |
| 412 | * psa_asymmetric_decrypt() will not fail with |
| 413 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 414 | * If the parameters are a valid combination that is not supported |
| 415 | * by the implementation, this macro either shall return either a |
| 416 | * sensible size or 0. |
| 417 | * If the parameters are not valid, the |
| 418 | * return value is unspecified. |
| 419 | */ |
Gilles Peskine | 49cee6c | 2018-06-27 21:03:58 +0200 | [diff] [blame] | 420 | #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \ |
| 421 | (PSA_KEY_TYPE_IS_RSA(key_type) ? \ |
| 422 | PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \ |
| 423 | 0) |
| 424 | |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 425 | /* Maximum size of the ASN.1 encoding of an INTEGER with the specified |
| 426 | * number of bits. |
| 427 | * |
| 428 | * This definition assumes that bits <= 2^19 - 9 so that the length field |
| 429 | * is at most 3 bytes. The length of the encoding is the length of the |
| 430 | * bit string padded to a whole number of bytes plus: |
| 431 | * - 1 type byte; |
| 432 | * - 1 to 3 length bytes; |
| 433 | * - 0 to 1 bytes of leading 0 due to the sign bit. |
| 434 | */ |
| 435 | #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \ |
| 436 | ((bits) / 8 + 5) |
| 437 | |
| 438 | /* Maximum size of the export encoding of an RSA public key. |
| 439 | * Assumes that the public exponent is less than 2^32. |
| 440 | * |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 441 | * RSAPublicKey ::= SEQUENCE { |
| 442 | * modulus INTEGER, -- n |
| 443 | * publicExponent INTEGER } -- e |
| 444 | * |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 445 | * - 4 bytes of SEQUENCE overhead; |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 446 | * - n : INTEGER; |
| 447 | * - 7 bytes for the public exponent. |
| 448 | */ |
| 449 | #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
Jaeden Amero | 25384a2 | 2019-01-10 10:23:21 +0000 | [diff] [blame] | 450 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11) |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 451 | |
| 452 | /* Maximum size of the export encoding of an RSA key pair. |
| 453 | * Assumes thatthe public exponent is less than 2^32 and that the size |
| 454 | * difference between the two primes is at most 1 bit. |
| 455 | * |
| 456 | * RSAPrivateKey ::= SEQUENCE { |
| 457 | * version Version, -- 0 |
| 458 | * modulus INTEGER, -- N-bit |
| 459 | * publicExponent INTEGER, -- 32-bit |
| 460 | * privateExponent INTEGER, -- N-bit |
| 461 | * prime1 INTEGER, -- N/2-bit |
| 462 | * prime2 INTEGER, -- N/2-bit |
| 463 | * exponent1 INTEGER, -- N/2-bit |
| 464 | * exponent2 INTEGER, -- N/2-bit |
| 465 | * coefficient INTEGER, -- N/2-bit |
| 466 | * } |
| 467 | * |
| 468 | * - 4 bytes of SEQUENCE overhead; |
| 469 | * - 3 bytes of version; |
| 470 | * - 7 half-size INTEGERs plus 2 full-size INTEGERs, |
| 471 | * overapproximated as 9 half-size INTEGERS; |
| 472 | * - 7 bytes for the public exponent. |
| 473 | */ |
| 474 | #define PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) \ |
| 475 | (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14) |
| 476 | |
| 477 | /* Maximum size of the export encoding of a DSA public key. |
| 478 | * |
| 479 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 480 | * algorithm AlgorithmIdentifier, |
| 481 | * subjectPublicKey BIT STRING } -- contains DSAPublicKey |
| 482 | * AlgorithmIdentifier ::= SEQUENCE { |
| 483 | * algorithm OBJECT IDENTIFIER, |
| 484 | * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs |
| 485 | * DSAPublicKey ::= INTEGER -- public key, Y |
| 486 | * |
| 487 | * - 3 * 4 bytes of SEQUENCE overhead; |
| 488 | * - 1 + 1 + 7 bytes of algorithm (DSA OID); |
| 489 | * - 4 bytes of BIT STRING overhead; |
| 490 | * - 3 full-size INTEGERs (p, g, y); |
| 491 | * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits). |
| 492 | */ |
| 493 | #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
| 494 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59) |
| 495 | |
| 496 | /* Maximum size of the export encoding of a DSA key pair. |
| 497 | * |
| 498 | * DSAPrivateKey ::= SEQUENCE { |
| 499 | * version Version, -- 0 |
| 500 | * prime INTEGER, -- p |
| 501 | * subprime INTEGER, -- q |
| 502 | * generator INTEGER, -- g |
| 503 | * public INTEGER, -- y |
| 504 | * private INTEGER, -- x |
| 505 | * } |
| 506 | * |
| 507 | * - 4 bytes of SEQUENCE overhead; |
| 508 | * - 3 bytes of version; |
| 509 | * - 3 full-size INTEGERs (p, g, y); |
| 510 | * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits). |
| 511 | */ |
| 512 | #define PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) \ |
| 513 | (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75) |
| 514 | |
| 515 | /* Maximum size of the export encoding of an ECC public key. |
| 516 | * |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 517 | * The representation of an ECC public key is: |
| 518 | * - The byte 0x04; |
| 519 | * - `x_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 520 | * - `y_P` as a `ceiling(m/8)`-byte string, big-endian; |
| 521 | * - where m is the bit size associated with the curve. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 522 | * |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 523 | * - 1 byte + 2 * point size. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 524 | */ |
| 525 | #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \ |
Jaeden Amero | ccdce90 | 2019-01-10 11:42:27 +0000 | [diff] [blame] | 526 | (2 * PSA_BITS_TO_BYTES(key_bits) + 1) |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 527 | |
| 528 | /* Maximum size of the export encoding of an ECC key pair. |
| 529 | * |
Gilles Peskine | 5eb1521 | 2018-10-31 13:24:35 +0100 | [diff] [blame] | 530 | * An ECC key pair is represented by the secret value. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 531 | */ |
| 532 | #define PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) \ |
Gilles Peskine | 5eb1521 | 2018-10-31 13:24:35 +0100 | [diff] [blame] | 533 | (PSA_BITS_TO_BYTES(key_bits)) |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 534 | |
| 535 | /** Safe output buffer size for psa_export_key() or psa_export_public_key(). |
| 536 | * |
| 537 | * This macro returns a compile-time constant if its arguments are |
| 538 | * compile-time constants. |
| 539 | * |
| 540 | * \warning This function may call its arguments multiple times or |
| 541 | * zero times, so you should not pass arguments that contain |
| 542 | * side effects. |
| 543 | * |
| 544 | * The following code illustrates how to allocate enough memory to export |
| 545 | * a key by querying the key type and size at runtime. |
| 546 | * \code{c} |
| 547 | * psa_key_type_t key_type; |
| 548 | * size_t key_bits; |
| 549 | * psa_status_t status; |
| 550 | * status = psa_get_key_information(key, &key_type, &key_bits); |
| 551 | * if (status != PSA_SUCCESS) handle_error(...); |
| 552 | * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits); |
| 553 | * unsigned char *buffer = malloc(buffer_size); |
| 554 | * if (buffer != NULL) handle_error(...); |
| 555 | * size_t buffer_length; |
| 556 | * status = psa_export_key(key, buffer, buffer_size, &buffer_length); |
| 557 | * if (status != PSA_SUCCESS) handle_error(...); |
| 558 | * \endcode |
| 559 | * |
| 560 | * For psa_export_public_key(), calculate the buffer size from the |
| 561 | * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR |
| 562 | * to convert a key pair type to the corresponding public key type. |
| 563 | * \code{c} |
| 564 | * psa_key_type_t key_type; |
| 565 | * size_t key_bits; |
| 566 | * psa_status_t status; |
| 567 | * status = psa_get_key_information(key, &key_type, &key_bits); |
| 568 | * if (status != PSA_SUCCESS) handle_error(...); |
| 569 | * psa_key_type_t public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(key_type); |
| 570 | * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits); |
| 571 | * unsigned char *buffer = malloc(buffer_size); |
| 572 | * if (buffer != NULL) handle_error(...); |
| 573 | * size_t buffer_length; |
| 574 | * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length); |
| 575 | * if (status != PSA_SUCCESS) handle_error(...); |
| 576 | * \endcode |
| 577 | * |
| 578 | * \param key_type A supported key type. |
| 579 | * \param key_bits The size of the key in bits. |
Gilles Peskine | 1be949b | 2018-08-10 19:06:59 +0200 | [diff] [blame] | 580 | * |
| 581 | * \return If the parameters are valid and supported, return |
| 582 | * a buffer size in bytes that guarantees that |
| 583 | * psa_asymmetric_sign() will not fail with |
| 584 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 585 | * If the parameters are a valid combination that is not supported |
| 586 | * by the implementation, this macro either shall return either a |
| 587 | * sensible size or 0. |
| 588 | * If the parameters are not valid, the |
| 589 | * return value is unspecified. |
| 590 | */ |
| 591 | #define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \ |
| 592 | (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \ |
| 593 | (key_type) == PSA_KEY_TYPE_RSA_KEYPAIR ? PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) : \ |
| 594 | (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
| 595 | (key_type) == PSA_KEY_TYPE_DSA_KEYPAIR ? PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) : \ |
| 596 | (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
| 597 | PSA_KEY_TYPE_IS_ECC_KEYPAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) : \ |
| 598 | PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \ |
| 599 | 0) |
| 600 | |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 601 | /** Safe output buffer size for psa_get_key_domain_parameters(). |
| 602 | * |
| 603 | * This macro returns a compile-time constant if its arguments are |
| 604 | * compile-time constants. |
| 605 | * |
| 606 | * \warning This function may call its arguments multiple times or |
| 607 | * zero times, so you should not pass arguments that contain |
| 608 | * side effects. |
| 609 | * |
| 610 | * \param key_type A supported key type. |
| 611 | * \param key_bits The size of the key in bits. |
| 612 | * |
| 613 | * \return If the parameters are valid and supported, return |
| 614 | * a buffer size in bytes that guarantees that |
| 615 | * psa_get_key_domain_parameters() will not fail with |
| 616 | * #PSA_ERROR_BUFFER_TOO_SMALL. |
| 617 | * If the parameters are a valid combination that is not supported |
| 618 | * by the implementation, this macro either shall return either a |
| 619 | * sensible size or 0. |
| 620 | * If the parameters are not valid, the |
| 621 | * return value is unspecified. |
| 622 | */ |
| 623 | #define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \ |
| 624 | (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \ |
| 625 | PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 626 | PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \ |
| 627 | 0) |
| 628 | #define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 629 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/) |
| 630 | #define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \ |
| 631 | (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/) |
| 632 | |
Gilles Peskine | 0cad07c | 2018-06-27 19:49:02 +0200 | [diff] [blame] | 633 | #endif /* PSA_CRYPTO_SIZES_H */ |