crypto_se_driver.h 67 KB

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