blob: 07b52e462a4eeb8a7a0c937fcd6612efd27fe466 [file] [log] [blame]
Antonio de Angelis377a1552018-11-22 17:02:40 +00001/*
2 * Copyright (c) 2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7/**
8 * \file psa_crypto_sizes.h
9 *
10 * \brief PSA cryptography module: buffer size macros
11 *
12 * \note This file may not be included directly. Applications must
13 * include psa_crypto.h.
14 *
15 * This file contains the definitions of macros that are useful to
16 * compute buffer sizes. The signatures and semantics of these macros
17 * are standardized, but the definitions are not, because they depend on
18 * the available algorithms and, in some cases, on permitted tolerances
19 * on buffer sizes.
20 *
21 * In implementations with isolation between the application and the
22 * cryptography module, implementers should take care to ensure that
23 * the definitions that are exposed to applications match what the
24 * module implements.
25 *
26 * Macros that compute sizes whose values do not depend on the
27 * implementation are in psa_crypto.h.
28 */
29
30#ifndef PSA_CRYPTO_SIZES_H
31#define PSA_CRYPTO_SIZES_H
32
33/** \def PSA_HASH_MAX_SIZE
34 *
35 * Maximum size of a hash.
36 *
37 * This macro must expand to a compile-time constant integer. This value
38 * should be the maximum size of a hash supported by the implementation,
39 * in bytes, and must be no smaller than this maximum.
40 */
41#define PSA_HASH_MAX_SIZE 64
42#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
43
44/** \def PSA_MAC_MAX_SIZE
45 *
46 * Maximum size of a MAC.
47 *
48 * This macro must expand to a compile-time constant integer. This value
49 * should be the maximum size of a MAC supported by the implementation,
50 * in bytes, and must be no smaller than this maximum.
51 */
52/* All non-HMAC MACs have a maximum size that's smaller than the
53 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
54#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
55
56/* The maximum size of an RSA key on this implementation, in bits.
57 * This is a vendor-specific macro.
58 *
59 * Mbed Crypto does not set a hard limit on the size of RSA keys: any key
60 * whose parameters fit in a bignum is accepted. However large keys can
61 * induce a large memory usage and long computation times. Unlike other
62 * auxiliary macros in this file and in crypto.h, which reflect how the
63 * library is configured, this macro defines how the library is
64 * configured. This implementation refuses to import or generate an
65 * RSA key whose size is larger than the value defined here.
66 *
67 * Note that an implementation may set different size limits for different
68 * operations, and does not need to accept all key sizes up to the limit. */
69#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
70
71/* The maximum size of an ECC key on this implementation, in bits.
72 * This is a vendor-specific macro. */
73#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
74
75/** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
76 *
77 * Maximum size of an asymmetric signature.
78 *
79 * This macro must expand to a compile-time constant integer. This value
80 * should be the maximum size of a MAC supported by the implementation,
81 * in bytes, and must be no smaller than this maximum.
82 */
83#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
84 PSA_BITS_TO_BYTES( \
85 PSA_VENDOR_RSA_MAX_KEY_BITS > PSA_VENDOR_ECC_MAX_CURVE_BITS ? \
86 PSA_VENDOR_RSA_MAX_KEY_BITS : \
87 PSA_VENDOR_ECC_MAX_CURVE_BITS \
88 )
89
90
91
92/** The size of the output of psa_mac_sign_finish(), in bytes.
93 *
94 * This is also the MAC size that psa_mac_verify_finish() expects.
95 *
96 * \param key_type The type of the MAC key.
97 * \param key_bits The size of the MAC key in bits.
98 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
99 * #PSA_ALG_IS_MAC(alg) is true).
100 *
101 * \return The MAC size for the specified algorithm with
102 * the specified key parameters.
103 * \return 0 if the MAC algorithm is not recognized.
104 * \return Either 0 or the correct size for a MAC algorithm that
105 * the implementation recognizes, but does not support.
106 * \return Unspecified if the key parameters are not consistent
107 * with the algorithm.
108 */
109#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
110 (PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_HASH(alg)) : \
111 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
112 0)
113
114/** The maximum size of the output of psa_aead_encrypt(), in bytes.
115 *
116 * If the size of the ciphertext buffer is at least this large, it is
117 * guaranteed that psa_aead_encrypt() will not fail due to an
118 * insufficient buffer size. Depending on the algorithm, the actual size of
119 * the ciphertext may be smaller.
120 *
121 * \param alg An AEAD algorithm
122 * (\c PSA_ALG_XXX value such that
123 * #PSA_ALG_IS_AEAD(alg) is true).
124 * \param plaintext_length Size of the plaintext in bytes.
125 *
126 * \return The AEAD ciphertext size for the specified
127 * algorithm.
128 * If the AEAD algorithm is not recognized, return 0.
129 * An implementation may return either 0 or a
130 * correct size for an AEAD algorithm that it
131 * recognizes, but does not support.
132 */
133#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
134 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
135 (plaintext_length) + PSA_AEAD_TAG_SIZE(alg) : \
136 0)
137
138/** The maximum size of the output of psa_aead_decrypt(), in bytes.
139 *
140 * If the size of the plaintext buffer is at least this large, it is
141 * guaranteed that psa_aead_decrypt() will not fail due to an
142 * insufficient buffer size. Depending on the algorithm, the actual size of
143 * the plaintext may be smaller.
144 *
145 * \param alg An AEAD algorithm
146 * (\c PSA_ALG_XXX value such that
147 * #PSA_ALG_IS_AEAD(alg) is true).
148 * \param ciphertext_length Size of the plaintext in bytes.
149 *
150 * \return The AEAD ciphertext size for the specified
151 * algorithm.
152 * If the AEAD algorithm is not recognized, return 0.
153 * An implementation may return either 0 or a
154 * correct size for an AEAD algorithm that it
155 * recognizes, but does not support.
156 */
157#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
158 (PSA_AEAD_TAG_SIZE(alg) != 0 ? \
159 (plaintext_length) - PSA_AEAD_TAG_SIZE(alg) : \
160 0)
161
162/** Safe signature buffer size for psa_asymmetric_sign().
163 *
164 * This macro returns a safe buffer size for a signature using a key
165 * of the specified type and size, with the specified algorithm.
166 * Note that the actual size of the signature may be smaller
167 * (some algorithms produce a variable-size signature).
168 *
169 * \warning This function may call its arguments multiple times or
170 * zero times, so you should not pass arguments that contain
171 * side effects.
172 *
173 * \param key_type An asymmetric key type (this may indifferently be a
174 * key pair type or a public key type).
175 * \param key_bits The size of the key in bits.
176 * \param alg The signature algorithm.
177 *
178 * \return If the parameters are valid and supported, return
179 * a buffer size in bytes that guarantees that
180 * psa_asymmetric_sign() will not fail with
181 * #PSA_ERROR_BUFFER_TOO_SMALL.
182 * If the parameters are a valid combination that is not supported
183 * by the implementation, this macro either shall return either a
184 * sensible size or 0.
185 * If the parameters are not valid, the
186 * return value is unspecified.
187 */
188#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
189 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
190 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
191 ((void)alg, 0))
192
193/** Safe output buffer size for psa_asymmetric_encrypt().
194 *
195 * This macro returns a safe buffer size for a ciphertext produced using
196 * a key of the specified type and size, with the specified algorithm.
197 * Note that the actual size of the ciphertext may be smaller, depending
198 * on the algorithm.
199 *
200 * \warning This function may call its arguments multiple times or
201 * zero times, so you should not pass arguments that contain
202 * side effects.
203 *
204 * \param key_type An asymmetric key type (this may indifferently be a
205 * key pair type or a public key type).
206 * \param key_bits The size of the key in bits.
207 * \param alg The signature algorithm.
208 *
209 * \return If the parameters are valid and supported, return
210 * a buffer size in bytes that guarantees that
211 * psa_asymmetric_encrypt() will not fail with
212 * #PSA_ERROR_BUFFER_TOO_SMALL.
213 * If the parameters are a valid combination that is not supported
214 * by the implementation, this macro either shall return either a
215 * sensible size or 0.
216 * If the parameters are not valid, the
217 * return value is unspecified.
218 */
219#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
220 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
221 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
222 0)
223
224/** Safe output buffer size for psa_asymmetric_decrypt().
225 *
226 * This macro returns a safe buffer size for a ciphertext produced using
227 * a key of the specified type and size, with the specified algorithm.
228 * Note that the actual size of the ciphertext may be smaller, depending
229 * on the algorithm.
230 *
231 * \warning This function may call its arguments multiple times or
232 * zero times, so you should not pass arguments that contain
233 * side effects.
234 *
235 * \param key_type An asymmetric key type (this may indifferently be a
236 * key pair type or a public key type).
237 * \param key_bits The size of the key in bits.
238 * \param alg The signature algorithm.
239 *
240 * \return If the parameters are valid and supported, return
241 * a buffer size in bytes that guarantees that
242 * psa_asymmetric_decrypt() will not fail with
243 * #PSA_ERROR_BUFFER_TOO_SMALL.
244 * If the parameters are a valid combination that is not supported
245 * by the implementation, this macro either shall return either a
246 * sensible size or 0.
247 * If the parameters are not valid, the
248 * return value is unspecified.
249 */
250#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
251 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
252 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
253 0)
254
255#endif /* PSA_CRYPTO_SIZES_H */