blob: f4d2cd839959fd1d4226b057c224d456a6c9c869 [file] [log] [blame]
Gilles Peskine0cad07c2018-06-27 19:49:02 +02001/**
2 * \file psa/crypto_sizes.h
3 *
4 * \brief PSA cryptography module: Mbed TLS buffer size macros
5 *
6 * This file contains the definitions of macros that are useful to
7 * compute buffer sizes. The signatures and semantics of these macros
8 * are standardized, but the definitions are not, because they depend on
9 * the available algorithms and, in some cases, on permitted tolerances
10 * on buffer sizes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +020011 *
12 * Macros that compute sizes whose values do not depend on the
13 * implementation are in crypto.h.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020014 */
15/*
16 * Copyright (C) 2018, ARM Limited, All Rights Reserved
17 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
30 *
31 * This file is part of mbed TLS (https://tls.mbed.org)
32 */
33
34#ifndef PSA_CRYPTO_SIZES_H
35#define PSA_CRYPTO_SIZES_H
36
37/* Include the Mbed TLS configuration file, the way Mbed TLS does it
38 * in each of its header files. */
39#if !defined(MBEDTLS_CONFIG_FILE)
40#include "../mbedtls/config.h"
41#else
42#include MBEDTLS_CONFIG_FILE
43#endif
44
Gilles Peskineaf3baab2018-06-27 22:55:52 +020045/** \def PSA_HASH_MAX_SIZE
46 *
47 * Maximum size of a hash.
48 *
49 * This macro must expand to a compile-time constant integer. This value
50 * should be the maximum size of a hash supported by the implementation,
51 * in bytes, and must be no smaller than this maximum.
52 */
Gilles Peskine0cad07c2018-06-27 19:49:02 +020053#if defined(MBEDTLS_SHA512_C)
54#define PSA_HASH_MAX_SIZE 64
55#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
56#else
57#define PSA_HASH_MAX_SIZE 32
58#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
59#endif
60
Gilles Peskineaf3baab2018-06-27 22:55:52 +020061/** \def PSA_MAC_MAX_SIZE
62 *
63 * Maximum size of a MAC.
64 *
65 * This macro must expand to a compile-time constant integer. This value
66 * should be the maximum size of a MAC supported by the implementation,
67 * in bytes, and must be no smaller than this maximum.
68 */
69/* All non-HMAC MACs have a maximum size that's smaller than the
70 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
71#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
72
73/* The maximum size of an RSA key on this implementation, in bits.
74 * This is a vendor-specific macro.
75 *
76 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
77 * whose parameters fit in a bignum is accepted. However large keys can
78 * induce a large memory usage and long computation times. Unlike other
79 * auxiliary macros in this file and in crypto.h, which reflect how the
80 * library is configured, this macro defines how the library is
81 * configured. This implementation refuses to import or generate an
82 * RSA key whose size is larger than the value defined here.
83 *
84 * Note that an implementation may set different size limits for different
85 * operations, and does not need to accept all key sizes up to the limit. */
86#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
87
88/* The maximum size of an ECC key on this implementation, in bits.
89 * This is a vendor-specific macro. */
90#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
91#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
92#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
93#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
94#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
95#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
96#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
97#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
98#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
99#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
100#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
101#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
102#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
103#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
104#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
105#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
106#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
107#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
108#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
109#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
110#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
111#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
112#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
113#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
114#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
115#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
116#else
117#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
118#endif
119
120/** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
121 *
122 * Maximum size of an asymmetric signature.
123 *
124 * This macro must expand to a compile-time constant integer. This value
125 * should be the maximum size of a MAC supported by the implementation,
126 * in bytes, and must be no smaller than this maximum.
127 */
128#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
129 PSA_BITS_TO_BYTES( \
130 PSA_VENDOR_RSA_MAX_KEY_BITS > PSA_VENDOR_ECC_MAX_CURVE_BITS ? \
131 PSA_VENDOR_RSA_MAX_KEY_BITS : \
132 PSA_VENDOR_ECC_MAX_CURVE_BITS \
133 )
134
135
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200136
137/** The size of the output of psa_mac_finish(), in bytes.
138 *
139 * This is also the MAC size that psa_mac_verify() expects.
140 *
141 * \param key_type The type of the MAC key.
142 * \param key_bits The size of the MAC key in bits.
143 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
144 * #PSA_ALG_IS_MAC(alg) is true).
145 *
146 * \return The MAC size for the specified algorithm with
147 * the specified key parameters.
148 * \return 0 if the MAC algorithm is not recognized.
149 * \return Either 0 or the correct size for a MAC algorithm that
150 * the implementation recognizes, but does not support.
151 * \return Unspecified if the key parameters are not consistent
152 * with the algorithm.
153 */
154#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
155 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
156 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
157 0)
158
159/** The maximum size of the output of psa_aead_encrypt(), in bytes.
160 *
161 * If the size of the ciphertext buffer is at least this large, it is
162 * guaranteed that psa_aead_encrypt() will not fail due to an
163 * insufficient buffer size. Depending on the algorithm, the actual size of
164 * the ciphertext may be smaller.
165 *
166 * \param alg An AEAD algorithm
167 * (\c PSA_ALG_XXX value such that
168 * #PSA_ALG_IS_AEAD(alg) is true).
169 * \param plaintext_length Size of the plaintext in bytes.
170 *
171 * \return The AEAD ciphertext size for the specified
172 * algorithm.
173 * If the AEAD algorithm is not recognized, return 0.
174 * An implementation may return either 0 or a
175 * correct size for an AEAD algorithm that it
176 * recognizes, but does not support.
177 */
178#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
179 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
180 (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \
181 0)
182
183/** The maximum size of the output of psa_aead_decrypt(), in bytes.
184 *
185 * If the size of the plaintext buffer is at least this large, it is
186 * guaranteed that psa_aead_decrypt() will not fail due to an
187 * insufficient buffer size. Depending on the algorithm, the actual size of
188 * the plaintext may be smaller.
189 *
190 * \param alg An AEAD algorithm
191 * (\c PSA_ALG_XXX value such that
192 * #PSA_ALG_IS_AEAD(alg) is true).
193 * \param ciphertext_length Size of the plaintext in bytes.
194 *
195 * \return The AEAD ciphertext size for the specified
196 * algorithm.
197 * If the AEAD algorithm is not recognized, return 0.
198 * An implementation may return either 0 or a
199 * correct size for an AEAD algorithm that it
200 * recognizes, but does not support.
201 */
202#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
203 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
204 (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \
205 0)
206
207/** Safe signature buffer size for psa_asymmetric_sign().
208 *
209 * This macro returns a safe buffer size for a signature using a key
210 * of the specified type and size, with the specified algorithm.
211 * Note that the actual size of the signature may be smaller
212 * (some algorithms produce a variable-size signature).
213 *
214 * \warning This function may call its arguments multiple times or
215 * zero times, so you should not pass arguments that contain
216 * side effects.
217 *
218 * \param key_type An asymmetric key type (this may indifferently be a
219 * key pair type or a public key type).
220 * \param key_bits The size of the key in bits.
221 * \param alg The signature algorithm.
222 *
223 * \return If the parameters are valid and supported, return
224 * a buffer size in bytes that guarantees that
225 * psa_asymmetric_sign() will not fail with
226 * #PSA_ERROR_BUFFER_TOO_SMALL.
227 * If the parameters are a valid combination that is not supported
228 * by the implementation, this macro either shall return either a
229 * sensible size or 0.
230 * If the parameters are not valid, the
231 * return value is unspecified.
232 *
233 */
234#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
235 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
236 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
237 ((void)alg, 0))
238
239#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
240 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
241 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
242 0)
243#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
244 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
245 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
246 0)
247
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200248#endif /* PSA_CRYPTO_SIZES_H */