crypto_struct.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. #ifdef __cplusplus
  63. extern "C" {
  64. #endif
  65. /* Include the Mbed TLS configuration file, the way Mbed TLS does it
  66. * in each of its header files. */
  67. #if !defined(MBEDTLS_CONFIG_FILE)
  68. #include "mbedtls/config.h"
  69. #else
  70. #include MBEDTLS_CONFIG_FILE
  71. #endif
  72. #include "mbedtls/cmac.h"
  73. #include "mbedtls/gcm.h"
  74. /* Include the context definition for the compiled-in drivers for the primitive
  75. * algorithms. */
  76. #include "psa/crypto_driver_contexts_primitives.h"
  77. struct psa_hash_operation_s
  78. {
  79. /** Unique ID indicating which driver got assigned to do the
  80. * operation. Since driver contexts are driver-specific, swapping
  81. * drivers halfway through the operation is not supported.
  82. * ID values are auto-generated in psa_driver_wrappers.h.
  83. * ID value zero means the context is not valid or not assigned to
  84. * any driver (i.e. the driver context is not active, in use). */
  85. unsigned int id;
  86. psa_driver_hash_context_t ctx;
  87. };
  88. #define PSA_HASH_OPERATION_INIT {0, {0}}
  89. static inline struct psa_hash_operation_s psa_hash_operation_init( void )
  90. {
  91. const struct psa_hash_operation_s v = PSA_HASH_OPERATION_INIT;
  92. return( v );
  93. }
  94. struct psa_cipher_operation_s
  95. {
  96. /** Unique ID indicating which driver got assigned to do the
  97. * operation. Since driver contexts are driver-specific, swapping
  98. * drivers halfway through the operation is not supported.
  99. * ID values are auto-generated in psa_crypto_driver_wrappers.h
  100. * ID value zero means the context is not valid or not assigned to
  101. * any driver (i.e. none of the driver contexts are active). */
  102. unsigned int id;
  103. unsigned int iv_required : 1;
  104. unsigned int iv_set : 1;
  105. uint8_t default_iv_length;
  106. psa_driver_cipher_context_t ctx;
  107. };
  108. #define PSA_CIPHER_OPERATION_INIT {0, 0, 0, 0, {0}}
  109. static inline struct psa_cipher_operation_s psa_cipher_operation_init( void )
  110. {
  111. const struct psa_cipher_operation_s v = PSA_CIPHER_OPERATION_INIT;
  112. return( v );
  113. }
  114. /* Include the context definition for the compiled-in drivers for the composite
  115. * algorithms. */
  116. #include "psa/crypto_driver_contexts_composites.h"
  117. struct psa_mac_operation_s
  118. {
  119. /** Unique ID indicating which driver got assigned to do the
  120. * operation. Since driver contexts are driver-specific, swapping
  121. * drivers halfway through the operation is not supported.
  122. * ID values are auto-generated in psa_driver_wrappers.h
  123. * ID value zero means the context is not valid or not assigned to
  124. * any driver (i.e. none of the driver contexts are active). */
  125. unsigned int id;
  126. uint8_t mac_size;
  127. unsigned int is_sign : 1;
  128. psa_driver_mac_context_t ctx;
  129. };
  130. #define PSA_MAC_OPERATION_INIT {0, 0, 0, {0}}
  131. static inline struct psa_mac_operation_s psa_mac_operation_init( void )
  132. {
  133. const struct psa_mac_operation_s v = PSA_MAC_OPERATION_INIT;
  134. return( v );
  135. }
  136. struct psa_aead_operation_s
  137. {
  138. psa_algorithm_t alg;
  139. unsigned int key_set : 1;
  140. unsigned int iv_set : 1;
  141. uint8_t iv_size;
  142. uint8_t block_size;
  143. union
  144. {
  145. unsigned dummy; /* Enable easier initializing of the union. */
  146. mbedtls_cipher_context_t cipher;
  147. } ctx;
  148. };
  149. #define PSA_AEAD_OPERATION_INIT {0, 0, 0, 0, 0, {0}}
  150. static inline struct psa_aead_operation_s psa_aead_operation_init( void )
  151. {
  152. const struct psa_aead_operation_s v = PSA_AEAD_OPERATION_INIT;
  153. return( v );
  154. }
  155. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  156. typedef struct
  157. {
  158. uint8_t *info;
  159. size_t info_length;
  160. #if PSA_HASH_MAX_SIZE > 0xff
  161. #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
  162. #endif
  163. uint8_t offset_in_block;
  164. uint8_t block_number;
  165. unsigned int state : 2;
  166. unsigned int info_set : 1;
  167. uint8_t output_block[PSA_HASH_MAX_SIZE];
  168. uint8_t prk[PSA_HASH_MAX_SIZE];
  169. struct psa_mac_operation_s hmac;
  170. } psa_hkdf_key_derivation_t;
  171. #endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
  172. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  173. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  174. typedef enum
  175. {
  176. PSA_TLS12_PRF_STATE_INIT, /* no input provided */
  177. PSA_TLS12_PRF_STATE_SEED_SET, /* seed has been set */
  178. PSA_TLS12_PRF_STATE_KEY_SET, /* key has been set */
  179. PSA_TLS12_PRF_STATE_LABEL_SET, /* label has been set */
  180. PSA_TLS12_PRF_STATE_OUTPUT /* output has been started */
  181. } psa_tls12_prf_key_derivation_state_t;
  182. typedef struct psa_tls12_prf_key_derivation_s
  183. {
  184. #if PSA_HASH_MAX_SIZE > 0xff
  185. #error "PSA_HASH_MAX_SIZE does not fit in uint8_t"
  186. #endif
  187. /* Indicates how many bytes in the current HMAC block have
  188. * not yet been read by the user. */
  189. uint8_t left_in_block;
  190. /* The 1-based number of the block. */
  191. uint8_t block_number;
  192. psa_tls12_prf_key_derivation_state_t state;
  193. uint8_t *secret;
  194. size_t secret_length;
  195. uint8_t *seed;
  196. size_t seed_length;
  197. uint8_t *label;
  198. size_t label_length;
  199. uint8_t Ai[PSA_HASH_MAX_SIZE];
  200. /* `HMAC_hash( prk, A(i) + seed )` in the notation of RFC 5246, Sect. 5. */
  201. uint8_t output_block[PSA_HASH_MAX_SIZE];
  202. } psa_tls12_prf_key_derivation_t;
  203. #endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
  204. * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
  205. struct psa_key_derivation_s
  206. {
  207. psa_algorithm_t alg;
  208. unsigned int can_output_key : 1;
  209. size_t capacity;
  210. union
  211. {
  212. /* Make the union non-empty even with no supported algorithms. */
  213. uint8_t dummy;
  214. #if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
  215. psa_hkdf_key_derivation_t hkdf;
  216. #endif
  217. #if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
  218. defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
  219. psa_tls12_prf_key_derivation_t tls12_prf;
  220. #endif
  221. } ctx;
  222. };
  223. /* This only zeroes out the first byte in the union, the rest is unspecified. */
  224. #define PSA_KEY_DERIVATION_OPERATION_INIT {0, 0, 0, {0}}
  225. static inline struct psa_key_derivation_s psa_key_derivation_operation_init( void )
  226. {
  227. const struct psa_key_derivation_s v = PSA_KEY_DERIVATION_OPERATION_INIT;
  228. return( v );
  229. }
  230. struct psa_key_policy_s
  231. {
  232. psa_key_usage_t usage;
  233. psa_algorithm_t alg;
  234. psa_algorithm_t alg2;
  235. };
  236. typedef struct psa_key_policy_s psa_key_policy_t;
  237. #define PSA_KEY_POLICY_INIT {0, 0, 0}
  238. static inline struct psa_key_policy_s psa_key_policy_init( void )
  239. {
  240. const struct psa_key_policy_s v = PSA_KEY_POLICY_INIT;
  241. return( v );
  242. }
  243. /* The type used internally for key sizes.
  244. * Public interfaces use size_t, but internally we use a smaller type. */
  245. typedef uint16_t psa_key_bits_t;
  246. /* The maximum value of the type used to represent bit-sizes.
  247. * This is used to mark an invalid key size. */
  248. #define PSA_KEY_BITS_TOO_LARGE ( (psa_key_bits_t) ( -1 ) )
  249. /* The maximum size of a key in bits.
  250. * Currently defined as the maximum that can be represented, rounded down
  251. * to a whole number of bytes.
  252. * This is an uncast value so that it can be used in preprocessor
  253. * conditionals. */
  254. #define PSA_MAX_KEY_BITS 0xfff8
  255. /** A mask of flags that can be stored in key attributes.
  256. *
  257. * This type is also used internally to store flags in slots. Internal
  258. * flags are defined in library/psa_crypto_core.h. Internal flags may have
  259. * the same value as external flags if they are properly handled during
  260. * key creation and in psa_get_key_attributes.
  261. */
  262. typedef uint16_t psa_key_attributes_flag_t;
  263. #define MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER \
  264. ( (psa_key_attributes_flag_t) 0x0001 )
  265. /* A mask of key attribute flags used externally only.
  266. * Only meant for internal checks inside the library. */
  267. #define MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ( \
  268. MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER | \
  269. 0 )
  270. /* A mask of key attribute flags used both internally and externally.
  271. * Currently there aren't any. */
  272. #define MBEDTLS_PSA_KA_MASK_DUAL_USE ( \
  273. 0 )
  274. typedef struct
  275. {
  276. psa_key_type_t type;
  277. psa_key_bits_t bits;
  278. psa_key_lifetime_t lifetime;
  279. mbedtls_svc_key_id_t id;
  280. psa_key_policy_t policy;
  281. psa_key_attributes_flag_t flags;
  282. } psa_core_key_attributes_t;
  283. #define PSA_CORE_KEY_ATTRIBUTES_INIT {PSA_KEY_TYPE_NONE, 0, PSA_KEY_LIFETIME_VOLATILE, MBEDTLS_SVC_KEY_ID_INIT, PSA_KEY_POLICY_INIT, 0}
  284. struct psa_key_attributes_s
  285. {
  286. psa_core_key_attributes_t core;
  287. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  288. psa_key_slot_number_t slot_number;
  289. #endif /* MBEDTLS_PSA_CRYPTO_SE_C */
  290. void *domain_parameters;
  291. size_t domain_parameters_size;
  292. };
  293. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  294. #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, 0, NULL, 0}
  295. #else
  296. #define PSA_KEY_ATTRIBUTES_INIT {PSA_CORE_KEY_ATTRIBUTES_INIT, NULL, 0}
  297. #endif
  298. static inline struct psa_key_attributes_s psa_key_attributes_init( void )
  299. {
  300. const struct psa_key_attributes_s v = PSA_KEY_ATTRIBUTES_INIT;
  301. return( v );
  302. }
  303. static inline void psa_set_key_id( psa_key_attributes_t *attributes,
  304. mbedtls_svc_key_id_t key )
  305. {
  306. psa_key_lifetime_t lifetime = attributes->core.lifetime;
  307. attributes->core.id = key;
  308. if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
  309. {
  310. attributes->core.lifetime =
  311. PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION(
  312. PSA_KEY_LIFETIME_PERSISTENT,
  313. PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) );
  314. }
  315. }
  316. static inline mbedtls_svc_key_id_t psa_get_key_id(
  317. const psa_key_attributes_t *attributes)
  318. {
  319. return( attributes->core.id );
  320. }
  321. #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
  322. static inline void mbedtls_set_key_owner_id( psa_key_attributes_t *attributes,
  323. mbedtls_key_owner_id_t owner )
  324. {
  325. attributes->core.id.owner = owner;
  326. }
  327. #endif
  328. static inline void psa_set_key_lifetime(psa_key_attributes_t *attributes,
  329. psa_key_lifetime_t lifetime)
  330. {
  331. attributes->core.lifetime = lifetime;
  332. if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
  333. {
  334. #ifdef MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER
  335. attributes->core.id.key_id = 0;
  336. #else
  337. attributes->core.id = 0;
  338. #endif
  339. }
  340. }
  341. static inline psa_key_lifetime_t psa_get_key_lifetime(
  342. const psa_key_attributes_t *attributes)
  343. {
  344. return( attributes->core.lifetime );
  345. }
  346. static inline void psa_extend_key_usage_flags( psa_key_usage_t *usage_flags )
  347. {
  348. if( *usage_flags & PSA_KEY_USAGE_SIGN_HASH )
  349. *usage_flags |= PSA_KEY_USAGE_SIGN_MESSAGE;
  350. if( *usage_flags & PSA_KEY_USAGE_VERIFY_HASH )
  351. *usage_flags |= PSA_KEY_USAGE_VERIFY_MESSAGE;
  352. }
  353. static inline void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
  354. psa_key_usage_t usage_flags)
  355. {
  356. psa_extend_key_usage_flags( &usage_flags );
  357. attributes->core.policy.usage = usage_flags;
  358. }
  359. static inline psa_key_usage_t psa_get_key_usage_flags(
  360. const psa_key_attributes_t *attributes)
  361. {
  362. return( attributes->core.policy.usage );
  363. }
  364. static inline void psa_set_key_algorithm(psa_key_attributes_t *attributes,
  365. psa_algorithm_t alg)
  366. {
  367. attributes->core.policy.alg = alg;
  368. }
  369. static inline psa_algorithm_t psa_get_key_algorithm(
  370. const psa_key_attributes_t *attributes)
  371. {
  372. return( attributes->core.policy.alg );
  373. }
  374. /* This function is declared in crypto_extra.h, which comes after this
  375. * header file, but we need the function here, so repeat the declaration. */
  376. psa_status_t psa_set_key_domain_parameters(psa_key_attributes_t *attributes,
  377. psa_key_type_t type,
  378. const uint8_t *data,
  379. size_t data_length);
  380. static inline void psa_set_key_type(psa_key_attributes_t *attributes,
  381. psa_key_type_t type)
  382. {
  383. if( attributes->domain_parameters == NULL )
  384. {
  385. /* Common case: quick path */
  386. attributes->core.type = type;
  387. }
  388. else
  389. {
  390. /* Call the bigger function to free the old domain paramteres.
  391. * Ignore any errors which may arise due to type requiring
  392. * non-default domain parameters, since this function can't
  393. * report errors. */
  394. (void) psa_set_key_domain_parameters( attributes, type, NULL, 0 );
  395. }
  396. }
  397. static inline psa_key_type_t psa_get_key_type(
  398. const psa_key_attributes_t *attributes)
  399. {
  400. return( attributes->core.type );
  401. }
  402. static inline void psa_set_key_bits(psa_key_attributes_t *attributes,
  403. size_t bits)
  404. {
  405. if( bits > PSA_MAX_KEY_BITS )
  406. attributes->core.bits = PSA_KEY_BITS_TOO_LARGE;
  407. else
  408. attributes->core.bits = (psa_key_bits_t) bits;
  409. }
  410. static inline size_t psa_get_key_bits(
  411. const psa_key_attributes_t *attributes)
  412. {
  413. return( attributes->core.bits );
  414. }
  415. #ifdef __cplusplus
  416. }
  417. #endif
  418. #endif /* PSA_CRYPTO_STRUCT_H */