crypto_compat.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /**
  2. * \file psa/crypto_compat.h
  3. *
  4. * \brief PSA cryptography module: Backward compatibility aliases
  5. *
  6. * This header declares alternative names for macro and functions.
  7. * New application code should not use these names.
  8. * These names may be removed in a future version of Mbed Crypto.
  9. *
  10. * \note This file may not be included directly. Applications must
  11. * include psa/crypto.h.
  12. */
  13. /*
  14. * Copyright The Mbed TLS Contributors
  15. * SPDX-License-Identifier: Apache-2.0
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. */
  29. #ifndef PSA_CRYPTO_COMPAT_H
  30. #define PSA_CRYPTO_COMPAT_H
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /*
  35. * To support both openless APIs and psa_open_key() temporarily, define
  36. * psa_key_handle_t to be equal to mbedtls_svc_key_id_t. Do not mark the
  37. * type and its utility macros and functions deprecated yet. This will be done
  38. * in a subsequent phase.
  39. */
  40. typedef mbedtls_svc_key_id_t psa_key_handle_t;
  41. #define PSA_KEY_HANDLE_INIT MBEDTLS_SVC_KEY_ID_INIT
  42. /** Check whether an handle is null.
  43. *
  44. * \param handle Handle
  45. *
  46. * \return Non-zero if the handle is null, zero otherwise.
  47. */
  48. static inline int psa_key_handle_is_null( psa_key_handle_t handle )
  49. {
  50. return( mbedtls_svc_key_id_is_null( handle ) );
  51. }
  52. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  53. /*
  54. * Mechanism for declaring deprecated values
  55. */
  56. #if defined(MBEDTLS_DEPRECATED_WARNING) && !defined(MBEDTLS_PSA_DEPRECATED)
  57. #define MBEDTLS_PSA_DEPRECATED __attribute__((deprecated))
  58. #else
  59. #define MBEDTLS_PSA_DEPRECATED
  60. #endif
  61. typedef MBEDTLS_PSA_DEPRECATED size_t mbedtls_deprecated_size_t;
  62. typedef MBEDTLS_PSA_DEPRECATED psa_status_t mbedtls_deprecated_psa_status_t;
  63. typedef MBEDTLS_PSA_DEPRECATED psa_key_usage_t mbedtls_deprecated_psa_key_usage_t;
  64. typedef MBEDTLS_PSA_DEPRECATED psa_ecc_family_t mbedtls_deprecated_psa_ecc_family_t;
  65. typedef MBEDTLS_PSA_DEPRECATED psa_dh_family_t mbedtls_deprecated_psa_dh_family_t;
  66. typedef MBEDTLS_PSA_DEPRECATED psa_ecc_family_t psa_ecc_curve_t;
  67. typedef MBEDTLS_PSA_DEPRECATED psa_dh_family_t psa_dh_group_t;
  68. typedef MBEDTLS_PSA_DEPRECATED psa_algorithm_t mbedtls_deprecated_psa_algorithm_t;
  69. #define PSA_KEY_TYPE_GET_CURVE PSA_KEY_TYPE_ECC_GET_FAMILY
  70. #define PSA_KEY_TYPE_GET_GROUP PSA_KEY_TYPE_DH_GET_FAMILY
  71. #define MBEDTLS_DEPRECATED_CONSTANT( type, value ) \
  72. ( (mbedtls_deprecated_##type) ( value ) )
  73. /*
  74. * Deprecated PSA Crypto error code definitions (PSA Crypto API <= 1.0 beta2)
  75. */
  76. #define PSA_ERROR_UNKNOWN_ERROR \
  77. MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_GENERIC_ERROR )
  78. #define PSA_ERROR_OCCUPIED_SLOT \
  79. MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_ALREADY_EXISTS )
  80. #define PSA_ERROR_EMPTY_SLOT \
  81. MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_DOES_NOT_EXIST )
  82. #define PSA_ERROR_INSUFFICIENT_CAPACITY \
  83. MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_INSUFFICIENT_DATA )
  84. #define PSA_ERROR_TAMPERING_DETECTED \
  85. MBEDTLS_DEPRECATED_CONSTANT( psa_status_t, PSA_ERROR_CORRUPTION_DETECTED )
  86. /*
  87. * Deprecated PSA Crypto numerical encodings (PSA Crypto API <= 1.0 beta3)
  88. */
  89. #define PSA_KEY_USAGE_SIGN \
  90. MBEDTLS_DEPRECATED_CONSTANT( psa_key_usage_t, PSA_KEY_USAGE_SIGN_HASH )
  91. #define PSA_KEY_USAGE_VERIFY \
  92. MBEDTLS_DEPRECATED_CONSTANT( psa_key_usage_t, PSA_KEY_USAGE_VERIFY_HASH )
  93. /*
  94. * Deprecated PSA Crypto size calculation macros (PSA Crypto API <= 1.0 beta3)
  95. */
  96. #define PSA_ASYMMETRIC_SIGNATURE_MAX_SIZE \
  97. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_SIGNATURE_MAX_SIZE )
  98. #define PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ) \
  99. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ) )
  100. #define PSA_KEY_EXPORT_MAX_SIZE( key_type, key_bits ) \
  101. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, key_bits ) )
  102. #define PSA_BLOCK_CIPHER_BLOCK_SIZE( type ) \
  103. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) )
  104. #define PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE \
  105. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE )
  106. #define PSA_HASH_SIZE( alg ) \
  107. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_HASH_LENGTH( alg ) )
  108. #define PSA_MAC_FINAL_SIZE( key_type, key_bits, alg ) \
  109. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_MAC_LENGTH( key_type, key_bits, alg ) )
  110. #define PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN \
  111. MBEDTLS_DEPRECATED_CONSTANT( size_t, PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
  112. /*
  113. * Deprecated PSA Crypto function names (PSA Crypto API <= 1.0 beta3)
  114. */
  115. MBEDTLS_PSA_DEPRECATED static inline psa_status_t psa_asymmetric_sign( psa_key_handle_t key,
  116. psa_algorithm_t alg,
  117. const uint8_t *hash,
  118. size_t hash_length,
  119. uint8_t *signature,
  120. size_t signature_size,
  121. size_t *signature_length )
  122. {
  123. return psa_sign_hash( key, alg, hash, hash_length, signature, signature_size, signature_length );
  124. }
  125. MBEDTLS_PSA_DEPRECATED static inline psa_status_t psa_asymmetric_verify( psa_key_handle_t key,
  126. psa_algorithm_t alg,
  127. const uint8_t *hash,
  128. size_t hash_length,
  129. const uint8_t *signature,
  130. size_t signature_length )
  131. {
  132. return psa_verify_hash( key, alg, hash, hash_length, signature, signature_length );
  133. }
  134. /*
  135. * Size-specific elliptic curve families.
  136. */
  137. #define PSA_ECC_CURVE_SECP160K1 \
  138. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
  139. #define PSA_ECC_CURVE_SECP192K1 \
  140. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
  141. #define PSA_ECC_CURVE_SECP224K1 \
  142. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
  143. #define PSA_ECC_CURVE_SECP256K1 \
  144. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
  145. #define PSA_ECC_CURVE_SECP160R1 \
  146. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
  147. #define PSA_ECC_CURVE_SECP192R1 \
  148. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
  149. #define PSA_ECC_CURVE_SECP224R1 \
  150. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
  151. #define PSA_ECC_CURVE_SECP256R1 \
  152. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
  153. #define PSA_ECC_CURVE_SECP384R1 \
  154. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
  155. #define PSA_ECC_CURVE_SECP521R1 \
  156. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
  157. #define PSA_ECC_CURVE_SECP160R2 \
  158. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R2 )
  159. #define PSA_ECC_CURVE_SECT163K1 \
  160. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
  161. #define PSA_ECC_CURVE_SECT233K1 \
  162. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
  163. #define PSA_ECC_CURVE_SECT239K1 \
  164. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
  165. #define PSA_ECC_CURVE_SECT283K1 \
  166. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
  167. #define PSA_ECC_CURVE_SECT409K1 \
  168. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
  169. #define PSA_ECC_CURVE_SECT571K1 \
  170. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
  171. #define PSA_ECC_CURVE_SECT163R1 \
  172. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
  173. #define PSA_ECC_CURVE_SECT193R1 \
  174. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
  175. #define PSA_ECC_CURVE_SECT233R1 \
  176. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
  177. #define PSA_ECC_CURVE_SECT283R1 \
  178. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
  179. #define PSA_ECC_CURVE_SECT409R1 \
  180. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
  181. #define PSA_ECC_CURVE_SECT571R1 \
  182. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
  183. #define PSA_ECC_CURVE_SECT163R2 \
  184. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R2 )
  185. #define PSA_ECC_CURVE_SECT193R2 \
  186. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R2 )
  187. #define PSA_ECC_CURVE_BRAINPOOL_P256R1 \
  188. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
  189. #define PSA_ECC_CURVE_BRAINPOOL_P384R1 \
  190. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
  191. #define PSA_ECC_CURVE_BRAINPOOL_P512R1 \
  192. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
  193. #define PSA_ECC_CURVE_CURVE25519 \
  194. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_MONTGOMERY )
  195. #define PSA_ECC_CURVE_CURVE448 \
  196. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_MONTGOMERY )
  197. /*
  198. * Curves that changed name due to PSA specification.
  199. */
  200. #define PSA_ECC_CURVE_SECP_K1 \
  201. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_K1 )
  202. #define PSA_ECC_CURVE_SECP_R1 \
  203. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R1 )
  204. #define PSA_ECC_CURVE_SECP_R2 \
  205. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECP_R2 )
  206. #define PSA_ECC_CURVE_SECT_K1 \
  207. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_K1 )
  208. #define PSA_ECC_CURVE_SECT_R1 \
  209. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R1 )
  210. #define PSA_ECC_CURVE_SECT_R2 \
  211. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_SECT_R2 )
  212. #define PSA_ECC_CURVE_BRAINPOOL_P_R1 \
  213. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_BRAINPOOL_P_R1 )
  214. #define PSA_ECC_CURVE_MONTGOMERY \
  215. MBEDTLS_DEPRECATED_CONSTANT( psa_ecc_family_t, PSA_ECC_FAMILY_MONTGOMERY )
  216. /*
  217. * Finite-field Diffie-Hellman families.
  218. */
  219. #define PSA_DH_GROUP_FFDHE2048 \
  220. MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
  221. #define PSA_DH_GROUP_FFDHE3072 \
  222. MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
  223. #define PSA_DH_GROUP_FFDHE4096 \
  224. MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
  225. #define PSA_DH_GROUP_FFDHE6144 \
  226. MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
  227. #define PSA_DH_GROUP_FFDHE8192 \
  228. MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
  229. /*
  230. * Diffie-Hellman families that changed name due to PSA specification.
  231. */
  232. #define PSA_DH_GROUP_RFC7919 \
  233. MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_RFC7919 )
  234. #define PSA_DH_GROUP_CUSTOM \
  235. MBEDTLS_DEPRECATED_CONSTANT( psa_dh_family_t, PSA_DH_FAMILY_CUSTOM )
  236. /*
  237. * Deprecated PSA Crypto stream cipher algorithms (PSA Crypto API <= 1.0 beta3)
  238. */
  239. #define PSA_ALG_ARC4 \
  240. MBEDTLS_DEPRECATED_CONSTANT( psa_algorithm_t, PSA_ALG_STREAM_CIPHER )
  241. #define PSA_ALG_CHACHA20 \
  242. MBEDTLS_DEPRECATED_CONSTANT( psa_algorithm_t, PSA_ALG_STREAM_CIPHER )
  243. /*
  244. * Renamed AEAD tag length macros (PSA Crypto API <= 1.0 beta3)
  245. */
  246. #define PSA_ALG_AEAD_WITH_DEFAULT_TAG_LENGTH( aead_alg ) \
  247. MBEDTLS_DEPRECATED_CONSTANT( psa_algorithm_t, PSA_ALG_AEAD_WITH_DEFAULT_LENGTH_TAG( aead_alg ) )
  248. #define PSA_ALG_AEAD_WITH_TAG_LENGTH( aead_alg, tag_length ) \
  249. MBEDTLS_DEPRECATED_CONSTANT( psa_algorithm_t, PSA_ALG_AEAD_WITH_SHORTENED_TAG( aead_alg, tag_length ) )
  250. /*
  251. * Deprecated PSA AEAD output size macros (PSA Crypto API <= 1.0 beta3)
  252. */
  253. /** The tag size for an AEAD algorithm, in bytes.
  254. *
  255. * \param alg An AEAD algorithm
  256. * (\c PSA_ALG_XXX value such that
  257. * #PSA_ALG_IS_AEAD(\p alg) is true).
  258. *
  259. * \return The tag size for the specified algorithm.
  260. * If the AEAD algorithm does not have an identified
  261. * tag that can be distinguished from the rest of
  262. * the ciphertext, return 0.
  263. * If the AEAD algorithm is not recognized, return 0.
  264. */
  265. #define PSA_AEAD_TAG_LENGTH_1_ARG( alg ) \
  266. MBEDTLS_DEPRECATED_CONSTANT( size_t, \
  267. PSA_ALG_IS_AEAD( alg ) ? \
  268. PSA_ALG_AEAD_GET_TAG_LENGTH( alg ) : \
  269. 0 )
  270. /** The maximum size of the output of psa_aead_encrypt(), in bytes.
  271. *
  272. * If the size of the ciphertext buffer is at least this large, it is
  273. * guaranteed that psa_aead_encrypt() will not fail due to an
  274. * insufficient buffer size. Depending on the algorithm, the actual size of
  275. * the ciphertext may be smaller.
  276. *
  277. * \warning This macro may evaluate its arguments multiple times or
  278. * zero times, so you should not pass arguments that contain
  279. * side effects.
  280. *
  281. * \param alg An AEAD algorithm
  282. * (\c PSA_ALG_XXX value such that
  283. * #PSA_ALG_IS_AEAD(\p alg) is true).
  284. * \param plaintext_length Size of the plaintext in bytes.
  285. *
  286. * \return The AEAD ciphertext size for the specified
  287. * algorithm.
  288. * If the AEAD algorithm is not recognized, return 0.
  289. */
  290. #define PSA_AEAD_ENCRYPT_OUTPUT_SIZE_2_ARG( alg, plaintext_length ) \
  291. MBEDTLS_DEPRECATED_CONSTANT( size_t, \
  292. PSA_ALG_IS_AEAD( alg ) ? \
  293. (plaintext_length) + PSA_ALG_AEAD_GET_TAG_LENGTH( alg ) : \
  294. 0 )
  295. /** The maximum size of the output of psa_aead_decrypt(), in bytes.
  296. *
  297. * If the size of the plaintext buffer is at least this large, it is
  298. * guaranteed that psa_aead_decrypt() will not fail due to an
  299. * insufficient buffer size. Depending on the algorithm, the actual size of
  300. * the plaintext may be smaller.
  301. *
  302. * \warning This macro may evaluate its arguments multiple times or
  303. * zero times, so you should not pass arguments that contain
  304. * side effects.
  305. *
  306. * \param alg An AEAD algorithm
  307. * (\c PSA_ALG_XXX value such that
  308. * #PSA_ALG_IS_AEAD(\p alg) is true).
  309. * \param ciphertext_length Size of the plaintext in bytes.
  310. *
  311. * \return The AEAD ciphertext size for the specified
  312. * algorithm.
  313. * If the AEAD algorithm is not recognized, return 0.
  314. */
  315. #define PSA_AEAD_DECRYPT_OUTPUT_SIZE_2_ARG( alg, ciphertext_length ) \
  316. MBEDTLS_DEPRECATED_CONSTANT( size_t, \
  317. PSA_ALG_IS_AEAD( alg ) && \
  318. (ciphertext_length) > PSA_ALG_AEAD_GET_TAG_LENGTH( alg ) ? \
  319. (ciphertext_length) - PSA_ALG_AEAD_GET_TAG_LENGTH( alg ) : \
  320. 0 )
  321. /** A sufficient output buffer size for psa_aead_update().
  322. *
  323. * If the size of the output buffer is at least this large, it is
  324. * guaranteed that psa_aead_update() will not fail due to an
  325. * insufficient buffer size. The actual size of the output may be smaller
  326. * in any given call.
  327. *
  328. * \warning This macro may evaluate its arguments multiple times or
  329. * zero times, so you should not pass arguments that contain
  330. * side effects.
  331. *
  332. * \param alg An AEAD algorithm
  333. * (\c PSA_ALG_XXX value such that
  334. * #PSA_ALG_IS_AEAD(\p alg) is true).
  335. * \param input_length Size of the input in bytes.
  336. *
  337. * \return A sufficient output buffer size for the specified
  338. * algorithm.
  339. * If the AEAD algorithm is not recognized, return 0.
  340. */
  341. /* For all the AEAD modes defined in this specification, it is possible
  342. * to emit output without delay. However, hardware may not always be
  343. * capable of this. So for modes based on a block cipher, allow the
  344. * implementation to delay the output until it has a full block. */
  345. #define PSA_AEAD_UPDATE_OUTPUT_SIZE_2_ARG( alg, input_length ) \
  346. MBEDTLS_DEPRECATED_CONSTANT( size_t, \
  347. PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER( alg ) ? \
  348. PSA_ROUND_UP_TO_MULTIPLE( PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE, (input_length) ) : \
  349. (input_length) )
  350. /** A sufficient ciphertext buffer size for psa_aead_finish().
  351. *
  352. * If the size of the ciphertext buffer is at least this large, it is
  353. * guaranteed that psa_aead_finish() will not fail due to an
  354. * insufficient ciphertext buffer size. The actual size of the output may
  355. * be smaller in any given call.
  356. *
  357. * \param alg An AEAD algorithm
  358. * (\c PSA_ALG_XXX value such that
  359. * #PSA_ALG_IS_AEAD(\p alg) is true).
  360. *
  361. * \return A sufficient ciphertext buffer size for the
  362. * specified algorithm.
  363. * If the AEAD algorithm is not recognized, return 0.
  364. */
  365. #define PSA_AEAD_FINISH_OUTPUT_SIZE_1_ARG( alg ) \
  366. MBEDTLS_DEPRECATED_CONSTANT( size_t, \
  367. PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER( alg ) ? \
  368. PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \
  369. 0 )
  370. /** A sufficient plaintext buffer size for psa_aead_verify().
  371. *
  372. * If the size of the plaintext buffer is at least this large, it is
  373. * guaranteed that psa_aead_verify() will not fail due to an
  374. * insufficient plaintext buffer size. The actual size of the output may
  375. * be smaller in any given call.
  376. *
  377. * \param alg An AEAD algorithm
  378. * (\c PSA_ALG_XXX value such that
  379. * #PSA_ALG_IS_AEAD(\p alg) is true).
  380. *
  381. * \return A sufficient plaintext buffer size for the
  382. * specified algorithm.
  383. * If the AEAD algorithm is not recognized, return 0.
  384. */
  385. #define PSA_AEAD_VERIFY_OUTPUT_SIZE_1_ARG( alg ) \
  386. MBEDTLS_DEPRECATED_CONSTANT( size_t, \
  387. PSA_ALG_IS_AEAD_ON_BLOCK_CIPHER( alg ) ? \
  388. PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE : \
  389. 0 )
  390. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  391. /** Open a handle to an existing persistent key.
  392. *
  393. * Open a handle to a persistent key. A key is persistent if it was created
  394. * with a lifetime other than #PSA_KEY_LIFETIME_VOLATILE. A persistent key
  395. * always has a nonzero key identifier, set with psa_set_key_id() when
  396. * creating the key. Implementations may provide additional pre-provisioned
  397. * keys that can be opened with psa_open_key(). Such keys have an application
  398. * key identifier in the vendor range, as documented in the description of
  399. * #psa_key_id_t.
  400. *
  401. * The application must eventually close the handle with psa_close_key() or
  402. * psa_destroy_key() to release associated resources. If the application dies
  403. * without calling one of these functions, the implementation should perform
  404. * the equivalent of a call to psa_close_key().
  405. *
  406. * Some implementations permit an application to open the same key multiple
  407. * times. If this is successful, each call to psa_open_key() will return a
  408. * different key handle.
  409. *
  410. * \note This API is not part of the PSA Cryptography API Release 1.0.0
  411. * specification. It was defined in the 1.0 Beta 3 version of the
  412. * specification but was removed in the 1.0.0 released version. This API is
  413. * kept for the time being to not break applications relying on it. It is not
  414. * deprecated yet but will be in the near future.
  415. *
  416. * \note Applications that rely on opening a key multiple times will not be
  417. * portable to implementations that only permit a single key handle to be
  418. * opened. See also :ref:\`key-handles\`.
  419. *
  420. *
  421. * \param key The persistent identifier of the key.
  422. * \param[out] handle On success, a handle to the key.
  423. *
  424. * \retval #PSA_SUCCESS
  425. * Success. The application can now use the value of `*handle`
  426. * to access the key.
  427. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  428. * The implementation does not have sufficient resources to open the
  429. * key. This can be due to reaching an implementation limit on the
  430. * number of open keys, the number of open key handles, or available
  431. * memory.
  432. * \retval #PSA_ERROR_DOES_NOT_EXIST
  433. * There is no persistent key with key identifier \p key.
  434. * \retval #PSA_ERROR_INVALID_ARGUMENT
  435. * \p key is not a valid persistent key identifier.
  436. * \retval #PSA_ERROR_NOT_PERMITTED
  437. * The specified key exists, but the application does not have the
  438. * permission to access it. Note that this specification does not
  439. * define any way to create such a key, but it may be possible
  440. * through implementation-specific means.
  441. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  442. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  443. * \retval #PSA_ERROR_STORAGE_FAILURE
  444. * \retval #PSA_ERROR_DATA_INVALID
  445. * \retval #PSA_ERROR_DATA_CORRUPT
  446. * \retval #PSA_ERROR_BAD_STATE
  447. * The library has not been previously initialized by psa_crypto_init().
  448. * It is implementation-dependent whether a failure to initialize
  449. * results in this error code.
  450. */
  451. psa_status_t psa_open_key( mbedtls_svc_key_id_t key,
  452. psa_key_handle_t *handle );
  453. /** Close a key handle.
  454. *
  455. * If the handle designates a volatile key, this will destroy the key material
  456. * and free all associated resources, just like psa_destroy_key().
  457. *
  458. * If this is the last open handle to a persistent key, then closing the handle
  459. * will free all resources associated with the key in volatile memory. The key
  460. * data in persistent storage is not affected and can be opened again later
  461. * with a call to psa_open_key().
  462. *
  463. * Closing the key handle makes the handle invalid, and the key handle
  464. * must not be used again by the application.
  465. *
  466. * \note This API is not part of the PSA Cryptography API Release 1.0.0
  467. * specification. It was defined in the 1.0 Beta 3 version of the
  468. * specification but was removed in the 1.0.0 released version. This API is
  469. * kept for the time being to not break applications relying on it. It is not
  470. * deprecated yet but will be in the near future.
  471. *
  472. * \note If the key handle was used to set up an active
  473. * :ref:\`multipart operation <multipart-operations>\`, then closing the
  474. * key handle can cause the multipart operation to fail. Applications should
  475. * maintain the key handle until after the multipart operation has finished.
  476. *
  477. * \param handle The key handle to close.
  478. * If this is \c 0, do nothing and return \c PSA_SUCCESS.
  479. *
  480. * \retval #PSA_SUCCESS
  481. * \p handle was a valid handle or \c 0. It is now closed.
  482. * \retval #PSA_ERROR_INVALID_HANDLE
  483. * \p handle is not a valid handle nor \c 0.
  484. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  485. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  486. * \retval #PSA_ERROR_BAD_STATE
  487. * The library has not been previously initialized by psa_crypto_init().
  488. * It is implementation-dependent whether a failure to initialize
  489. * results in this error code.
  490. */
  491. psa_status_t psa_close_key(psa_key_handle_t handle);
  492. #ifdef __cplusplus
  493. }
  494. #endif
  495. #endif /* PSA_CRYPTO_COMPAT_H */