blob: 39dbccb8988ded1d6932893eaac5cec21b363bc4 [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 *
Gilles Peskine07c91f52018-06-28 18:02:53 +02006 * \note This file may not be included directly. Applications must
7 * include psa/crypto.h.
8 *
Gilles Peskine0cad07c2018-06-27 19:49:02 +02009 * 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 Peskine49cee6c2018-06-27 21:03:58 +020014 *
Gilles Peskine07c91f52018-06-28 18:02:53 +020015 * 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 Peskine49cee6c2018-06-27 21:03:58 +020020 * Macros that compute sizes whose values do not depend on the
21 * implementation are in crypto.h.
Gilles Peskine0cad07c2018-06-27 19:49:02 +020022 */
23/*
24 * Copyright (C) 2018, ARM Limited, All Rights Reserved
25 * SPDX-License-Identifier: Apache-2.0
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
38 *
39 * This file is part of mbed TLS (https://tls.mbed.org)
40 */
41
42#ifndef PSA_CRYPTO_SIZES_H
43#define PSA_CRYPTO_SIZES_H
44
45/* Include the Mbed TLS configuration file, the way Mbed TLS does it
46 * in each of its header files. */
47#if !defined(MBEDTLS_CONFIG_FILE)
48#include "../mbedtls/config.h"
49#else
50#include MBEDTLS_CONFIG_FILE
51#endif
52
Gilles Peskinea7c26db2018-12-12 13:42:25 +010053#define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
54#define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
55
56/** The size of the output of psa_hash_finish(), in bytes.
57 *
58 * This is also the hash size that psa_hash_verify() expects.
59 *
60 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
61 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
62 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
63 * hash algorithm).
64 *
65 * \return The hash size for the specified hash algorithm.
66 * If the hash algorithm is not recognized, return 0.
67 * An implementation may return either 0 or the correct size
68 * for a hash algorithm that it recognizes, but does not support.
69 */
70#define PSA_HASH_SIZE(alg) \
71 ( \
72 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
73 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
74 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
87 0)
88
Gilles Peskineaf3baab2018-06-27 22:55:52 +020089/** \def PSA_HASH_MAX_SIZE
90 *
91 * Maximum size of a hash.
92 *
93 * This macro must expand to a compile-time constant integer. This value
94 * should be the maximum size of a hash supported by the implementation,
95 * in bytes, and must be no smaller than this maximum.
96 */
Gilles Peskine3052f532018-09-17 14:13:26 +020097/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
98 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
99 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200100#if defined(MBEDTLS_SHA512_C)
101#define PSA_HASH_MAX_SIZE 64
102#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
103#else
104#define PSA_HASH_MAX_SIZE 32
105#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
106#endif
107
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200108/** \def PSA_MAC_MAX_SIZE
109 *
110 * Maximum size of a MAC.
111 *
112 * This macro must expand to a compile-time constant integer. This value
113 * should be the maximum size of a MAC supported by the implementation,
114 * in bytes, and must be no smaller than this maximum.
115 */
116/* All non-HMAC MACs have a maximum size that's smaller than the
117 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200118/* Note that the encoding of truncated MAC algorithms limits this value
119 * to 64 bytes.
120 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200121#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
122
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100123/** The tag size for an AEAD algorithm, in bytes.
124 *
125 * \param alg An AEAD algorithm
126 * (\c PSA_ALG_XXX value such that
127 * #PSA_ALG_IS_AEAD(\p alg) is true).
128 *
129 * \return The tag size for the specified algorithm.
130 * If the AEAD algorithm does not have an identified
131 * tag that can be distinguished from the rest of
132 * the ciphertext, return 0.
133 * If the AEAD algorithm is not recognized, return 0.
134 * An implementation may return either 0 or a
135 * correct size for an AEAD algorithm that it
136 * recognizes, but does not support.
137 */
138#define PSA_AEAD_TAG_LENGTH(alg) \
139 (PSA_ALG_IS_AEAD(alg) ? \
140 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
141 0)
142
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200143/* The maximum size of an RSA key on this implementation, in bits.
144 * This is a vendor-specific macro.
145 *
146 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
147 * whose parameters fit in a bignum is accepted. However large keys can
148 * induce a large memory usage and long computation times. Unlike other
149 * auxiliary macros in this file and in crypto.h, which reflect how the
150 * library is configured, this macro defines how the library is
151 * configured. This implementation refuses to import or generate an
152 * RSA key whose size is larger than the value defined here.
153 *
154 * Note that an implementation may set different size limits for different
155 * operations, and does not need to accept all key sizes up to the limit. */
156#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
157
158/* The maximum size of an ECC key on this implementation, in bits.
159 * This is a vendor-specific macro. */
160#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
161#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
162#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
163#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
164#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
165#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
166#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
167#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
168#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
169#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
170#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
171#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
172#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
173#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
174#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
175#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
176#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
177#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
178#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
179#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
180#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
181#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
182#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
183#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
184#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
185#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
186#else
187#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
188#endif
189
Gilles Peskine6c951442019-05-13 12:51:03 +0200190/** Bit size associated with an elliptic curve.
191 *
192 * \param curve An elliptic curve (value of type #psa_ecc_curve_t).
193 *
194 * \return The size associated with \p curve, in bits.
195 * This may be 0 if the implementation does not support
196 * the specified curve.
197 */
198#define PSA_ECC_CURVE_BITS(curve) \
199 ((curve) == PSA_ECC_CURVE_SECT163K1 ? 163 : \
200 (curve) == PSA_ECC_CURVE_SECT163R1 ? 163 : \
201 (curve) == PSA_ECC_CURVE_SECT163R2 ? 163 : \
202 (curve) == PSA_ECC_CURVE_SECT193R1 ? 193 : \
203 (curve) == PSA_ECC_CURVE_SECT193R2 ? 193 : \
204 (curve) == PSA_ECC_CURVE_SECT233K1 ? 233 : \
205 (curve) == PSA_ECC_CURVE_SECT233R1 ? 233 : \
206 (curve) == PSA_ECC_CURVE_SECT239K1 ? 239 : \
207 (curve) == PSA_ECC_CURVE_SECT283K1 ? 283 : \
208 (curve) == PSA_ECC_CURVE_SECT283R1 ? 283 : \
209 (curve) == PSA_ECC_CURVE_SECT409K1 ? 409 : \
210 (curve) == PSA_ECC_CURVE_SECT409R1 ? 409 : \
211 (curve) == PSA_ECC_CURVE_SECT571K1 ? 571 : \
212 (curve) == PSA_ECC_CURVE_SECT571R1 ? 571 : \
213 (curve) == PSA_ECC_CURVE_SECP160K1 ? 160 : \
214 (curve) == PSA_ECC_CURVE_SECP160R1 ? 160 : \
215 (curve) == PSA_ECC_CURVE_SECP160R2 ? 160 : \
216 (curve) == PSA_ECC_CURVE_SECP192K1 ? 192 : \
217 (curve) == PSA_ECC_CURVE_SECP192R1 ? 192 : \
218 (curve) == PSA_ECC_CURVE_SECP224K1 ? 224 : \
219 (curve) == PSA_ECC_CURVE_SECP224R1 ? 224 : \
220 (curve) == PSA_ECC_CURVE_SECP256K1 ? 256 : \
221 (curve) == PSA_ECC_CURVE_SECP256R1 ? 256 : \
222 (curve) == PSA_ECC_CURVE_SECP384R1 ? 384 : \
223 (curve) == PSA_ECC_CURVE_SECP521R1 ? 521 : \
224 (curve) == PSA_ECC_CURVE_BRAINPOOL_P256R1 ? 256 : \
225 (curve) == PSA_ECC_CURVE_BRAINPOOL_P384R1 ? 384 : \
226 (curve) == PSA_ECC_CURVE_BRAINPOOL_P512R1 ? 512 : \
227 (curve) == PSA_ECC_CURVE_CURVE25519 ? 255 : \
228 (curve) == PSA_ECC_CURVE_CURVE448 ? 448 : \
229 0)
230
Hanno Becker8dbfca42018-10-12 11:56:55 +0100231/** \def PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN
232 *
233 * This macro returns the maximum length of the PSK supported
234 * by the TLS-1.2 PSK-to-MS key derivation.
235 *
236 * Quoting RFC 4279, Sect 5.3:
237 * TLS implementations supporting these ciphersuites MUST support
238 * arbitrary PSK identities up to 128 octets in length, and arbitrary
239 * PSKs up to 64 octets in length. Supporting longer identities and
240 * keys is RECOMMENDED.
241 *
242 * Therefore, no implementation should define a value smaller than 64
243 * for #PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN.
244 */
245#define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN 128
246
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200247/** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
248 *
249 * Maximum size of an asymmetric signature.
250 *
251 * This macro must expand to a compile-time constant integer. This value
252 * should be the maximum size of a MAC supported by the implementation,
253 * in bytes, and must be no smaller than this maximum.
254 */
255#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
256 PSA_BITS_TO_BYTES( \
257 PSA_VENDOR_RSA_MAX_KEY_BITS > PSA_VENDOR_ECC_MAX_CURVE_BITS ? \
258 PSA_VENDOR_RSA_MAX_KEY_BITS : \
259 PSA_VENDOR_ECC_MAX_CURVE_BITS \
260 )
261
Gilles Peskined911eb72018-08-14 15:18:45 +0200262/** The maximum size of a block cipher supported by the implementation. */
263#define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200264
Gilles Peskineacd4be32018-07-08 19:56:25 +0200265/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200266 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200267 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200268 *
269 * \param key_type The type of the MAC key.
270 * \param key_bits The size of the MAC key in bits.
271 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100272 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200273 *
274 * \return The MAC size for the specified algorithm with
275 * the specified key parameters.
276 * \return 0 if the MAC algorithm is not recognized.
277 * \return Either 0 or the correct size for a MAC algorithm that
278 * the implementation recognizes, but does not support.
279 * \return Unspecified if the key parameters are not consistent
280 * with the algorithm.
281 */
282#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Gilles Peskined911eb72018-08-14 15:18:45 +0200283 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
284 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200285 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200286 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200287
288/** The maximum size of the output of psa_aead_encrypt(), in bytes.
289 *
290 * If the size of the ciphertext buffer is at least this large, it is
291 * guaranteed that psa_aead_encrypt() will not fail due to an
292 * insufficient buffer size. Depending on the algorithm, the actual size of
293 * the ciphertext may be smaller.
294 *
295 * \param alg An AEAD algorithm
296 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100297 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200298 * \param plaintext_length Size of the plaintext in bytes.
299 *
300 * \return The AEAD ciphertext size for the specified
301 * algorithm.
302 * If the AEAD algorithm is not recognized, return 0.
303 * An implementation may return either 0 or a
304 * correct size for an AEAD algorithm that it
305 * recognizes, but does not support.
306 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200307#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
308 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
309 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200310 0)
311
Gilles Peskine30a9e412019-01-14 18:36:12 +0100312/** The maximum size of the output of psa_aead_finish(), in bytes.
313 *
314 * If the size of the ciphertext buffer is at least this large, it is
315 * guaranteed that psa_aead_finish() will not fail due to an
316 * insufficient buffer size. Depending on the algorithm, the actual size of
317 * the ciphertext may be smaller.
318 *
319 * \param alg An AEAD algorithm
320 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100321 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine30a9e412019-01-14 18:36:12 +0100322 *
323 * \return The maximum trailing ciphertext size for the
324 * specified algorithm.
325 * If the AEAD algorithm is not recognized, return 0.
326 * An implementation may return either 0 or a
327 * correct size for an AEAD algorithm that it
328 * recognizes, but does not support.
329 */
Gilles Peskine6bce7f72019-02-08 11:24:03 +0100330#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
Gilles Peskine30a9e412019-01-14 18:36:12 +0100331 ((size_t)0)
332
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200333/** The maximum size of the output of psa_aead_decrypt(), in bytes.
334 *
335 * If the size of the plaintext buffer is at least this large, it is
336 * guaranteed that psa_aead_decrypt() will not fail due to an
337 * insufficient buffer size. Depending on the algorithm, the actual size of
338 * the plaintext may be smaller.
339 *
340 * \param alg An AEAD algorithm
341 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100342 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200343 * \param ciphertext_length Size of the plaintext in bytes.
344 *
345 * \return The AEAD ciphertext size for the specified
346 * algorithm.
347 * If the AEAD algorithm is not recognized, return 0.
348 * An implementation may return either 0 or a
349 * correct size for an AEAD algorithm that it
350 * recognizes, but does not support.
351 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200352#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
353 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
354 (plaintext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200355 0)
356
Jaeden Amero7f042142019-02-07 10:44:38 +0000357#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
358 (PSA_ALG_IS_RSA_OAEP(alg) ? \
359 2 * PSA_HASH_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100360 11 /*PKCS#1v1.5*/)
361
362/**
363 * \brief ECDSA signature size for a given curve bit size
364 *
365 * \param curve_bits Curve size in bits.
366 * \return Signature size in bytes.
367 *
368 * \note This macro returns a compile-time constant if its argument is one.
369 */
370#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
371 (PSA_BITS_TO_BYTES(curve_bits) * 2)
372
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200373/** Safe signature buffer size for psa_asymmetric_sign().
374 *
375 * This macro returns a safe buffer size for a signature using a key
376 * of the specified type and size, with the specified algorithm.
377 * Note that the actual size of the signature may be smaller
378 * (some algorithms produce a variable-size signature).
379 *
380 * \warning This function may call its arguments multiple times or
381 * zero times, so you should not pass arguments that contain
382 * side effects.
383 *
384 * \param key_type An asymmetric key type (this may indifferently be a
385 * key pair type or a public key type).
386 * \param key_bits The size of the key in bits.
387 * \param alg The signature algorithm.
388 *
389 * \return If the parameters are valid and supported, return
390 * a buffer size in bytes that guarantees that
391 * psa_asymmetric_sign() will not fail with
392 * #PSA_ERROR_BUFFER_TOO_SMALL.
393 * If the parameters are a valid combination that is not supported
394 * by the implementation, this macro either shall return either a
395 * sensible size or 0.
396 * If the parameters are not valid, the
397 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200398 */
399#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
400 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
401 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
402 ((void)alg, 0))
403
Gilles Peskinedcd14942018-07-12 00:30:52 +0200404/** Safe output buffer size for psa_asymmetric_encrypt().
405 *
406 * This macro returns a safe buffer size for a ciphertext produced using
407 * a key of the specified type and size, with the specified algorithm.
408 * Note that the actual size of the ciphertext may be smaller, depending
409 * on the algorithm.
410 *
411 * \warning This function may call its arguments multiple times or
412 * zero times, so you should not pass arguments that contain
413 * side effects.
414 *
415 * \param key_type An asymmetric key type (this may indifferently be a
416 * key pair type or a public key type).
417 * \param key_bits The size of the key in bits.
418 * \param alg The signature algorithm.
419 *
420 * \return If the parameters are valid and supported, return
421 * a buffer size in bytes that guarantees that
422 * psa_asymmetric_encrypt() will not fail with
423 * #PSA_ERROR_BUFFER_TOO_SMALL.
424 * If the parameters are a valid combination that is not supported
425 * by the implementation, this macro either shall return either a
426 * sensible size or 0.
427 * If the parameters are not valid, the
428 * return value is unspecified.
429 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200430#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
431 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
432 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
433 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200434
435/** Safe output buffer size for psa_asymmetric_decrypt().
436 *
437 * This macro returns a safe buffer size for a ciphertext produced using
438 * a key of the specified type and size, with the specified algorithm.
439 * Note that the actual size of the ciphertext may be smaller, depending
440 * on the algorithm.
441 *
442 * \warning This function may call its arguments multiple times or
443 * zero times, so you should not pass arguments that contain
444 * side effects.
445 *
446 * \param key_type An asymmetric key type (this may indifferently be a
447 * key pair type or a public key type).
448 * \param key_bits The size of the key in bits.
449 * \param alg The signature algorithm.
450 *
451 * \return If the parameters are valid and supported, return
452 * a buffer size in bytes that guarantees that
453 * psa_asymmetric_decrypt() will not fail with
454 * #PSA_ERROR_BUFFER_TOO_SMALL.
455 * If the parameters are a valid combination that is not supported
456 * by the implementation, this macro either shall return either a
457 * sensible size or 0.
458 * If the parameters are not valid, the
459 * return value is unspecified.
460 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200461#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
462 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
463 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
464 0)
465
Gilles Peskine1be949b2018-08-10 19:06:59 +0200466/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
467 * number of bits.
468 *
469 * This definition assumes that bits <= 2^19 - 9 so that the length field
470 * is at most 3 bytes. The length of the encoding is the length of the
471 * bit string padded to a whole number of bytes plus:
472 * - 1 type byte;
473 * - 1 to 3 length bytes;
474 * - 0 to 1 bytes of leading 0 due to the sign bit.
475 */
476#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
477 ((bits) / 8 + 5)
478
479/* Maximum size of the export encoding of an RSA public key.
480 * Assumes that the public exponent is less than 2^32.
481 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200482 * RSAPublicKey ::= SEQUENCE {
483 * modulus INTEGER, -- n
484 * publicExponent INTEGER } -- e
485 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000486 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200487 * - n : INTEGER;
488 * - 7 bytes for the public exponent.
489 */
490#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000491 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200492
493/* Maximum size of the export encoding of an RSA key pair.
494 * Assumes thatthe public exponent is less than 2^32 and that the size
495 * difference between the two primes is at most 1 bit.
496 *
497 * RSAPrivateKey ::= SEQUENCE {
498 * version Version, -- 0
499 * modulus INTEGER, -- N-bit
500 * publicExponent INTEGER, -- 32-bit
501 * privateExponent INTEGER, -- N-bit
502 * prime1 INTEGER, -- N/2-bit
503 * prime2 INTEGER, -- N/2-bit
504 * exponent1 INTEGER, -- N/2-bit
505 * exponent2 INTEGER, -- N/2-bit
506 * coefficient INTEGER, -- N/2-bit
507 * }
508 *
509 * - 4 bytes of SEQUENCE overhead;
510 * - 3 bytes of version;
511 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
512 * overapproximated as 9 half-size INTEGERS;
513 * - 7 bytes for the public exponent.
514 */
515#define PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) \
516 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
517
518/* Maximum size of the export encoding of a DSA public key.
519 *
520 * SubjectPublicKeyInfo ::= SEQUENCE {
521 * algorithm AlgorithmIdentifier,
522 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
523 * AlgorithmIdentifier ::= SEQUENCE {
524 * algorithm OBJECT IDENTIFIER,
525 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
526 * DSAPublicKey ::= INTEGER -- public key, Y
527 *
528 * - 3 * 4 bytes of SEQUENCE overhead;
529 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
530 * - 4 bytes of BIT STRING overhead;
531 * - 3 full-size INTEGERs (p, g, y);
532 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
533 */
534#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
535 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
536
537/* Maximum size of the export encoding of a DSA key pair.
538 *
539 * DSAPrivateKey ::= SEQUENCE {
540 * version Version, -- 0
541 * prime INTEGER, -- p
542 * subprime INTEGER, -- q
543 * generator INTEGER, -- g
544 * public INTEGER, -- y
545 * private INTEGER, -- x
546 * }
547 *
548 * - 4 bytes of SEQUENCE overhead;
549 * - 3 bytes of version;
550 * - 3 full-size INTEGERs (p, g, y);
551 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
552 */
553#define PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) \
554 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
555
556/* Maximum size of the export encoding of an ECC public key.
557 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000558 * The representation of an ECC public key is:
559 * - The byte 0x04;
560 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
561 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
562 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200563 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000564 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200565 */
566#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000567 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200568
569/* Maximum size of the export encoding of an ECC key pair.
570 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100571 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200572 */
573#define PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100574 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200575
576/** Safe output buffer size for psa_export_key() or psa_export_public_key().
577 *
578 * This macro returns a compile-time constant if its arguments are
579 * compile-time constants.
580 *
581 * \warning This function may call its arguments multiple times or
582 * zero times, so you should not pass arguments that contain
583 * side effects.
584 *
585 * The following code illustrates how to allocate enough memory to export
586 * a key by querying the key type and size at runtime.
587 * \code{c}
588 * psa_key_type_t key_type;
589 * size_t key_bits;
590 * psa_status_t status;
591 * status = psa_get_key_information(key, &key_type, &key_bits);
592 * if (status != PSA_SUCCESS) handle_error(...);
593 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
594 * unsigned char *buffer = malloc(buffer_size);
595 * if (buffer != NULL) handle_error(...);
596 * size_t buffer_length;
597 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
598 * if (status != PSA_SUCCESS) handle_error(...);
599 * \endcode
600 *
601 * For psa_export_public_key(), calculate the buffer size from the
602 * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR
603 * to convert a key pair type to the corresponding public key type.
604 * \code{c}
605 * psa_key_type_t key_type;
606 * size_t key_bits;
607 * psa_status_t status;
608 * status = psa_get_key_information(key, &key_type, &key_bits);
609 * if (status != PSA_SUCCESS) handle_error(...);
610 * psa_key_type_t public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(key_type);
611 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits);
612 * unsigned char *buffer = malloc(buffer_size);
613 * if (buffer != NULL) handle_error(...);
614 * size_t buffer_length;
615 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
616 * if (status != PSA_SUCCESS) handle_error(...);
617 * \endcode
618 *
619 * \param key_type A supported key type.
620 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200621 *
622 * \return If the parameters are valid and supported, return
623 * a buffer size in bytes that guarantees that
624 * psa_asymmetric_sign() will not fail with
625 * #PSA_ERROR_BUFFER_TOO_SMALL.
626 * If the parameters are a valid combination that is not supported
627 * by the implementation, this macro either shall return either a
628 * sensible size or 0.
629 * If the parameters are not valid, the
630 * return value is unspecified.
631 */
632#define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \
633 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
634 (key_type) == PSA_KEY_TYPE_RSA_KEYPAIR ? PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) : \
635 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
636 (key_type) == PSA_KEY_TYPE_DSA_KEYPAIR ? PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) : \
637 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
638 PSA_KEY_TYPE_IS_ECC_KEYPAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) : \
639 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
640 0)
641
Gilles Peskineaa02c172019-04-28 11:44:17 +0200642/** Safe output buffer size for psa_get_key_domain_parameters().
643 *
644 * This macro returns a compile-time constant if its arguments are
645 * compile-time constants.
646 *
647 * \warning This function may call its arguments multiple times or
648 * zero times, so you should not pass arguments that contain
649 * side effects.
650 *
651 * \param key_type A supported key type.
652 * \param key_bits The size of the key in bits.
653 *
654 * \return If the parameters are valid and supported, return
655 * a buffer size in bytes that guarantees that
656 * psa_get_key_domain_parameters() will not fail with
657 * #PSA_ERROR_BUFFER_TOO_SMALL.
658 * If the parameters are a valid combination that is not supported
659 * by the implementation, this macro either shall return either a
660 * sensible size or 0.
661 * If the parameters are not valid, the
662 * return value is unspecified.
663 */
664#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
665 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
666 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
667 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
668 0)
669#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
670 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
671#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
672 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
673
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200674#endif /* PSA_CRYPTO_SIZES_H */