psa_crypto_core.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * PSA crypto core internal interfaces
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  7. */
  8. #ifndef PSA_CRYPTO_CORE_H
  9. #define PSA_CRYPTO_CORE_H
  10. /*
  11. * Include the build-time configuration information header. Here, we do not
  12. * include `"mbedtls/build_info.h"` directly but `"psa/build_info.h"`, which
  13. * is basically just an alias to it. This is to ease the maintenance of the
  14. * TF-PSA-Crypto repository which has a different build system and
  15. * configuration.
  16. */
  17. #include "psa/build_info.h"
  18. #include "psa/crypto.h"
  19. #include "psa/crypto_se_driver.h"
  20. #if defined(MBEDTLS_THREADING_C)
  21. #include "mbedtls/threading.h"
  22. #endif
  23. /**
  24. * Tell if PSA is ready for this cipher.
  25. *
  26. * \note For now, only checks the state of the driver subsystem,
  27. * not the algorithm. Might do more in the future.
  28. *
  29. * \param cipher_alg The cipher algorithm (ignored for now).
  30. *
  31. * \return 1 if the driver subsytem is ready, 0 otherwise.
  32. */
  33. int psa_can_do_cipher(psa_key_type_t key_type, psa_algorithm_t cipher_alg);
  34. typedef enum {
  35. PSA_SLOT_EMPTY = 0,
  36. PSA_SLOT_FILLING,
  37. PSA_SLOT_FULL,
  38. PSA_SLOT_PENDING_DELETION,
  39. } psa_key_slot_state_t;
  40. /** The data structure representing a key slot, containing key material
  41. * and metadata for one key.
  42. */
  43. typedef struct {
  44. /* This field is accessed in a lot of places. Putting it first
  45. * reduces the code size. */
  46. psa_key_attributes_t attr;
  47. /*
  48. * The current state of the key slot, as described in
  49. * docs/architecture/psa-thread-safety/psa-thread-safety.md.
  50. *
  51. * Library functions can modify the state of a key slot by calling
  52. * psa_key_slot_state_transition.
  53. *
  54. * The state variable is used to help determine whether library functions
  55. * which operate on the slot succeed. For example, psa_finish_key_creation,
  56. * which transfers the state of a slot from PSA_SLOT_FILLING to
  57. * PSA_SLOT_FULL, must fail with error code PSA_ERROR_CORRUPTION_DETECTED
  58. * if the state of the slot is not PSA_SLOT_FILLING.
  59. *
  60. * Library functions which traverse the array of key slots only consider
  61. * slots that are in a suitable state for the function.
  62. * For example, psa_get_and_lock_key_slot_in_memory, which finds a slot
  63. * containing a given key ID, will only check slots whose state variable is
  64. * PSA_SLOT_FULL.
  65. */
  66. psa_key_slot_state_t state;
  67. #if defined(MBEDTLS_PSA_KEY_STORE_DYNAMIC)
  68. /* The index of the slice containing this slot.
  69. * This field must be filled if the slot contains a key
  70. * (including keys being created or destroyed), and can be either
  71. * filled or 0 when the slot is free.
  72. *
  73. * In most cases, the slice index can be deduced from the key identifer.
  74. * We keep it in a separate field for robustness (it reduces the chance
  75. * that a coding mistake in the key store will result in accessing the
  76. * wrong slice), and also so that it's available even on code paths
  77. * during creation or destruction where the key identifier might not be
  78. * filled in.
  79. * */
  80. uint8_t slice_index;
  81. #endif /* MBEDTLS_PSA_KEY_STORE_DYNAMIC */
  82. union {
  83. struct {
  84. /* The index of the next slot in the free list for this
  85. * slice, relative * to the next array element.
  86. *
  87. * That is, 0 means the next slot, 1 means the next slot
  88. * but one, etc. -1 would mean the slot itself. -2 means
  89. * the previous slot, etc.
  90. *
  91. * If this is beyond the array length, the free list ends with the
  92. * current element.
  93. *
  94. * The reason for this strange encoding is that 0 means the next
  95. * element. This way, when we allocate a slice and initialize it
  96. * to all-zero, the slice is ready for use, with a free list that
  97. * consists of all the slots in order.
  98. */
  99. int32_t next_free_relative_to_next;
  100. } free;
  101. struct {
  102. /*
  103. * Number of functions registered as reading the material in the key slot.
  104. *
  105. * Library functions must not write directly to registered_readers
  106. *
  107. * A function must call psa_register_read(slot) before reading
  108. * the current contents of the slot for an operation.
  109. * They then must call psa_unregister_read(slot) once they have
  110. * finished reading the current contents of the slot. If the key
  111. * slot mutex is not held (when mutexes are enabled), this call
  112. * must be done via a call to
  113. * psa_unregister_read_under_mutex(slot).
  114. * A function must call psa_key_slot_has_readers(slot) to check if
  115. * the slot is in use for reading.
  116. *
  117. * This counter is used to prevent resetting the key slot while
  118. * the library may access it. For example, such control is needed
  119. * in the following scenarios:
  120. * . In case of key slot starvation, all key slots contain the
  121. * description of a key, and the library asks for the
  122. * description of a persistent key not present in the
  123. * key slots, the key slots currently accessed by the
  124. * library cannot be reclaimed to free a key slot to load
  125. * the persistent key.
  126. * . In case of a multi-threaded application where one thread
  127. * asks to close or purge or destroy a key while it is in use
  128. * by the library through another thread. */
  129. size_t registered_readers;
  130. } occupied;
  131. } var;
  132. /* Dynamically allocated key data buffer.
  133. * Format as specified in psa_export_key(). */
  134. struct key_data {
  135. #if defined(MBEDTLS_PSA_STATIC_KEY_SLOTS)
  136. uint8_t data[MBEDTLS_PSA_STATIC_KEY_SLOT_BUFFER_SIZE];
  137. #else
  138. uint8_t *data;
  139. #endif
  140. size_t bytes;
  141. } key;
  142. } psa_key_slot_t;
  143. #if defined(MBEDTLS_THREADING_C)
  144. /** Perform a mutex operation and return immediately upon failure.
  145. *
  146. * Returns PSA_ERROR_SERVICE_FAILURE if the operation fails
  147. * and status was PSA_SUCCESS.
  148. *
  149. * Assumptions:
  150. * psa_status_t status exists.
  151. * f is a mutex operation which returns 0 upon success.
  152. */
  153. #define PSA_THREADING_CHK_RET(f) \
  154. do \
  155. { \
  156. if ((f) != 0) { \
  157. if (status == PSA_SUCCESS) { \
  158. return PSA_ERROR_SERVICE_FAILURE; \
  159. } \
  160. return status; \
  161. } \
  162. } while (0);
  163. /** Perform a mutex operation and goto exit on failure.
  164. *
  165. * Sets status to PSA_ERROR_SERVICE_FAILURE if status was PSA_SUCCESS.
  166. *
  167. * Assumptions:
  168. * psa_status_t status exists.
  169. * Label exit: exists.
  170. * f is a mutex operation which returns 0 upon success.
  171. */
  172. #define PSA_THREADING_CHK_GOTO_EXIT(f) \
  173. do \
  174. { \
  175. if ((f) != 0) { \
  176. if (status == PSA_SUCCESS) { \
  177. status = PSA_ERROR_SERVICE_FAILURE; \
  178. } \
  179. goto exit; \
  180. } \
  181. } while (0);
  182. #endif
  183. /** Test whether a key slot has any registered readers.
  184. * If multi-threading is enabled, the caller must hold the
  185. * global key slot mutex.
  186. *
  187. * \param[in] slot The key slot to test.
  188. *
  189. * \return 1 if the slot has any registered readers, 0 otherwise.
  190. */
  191. static inline int psa_key_slot_has_readers(const psa_key_slot_t *slot)
  192. {
  193. return slot->var.occupied.registered_readers > 0;
  194. }
  195. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  196. /** Get the SE slot number of a key from the key slot storing its description.
  197. *
  198. * \param[in] slot The key slot to query. This must be a key slot storing
  199. * the description of a key of a dynamically registered
  200. * secure element, otherwise the behaviour is undefined.
  201. */
  202. static inline psa_key_slot_number_t psa_key_slot_get_slot_number(
  203. const psa_key_slot_t *slot)
  204. {
  205. return *((psa_key_slot_number_t *) (slot->key.data));
  206. }
  207. #endif
  208. /** Completely wipe a slot in memory, including its policy.
  209. *
  210. * Persistent storage is not affected.
  211. * Sets the slot's state to PSA_SLOT_EMPTY.
  212. * If multi-threading is enabled, the caller must hold the
  213. * global key slot mutex.
  214. *
  215. * \param[in,out] slot The key slot to wipe.
  216. *
  217. * \retval #PSA_SUCCESS
  218. * The slot has been successfully wiped.
  219. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  220. * The slot's state was PSA_SLOT_FULL or PSA_SLOT_PENDING_DELETION, and
  221. * the amount of registered readers was not equal to 1. Or,
  222. * the slot's state was PSA_SLOT_EMPTY. Or,
  223. * the slot's state was PSA_SLOT_FILLING, and the amount
  224. * of registered readers was not equal to 0.
  225. */
  226. psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot);
  227. /** Try to allocate a buffer to an empty key slot.
  228. *
  229. * \param[in,out] slot Key slot to attach buffer to.
  230. * \param[in] buffer_length Requested size of the buffer.
  231. *
  232. * \retval #PSA_SUCCESS
  233. * The buffer has been successfully allocated.
  234. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  235. * Not enough memory was available for allocation.
  236. * \retval #PSA_ERROR_ALREADY_EXISTS
  237. * Trying to allocate a buffer to a non-empty key slot.
  238. */
  239. psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
  240. size_t buffer_length);
  241. /** Wipe key data from a slot. Preserves metadata such as the policy. */
  242. psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot);
  243. /** Copy key data (in export format) into an empty key slot.
  244. *
  245. * This function assumes that the slot does not contain
  246. * any key material yet. On failure, the slot content is unchanged.
  247. *
  248. * \param[in,out] slot Key slot to copy the key into.
  249. * \param[in] data Buffer containing the key material.
  250. * \param data_length Size of the key buffer.
  251. *
  252. * \retval #PSA_SUCCESS
  253. * The key has been copied successfully.
  254. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  255. * Not enough memory was available for allocation of the
  256. * copy buffer.
  257. * \retval #PSA_ERROR_ALREADY_EXISTS
  258. * There was other key material already present in the slot.
  259. */
  260. psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
  261. const uint8_t *data,
  262. size_t data_length);
  263. /** Convert an Mbed TLS error code to a PSA error code
  264. *
  265. * \note This function is provided solely for the convenience of
  266. * Mbed TLS and may be removed at any time without notice.
  267. *
  268. * \param ret An Mbed TLS-thrown error code
  269. *
  270. * \return The corresponding PSA error code
  271. */
  272. psa_status_t mbedtls_to_psa_error(int ret);
  273. /** Import a key in binary format.
  274. *
  275. * \note The signature of this function is that of a PSA driver
  276. * import_key entry point. This function behaves as an import_key
  277. * entry point as defined in the PSA driver interface specification for
  278. * transparent drivers.
  279. *
  280. * \param[in] attributes The attributes for the key to import.
  281. * \param[in] data The buffer containing the key data in import
  282. * format.
  283. * \param[in] data_length Size of the \p data buffer in bytes.
  284. * \param[out] key_buffer The buffer to contain the key data in output
  285. * format upon successful return.
  286. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
  287. * size is greater or equal to \p data_length.
  288. * \param[out] key_buffer_length The length of the data written in \p
  289. * key_buffer in bytes.
  290. * \param[out] bits The key size in number of bits.
  291. *
  292. * \retval #PSA_SUCCESS The key was imported successfully.
  293. * \retval #PSA_ERROR_INVALID_ARGUMENT
  294. * The key data is not correctly formatted.
  295. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  296. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  297. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  298. */
  299. psa_status_t psa_import_key_into_slot(
  300. const psa_key_attributes_t *attributes,
  301. const uint8_t *data, size_t data_length,
  302. uint8_t *key_buffer, size_t key_buffer_size,
  303. size_t *key_buffer_length, size_t *bits);
  304. /** Export a key in binary format
  305. *
  306. * \note The signature of this function is that of a PSA driver export_key
  307. * entry point. This function behaves as an export_key entry point as
  308. * defined in the PSA driver interface specification.
  309. *
  310. * \param[in] attributes The attributes for the key to export.
  311. * \param[in] key_buffer Material or context of the key to export.
  312. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  313. * \param[out] data Buffer where the key data is to be written.
  314. * \param[in] data_size Size of the \p data buffer in bytes.
  315. * \param[out] data_length On success, the number of bytes written in
  316. * \p data
  317. *
  318. * \retval #PSA_SUCCESS The key was exported successfully.
  319. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  320. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  321. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  322. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  323. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  324. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  325. */
  326. psa_status_t psa_export_key_internal(
  327. const psa_key_attributes_t *attributes,
  328. const uint8_t *key_buffer, size_t key_buffer_size,
  329. uint8_t *data, size_t data_size, size_t *data_length);
  330. /** Export a public key or the public part of a key pair in binary format.
  331. *
  332. * \note The signature of this function is that of a PSA driver
  333. * export_public_key entry point. This function behaves as an
  334. * export_public_key entry point as defined in the PSA driver interface
  335. * specification.
  336. *
  337. * \param[in] attributes The attributes for the key to export.
  338. * \param[in] key_buffer Material or context of the key to export.
  339. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  340. * \param[out] data Buffer where the key data is to be written.
  341. * \param[in] data_size Size of the \p data buffer in bytes.
  342. * \param[out] data_length On success, the number of bytes written in
  343. * \p data
  344. *
  345. * \retval #PSA_SUCCESS The public key was exported successfully.
  346. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  347. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  348. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  349. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  350. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  351. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  352. */
  353. psa_status_t psa_export_public_key_internal(
  354. const psa_key_attributes_t *attributes,
  355. const uint8_t *key_buffer, size_t key_buffer_size,
  356. uint8_t *data, size_t data_size, size_t *data_length);
  357. /** Whether a key custom production parameters structure is the default.
  358. *
  359. * Calls to a key generation driver with non-default custom production parameters
  360. * require a driver supporting custom production parameters.
  361. *
  362. * \param[in] custom The key custom production parameters to check.
  363. * \param custom_data_length Size of the associated variable-length data
  364. * in bytes.
  365. */
  366. int psa_custom_key_parameters_are_default(
  367. const psa_custom_key_parameters_t *custom,
  368. size_t custom_data_length);
  369. /**
  370. * \brief Generate a key.
  371. *
  372. * \note The signature of the function is that of a PSA driver generate_key
  373. * entry point.
  374. *
  375. * \param[in] attributes The attributes for the key to generate.
  376. * \param[in] custom Custom parameters for the key generation.
  377. * \param[in] custom_data Variable-length data associated with \c custom.
  378. * \param custom_data_length Length of `custom_data` in bytes.
  379. * \param[out] key_buffer Buffer where the key data is to be written.
  380. * \param[in] key_buffer_size Size of \p key_buffer in bytes.
  381. * \param[out] key_buffer_length On success, the number of bytes written in
  382. * \p key_buffer.
  383. *
  384. * \retval #PSA_SUCCESS
  385. * The key was generated successfully.
  386. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  387. * \retval #PSA_ERROR_NOT_SUPPORTED
  388. * Key size in bits or type not supported.
  389. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  390. * The size of \p key_buffer is too small.
  391. */
  392. psa_status_t psa_generate_key_internal(const psa_key_attributes_t *attributes,
  393. const psa_custom_key_parameters_t *custom,
  394. const uint8_t *custom_data,
  395. size_t custom_data_length,
  396. uint8_t *key_buffer,
  397. size_t key_buffer_size,
  398. size_t *key_buffer_length);
  399. /** Sign a message with a private key. For hash-and-sign algorithms,
  400. * this includes the hashing step.
  401. *
  402. * \note The signature of this function is that of a PSA driver
  403. * sign_message entry point. This function behaves as a sign_message
  404. * entry point as defined in the PSA driver interface specification for
  405. * transparent drivers.
  406. *
  407. * \note This function will call the driver for psa_sign_hash
  408. * and go through driver dispatch again.
  409. *
  410. * \param[in] attributes The attributes of the key to use for the
  411. * operation.
  412. * \param[in] key_buffer The buffer containing the key context.
  413. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  414. * \param[in] alg A signature algorithm that is compatible with
  415. * the type of the key.
  416. * \param[in] input The input message to sign.
  417. * \param[in] input_length Size of the \p input buffer in bytes.
  418. * \param[out] signature Buffer where the signature is to be written.
  419. * \param[in] signature_size Size of the \p signature buffer in bytes.
  420. * \param[out] signature_length On success, the number of bytes
  421. * that make up the returned signature value.
  422. *
  423. * \retval #PSA_SUCCESS \emptydescription
  424. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  425. * The size of the \p signature buffer is too small. You can
  426. * determine a sufficient buffer size by calling
  427. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  428. * where \c key_type and \c key_bits are the type and bit-size
  429. * respectively of the key.
  430. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  431. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  432. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  433. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  434. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  435. */
  436. psa_status_t psa_sign_message_builtin(
  437. const psa_key_attributes_t *attributes,
  438. const uint8_t *key_buffer, size_t key_buffer_size,
  439. psa_algorithm_t alg, const uint8_t *input, size_t input_length,
  440. uint8_t *signature, size_t signature_size, size_t *signature_length);
  441. /** Verify the signature of a message with a public key, using
  442. * a hash-and-sign verification algorithm.
  443. *
  444. * \note The signature of this function is that of a PSA driver
  445. * verify_message entry point. This function behaves as a verify_message
  446. * entry point as defined in the PSA driver interface specification for
  447. * transparent drivers.
  448. *
  449. * \note This function will call the driver for psa_verify_hash
  450. * and go through driver dispatch again.
  451. *
  452. * \param[in] attributes The attributes of the key to use for the
  453. * operation.
  454. * \param[in] key_buffer The buffer containing the key context.
  455. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  456. * \param[in] alg A signature algorithm that is compatible with
  457. * the type of the key.
  458. * \param[in] input The message whose signature is to be verified.
  459. * \param[in] input_length Size of the \p input buffer in bytes.
  460. * \param[in] signature Buffer containing the signature to verify.
  461. * \param[in] signature_length Size of the \p signature buffer in bytes.
  462. *
  463. * \retval #PSA_SUCCESS
  464. * The signature is valid.
  465. * \retval #PSA_ERROR_INVALID_SIGNATURE
  466. * The calculation was performed successfully, but the passed
  467. * signature is not a valid signature.
  468. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  469. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  470. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  471. */
  472. psa_status_t psa_verify_message_builtin(
  473. const psa_key_attributes_t *attributes,
  474. const uint8_t *key_buffer, size_t key_buffer_size,
  475. psa_algorithm_t alg, const uint8_t *input, size_t input_length,
  476. const uint8_t *signature, size_t signature_length);
  477. /** Sign an already-calculated hash with a private key.
  478. *
  479. * \note The signature of this function is that of a PSA driver
  480. * sign_hash entry point. This function behaves as a sign_hash
  481. * entry point as defined in the PSA driver interface specification for
  482. * transparent drivers.
  483. *
  484. * \param[in] attributes The attributes of the key to use for the
  485. * operation.
  486. * \param[in] key_buffer The buffer containing the key context.
  487. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  488. * \param[in] alg A signature algorithm that is compatible with
  489. * the type of the key.
  490. * \param[in] hash The hash or message to sign.
  491. * \param[in] hash_length Size of the \p hash buffer in bytes.
  492. * \param[out] signature Buffer where the signature is to be written.
  493. * \param[in] signature_size Size of the \p signature buffer in bytes.
  494. * \param[out] signature_length On success, the number of bytes
  495. * that make up the returned signature value.
  496. *
  497. * \retval #PSA_SUCCESS \emptydescription
  498. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  499. * The size of the \p signature buffer is too small. You can
  500. * determine a sufficient buffer size by calling
  501. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  502. * where \c key_type and \c key_bits are the type and bit-size
  503. * respectively of the key.
  504. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  505. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  506. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  507. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  508. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  509. */
  510. psa_status_t psa_sign_hash_builtin(
  511. const psa_key_attributes_t *attributes,
  512. const uint8_t *key_buffer, size_t key_buffer_size,
  513. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  514. uint8_t *signature, size_t signature_size, size_t *signature_length);
  515. /**
  516. * \brief Verify the signature a hash or short message using a public key.
  517. *
  518. * \note The signature of this function is that of a PSA driver
  519. * verify_hash entry point. This function behaves as a verify_hash
  520. * entry point as defined in the PSA driver interface specification for
  521. * transparent drivers.
  522. *
  523. * \param[in] attributes The attributes of the key to use for the
  524. * operation.
  525. * \param[in] key_buffer The buffer containing the key context.
  526. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  527. * \param[in] alg A signature algorithm that is compatible with
  528. * the type of the key.
  529. * \param[in] hash The hash or message whose signature is to be
  530. * verified.
  531. * \param[in] hash_length Size of the \p hash buffer in bytes.
  532. * \param[in] signature Buffer containing the signature to verify.
  533. * \param[in] signature_length Size of the \p signature buffer in bytes.
  534. *
  535. * \retval #PSA_SUCCESS
  536. * The signature is valid.
  537. * \retval #PSA_ERROR_INVALID_SIGNATURE
  538. * The calculation was performed successfully, but the passed
  539. * signature is not a valid signature.
  540. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  541. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  542. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  543. */
  544. psa_status_t psa_verify_hash_builtin(
  545. const psa_key_attributes_t *attributes,
  546. const uint8_t *key_buffer, size_t key_buffer_size,
  547. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  548. const uint8_t *signature, size_t signature_length);
  549. /**
  550. * \brief Validate the key bit size for unstructured keys.
  551. *
  552. * \note Check that the bit size is acceptable for a given key type for
  553. * unstructured keys.
  554. *
  555. * \param[in] type The key type
  556. * \param[in] bits The number of bits of the key
  557. *
  558. * \retval #PSA_SUCCESS
  559. * The key type and size are valid.
  560. * \retval #PSA_ERROR_INVALID_ARGUMENT
  561. * The size in bits of the key is not valid.
  562. * \retval #PSA_ERROR_NOT_SUPPORTED
  563. * The type and/or the size in bits of the key or the combination of
  564. * the two is not supported.
  565. */
  566. psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
  567. size_t bits);
  568. /** Perform a key agreement and return the raw shared secret, using
  569. built-in raw key agreement functions.
  570. *
  571. * \note The signature of this function is that of a PSA driver
  572. * key_agreement entry point. This function behaves as a key_agreement
  573. * entry point as defined in the PSA driver interface specification for
  574. * transparent drivers.
  575. *
  576. * \param[in] attributes The attributes of the key to use for the
  577. * operation.
  578. * \param[in] key_buffer The buffer containing the private key
  579. * context.
  580. * \param[in] key_buffer_size Size of the \p key_buffer buffer in
  581. * bytes.
  582. * \param[in] alg A key agreement algorithm that is
  583. * compatible with the type of the key.
  584. * \param[in] peer_key The buffer containing the key context
  585. * of the peer's public key.
  586. * \param[in] peer_key_length Size of the \p peer_key buffer in
  587. * bytes.
  588. * \param[out] shared_secret The buffer to which the shared secret
  589. * is to be written.
  590. * \param[in] shared_secret_size Size of the \p shared_secret buffer in
  591. * bytes.
  592. * \param[out] shared_secret_length On success, the number of bytes that make
  593. * up the returned shared secret.
  594. * \retval #PSA_SUCCESS
  595. * Success. Shared secret successfully calculated.
  596. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  597. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  598. * \retval #PSA_ERROR_INVALID_ARGUMENT
  599. * \p alg is not a key agreement algorithm, or
  600. * \p private_key is not compatible with \p alg,
  601. * or \p peer_key is not valid for \p alg or not compatible with
  602. * \p private_key.
  603. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  604. * \p shared_secret_size is too small
  605. * \retval #PSA_ERROR_NOT_SUPPORTED
  606. * \p alg is not a supported key agreement algorithm.
  607. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  608. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  609. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  610. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  611. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  612. * \retval #PSA_ERROR_BAD_STATE \emptydescription
  613. */
  614. psa_status_t psa_key_agreement_raw_builtin(
  615. const psa_key_attributes_t *attributes,
  616. const uint8_t *key_buffer,
  617. size_t key_buffer_size,
  618. psa_algorithm_t alg,
  619. const uint8_t *peer_key,
  620. size_t peer_key_length,
  621. uint8_t *shared_secret,
  622. size_t shared_secret_size,
  623. size_t *shared_secret_length);
  624. /**
  625. * \brief Set the maximum number of ops allowed to be executed by an
  626. * interruptible function in a single call.
  627. *
  628. * \note The signature of this function is that of a PSA driver
  629. * interruptible_set_max_ops entry point. This function behaves as an
  630. * interruptible_set_max_ops entry point as defined in the PSA driver
  631. * interface specification for transparent drivers.
  632. *
  633. * \param[in] max_ops The maximum number of ops to be executed in a
  634. * single call, this can be a number from 0 to
  635. * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
  636. * is obviously the least amount of work done per
  637. * call.
  638. */
  639. void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops);
  640. /**
  641. * \brief Get the maximum number of ops allowed to be executed by an
  642. * interruptible function in a single call.
  643. *
  644. * \note The signature of this function is that of a PSA driver
  645. * interruptible_get_max_ops entry point. This function behaves as an
  646. * interruptible_get_max_ops entry point as defined in the PSA driver
  647. * interface specification for transparent drivers.
  648. *
  649. * \return Maximum number of ops allowed to be executed
  650. * by an interruptible function in a single call.
  651. */
  652. uint32_t mbedtls_psa_interruptible_get_max_ops(void);
  653. /**
  654. * \brief Get the number of ops that a hash signing operation has taken for the
  655. * previous call. If no call or work has taken place, this will return
  656. * zero.
  657. *
  658. * \note The signature of this function is that of a PSA driver
  659. * sign_hash_get_num_ops entry point. This function behaves as an
  660. * sign_hash_get_num_ops entry point as defined in the PSA driver
  661. * interface specification for transparent drivers.
  662. *
  663. * \param operation The \c
  664. * mbedtls_psa_sign_hash_interruptible_operation_t
  665. * to use. This must be initialized first.
  666. *
  667. * \return Number of ops that were completed
  668. * in the last call to \c
  669. * mbedtls_psa_sign_hash_complete().
  670. */
  671. uint32_t mbedtls_psa_sign_hash_get_num_ops(
  672. const mbedtls_psa_sign_hash_interruptible_operation_t *operation);
  673. /**
  674. * \brief Get the number of ops that a hash verification operation has taken for
  675. * the previous call. If no call or work has taken place, this will
  676. * return zero.
  677. *
  678. * \note The signature of this function is that of a PSA driver
  679. * verify_hash_get_num_ops entry point. This function behaves as an
  680. * verify_hash_get_num_ops entry point as defined in the PSA driver
  681. * interface specification for transparent drivers.
  682. *
  683. * \param operation The \c
  684. * mbedtls_psa_verify_hash_interruptible_operation_t
  685. * to use. This must be initialized first.
  686. *
  687. * \return Number of ops that were completed
  688. * in the last call to \c
  689. * mbedtls_psa_verify_hash_complete().
  690. */
  691. uint32_t mbedtls_psa_verify_hash_get_num_ops(
  692. const mbedtls_psa_verify_hash_interruptible_operation_t *operation);
  693. /**
  694. * \brief Start signing a hash or short message with a private key, in an
  695. * interruptible manner.
  696. *
  697. * \note The signature of this function is that of a PSA driver
  698. * sign_hash_start entry point. This function behaves as a
  699. * sign_hash_start entry point as defined in the PSA driver interface
  700. * specification for transparent drivers.
  701. *
  702. * \param[in] operation The \c
  703. * mbedtls_psa_sign_hash_interruptible_operation_t
  704. * to use. This must be initialized first.
  705. * \param[in] attributes The attributes of the key to use for the
  706. * operation.
  707. * \param[in] key_buffer The buffer containing the key context.
  708. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  709. * \param[in] alg A signature algorithm that is compatible with
  710. * the type of the key.
  711. * \param[in] hash The hash or message to sign.
  712. * \param hash_length Size of the \p hash buffer in bytes.
  713. *
  714. * \retval #PSA_SUCCESS
  715. * The operation started successfully - call \c psa_sign_hash_complete()
  716. * with the same context to complete the operation
  717. * \retval #PSA_ERROR_INVALID_ARGUMENT
  718. * An unsupported, incorrectly formatted or incorrect type of key was
  719. * used.
  720. * \retval #PSA_ERROR_NOT_SUPPORTED Either no internal interruptible operations
  721. * are currently supported, or the key type is currently unsupported.
  722. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  723. * There was insufficient memory to load the key representation.
  724. */
  725. psa_status_t mbedtls_psa_sign_hash_start(
  726. mbedtls_psa_sign_hash_interruptible_operation_t *operation,
  727. const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
  728. size_t key_buffer_size, psa_algorithm_t alg,
  729. const uint8_t *hash, size_t hash_length);
  730. /**
  731. * \brief Continue and eventually complete the action of signing a hash or
  732. * short message with a private key, in an interruptible manner.
  733. *
  734. * \note The signature of this function is that of a PSA driver
  735. * sign_hash_complete entry point. This function behaves as a
  736. * sign_hash_complete entry point as defined in the PSA driver interface
  737. * specification for transparent drivers.
  738. *
  739. * \param[in] operation The \c
  740. * mbedtls_psa_sign_hash_interruptible_operation_t
  741. * to use. This must be initialized first.
  742. *
  743. * \param[out] signature Buffer where the signature is to be written.
  744. * \param signature_size Size of the \p signature buffer in bytes. This
  745. * must be appropriate for the selected
  746. * algorithm and key.
  747. * \param[out] signature_length On success, the number of bytes that make up
  748. * the returned signature value.
  749. *
  750. * \retval #PSA_SUCCESS
  751. * Operation completed successfully
  752. *
  753. * \retval #PSA_OPERATION_INCOMPLETE
  754. * Operation was interrupted due to the setting of \c
  755. * psa_interruptible_set_max_ops(), there is still work to be done,
  756. * please call this function again with the same operation object.
  757. *
  758. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  759. * The size of the \p signature buffer is too small. You can
  760. * determine a sufficient buffer size by calling
  761. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  762. * where \c key_type and \c key_bits are the type and bit-size
  763. * respectively of \p key.
  764. *
  765. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  766. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  767. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  768. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  769. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  770. */
  771. psa_status_t mbedtls_psa_sign_hash_complete(
  772. mbedtls_psa_sign_hash_interruptible_operation_t *operation,
  773. uint8_t *signature, size_t signature_size,
  774. size_t *signature_length);
  775. /**
  776. * \brief Abort a sign hash operation.
  777. *
  778. * \note The signature of this function is that of a PSA driver sign_hash_abort
  779. * entry point. This function behaves as a sign_hash_abort entry point as
  780. * defined in the PSA driver interface specification for transparent
  781. * drivers.
  782. *
  783. * \param[in] operation The \c
  784. * mbedtls_psa_sign_hash_interruptible_operation_t
  785. * to abort.
  786. *
  787. * \retval #PSA_SUCCESS
  788. * The operation was aborted successfully.
  789. */
  790. psa_status_t mbedtls_psa_sign_hash_abort(
  791. mbedtls_psa_sign_hash_interruptible_operation_t *operation);
  792. /**
  793. * \brief Start reading and verifying a hash or short message, in an
  794. * interruptible manner.
  795. *
  796. * \note The signature of this function is that of a PSA driver
  797. * verify_hash_start entry point. This function behaves as a
  798. * verify_hash_start entry point as defined in the PSA driver interface
  799. * specification for transparent drivers.
  800. *
  801. * \param[in] operation The \c
  802. * mbedtls_psa_verify_hash_interruptible_operation_t
  803. * to use. This must be initialized first.
  804. * \param[in] attributes The attributes of the key to use for the
  805. * operation.
  806. * \param[in] key_buffer The buffer containing the key context.
  807. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  808. * \param[in] alg A signature algorithm that is compatible with
  809. * the type of the key.
  810. * \param[in] hash The hash whose signature is to be verified.
  811. * \param hash_length Size of the \p hash buffer in bytes.
  812. * \param[in] signature Buffer containing the signature to verify.
  813. * \param signature_length Size of the \p signature buffer in bytes.
  814. *
  815. * \retval #PSA_SUCCESS
  816. * The operation started successfully - call \c psa_sign_hash_complete()
  817. * with the same context to complete the operation
  818. * \retval #PSA_ERROR_INVALID_ARGUMENT
  819. * An unsupported or incorrect type of key was used.
  820. * \retval #PSA_ERROR_NOT_SUPPORTED
  821. * Either no internal interruptible operations are currently supported,
  822. * or the key type is currently unsupported.
  823. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  824. * There was insufficient memory either to load the key representation,
  825. * or to prepare the operation.
  826. */
  827. psa_status_t mbedtls_psa_verify_hash_start(
  828. mbedtls_psa_verify_hash_interruptible_operation_t *operation,
  829. const psa_key_attributes_t *attributes,
  830. const uint8_t *key_buffer, size_t key_buffer_size,
  831. psa_algorithm_t alg,
  832. const uint8_t *hash, size_t hash_length,
  833. const uint8_t *signature, size_t signature_length);
  834. /**
  835. * \brief Continue and eventually complete the action of signing a hash or
  836. * short message with a private key, in an interruptible manner.
  837. *
  838. * \note The signature of this function is that of a PSA driver
  839. * sign_hash_complete entry point. This function behaves as a
  840. * sign_hash_complete entry point as defined in the PSA driver interface
  841. * specification for transparent drivers.
  842. *
  843. * \param[in] operation The \c
  844. * mbedtls_psa_sign_hash_interruptible_operation_t
  845. * to use. This must be initialized first.
  846. *
  847. * \retval #PSA_SUCCESS
  848. * Operation completed successfully, and the passed signature is valid.
  849. *
  850. * \retval #PSA_OPERATION_INCOMPLETE
  851. * Operation was interrupted due to the setting of \c
  852. * psa_interruptible_set_max_ops(), there is still work to be done,
  853. * please call this function again with the same operation object.
  854. *
  855. * \retval #PSA_ERROR_INVALID_SIGNATURE
  856. * The calculation was performed successfully, but the passed
  857. * signature is not a valid signature.
  858. *
  859. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  860. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  861. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  862. */
  863. psa_status_t mbedtls_psa_verify_hash_complete(
  864. mbedtls_psa_verify_hash_interruptible_operation_t *operation);
  865. /**
  866. * \brief Abort a verify signed hash operation.
  867. *
  868. * \note The signature of this function is that of a PSA driver
  869. * verify_hash_abort entry point. This function behaves as a
  870. * verify_hash_abort entry point as defined in the PSA driver interface
  871. * specification for transparent drivers.
  872. *
  873. * \param[in] operation The \c
  874. * mbedtls_psa_verify_hash_interruptible_operation_t
  875. * to abort.
  876. *
  877. * \retval #PSA_SUCCESS
  878. * The operation was aborted successfully.
  879. */
  880. psa_status_t mbedtls_psa_verify_hash_abort(
  881. mbedtls_psa_verify_hash_interruptible_operation_t *operation);
  882. typedef struct psa_crypto_local_input_s {
  883. uint8_t *buffer;
  884. size_t length;
  885. } psa_crypto_local_input_t;
  886. #define PSA_CRYPTO_LOCAL_INPUT_INIT ((psa_crypto_local_input_t) { NULL, 0 })
  887. /** Allocate a local copy of an input buffer and copy the contents into it.
  888. *
  889. * \param[in] input Pointer to input buffer.
  890. * \param[in] input_len Length of the input buffer.
  891. * \param[out] local_input Pointer to a psa_crypto_local_input_t struct
  892. * containing a local input copy.
  893. * \return #PSA_SUCCESS, if the buffer was successfully
  894. * copied.
  895. * \return #PSA_ERROR_INSUFFICIENT_MEMORY, if a copy of
  896. * the buffer cannot be allocated.
  897. */
  898. psa_status_t psa_crypto_local_input_alloc(const uint8_t *input, size_t input_len,
  899. psa_crypto_local_input_t *local_input);
  900. /** Free a local copy of an input buffer.
  901. *
  902. * \param[in] local_input Pointer to a psa_crypto_local_input_t struct
  903. * populated by a previous call to
  904. * psa_crypto_local_input_alloc().
  905. */
  906. void psa_crypto_local_input_free(psa_crypto_local_input_t *local_input);
  907. typedef struct psa_crypto_local_output_s {
  908. uint8_t *original;
  909. uint8_t *buffer;
  910. size_t length;
  911. } psa_crypto_local_output_t;
  912. #define PSA_CRYPTO_LOCAL_OUTPUT_INIT ((psa_crypto_local_output_t) { NULL, NULL, 0 })
  913. /** Allocate a local copy of an output buffer.
  914. *
  915. * \note This does not copy any data from the original
  916. * output buffer but only allocates a buffer
  917. * whose contents will be copied back to the
  918. * original in a future call to
  919. * psa_crypto_local_output_free().
  920. *
  921. * \param[in] output Pointer to output buffer.
  922. * \param[in] output_len Length of the output buffer.
  923. * \param[out] local_output Pointer to a psa_crypto_local_output_t struct to
  924. * populate with the local output copy.
  925. * \return #PSA_SUCCESS, if the buffer was successfully
  926. * copied.
  927. * \return #PSA_ERROR_INSUFFICIENT_MEMORY, if a copy of
  928. * the buffer cannot be allocated.
  929. */
  930. psa_status_t psa_crypto_local_output_alloc(uint8_t *output, size_t output_len,
  931. psa_crypto_local_output_t *local_output);
  932. /** Copy from a local copy of an output buffer back to the original, then
  933. * free the local copy.
  934. *
  935. * \param[in] local_output Pointer to a psa_crypto_local_output_t struct
  936. * populated by a previous call to
  937. * psa_crypto_local_output_alloc().
  938. * \return #PSA_SUCCESS, if the local output was
  939. * successfully copied back to the original.
  940. * \return #PSA_ERROR_CORRUPTION_DETECTED, if the output
  941. * could not be copied back to the original.
  942. */
  943. psa_status_t psa_crypto_local_output_free(psa_crypto_local_output_t *local_output);
  944. #endif /* PSA_CRYPTO_CORE_H */