crypto_struct.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * \file psa/crypto_struct.h
  3. *
  4. * \brief PSA cryptography module: Mbed TLS structured type implementations
  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 some data structures with
  10. * implementation-specific definitions.
  11. *
  12. * In implementations with isolation between the application and the
  13. * cryptography module, it is expected that the front-end and the back-end
  14. * would have different versions of this file.
  15. *
  16. * <h3>Design notes about multipart operation structures</h3>
  17. *
  18. * For multipart operations without driver delegation support, each multipart
  19. * operation structure contains a `psa_algorithm_t alg` field which indicates
  20. * which specific algorithm the structure is for. When the structure is not in
  21. * use, `alg` is 0. Most of the structure consists of a union which is
  22. * discriminated by `alg`.
  23. *
  24. * For multipart operations with driver delegation support, each multipart
  25. * operation structure contains an `unsigned int id` field indicating which
  26. * driver got assigned to do the operation. When the structure is not in use,
  27. * 'id' is 0. The structure contains also a driver context which is the union
  28. * of the contexts of all drivers able to handle the type of multipart
  29. * operation.
  30. *
  31. * Note that when `alg` or `id` is 0, the content of other fields is undefined.
  32. * In particular, it is not guaranteed that a freshly-initialized structure
  33. * is all-zero: we initialize structures to something like `{0, 0}`, which
  34. * is only guaranteed to initializes the first member of the union;
  35. * GCC and Clang initialize the whole structure to 0 (at the time of writing),
  36. * but MSVC and CompCert don't.
  37. *
  38. * In Mbed Crypto, multipart operation structures live independently from
  39. * the key. This allows Mbed Crypto to free the key objects when destroying
  40. * a key slot. If a multipart operation needs to remember the key after
  41. * the setup function returns, the operation structure needs to contain a
  42. * copy of the key.
  43. */
  44. /*
  45. * Copyright The Mbed TLS Contributors
  46. * SPDX-License-Identifier: Apache-2.0
  47. *
  48. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  49. * not use this file except in compliance with the License.
  50. * You may obtain a copy of the License at
  51. *
  52. * http://www.apache.org/licenses/LICENSE-2.0
  53. *
  54. * Unless required by applicable law or agreed to in writing, software
  55. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  56. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  57. * See the License for the specific language governing permissions and
  58. * limitations under the License.
  59. */
  60. #ifndef PSA_CRYPTO_STRUCT_H
  61. #define PSA_CRYPTO_STRUCT_H
  62. #include "mbedtls/private_access.h"
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. /* Include the Mbed TLS configuration file, the way Mbed TLS does it
  67. * in each of its header files. */
  68. #include "mbedtls/build_info.h"
  69. #include "mbedtls/cmac.h"
  70. #include "mbedtls/gcm.h"
  71. #include "mbedtls/ccm.h"
  72. #include "mbedtls/chachapoly.h"
  73. /* Include the context definition for the compiled-in drivers for the primitive
  74. * algorithms. */
  75. #include "psa/crypto_driver_contexts_primitives.h"
  76. struct psa_hash_operation_s {
  77. /** Unique ID indicating which driver got assigned to do the
  78. * operation. Since driver contexts are driver-specific, swapping
  79. * drivers halfway through the operation is not supported.
  80. * ID values are auto-generated in psa_driver_wrappers.h.
  81. * ID value zero means the context is not valid or not assigned to
  82. * any driver (i.e. the driver context is not active, in use). */
  83. unsigned int MBEDTLS_PRIVATE(id);
  84. psa_driver_hash_context_t MBEDTLS_PRIVATE(ctx);
  85. };
  86. #define PSA_HASH_OPERATION_INIT { 0, { 0 } }
  87. static inline struct psa_hash_operation_s psa_hash_operation_init(void)
  88. {
  89. const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
  90. return v;
  91. }
  92. struct psa_cipher_operation_s {
  93. /** Unique ID indicating which driver got assigned to do the
  94. * operation. Since driver contexts are driver-specific, swapping
  95. * drivers halfway through the operation is not supported.
  96. * ID values are auto-generated in psa_crypto_driver_wrappers.h
  97. * ID value zero means the context is not valid or not assigned to
  98. * any driver (i.e. none of the driver contexts are active). */
  99. unsigned int MBEDTLS_PRIVATE(id);
  100. unsigned int MBEDTLS_PRIVATE(iv_required) : 1;
  101. unsigned int MBEDTLS_PRIVATE(iv_set) : 1;
  102. uint8_t MBEDTLS_PRIVATE(default_iv_length);
  103. psa_driver_cipher_context_t MBEDTLS_PRIVATE(ctx);
  104. };
  105. #define PSA_CIPHER_OPERATION_INIT { 0, 0, 0, 0, { 0 } }
  106. static inline struct psa_cipher_operation_s psa_cipher_operation_init(void)
  107. {
  108. const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
  109. return v;
  110. }
  111. /* Include the context definition for the compiled-in drivers for the composite
  112. * algorithms. */
  113. #include "psa/crypto_driver_contexts_composites.h"
  114. struct psa_mac_operation_s {
  115. /** Unique ID indicating which driver got assigned to do the
  116. * operation. Since driver contexts are driver-specific, swapping
  117. * drivers halfway through the operation is not supported.
  118. * ID values are auto-generated in psa_driver_wrappers.h
  119. * ID value zero means the context is not valid or not assigned to
  120. * any driver (i.e. none of the driver contexts are active). */
  121. unsigned int MBEDTLS_PRIVATE(id);
  122. uint8_t MBEDTLS_PRIVATE(mac_size);
  123. unsigned int MBEDTLS_PRIVATE(is_sign) : 1;
  124. psa_driver_mac_context_t MBEDTLS_PRIVATE(ctx);
  125. };
  126. #define PSA_MAC_OPERATION_INIT { 0, 0, 0, { 0 } }
  127. static inline struct psa_mac_operation_s psa_mac_operation_init(void)
  128. {
  129. const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
  130. return v;
  131. }
  132. struct psa_aead_operation_s {
  133. /** Unique ID indicating which driver got assigned to do the
  134. * operation. Since driver contexts are driver-specific, swapping
  135. * drivers halfway through the operation is not supported.
  136. * ID values are auto-generated in psa_crypto_driver_wrappers.h
  137. * ID value zero means the context is not valid or not assigned to
  138. * any driver (i.e. none of the driver contexts are active). */
  139. unsigned int MBEDTLS_PRIVATE(id);
  140. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  141. psa_key_type_t MBEDTLS_PRIVATE(key_type);
  142. size_t MBEDTLS_PRIVATE(ad_remaining);
  143. size_t MBEDTLS_PRIVATE(body_remaining);
  144. unsigned int MBEDTLS_PRIVATE(nonce_set) : 1;
  145. unsigned int MBEDTLS_PRIVATE(lengths_set) : 1;
  146. unsigned int MBEDTLS_PRIVATE(ad_started) : 1;
  147. unsigned int MBEDTLS_PRIVATE(body_started) : 1;
  148. unsigned int MBEDTLS_PRIVATE(is_encrypt) : 1;
  149. psa_driver_aead_context_t MBEDTLS_PRIVATE(ctx);
  150. };
  151. #define PSA_AEAD_OPERATION_INIT { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, { 0 } }
  152. static inline struct psa_aead_operation_s psa_aead_operation_init(void)
  153. {
  154. const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
  155. return v;
  156. }
  157. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
  158. defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
  159. defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
  160. typedef struct {
  161. uint8_t *MBEDTLS_PRIVATE(info);
  162. size_t MBEDTLS_PRIVATE(info_length);
  163. #if PSA_HASH_MAX_SIZE > 0xff
  164. #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
  165. #endif
  166. uint8_t MBEDTLS_PRIVATE(offset_in_block);
  167. uint8_t MBEDTLS_PRIVATE(block_number);
  168. unsigned int MBEDTLS_PRIVATE(state) : 2;
  169. unsigned int MBEDTLS_PRIVATE(info_set) : 1;
  170. uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
  171. uint8_t MBEDTLS_PRIVATE(prk)[PSA_HASH_MAX_SIZE];
  172. struct psa_mac_operation_s MBEDTLS_PRIVATE(hmac);
  173. } psa_hkdf_key_derivation_t;
  174. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF ||
  175. MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT ||
  176. MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND */
  177. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
  178. typedef struct {
  179. uint8_t MBEDTLS_PRIVATE(data)[PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE];
  180. } psa_tls12_ecjpake_to_pms_t;
  181. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS */
  182. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  183. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  184. typedef enum {
  185. PSA_TLS12_PRF_STATE_INIT, /* no input provided */
  186. PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */
  187. PSA_TLS12_PRF_STATE_OTHER_KEY_SET, /* other key has been set - optional */
  188. PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */
  189. PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */
  190. PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */
  191. } psa_tls12_prf_key_derivation_state_t;
  192. typedef struct psa_tls12_prf_key_derivation_s {
  193. #if PSA_HASH_MAX_SIZE > 0xff
  194. #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
  195. #endif
  196. /* Indicates how many bytes in the current HMAC block have
  197. * not yet been read by the user. */
  198. uint8_t MBEDTLS_PRIVATE(left_in_block);
  199. /* The 1-based number of the block. */
  200. uint8_t MBEDTLS_PRIVATE(block_number);
  201. psa_tls12_prf_key_derivation_state_t MBEDTLS_PRIVATE(state);
  202. uint8_t *MBEDTLS_PRIVATE(secret);
  203. size_t MBEDTLS_PRIVATE(secret_length);
  204. uint8_t *MBEDTLS_PRIVATE(seed);
  205. size_t MBEDTLS_PRIVATE(seed_length);
  206. uint8_t *MBEDTLS_PRIVATE(label);
  207. size_t MBEDTLS_PRIVATE(label_length);
  208. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  209. uint8_t *MBEDTLS_PRIVATE(other_secret);
  210. size_t MBEDTLS_PRIVATE(other_secret_length);
  211. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  212. uint8_t MBEDTLS_PRIVATE(Ai)[PSA_HASH_MAX_SIZE];
  213. /* `HMAC_hash( prk, A( i ) + seed )` in the notation of RFC 5246, Sect. 5. */
  214. uint8_t MBEDTLS_PRIVATE(output_block)[PSA_HASH_MAX_SIZE];
  215. } psa_tls12_prf_key_derivation_t;
  216. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
  217. * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  218. struct psa_key_derivation_s {
  219. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  220. unsigned int MBEDTLS_PRIVATE(can_output_key) : 1;
  221. size_t MBEDTLS_PRIVATE(capacity);
  222. union {
  223. /* Make the union non-empty even with no supported algorithms. */
  224. uint8_t MBEDTLS_PRIVATE(dummy);
  225. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
  226. defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXTRACT) || \
  227. defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF_EXPAND)
  228. psa_hkdf_key_derivation_t MBEDTLS_PRIVATE(hkdf);
  229. #endif
  230. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  231. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  232. psa_tls12_prf_key_derivation_t MBEDTLS_PRIVATE(tls12_prf);
  233. #endif
  234. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_ECJPAKE_TO_PMS)
  235. psa_tls12_ecjpake_to_pms_t MBEDTLS_PRIVATE(tls12_ecjpake_to_pms);
  236. #endif
  237. } MBEDTLS_PRIVATE(ctx);
  238. };
  239. /* This only zeroes out the first byte in the union, the rest is unspecified. */
  240. #define PSA_KEY_DERIVATION_OPERATION_INIT { 0, 0, 0, { 0 } }
  241. static inline struct psa_key_derivation_s psa_key_derivation_operation_init(
  242. void)
  243. {
  244. const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
  245. return v;
  246. }
  247. struct psa_key_policy_s {
  248. psa_key_usage_t MBEDTLS_PRIVATE(usage);
  249. psa_algorithm_t MBEDTLS_PRIVATE(alg);
  250. psa_algorithm_t MBEDTLS_PRIVATE(alg2);
  251. };
  252. typedef struct psa_key_policy_s psa_key_policy_t;
  253. #define PSA_KEY_POLICY_INIT { 0, 0, 0 }
  254. static inline struct psa_key_policy_s psa_key_policy_init(void)
  255. {
  256. const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
  257. return v;
  258. }
  259. /* The type used internally for key sizes.
  260. * Public interfaces use size_t, but internally we use a smaller type. */
  261. typedef uint16_t psa_key_bits_t;
  262. /* The maximum value of the type used to represent bit-sizes.
  263. * This is used to mark an invalid key size. */
  264. #define PSA_KEY_BITS_TOO_LARGE ((psa_key_bits_t) -1)
  265. /* The maximum size of a key in bits.
  266. * Currently defined as the maximum that can be represented, rounded down
  267. * to a whole number of bytes.
  268. * This is an uncast value so that it can be used in preprocessor
  269. * conditionals. */
  270. #define PSA_MAX_KEY_BITS 0xfff8
  271. /** A mask of flags that can be stored in key attributes.
  272. *
  273. * This type is also used internally to store flags in slots. Internal
  274. * flags are defined in library/psa_crypto_core.h. Internal flags may have
  275. * the same value as external flags if they are properly handled during
  276. * key creation and in psa_get_key_attributes.
  277. */
  278. typedef uint16_t psa_key_attributes_flag_t;
  279. #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
  280. ((psa_key_attributes_flag_t) 0x0001)
  281. /* A mask of key attribute flags used externally only.
  282. * Only meant for internal checks inside the library. */
  283. #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
  284. MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
  285. 0)
  286. /* A mask of key attribute flags used both internally and externally.
  287. * Currently there aren't any. */
  288. #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
  289. 0)
  290. typedef struct {
  291. psa_key_type_t MBEDTLS_PRIVATE(type);
  292. psa_key_bits_t MBEDTLS_PRIVATE(bits);
  293. psa_key_lifetime_t MBEDTLS_PRIVATE(lifetime);
  294. mbedtls_svc_key_id_t MBEDTLS_PRIVATE(id);
  295. psa_key_policy_t MBEDTLS_PRIVATE(policy);
  296. psa_key_attributes_flag_t MBEDTLS_PRIVATE(flags);
  297. } psa_core_key_attributes_t;
  298. #define PSA_CORE_KEY_ATTRIBUTES_INIT { PSA_KEY_TYPE_NONE, 0, \
  299. PSA_KEY_LIFETIME_VOLATILE, \
  300. MBEDTLS_SVC_KEY_ID_INIT, \
  301. PSA_KEY_POLICY_INIT, 0 }
  302. struct psa_key_attributes_s {
  303. psa_core_key_attributes_t MBEDTLS_PRIVATE(core);
  304. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  305. psa_key_slot_number_t MBEDTLS_PRIVATE(slot_number);
  306. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  307. void *MBEDTLS_PRIVATE(domain_parameters);
  308. size_t MBEDTLS_PRIVATE(domain_parameters_size);
  309. };
  310. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  311. #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0 }
  312. #else
  313. #define PSA_KEY_ATTRIBUTES_INIT { PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0 }
  314. #endif
  315. static inline struct psa_key_attributes_s psa_key_attributes_init(void)
  316. {
  317. const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
  318. return v;
  319. }
  320. static inline void psa_set_key_id(psa_key_attributes_t *attributes,
  321. mbedtls_svc_key_id_t key)
  322. {
  323. psa_key_lifetime_t lifetime = attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
  324. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = key;
  325. if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
  326. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) =
  327. PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
  328. PSA_KEY_LIFETIME_PERSISTENT,
  329. PSA_KEY_LIFETIME_GET_LOCATION(lifetime));
  330. }
  331. }
  332. static inline mbedtls_svc_key_id_t psa_get_key_id(
  333. const psa_key_attributes_t *attributes)
  334. {
  335. return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id);
  336. }
  337. #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
  338. static inline void mbedtls_set_key_owner_id(psa_key_attributes_t *attributes,
  339. mbedtls_key_owner_id_t owner)
  340. {
  341. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(owner) = owner;
  342. }
  343. #endif
  344. static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
  345. psa_key_lifetime_t lifetime)
  346. {
  347. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime) = lifetime;
  348. if (PSA_KEY_LIFETIME_IS_VOLATILE(lifetime)) {
  349. #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
  350. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id).MBEDTLS_PRIVATE(key_id) = 0;
  351. #else
  352. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(id) = 0;
  353. #endif
  354. }
  355. }
  356. static inline psa_key_lifetime_t psa_get_key_lifetime(
  357. const psa_key_attributes_t *attributes)
  358. {
  359. return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(lifetime);
  360. }
  361. static inline void psa_extend_key_usage_flags(psa_key_usage_t *usage_flags)
  362. {
  363. if (*usage_flags & PSA_KEY_USAGE_SIGN_HASH) {
  364. *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
  365. }
  366. if (*usage_flags & PSA_KEY_USAGE_VERIFY_HASH) {
  367. *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
  368. }
  369. }
  370. static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
  371. psa_key_usage_t usage_flags)
  372. {
  373. psa_extend_key_usage_flags(&usage_flags);
  374. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage) = usage_flags;
  375. }
  376. static inline psa_key_usage_t psa_get_key_usage_flags(
  377. const psa_key_attributes_t *attributes)
  378. {
  379. return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(usage);
  380. }
  381. static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
  382. psa_algorithm_t alg)
  383. {
  384. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg) = alg;
  385. }
  386. static inline psa_algorithm_t psa_get_key_algorithm(
  387. const psa_key_attributes_t *attributes)
  388. {
  389. return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(policy).MBEDTLS_PRIVATE(alg);
  390. }
  391. /* This function is declared in crypto_extra.h, which comes after this
  392. * header file, but we need the function here, so repeat the declaration. */
  393. psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
  394. psa_key_type_t type,
  395. const uint8_t *data,
  396. size_t data_length);
  397. static inline void psa_set_key_type(psa_key_attributes_t *attributes,
  398. psa_key_type_t type)
  399. {
  400. if (attributes->MBEDTLS_PRIVATE(domain_parameters) == NULL) {
  401. /* Common case: quick path */
  402. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type) = type;
  403. } else {
  404. /* Call the bigger function to free the old domain parameters.
  405. * Ignore any errors which may arise due to type requiring
  406. * non-default domain parameters, since this function can't
  407. * report errors. */
  408. (void) psa_set_key_domain_parameters(attributes, type, NULL, 0);
  409. }
  410. }
  411. static inline psa_key_type_t psa_get_key_type(
  412. const psa_key_attributes_t *attributes)
  413. {
  414. return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(type);
  415. }
  416. static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
  417. size_t bits)
  418. {
  419. if (bits > PSA_MAX_KEY_BITS) {
  420. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = PSA_KEY_BITS_TOO_LARGE;
  421. } else {
  422. attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits) = (psa_key_bits_t) bits;
  423. }
  424. }
  425. static inline size_t psa_get_key_bits(
  426. const psa_key_attributes_t *attributes)
  427. {
  428. return attributes->MBEDTLS_PRIVATE(core).MBEDTLS_PRIVATE(bits);
  429. }
  430. /**
  431. * \brief The context for PSA interruptible hash signing.
  432. */
  433. struct psa_sign_hash_interruptible_operation_s {
  434. /** Unique ID indicating which driver got assigned to do the
  435. * operation. Since driver contexts are driver-specific, swapping
  436. * drivers halfway through the operation is not supported.
  437. * ID values are auto-generated in psa_crypto_driver_wrappers.h
  438. * ID value zero means the context is not valid or not assigned to
  439. * any driver (i.e. none of the driver contexts are active). */
  440. unsigned int MBEDTLS_PRIVATE(id);
  441. psa_driver_sign_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
  442. unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
  443. uint32_t MBEDTLS_PRIVATE(num_ops);
  444. };
  445. #define PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
  446. static inline struct psa_sign_hash_interruptible_operation_s
  447. psa_sign_hash_interruptible_operation_init(void)
  448. {
  449. const struct psa_sign_hash_interruptible_operation_s v =
  450. PSA_SIGN_HASH_INTERRUPTIBLE_OPERATION_INIT;
  451. return v;
  452. }
  453. /**
  454. * \brief The context for PSA interruptible hash verification.
  455. */
  456. struct psa_verify_hash_interruptible_operation_s {
  457. /** Unique ID indicating which driver got assigned to do the
  458. * operation. Since driver contexts are driver-specific, swapping
  459. * drivers halfway through the operation is not supported.
  460. * ID values are auto-generated in psa_crypto_driver_wrappers.h
  461. * ID value zero means the context is not valid or not assigned to
  462. * any driver (i.e. none of the driver contexts are active). */
  463. unsigned int MBEDTLS_PRIVATE(id);
  464. psa_driver_verify_hash_interruptible_context_t MBEDTLS_PRIVATE(ctx);
  465. unsigned int MBEDTLS_PRIVATE(error_occurred) : 1;
  466. uint32_t MBEDTLS_PRIVATE(num_ops);
  467. };
  468. #define PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT { 0, { 0 }, 0, 0 }
  469. static inline struct psa_verify_hash_interruptible_operation_s
  470. psa_verify_hash_interruptible_operation_init(void)
  471. {
  472. const struct psa_verify_hash_interruptible_operation_s v =
  473. PSA_VERIFY_HASH_INTERRUPTIBLE_OPERATION_INIT;
  474. return v;
  475. }
  476. #ifdef __cplusplus
  477. }
  478. #endif
  479. #endif /* PSA_CRYPTO_STRUCT_H */