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