crypto_se_driver.h 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /**
  2. * \file psa/crypto_se_driver.h
  3. * \brief PSA external cryptoprocessor driver module
  4. *
  5. * This header declares types and function signatures for cryptography
  6. * drivers that access key material via opaque references.
  7. * This is meant for cryptoprocessors that have a separate key storage from the
  8. * space in which the PSA Crypto implementation runs, typically secure
  9. * elements (SEs).
  10. *
  11. * This file is part of the PSA Crypto Driver HAL (hardware abstraction layer),
  12. * containing functions for driver developers to implement to enable hardware
  13. * to be called in a standardized way by a PSA Cryptography API
  14. * implementation. The functions comprising the driver HAL, which driver
  15. * authors implement, are not intended to be called by application developers.
  16. */
  17. /*
  18. * Copyright The Mbed TLS Contributors
  19. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  20. */
  21. #ifndef PSA_CRYPTO_SE_DRIVER_H
  22. #define PSA_CRYPTO_SE_DRIVER_H
  23. #include "mbedtls/private_access.h"
  24. #include "crypto_driver_common.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. /** \defgroup se_init Secure element driver initialization
  29. */
  30. /**@{*/
  31. /** \brief Driver context structure
  32. *
  33. * Driver functions receive a pointer to this structure.
  34. * Each registered driver has one instance of this structure.
  35. *
  36. * Implementations must include the fields specified here and
  37. * may include other fields.
  38. */
  39. typedef struct {
  40. /** A read-only pointer to the driver's persistent data.
  41. *
  42. * Drivers typically use this persistent data to keep track of
  43. * which slot numbers are available. This is only a guideline:
  44. * drivers may use the persistent data for any purpose, keeping
  45. * in mind the restrictions on when the persistent data is saved
  46. * to storage: the persistent data is only saved after calling
  47. * certain functions that receive a writable pointer to the
  48. * persistent data.
  49. *
  50. * The core allocates a memory buffer for the persistent data.
  51. * The pointer is guaranteed to be suitably aligned for any data type,
  52. * like a pointer returned by `malloc` (but the core can use any
  53. * method to allocate the buffer, not necessarily `malloc`).
  54. *
  55. * The size of this buffer is in the \c persistent_data_size field of
  56. * this structure.
  57. *
  58. * Before the driver is initialized for the first time, the content of
  59. * the persistent data is all-bits-zero. After a driver upgrade, if the
  60. * size of the persistent data has increased, the original data is padded
  61. * on the right with zeros; if the size has decreased, the original data
  62. * is truncated to the new size.
  63. *
  64. * This pointer is to read-only data. Only a few driver functions are
  65. * allowed to modify the persistent data. These functions receive a
  66. * writable pointer. These functions are:
  67. * - psa_drv_se_t::p_init
  68. * - psa_drv_se_key_management_t::p_allocate
  69. * - psa_drv_se_key_management_t::p_destroy
  70. *
  71. * The PSA Cryptography core saves the persistent data from one
  72. * session to the next. It does this before returning from API functions
  73. * that call a driver method that is allowed to modify the persistent
  74. * data, specifically:
  75. * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call
  76. * psa_drv_se_key_management_t::p_destroy to complete an action
  77. * that was interrupted by a power failure.
  78. * - Key creation functions cause a call to
  79. * psa_drv_se_key_management_t::p_allocate, and may cause a call to
  80. * psa_drv_se_key_management_t::p_destroy in case an error occurs.
  81. * - psa_destroy_key() causes a call to
  82. * psa_drv_se_key_management_t::p_destroy.
  83. */
  84. const void *const MBEDTLS_PRIVATE(persistent_data);
  85. /** The size of \c persistent_data in bytes.
  86. *
  87. * This is always equal to the value of the `persistent_data_size` field
  88. * of the ::psa_drv_se_t structure when the driver is registered.
  89. */
  90. const size_t MBEDTLS_PRIVATE(persistent_data_size);
  91. /** Driver transient data.
  92. *
  93. * The core initializes this value to 0 and does not read or modify it
  94. * afterwards. The driver may store whatever it wants in this field.
  95. */
  96. uintptr_t MBEDTLS_PRIVATE(transient_data);
  97. } psa_drv_se_context_t;
  98. /** \brief A driver initialization function.
  99. *
  100. * \param[in,out] drv_context The driver context structure.
  101. * \param[in,out] persistent_data A pointer to the persistent data
  102. * that allows writing.
  103. * \param location The location value for which this driver
  104. * is registered. The driver will be invoked
  105. * for all keys whose lifetime is in this
  106. * location.
  107. *
  108. * \retval #PSA_SUCCESS
  109. * The driver is operational.
  110. * The core will update the persistent data in storage.
  111. * \return
  112. * Any other return value prevents the driver from being used in
  113. * this session.
  114. * The core will NOT update the persistent data in storage.
  115. */
  116. typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
  117. void *persistent_data,
  118. psa_key_location_t location);
  119. #if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
  120. /* Mbed TLS with secure element support enabled defines this type in
  121. * crypto_types.h because it is also visible to applications through an
  122. * implementation-specific extension.
  123. * For the PSA Cryptography specification, this type is only visible
  124. * via crypto_se_driver.h. */
  125. /** An internal designation of a key slot between the core part of the
  126. * PSA Crypto implementation and the driver. The meaning of this value
  127. * is driver-dependent. */
  128. typedef uint64_t psa_key_slot_number_t;
  129. #endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
  130. /**@}*/
  131. /** \defgroup se_mac Secure Element Message Authentication Codes
  132. * Generation and authentication of Message Authentication Codes (MACs) using
  133. * a secure element can be done either as a single function call (via the
  134. * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
  135. * parts using the following sequence:
  136. * - `psa_drv_se_mac_setup_t`
  137. * - `psa_drv_se_mac_update_t`
  138. * - `psa_drv_se_mac_update_t`
  139. * - ...
  140. * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
  141. *
  142. * If a previously started secure element MAC operation needs to be terminated,
  143. * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may
  144. * result in allocated resources not being freed or in other undefined
  145. * behavior.
  146. */
  147. /**@{*/
  148. /** \brief A function that starts a secure element MAC operation for a PSA
  149. * Crypto Driver implementation
  150. *
  151. * \param[in,out] drv_context The driver context structure.
  152. * \param[in,out] op_context A structure that will contain the
  153. * hardware-specific MAC context
  154. * \param[in] key_slot The slot of the key to be used for the
  155. * operation
  156. * \param[in] algorithm The algorithm to be used to underly the MAC
  157. * operation
  158. *
  159. * \retval #PSA_SUCCESS
  160. * Success.
  161. */
  162. typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
  163. void *op_context,
  164. psa_key_slot_number_t key_slot,
  165. psa_algorithm_t algorithm);
  166. /** \brief A function that continues a previously started secure element MAC
  167. * operation
  168. *
  169. * \param[in,out] op_context A hardware-specific structure for the
  170. * previously-established MAC operation to be
  171. * updated
  172. * \param[in] p_input A buffer containing the message to be appended
  173. * to the MAC operation
  174. * \param[in] input_length The size in bytes of the input message buffer
  175. */
  176. typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
  177. const uint8_t *p_input,
  178. size_t input_length);
  179. /** \brief a function that completes a previously started secure element MAC
  180. * operation by returning the resulting MAC.
  181. *
  182. * \param[in,out] op_context A hardware-specific structure for the
  183. * previously started MAC operation to be
  184. * finished
  185. * \param[out] p_mac A buffer where the generated MAC will be
  186. * placed
  187. * \param[in] mac_size The size in bytes of the buffer that has been
  188. * allocated for the `output` buffer
  189. * \param[out] p_mac_length After completion, will contain the number of
  190. * bytes placed in the `p_mac` buffer
  191. *
  192. * \retval #PSA_SUCCESS
  193. * Success.
  194. */
  195. typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
  196. uint8_t *p_mac,
  197. size_t mac_size,
  198. size_t *p_mac_length);
  199. /** \brief A function that completes a previously started secure element MAC
  200. * operation by comparing the resulting MAC against a provided value
  201. *
  202. * \param[in,out] op_context A hardware-specific structure for the previously
  203. * started MAC operation to be finished
  204. * \param[in] p_mac The MAC value against which the resulting MAC
  205. * will be compared against
  206. * \param[in] mac_length The size in bytes of the value stored in `p_mac`
  207. *
  208. * \retval #PSA_SUCCESS
  209. * The operation completed successfully and the MACs matched each
  210. * other
  211. * \retval #PSA_ERROR_INVALID_SIGNATURE
  212. * The operation completed successfully, but the calculated MAC did
  213. * not match the provided MAC
  214. */
  215. typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
  216. const uint8_t *p_mac,
  217. size_t mac_length);
  218. /** \brief A function that aborts a previous started secure element MAC
  219. * operation
  220. *
  221. * \param[in,out] op_context A hardware-specific structure for the previously
  222. * started MAC operation to be aborted
  223. */
  224. typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
  225. /** \brief A function that performs a secure element MAC operation in one
  226. * command and returns the calculated MAC
  227. *
  228. * \param[in,out] drv_context The driver context structure.
  229. * \param[in] p_input A buffer containing the message to be MACed
  230. * \param[in] input_length The size in bytes of `p_input`
  231. * \param[in] key_slot The slot of the key to be used
  232. * \param[in] alg The algorithm to be used to underlie the MAC
  233. * operation
  234. * \param[out] p_mac A buffer where the generated MAC will be
  235. * placed
  236. * \param[in] mac_size The size in bytes of the `p_mac` buffer
  237. * \param[out] p_mac_length After completion, will contain the number of
  238. * bytes placed in the `output` buffer
  239. *
  240. * \retval #PSA_SUCCESS
  241. * Success.
  242. */
  243. typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
  244. const uint8_t *p_input,
  245. size_t input_length,
  246. psa_key_slot_number_t key_slot,
  247. psa_algorithm_t alg,
  248. uint8_t *p_mac,
  249. size_t mac_size,
  250. size_t *p_mac_length);
  251. /** \brief A function that performs a secure element MAC operation in one
  252. * command and compares the resulting MAC against a provided value
  253. *
  254. * \param[in,out] drv_context The driver context structure.
  255. * \param[in] p_input A buffer containing the message to be MACed
  256. * \param[in] input_length The size in bytes of `input`
  257. * \param[in] key_slot The slot of the key to be used
  258. * \param[in] alg The algorithm to be used to underlie the MAC
  259. * operation
  260. * \param[in] p_mac The MAC value against which the resulting MAC will
  261. * be compared against
  262. * \param[in] mac_length The size in bytes of `mac`
  263. *
  264. * \retval #PSA_SUCCESS
  265. * The operation completed successfully and the MACs matched each
  266. * other
  267. * \retval #PSA_ERROR_INVALID_SIGNATURE
  268. * The operation completed successfully, but the calculated MAC did
  269. * not match the provided MAC
  270. */
  271. typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
  272. const uint8_t *p_input,
  273. size_t input_length,
  274. psa_key_slot_number_t key_slot,
  275. psa_algorithm_t alg,
  276. const uint8_t *p_mac,
  277. size_t mac_length);
  278. /** \brief A struct containing all of the function pointers needed to
  279. * perform secure element MAC operations
  280. *
  281. * PSA Crypto API implementations should populate the table as appropriate
  282. * upon startup.
  283. *
  284. * If one of the functions is not implemented (such as
  285. * `psa_drv_se_mac_generate_t`), it should be set to NULL.
  286. *
  287. * Driver implementers should ensure that they implement all of the functions
  288. * that make sense for their hardware, and that they provide a full solution
  289. * (for example, if they support `p_setup`, they should also support
  290. * `p_update` and at least one of `p_finish` or `p_finish_verify`).
  291. *
  292. */
  293. typedef struct {
  294. /**The size in bytes of the hardware-specific secure element MAC context
  295. * structure
  296. */
  297. size_t MBEDTLS_PRIVATE(context_size);
  298. /** Function that performs a MAC setup operation
  299. */
  300. psa_drv_se_mac_setup_t MBEDTLS_PRIVATE(p_setup);
  301. /** Function that performs a MAC update operation
  302. */
  303. psa_drv_se_mac_update_t MBEDTLS_PRIVATE(p_update);
  304. /** Function that completes a MAC operation
  305. */
  306. psa_drv_se_mac_finish_t MBEDTLS_PRIVATE(p_finish);
  307. /** Function that completes a MAC operation with a verify check
  308. */
  309. psa_drv_se_mac_finish_verify_t MBEDTLS_PRIVATE(p_finish_verify);
  310. /** Function that aborts a previously started MAC operation
  311. */
  312. psa_drv_se_mac_abort_t MBEDTLS_PRIVATE(p_abort);
  313. /** Function that performs a MAC operation in one call
  314. */
  315. psa_drv_se_mac_generate_t MBEDTLS_PRIVATE(p_mac);
  316. /** Function that performs a MAC and verify operation in one call
  317. */
  318. psa_drv_se_mac_verify_t MBEDTLS_PRIVATE(p_mac_verify);
  319. } psa_drv_se_mac_t;
  320. /**@}*/
  321. /** \defgroup se_cipher Secure Element Symmetric Ciphers
  322. *
  323. * Encryption and Decryption using secure element keys in block modes other
  324. * than ECB must be done in multiple parts, using the following flow:
  325. * - `psa_drv_se_cipher_setup_t`
  326. * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
  327. * - `psa_drv_se_cipher_update_t`
  328. * - `psa_drv_se_cipher_update_t`
  329. * - ...
  330. * - `psa_drv_se_cipher_finish_t`
  331. *
  332. * If a previously started secure element Cipher operation needs to be
  333. * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
  334. * to do so may result in allocated resources not being freed or in other
  335. * undefined behavior.
  336. *
  337. * In situations where a PSA Cryptographic API implementation is using a block
  338. * mode not-supported by the underlying hardware or driver, it can construct
  339. * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
  340. * for the cipher operations.
  341. */
  342. /**@{*/
  343. /** \brief A function that provides the cipher setup function for a
  344. * secure element driver
  345. *
  346. * \param[in,out] drv_context The driver context structure.
  347. * \param[in,out] op_context A structure that will contain the
  348. * hardware-specific cipher context.
  349. * \param[in] key_slot The slot of the key to be used for the
  350. * operation
  351. * \param[in] algorithm The algorithm to be used in the cipher
  352. * operation
  353. * \param[in] direction Indicates whether the operation is an encrypt
  354. * or decrypt
  355. *
  356. * \retval #PSA_SUCCESS \emptydescription
  357. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  358. */
  359. typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
  360. void *op_context,
  361. psa_key_slot_number_t key_slot,
  362. psa_algorithm_t algorithm,
  363. psa_encrypt_or_decrypt_t direction);
  364. /** \brief A function that sets the initialization vector (if
  365. * necessary) for a secure element cipher operation
  366. *
  367. * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
  368. * two IV functions: one to set the IV, and one to generate it internally. The
  369. * generate function is not necessary for the drivers to implement as the PSA
  370. * Crypto implementation can do the generation using its RNG features.
  371. *
  372. * \param[in,out] op_context A structure that contains the previously set up
  373. * hardware-specific cipher context
  374. * \param[in] p_iv A buffer containing the initialization vector
  375. * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
  376. *
  377. * \retval #PSA_SUCCESS \emptydescription
  378. */
  379. typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
  380. const uint8_t *p_iv,
  381. size_t iv_length);
  382. /** \brief A function that continues a previously started secure element cipher
  383. * operation
  384. *
  385. * \param[in,out] op_context A hardware-specific structure for the
  386. * previously started cipher operation
  387. * \param[in] p_input A buffer containing the data to be
  388. * encrypted/decrypted
  389. * \param[in] input_size The size in bytes of the buffer pointed to
  390. * by `p_input`
  391. * \param[out] p_output The caller-allocated buffer where the
  392. * output will be placed
  393. * \param[in] output_size The allocated size in bytes of the
  394. * `p_output` buffer
  395. * \param[out] p_output_length After completion, will contain the number
  396. * of bytes placed in the `p_output` buffer
  397. *
  398. * \retval #PSA_SUCCESS \emptydescription
  399. */
  400. typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
  401. const uint8_t *p_input,
  402. size_t input_size,
  403. uint8_t *p_output,
  404. size_t output_size,
  405. size_t *p_output_length);
  406. /** \brief A function that completes a previously started secure element cipher
  407. * operation
  408. *
  409. * \param[in,out] op_context A hardware-specific structure for the
  410. * previously started cipher operation
  411. * \param[out] p_output The caller-allocated buffer where the output
  412. * will be placed
  413. * \param[in] output_size The allocated size in bytes of the `p_output`
  414. * buffer
  415. * \param[out] p_output_length After completion, will contain the number of
  416. * bytes placed in the `p_output` buffer
  417. *
  418. * \retval #PSA_SUCCESS \emptydescription
  419. */
  420. typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
  421. uint8_t *p_output,
  422. size_t output_size,
  423. size_t *p_output_length);
  424. /** \brief A function that aborts a previously started secure element cipher
  425. * operation
  426. *
  427. * \param[in,out] op_context A hardware-specific structure for the
  428. * previously started cipher operation
  429. */
  430. typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
  431. /** \brief A function that performs the ECB block mode for secure element
  432. * cipher operations
  433. *
  434. * Note: this function should only be used with implementations that do not
  435. * provide a needed higher-level operation.
  436. *
  437. * \param[in,out] drv_context The driver context structure.
  438. * \param[in] key_slot The slot of the key to be used for the operation
  439. * \param[in] algorithm The algorithm to be used in the cipher operation
  440. * \param[in] direction Indicates whether the operation is an encrypt or
  441. * decrypt
  442. * \param[in] p_input A buffer containing the data to be
  443. * encrypted/decrypted
  444. * \param[in] input_size The size in bytes of the buffer pointed to by
  445. * `p_input`
  446. * \param[out] p_output The caller-allocated buffer where the output
  447. * will be placed
  448. * \param[in] output_size The allocated size in bytes of the `p_output`
  449. * buffer
  450. *
  451. * \retval #PSA_SUCCESS \emptydescription
  452. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  453. */
  454. typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
  455. psa_key_slot_number_t key_slot,
  456. psa_algorithm_t algorithm,
  457. psa_encrypt_or_decrypt_t direction,
  458. const uint8_t *p_input,
  459. size_t input_size,
  460. uint8_t *p_output,
  461. size_t output_size);
  462. /**
  463. * \brief A struct containing all of the function pointers needed to implement
  464. * cipher operations using secure elements.
  465. *
  466. * PSA Crypto API implementations should populate instances of the table as
  467. * appropriate upon startup or at build time.
  468. *
  469. * If one of the functions is not implemented (such as
  470. * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
  471. */
  472. typedef struct {
  473. /** The size in bytes of the hardware-specific secure element cipher
  474. * context structure
  475. */
  476. size_t MBEDTLS_PRIVATE(context_size);
  477. /** Function that performs a cipher setup operation */
  478. psa_drv_se_cipher_setup_t MBEDTLS_PRIVATE(p_setup);
  479. /** Function that sets a cipher IV (if necessary) */
  480. psa_drv_se_cipher_set_iv_t MBEDTLS_PRIVATE(p_set_iv);
  481. /** Function that performs a cipher update operation */
  482. psa_drv_se_cipher_update_t MBEDTLS_PRIVATE(p_update);
  483. /** Function that completes a cipher operation */
  484. psa_drv_se_cipher_finish_t MBEDTLS_PRIVATE(p_finish);
  485. /** Function that aborts a cipher operation */
  486. psa_drv_se_cipher_abort_t MBEDTLS_PRIVATE(p_abort);
  487. /** Function that performs ECB mode for a cipher operation
  488. * (Danger: ECB mode should not be used directly by clients of the PSA
  489. * Crypto Client API)
  490. */
  491. psa_drv_se_cipher_ecb_t MBEDTLS_PRIVATE(p_ecb);
  492. } psa_drv_se_cipher_t;
  493. /**@}*/
  494. /** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
  495. *
  496. * Since the amount of data that can (or should) be encrypted or signed using
  497. * asymmetric keys is limited by the key size, asymmetric key operations using
  498. * keys in a secure element must be done in single function calls.
  499. */
  500. /**@{*/
  501. /**
  502. * \brief A function that signs a hash or short message with a private key in
  503. * a secure element
  504. *
  505. * \param[in,out] drv_context The driver context structure.
  506. * \param[in] key_slot Key slot of an asymmetric key pair
  507. * \param[in] alg A signature algorithm that is compatible
  508. * with the type of `key`
  509. * \param[in] p_hash The hash to sign
  510. * \param[in] hash_length Size of the `p_hash` buffer in bytes
  511. * \param[out] p_signature Buffer where the signature is to be written
  512. * \param[in] signature_size Size of the `p_signature` buffer in bytes
  513. * \param[out] p_signature_length On success, the number of bytes
  514. * that make up the returned signature value
  515. *
  516. * \retval #PSA_SUCCESS \emptydescription
  517. */
  518. typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
  519. psa_key_slot_number_t key_slot,
  520. psa_algorithm_t alg,
  521. const uint8_t *p_hash,
  522. size_t hash_length,
  523. uint8_t *p_signature,
  524. size_t signature_size,
  525. size_t *p_signature_length);
  526. /**
  527. * \brief A function that verifies the signature a hash or short message using
  528. * an asymmetric public key in a secure element
  529. *
  530. * \param[in,out] drv_context The driver context structure.
  531. * \param[in] key_slot Key slot of a public key or an asymmetric key
  532. * pair
  533. * \param[in] alg A signature algorithm that is compatible with
  534. * the type of `key`
  535. * \param[in] p_hash The hash whose signature is to be verified
  536. * \param[in] hash_length Size of the `p_hash` buffer in bytes
  537. * \param[in] p_signature Buffer containing the signature to verify
  538. * \param[in] signature_length Size of the `p_signature` buffer in bytes
  539. *
  540. * \retval #PSA_SUCCESS
  541. * The signature is valid.
  542. */
  543. typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
  544. psa_key_slot_number_t key_slot,
  545. psa_algorithm_t alg,
  546. const uint8_t *p_hash,
  547. size_t hash_length,
  548. const uint8_t *p_signature,
  549. size_t signature_length);
  550. /**
  551. * \brief A function that encrypts a short message with an asymmetric public
  552. * key in a secure element
  553. *
  554. * \param[in,out] drv_context The driver context structure.
  555. * \param[in] key_slot Key slot of a public key or an asymmetric key
  556. * pair
  557. * \param[in] alg An asymmetric encryption algorithm that is
  558. * compatible with the type of `key`
  559. * \param[in] p_input The message to encrypt
  560. * \param[in] input_length Size of the `p_input` buffer in bytes
  561. * \param[in] p_salt A salt or label, if supported by the
  562. * encryption algorithm
  563. * If the algorithm does not support a
  564. * salt, pass `NULL`.
  565. * If the algorithm supports an optional
  566. * salt and you do not want to pass a salt,
  567. * pass `NULL`.
  568. * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  569. * supported.
  570. * \param[in] salt_length Size of the `p_salt` buffer in bytes
  571. * If `p_salt` is `NULL`, pass 0.
  572. * \param[out] p_output Buffer where the encrypted message is to
  573. * be written
  574. * \param[in] output_size Size of the `p_output` buffer in bytes
  575. * \param[out] p_output_length On success, the number of bytes that make up
  576. * the returned output
  577. *
  578. * \retval #PSA_SUCCESS \emptydescription
  579. */
  580. typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
  581. psa_key_slot_number_t key_slot,
  582. psa_algorithm_t alg,
  583. const uint8_t *p_input,
  584. size_t input_length,
  585. const uint8_t *p_salt,
  586. size_t salt_length,
  587. uint8_t *p_output,
  588. size_t output_size,
  589. size_t *p_output_length);
  590. /**
  591. * \brief A function that decrypts a short message with an asymmetric private
  592. * key in a secure element.
  593. *
  594. * \param[in,out] drv_context The driver context structure.
  595. * \param[in] key_slot Key slot of an asymmetric key pair
  596. * \param[in] alg An asymmetric encryption algorithm that is
  597. * compatible with the type of `key`
  598. * \param[in] p_input The message to decrypt
  599. * \param[in] input_length Size of the `p_input` buffer in bytes
  600. * \param[in] p_salt A salt or label, if supported by the
  601. * encryption algorithm
  602. * If the algorithm does not support a
  603. * salt, pass `NULL`.
  604. * If the algorithm supports an optional
  605. * salt and you do not want to pass a salt,
  606. * pass `NULL`.
  607. * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  608. * supported.
  609. * \param[in] salt_length Size of the `p_salt` buffer in bytes
  610. * If `p_salt` is `NULL`, pass 0.
  611. * \param[out] p_output Buffer where the decrypted message is to
  612. * be written
  613. * \param[in] output_size Size of the `p_output` buffer in bytes
  614. * \param[out] p_output_length On success, the number of bytes
  615. * that make up the returned output
  616. *
  617. * \retval #PSA_SUCCESS \emptydescription
  618. */
  619. typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
  620. psa_key_slot_number_t key_slot,
  621. psa_algorithm_t alg,
  622. const uint8_t *p_input,
  623. size_t input_length,
  624. const uint8_t *p_salt,
  625. size_t salt_length,
  626. uint8_t *p_output,
  627. size_t output_size,
  628. size_t *p_output_length);
  629. /**
  630. * \brief A struct containing all of the function pointers needed to implement
  631. * asymmetric cryptographic operations using secure elements.
  632. *
  633. * PSA Crypto API implementations should populate instances of the table as
  634. * appropriate upon startup or at build time.
  635. *
  636. * If one of the functions is not implemented, it should be set to NULL.
  637. */
  638. typedef struct {
  639. /** Function that performs an asymmetric sign operation */
  640. psa_drv_se_asymmetric_sign_t MBEDTLS_PRIVATE(p_sign);
  641. /** Function that performs an asymmetric verify operation */
  642. psa_drv_se_asymmetric_verify_t MBEDTLS_PRIVATE(p_verify);
  643. /** Function that performs an asymmetric encrypt operation */
  644. psa_drv_se_asymmetric_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
  645. /** Function that performs an asymmetric decrypt operation */
  646. psa_drv_se_asymmetric_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
  647. } psa_drv_se_asymmetric_t;
  648. /**@}*/
  649. /** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
  650. * Authenticated Encryption with Additional Data (AEAD) operations with secure
  651. * elements must be done in one function call. While this creates a burden for
  652. * implementers as there must be sufficient space in memory for the entire
  653. * message, it prevents decrypted data from being made available before the
  654. * authentication operation is complete and the data is known to be authentic.
  655. */
  656. /**@{*/
  657. /** \brief A function that performs a secure element authenticated encryption
  658. * operation
  659. *
  660. * \param[in,out] drv_context The driver context structure.
  661. * \param[in] key_slot Slot containing the key to use.
  662. * \param[in] algorithm The AEAD algorithm to compute
  663. * (\c PSA_ALG_XXX value such that
  664. * #PSA_ALG_IS_AEAD(`alg`) is true)
  665. * \param[in] p_nonce Nonce or IV to use
  666. * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
  667. * \param[in] p_additional_data Additional data that will be
  668. * authenticated but not encrypted
  669. * \param[in] additional_data_length Size of `p_additional_data` in bytes
  670. * \param[in] p_plaintext Data that will be authenticated and
  671. * encrypted
  672. * \param[in] plaintext_length Size of `p_plaintext` in bytes
  673. * \param[out] p_ciphertext Output buffer for the authenticated and
  674. * encrypted data. The additional data is
  675. * not part of this output. For algorithms
  676. * where the encrypted data and the
  677. * authentication tag are defined as
  678. * separate outputs, the authentication
  679. * tag is appended to the encrypted data.
  680. * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
  681. * bytes
  682. * \param[out] p_ciphertext_length On success, the size of the output in
  683. * the `p_ciphertext` buffer
  684. *
  685. * \retval #PSA_SUCCESS
  686. * Success.
  687. */
  688. typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
  689. psa_key_slot_number_t key_slot,
  690. psa_algorithm_t algorithm,
  691. const uint8_t *p_nonce,
  692. size_t nonce_length,
  693. const uint8_t *p_additional_data,
  694. size_t additional_data_length,
  695. const uint8_t *p_plaintext,
  696. size_t plaintext_length,
  697. uint8_t *p_ciphertext,
  698. size_t ciphertext_size,
  699. size_t *p_ciphertext_length);
  700. /** A function that performs a secure element authenticated decryption operation
  701. *
  702. * \param[in,out] drv_context The driver context structure.
  703. * \param[in] key_slot Slot containing the key to use
  704. * \param[in] algorithm The AEAD algorithm to compute
  705. * (\c PSA_ALG_XXX value such that
  706. * #PSA_ALG_IS_AEAD(`alg`) is true)
  707. * \param[in] p_nonce Nonce or IV to use
  708. * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
  709. * \param[in] p_additional_data Additional data that has been
  710. * authenticated but not encrypted
  711. * \param[in] additional_data_length Size of `p_additional_data` in bytes
  712. * \param[in] p_ciphertext Data that has been authenticated and
  713. * encrypted.
  714. * For algorithms where the encrypted data
  715. * and the authentication tag are defined
  716. * as separate inputs, the buffer must
  717. * contain the encrypted data followed by
  718. * the authentication tag.
  719. * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
  720. * \param[out] p_plaintext Output buffer for the decrypted data
  721. * \param[in] plaintext_size Size of the `p_plaintext` buffer in
  722. * bytes
  723. * \param[out] p_plaintext_length On success, the size of the output in
  724. * the `p_plaintext` buffer
  725. *
  726. * \retval #PSA_SUCCESS
  727. * Success.
  728. */
  729. typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
  730. psa_key_slot_number_t key_slot,
  731. psa_algorithm_t algorithm,
  732. const uint8_t *p_nonce,
  733. size_t nonce_length,
  734. const uint8_t *p_additional_data,
  735. size_t additional_data_length,
  736. const uint8_t *p_ciphertext,
  737. size_t ciphertext_length,
  738. uint8_t *p_plaintext,
  739. size_t plaintext_size,
  740. size_t *p_plaintext_length);
  741. /**
  742. * \brief A struct containing all of the function pointers needed to implement
  743. * secure element Authenticated Encryption with Additional Data operations
  744. *
  745. * PSA Crypto API implementations should populate instances of the table as
  746. * appropriate upon startup.
  747. *
  748. * If one of the functions is not implemented, it should be set to NULL.
  749. */
  750. typedef struct {
  751. /** Function that performs the AEAD encrypt operation */
  752. psa_drv_se_aead_encrypt_t MBEDTLS_PRIVATE(p_encrypt);
  753. /** Function that performs the AEAD decrypt operation */
  754. psa_drv_se_aead_decrypt_t MBEDTLS_PRIVATE(p_decrypt);
  755. } psa_drv_se_aead_t;
  756. /**@}*/
  757. /** \defgroup se_key_management Secure Element Key Management
  758. * Currently, key management is limited to importing keys in the clear,
  759. * destroying keys, and exporting keys in the clear.
  760. * Whether a key may be exported is determined by the key policies in place
  761. * on the key slot.
  762. */
  763. /**@{*/
  764. /** An enumeration indicating how a key is created.
  765. */
  766. typedef enum {
  767. PSA_KEY_CREATION_IMPORT, /**< During psa_import_key() */
  768. PSA_KEY_CREATION_GENERATE, /**< During psa_generate_key() */
  769. PSA_KEY_CREATION_DERIVE, /**< During psa_key_derivation_output_key() */
  770. PSA_KEY_CREATION_COPY, /**< During psa_copy_key() */
  771. #ifndef __DOXYGEN_ONLY__
  772. /** A key is being registered with mbedtls_psa_register_se_key().
  773. *
  774. * The core only passes this value to
  775. * psa_drv_se_key_management_t::p_validate_slot_number, not to
  776. * psa_drv_se_key_management_t::p_allocate. The call to
  777. * `p_validate_slot_number` is not followed by any other call to the
  778. * driver: the key is considered successfully registered if the call to
  779. * `p_validate_slot_number` succeeds, or if `p_validate_slot_number` is
  780. * null.
  781. *
  782. * With this creation method, the driver must return #PSA_SUCCESS if
  783. * the given attributes are compatible with the existing key in the slot,
  784. * and #PSA_ERROR_DOES_NOT_EXIST if the driver can determine that there
  785. * is no key with the specified slot number.
  786. *
  787. * This is an Mbed TLS extension.
  788. */
  789. PSA_KEY_CREATION_REGISTER,
  790. #endif
  791. } psa_key_creation_method_t;
  792. /** \brief A function that allocates a slot for a key.
  793. *
  794. * To create a key in a specific slot in a secure element, the core
  795. * first calls this function to determine a valid slot number,
  796. * then calls a function to create the key material in that slot.
  797. * In nominal conditions (that is, if no error occurs),
  798. * the effect of a call to a key creation function in the PSA Cryptography
  799. * API with a lifetime that places the key in a secure element is the
  800. * following:
  801. * -# The core calls psa_drv_se_key_management_t::p_allocate
  802. * (or in some implementations
  803. * psa_drv_se_key_management_t::p_validate_slot_number). The driver
  804. * selects (or validates) a suitable slot number given the key attributes
  805. * and the state of the secure element.
  806. * -# The core calls a key creation function in the driver.
  807. *
  808. * The key creation functions in the PSA Cryptography API are:
  809. * - psa_import_key(), which causes
  810. * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_IMPORT
  811. * then a call to psa_drv_se_key_management_t::p_import.
  812. * - psa_generate_key(), which causes
  813. * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_GENERATE
  814. * then a call to psa_drv_se_key_management_t::p_import.
  815. * - psa_key_derivation_output_key(), which causes
  816. * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_DERIVE
  817. * then a call to psa_drv_se_key_derivation_t::p_derive.
  818. * - psa_copy_key(), which causes
  819. * a call to `p_allocate` with \p method = #PSA_KEY_CREATION_COPY
  820. * then a call to psa_drv_se_key_management_t::p_export.
  821. *
  822. * In case of errors, other behaviors are possible.
  823. * - If the PSA Cryptography subsystem dies after the first step,
  824. * for example because the device has lost power abruptly,
  825. * the second step may never happen, or may happen after a reset
  826. * and re-initialization. Alternatively, after a reset and
  827. * re-initialization, the core may call
  828. * psa_drv_se_key_management_t::p_destroy on the slot number that
  829. * was allocated (or validated) instead of calling a key creation function.
  830. * - If an error occurs, the core may call
  831. * psa_drv_se_key_management_t::p_destroy on the slot number that
  832. * was allocated (or validated) instead of calling a key creation function.
  833. *
  834. * Errors and system resets also have an impact on the driver's persistent
  835. * data. If a reset happens before the overall key creation process is
  836. * completed (before or after the second step above), it is unspecified
  837. * whether the persistent data after the reset is identical to what it
  838. * was before or after the call to `p_allocate` (or `p_validate_slot_number`).
  839. *
  840. * \param[in,out] drv_context The driver context structure.
  841. * \param[in,out] persistent_data A pointer to the persistent data
  842. * that allows writing.
  843. * \param[in] attributes Attributes of the key.
  844. * \param method The way in which the key is being created.
  845. * \param[out] key_slot Slot where the key will be stored.
  846. * This must be a valid slot for a key of the
  847. * chosen type. It must be unoccupied.
  848. *
  849. * \retval #PSA_SUCCESS
  850. * Success.
  851. * The core will record \c *key_slot as the key slot where the key
  852. * is stored and will update the persistent data in storage.
  853. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  854. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  855. */
  856. typedef psa_status_t (*psa_drv_se_allocate_key_t)(
  857. psa_drv_se_context_t *drv_context,
  858. void *persistent_data,
  859. const psa_key_attributes_t *attributes,
  860. psa_key_creation_method_t method,
  861. psa_key_slot_number_t *key_slot);
  862. /** \brief A function that determines whether a slot number is valid
  863. * for a key.
  864. *
  865. * To create a key in a specific slot in a secure element, the core
  866. * first calls this function to validate the choice of slot number,
  867. * then calls a function to create the key material in that slot.
  868. * See the documentation of #psa_drv_se_allocate_key_t for more details.
  869. *
  870. * As of the PSA Cryptography API specification version 1.0, there is no way
  871. * for applications to trigger a call to this function. However some
  872. * implementations offer the capability to create or declare a key in
  873. * a specific slot via implementation-specific means, generally for the
  874. * sake of initial device provisioning or onboarding. Such a mechanism may
  875. * be added to a future version of the PSA Cryptography API specification.
  876. *
  877. * This function may update the driver's persistent data through
  878. * \p persistent_data. The core will save the updated persistent data at the
  879. * end of the key creation process. See the description of
  880. * ::psa_drv_se_allocate_key_t for more information.
  881. *
  882. * \param[in,out] drv_context The driver context structure.
  883. * \param[in,out] persistent_data A pointer to the persistent data
  884. * that allows writing.
  885. * \param[in] attributes Attributes of the key.
  886. * \param method The way in which the key is being created.
  887. * \param[in] key_slot Slot where the key is to be stored.
  888. *
  889. * \retval #PSA_SUCCESS
  890. * The given slot number is valid for a key with the given
  891. * attributes.
  892. * \retval #PSA_ERROR_INVALID_ARGUMENT
  893. * The given slot number is not valid for a key with the
  894. * given attributes. This includes the case where the slot
  895. * number is not valid at all.
  896. * \retval #PSA_ERROR_ALREADY_EXISTS
  897. * There is already a key with the specified slot number.
  898. * Drivers may choose to return this error from the key
  899. * creation function instead.
  900. */
  901. typedef psa_status_t (*psa_drv_se_validate_slot_number_t)(
  902. psa_drv_se_context_t *drv_context,
  903. void *persistent_data,
  904. const psa_key_attributes_t *attributes,
  905. psa_key_creation_method_t method,
  906. psa_key_slot_number_t key_slot);
  907. /** \brief A function that imports a key into a secure element in binary format
  908. *
  909. * This function can support any output from psa_export_key(). Refer to the
  910. * documentation of psa_export_key() for the format for each key type.
  911. *
  912. * \param[in,out] drv_context The driver context structure.
  913. * \param key_slot Slot where the key will be stored.
  914. * This must be a valid slot for a key of the
  915. * chosen type. It must be unoccupied.
  916. * \param[in] attributes The key attributes, including the lifetime,
  917. * the key type and the usage policy.
  918. * Drivers should not access the key size stored
  919. * in the attributes: it may not match the
  920. * data passed in \p data.
  921. * Drivers can call psa_get_key_lifetime(),
  922. * psa_get_key_type(),
  923. * psa_get_key_usage_flags() and
  924. * psa_get_key_algorithm() to access this
  925. * information.
  926. * \param[in] data Buffer containing the key data.
  927. * \param[in] data_length Size of the \p data buffer in bytes.
  928. * \param[out] bits On success, the key size in bits. The driver
  929. * must determine this value after parsing the
  930. * key according to the key type.
  931. * This value is not used if the function fails.
  932. *
  933. * \retval #PSA_SUCCESS
  934. * Success.
  935. */
  936. typedef psa_status_t (*psa_drv_se_import_key_t)(
  937. psa_drv_se_context_t *drv_context,
  938. psa_key_slot_number_t key_slot,
  939. const psa_key_attributes_t *attributes,
  940. const uint8_t *data,
  941. size_t data_length,
  942. size_t *bits);
  943. /**
  944. * \brief A function that destroys a secure element key and restore the slot to
  945. * its default state
  946. *
  947. * This function destroys the content of the key from a secure element.
  948. * Implementations shall make a best effort to ensure that any previous content
  949. * of the slot is unrecoverable.
  950. *
  951. * This function returns the specified slot to its default state.
  952. *
  953. * \param[in,out] drv_context The driver context structure.
  954. * \param[in,out] persistent_data A pointer to the persistent data
  955. * that allows writing.
  956. * \param key_slot The key slot to erase.
  957. *
  958. * \retval #PSA_SUCCESS
  959. * The slot's content, if any, has been erased.
  960. */
  961. typedef psa_status_t (*psa_drv_se_destroy_key_t)(
  962. psa_drv_se_context_t *drv_context,
  963. void *persistent_data,
  964. psa_key_slot_number_t key_slot);
  965. /**
  966. * \brief A function that exports a secure element key in binary format
  967. *
  968. * The output of this function can be passed to psa_import_key() to
  969. * create an equivalent object.
  970. *
  971. * If a key is created with `psa_import_key()` and then exported with
  972. * this function, it is not guaranteed that the resulting data is
  973. * identical: the implementation may choose a different representation
  974. * of the same key if the format permits it.
  975. *
  976. * This function should generate output in the same format that
  977. * `psa_export_key()` does. Refer to the
  978. * documentation of `psa_export_key()` for the format for each key type.
  979. *
  980. * \param[in,out] drv_context The driver context structure.
  981. * \param[in] key Slot whose content is to be exported. This must
  982. * be an occupied key slot.
  983. * \param[out] p_data Buffer where the key data is to be written.
  984. * \param[in] data_size Size of the `p_data` buffer in bytes.
  985. * \param[out] p_data_length On success, the number of bytes
  986. * that make up the key data.
  987. *
  988. * \retval #PSA_SUCCESS \emptydescription
  989. * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
  990. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  991. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  992. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  993. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  994. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  995. */
  996. typedef psa_status_t (*psa_drv_se_export_key_t)(psa_drv_se_context_t *drv_context,
  997. psa_key_slot_number_t key,
  998. uint8_t *p_data,
  999. size_t data_size,
  1000. size_t *p_data_length);
  1001. /**
  1002. * \brief A function that generates a symmetric or asymmetric key on a secure
  1003. * element
  1004. *
  1005. * If the key type \c type recorded in \p attributes
  1006. * is asymmetric (#PSA_KEY_TYPE_IS_ASYMMETRIC(\c type) = 1),
  1007. * the driver may export the public key at the time of generation,
  1008. * in the format documented for psa_export_public_key() by writing it
  1009. * to the \p pubkey buffer.
  1010. * This is optional, intended for secure elements that output the
  1011. * public key at generation time and that cannot export the public key
  1012. * later. Drivers that do not need this feature should leave
  1013. * \p *pubkey_length set to 0 and should
  1014. * implement the psa_drv_key_management_t::p_export_public function.
  1015. * Some implementations do not support this feature, in which case
  1016. * \p pubkey is \c NULL and \p pubkey_size is 0.
  1017. *
  1018. * \param[in,out] drv_context The driver context structure.
  1019. * \param key_slot Slot where the key will be stored.
  1020. * This must be a valid slot for a key of the
  1021. * chosen type. It must be unoccupied.
  1022. * \param[in] attributes The key attributes, including the lifetime,
  1023. * the key type and size, and the usage policy.
  1024. * Drivers can call psa_get_key_lifetime(),
  1025. * psa_get_key_type(), psa_get_key_bits(),
  1026. * psa_get_key_usage_flags() and
  1027. * psa_get_key_algorithm() to access this
  1028. * information.
  1029. * \param[out] pubkey A buffer where the driver can write the
  1030. * public key, when generating an asymmetric
  1031. * key pair.
  1032. * This is \c NULL when generating a symmetric
  1033. * key or if the core does not support
  1034. * exporting the public key at generation time.
  1035. * \param pubkey_size The size of the `pubkey` buffer in bytes.
  1036. * This is 0 when generating a symmetric
  1037. * key or if the core does not support
  1038. * exporting the public key at generation time.
  1039. * \param[out] pubkey_length On entry, this is always 0.
  1040. * On success, the number of bytes written to
  1041. * \p pubkey. If this is 0 or unchanged on return,
  1042. * the core will not read the \p pubkey buffer,
  1043. * and will instead call the driver's
  1044. * psa_drv_key_management_t::p_export_public
  1045. * function to export the public key when needed.
  1046. */
  1047. typedef psa_status_t (*psa_drv_se_generate_key_t)(
  1048. psa_drv_se_context_t *drv_context,
  1049. psa_key_slot_number_t key_slot,
  1050. const psa_key_attributes_t *attributes,
  1051. uint8_t *pubkey, size_t pubkey_size, size_t *pubkey_length);
  1052. /**
  1053. * \brief A struct containing all of the function pointers needed to for secure
  1054. * element key management
  1055. *
  1056. * PSA Crypto API implementations should populate instances of the table as
  1057. * appropriate upon startup or at build time.
  1058. *
  1059. * If one of the functions is not implemented, it should be set to NULL.
  1060. */
  1061. typedef struct {
  1062. /** Function that allocates a slot for a key. */
  1063. psa_drv_se_allocate_key_t MBEDTLS_PRIVATE(p_allocate);
  1064. /** Function that checks the validity of a slot for a key. */
  1065. psa_drv_se_validate_slot_number_t MBEDTLS_PRIVATE(p_validate_slot_number);
  1066. /** Function that performs a key import operation */
  1067. psa_drv_se_import_key_t MBEDTLS_PRIVATE(p_import);
  1068. /** Function that performs a generation */
  1069. psa_drv_se_generate_key_t MBEDTLS_PRIVATE(p_generate);
  1070. /** Function that performs a key destroy operation */
  1071. psa_drv_se_destroy_key_t MBEDTLS_PRIVATE(p_destroy);
  1072. /** Function that performs a key export operation */
  1073. psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export);
  1074. /** Function that performs a public key export operation */
  1075. psa_drv_se_export_key_t MBEDTLS_PRIVATE(p_export_public);
  1076. } psa_drv_se_key_management_t;
  1077. /**@}*/
  1078. /** \defgroup driver_derivation Secure Element Key Derivation and Agreement
  1079. * Key derivation is the process of generating new key material using an
  1080. * existing key and additional parameters, iterating through a basic
  1081. * cryptographic function, such as a hash.
  1082. * Key agreement is a part of cryptographic protocols that allows two parties
  1083. * to agree on the same key value, but starting from different original key
  1084. * material.
  1085. * The flows are similar, and the PSA Crypto Driver Model uses the same functions
  1086. * for both of the flows.
  1087. *
  1088. * There are two different final functions for the flows,
  1089. * `psa_drv_se_key_derivation_derive` and `psa_drv_se_key_derivation_export`.
  1090. * `psa_drv_se_key_derivation_derive` is used when the key material should be
  1091. * placed in a slot on the hardware and not exposed to the caller.
  1092. * `psa_drv_se_key_derivation_export` is used when the key material should be
  1093. * returned to the PSA Cryptographic API implementation.
  1094. *
  1095. * Different key derivation algorithms require a different number of inputs.
  1096. * Instead of having an API that takes as input variable length arrays, which
  1097. * can be problematic to manage on embedded platforms, the inputs are passed
  1098. * to the driver via a function, `psa_drv_se_key_derivation_collateral`, that
  1099. * is called multiple times with different `collateral_id`s. Thus, for a key
  1100. * derivation algorithm that required 3 parameter inputs, the flow would look
  1101. * something like:
  1102. * ~~~~~~~~~~~~~{.c}
  1103. * psa_drv_se_key_derivation_setup(kdf_algorithm, source_key, dest_key_size_bytes);
  1104. * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_0,
  1105. * p_collateral_0,
  1106. * collateral_0_size);
  1107. * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_1,
  1108. * p_collateral_1,
  1109. * collateral_1_size);
  1110. * psa_drv_se_key_derivation_collateral(kdf_algorithm_collateral_id_2,
  1111. * p_collateral_2,
  1112. * collateral_2_size);
  1113. * psa_drv_se_key_derivation_derive();
  1114. * ~~~~~~~~~~~~~
  1115. *
  1116. * key agreement example:
  1117. * ~~~~~~~~~~~~~{.c}
  1118. * psa_drv_se_key_derivation_setup(alg, source_key. dest_key_size_bytes);
  1119. * psa_drv_se_key_derivation_collateral(DHE_PUBKEY, p_pubkey, pubkey_size);
  1120. * psa_drv_se_key_derivation_export(p_session_key,
  1121. * session_key_size,
  1122. * &session_key_length);
  1123. * ~~~~~~~~~~~~~
  1124. */
  1125. /**@{*/
  1126. /** \brief A function that Sets up a secure element key derivation operation by
  1127. * specifying the algorithm and the source key sot
  1128. *
  1129. * \param[in,out] drv_context The driver context structure.
  1130. * \param[in,out] op_context A hardware-specific structure containing any
  1131. * context information for the implementation
  1132. * \param[in] kdf_alg The algorithm to be used for the key derivation
  1133. * \param[in] source_key The key to be used as the source material for
  1134. * the key derivation
  1135. *
  1136. * \retval #PSA_SUCCESS \emptydescription
  1137. */
  1138. typedef psa_status_t (*psa_drv_se_key_derivation_setup_t)(psa_drv_se_context_t *drv_context,
  1139. void *op_context,
  1140. psa_algorithm_t kdf_alg,
  1141. psa_key_slot_number_t source_key);
  1142. /** \brief A function that provides collateral (parameters) needed for a secure
  1143. * element key derivation or key agreement operation
  1144. *
  1145. * Since many key derivation algorithms require multiple parameters, it is
  1146. * expected that this function may be called multiple times for the same
  1147. * operation, each with a different algorithm-specific `collateral_id`
  1148. *
  1149. * \param[in,out] op_context A hardware-specific structure containing any
  1150. * context information for the implementation
  1151. * \param[in] collateral_id An ID for the collateral being provided
  1152. * \param[in] p_collateral A buffer containing the collateral data
  1153. * \param[in] collateral_size The size in bytes of the collateral
  1154. *
  1155. * \retval #PSA_SUCCESS \emptydescription
  1156. */
  1157. typedef psa_status_t (*psa_drv_se_key_derivation_collateral_t)(void *op_context,
  1158. uint32_t collateral_id,
  1159. const uint8_t *p_collateral,
  1160. size_t collateral_size);
  1161. /** \brief A function that performs the final secure element key derivation
  1162. * step and place the generated key material in a slot
  1163. *
  1164. * \param[in,out] op_context A hardware-specific structure containing any
  1165. * context information for the implementation
  1166. * \param[in] dest_key The slot where the generated key material
  1167. * should be placed
  1168. *
  1169. * \retval #PSA_SUCCESS \emptydescription
  1170. */
  1171. typedef psa_status_t (*psa_drv_se_key_derivation_derive_t)(void *op_context,
  1172. psa_key_slot_number_t dest_key);
  1173. /** \brief A function that performs the final step of a secure element key
  1174. * agreement and place the generated key material in a buffer
  1175. *
  1176. * \param[out] p_output Buffer in which to place the generated key
  1177. * material
  1178. * \param[in] output_size The size in bytes of `p_output`
  1179. * \param[out] p_output_length Upon success, contains the number of bytes of
  1180. * key material placed in `p_output`
  1181. *
  1182. * \retval #PSA_SUCCESS \emptydescription
  1183. */
  1184. typedef psa_status_t (*psa_drv_se_key_derivation_export_t)(void *op_context,
  1185. uint8_t *p_output,
  1186. size_t output_size,
  1187. size_t *p_output_length);
  1188. /**
  1189. * \brief A struct containing all of the function pointers needed to for secure
  1190. * element key derivation and agreement
  1191. *
  1192. * PSA Crypto API implementations should populate instances of the table as
  1193. * appropriate upon startup.
  1194. *
  1195. * If one of the functions is not implemented, it should be set to NULL.
  1196. */
  1197. typedef struct {
  1198. /** The driver-specific size of the key derivation context */
  1199. size_t MBEDTLS_PRIVATE(context_size);
  1200. /** Function that performs a key derivation setup */
  1201. psa_drv_se_key_derivation_setup_t MBEDTLS_PRIVATE(p_setup);
  1202. /** Function that sets key derivation collateral */
  1203. psa_drv_se_key_derivation_collateral_t MBEDTLS_PRIVATE(p_collateral);
  1204. /** Function that performs a final key derivation step */
  1205. psa_drv_se_key_derivation_derive_t MBEDTLS_PRIVATE(p_derive);
  1206. /** Function that performs a final key derivation or agreement and
  1207. * exports the key */
  1208. psa_drv_se_key_derivation_export_t MBEDTLS_PRIVATE(p_export);
  1209. } psa_drv_se_key_derivation_t;
  1210. /**@}*/
  1211. /** \defgroup se_registration Secure element driver registration
  1212. */
  1213. /**@{*/
  1214. /** A structure containing pointers to all the entry points of a
  1215. * secure element driver.
  1216. *
  1217. * Future versions of this specification may add extra substructures at
  1218. * the end of this structure.
  1219. */
  1220. typedef struct {
  1221. /** The version of the driver HAL that this driver implements.
  1222. * This is a protection against loading driver binaries built against
  1223. * a different version of this specification.
  1224. * Use #PSA_DRV_SE_HAL_VERSION.
  1225. */
  1226. uint32_t MBEDTLS_PRIVATE(hal_version);
  1227. /** The size of the driver's persistent data in bytes.
  1228. *
  1229. * This can be 0 if the driver does not need persistent data.
  1230. *
  1231. * See the documentation of psa_drv_se_context_t::persistent_data
  1232. * for more information about why and how a driver can use
  1233. * persistent data.
  1234. */
  1235. size_t MBEDTLS_PRIVATE(persistent_data_size);
  1236. /** The driver initialization function.
  1237. *
  1238. * This function is called once during the initialization of the
  1239. * PSA Cryptography subsystem, before any other function of the
  1240. * driver is called. If this function returns a failure status,
  1241. * the driver will be unusable, at least until the next system reset.
  1242. *
  1243. * If this field is \c NULL, it is equivalent to a function that does
  1244. * nothing and returns #PSA_SUCCESS.
  1245. */
  1246. psa_drv_se_init_t MBEDTLS_PRIVATE(p_init);
  1247. const psa_drv_se_key_management_t *MBEDTLS_PRIVATE(key_management);
  1248. const psa_drv_se_mac_t *MBEDTLS_PRIVATE(mac);
  1249. const psa_drv_se_cipher_t *MBEDTLS_PRIVATE(cipher);
  1250. const psa_drv_se_aead_t *MBEDTLS_PRIVATE(aead);
  1251. const psa_drv_se_asymmetric_t *MBEDTLS_PRIVATE(asymmetric);
  1252. const psa_drv_se_key_derivation_t *MBEDTLS_PRIVATE(derivation);
  1253. } psa_drv_se_t;
  1254. /** The current version of the secure element driver HAL.
  1255. */
  1256. /* 0.0.0 patchlevel 5 */
  1257. #define PSA_DRV_SE_HAL_VERSION 0x00000005
  1258. /** Register an external cryptoprocessor (secure element) driver.
  1259. *
  1260. * This function is only intended to be used by driver code, not by
  1261. * application code. In implementations with separation between the
  1262. * PSA cryptography module and applications, this function should
  1263. * only be available to callers that run in the same memory space as
  1264. * the cryptography module, and should not be exposed to applications
  1265. * running in a different memory space.
  1266. *
  1267. * This function may be called before psa_crypto_init(). It is
  1268. * implementation-defined whether this function may be called
  1269. * after psa_crypto_init().
  1270. *
  1271. * \note Implementations store metadata about keys including the lifetime
  1272. * value, which contains the driver's location indicator. Therefore,
  1273. * from one instantiation of the PSA Cryptography
  1274. * library to the next one, if there is a key in storage with a certain
  1275. * lifetime value, you must always register the same driver (or an
  1276. * updated version that communicates with the same secure element)
  1277. * with the same location value.
  1278. *
  1279. * \param location The location value through which this driver will
  1280. * be exposed to applications.
  1281. * This driver will be used for all keys such that
  1282. * `location == #PSA_KEY_LIFETIME_GET_LOCATION( lifetime )`.
  1283. * The value #PSA_KEY_LOCATION_LOCAL_STORAGE is reserved
  1284. * and may not be used for drivers. Implementations
  1285. * may reserve other values.
  1286. * \param[in] methods The method table of the driver. This structure must
  1287. * remain valid for as long as the cryptography
  1288. * module keeps running. It is typically a global
  1289. * constant.
  1290. *
  1291. * \return #PSA_SUCCESS
  1292. * The driver was successfully registered. Applications can now
  1293. * use \p location to access keys through the methods passed to
  1294. * this function.
  1295. * \return #PSA_ERROR_BAD_STATE
  1296. * This function was called after the initialization of the
  1297. * cryptography module, and this implementation does not support
  1298. * driver registration at this stage.
  1299. * \return #PSA_ERROR_ALREADY_EXISTS
  1300. * There is already a registered driver for this value of \p location.
  1301. * \return #PSA_ERROR_INVALID_ARGUMENT
  1302. * \p location is a reserved value.
  1303. * \return #PSA_ERROR_NOT_SUPPORTED
  1304. * `methods->hal_version` is not supported by this implementation.
  1305. * \return #PSA_ERROR_INSUFFICIENT_MEMORY
  1306. * \return #PSA_ERROR_NOT_PERMITTED
  1307. * \return #PSA_ERROR_STORAGE_FAILURE
  1308. * \return #PSA_ERROR_DATA_CORRUPT
  1309. */
  1310. psa_status_t psa_register_se_driver(
  1311. psa_key_location_t location,
  1312. const psa_drv_se_t *methods);
  1313. /**@}*/
  1314. #ifdef __cplusplus
  1315. }
  1316. #endif
  1317. #endif /* PSA_CRYPTO_SE_DRIVER_H */