psa_crypto_core.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * PSA crypto core internal interfaces
  3. */
  4. /*
  5. * Copyright The Mbed TLS Contributors
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef PSA_CRYPTO_CORE_H
  21. #define PSA_CRYPTO_CORE_H
  22. #include "mbedtls/build_info.h"
  23. #include "psa/crypto.h"
  24. #include "psa/crypto_se_driver.h"
  25. /**
  26. * Tell if PSA is ready for this hash.
  27. *
  28. * \note For now, only checks the state of the driver subsystem,
  29. * not the algorithm. Might do more in the future.
  30. *
  31. * \param hash_alg The hash algorithm (ignored for now).
  32. *
  33. * \return 1 if the driver subsytem is ready, 0 otherwise.
  34. */
  35. int psa_can_do_hash(psa_algorithm_t hash_alg);
  36. /** Constant-time buffer comparison
  37. *
  38. * \param[in] a Left-hand buffer for comparison.
  39. * \param[in] b Right-hand buffer for comparison.
  40. * \param n Amount of bytes to compare.
  41. *
  42. * \return 0 if the buffer contents are equal, non-zero otherwise
  43. */
  44. static inline int mbedtls_psa_safer_memcmp(
  45. const uint8_t *a, const uint8_t *b, size_t n)
  46. {
  47. size_t i;
  48. unsigned char diff = 0;
  49. for (i = 0; i < n; i++) {
  50. diff |= a[i] ^ b[i];
  51. }
  52. return diff;
  53. }
  54. /** The data structure representing a key slot, containing key material
  55. * and metadata for one key.
  56. */
  57. typedef struct {
  58. psa_core_key_attributes_t attr;
  59. /*
  60. * Number of locks on the key slot held by the library.
  61. *
  62. * This counter is incremented by one each time a library function
  63. * retrieves through one of the dedicated internal API a pointer to the
  64. * key slot.
  65. *
  66. * This counter is decremented by one each time a library function stops
  67. * accessing the key slot and states it by calling the
  68. * psa_unlock_key_slot() API.
  69. *
  70. * This counter is used to prevent resetting the key slot while the library
  71. * may access it. For example, such control is needed in the following
  72. * scenarios:
  73. * . In case of key slot starvation, all key slots contain the description
  74. * of a key, and the library asks for the description of a persistent
  75. * key not present in the key slots, the key slots currently accessed by
  76. * the library cannot be reclaimed to free a key slot to load the
  77. * persistent key.
  78. * . In case of a multi-threaded application where one thread asks to close
  79. * or purge or destroy a key while it is in used by the library through
  80. * another thread.
  81. */
  82. size_t lock_count;
  83. /* Dynamically allocated key data buffer.
  84. * Format as specified in psa_export_key(). */
  85. struct key_data {
  86. uint8_t *data;
  87. size_t bytes;
  88. } key;
  89. } psa_key_slot_t;
  90. /* A mask of key attribute flags used only internally.
  91. * Currently there aren't any. */
  92. #define PSA_KA_MASK_INTERNAL_ONLY ( \
  93. 0)
  94. /** Test whether a key slot is occupied.
  95. *
  96. * A key slot is occupied iff the key type is nonzero. This works because
  97. * no valid key can have 0 as its key type.
  98. *
  99. * \param[in] slot The key slot to test.
  100. *
  101. * \return 1 if the slot is occupied, 0 otherwise.
  102. */
  103. static inline int psa_is_key_slot_occupied(const psa_key_slot_t *slot)
  104. {
  105. return slot->attr.type != 0;
  106. }
  107. /** Test whether a key slot is locked.
  108. *
  109. * A key slot is locked iff its lock counter is strictly greater than 0.
  110. *
  111. * \param[in] slot The key slot to test.
  112. *
  113. * \return 1 if the slot is locked, 0 otherwise.
  114. */
  115. static inline int psa_is_key_slot_locked(const psa_key_slot_t *slot)
  116. {
  117. return slot->lock_count > 0;
  118. }
  119. /** Retrieve flags from psa_key_slot_t::attr::core::flags.
  120. *
  121. * \param[in] slot The key slot to query.
  122. * \param mask The mask of bits to extract.
  123. *
  124. * \return The key attribute flags in the given slot,
  125. * bitwise-anded with \p mask.
  126. */
  127. static inline uint16_t psa_key_slot_get_flags(const psa_key_slot_t *slot,
  128. uint16_t mask)
  129. {
  130. return slot->attr.flags & mask;
  131. }
  132. /** Set flags in psa_key_slot_t::attr::core::flags.
  133. *
  134. * \param[in,out] slot The key slot to modify.
  135. * \param mask The mask of bits to modify.
  136. * \param value The new value of the selected bits.
  137. */
  138. static inline void psa_key_slot_set_flags(psa_key_slot_t *slot,
  139. uint16_t mask,
  140. uint16_t value)
  141. {
  142. slot->attr.flags = ((~mask & slot->attr.flags) |
  143. (mask & value));
  144. }
  145. /** Turn on flags in psa_key_slot_t::attr::core::flags.
  146. *
  147. * \param[in,out] slot The key slot to modify.
  148. * \param mask The mask of bits to set.
  149. */
  150. static inline void psa_key_slot_set_bits_in_flags(psa_key_slot_t *slot,
  151. uint16_t mask)
  152. {
  153. slot->attr.flags |= mask;
  154. }
  155. /** Turn off flags in psa_key_slot_t::attr::core::flags.
  156. *
  157. * \param[in,out] slot The key slot to modify.
  158. * \param mask The mask of bits to clear.
  159. */
  160. static inline void psa_key_slot_clear_bits(psa_key_slot_t *slot,
  161. uint16_t mask)
  162. {
  163. slot->attr.flags &= ~mask;
  164. }
  165. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  166. /** Get the SE slot number of a key from the key slot storing its description.
  167. *
  168. * \param[in] slot The key slot to query. This must be a key slot storing
  169. * the description of a key of a dynamically registered
  170. * secure element, otherwise the behaviour is undefined.
  171. */
  172. static inline psa_key_slot_number_t psa_key_slot_get_slot_number(
  173. const psa_key_slot_t *slot)
  174. {
  175. return *((psa_key_slot_number_t *) (slot->key.data));
  176. }
  177. #endif
  178. /** Completely wipe a slot in memory, including its policy.
  179. *
  180. * Persistent storage is not affected.
  181. *
  182. * \param[in,out] slot The key slot to wipe.
  183. *
  184. * \retval #PSA_SUCCESS
  185. * Success. This includes the case of a key slot that was
  186. * already fully wiped.
  187. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  188. */
  189. psa_status_t psa_wipe_key_slot(psa_key_slot_t *slot);
  190. /** Try to allocate a buffer to an empty key slot.
  191. *
  192. * \param[in,out] slot Key slot to attach buffer to.
  193. * \param[in] buffer_length Requested size of the buffer.
  194. *
  195. * \retval #PSA_SUCCESS
  196. * The buffer has been successfully allocated.
  197. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  198. * Not enough memory was available for allocation.
  199. * \retval #PSA_ERROR_ALREADY_EXISTS
  200. * Trying to allocate a buffer to a non-empty key slot.
  201. */
  202. psa_status_t psa_allocate_buffer_to_slot(psa_key_slot_t *slot,
  203. size_t buffer_length);
  204. /** Wipe key data from a slot. Preserves metadata such as the policy. */
  205. psa_status_t psa_remove_key_data_from_memory(psa_key_slot_t *slot);
  206. /** Copy key data (in export format) into an empty key slot.
  207. *
  208. * This function assumes that the slot does not contain
  209. * any key material yet. On failure, the slot content is unchanged.
  210. *
  211. * \param[in,out] slot Key slot to copy the key into.
  212. * \param[in] data Buffer containing the key material.
  213. * \param data_length Size of the key buffer.
  214. *
  215. * \retval #PSA_SUCCESS
  216. * The key has been copied successfully.
  217. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  218. * Not enough memory was available for allocation of the
  219. * copy buffer.
  220. * \retval #PSA_ERROR_ALREADY_EXISTS
  221. * There was other key material already present in the slot.
  222. */
  223. psa_status_t psa_copy_key_material_into_slot(psa_key_slot_t *slot,
  224. const uint8_t *data,
  225. size_t data_length);
  226. /** Convert an mbed TLS error code to a PSA error code
  227. *
  228. * \note This function is provided solely for the convenience of
  229. * Mbed TLS and may be removed at any time without notice.
  230. *
  231. * \param ret An mbed TLS-thrown error code
  232. *
  233. * \return The corresponding PSA error code
  234. */
  235. psa_status_t mbedtls_to_psa_error(int ret);
  236. /** Import a key in binary format.
  237. *
  238. * \note The signature of this function is that of a PSA driver
  239. * import_key entry point. This function behaves as an import_key
  240. * entry point as defined in the PSA driver interface specification for
  241. * transparent drivers.
  242. *
  243. * \param[in] attributes The attributes for the key to import.
  244. * \param[in] data The buffer containing the key data in import
  245. * format.
  246. * \param[in] data_length Size of the \p data buffer in bytes.
  247. * \param[out] key_buffer The buffer to contain the key data in output
  248. * format upon successful return.
  249. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
  250. * size is greater or equal to \p data_length.
  251. * \param[out] key_buffer_length The length of the data written in \p
  252. * key_buffer in bytes.
  253. * \param[out] bits The key size in number of bits.
  254. *
  255. * \retval #PSA_SUCCESS The key was imported successfully.
  256. * \retval #PSA_ERROR_INVALID_ARGUMENT
  257. * The key data is not correctly formatted.
  258. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  259. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  260. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  261. */
  262. psa_status_t psa_import_key_into_slot(
  263. const psa_key_attributes_t *attributes,
  264. const uint8_t *data, size_t data_length,
  265. uint8_t *key_buffer, size_t key_buffer_size,
  266. size_t *key_buffer_length, size_t *bits);
  267. /** Export a key in binary format
  268. *
  269. * \note The signature of this function is that of a PSA driver export_key
  270. * entry point. This function behaves as an export_key entry point as
  271. * defined in the PSA driver interface specification.
  272. *
  273. * \param[in] attributes The attributes for the key to export.
  274. * \param[in] key_buffer Material or context of the key to export.
  275. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  276. * \param[out] data Buffer where the key data is to be written.
  277. * \param[in] data_size Size of the \p data buffer in bytes.
  278. * \param[out] data_length On success, the number of bytes written in
  279. * \p data
  280. *
  281. * \retval #PSA_SUCCESS The key was exported successfully.
  282. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  283. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  284. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  285. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  286. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  287. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  288. */
  289. psa_status_t psa_export_key_internal(
  290. const psa_key_attributes_t *attributes,
  291. const uint8_t *key_buffer, size_t key_buffer_size,
  292. uint8_t *data, size_t data_size, size_t *data_length);
  293. /** Export a public key or the public part of a key pair in binary format.
  294. *
  295. * \note The signature of this function is that of a PSA driver
  296. * export_public_key entry point. This function behaves as an
  297. * export_public_key entry point as defined in the PSA driver interface
  298. * specification.
  299. *
  300. * \param[in] attributes The attributes for the key to export.
  301. * \param[in] key_buffer Material or context of the key to export.
  302. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  303. * \param[out] data Buffer where the key data is to be written.
  304. * \param[in] data_size Size of the \p data buffer in bytes.
  305. * \param[out] data_length On success, the number of bytes written in
  306. * \p data
  307. *
  308. * \retval #PSA_SUCCESS The public key was exported successfully.
  309. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  310. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  311. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  312. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  313. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  314. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  315. */
  316. psa_status_t psa_export_public_key_internal(
  317. const psa_key_attributes_t *attributes,
  318. const uint8_t *key_buffer, size_t key_buffer_size,
  319. uint8_t *data, size_t data_size, size_t *data_length);
  320. /**
  321. * \brief Generate a key.
  322. *
  323. * \note The signature of the function is that of a PSA driver generate_key
  324. * entry point.
  325. *
  326. * \param[in] attributes The attributes for the key to generate.
  327. * \param[out] key_buffer Buffer where the key data is to be written.
  328. * \param[in] key_buffer_size Size of \p key_buffer in bytes.
  329. * \param[out] key_buffer_length On success, the number of bytes written in
  330. * \p key_buffer.
  331. *
  332. * \retval #PSA_SUCCESS
  333. * The key was generated successfully.
  334. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  335. * \retval #PSA_ERROR_NOT_SUPPORTED
  336. * Key size in bits or type not supported.
  337. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  338. * The size of \p key_buffer is too small.
  339. */
  340. psa_status_t psa_generate_key_internal(const psa_key_attributes_t *attributes,
  341. uint8_t *key_buffer,
  342. size_t key_buffer_size,
  343. size_t *key_buffer_length);
  344. /** Sign a message with a private key. For hash-and-sign algorithms,
  345. * this includes the hashing step.
  346. *
  347. * \note The signature of this function is that of a PSA driver
  348. * sign_message entry point. This function behaves as a sign_message
  349. * entry point as defined in the PSA driver interface specification for
  350. * transparent drivers.
  351. *
  352. * \note This function will call the driver for psa_sign_hash
  353. * and go through driver dispatch again.
  354. *
  355. * \param[in] attributes The attributes of the key to use for the
  356. * operation.
  357. * \param[in] key_buffer The buffer containing the key context.
  358. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  359. * \param[in] alg A signature algorithm that is compatible with
  360. * the type of the key.
  361. * \param[in] input The input message to sign.
  362. * \param[in] input_length Size of the \p input buffer in bytes.
  363. * \param[out] signature Buffer where the signature is to be written.
  364. * \param[in] signature_size Size of the \p signature buffer in bytes.
  365. * \param[out] signature_length On success, the number of bytes
  366. * that make up the returned signature value.
  367. *
  368. * \retval #PSA_SUCCESS \emptydescription
  369. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  370. * The size of the \p signature buffer is too small. You can
  371. * determine a sufficient buffer size by calling
  372. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  373. * where \c key_type and \c key_bits are the type and bit-size
  374. * respectively of the key.
  375. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  376. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  377. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  378. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  379. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  380. */
  381. psa_status_t psa_sign_message_builtin(
  382. const psa_key_attributes_t *attributes,
  383. const uint8_t *key_buffer, size_t key_buffer_size,
  384. psa_algorithm_t alg, const uint8_t *input, size_t input_length,
  385. uint8_t *signature, size_t signature_size, size_t *signature_length);
  386. /** Verify the signature of a message with a public key, using
  387. * a hash-and-sign verification algorithm.
  388. *
  389. * \note The signature of this function is that of a PSA driver
  390. * verify_message entry point. This function behaves as a verify_message
  391. * entry point as defined in the PSA driver interface specification for
  392. * transparent drivers.
  393. *
  394. * \note This function will call the driver for psa_verify_hash
  395. * and go through driver dispatch again.
  396. *
  397. * \param[in] attributes The attributes of the key to use for the
  398. * operation.
  399. * \param[in] key_buffer The buffer containing the key context.
  400. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  401. * \param[in] alg A signature algorithm that is compatible with
  402. * the type of the key.
  403. * \param[in] input The message whose signature is to be verified.
  404. * \param[in] input_length Size of the \p input buffer in bytes.
  405. * \param[in] signature Buffer containing the signature to verify.
  406. * \param[in] signature_length Size of the \p signature buffer in bytes.
  407. *
  408. * \retval #PSA_SUCCESS
  409. * The signature is valid.
  410. * \retval #PSA_ERROR_INVALID_SIGNATURE
  411. * The calculation was performed successfully, but the passed
  412. * signature is not a valid signature.
  413. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  414. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  415. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  416. */
  417. psa_status_t psa_verify_message_builtin(
  418. const psa_key_attributes_t *attributes,
  419. const uint8_t *key_buffer, size_t key_buffer_size,
  420. psa_algorithm_t alg, const uint8_t *input, size_t input_length,
  421. const uint8_t *signature, size_t signature_length);
  422. /** Sign an already-calculated hash with a private key.
  423. *
  424. * \note The signature of this function is that of a PSA driver
  425. * sign_hash entry point. This function behaves as a sign_hash
  426. * entry point as defined in the PSA driver interface specification for
  427. * transparent drivers.
  428. *
  429. * \param[in] attributes The attributes of the key to use for the
  430. * operation.
  431. * \param[in] key_buffer The buffer containing the key context.
  432. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  433. * \param[in] alg A signature algorithm that is compatible with
  434. * the type of the key.
  435. * \param[in] hash The hash or message to sign.
  436. * \param[in] hash_length Size of the \p hash buffer in bytes.
  437. * \param[out] signature Buffer where the signature is to be written.
  438. * \param[in] signature_size Size of the \p signature buffer in bytes.
  439. * \param[out] signature_length On success, the number of bytes
  440. * that make up the returned signature value.
  441. *
  442. * \retval #PSA_SUCCESS \emptydescription
  443. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  444. * The size of the \p signature buffer is too small. You can
  445. * determine a sufficient buffer size by calling
  446. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  447. * where \c key_type and \c key_bits are the type and bit-size
  448. * respectively of the key.
  449. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  450. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  451. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  452. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  453. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  454. */
  455. psa_status_t psa_sign_hash_builtin(
  456. const psa_key_attributes_t *attributes,
  457. const uint8_t *key_buffer, size_t key_buffer_size,
  458. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  459. uint8_t *signature, size_t signature_size, size_t *signature_length);
  460. /**
  461. * \brief Verify the signature a hash or short message using a public key.
  462. *
  463. * \note The signature of this function is that of a PSA driver
  464. * verify_hash entry point. This function behaves as a verify_hash
  465. * entry point as defined in the PSA driver interface specification for
  466. * transparent drivers.
  467. *
  468. * \param[in] attributes The attributes of the key to use for the
  469. * operation.
  470. * \param[in] key_buffer The buffer containing the key context.
  471. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  472. * \param[in] alg A signature algorithm that is compatible with
  473. * the type of the key.
  474. * \param[in] hash The hash or message whose signature is to be
  475. * verified.
  476. * \param[in] hash_length Size of the \p hash buffer in bytes.
  477. * \param[in] signature Buffer containing the signature to verify.
  478. * \param[in] signature_length Size of the \p signature buffer in bytes.
  479. *
  480. * \retval #PSA_SUCCESS
  481. * The signature is valid.
  482. * \retval #PSA_ERROR_INVALID_SIGNATURE
  483. * The calculation was performed successfully, but the passed
  484. * signature is not a valid signature.
  485. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  486. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  487. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  488. */
  489. psa_status_t psa_verify_hash_builtin(
  490. const psa_key_attributes_t *attributes,
  491. const uint8_t *key_buffer, size_t key_buffer_size,
  492. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  493. const uint8_t *signature, size_t signature_length);
  494. /**
  495. * \brief Validate the key bit size for unstructured keys.
  496. *
  497. * \note Check that the bit size is acceptable for a given key type for
  498. * unstructured keys.
  499. *
  500. * \param[in] type The key type
  501. * \param[in] bits The number of bits of the key
  502. *
  503. * \retval #PSA_SUCCESS
  504. * The key type and size are valid.
  505. * \retval #PSA_ERROR_INVALID_ARGUMENT
  506. * The size in bits of the key is not valid.
  507. * \retval #PSA_ERROR_NOT_SUPPORTED
  508. * The type and/or the size in bits of the key or the combination of
  509. * the two is not supported.
  510. */
  511. psa_status_t psa_validate_unstructured_key_bit_size(psa_key_type_t type,
  512. size_t bits);
  513. /** Perform a key agreement and return the raw shared secret, using
  514. built-in raw key agreement functions.
  515. *
  516. * \note The signature of this function is that of a PSA driver
  517. * key_agreement entry point. This function behaves as a key_agreement
  518. * entry point as defined in the PSA driver interface specification for
  519. * transparent drivers.
  520. *
  521. * \param[in] attributes The attributes of the key to use for the
  522. * operation.
  523. * \param[in] key_buffer The buffer containing the private key
  524. * context.
  525. * \param[in] key_buffer_size Size of the \p key_buffer buffer in
  526. * bytes.
  527. * \param[in] alg A key agreement algorithm that is
  528. * compatible with the type of the key.
  529. * \param[in] peer_key The buffer containing the key context
  530. * of the peer's public key.
  531. * \param[in] peer_key_length Size of the \p peer_key buffer in
  532. * bytes.
  533. * \param[out] shared_secret The buffer to which the shared secret
  534. * is to be written.
  535. * \param[in] shared_secret_size Size of the \p shared_secret buffer in
  536. * bytes.
  537. * \param[out] shared_secret_length On success, the number of bytes that make
  538. * up the returned shared secret.
  539. * \retval #PSA_SUCCESS
  540. * Success. Shared secret successfully calculated.
  541. * \retval #PSA_ERROR_INVALID_HANDLE \emptydescription
  542. * \retval #PSA_ERROR_NOT_PERMITTED \emptydescription
  543. * \retval #PSA_ERROR_INVALID_ARGUMENT
  544. * \p alg is not a key agreement algorithm, or
  545. * \p private_key is not compatible with \p alg,
  546. * or \p peer_key is not valid for \p alg or not compatible with
  547. * \p private_key.
  548. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  549. * \p shared_secret_size is too small
  550. * \retval #PSA_ERROR_NOT_SUPPORTED
  551. * \p alg is not a supported key agreement algorithm.
  552. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  553. * \retval #PSA_ERROR_COMMUNICATION_FAILURE \emptydescription
  554. * \retval #PSA_ERROR_HARDWARE_FAILURE \emptydescription
  555. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  556. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  557. * \retval #PSA_ERROR_BAD_STATE \emptydescription
  558. */
  559. psa_status_t psa_key_agreement_raw_builtin(
  560. const psa_key_attributes_t *attributes,
  561. const uint8_t *key_buffer,
  562. size_t key_buffer_size,
  563. psa_algorithm_t alg,
  564. const uint8_t *peer_key,
  565. size_t peer_key_length,
  566. uint8_t *shared_secret,
  567. size_t shared_secret_size,
  568. size_t *shared_secret_length);
  569. /**
  570. * \brief Set the maximum number of ops allowed to be executed by an
  571. * interruptible function in a single call.
  572. *
  573. * \note The signature of this function is that of a PSA driver
  574. * interruptible_set_max_ops entry point. This function behaves as an
  575. * interruptible_set_max_ops entry point as defined in the PSA driver
  576. * interface specification for transparent drivers.
  577. *
  578. * \param[in] max_ops The maximum number of ops to be executed in a
  579. * single call, this can be a number from 0 to
  580. * #PSA_INTERRUPTIBLE_MAX_OPS_UNLIMITED, where 0
  581. * is obviously the least amount of work done per
  582. * call.
  583. */
  584. void mbedtls_psa_interruptible_set_max_ops(uint32_t max_ops);
  585. /**
  586. * \brief Get the maximum number of ops allowed to be executed by an
  587. * interruptible function in a single call.
  588. *
  589. * \note The signature of this function is that of a PSA driver
  590. * interruptible_get_max_ops entry point. This function behaves as an
  591. * interruptible_get_max_ops entry point as defined in the PSA driver
  592. * interface specification for transparent drivers.
  593. *
  594. * \return Maximum number of ops allowed to be executed
  595. * by an interruptible function in a single call.
  596. */
  597. uint32_t mbedtls_psa_interruptible_get_max_ops(void);
  598. /**
  599. * \brief Get the number of ops that a hash signing operation has taken for the
  600. * previous call. If no call or work has taken place, this will return
  601. * zero.
  602. *
  603. * \note The signature of this function is that of a PSA driver
  604. * sign_hash_get_num_ops entry point. This function behaves as an
  605. * sign_hash_get_num_ops entry point as defined in the PSA driver
  606. * interface specification for transparent drivers.
  607. *
  608. * \param operation The \c
  609. * mbedtls_psa_sign_hash_interruptible_operation_t
  610. * to use. This must be initialized first.
  611. *
  612. * \return Number of ops that were completed
  613. * in the last call to \c
  614. * mbedtls_psa_sign_hash_complete().
  615. */
  616. uint32_t mbedtls_psa_sign_hash_get_num_ops(
  617. const mbedtls_psa_sign_hash_interruptible_operation_t *operation);
  618. /**
  619. * \brief Get the number of ops that a hash verification operation has taken for
  620. * the previous call. If no call or work has taken place, this will
  621. * return zero.
  622. *
  623. * \note The signature of this function is that of a PSA driver
  624. * verify_hash_get_num_ops entry point. This function behaves as an
  625. * verify_hash_get_num_ops entry point as defined in the PSA driver
  626. * interface specification for transparent drivers.
  627. *
  628. * \param operation The \c
  629. * mbedtls_psa_verify_hash_interruptible_operation_t
  630. * to use. This must be initialized first.
  631. *
  632. * \return Number of ops that were completed
  633. * in the last call to \c
  634. * mbedtls_psa_verify_hash_complete().
  635. */
  636. uint32_t mbedtls_psa_verify_hash_get_num_ops(
  637. const mbedtls_psa_verify_hash_interruptible_operation_t *operation);
  638. /**
  639. * \brief Start signing a hash or short message with a private key, in an
  640. * interruptible manner.
  641. *
  642. * \note The signature of this function is that of a PSA driver
  643. * sign_hash_start entry point. This function behaves as a
  644. * sign_hash_start entry point as defined in the PSA driver interface
  645. * specification for transparent drivers.
  646. *
  647. * \param[in] operation The \c
  648. * mbedtls_psa_sign_hash_interruptible_operation_t
  649. * to use. This must be initialized first.
  650. * \param[in] attributes The attributes of the key to use for the
  651. * operation.
  652. * \param[in] key_buffer The buffer containing the key context.
  653. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  654. * \param[in] alg A signature algorithm that is compatible with
  655. * the type of the key.
  656. * \param[in] hash The hash or message to sign.
  657. * \param hash_length Size of the \p hash buffer in bytes.
  658. *
  659. * \retval #PSA_SUCCESS
  660. * The operation started successfully - call \c psa_sign_hash_complete()
  661. * with the same context to complete the operation
  662. * \retval #PSA_ERROR_INVALID_ARGUMENT
  663. * An unsupported, incorrectly formatted or incorrect type of key was
  664. * used.
  665. * \retval #PSA_ERROR_NOT_SUPPORTED Either no internal interruptible operations
  666. * are currently supported, or the key type is currently unsupported.
  667. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  668. * There was insufficient memory to load the key representation.
  669. */
  670. psa_status_t mbedtls_psa_sign_hash_start(
  671. mbedtls_psa_sign_hash_interruptible_operation_t *operation,
  672. const psa_key_attributes_t *attributes, const uint8_t *key_buffer,
  673. size_t key_buffer_size, psa_algorithm_t alg,
  674. const uint8_t *hash, size_t hash_length);
  675. /**
  676. * \brief Continue and eventually complete the action of signing a hash or
  677. * short message with a private key, in an interruptible manner.
  678. *
  679. * \note The signature of this function is that of a PSA driver
  680. * sign_hash_complete entry point. This function behaves as a
  681. * sign_hash_complete entry point as defined in the PSA driver interface
  682. * specification for transparent drivers.
  683. *
  684. * \param[in] operation The \c
  685. * mbedtls_psa_sign_hash_interruptible_operation_t
  686. * to use. This must be initialized first.
  687. *
  688. * \param[out] signature Buffer where the signature is to be written.
  689. * \param signature_size Size of the \p signature buffer in bytes. This
  690. * must be appropriate for the selected
  691. * algorithm and key.
  692. * \param[out] signature_length On success, the number of bytes that make up
  693. * the returned signature value.
  694. *
  695. * \retval #PSA_SUCCESS
  696. * Operation completed successfully
  697. *
  698. * \retval #PSA_OPERATION_INCOMPLETE
  699. * Operation was interrupted due to the setting of \c
  700. * psa_interruptible_set_max_ops(), there is still work to be done,
  701. * please call this function again with the same operation object.
  702. *
  703. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  704. * The size of the \p signature buffer is too small. You can
  705. * determine a sufficient buffer size by calling
  706. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  707. * where \c key_type and \c key_bits are the type and bit-size
  708. * respectively of \p key.
  709. *
  710. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  711. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  712. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  713. * \retval #PSA_ERROR_CORRUPTION_DETECTED \emptydescription
  714. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY \emptydescription
  715. */
  716. psa_status_t mbedtls_psa_sign_hash_complete(
  717. mbedtls_psa_sign_hash_interruptible_operation_t *operation,
  718. uint8_t *signature, size_t signature_size,
  719. size_t *signature_length);
  720. /**
  721. * \brief Abort a sign hash operation.
  722. *
  723. * \note The signature of this function is that of a PSA driver sign_hash_abort
  724. * entry point. This function behaves as a sign_hash_abort entry point as
  725. * defined in the PSA driver interface specification for transparent
  726. * drivers.
  727. *
  728. * \param[in] operation The \c
  729. * mbedtls_psa_sign_hash_interruptible_operation_t
  730. * to abort.
  731. *
  732. * \retval #PSA_SUCCESS
  733. * The operation was aborted successfully.
  734. */
  735. psa_status_t mbedtls_psa_sign_hash_abort(
  736. mbedtls_psa_sign_hash_interruptible_operation_t *operation);
  737. /**
  738. * \brief Start reading and verifying a hash or short message, in an
  739. * interruptible manner.
  740. *
  741. * \note The signature of this function is that of a PSA driver
  742. * verify_hash_start entry point. This function behaves as a
  743. * verify_hash_start entry point as defined in the PSA driver interface
  744. * specification for transparent drivers.
  745. *
  746. * \param[in] operation The \c
  747. * mbedtls_psa_verify_hash_interruptible_operation_t
  748. * to use. This must be initialized first.
  749. * \param[in] attributes The attributes of the key to use for the
  750. * operation.
  751. * \param[in] key_buffer The buffer containing the key context.
  752. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  753. * \param[in] alg A signature algorithm that is compatible with
  754. * the type of the key.
  755. * \param[in] hash The hash whose signature is to be verified.
  756. * \param hash_length Size of the \p hash buffer in bytes.
  757. * \param[in] signature Buffer containing the signature to verify.
  758. * \param signature_length Size of the \p signature buffer in bytes.
  759. *
  760. * \retval #PSA_SUCCESS
  761. * The operation started successfully - call \c psa_sign_hash_complete()
  762. * with the same context to complete the operation
  763. * \retval #PSA_ERROR_INVALID_ARGUMENT
  764. * An unsupported or incorrect type of key was used.
  765. * \retval #PSA_ERROR_NOT_SUPPORTED
  766. * Either no internal interruptible operations are currently supported,
  767. * or the key type is currently unsupported.
  768. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  769. * There was insufficient memory either to load the key representation,
  770. * or to prepare the operation.
  771. */
  772. psa_status_t mbedtls_psa_verify_hash_start(
  773. mbedtls_psa_verify_hash_interruptible_operation_t *operation,
  774. const psa_key_attributes_t *attributes,
  775. const uint8_t *key_buffer, size_t key_buffer_size,
  776. psa_algorithm_t alg,
  777. const uint8_t *hash, size_t hash_length,
  778. const uint8_t *signature, size_t signature_length);
  779. /**
  780. * \brief Continue and eventually complete the action of signing a hash or
  781. * short message with a private key, in an interruptible manner.
  782. *
  783. * \note The signature of this function is that of a PSA driver
  784. * sign_hash_complete entry point. This function behaves as a
  785. * sign_hash_complete entry point as defined in the PSA driver interface
  786. * specification for transparent drivers.
  787. *
  788. * \param[in] operation The \c
  789. * mbedtls_psa_sign_hash_interruptible_operation_t
  790. * to use. This must be initialized first.
  791. *
  792. * \retval #PSA_SUCCESS
  793. * Operation completed successfully, and the passed signature is valid.
  794. *
  795. * \retval #PSA_OPERATION_INCOMPLETE
  796. * Operation was interrupted due to the setting of \c
  797. * psa_interruptible_set_max_ops(), there is still work to be done,
  798. * please call this function again with the same operation object.
  799. *
  800. * \retval #PSA_ERROR_INVALID_SIGNATURE
  801. * The calculation was performed successfully, but the passed
  802. * signature is not a valid signature.
  803. *
  804. * \retval #PSA_ERROR_NOT_SUPPORTED \emptydescription
  805. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  806. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  807. */
  808. psa_status_t mbedtls_psa_verify_hash_complete(
  809. mbedtls_psa_verify_hash_interruptible_operation_t *operation);
  810. /**
  811. * \brief Abort a verify signed hash operation.
  812. *
  813. * \note The signature of this function is that of a PSA driver
  814. * verify_hash_abort entry point. This function behaves as a
  815. * verify_hash_abort entry point as defined in the PSA driver interface
  816. * specification for transparent drivers.
  817. *
  818. * \param[in] operation The \c
  819. * mbedtls_psa_verify_hash_interruptible_operation_t
  820. * to abort.
  821. *
  822. * \retval #PSA_SUCCESS
  823. * The operation was aborted successfully.
  824. */
  825. psa_status_t mbedtls_psa_verify_hash_abort(
  826. mbedtls_psa_verify_hash_interruptible_operation_t *operation);
  827. #endif /* PSA_CRYPTO_CORE_H */