crypto_sizes.h 55 KB

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