psa_crypto_core.h 44 KB

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