blob: 8002132a3fe8e85fc1cc16e0b4cf8d9009999e41 [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
Gilles Peskine248010c2019-05-14 16:08:59 +020056#define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
57 (((length) + (block_size) - 1) / (block_size) * (block_size))
58
Gilles Peskinea7c26db2018-12-12 13:42:25 +010059/** The size of the output of psa_hash_finish(), in bytes.
60 *
61 * This is also the hash size that psa_hash_verify() expects.
62 *
63 * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
64 * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
65 * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
66 * hash algorithm).
67 *
68 * \return The hash size for the specified hash algorithm.
69 * If the hash algorithm is not recognized, return 0.
70 * An implementation may return either 0 or the correct size
71 * for a hash algorithm that it recognizes, but does not support.
72 */
73#define PSA_HASH_SIZE(alg) \
74 ( \
75 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD2 ? 16 : \
76 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD4 ? 16 : \
77 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
78 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
79 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
80 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
81 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
82 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
83 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
84 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
85 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
86 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
87 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
88 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
89 PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
90 0)
91
Gilles Peskineaf3baab2018-06-27 22:55:52 +020092/** \def PSA_HASH_MAX_SIZE
93 *
94 * Maximum size of a hash.
95 *
96 * This macro must expand to a compile-time constant integer. This value
97 * should be the maximum size of a hash supported by the implementation,
98 * in bytes, and must be no smaller than this maximum.
99 */
Gilles Peskine3052f532018-09-17 14:13:26 +0200100/* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
101 * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
102 * HMAC-SHA3-512. */
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200103#if defined(MBEDTLS_SHA512_C)
104#define PSA_HASH_MAX_SIZE 64
105#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
106#else
107#define PSA_HASH_MAX_SIZE 32
108#define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
109#endif
110
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200111/** \def PSA_MAC_MAX_SIZE
112 *
113 * Maximum size of a MAC.
114 *
115 * This macro must expand to a compile-time constant integer. This value
116 * should be the maximum size of a MAC supported by the implementation,
117 * in bytes, and must be no smaller than this maximum.
118 */
119/* All non-HMAC MACs have a maximum size that's smaller than the
120 * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
Gilles Peskinee1f2d7d2018-08-21 14:54:54 +0200121/* Note that the encoding of truncated MAC algorithms limits this value
122 * to 64 bytes.
123 */
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200124#define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
125
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100126/** The tag size for an AEAD algorithm, in bytes.
127 *
128 * \param alg An AEAD algorithm
129 * (\c PSA_ALG_XXX value such that
130 * #PSA_ALG_IS_AEAD(\p alg) is true).
131 *
132 * \return The tag size for the specified algorithm.
133 * If the AEAD algorithm does not have an identified
134 * tag that can be distinguished from the rest of
135 * the ciphertext, return 0.
136 * If the AEAD algorithm is not recognized, return 0.
137 * An implementation may return either 0 or a
138 * correct size for an AEAD algorithm that it
139 * recognizes, but does not support.
140 */
141#define PSA_AEAD_TAG_LENGTH(alg) \
142 (PSA_ALG_IS_AEAD(alg) ? \
143 (((alg) & PSA_ALG_AEAD_TAG_LENGTH_MASK) >> PSA_AEAD_TAG_LENGTH_OFFSET) : \
144 0)
145
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200146/* The maximum size of an RSA key on this implementation, in bits.
147 * This is a vendor-specific macro.
148 *
149 * Mbed TLS does not set a hard limit on the size of RSA keys: any key
150 * whose parameters fit in a bignum is accepted. However large keys can
151 * induce a large memory usage and long computation times. Unlike other
152 * auxiliary macros in this file and in crypto.h, which reflect how the
153 * library is configured, this macro defines how the library is
154 * configured. This implementation refuses to import or generate an
155 * RSA key whose size is larger than the value defined here.
156 *
157 * Note that an implementation may set different size limits for different
158 * operations, and does not need to accept all key sizes up to the limit. */
159#define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
160
161/* The maximum size of an ECC key on this implementation, in bits.
162 * This is a vendor-specific macro. */
163#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
164#define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
165#elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
166#define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
167#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
168#define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
169#elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
170#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
171#elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
172#define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
173#elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
174#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
175#elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
176#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
177#elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
178#define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
179#elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
180#define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
181#elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
182#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
183#elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
184#define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
185#elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
186#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
187#elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
188#define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
189#else
190#define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
191#endif
192
Hanno Becker8dbfca42018-10-12 11:56:55 +0100193/** \def PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN
194 *
195 * This macro returns the maximum length of the PSK supported
196 * by the TLS-1.2 PSK-to-MS key derivation.
197 *
198 * Quoting RFC 4279, Sect 5.3:
199 * TLS implementations supporting these ciphersuites MUST support
200 * arbitrary PSK identities up to 128 octets in length, and arbitrary
201 * PSKs up to 64 octets in length. Supporting longer identities and
202 * keys is RECOMMENDED.
203 *
204 * Therefore, no implementation should define a value smaller than 64
205 * for #PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN.
206 */
207#define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN 128
208
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200209/** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
210 *
211 * Maximum size of an asymmetric signature.
212 *
213 * This macro must expand to a compile-time constant integer. This value
214 * should be the maximum size of a MAC supported by the implementation,
215 * in bytes, and must be no smaller than this maximum.
216 */
217#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
218 PSA_BITS_TO_BYTES( \
219 PSA_VENDOR_RSA_MAX_KEY_BITS > PSA_VENDOR_ECC_MAX_CURVE_BITS ? \
220 PSA_VENDOR_RSA_MAX_KEY_BITS : \
221 PSA_VENDOR_ECC_MAX_CURVE_BITS \
222 )
223
Gilles Peskined911eb72018-08-14 15:18:45 +0200224/** The maximum size of a block cipher supported by the implementation. */
225#define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200226
Gilles Peskineacd4be32018-07-08 19:56:25 +0200227/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200228 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200229 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200230 *
231 * \param key_type The type of the MAC key.
232 * \param key_bits The size of the MAC key in bits.
233 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100234 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200235 *
236 * \return The MAC size for the specified algorithm with
237 * the specified key parameters.
238 * \return 0 if the MAC algorithm is not recognized.
239 * \return Either 0 or the correct size for a MAC algorithm that
240 * the implementation recognizes, but does not support.
241 * \return Unspecified if the key parameters are not consistent
242 * with the algorithm.
243 */
244#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Gilles Peskined911eb72018-08-14 15:18:45 +0200245 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
246 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200247 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200248 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200249
250/** The maximum size of the output of psa_aead_encrypt(), in bytes.
251 *
252 * If the size of the ciphertext buffer is at least this large, it is
253 * guaranteed that psa_aead_encrypt() will not fail due to an
254 * insufficient buffer size. Depending on the algorithm, the actual size of
255 * the ciphertext may be smaller.
256 *
257 * \param alg An AEAD algorithm
258 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100259 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200260 * \param plaintext_length Size of the plaintext in bytes.
261 *
262 * \return The AEAD ciphertext size for the specified
263 * algorithm.
264 * If the AEAD algorithm is not recognized, return 0.
265 * An implementation may return either 0 or a
266 * correct size for an AEAD algorithm that it
267 * recognizes, but does not support.
268 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200269#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
270 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
271 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200272 0)
273
274/** The maximum size of the output of psa_aead_decrypt(), in bytes.
275 *
276 * If the size of the plaintext buffer is at least this large, it is
277 * guaranteed that psa_aead_decrypt() will not fail due to an
278 * insufficient buffer size. Depending on the algorithm, the actual size of
279 * the plaintext may be smaller.
280 *
281 * \param alg An AEAD algorithm
282 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100283 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200284 * \param ciphertext_length Size of the plaintext in bytes.
285 *
286 * \return The AEAD ciphertext size for the specified
287 * algorithm.
288 * If the AEAD algorithm is not recognized, return 0.
289 * An implementation may return either 0 or a
290 * correct size for an AEAD algorithm that it
291 * recognizes, but does not support.
292 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200293#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
294 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
295 (plaintext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200296 0)
297
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200298/** A sufficient output buffer size for psa_aead_update().
299 *
300 * If the size of the output buffer is at least this large, it is
301 * guaranteed that psa_aead_finish() will not fail due to an
302 * insufficient buffer size. The actual size of the output may be smaller
303 * in any given call.
304 *
305 * \param alg An AEAD algorithm
306 * (\c PSA_ALG_XXX value such that
307 * #PSA_ALG_IS_AEAD(\p alg) is true).
308 * \param input_length Size of the input in bytes.
309 *
310 * \return A sufficient output buffer size for the specified
311 * algorithm.
312 * If the AEAD algorithm is not recognized, return 0.
313 * An implementation may return either 0 or a
314 * correct size for an AEAD algorithm that it
315 * recognizes, but does not support.
316 */
317/* For all the AEAD modes defined in this specification, it is possible
318 * to emit output without delay. However, hardware may not always be
319 * capable of this. So for modes based on a block cipher, allow the
320 * implementation to delay the output until it has a full block. */
Gilles Peskine248010c2019-05-14 16:08:59 +0200321#define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200322 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
Gilles Peskine248010c2019-05-14 16:08:59 +0200323 PSA_ROUND_UP_TO_MULTIPLE(PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE, (input_length)) : \
324 (input_length))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200325
326/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200327 *
328 * If the size of the ciphertext buffer is at least this large, it is
329 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200330 * insufficient ciphertext buffer size. The actual size of the output may
331 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200332 *
333 * \param alg An AEAD algorithm
334 * (\c PSA_ALG_XXX value such that
335 * #PSA_ALG_IS_AEAD(\p alg) is true).
336 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200337 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200338 * specified algorithm.
339 * If the AEAD algorithm is not recognized, return 0.
340 * An implementation may return either 0 or a
341 * correct size for an AEAD algorithm that it
342 * recognizes, but does not support.
343 */
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200344#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
345 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
346 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
347 0)
348
349/** A sufficient plaintext buffer size for psa_aead_verify().
350 *
351 * If the size of the plaintext buffer is at least this large, it is
352 * guaranteed that psa_aead_verify() will not fail due to an
353 * insufficient plaintext buffer size. The actual size of the output may
354 * be smaller in any given call.
355 *
356 * \param alg An AEAD algorithm
357 * (\c PSA_ALG_XXX value such that
358 * #PSA_ALG_IS_AEAD(\p alg) is true).
359 *
360 * \return A sufficient plaintext buffer size for the
361 * specified algorithm.
362 * If the AEAD algorithm is not recognized, return 0.
363 * An implementation may return either 0 or a
364 * correct size for an AEAD algorithm that it
365 * recognizes, but does not support.
366 */
367#define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \
368 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
369 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
370 0)
Gilles Peskinebdc27862019-05-06 15:45:16 +0200371
Jaeden Amero7f042142019-02-07 10:44:38 +0000372#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
373 (PSA_ALG_IS_RSA_OAEP(alg) ? \
374 2 * PSA_HASH_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100375 11 /*PKCS#1v1.5*/)
376
377/**
378 * \brief ECDSA signature size for a given curve bit size
379 *
380 * \param curve_bits Curve size in bits.
381 * \return Signature size in bytes.
382 *
383 * \note This macro returns a compile-time constant if its argument is one.
384 */
385#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
386 (PSA_BITS_TO_BYTES(curve_bits) * 2)
387
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200388/** Safe signature buffer size for psa_asymmetric_sign().
389 *
390 * This macro returns a safe buffer size for a signature using a key
391 * of the specified type and size, with the specified algorithm.
392 * Note that the actual size of the signature may be smaller
393 * (some algorithms produce a variable-size signature).
394 *
395 * \warning This function may call its arguments multiple times or
396 * zero times, so you should not pass arguments that contain
397 * side effects.
398 *
399 * \param key_type An asymmetric key type (this may indifferently be a
400 * key pair type or a public key type).
401 * \param key_bits The size of the key in bits.
402 * \param alg The signature algorithm.
403 *
404 * \return If the parameters are valid and supported, return
405 * a buffer size in bytes that guarantees that
406 * psa_asymmetric_sign() will not fail with
407 * #PSA_ERROR_BUFFER_TOO_SMALL.
408 * If the parameters are a valid combination that is not supported
409 * by the implementation, this macro either shall return either a
410 * sensible size or 0.
411 * If the parameters are not valid, the
412 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200413 */
414#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
415 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
416 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
417 ((void)alg, 0))
418
Gilles Peskinedcd14942018-07-12 00:30:52 +0200419/** Safe output buffer size for psa_asymmetric_encrypt().
420 *
421 * This macro returns a safe buffer size for a ciphertext produced using
422 * a key of the specified type and size, with the specified algorithm.
423 * Note that the actual size of the ciphertext may be smaller, depending
424 * on the algorithm.
425 *
426 * \warning This function may call its arguments multiple times or
427 * zero times, so you should not pass arguments that contain
428 * side effects.
429 *
430 * \param key_type An asymmetric key type (this may indifferently be a
431 * key pair type or a public key type).
432 * \param key_bits The size of the key in bits.
433 * \param alg The signature algorithm.
434 *
435 * \return If the parameters are valid and supported, return
436 * a buffer size in bytes that guarantees that
437 * psa_asymmetric_encrypt() will not fail with
438 * #PSA_ERROR_BUFFER_TOO_SMALL.
439 * If the parameters are a valid combination that is not supported
440 * by the implementation, this macro either shall return either a
441 * sensible size or 0.
442 * If the parameters are not valid, the
443 * return value is unspecified.
444 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200445#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
446 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
447 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
448 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200449
450/** Safe output buffer size for psa_asymmetric_decrypt().
451 *
452 * This macro returns a safe buffer size for a ciphertext produced using
453 * a key of the specified type and size, with the specified algorithm.
454 * Note that the actual size of the ciphertext may be smaller, depending
455 * on the algorithm.
456 *
457 * \warning This function may call its arguments multiple times or
458 * zero times, so you should not pass arguments that contain
459 * side effects.
460 *
461 * \param key_type An asymmetric key type (this may indifferently be a
462 * key pair type or a public key type).
463 * \param key_bits The size of the key in bits.
464 * \param alg The signature algorithm.
465 *
466 * \return If the parameters are valid and supported, return
467 * a buffer size in bytes that guarantees that
468 * psa_asymmetric_decrypt() will not fail with
469 * #PSA_ERROR_BUFFER_TOO_SMALL.
470 * If the parameters are a valid combination that is not supported
471 * by the implementation, this macro either shall return either a
472 * sensible size or 0.
473 * If the parameters are not valid, the
474 * return value is unspecified.
475 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200476#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
477 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
478 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
479 0)
480
Gilles Peskine1be949b2018-08-10 19:06:59 +0200481/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
482 * number of bits.
483 *
484 * This definition assumes that bits <= 2^19 - 9 so that the length field
485 * is at most 3 bytes. The length of the encoding is the length of the
486 * bit string padded to a whole number of bytes plus:
487 * - 1 type byte;
488 * - 1 to 3 length bytes;
489 * - 0 to 1 bytes of leading 0 due to the sign bit.
490 */
491#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
492 ((bits) / 8 + 5)
493
494/* Maximum size of the export encoding of an RSA public key.
495 * Assumes that the public exponent is less than 2^32.
496 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200497 * RSAPublicKey ::= SEQUENCE {
498 * modulus INTEGER, -- n
499 * publicExponent INTEGER } -- e
500 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000501 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200502 * - n : INTEGER;
503 * - 7 bytes for the public exponent.
504 */
505#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000506 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200507
508/* Maximum size of the export encoding of an RSA key pair.
509 * Assumes thatthe public exponent is less than 2^32 and that the size
510 * difference between the two primes is at most 1 bit.
511 *
512 * RSAPrivateKey ::= SEQUENCE {
513 * version Version, -- 0
514 * modulus INTEGER, -- N-bit
515 * publicExponent INTEGER, -- 32-bit
516 * privateExponent INTEGER, -- N-bit
517 * prime1 INTEGER, -- N/2-bit
518 * prime2 INTEGER, -- N/2-bit
519 * exponent1 INTEGER, -- N/2-bit
520 * exponent2 INTEGER, -- N/2-bit
521 * coefficient INTEGER, -- N/2-bit
522 * }
523 *
524 * - 4 bytes of SEQUENCE overhead;
525 * - 3 bytes of version;
526 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
527 * overapproximated as 9 half-size INTEGERS;
528 * - 7 bytes for the public exponent.
529 */
530#define PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) \
531 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
532
533/* Maximum size of the export encoding of a DSA public key.
534 *
535 * SubjectPublicKeyInfo ::= SEQUENCE {
536 * algorithm AlgorithmIdentifier,
537 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
538 * AlgorithmIdentifier ::= SEQUENCE {
539 * algorithm OBJECT IDENTIFIER,
540 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
541 * DSAPublicKey ::= INTEGER -- public key, Y
542 *
543 * - 3 * 4 bytes of SEQUENCE overhead;
544 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
545 * - 4 bytes of BIT STRING overhead;
546 * - 3 full-size INTEGERs (p, g, y);
547 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
548 */
549#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
550 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
551
552/* Maximum size of the export encoding of a DSA key pair.
553 *
554 * DSAPrivateKey ::= SEQUENCE {
555 * version Version, -- 0
556 * prime INTEGER, -- p
557 * subprime INTEGER, -- q
558 * generator INTEGER, -- g
559 * public INTEGER, -- y
560 * private INTEGER, -- x
561 * }
562 *
563 * - 4 bytes of SEQUENCE overhead;
564 * - 3 bytes of version;
565 * - 3 full-size INTEGERs (p, g, y);
566 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
567 */
568#define PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) \
569 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
570
571/* Maximum size of the export encoding of an ECC public key.
572 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000573 * The representation of an ECC public key is:
574 * - The byte 0x04;
575 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
576 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
577 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200578 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000579 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200580 */
581#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000582 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200583
584/* Maximum size of the export encoding of an ECC key pair.
585 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100586 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200587 */
588#define PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100589 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200590
591/** Safe output buffer size for psa_export_key() or psa_export_public_key().
592 *
593 * This macro returns a compile-time constant if its arguments are
594 * compile-time constants.
595 *
596 * \warning This function may call its arguments multiple times or
597 * zero times, so you should not pass arguments that contain
598 * side effects.
599 *
600 * The following code illustrates how to allocate enough memory to export
601 * a key by querying the key type and size at runtime.
602 * \code{c}
603 * psa_key_type_t key_type;
604 * size_t key_bits;
605 * psa_status_t status;
606 * status = psa_get_key_information(key, &key_type, &key_bits);
607 * if (status != PSA_SUCCESS) handle_error(...);
608 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
609 * unsigned char *buffer = malloc(buffer_size);
610 * if (buffer != NULL) handle_error(...);
611 * size_t buffer_length;
612 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
613 * if (status != PSA_SUCCESS) handle_error(...);
614 * \endcode
615 *
616 * For psa_export_public_key(), calculate the buffer size from the
617 * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR
618 * to convert a key pair type to the corresponding public key type.
619 * \code{c}
620 * psa_key_type_t key_type;
621 * size_t key_bits;
622 * psa_status_t status;
623 * status = psa_get_key_information(key, &key_type, &key_bits);
624 * if (status != PSA_SUCCESS) handle_error(...);
625 * psa_key_type_t public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEYPAIR(key_type);
626 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits);
627 * unsigned char *buffer = malloc(buffer_size);
628 * if (buffer != NULL) handle_error(...);
629 * size_t buffer_length;
630 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
631 * if (status != PSA_SUCCESS) handle_error(...);
632 * \endcode
633 *
634 * \param key_type A supported key type.
635 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200636 *
637 * \return If the parameters are valid and supported, return
638 * a buffer size in bytes that guarantees that
639 * psa_asymmetric_sign() will not fail with
640 * #PSA_ERROR_BUFFER_TOO_SMALL.
641 * If the parameters are a valid combination that is not supported
642 * by the implementation, this macro either shall return either a
643 * sensible size or 0.
644 * If the parameters are not valid, the
645 * return value is unspecified.
646 */
647#define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \
648 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
649 (key_type) == PSA_KEY_TYPE_RSA_KEYPAIR ? PSA_KEY_EXPORT_RSA_KEYPAIR_MAX_SIZE(key_bits) : \
650 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
651 (key_type) == PSA_KEY_TYPE_DSA_KEYPAIR ? PSA_KEY_EXPORT_DSA_KEYPAIR_MAX_SIZE(key_bits) : \
652 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
653 PSA_KEY_TYPE_IS_ECC_KEYPAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEYPAIR_MAX_SIZE(key_bits) : \
654 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
655 0)
656
Gilles Peskineaa02c172019-04-28 11:44:17 +0200657/** Safe output buffer size for psa_get_key_domain_parameters().
658 *
659 * This macro returns a compile-time constant if its arguments are
660 * compile-time constants.
661 *
662 * \warning This function may call its arguments multiple times or
663 * zero times, so you should not pass arguments that contain
664 * side effects.
665 *
666 * \param key_type A supported key type.
667 * \param key_bits The size of the key in bits.
668 *
669 * \return If the parameters are valid and supported, return
670 * a buffer size in bytes that guarantees that
671 * psa_get_key_domain_parameters() will not fail with
672 * #PSA_ERROR_BUFFER_TOO_SMALL.
673 * If the parameters are a valid combination that is not supported
674 * by the implementation, this macro either shall return either a
675 * sensible size or 0.
676 * If the parameters are not valid, the
677 * return value is unspecified.
678 */
679#define PSA_KEY_DOMAIN_PARAMETERS_SIZE(key_type, key_bits) \
680 (PSA_KEY_TYPE_IS_RSA(key_type) ? sizeof(int) : \
681 PSA_KEY_TYPE_IS_DH(key_type) ? PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
682 PSA_KEY_TYPE_IS_DSA(key_type) ? PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) : \
683 0)
684#define PSA_DH_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
685 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 3 /*without optional parts*/)
686#define PSA_DSA_KEY_DOMAIN_PARAMETERS_SIZE(key_bits) \
687 (4 + (PSA_BITS_TO_BYTES(key_bits) + 5) * 2 /*p, g*/ + 34 /*q*/)
688
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200689#endif /* PSA_CRYPTO_SIZES_H */