crypto_sizes.h 51 KB

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