crypto_se_driver.h 66 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 "crypto_driver_common.h"
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. /** \defgroup se_init Secure element driver initialization
  40. */
  41. /**@{*/
  42. /** \brief Driver context structure
  43. *
  44. * Driver functions receive a pointer to this structure.
  45. * Each registered driver has one instance of this structure.
  46. *
  47. * Implementations must include the fields specified here and
  48. * may include other fields.
  49. */
  50. typedef struct {
  51. /** A read-only pointer to the driver's persistent data.
  52. *
  53. * Drivers typically use this persistent data to keep track of
  54. * which slot numbers are available. This is only a guideline:
  55. * drivers may use the persistent data for any purpose, keeping
  56. * in mind the restrictions on when the persistent data is saved
  57. * to storage: the persistent data is only saved after calling
  58. * certain functions that receive a writable pointer to the
  59. * persistent data.
  60. *
  61. * The core allocates a memory buffer for the persistent data.
  62. * The pointer is guaranteed to be suitably aligned for any data type,
  63. * like a pointer returned by `malloc` (but the core can use any
  64. * method to allocate the buffer, not necessarily `malloc`).
  65. *
  66. * The size of this buffer is in the \c persistent_data_size field of
  67. * this structure.
  68. *
  69. * Before the driver is initialized for the first time, the content of
  70. * the persistent data is all-bits-zero. After a driver upgrade, if the
  71. * size of the persistent data has increased, the original data is padded
  72. * on the right with zeros; if the size has decreased, the original data
  73. * is truncated to the new size.
  74. *
  75. * This pointer is to read-only data. Only a few driver functions are
  76. * allowed to modify the persistent data. These functions receive a
  77. * writable pointer. These functions are:
  78. * - psa_drv_se_t::p_init
  79. * - psa_drv_se_key_management_t::p_allocate
  80. * - psa_drv_se_key_management_t::p_destroy
  81. *
  82. * The PSA Cryptography core saves the persistent data from one
  83. * session to the next. It does this before returning from API functions
  84. * that call a driver method that is allowed to modify the persistent
  85. * data, specifically:
  86. * - psa_crypto_init() causes a call to psa_drv_se_t::p_init, and may call
  87. * psa_drv_se_key_management_t::p_destroy to complete an action
  88. * that was interrupted by a power failure.
  89. * - Key creation functions cause a call to
  90. * psa_drv_se_key_management_t::p_allocate, and may cause a call to
  91. * psa_drv_se_key_management_t::p_destroy in case an error occurs.
  92. * - psa_destroy_key() causes a call to
  93. * psa_drv_se_key_management_t::p_destroy.
  94. */
  95. const void *const persistent_data;
  96. /** The size of \c persistent_data in bytes.
  97. *
  98. * This is always equal to the value of the `persistent_data_size` field
  99. * of the ::psa_drv_se_t structure when the driver is registered.
  100. */
  101. const size_t persistent_data_size;
  102. /** Driver transient data.
  103. *
  104. * The core initializes this value to 0 and does not read or modify it
  105. * afterwards. The driver may store whatever it wants in this field.
  106. */
  107. uintptr_t transient_data;
  108. } psa_drv_se_context_t;
  109. /** \brief A driver initialization function.
  110. *
  111. * \param[in,out] drv_context The driver context structure.
  112. * \param[in,out] persistent_data A pointer to the persistent data
  113. * that allows writing.
  114. * \param location The location value for which this driver
  115. * is registered. The driver will be invoked
  116. * for all keys whose lifetime is in this
  117. * location.
  118. *
  119. * \retval #PSA_SUCCESS
  120. * The driver is operational.
  121. * The core will update the persistent data in storage.
  122. * \return
  123. * Any other return value prevents the driver from being used in
  124. * this session.
  125. * The core will NOT update the persistent data in storage.
  126. */
  127. typedef psa_status_t (*psa_drv_se_init_t)(psa_drv_se_context_t *drv_context,
  128. void *persistent_data,
  129. psa_key_location_t location);
  130. #if defined(__DOXYGEN_ONLY__) || !defined(MBEDTLS_PSA_CRYPTO_SE_C)
  131. /* Mbed Crypto with secure element support enabled defines this type in
  132. * crypto_types.h because it is also visible to applications through an
  133. * implementation-specific extension.
  134. * For the PSA Cryptography specification, this type is only visible
  135. * via crypto_se_driver.h. */
  136. /** An internal designation of a key slot between the core part of the
  137. * PSA Crypto implementation and the driver. The meaning of this value
  138. * is driver-dependent. */
  139. typedef uint64_t psa_key_slot_number_t;
  140. #endif /* __DOXYGEN_ONLY__ || !MBEDTLS_PSA_CRYPTO_SE_C */
  141. /**@}*/
  142. /** \defgroup se_mac Secure Element Message Authentication Codes
  143. * Generation and authentication of Message Authentication Codes (MACs) using
  144. * a secure element can be done either as a single function call (via the
  145. * `psa_drv_se_mac_generate_t` or `psa_drv_se_mac_verify_t` functions), or in
  146. * parts using the following sequence:
  147. * - `psa_drv_se_mac_setup_t`
  148. * - `psa_drv_se_mac_update_t`
  149. * - `psa_drv_se_mac_update_t`
  150. * - ...
  151. * - `psa_drv_se_mac_finish_t` or `psa_drv_se_mac_finish_verify_t`
  152. *
  153. * If a previously started secure element MAC operation needs to be terminated,
  154. * it should be done so by the `psa_drv_se_mac_abort_t`. Failure to do so may
  155. * result in allocated resources not being freed or in other undefined
  156. * behavior.
  157. */
  158. /**@{*/
  159. /** \brief A function that starts a secure element MAC operation for a PSA
  160. * Crypto Driver implementation
  161. *
  162. * \param[in,out] drv_context The driver context structure.
  163. * \param[in,out] op_context A structure that will contain the
  164. * hardware-specific MAC context
  165. * \param[in] key_slot The slot of the key to be used for the
  166. * operation
  167. * \param[in] algorithm The algorithm to be used to underly the MAC
  168. * operation
  169. *
  170. * \retval #PSA_SUCCESS
  171. * Success.
  172. */
  173. typedef psa_status_t (*psa_drv_se_mac_setup_t)(psa_drv_se_context_t *drv_context,
  174. void *op_context,
  175. psa_key_slot_number_t key_slot,
  176. psa_algorithm_t algorithm);
  177. /** \brief A function that continues a previously started secure element MAC
  178. * operation
  179. *
  180. * \param[in,out] op_context A hardware-specific structure for the
  181. * previously-established MAC operation to be
  182. * updated
  183. * \param[in] p_input A buffer containing the message to be appended
  184. * to the MAC operation
  185. * \param[in] input_length The size in bytes of the input message buffer
  186. */
  187. typedef psa_status_t (*psa_drv_se_mac_update_t)(void *op_context,
  188. const uint8_t *p_input,
  189. size_t input_length);
  190. /** \brief a function that completes a previously started secure element MAC
  191. * operation by returning the resulting MAC.
  192. *
  193. * \param[in,out] op_context A hardware-specific structure for the
  194. * previously started MAC operation to be
  195. * finished
  196. * \param[out] p_mac A buffer where the generated MAC will be
  197. * placed
  198. * \param[in] mac_size The size in bytes of the buffer that has been
  199. * allocated for the `output` buffer
  200. * \param[out] p_mac_length After completion, will contain the number of
  201. * bytes placed in the `p_mac` buffer
  202. *
  203. * \retval #PSA_SUCCESS
  204. * Success.
  205. */
  206. typedef psa_status_t (*psa_drv_se_mac_finish_t)(void *op_context,
  207. uint8_t *p_mac,
  208. size_t mac_size,
  209. size_t *p_mac_length);
  210. /** \brief A function that completes a previously started secure element MAC
  211. * operation by comparing the resulting MAC against a provided value
  212. *
  213. * \param[in,out] op_context A hardware-specific structure for the previously
  214. * started MAC operation to be fiinished
  215. * \param[in] p_mac The MAC value against which the resulting MAC
  216. * will be compared against
  217. * \param[in] mac_length The size in bytes of the value stored in `p_mac`
  218. *
  219. * \retval #PSA_SUCCESS
  220. * The operation completed successfully and the MACs matched each
  221. * other
  222. * \retval #PSA_ERROR_INVALID_SIGNATURE
  223. * The operation completed successfully, but the calculated MAC did
  224. * not match the provided MAC
  225. */
  226. typedef psa_status_t (*psa_drv_se_mac_finish_verify_t)(void *op_context,
  227. const uint8_t *p_mac,
  228. size_t mac_length);
  229. /** \brief A function that aborts a previous started secure element MAC
  230. * operation
  231. *
  232. * \param[in,out] op_context A hardware-specific structure for the previously
  233. * started MAC operation to be aborted
  234. */
  235. typedef psa_status_t (*psa_drv_se_mac_abort_t)(void *op_context);
  236. /** \brief A function that performs a secure element MAC operation in one
  237. * command and returns the calculated MAC
  238. *
  239. * \param[in,out] drv_context The driver context structure.
  240. * \param[in] p_input A buffer containing the message to be MACed
  241. * \param[in] input_length The size in bytes of `p_input`
  242. * \param[in] key_slot The slot of the key to be used
  243. * \param[in] alg The algorithm to be used to underlie the MAC
  244. * operation
  245. * \param[out] p_mac A buffer where the generated MAC will be
  246. * placed
  247. * \param[in] mac_size The size in bytes of the `p_mac` buffer
  248. * \param[out] p_mac_length After completion, will contain the number of
  249. * bytes placed in the `output` buffer
  250. *
  251. * \retval #PSA_SUCCESS
  252. * Success.
  253. */
  254. typedef psa_status_t (*psa_drv_se_mac_generate_t)(psa_drv_se_context_t *drv_context,
  255. const uint8_t *p_input,
  256. size_t input_length,
  257. psa_key_slot_number_t key_slot,
  258. psa_algorithm_t alg,
  259. uint8_t *p_mac,
  260. size_t mac_size,
  261. size_t *p_mac_length);
  262. /** \brief A function that performs a secure element MAC operation in one
  263. * command and compares the resulting MAC against a provided value
  264. *
  265. * \param[in,out] drv_context The driver context structure.
  266. * \param[in] p_input A buffer containing the message to be MACed
  267. * \param[in] input_length The size in bytes of `input`
  268. * \param[in] key_slot The slot of the key to be used
  269. * \param[in] alg The algorithm to be used to underlie the MAC
  270. * operation
  271. * \param[in] p_mac The MAC value against which the resulting MAC will
  272. * be compared against
  273. * \param[in] mac_length The size in bytes of `mac`
  274. *
  275. * \retval #PSA_SUCCESS
  276. * The operation completed successfully and the MACs matched each
  277. * other
  278. * \retval #PSA_ERROR_INVALID_SIGNATURE
  279. * The operation completed successfully, but the calculated MAC did
  280. * not match the provided MAC
  281. */
  282. typedef psa_status_t (*psa_drv_se_mac_verify_t)(psa_drv_se_context_t *drv_context,
  283. const uint8_t *p_input,
  284. size_t input_length,
  285. psa_key_slot_number_t key_slot,
  286. psa_algorithm_t alg,
  287. const uint8_t *p_mac,
  288. size_t mac_length);
  289. /** \brief A struct containing all of the function pointers needed to
  290. * perform secure element MAC operations
  291. *
  292. * PSA Crypto API implementations should populate the table as appropriate
  293. * upon startup.
  294. *
  295. * If one of the functions is not implemented (such as
  296. * `psa_drv_se_mac_generate_t`), it should be set to NULL.
  297. *
  298. * Driver implementers should ensure that they implement all of the functions
  299. * that make sense for their hardware, and that they provide a full solution
  300. * (for example, if they support `p_setup`, they should also support
  301. * `p_update` and at least one of `p_finish` or `p_finish_verify`).
  302. *
  303. */
  304. typedef struct {
  305. /**The size in bytes of the hardware-specific secure element MAC context
  306. * structure
  307. */
  308. size_t context_size;
  309. /** Function that performs a MAC setup operation
  310. */
  311. psa_drv_se_mac_setup_t p_setup;
  312. /** Function that performs a MAC update operation
  313. */
  314. psa_drv_se_mac_update_t p_update;
  315. /** Function that completes a MAC operation
  316. */
  317. psa_drv_se_mac_finish_t p_finish;
  318. /** Function that completes a MAC operation with a verify check
  319. */
  320. psa_drv_se_mac_finish_verify_t p_finish_verify;
  321. /** Function that aborts a previoustly started MAC operation
  322. */
  323. psa_drv_se_mac_abort_t p_abort;
  324. /** Function that performs a MAC operation in one call
  325. */
  326. psa_drv_se_mac_generate_t p_mac;
  327. /** Function that performs a MAC and verify operation in one call
  328. */
  329. psa_drv_se_mac_verify_t p_mac_verify;
  330. } psa_drv_se_mac_t;
  331. /**@}*/
  332. /** \defgroup se_cipher Secure Element Symmetric Ciphers
  333. *
  334. * Encryption and Decryption using secure element keys in block modes other
  335. * than ECB must be done in multiple parts, using the following flow:
  336. * - `psa_drv_se_cipher_setup_t`
  337. * - `psa_drv_se_cipher_set_iv_t` (optional depending upon block mode)
  338. * - `psa_drv_se_cipher_update_t`
  339. * - `psa_drv_se_cipher_update_t`
  340. * - ...
  341. * - `psa_drv_se_cipher_finish_t`
  342. *
  343. * If a previously started secure element Cipher operation needs to be
  344. * terminated, it should be done so by the `psa_drv_se_cipher_abort_t`. Failure
  345. * to do so may result in allocated resources not being freed or in other
  346. * undefined behavior.
  347. *
  348. * In situations where a PSA Cryptographic API implementation is using a block
  349. * mode not-supported by the underlying hardware or driver, it can construct
  350. * the block mode itself, while calling the `psa_drv_se_cipher_ecb_t` function
  351. * for the cipher operations.
  352. */
  353. /**@{*/
  354. /** \brief A function that provides the cipher setup function for a
  355. * secure element driver
  356. *
  357. * \param[in,out] drv_context The driver context structure.
  358. * \param[in,out] op_context A structure that will contain the
  359. * hardware-specific cipher context.
  360. * \param[in] key_slot The slot of the key to be used for the
  361. * operation
  362. * \param[in] algorithm The algorithm to be used in the cipher
  363. * operation
  364. * \param[in] direction Indicates whether the operation is an encrypt
  365. * or decrypt
  366. *
  367. * \retval #PSA_SUCCESS
  368. * \retval #PSA_ERROR_NOT_SUPPORTED
  369. */
  370. typedef psa_status_t (*psa_drv_se_cipher_setup_t)(psa_drv_se_context_t *drv_context,
  371. void *op_context,
  372. psa_key_slot_number_t key_slot,
  373. psa_algorithm_t algorithm,
  374. psa_encrypt_or_decrypt_t direction);
  375. /** \brief A function that sets the initialization vector (if
  376. * necessary) for an secure element cipher operation
  377. *
  378. * Rationale: The `psa_se_cipher_*` operation in the PSA Cryptographic API has
  379. * two IV functions: one to set the IV, and one to generate it internally. The
  380. * generate function is not necessary for the drivers to implement as the PSA
  381. * Crypto implementation can do the generation using its RNG features.
  382. *
  383. * \param[in,out] op_context A structure that contains the previously set up
  384. * hardware-specific cipher context
  385. * \param[in] p_iv A buffer containing the initialization vector
  386. * \param[in] iv_length The size (in bytes) of the `p_iv` buffer
  387. *
  388. * \retval #PSA_SUCCESS
  389. */
  390. typedef psa_status_t (*psa_drv_se_cipher_set_iv_t)(void *op_context,
  391. const uint8_t *p_iv,
  392. size_t iv_length);
  393. /** \brief A function that continues a previously started secure element cipher
  394. * operation
  395. *
  396. * \param[in,out] op_context A hardware-specific structure for the
  397. * previously started cipher operation
  398. * \param[in] p_input A buffer containing the data to be
  399. * encrypted/decrypted
  400. * \param[in] input_size The size in bytes of the buffer pointed to
  401. * by `p_input`
  402. * \param[out] p_output The caller-allocated buffer where the
  403. * output will be placed
  404. * \param[in] output_size The allocated size in bytes of the
  405. * `p_output` buffer
  406. * \param[out] p_output_length After completion, will contain the number
  407. * of bytes placed in the `p_output` buffer
  408. *
  409. * \retval #PSA_SUCCESS
  410. */
  411. typedef psa_status_t (*psa_drv_se_cipher_update_t)(void *op_context,
  412. const uint8_t *p_input,
  413. size_t input_size,
  414. uint8_t *p_output,
  415. size_t output_size,
  416. size_t *p_output_length);
  417. /** \brief A function that completes a previously started secure element cipher
  418. * operation
  419. *
  420. * \param[in,out] op_context A hardware-specific structure for the
  421. * previously started cipher operation
  422. * \param[out] p_output The caller-allocated buffer where the output
  423. * will be placed
  424. * \param[in] output_size The allocated size in bytes of the `p_output`
  425. * buffer
  426. * \param[out] p_output_length After completion, will contain the number of
  427. * bytes placed in the `p_output` buffer
  428. *
  429. * \retval #PSA_SUCCESS
  430. */
  431. typedef psa_status_t (*psa_drv_se_cipher_finish_t)(void *op_context,
  432. uint8_t *p_output,
  433. size_t output_size,
  434. size_t *p_output_length);
  435. /** \brief A function that aborts a previously started secure element cipher
  436. * operation
  437. *
  438. * \param[in,out] op_context A hardware-specific structure for the
  439. * previously started cipher operation
  440. */
  441. typedef psa_status_t (*psa_drv_se_cipher_abort_t)(void *op_context);
  442. /** \brief A function that performs the ECB block mode for secure element
  443. * cipher operations
  444. *
  445. * Note: this function should only be used with implementations that do not
  446. * provide a needed higher-level operation.
  447. *
  448. * \param[in,out] drv_context The driver context structure.
  449. * \param[in] key_slot The slot of the key to be used for the operation
  450. * \param[in] algorithm The algorithm to be used in the cipher operation
  451. * \param[in] direction Indicates whether the operation is an encrypt or
  452. * decrypt
  453. * \param[in] p_input A buffer containing the data to be
  454. * encrypted/decrypted
  455. * \param[in] input_size The size in bytes of the buffer pointed to by
  456. * `p_input`
  457. * \param[out] p_output The caller-allocated buffer where the output
  458. * will be placed
  459. * \param[in] output_size The allocated size in bytes of the `p_output`
  460. * buffer
  461. *
  462. * \retval #PSA_SUCCESS
  463. * \retval #PSA_ERROR_NOT_SUPPORTED
  464. */
  465. typedef psa_status_t (*psa_drv_se_cipher_ecb_t)(psa_drv_se_context_t *drv_context,
  466. psa_key_slot_number_t key_slot,
  467. psa_algorithm_t algorithm,
  468. psa_encrypt_or_decrypt_t direction,
  469. const uint8_t *p_input,
  470. size_t input_size,
  471. uint8_t *p_output,
  472. size_t output_size);
  473. /**
  474. * \brief A struct containing all of the function pointers needed to implement
  475. * cipher operations using secure elements.
  476. *
  477. * PSA Crypto API implementations should populate instances of the table as
  478. * appropriate upon startup or at build time.
  479. *
  480. * If one of the functions is not implemented (such as
  481. * `psa_drv_se_cipher_ecb_t`), it should be set to NULL.
  482. */
  483. typedef struct {
  484. /** The size in bytes of the hardware-specific secure element cipher
  485. * context structure
  486. */
  487. size_t context_size;
  488. /** Function that performs a cipher setup operation */
  489. psa_drv_se_cipher_setup_t p_setup;
  490. /** Function that sets a cipher IV (if necessary) */
  491. psa_drv_se_cipher_set_iv_t p_set_iv;
  492. /** Function that performs a cipher update operation */
  493. psa_drv_se_cipher_update_t p_update;
  494. /** Function that completes a cipher operation */
  495. psa_drv_se_cipher_finish_t p_finish;
  496. /** Function that aborts a cipher operation */
  497. psa_drv_se_cipher_abort_t p_abort;
  498. /** Function that performs ECB mode for a cipher operation
  499. * (Danger: ECB mode should not be used directly by clients of the PSA
  500. * Crypto Client API)
  501. */
  502. psa_drv_se_cipher_ecb_t p_ecb;
  503. } psa_drv_se_cipher_t;
  504. /**@}*/
  505. /** \defgroup se_asymmetric Secure Element Asymmetric Cryptography
  506. *
  507. * Since the amount of data that can (or should) be encrypted or signed using
  508. * asymmetric keys is limited by the key size, asymmetric key operations using
  509. * keys in a secure element must be done in single function calls.
  510. */
  511. /**@{*/
  512. /**
  513. * \brief A function that signs a hash or short message with a private key in
  514. * a secure element
  515. *
  516. * \param[in,out] drv_context The driver context structure.
  517. * \param[in] key_slot Key slot of an asymmetric key pair
  518. * \param[in] alg A signature algorithm that is compatible
  519. * with the type of `key`
  520. * \param[in] p_hash The hash to sign
  521. * \param[in] hash_length Size of the `p_hash` buffer in bytes
  522. * \param[out] p_signature Buffer where the signature is to be written
  523. * \param[in] signature_size Size of the `p_signature` buffer in bytes
  524. * \param[out] p_signature_length On success, the number of bytes
  525. * that make up the returned signature value
  526. *
  527. * \retval #PSA_SUCCESS
  528. */
  529. typedef psa_status_t (*psa_drv_se_asymmetric_sign_t)(psa_drv_se_context_t *drv_context,
  530. psa_key_slot_number_t key_slot,
  531. psa_algorithm_t alg,
  532. const uint8_t *p_hash,
  533. size_t hash_length,
  534. uint8_t *p_signature,
  535. size_t signature_size,
  536. size_t *p_signature_length);
  537. /**
  538. * \brief A function that verifies the signature a hash or short message using
  539. * an asymmetric public key in a secure element
  540. *
  541. * \param[in,out] drv_context The driver context structure.
  542. * \param[in] key_slot Key slot of a public key or an asymmetric key
  543. * pair
  544. * \param[in] alg A signature algorithm that is compatible with
  545. * the type of `key`
  546. * \param[in] p_hash The hash whose signature is to be verified
  547. * \param[in] hash_length Size of the `p_hash` buffer in bytes
  548. * \param[in] p_signature Buffer containing the signature to verify
  549. * \param[in] signature_length Size of the `p_signature` buffer in bytes
  550. *
  551. * \retval #PSA_SUCCESS
  552. * The signature is valid.
  553. */
  554. typedef psa_status_t (*psa_drv_se_asymmetric_verify_t)(psa_drv_se_context_t *drv_context,
  555. psa_key_slot_number_t key_slot,
  556. psa_algorithm_t alg,
  557. const uint8_t *p_hash,
  558. size_t hash_length,
  559. const uint8_t *p_signature,
  560. size_t signature_length);
  561. /**
  562. * \brief A function that encrypts a short message with an asymmetric public
  563. * key in a secure element
  564. *
  565. * \param[in,out] drv_context The driver context structure.
  566. * \param[in] key_slot Key slot of a public key or an asymmetric key
  567. * pair
  568. * \param[in] alg An asymmetric encryption algorithm that is
  569. * compatible with the type of `key`
  570. * \param[in] p_input The message to encrypt
  571. * \param[in] input_length Size of the `p_input` buffer in bytes
  572. * \param[in] p_salt A salt or label, if supported by the
  573. * encryption algorithm
  574. * If the algorithm does not support a
  575. * salt, pass `NULL`.
  576. * If the algorithm supports an optional
  577. * salt and you do not want to pass a salt,
  578. * pass `NULL`.
  579. * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  580. * supported.
  581. * \param[in] salt_length Size of the `p_salt` buffer in bytes
  582. * If `p_salt` is `NULL`, pass 0.
  583. * \param[out] p_output Buffer where the encrypted message is to
  584. * be written
  585. * \param[in] output_size Size of the `p_output` buffer in bytes
  586. * \param[out] p_output_length On success, the number of bytes that make up
  587. * the returned output
  588. *
  589. * \retval #PSA_SUCCESS
  590. */
  591. typedef psa_status_t (*psa_drv_se_asymmetric_encrypt_t)(psa_drv_se_context_t *drv_context,
  592. psa_key_slot_number_t key_slot,
  593. psa_algorithm_t alg,
  594. const uint8_t *p_input,
  595. size_t input_length,
  596. const uint8_t *p_salt,
  597. size_t salt_length,
  598. uint8_t *p_output,
  599. size_t output_size,
  600. size_t *p_output_length);
  601. /**
  602. * \brief A function that decrypts a short message with an asymmetric private
  603. * key in a secure element.
  604. *
  605. * \param[in,out] drv_context The driver context structure.
  606. * \param[in] key_slot Key slot of an asymmetric key pair
  607. * \param[in] alg An asymmetric encryption algorithm that is
  608. * compatible with the type of `key`
  609. * \param[in] p_input The message to decrypt
  610. * \param[in] input_length Size of the `p_input` buffer in bytes
  611. * \param[in] p_salt A salt or label, if supported by the
  612. * encryption algorithm
  613. * If the algorithm does not support a
  614. * salt, pass `NULL`.
  615. * If the algorithm supports an optional
  616. * salt and you do not want to pass a salt,
  617. * pass `NULL`.
  618. * For #PSA_ALG_RSA_PKCS1V15_CRYPT, no salt is
  619. * supported.
  620. * \param[in] salt_length Size of the `p_salt` buffer in bytes
  621. * If `p_salt` is `NULL`, pass 0.
  622. * \param[out] p_output Buffer where the decrypted message is to
  623. * be written
  624. * \param[in] output_size Size of the `p_output` buffer in bytes
  625. * \param[out] p_output_length On success, the number of bytes
  626. * that make up the returned output
  627. *
  628. * \retval #PSA_SUCCESS
  629. */
  630. typedef psa_status_t (*psa_drv_se_asymmetric_decrypt_t)(psa_drv_se_context_t *drv_context,
  631. psa_key_slot_number_t key_slot,
  632. psa_algorithm_t alg,
  633. const uint8_t *p_input,
  634. size_t input_length,
  635. const uint8_t *p_salt,
  636. size_t salt_length,
  637. uint8_t *p_output,
  638. size_t output_size,
  639. size_t *p_output_length);
  640. /**
  641. * \brief A struct containing all of the function pointers needed to implement
  642. * asymmetric cryptographic operations using secure elements.
  643. *
  644. * PSA Crypto API implementations should populate instances of the table as
  645. * appropriate upon startup or at build time.
  646. *
  647. * If one of the functions is not implemented, it should be set to NULL.
  648. */
  649. typedef struct {
  650. /** Function that performs an asymmetric sign operation */
  651. psa_drv_se_asymmetric_sign_t p_sign;
  652. /** Function that performs an asymmetric verify operation */
  653. psa_drv_se_asymmetric_verify_t p_verify;
  654. /** Function that performs an asymmetric encrypt operation */
  655. psa_drv_se_asymmetric_encrypt_t p_encrypt;
  656. /** Function that performs an asymmetric decrypt operation */
  657. psa_drv_se_asymmetric_decrypt_t p_decrypt;
  658. } psa_drv_se_asymmetric_t;
  659. /**@}*/
  660. /** \defgroup se_aead Secure Element Authenticated Encryption with Additional Data
  661. * Authenticated Encryption with Additional Data (AEAD) operations with secure
  662. * elements must be done in one function call. While this creates a burden for
  663. * implementers as there must be sufficient space in memory for the entire
  664. * message, it prevents decrypted data from being made available before the
  665. * authentication operation is complete and the data is known to be authentic.
  666. */
  667. /**@{*/
  668. /** \brief A function that performs a secure element authenticated encryption
  669. * operation
  670. *
  671. * \param[in,out] drv_context The driver context structure.
  672. * \param[in] key_slot Slot containing the key to use.
  673. * \param[in] algorithm The AEAD algorithm to compute
  674. * (\c PSA_ALG_XXX value such that
  675. * #PSA_ALG_IS_AEAD(`alg`) is true)
  676. * \param[in] p_nonce Nonce or IV to use
  677. * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
  678. * \param[in] p_additional_data Additional data that will be
  679. * authenticated but not encrypted
  680. * \param[in] additional_data_length Size of `p_additional_data` in bytes
  681. * \param[in] p_plaintext Data that will be authenticated and
  682. * encrypted
  683. * \param[in] plaintext_length Size of `p_plaintext` in bytes
  684. * \param[out] p_ciphertext Output buffer for the authenticated and
  685. * encrypted data. The additional data is
  686. * not part of this output. For algorithms
  687. * where the encrypted data and the
  688. * authentication tag are defined as
  689. * separate outputs, the authentication
  690. * tag is appended to the encrypted data.
  691. * \param[in] ciphertext_size Size of the `p_ciphertext` buffer in
  692. * bytes
  693. * \param[out] p_ciphertext_length On success, the size of the output in
  694. * the `p_ciphertext` buffer
  695. *
  696. * \retval #PSA_SUCCESS
  697. * Success.
  698. */
  699. typedef psa_status_t (*psa_drv_se_aead_encrypt_t)(psa_drv_se_context_t *drv_context,
  700. psa_key_slot_number_t key_slot,
  701. psa_algorithm_t algorithm,
  702. const uint8_t *p_nonce,
  703. size_t nonce_length,
  704. const uint8_t *p_additional_data,
  705. size_t additional_data_length,
  706. const uint8_t *p_plaintext,
  707. size_t plaintext_length,
  708. uint8_t *p_ciphertext,
  709. size_t ciphertext_size,
  710. size_t *p_ciphertext_length);
  711. /** A function that peforms a secure element authenticated decryption operation
  712. *
  713. * \param[in,out] drv_context The driver context structure.
  714. * \param[in] key_slot Slot containing the key to use
  715. * \param[in] algorithm The AEAD algorithm to compute
  716. * (\c PSA_ALG_XXX value such that
  717. * #PSA_ALG_IS_AEAD(`alg`) is true)
  718. * \param[in] p_nonce Nonce or IV to use
  719. * \param[in] nonce_length Size of the `p_nonce` buffer in bytes
  720. * \param[in] p_additional_data Additional data that has been
  721. * authenticated but not encrypted
  722. * \param[in] additional_data_length Size of `p_additional_data` in bytes
  723. * \param[in] p_ciphertext Data that has been authenticated and
  724. * encrypted.
  725. * For algorithms where the encrypted data
  726. * and the authentication tag are defined
  727. * as separate inputs, the buffer must
  728. * contain the encrypted data followed by
  729. * the authentication tag.
  730. * \param[in] ciphertext_length Size of `p_ciphertext` in bytes
  731. * \param[out] p_plaintext Output buffer for the decrypted data
  732. * \param[in] plaintext_size Size of the `p_plaintext` buffer in
  733. * bytes
  734. * \param[out] p_plaintext_length On success, the size of the output in
  735. * the `p_plaintext` buffer
  736. *
  737. * \retval #PSA_SUCCESS
  738. * Success.
  739. */
  740. typedef psa_status_t (*psa_drv_se_aead_decrypt_t)(psa_drv_se_context_t *drv_context,
  741. psa_key_slot_number_t key_slot,
  742. psa_algorithm_t algorithm,
  743. const uint8_t *p_nonce,
  744. size_t nonce_length,
  745. const uint8_t *p_additional_data,
  746. size_t additional_data_length,
  747. const uint8_t *p_ciphertext,
  748. size_t ciphertext_length,
  749. uint8_t *p_plaintext,
  750. size_t plaintext_size,
  751. size_t *p_plaintext_length);
  752. /**
  753. * \brief A struct containing all of the function pointers needed to implement
  754. * secure element Authenticated Encryption with Additional Data operations
  755. *
  756. * PSA Crypto API implementations should populate instances of the table as
  757. * appropriate upon startup.
  758. *
  759. * If one of the functions is not implemented, it should be set to NULL.
  760. */
  761. typedef struct {
  762. /** Function that performs the AEAD encrypt operation */
  763. psa_drv_se_aead_encrypt_t p_encrypt;
  764. /** Function that performs the AEAD decrypt operation */
  765. psa_drv_se_aead_decrypt_t p_decrypt;
  766. } psa_drv_se_aead_t;
  767. /**@}*/
  768. /** \defgroup se_key_management Secure Element Key Management
  769. * Currently, key management is limited to importing keys in the clear,
  770. * destroying keys, and exporting keys in the clear.
  771. * Whether a key may be exported is determined by the key policies in place
  772. * on the key slot.
  773. */
  774. /**@{*/
  775. /** An enumeration indicating how a key is created.
  776. */
  777. typedef enum
  778. {
  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
  866. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
  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
  1001. * \retval #PSA_ERROR_DOES_NOT_EXIST
  1002. * \retval #PSA_ERROR_NOT_PERMITTED
  1003. * \retval #PSA_ERROR_NOT_SUPPORTED
  1004. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  1005. * \retval #PSA_ERROR_HARDWARE_FAILURE
  1006. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  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 p_allocate;
  1076. /** Function that checks the validity of a slot for a key. */
  1077. psa_drv_se_validate_slot_number_t p_validate_slot_number;
  1078. /** Function that performs a key import operation */
  1079. psa_drv_se_import_key_t p_import;
  1080. /** Function that performs a generation */
  1081. psa_drv_se_generate_key_t p_generate;
  1082. /** Function that performs a key destroy operation */
  1083. psa_drv_se_destroy_key_t p_destroy;
  1084. /** Function that performs a key export operation */
  1085. psa_drv_se_export_key_t p_export;
  1086. /** Function that performs a public key export operation */
  1087. psa_drv_se_export_key_t 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 problemmatic 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
  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
  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
  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
  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 context_size;
  1212. /** Function that performs a key derivation setup */
  1213. psa_drv_se_key_derivation_setup_t p_setup;
  1214. /** Function that sets key derivation collateral */
  1215. psa_drv_se_key_derivation_collateral_t p_collateral;
  1216. /** Function that performs a final key derivation step */
  1217. psa_drv_se_key_derivation_derive_t p_derive;
  1218. /** Function that perforsm a final key derivation or agreement and
  1219. * exports the key */
  1220. psa_drv_se_key_derivation_export_t 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 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 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 p_init;
  1259. const psa_drv_se_key_management_t *key_management;
  1260. const psa_drv_se_mac_t *mac;
  1261. const psa_drv_se_cipher_t *cipher;
  1262. const psa_drv_se_aead_t *aead;
  1263. const psa_drv_se_asymmetric_t *asymmetric;
  1264. const psa_drv_se_key_derivation_t *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 */