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