2
0

crypto_sizes.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /**
  2. * \file psa/crypto_sizes.h
  3. *
  4. * \brief PSA cryptography module: Mbed TLS buffer size macros
  5. *
  6. * \note This file may not be included directly. Applications must
  7. * include psa/crypto.h.
  8. *
  9. * 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.
  14. *
  15. * 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. *
  20. * Macros that compute sizes whose values do not depend on the
  21. * implementation are in crypto.h.
  22. */
  23. /*
  24. * Copyright The Mbed TLS Contributors
  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. #ifndef PSA_CRYPTO_SIZES_H
  40. #define PSA_CRYPTO_SIZES_H
  41. /* Include the Mbed TLS configuration file, the way Mbed TLS does it
  42. * in each of its header files. */
  43. #include "mbedtls/build_info.h"
  44. #define PSA_BITS_TO_BYTES(bits) (((bits) + 7) / 8)
  45. #define PSA_BYTES_TO_BITS(bytes) ((bytes) * 8)
  46. #define PSA_ROUND_UP_TO_MULTIPLE(block_size, length) \
  47. (((length) + (block_size) - 1) / (block_size) * (block_size))
  48. /** The size of the output of psa_hash_finish(), in bytes.
  49. *
  50. * This is also the hash size that psa_hash_verify() expects.
  51. *
  52. * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
  53. * #PSA_ALG_IS_HASH(\p alg) is true), or an HMAC algorithm
  54. * (#PSA_ALG_HMAC(\c hash_alg) where \c hash_alg is a
  55. * hash algorithm).
  56. *
  57. * \return The hash size for the specified hash algorithm.
  58. * If the hash algorithm is not recognized, return 0.
  59. */
  60. #define PSA_HASH_LENGTH(alg) \
  61. ( \
  62. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 16 : \
  63. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 20 : \
  64. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 20 : \
  65. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 28 : \
  66. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 32 : \
  67. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 48 : \
  68. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 64 : \
  69. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 28 : \
  70. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 32 : \
  71. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 28 : \
  72. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 32 : \
  73. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 48 : \
  74. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 64 : \
  75. 0)
  76. /** The input block size of a hash algorithm, in bytes.
  77. *
  78. * Hash algorithms process their input data in blocks. Hash operations will
  79. * retain any partial blocks until they have enough input to fill the block or
  80. * until the operation is finished.
  81. * This affects the output from psa_hash_suspend().
  82. *
  83. * \param alg A hash algorithm (\c PSA_ALG_XXX value such that
  84. * PSA_ALG_IS_HASH(\p alg) is true).
  85. *
  86. * \return The block size in bytes for the specified hash algorithm.
  87. * If the hash algorithm is not recognized, return 0.
  88. * An implementation can return either 0 or the correct size for a
  89. * hash algorithm that it recognizes, but does not support.
  90. */
  91. #define PSA_HASH_BLOCK_LENGTH(alg) \
  92. ( \
  93. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_MD5 ? 64 : \
  94. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_RIPEMD160 ? 64 : \
  95. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_1 ? 64 : \
  96. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_224 ? 64 : \
  97. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_256 ? 64 : \
  98. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_384 ? 128 : \
  99. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512 ? 128 : \
  100. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_224 ? 128 : \
  101. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA_512_256 ? 128 : \
  102. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_224 ? 144 : \
  103. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_256 ? 136 : \
  104. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_384 ? 104 : \
  105. PSA_ALG_HMAC_GET_HASH(alg) == PSA_ALG_SHA3_512 ? 72 : \
  106. 0)
  107. /** \def PSA_HASH_MAX_SIZE
  108. *
  109. * Maximum size of a hash.
  110. *
  111. * This macro expands to a compile-time constant integer. This value
  112. * is the maximum size of a hash in bytes.
  113. */
  114. /* Note: for HMAC-SHA-3, the block size is 144 bytes for HMAC-SHA3-226,
  115. * 136 bytes for HMAC-SHA3-256, 104 bytes for SHA3-384, 72 bytes for
  116. * HMAC-SHA3-512. */
  117. #if defined(MBEDTLS_SHA512_C)
  118. #define PSA_HASH_MAX_SIZE 64
  119. #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 128
  120. #else
  121. #define PSA_HASH_MAX_SIZE 32
  122. #define PSA_HMAC_MAX_HASH_BLOCK_SIZE 64
  123. #endif
  124. /** \def PSA_MAC_MAX_SIZE
  125. *
  126. * Maximum size of a MAC.
  127. *
  128. * This macro expands to a compile-time constant integer. This value
  129. * is the maximum size of a MAC in bytes.
  130. */
  131. /* All non-HMAC MACs have a maximum size that's smaller than the
  132. * minimum possible value of PSA_HASH_MAX_SIZE in this implementation. */
  133. /* Note that the encoding of truncated MAC algorithms limits this value
  134. * to 64 bytes.
  135. */
  136. #define PSA_MAC_MAX_SIZE PSA_HASH_MAX_SIZE
  137. /** The length of a tag for an AEAD algorithm, in bytes.
  138. *
  139. * This macro can be used to allocate a buffer of sufficient size to store the
  140. * tag output from psa_aead_finish().
  141. *
  142. * See also #PSA_AEAD_TAG_MAX_SIZE.
  143. *
  144. * \param key_type The type of the AEAD key.
  145. * \param key_bits The size of the AEAD key in bits.
  146. * \param alg An AEAD algorithm
  147. * (\c PSA_ALG_XXX value such that
  148. * #PSA_ALG_IS_AEAD(\p alg) is true).
  149. *
  150. * \return The tag length for the specified algorithm and key.
  151. * If the AEAD algorithm does not have an identified
  152. * tag that can be distinguished from the rest of
  153. * the ciphertext, return 0.
  154. * If the key type or AEAD algorithm is not
  155. * recognized, or the parameters are incompatible,
  156. * return 0.
  157. */
  158. #define PSA_AEAD_TAG_LENGTH(key_type, key_bits, alg) \
  159. (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
  160. PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
  161. ((void) (key_bits), 0))
  162. /** The maximum tag size for all supported AEAD algorithms, in bytes.
  163. *
  164. * See also #PSA_AEAD_TAG_LENGTH(\p key_type, \p key_bits, \p alg).
  165. */
  166. #define PSA_AEAD_TAG_MAX_SIZE 16
  167. /* The maximum size of an RSA key on this implementation, in bits.
  168. * This is a vendor-specific macro.
  169. *
  170. * Mbed TLS does not set a hard limit on the size of RSA keys: any key
  171. * whose parameters fit in a bignum is accepted. However large keys can
  172. * induce a large memory usage and long computation times. Unlike other
  173. * auxiliary macros in this file and in crypto.h, which reflect how the
  174. * library is configured, this macro defines how the library is
  175. * configured. This implementation refuses to import or generate an
  176. * RSA key whose size is larger than the value defined here.
  177. *
  178. * Note that an implementation may set different size limits for different
  179. * operations, and does not need to accept all key sizes up to the limit. */
  180. #define PSA_VENDOR_RSA_MAX_KEY_BITS 4096
  181. /* The maximum size of an ECC key on this implementation, in bits.
  182. * This is a vendor-specific macro. */
  183. #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
  184. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 521
  185. #elif defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
  186. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 512
  187. #elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
  188. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 448
  189. #elif defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
  190. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
  191. #elif defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
  192. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 384
  193. #elif defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
  194. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
  195. #elif defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
  196. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
  197. #elif defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
  198. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 256
  199. #elif defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
  200. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 255
  201. #elif defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
  202. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
  203. #elif defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
  204. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 224
  205. #elif defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
  206. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
  207. #elif defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
  208. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 192
  209. #else
  210. #define PSA_VENDOR_ECC_MAX_CURVE_BITS 0
  211. #endif
  212. /** This macro returns the maximum supported length of the PSK for the
  213. * TLS-1.2 PSK-to-MS key derivation
  214. * (#PSA_ALG_TLS12_PSK_TO_MS(\c hash_alg)).
  215. *
  216. * The maximum supported length does not depend on the chosen hash algorithm.
  217. *
  218. * Quoting RFC 4279, Sect 5.3:
  219. * TLS implementations supporting these ciphersuites MUST support
  220. * arbitrary PSK identities up to 128 octets in length, and arbitrary
  221. * PSKs up to 64 octets in length. Supporting longer identities and
  222. * keys is RECOMMENDED.
  223. *
  224. * Therefore, no implementation should define a value smaller than 64
  225. * for #PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE.
  226. */
  227. #define PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE 128
  228. /** The maximum size of a block cipher. */
  229. #define PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE 16
  230. /** The size of the output of psa_mac_sign_finish(), in bytes.
  231. *
  232. * This is also the MAC size that psa_mac_verify_finish() expects.
  233. *
  234. * \warning This macro may evaluate its arguments multiple times or
  235. * zero times, so you should not pass arguments that contain
  236. * side effects.
  237. *
  238. * \param key_type The type of the MAC key.
  239. * \param key_bits The size of the MAC key in bits.
  240. * \param alg A MAC algorithm (\c PSA_ALG_XXX value such that
  241. * #PSA_ALG_IS_MAC(\p alg) is true).
  242. *
  243. * \return The MAC size for the specified algorithm with
  244. * the specified key parameters.
  245. * \return 0 if the MAC algorithm is not recognized.
  246. * \return Either 0 or the correct size for a MAC algorithm that
  247. * the implementation recognizes, but does not support.
  248. * \return Unspecified if the key parameters are not consistent
  249. * with the algorithm.
  250. */
  251. #define PSA_MAC_LENGTH(key_type, key_bits, alg) \
  252. ((alg) & PSA_ALG_MAC_TRUNCATION_MASK ? PSA_MAC_TRUNCATED_LENGTH(alg) : \
  253. PSA_ALG_IS_HMAC(alg) ? PSA_HASH_LENGTH(PSA_ALG_HMAC_GET_HASH(alg)) : \
  254. PSA_ALG_IS_BLOCK_CIPHER_MAC(alg) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
  255. ((void)(key_type), (void)(key_bits), 0))
  256. /** The maximum size of the output of psa_aead_encrypt(), in bytes.
  257. *
  258. * If the size of the ciphertext buffer is at least this large, it is
  259. * guaranteed that psa_aead_encrypt() will not fail due to an
  260. * insufficient buffer size. Depending on the algorithm, the actual size of
  261. * the ciphertext may be smaller.
  262. *
  263. * See also #PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(\p plaintext_length).
  264. *
  265. * \warning This macro may evaluate its arguments multiple times or
  266. * zero times, so you should not pass arguments that contain
  267. * side effects.
  268. *
  269. * \param key_type A symmetric key type that is
  270. * compatible with algorithm \p alg.
  271. * \param alg An AEAD algorithm
  272. * (\c PSA_ALG_XXX value such that
  273. * #PSA_ALG_IS_AEAD(\p alg) is true).
  274. * \param plaintext_length Size of the plaintext in bytes.
  275. *
  276. * \return The AEAD ciphertext size for the specified
  277. * algorithm.
  278. * If the key type or AEAD algorithm is not
  279. * recognized, or the parameters are incompatible,
  280. * return 0.
  281. */
  282. #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE(key_type, alg, plaintext_length) \
  283. (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
  284. (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
  285. 0)
  286. /** A sufficient output buffer size for psa_aead_encrypt(), for any of the
  287. * supported key types and AEAD algorithms.
  288. *
  289. * If the size of the ciphertext buffer is at least this large, it is guaranteed
  290. * that psa_aead_encrypt() will not fail due to an insufficient buffer size.
  291. *
  292. * \note This macro returns a compile-time constant if its arguments are
  293. * compile-time constants.
  294. *
  295. * See also #PSA_AEAD_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg,
  296. * \p plaintext_length).
  297. *
  298. * \param plaintext_length Size of the plaintext in bytes.
  299. *
  300. * \return A sufficient output buffer size for any of the
  301. * supported key types and AEAD algorithms.
  302. *
  303. */
  304. #define PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE(plaintext_length) \
  305. ((plaintext_length) + PSA_AEAD_TAG_MAX_SIZE)
  306. /** The maximum size of the output of psa_aead_decrypt(), in bytes.
  307. *
  308. * If the size of the plaintext buffer is at least this large, it is
  309. * guaranteed that psa_aead_decrypt() will not fail due to an
  310. * insufficient buffer size. Depending on the algorithm, the actual size of
  311. * the plaintext may be smaller.
  312. *
  313. * See also #PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(\p ciphertext_length).
  314. *
  315. * \warning This macro may evaluate its arguments multiple times or
  316. * zero times, so you should not pass arguments that contain
  317. * side effects.
  318. *
  319. * \param key_type A symmetric key type that is
  320. * compatible with algorithm \p alg.
  321. * \param alg An AEAD algorithm
  322. * (\c PSA_ALG_XXX value such that
  323. * #PSA_ALG_IS_AEAD(\p alg) is true).
  324. * \param ciphertext_length Size of the plaintext in bytes.
  325. *
  326. * \return The AEAD ciphertext size for the specified
  327. * algorithm.
  328. * If the key type or AEAD algorithm is not
  329. * recognized, or the parameters are incompatible,
  330. * return 0.
  331. */
  332. #define PSA_AEAD_DECRYPT_OUTPUT_SIZE(key_type, alg, ciphertext_length) \
  333. (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
  334. (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH(alg) ? \
  335. (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH(alg) : \
  336. 0)
  337. /** A sufficient output buffer size for psa_aead_decrypt(), for any of the
  338. * supported key types and AEAD algorithms.
  339. *
  340. * If the size of the plaintext buffer is at least this large, it is guaranteed
  341. * that psa_aead_decrypt() will not fail due to an insufficient buffer size.
  342. *
  343. * \note This macro returns a compile-time constant if its arguments are
  344. * compile-time constants.
  345. *
  346. * See also #PSA_AEAD_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg,
  347. * \p ciphertext_length).
  348. *
  349. * \param ciphertext_length Size of the ciphertext in bytes.
  350. *
  351. * \return A sufficient output buffer size for any of the
  352. * supported key types and AEAD algorithms.
  353. *
  354. */
  355. #define PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE(ciphertext_length) \
  356. (ciphertext_length)
  357. /** The default nonce size for an AEAD algorithm, in bytes.
  358. *
  359. * This macro can be used to allocate a buffer of sufficient size to
  360. * store the nonce output from #psa_aead_generate_nonce().
  361. *
  362. * See also #PSA_AEAD_NONCE_MAX_SIZE.
  363. *
  364. * \note This is not the maximum size of nonce supported as input to
  365. * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
  366. * just the default size that is generated by #psa_aead_generate_nonce().
  367. *
  368. * \warning This macro may evaluate its arguments multiple times or
  369. * zero times, so you should not pass arguments that contain
  370. * side effects.
  371. *
  372. * \param key_type A symmetric key type that is compatible with
  373. * algorithm \p alg.
  374. *
  375. * \param alg An AEAD algorithm (\c PSA_ALG_XXX value such that
  376. * #PSA_ALG_IS_AEAD(\p alg) is true).
  377. *
  378. * \return The default nonce size for the specified key type and algorithm.
  379. * If the key type or AEAD algorithm is not recognized,
  380. * or the parameters are incompatible, return 0.
  381. */
  382. #define PSA_AEAD_NONCE_LENGTH(key_type, alg) \
  383. (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) == 16 ? \
  384. MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CCM) ? 13 : \
  385. MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_GCM) ? 12 : \
  386. 0 : \
  387. (key_type) == PSA_KEY_TYPE_CHACHA20 && \
  388. MBEDTLS_PSA_ALG_AEAD_EQUAL(alg, PSA_ALG_CHACHA20_POLY1305) ? 12 : \
  389. 0)
  390. /** The maximum default nonce size among all supported pairs of key types and
  391. * AEAD algorithms, in bytes.
  392. *
  393. * This is equal to or greater than any value that #PSA_AEAD_NONCE_LENGTH()
  394. * may return.
  395. *
  396. * \note This is not the maximum size of nonce supported as input to
  397. * #psa_aead_set_nonce(), #psa_aead_encrypt() or #psa_aead_decrypt(),
  398. * just the largest size that may be generated by
  399. * #psa_aead_generate_nonce().
  400. */
  401. #define PSA_AEAD_NONCE_MAX_SIZE 13
  402. /** A sufficient output buffer size for psa_aead_update().
  403. *
  404. * If the size of the output buffer is at least this large, it is
  405. * guaranteed that psa_aead_update() will not fail due to an
  406. * insufficient buffer size. The actual size of the output may be smaller
  407. * in any given call.
  408. *
  409. * See also #PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
  410. *
  411. * \warning This macro may evaluate its arguments multiple times or
  412. * zero times, so you should not pass arguments that contain
  413. * side effects.
  414. *
  415. * \param key_type A symmetric key type that is
  416. * compatible with algorithm \p alg.
  417. * \param alg An AEAD algorithm
  418. * (\c PSA_ALG_XXX value such that
  419. * #PSA_ALG_IS_AEAD(\p alg) is true).
  420. * \param input_length Size of the input in bytes.
  421. *
  422. * \return A sufficient output buffer size for the specified
  423. * algorithm.
  424. * If the key type or AEAD algorithm is not
  425. * recognized, or the parameters are incompatible,
  426. * return 0.
  427. */
  428. /* For all the AEAD modes defined in this specification, it is possible
  429. * to emit output without delay. However, hardware may not always be
  430. * capable of this. So for modes based on a block cipher, allow the
  431. * implementation to delay the output until it has a full block. */
  432. #define PSA_AEAD_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
  433. (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 ? \
  434. PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
  435. PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), (input_length)) : \
  436. (input_length) : \
  437. 0)
  438. /** A sufficient output buffer size for psa_aead_update(), for any of the
  439. * supported key types and AEAD algorithms.
  440. *
  441. * If the size of the output buffer is at least this large, it is guaranteed
  442. * that psa_aead_update() will not fail due to an insufficient buffer size.
  443. *
  444. * See also #PSA_AEAD_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
  445. *
  446. * \param input_length Size of the input in bytes.
  447. */
  448. #define PSA_AEAD_UPDATE_OUTPUT_MAX_SIZE(input_length) \
  449. (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length)))
  450. /** A sufficient ciphertext buffer size for psa_aead_finish().
  451. *
  452. * If the size of the ciphertext buffer is at least this large, it is
  453. * guaranteed that psa_aead_finish() will not fail due to an
  454. * insufficient ciphertext buffer size. The actual size of the output may
  455. * be smaller in any given call.
  456. *
  457. * See also #PSA_AEAD_FINISH_OUTPUT_MAX_SIZE.
  458. *
  459. * \param key_type A symmetric key type that is
  460. compatible with algorithm \p alg.
  461. * \param alg An AEAD algorithm
  462. * (\c PSA_ALG_XXX value such that
  463. * #PSA_ALG_IS_AEAD(\p alg) is true).
  464. *
  465. * \return A sufficient ciphertext buffer size for the
  466. * specified algorithm.
  467. * If the key type or AEAD algorithm is not
  468. * recognized, or the parameters are incompatible,
  469. * return 0.
  470. */
  471. #define PSA_AEAD_FINISH_OUTPUT_SIZE(key_type, alg) \
  472. (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
  473. PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
  474. PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
  475. 0)
  476. /** A sufficient ciphertext buffer size for psa_aead_finish(), for any of the
  477. * supported key types and AEAD algorithms.
  478. *
  479. * See also #PSA_AEAD_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
  480. */
  481. #define PSA_AEAD_FINISH_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
  482. /** A sufficient plaintext buffer size for psa_aead_verify().
  483. *
  484. * If the size of the plaintext buffer is at least this large, it is
  485. * guaranteed that psa_aead_verify() will not fail due to an
  486. * insufficient plaintext buffer size. The actual size of the output may
  487. * be smaller in any given call.
  488. *
  489. * See also #PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE.
  490. *
  491. * \param key_type A symmetric key type that is
  492. * compatible with algorithm \p alg.
  493. * \param alg An AEAD algorithm
  494. * (\c PSA_ALG_XXX value such that
  495. * #PSA_ALG_IS_AEAD(\p alg) is true).
  496. *
  497. * \return A sufficient plaintext buffer size for the
  498. * specified algorithm.
  499. * If the key type or AEAD algorithm is not
  500. * recognized, or the parameters are incompatible,
  501. * return 0.
  502. */
  503. #define PSA_AEAD_VERIFY_OUTPUT_SIZE(key_type, alg) \
  504. (PSA_AEAD_NONCE_LENGTH(key_type, alg) != 0 && \
  505. PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER(alg) ? \
  506. PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
  507. 0)
  508. /** A sufficient plaintext buffer size for psa_aead_verify(), for any of the
  509. * supported key types and AEAD algorithms.
  510. *
  511. * See also #PSA_AEAD_VERIFY_OUTPUT_SIZE(\p key_type, \p alg).
  512. */
  513. #define PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
  514. #define PSA_RSA_MINIMUM_PADDING_SIZE(alg) \
  515. (PSA_ALG_IS_RSA_OAEP(alg) ? \
  516. 2 * PSA_HASH_LENGTH(PSA_ALG_RSA_OAEP_GET_HASH(alg)) + 1 : \
  517. 11 /*PKCS#1v1.5*/)
  518. /**
  519. * \brief ECDSA signature size for a given curve bit size
  520. *
  521. * \param curve_bits Curve size in bits.
  522. * \return Signature size in bytes.
  523. *
  524. * \note This macro returns a compile-time constant if its argument is one.
  525. */
  526. #define PSA_ECDSA_SIGNATURE_SIZE(curve_bits) \
  527. (PSA_BITS_TO_BYTES(curve_bits) * 2)
  528. /** Sufficient signature buffer size for psa_sign_hash().
  529. *
  530. * This macro returns a sufficient buffer size for a signature using a key
  531. * of the specified type and size, with the specified algorithm.
  532. * Note that the actual size of the signature may be smaller
  533. * (some algorithms produce a variable-size signature).
  534. *
  535. * \warning This function may call its arguments multiple times or
  536. * zero times, so you should not pass arguments that contain
  537. * side effects.
  538. *
  539. * \param key_type An asymmetric key type (this may indifferently be a
  540. * key pair type or a public key type).
  541. * \param key_bits The size of the key in bits.
  542. * \param alg The signature algorithm.
  543. *
  544. * \return If the parameters are valid and supported, return
  545. * a buffer size in bytes that guarantees that
  546. * psa_sign_hash() will not fail with
  547. * #PSA_ERROR_BUFFER_TOO_SMALL.
  548. * If the parameters are a valid combination that is not supported,
  549. * return either a sensible size or 0.
  550. * If the parameters are not valid, the
  551. * return value is unspecified.
  552. */
  553. #define PSA_SIGN_OUTPUT_SIZE(key_type, key_bits, alg) \
  554. (PSA_KEY_TYPE_IS_RSA(key_type) ? ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
  555. PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_ECDSA_SIGNATURE_SIZE(key_bits) : \
  556. ((void)alg, 0))
  557. #define PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE \
  558. PSA_ECDSA_SIGNATURE_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
  559. /** \def PSA_SIGNATURE_MAX_SIZE
  560. *
  561. * Maximum size of an asymmetric signature.
  562. *
  563. * This macro expands to a compile-time constant integer. This value
  564. * is the maximum size of a signature in bytes.
  565. */
  566. #define PSA_SIGNATURE_MAX_SIZE \
  567. (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) > PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE ? \
  568. PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
  569. PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE)
  570. /** Sufficient output buffer size for psa_asymmetric_encrypt().
  571. *
  572. * This macro returns a sufficient buffer size for a ciphertext produced using
  573. * a key of the specified type and size, with the specified algorithm.
  574. * Note that the actual size of the ciphertext may be smaller, depending
  575. * on the algorithm.
  576. *
  577. * \warning This function may call its arguments multiple times or
  578. * zero times, so you should not pass arguments that contain
  579. * side effects.
  580. *
  581. * \param key_type An asymmetric key type (this may indifferently be a
  582. * key pair type or a public key type).
  583. * \param key_bits The size of the key in bits.
  584. * \param alg The asymmetric encryption algorithm.
  585. *
  586. * \return If the parameters are valid and supported, return
  587. * a buffer size in bytes that guarantees that
  588. * psa_asymmetric_encrypt() will not fail with
  589. * #PSA_ERROR_BUFFER_TOO_SMALL.
  590. * If the parameters are a valid combination that is not supported,
  591. * return either a sensible size or 0.
  592. * If the parameters are not valid, the
  593. * return value is unspecified.
  594. */
  595. #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
  596. (PSA_KEY_TYPE_IS_RSA(key_type) ? \
  597. ((void)alg, PSA_BITS_TO_BYTES(key_bits)) : \
  598. 0)
  599. /** A sufficient output buffer size for psa_asymmetric_encrypt(), for any
  600. * supported asymmetric encryption.
  601. *
  602. * See also #PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
  603. */
  604. /* This macro assumes that RSA is the only supported asymmetric encryption. */
  605. #define PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE \
  606. (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
  607. /** Sufficient output buffer size for psa_asymmetric_decrypt().
  608. *
  609. * This macro returns a sufficient buffer size for a plaintext produced using
  610. * a key of the specified type and size, with the specified algorithm.
  611. * Note that the actual size of the plaintext may be smaller, depending
  612. * on the algorithm.
  613. *
  614. * \warning This function may call its arguments multiple times or
  615. * zero times, so you should not pass arguments that contain
  616. * side effects.
  617. *
  618. * \param key_type An asymmetric key type (this may indifferently be a
  619. * key pair type or a public key type).
  620. * \param key_bits The size of the key in bits.
  621. * \param alg The asymmetric encryption algorithm.
  622. *
  623. * \return If the parameters are valid and supported, return
  624. * a buffer size in bytes that guarantees that
  625. * psa_asymmetric_decrypt() will not fail with
  626. * #PSA_ERROR_BUFFER_TOO_SMALL.
  627. * If the parameters are a valid combination that is not supported,
  628. * return either a sensible size or 0.
  629. * If the parameters are not valid, the
  630. * return value is unspecified.
  631. */
  632. #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(key_type, key_bits, alg) \
  633. (PSA_KEY_TYPE_IS_RSA(key_type) ? \
  634. PSA_BITS_TO_BYTES(key_bits) - PSA_RSA_MINIMUM_PADDING_SIZE(alg) : \
  635. 0)
  636. /** A sufficient output buffer size for psa_asymmetric_decrypt(), for any
  637. * supported asymmetric decryption.
  638. *
  639. * This macro assumes that RSA is the only supported asymmetric encryption.
  640. *
  641. * See also #PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE(\p key_type, \p key_bits, \p alg).
  642. */
  643. #define PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE \
  644. (PSA_BITS_TO_BYTES(PSA_VENDOR_RSA_MAX_KEY_BITS))
  645. /* Maximum size of the ASN.1 encoding of an INTEGER with the specified
  646. * number of bits.
  647. *
  648. * This definition assumes that bits <= 2^19 - 9 so that the length field
  649. * is at most 3 bytes. The length of the encoding is the length of the
  650. * bit string padded to a whole number of bytes plus:
  651. * - 1 type byte;
  652. * - 1 to 3 length bytes;
  653. * - 0 to 1 bytes of leading 0 due to the sign bit.
  654. */
  655. #define PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(bits) \
  656. ((bits) / 8 + 5)
  657. /* Maximum size of the export encoding of an RSA public key.
  658. * Assumes that the public exponent is less than 2^32.
  659. *
  660. * RSAPublicKey ::= SEQUENCE {
  661. * modulus INTEGER, -- n
  662. * publicExponent INTEGER } -- e
  663. *
  664. * - 4 bytes of SEQUENCE overhead;
  665. * - n : INTEGER;
  666. * - 7 bytes for the public exponent.
  667. */
  668. #define PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
  669. (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) + 11)
  670. /* Maximum size of the export encoding of an RSA key pair.
  671. * Assumes thatthe public exponent is less than 2^32 and that the size
  672. * difference between the two primes is at most 1 bit.
  673. *
  674. * RSAPrivateKey ::= SEQUENCE {
  675. * version Version, -- 0
  676. * modulus INTEGER, -- N-bit
  677. * publicExponent INTEGER, -- 32-bit
  678. * privateExponent INTEGER, -- N-bit
  679. * prime1 INTEGER, -- N/2-bit
  680. * prime2 INTEGER, -- N/2-bit
  681. * exponent1 INTEGER, -- N/2-bit
  682. * exponent2 INTEGER, -- N/2-bit
  683. * coefficient INTEGER, -- N/2-bit
  684. * }
  685. *
  686. * - 4 bytes of SEQUENCE overhead;
  687. * - 3 bytes of version;
  688. * - 7 half-size INTEGERs plus 2 full-size INTEGERs,
  689. * overapproximated as 9 half-size INTEGERS;
  690. * - 7 bytes for the public exponent.
  691. */
  692. #define PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) \
  693. (9 * PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE((key_bits) / 2 + 1) + 14)
  694. /* Maximum size of the export encoding of a DSA public key.
  695. *
  696. * SubjectPublicKeyInfo ::= SEQUENCE {
  697. * algorithm AlgorithmIdentifier,
  698. * subjectPublicKey BIT STRING } -- contains DSAPublicKey
  699. * AlgorithmIdentifier ::= SEQUENCE {
  700. * algorithm OBJECT IDENTIFIER,
  701. * parameters Dss-Parms } -- SEQUENCE of 3 INTEGERs
  702. * DSAPublicKey ::= INTEGER -- public key, Y
  703. *
  704. * - 3 * 4 bytes of SEQUENCE overhead;
  705. * - 1 + 1 + 7 bytes of algorithm (DSA OID);
  706. * - 4 bytes of BIT STRING overhead;
  707. * - 3 full-size INTEGERs (p, g, y);
  708. * - 1 + 1 + 32 bytes for 1 sub-size INTEGER (q <= 256 bits).
  709. */
  710. #define PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) \
  711. (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 59)
  712. /* Maximum size of the export encoding of a DSA key pair.
  713. *
  714. * DSAPrivateKey ::= SEQUENCE {
  715. * version Version, -- 0
  716. * prime INTEGER, -- p
  717. * subprime INTEGER, -- q
  718. * generator INTEGER, -- g
  719. * public INTEGER, -- y
  720. * private INTEGER, -- x
  721. * }
  722. *
  723. * - 4 bytes of SEQUENCE overhead;
  724. * - 3 bytes of version;
  725. * - 3 full-size INTEGERs (p, g, y);
  726. * - 2 * (1 + 1 + 32) bytes for 2 sub-size INTEGERs (q, x <= 256 bits).
  727. */
  728. #define PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) \
  729. (PSA_KEY_EXPORT_ASN1_INTEGER_MAX_SIZE(key_bits) * 3 + 75)
  730. /* Maximum size of the export encoding of an ECC public key.
  731. *
  732. * The representation of an ECC public key is:
  733. * - The byte 0x04;
  734. * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
  735. * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
  736. * - where m is the bit size associated with the curve.
  737. *
  738. * - 1 byte + 2 * point size.
  739. */
  740. #define PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) \
  741. (2 * PSA_BITS_TO_BYTES(key_bits) + 1)
  742. /* Maximum size of the export encoding of an ECC key pair.
  743. *
  744. * An ECC key pair is represented by the secret value.
  745. */
  746. #define PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) \
  747. (PSA_BITS_TO_BYTES(key_bits))
  748. /** Sufficient output buffer size for psa_export_key() or
  749. * psa_export_public_key().
  750. *
  751. * This macro returns a compile-time constant if its arguments are
  752. * compile-time constants.
  753. *
  754. * \warning This macro may evaluate its arguments multiple times or
  755. * zero times, so you should not pass arguments that contain
  756. * side effects.
  757. *
  758. * The following code illustrates how to allocate enough memory to export
  759. * a key by querying the key type and size at runtime.
  760. * \code{c}
  761. * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  762. * psa_status_t status;
  763. * status = psa_get_key_attributes(key, &attributes);
  764. * if (status != PSA_SUCCESS) handle_error(...);
  765. * psa_key_type_t key_type = psa_get_key_type(&attributes);
  766. * size_t key_bits = psa_get_key_bits(&attributes);
  767. * size_t buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
  768. * psa_reset_key_attributes(&attributes);
  769. * uint8_t *buffer = malloc(buffer_size);
  770. * if (buffer == NULL) handle_error(...);
  771. * size_t buffer_length;
  772. * status = psa_export_key(key, buffer, buffer_size, &buffer_length);
  773. * if (status != PSA_SUCCESS) handle_error(...);
  774. * \endcode
  775. *
  776. * \param key_type A supported key type.
  777. * \param key_bits The size of the key in bits.
  778. *
  779. * \return If the parameters are valid and supported, return
  780. * a buffer size in bytes that guarantees that
  781. * psa_export_key() or psa_export_public_key() will not fail with
  782. * #PSA_ERROR_BUFFER_TOO_SMALL.
  783. * If the parameters are a valid combination that is not supported,
  784. * return either a sensible size or 0.
  785. * If the parameters are not valid, the return value is unspecified.
  786. */
  787. #define PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits) \
  788. (PSA_KEY_TYPE_IS_UNSTRUCTURED(key_type) ? PSA_BITS_TO_BYTES(key_bits) : \
  789. (key_type) == PSA_KEY_TYPE_RSA_KEY_PAIR ? PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits) : \
  790. (key_type) == PSA_KEY_TYPE_RSA_PUBLIC_KEY ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
  791. (key_type) == PSA_KEY_TYPE_DSA_KEY_PAIR ? PSA_KEY_EXPORT_DSA_KEY_PAIR_MAX_SIZE(key_bits) : \
  792. (key_type) == PSA_KEY_TYPE_DSA_PUBLIC_KEY ? PSA_KEY_EXPORT_DSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
  793. PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(key_bits) : \
  794. PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
  795. 0)
  796. /** Sufficient output buffer size for psa_export_public_key().
  797. *
  798. * This macro returns a compile-time constant if its arguments are
  799. * compile-time constants.
  800. *
  801. * \warning This macro may evaluate its arguments multiple times or
  802. * zero times, so you should not pass arguments that contain
  803. * side effects.
  804. *
  805. * The following code illustrates how to allocate enough memory to export
  806. * a public key by querying the key type and size at runtime.
  807. * \code{c}
  808. * psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  809. * psa_status_t status;
  810. * status = psa_get_key_attributes(key, &attributes);
  811. * if (status != PSA_SUCCESS) handle_error(...);
  812. * psa_key_type_t key_type = psa_get_key_type(&attributes);
  813. * size_t key_bits = psa_get_key_bits(&attributes);
  814. * size_t buffer_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits);
  815. * psa_reset_key_attributes(&attributes);
  816. * uint8_t *buffer = malloc(buffer_size);
  817. * if (buffer == NULL) handle_error(...);
  818. * size_t buffer_length;
  819. * status = psa_export_public_key(key, buffer, buffer_size, &buffer_length);
  820. * if (status != PSA_SUCCESS) handle_error(...);
  821. * \endcode
  822. *
  823. * \param key_type A public key or key pair key type.
  824. * \param key_bits The size of the key in bits.
  825. *
  826. * \return If the parameters are valid and supported, return
  827. * a buffer size in bytes that guarantees that
  828. * psa_export_public_key() will not fail with
  829. * #PSA_ERROR_BUFFER_TOO_SMALL.
  830. * If the parameters are a valid combination that is not
  831. * supported, return either a sensible size or 0.
  832. * If the parameters are not valid,
  833. * the return value is unspecified.
  834. *
  835. * If the parameters are valid and supported,
  836. * return the same result as
  837. * #PSA_EXPORT_KEY_OUTPUT_SIZE(
  838. * \p #PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(\p key_type),
  839. * \p key_bits).
  840. */
  841. #define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) \
  842. (PSA_KEY_TYPE_IS_RSA(key_type) ? PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(key_bits) : \
  843. PSA_KEY_TYPE_IS_ECC(key_type) ? PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(key_bits) : \
  844. 0)
  845. /** Sufficient buffer size for exporting any asymmetric key pair.
  846. *
  847. * This macro expands to a compile-time constant integer. This value is
  848. * a sufficient buffer size when calling psa_export_key() to export any
  849. * asymmetric key pair, regardless of the exact key type and key size.
  850. *
  851. * See also #PSA_EXPORT_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
  852. */
  853. #define PSA_EXPORT_KEY_PAIR_MAX_SIZE \
  854. (PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
  855. PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
  856. PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
  857. PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
  858. /** Sufficient buffer size for exporting any asymmetric public key.
  859. *
  860. * This macro expands to a compile-time constant integer. This value is
  861. * a sufficient buffer size when calling psa_export_key() or
  862. * psa_export_public_key() to export any asymmetric public key,
  863. * regardless of the exact key type and key size.
  864. *
  865. * See also #PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(\p key_type, \p key_bits).
  866. */
  867. #define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
  868. (PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) > \
  869. PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS) ? \
  870. PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS) : \
  871. PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS))
  872. /** Sufficient output buffer size for psa_raw_key_agreement().
  873. *
  874. * This macro returns a compile-time constant if its arguments are
  875. * compile-time constants.
  876. *
  877. * \warning This macro may evaluate its arguments multiple times or
  878. * zero times, so you should not pass arguments that contain
  879. * side effects.
  880. *
  881. * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE.
  882. *
  883. * \param key_type A supported key type.
  884. * \param key_bits The size of the key in bits.
  885. *
  886. * \return If the parameters are valid and supported, return
  887. * a buffer size in bytes that guarantees that
  888. * psa_raw_key_agreement() will not fail with
  889. * #PSA_ERROR_BUFFER_TOO_SMALL.
  890. * If the parameters are a valid combination that
  891. * is not supported, return either a sensible size or 0.
  892. * If the parameters are not valid,
  893. * the return value is unspecified.
  894. */
  895. /* FFDH is not yet supported in PSA. */
  896. #define PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(key_type, key_bits) \
  897. (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ? \
  898. PSA_BITS_TO_BYTES(key_bits) : \
  899. 0)
  900. /** Maximum size of the output from psa_raw_key_agreement().
  901. *
  902. * This macro expands to a compile-time constant integer. This value is the
  903. * maximum size of the output any raw key agreement algorithm, in bytes.
  904. *
  905. * See also #PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE(\p key_type, \p key_bits).
  906. */
  907. #define PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE \
  908. (PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS))
  909. /** The default IV size for a cipher algorithm, in bytes.
  910. *
  911. * The IV that is generated as part of a call to #psa_cipher_encrypt() is always
  912. * the default IV length for the algorithm.
  913. *
  914. * This macro can be used to allocate a buffer of sufficient size to
  915. * store the IV output from #psa_cipher_generate_iv() when using
  916. * a multi-part cipher operation.
  917. *
  918. * See also #PSA_CIPHER_IV_MAX_SIZE.
  919. *
  920. * \warning This macro may evaluate its arguments multiple times or
  921. * zero times, so you should not pass arguments that contain
  922. * side effects.
  923. *
  924. * \param key_type A symmetric key type that is compatible with algorithm \p alg.
  925. *
  926. * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that #PSA_ALG_IS_CIPHER(\p alg) is true).
  927. *
  928. * \return The default IV size for the specified key type and algorithm.
  929. * If the algorithm does not use an IV, return 0.
  930. * If the key type or cipher algorithm is not recognized,
  931. * or the parameters are incompatible, return 0.
  932. */
  933. #define PSA_CIPHER_IV_LENGTH(key_type, alg) \
  934. (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) > 1 && \
  935. ((alg) == PSA_ALG_CTR || \
  936. (alg) == PSA_ALG_CFB || \
  937. (alg) == PSA_ALG_OFB || \
  938. (alg) == PSA_ALG_XTS || \
  939. (alg) == PSA_ALG_CBC_NO_PADDING || \
  940. (alg) == PSA_ALG_CBC_PKCS7) ? PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
  941. (key_type) == PSA_KEY_TYPE_CHACHA20 && \
  942. (alg) == PSA_ALG_STREAM_CIPHER ? 12 : \
  943. (alg) == PSA_ALG_CCM_STAR_NO_TAG ? 13 : \
  944. 0)
  945. /** The maximum IV size for all supported cipher algorithms, in bytes.
  946. *
  947. * See also #PSA_CIPHER_IV_LENGTH().
  948. */
  949. #define PSA_CIPHER_IV_MAX_SIZE 16
  950. /** The maximum size of the output of psa_cipher_encrypt(), in bytes.
  951. *
  952. * If the size of the output buffer is at least this large, it is guaranteed
  953. * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
  954. * Depending on the algorithm, the actual size of the output might be smaller.
  955. *
  956. * See also #PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(\p input_length).
  957. *
  958. * \warning This macro may evaluate its arguments multiple times or
  959. * zero times, so you should not pass arguments that contain
  960. * side effects.
  961. *
  962. * \param key_type A symmetric key type that is compatible with algorithm
  963. * alg.
  964. * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
  965. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  966. * \param input_length Size of the input in bytes.
  967. *
  968. * \return A sufficient output size for the specified key type and
  969. * algorithm. If the key type or cipher algorithm is not
  970. * recognized, or the parameters are incompatible,
  971. * return 0.
  972. */
  973. #define PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
  974. (alg == PSA_ALG_CBC_PKCS7 ? \
  975. (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
  976. PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
  977. (input_length) + 1) + \
  978. PSA_CIPHER_IV_LENGTH((key_type), (alg)) : 0) : \
  979. (PSA_ALG_IS_CIPHER(alg) ? \
  980. (input_length) + PSA_CIPHER_IV_LENGTH((key_type), (alg)) : \
  981. 0))
  982. /** A sufficient output buffer size for psa_cipher_encrypt(), for any of the
  983. * supported key types and cipher algorithms.
  984. *
  985. * If the size of the output buffer is at least this large, it is guaranteed
  986. * that psa_cipher_encrypt() will not fail due to an insufficient buffer size.
  987. *
  988. * See also #PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
  989. *
  990. * \param input_length Size of the input in bytes.
  991. *
  992. */
  993. #define PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE(input_length) \
  994. (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, \
  995. (input_length) + 1) + \
  996. PSA_CIPHER_IV_MAX_SIZE)
  997. /** The maximum size of the output of psa_cipher_decrypt(), in bytes.
  998. *
  999. * If the size of the output buffer is at least this large, it is guaranteed
  1000. * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
  1001. * Depending on the algorithm, the actual size of the output might be smaller.
  1002. *
  1003. * See also #PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(\p input_length).
  1004. *
  1005. * \param key_type A symmetric key type that is compatible with algorithm
  1006. * alg.
  1007. * \param alg A cipher algorithm (\c PSA_ALG_XXX value such that
  1008. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1009. * \param input_length Size of the input in bytes.
  1010. *
  1011. * \return A sufficient output size for the specified key type and
  1012. * algorithm. If the key type or cipher algorithm is not
  1013. * recognized, or the parameters are incompatible,
  1014. * return 0.
  1015. */
  1016. #define PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, input_length) \
  1017. (PSA_ALG_IS_CIPHER(alg) && \
  1018. ((key_type) & PSA_KEY_TYPE_CATEGORY_MASK) == PSA_KEY_TYPE_CATEGORY_SYMMETRIC ? \
  1019. (input_length) : \
  1020. 0)
  1021. /** A sufficient output buffer size for psa_cipher_decrypt(), for any of the
  1022. * supported key types and cipher algorithms.
  1023. *
  1024. * If the size of the output buffer is at least this large, it is guaranteed
  1025. * that psa_cipher_decrypt() will not fail due to an insufficient buffer size.
  1026. *
  1027. * See also #PSA_CIPHER_DECRYPT_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
  1028. *
  1029. * \param input_length Size of the input in bytes.
  1030. */
  1031. #define PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE(input_length) \
  1032. (input_length)
  1033. /** A sufficient output buffer size for psa_cipher_update().
  1034. *
  1035. * If the size of the output buffer is at least this large, it is guaranteed
  1036. * that psa_cipher_update() will not fail due to an insufficient buffer size.
  1037. * The actual size of the output might be smaller in any given call.
  1038. *
  1039. * See also #PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(\p input_length).
  1040. *
  1041. * \param key_type A symmetric key type that is compatible with algorithm
  1042. * alg.
  1043. * \param alg A cipher algorithm (PSA_ALG_XXX value such that
  1044. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1045. * \param input_length Size of the input in bytes.
  1046. *
  1047. * \return A sufficient output size for the specified key type and
  1048. * algorithm. If the key type or cipher algorithm is not
  1049. * recognized, or the parameters are incompatible, return 0.
  1050. */
  1051. #define PSA_CIPHER_UPDATE_OUTPUT_SIZE(key_type, alg, input_length) \
  1052. (PSA_ALG_IS_CIPHER(alg) ? \
  1053. (PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) != 0 ? \
  1054. (((alg) == PSA_ALG_CBC_PKCS7 || \
  1055. (alg) == PSA_ALG_CBC_NO_PADDING || \
  1056. (alg) == PSA_ALG_ECB_NO_PADDING) ? \
  1057. PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type), \
  1058. input_length) : \
  1059. (input_length)) : 0) : \
  1060. 0)
  1061. /** A sufficient output buffer size for psa_cipher_update(), for any of the
  1062. * supported key types and cipher algorithms.
  1063. *
  1064. * If the size of the output buffer is at least this large, it is guaranteed
  1065. * that psa_cipher_update() will not fail due to an insufficient buffer size.
  1066. *
  1067. * See also #PSA_CIPHER_UPDATE_OUTPUT_SIZE(\p key_type, \p alg, \p input_length).
  1068. *
  1069. * \param input_length Size of the input in bytes.
  1070. */
  1071. #define PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE(input_length) \
  1072. (PSA_ROUND_UP_TO_MULTIPLE(PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, input_length))
  1073. /** A sufficient ciphertext buffer size for psa_cipher_finish().
  1074. *
  1075. * If the size of the ciphertext buffer is at least this large, it is
  1076. * guaranteed that psa_cipher_finish() will not fail due to an insufficient
  1077. * ciphertext buffer size. The actual size of the output might be smaller in
  1078. * any given call.
  1079. *
  1080. * See also #PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE().
  1081. *
  1082. * \param key_type A symmetric key type that is compatible with algorithm
  1083. * alg.
  1084. * \param alg A cipher algorithm (PSA_ALG_XXX value such that
  1085. * #PSA_ALG_IS_CIPHER(\p alg) is true).
  1086. * \return A sufficient output size for the specified key type and
  1087. * algorithm. If the key type or cipher algorithm is not
  1088. * recognized, or the parameters are incompatible, return 0.
  1089. */
  1090. #define PSA_CIPHER_FINISH_OUTPUT_SIZE(key_type, alg) \
  1091. (PSA_ALG_IS_CIPHER(alg) ? \
  1092. (alg == PSA_ALG_CBC_PKCS7 ? \
  1093. PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type) : \
  1094. 0) : \
  1095. 0)
  1096. /** A sufficient ciphertext buffer size for psa_cipher_finish(), for any of the
  1097. * supported key types and cipher algorithms.
  1098. *
  1099. * See also #PSA_CIPHER_FINISH_OUTPUT_SIZE(\p key_type, \p alg).
  1100. */
  1101. #define PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE \
  1102. (PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE)
  1103. #endif /* PSA_CRYPTO_SIZES_H */