blob: 33322472a745851319cd067946c71bf72361f7a9 [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)
Jaeden Amerod58a00d2019-06-07 11:49:59 +010048#include "mbedtls/config.h"
Gilles Peskine0cad07c2018-06-27 19:49:02 +020049#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
Gilles Peskine6c951442019-05-13 12:51:03 +0200193/** Bit size associated with an elliptic curve.
194 *
195 * \param curve An elliptic curve (value of type #psa_ecc_curve_t).
196 *
197 * \return The size associated with \p curve, in bits.
198 * This may be 0 if the implementation does not support
199 * the specified curve.
200 */
201#define PSA_ECC_CURVE_BITS(curve) \
202 ((curve) == PSA_ECC_CURVE_SECT163K1 ? 163 : \
203 (curve) == PSA_ECC_CURVE_SECT163R1 ? 163 : \
204 (curve) == PSA_ECC_CURVE_SECT163R2 ? 163 : \
205 (curve) == PSA_ECC_CURVE_SECT193R1 ? 193 : \
206 (curve) == PSA_ECC_CURVE_SECT193R2 ? 193 : \
207 (curve) == PSA_ECC_CURVE_SECT233K1 ? 233 : \
208 (curve) == PSA_ECC_CURVE_SECT233R1 ? 233 : \
209 (curve) == PSA_ECC_CURVE_SECT239K1 ? 239 : \
210 (curve) == PSA_ECC_CURVE_SECT283K1 ? 283 : \
211 (curve) == PSA_ECC_CURVE_SECT283R1 ? 283 : \
212 (curve) == PSA_ECC_CURVE_SECT409K1 ? 409 : \
213 (curve) == PSA_ECC_CURVE_SECT409R1 ? 409 : \
214 (curve) == PSA_ECC_CURVE_SECT571K1 ? 571 : \
215 (curve) == PSA_ECC_CURVE_SECT571R1 ? 571 : \
216 (curve) == PSA_ECC_CURVE_SECP160K1 ? 160 : \
217 (curve) == PSA_ECC_CURVE_SECP160R1 ? 160 : \
218 (curve) == PSA_ECC_CURVE_SECP160R2 ? 160 : \
219 (curve) == PSA_ECC_CURVE_SECP192K1 ? 192 : \
220 (curve) == PSA_ECC_CURVE_SECP192R1 ? 192 : \
221 (curve) == PSA_ECC_CURVE_SECP224K1 ? 224 : \
222 (curve) == PSA_ECC_CURVE_SECP224R1 ? 224 : \
223 (curve) == PSA_ECC_CURVE_SECP256K1 ? 256 : \
224 (curve) == PSA_ECC_CURVE_SECP256R1 ? 256 : \
225 (curve) == PSA_ECC_CURVE_SECP384R1 ? 384 : \
226 (curve) == PSA_ECC_CURVE_SECP521R1 ? 521 : \
227 (curve) == PSA_ECC_CURVE_BRAINPOOL_P256R1 ? 256 : \
228 (curve) == PSA_ECC_CURVE_BRAINPOOL_P384R1 ? 384 : \
229 (curve) == PSA_ECC_CURVE_BRAINPOOL_P512R1 ? 512 : \
230 (curve) == PSA_ECC_CURVE_CURVE25519 ? 255 : \
231 (curve) == PSA_ECC_CURVE_CURVE448 ? 448 : \
232 0)
233
Hanno Becker8dbfca42018-10-12 11:56:55 +0100234/** \def PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN
235 *
236 * This macro returns the maximum length of the PSK supported
237 * by the TLS-1.2 PSK-to-MS key derivation.
238 *
239 * Quoting RFC 4279, Sect 5.3:
240 * TLS implementations supporting these ciphersuites MUST support
241 * arbitrary PSK identities up to 128 octets in length, and arbitrary
242 * PSKs up to 64 octets in length. Supporting longer identities and
243 * keys is RECOMMENDED.
244 *
245 * Therefore, no implementation should define a value smaller than 64
246 * for #PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN.
247 */
248#define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN 128
249
Gilles Peskined911eb72018-08-14 15:18:45 +0200250/** The maximum size of a block cipher supported by the implementation. */
251#define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE 16
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200252
Gilles Peskineacd4be32018-07-08 19:56:25 +0200253/** The size of the output of psa_mac_sign_finish(), in bytes.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200254 *
Gilles Peskineacd4be32018-07-08 19:56:25 +0200255 * This is also the MAC size that psa_mac_verify_finish() expects.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200256 *
257 * \param key_type The type of the MAC key.
258 * \param key_bits The size of the MAC key in bits.
259 * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100260 * #PSA_ALG_IS_MAC(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200261 *
262 * \return The MAC size for the specified algorithm with
263 * the specified key parameters.
264 * \return 0 if the MAC algorithm is not recognized.
265 * \return Either 0 or the correct size for a MAC algorithm that
266 * the implementation recognizes, but does not support.
267 * \return Unspecified if the key parameters are not consistent
268 * with the algorithm.
269 */
270#define PSA_MAC_FINAL_SIZE(key_type, key_bits, alg) \
Gilles Peskined911eb72018-08-14 15:18:45 +0200271 ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
272 PSA_ALG_IS_HMAC(alg) ? PSA_HASH_SIZE(PSA_ALG_HMAC_GET_HASH(alg)) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200273 PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_SIZE(key_type) : \
Gilles Peskine35fe2032018-08-22 18:26:02 +0200274 ((void)(key_type), (void)(key_bits), 0))
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200275
276/** The maximum size of the output of psa_aead_encrypt(), in bytes.
277 *
278 * If the size of the ciphertext buffer is at least this large, it is
279 * guaranteed that psa_aead_encrypt() will not fail due to an
280 * insufficient buffer size. Depending on the algorithm, the actual size of
281 * the ciphertext may be smaller.
282 *
283 * \param alg An AEAD algorithm
284 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100285 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200286 * \param plaintext_length Size of the plaintext in bytes.
287 *
288 * \return The AEAD ciphertext size for the specified
289 * algorithm.
290 * If the AEAD algorithm is not recognized, return 0.
291 * An implementation may return either 0 or a
292 * correct size for an AEAD algorithm that it
293 * recognizes, but does not support.
294 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200295#define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(alg, plaintext_length) \
296 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
297 (plaintext_length) + PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200298 0)
299
300/** The maximum size of the output of psa_aead_decrypt(), in bytes.
301 *
302 * If the size of the plaintext buffer is at least this large, it is
303 * guaranteed that psa_aead_decrypt() will not fail due to an
304 * insufficient buffer size. Depending on the algorithm, the actual size of
305 * the plaintext may be smaller.
306 *
307 * \param alg An AEAD algorithm
308 * (\c PSA_ALG_XXX value such that
Gilles Peskine63f79302019-02-15 13:01:17 +0100309 * #PSA_ALG_IS_AEAD(\p alg) is true).
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200310 * \param ciphertext_length Size of the plaintext in bytes.
311 *
312 * \return The AEAD ciphertext size for the specified
313 * algorithm.
314 * If the AEAD algorithm is not recognized, return 0.
315 * An implementation may return either 0 or a
316 * correct size for an AEAD algorithm that it
317 * recognizes, but does not support.
318 */
Gilles Peskine23cc2ff2018-08-17 19:47:52 +0200319#define PSA_AEAD_DECRYPT_OUTPUT_SIZE(alg, ciphertext_length) \
320 (PSA_AEAD_TAG_LENGTH(alg) != 0 ? \
Gilles Peskine36d477d2019-05-14 16:09:22 +0200321 (ciphertext_length) - PSA_AEAD_TAG_LENGTH(alg) : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200322 0)
323
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200324/** A sufficient output buffer size for psa_aead_update().
325 *
326 * If the size of the output buffer is at least this large, it is
Gilles Peskineac99e322019-05-14 16:10:53 +0200327 * guaranteed that psa_aead_update() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200328 * insufficient buffer size. The actual size of the output may be smaller
329 * in any given call.
330 *
331 * \param alg An AEAD algorithm
332 * (\c PSA_ALG_XXX value such that
333 * #PSA_ALG_IS_AEAD(\p alg) is true).
334 * \param input_length Size of the input in bytes.
335 *
336 * \return A sufficient output buffer size for the specified
337 * algorithm.
338 * If the AEAD algorithm is not recognized, return 0.
339 * An implementation may return either 0 or a
340 * correct size for an AEAD algorithm that it
341 * recognizes, but does not support.
342 */
343/* For all the AEAD modes defined in this specification, it is possible
344 * to emit output without delay. However, hardware may not always be
345 * capable of this. So for modes based on a block cipher, allow the
346 * implementation to delay the output until it has a full block. */
Gilles Peskine248010c2019-05-14 16:08:59 +0200347#define PSA_AEAD_UPDATE_OUTPUT_SIZE(alg, input_length) \
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200348 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
Gilles Peskine248010c2019-05-14 16:08:59 +0200349 PSA_ROUND_UP_TO_MULTIPLE(PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE, (input_length)) : \
350 (input_length))
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200351
352/** A sufficient ciphertext buffer size for psa_aead_finish().
Gilles Peskinebdc27862019-05-06 15:45:16 +0200353 *
354 * If the size of the ciphertext buffer is at least this large, it is
355 * guaranteed that psa_aead_finish() will not fail due to an
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200356 * insufficient ciphertext buffer size. The actual size of the output may
357 * be smaller in any given call.
Gilles Peskinebdc27862019-05-06 15:45:16 +0200358 *
359 * \param alg An AEAD algorithm
360 * (\c PSA_ALG_XXX value such that
361 * #PSA_ALG_IS_AEAD(\p alg) is true).
362 *
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200363 * \return A sufficient ciphertext buffer size for the
Gilles Peskinebdc27862019-05-06 15:45:16 +0200364 * specified algorithm.
365 * If the AEAD algorithm is not recognized, return 0.
366 * An implementation may return either 0 or a
367 * correct size for an AEAD algorithm that it
368 * recognizes, but does not support.
369 */
Gilles Peskine49dd8d82019-05-06 15:16:19 +0200370#define PSA_AEAD_FINISH_OUTPUT_SIZE(alg) \
371 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
372 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
373 0)
374
375/** A sufficient plaintext buffer size for psa_aead_verify().
376 *
377 * If the size of the plaintext buffer is at least this large, it is
378 * guaranteed that psa_aead_verify() will not fail due to an
379 * insufficient plaintext buffer size. The actual size of the output may
380 * be smaller in any given call.
381 *
382 * \param alg An AEAD algorithm
383 * (\c PSA_ALG_XXX value such that
384 * #PSA_ALG_IS_AEAD(\p alg) is true).
385 *
386 * \return A sufficient plaintext buffer size for the
387 * specified algorithm.
388 * If the AEAD algorithm is not recognized, return 0.
389 * An implementation may return either 0 or a
390 * correct size for an AEAD algorithm that it
391 * recognizes, but does not support.
392 */
393#define PSA_AEAD_VERIFY_OUTPUT_SIZE(alg) \
394 (PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
395 PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE : \
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200396 0)
397
Jaeden Amero7f042142019-02-07 10:44:38 +0000398#define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
399 (PSA_ALG_IS_RSA_OAEP(alg) ? \
400 2 * PSA_HASH_SIZE(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
Gilles Peskinea7c26db2018-12-12 13:42:25 +0100401 11 /*PKCS#1v1.5*/)
402
403/**
404 * \brief ECDSA signature size for a given curve bit size
405 *
406 * \param curve_bits Curve size in bits.
407 * \return Signature size in bytes.
408 *
409 * \note This macro returns a compile-time constant if its argument is one.
410 */
411#define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
412 (PSA_BITS_TO_BYTES(curve_bits) * 2)
413
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200414/** Sufficient signature buffer size for psa_asymmetric_sign().
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200415 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200416 * This macro returns a sufficient buffer size for a signature using a key
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200417 * of the specified type and size, with the specified algorithm.
418 * Note that the actual size of the signature may be smaller
419 * (some algorithms produce a variable-size signature).
420 *
421 * \warning This function may call its arguments multiple times or
422 * zero times, so you should not pass arguments that contain
423 * side effects.
424 *
425 * \param key_type An asymmetric key type (this may indifferently be a
426 * key pair type or a public key type).
427 * \param key_bits The size of the key in bits.
428 * \param alg The signature algorithm.
429 *
430 * \return If the parameters are valid and supported, return
431 * a buffer size in bytes that guarantees that
432 * psa_asymmetric_sign() will not fail with
433 * #PSA_ERROR_BUFFER_TOO_SMALL.
434 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200435 * by the implementation, this macro shall return either a
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200436 * sensible size or 0.
437 * If the parameters are not valid, the
438 * return value is unspecified.
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200439 */
440#define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
441 (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
442 PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
443 ((void)alg, 0))
444
Gilles Peskine29755712019-11-08 15:49:40 +0100445#define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
446 PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
447
448/** \def PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE
449 *
450 * Maximum size of an asymmetric signature.
451 *
452 * This macro must expand to a compile-time constant integer. This value
453 * should be the maximum size of a signature supported by the implementation,
454 * in bytes, and must be no smaller than this maximum.
455 */
456#define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
457 (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
458 PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
459 PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
460
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200461/** Sufficient output buffer size for psa_asymmetric_encrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200462 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200463 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200464 * a key of the specified type and size, with the specified algorithm.
465 * Note that the actual size of the ciphertext may be smaller, depending
466 * on the algorithm.
467 *
468 * \warning This function may call its arguments multiple times or
469 * zero times, so you should not pass arguments that contain
470 * side effects.
471 *
472 * \param key_type An asymmetric key type (this may indifferently be a
473 * key pair type or a public key type).
474 * \param key_bits The size of the key in bits.
475 * \param alg The signature algorithm.
476 *
477 * \return If the parameters are valid and supported, return
478 * a buffer size in bytes that guarantees that
479 * psa_asymmetric_encrypt() will not fail with
480 * #PSA_ERROR_BUFFER_TOO_SMALL.
481 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200482 * by the implementation, this macro shall return either a
Gilles Peskinedcd14942018-07-12 00:30:52 +0200483 * sensible size or 0.
484 * If the parameters are not valid, the
485 * return value is unspecified.
486 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200487#define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
488 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
489 ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
490 0)
Gilles Peskinedcd14942018-07-12 00:30:52 +0200491
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200492/** Sufficient output buffer size for psa_asymmetric_decrypt().
Gilles Peskinedcd14942018-07-12 00:30:52 +0200493 *
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200494 * This macro returns a sufficient buffer size for a ciphertext produced using
Gilles Peskinedcd14942018-07-12 00:30:52 +0200495 * a key of the specified type and size, with the specified algorithm.
496 * Note that the actual size of the ciphertext may be smaller, depending
497 * on the algorithm.
498 *
499 * \warning This function may call its arguments multiple times or
500 * zero times, so you should not pass arguments that contain
501 * side effects.
502 *
503 * \param key_type An asymmetric key type (this may indifferently be a
504 * key pair type or a public key type).
505 * \param key_bits The size of the key in bits.
506 * \param alg The signature algorithm.
507 *
508 * \return If the parameters are valid and supported, return
509 * a buffer size in bytes that guarantees that
510 * psa_asymmetric_decrypt() will not fail with
511 * #PSA_ERROR_BUFFER_TOO_SMALL.
512 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200513 * by the implementation, this macro shall return either a
Gilles Peskinedcd14942018-07-12 00:30:52 +0200514 * sensible size or 0.
515 * If the parameters are not valid, the
516 * return value is unspecified.
517 */
Gilles Peskine49cee6c2018-06-27 21:03:58 +0200518#define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
519 (PSA_KEY_TYPE_IS_RSA(key_type) ? \
520 PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
521 0)
522
Gilles Peskine1be949b2018-08-10 19:06:59 +0200523/* Maximum size of the ASN.1 encoding of an INTEGER with the specified
524 * number of bits.
525 *
526 * This definition assumes that bits <= 2^19 - 9 so that the length field
527 * is at most 3 bytes. The length of the encoding is the length of the
528 * bit string padded to a whole number of bytes plus:
529 * - 1 type byte;
530 * - 1 to 3 length bytes;
531 * - 0 to 1 bytes of leading 0 due to the sign bit.
532 */
533#define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
534 ((bits) / 8 + 5)
535
536/* Maximum size of the export encoding of an RSA public key.
537 * Assumes that the public exponent is less than 2^32.
538 *
Gilles Peskine1be949b2018-08-10 19:06:59 +0200539 * RSAPublicKey ::= SEQUENCE {
540 * modulus INTEGER, -- n
541 * publicExponent INTEGER } -- e
542 *
Jaeden Amero25384a22019-01-10 10:23:21 +0000543 * - 4 bytes of SEQUENCE overhead;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200544 * - n : INTEGER;
545 * - 7 bytes for the public exponent.
546 */
547#define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Amero25384a22019-01-10 10:23:21 +0000548 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200549
550/* Maximum size of the export encoding of an RSA key pair.
551 * Assumes thatthe public exponent is less than 2^32 and that the size
552 * difference between the two primes is at most 1 bit.
553 *
554 * RSAPrivateKey ::= SEQUENCE {
555 * version Version, -- 0
556 * modulus INTEGER, -- N-bit
557 * publicExponent INTEGER, -- 32-bit
558 * privateExponent INTEGER, -- N-bit
559 * prime1 INTEGER, -- N/2-bit
560 * prime2 INTEGER, -- N/2-bit
561 * exponent1 INTEGER, -- N/2-bit
562 * exponent2 INTEGER, -- N/2-bit
563 * coefficient INTEGER, -- N/2-bit
564 * }
565 *
566 * - 4 bytes of SEQUENCE overhead;
567 * - 3 bytes of version;
568 * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
569 * overapproximated as 9 half-size INTEGERS;
570 * - 7 bytes for the public exponent.
571 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200572#define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200573 (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
574
575/* Maximum size of the export encoding of a DSA public key.
576 *
577 * SubjectPublicKeyInfo ::= SEQUENCE {
578 * algorithm AlgorithmIdentifier,
579 * subjectPublicKey BIT STRING } -- contains DSAPublicKey
580 * AlgorithmIdentifier ::= SEQUENCE {
581 * algorithm OBJECT IDENTIFIER,
582 * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
583 * DSAPublicKey ::= INTEGER -- public key, Y
584 *
585 * - 3 * 4 bytes of SEQUENCE overhead;
586 * - 1 + 1 + 7 bytes of algorithm (DSA OID);
587 * - 4 bytes of BIT STRING overhead;
588 * - 3 full-size INTEGERs (p, g, y);
589 * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
590 */
591#define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
592 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
593
594/* Maximum size of the export encoding of a DSA key pair.
595 *
596 * DSAPrivateKey ::= SEQUENCE {
597 * version Version, -- 0
598 * prime INTEGER, -- p
599 * subprime INTEGER, -- q
600 * generator INTEGER, -- g
601 * public INTEGER, -- y
602 * private INTEGER, -- x
603 * }
604 *
605 * - 4 bytes of SEQUENCE overhead;
606 * - 3 bytes of version;
607 * - 3 full-size INTEGERs (p, g, y);
608 * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
609 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200610#define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200611 (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
612
613/* Maximum size of the export encoding of an ECC public key.
614 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000615 * The representation of an ECC public key is:
616 * - The byte 0x04;
617 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
618 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
619 * - where m is the bit size associated with the curve.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200620 *
Jaeden Ameroccdce902019-01-10 11:42:27 +0000621 * - 1 byte + 2 * point size.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200622 */
623#define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
Jaeden Ameroccdce902019-01-10 11:42:27 +0000624 (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
Gilles Peskine1be949b2018-08-10 19:06:59 +0200625
626/* Maximum size of the export encoding of an ECC key pair.
627 *
Gilles Peskine5eb15212018-10-31 13:24:35 +0100628 * An ECC key pair is represented by the secret value.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200629 */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200630#define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
Gilles Peskine5eb15212018-10-31 13:24:35 +0100631 (PSA_BITS_TO_BYTES(key_bits))
Gilles Peskine1be949b2018-08-10 19:06:59 +0200632
Gilles Peskine56e2dc82019-05-21 15:59:56 +0200633/** Sufficient output buffer size for psa_export_key() or psa_export_public_key().
Gilles Peskine1be949b2018-08-10 19:06:59 +0200634 *
635 * This macro returns a compile-time constant if its arguments are
636 * compile-time constants.
637 *
638 * \warning This function may call its arguments multiple times or
639 * zero times, so you should not pass arguments that contain
640 * side effects.
641 *
642 * The following code illustrates how to allocate enough memory to export
643 * a key by querying the key type and size at runtime.
644 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200645 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200646 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200647 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200648 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200649 * psa_key_type_t key_type = psa_get_key_type(&attributes);
650 * size_t key_bits = psa_get_key_bits(&attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200651 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200652 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200653 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200654 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200655 * size_t buffer_length;
656 * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
657 * if (status != PSA_SUCCESS) handle_error(...);
658 * \endcode
659 *
660 * For psa_export_public_key(), calculate the buffer size from the
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200661 * public key type. You can use the macro #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR
Gilles Peskine1be949b2018-08-10 19:06:59 +0200662 * to convert a key pair type to the corresponding public key type.
663 * \code{c}
Gilles Peskined7d43b92019-05-21 15:56:03 +0200664 * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine1be949b2018-08-10 19:06:59 +0200665 * psa_status_t status;
Gilles Peskined7d43b92019-05-21 15:56:03 +0200666 * status = psa_get_key_attributes(key, &attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200667 * if (status != PSA_SUCCESS) handle_error(...);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200668 * psa_key_type_t key_type = psa_get_key_type(&attributes);
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200669 * psa_key_type_t public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200670 * size_t key_bits = psa_get_key_bits(&attributes);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200671 * size_t buffer_size = PSA_KEY_EXPORT_MAX_SIZE(public_key_type, key_bits);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200672 * psa_reset_key_attributes(&attributes);
Gilles Peskinef82088a2019-07-15 11:07:38 +0200673 * uint8_t *buffer = malloc(buffer_size);
Gilles Peskined7d43b92019-05-21 15:56:03 +0200674 * if (buffer == NULL) handle_error(...);
Gilles Peskine1be949b2018-08-10 19:06:59 +0200675 * size_t buffer_length;
676 * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
677 * if (status != PSA_SUCCESS) handle_error(...);
678 * \endcode
679 *
680 * \param key_type A supported key type.
681 * \param key_bits The size of the key in bits.
Gilles Peskine1be949b2018-08-10 19:06:59 +0200682 *
683 * \return If the parameters are valid and supported, return
684 * a buffer size in bytes that guarantees that
685 * psa_asymmetric_sign() will not fail with
686 * #PSA_ERROR_BUFFER_TOO_SMALL.
687 * If the parameters are a valid combination that is not supported
Gilles Peskine27a983d2019-05-16 17:24:53 +0200688 * by the implementation, this macro shall return either a
Gilles Peskine1be949b2018-08-10 19:06:59 +0200689 * sensible size or 0.
690 * If the parameters are not valid, the
691 * return value is unspecified.
692 */
693#define PSA_KEY_EXPORT_MAX_SIZE(key_type, key_bits) \
694 (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200695 (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200696 (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200697 (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200698 (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200699 PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
Gilles Peskine1be949b2018-08-10 19:06:59 +0200700 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
701 0)
702
Gilles Peskine0cad07c2018-06-27 19:49:02 +0200703#endif /* PSA_CRYPTO_SIZES_H */