psa_crypto_core.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  23. #include "mbedtls/config.h"
  24. #else
  25. #include MBEDTLS_CONFIG_FILE
  26. #endif
  27. #include "psa/crypto.h"
  28. #include "psa/crypto_se_driver.h"
  29. /** Constant-time buffer comparison
  30. *
  31. * \param[in] a Left-hand buffer for comparison.
  32. * \param[in] b Right-hand buffer for comparison.
  33. * \param n Amount of bytes to compare.
  34. *
  35. * \return 0 if the buffer contents are equal, non-zero otherwise
  36. */
  37. static inline int mbedtls_psa_safer_memcmp(
  38. const uint8_t *a, const uint8_t *b, size_t n )
  39. {
  40. size_t i;
  41. unsigned char diff = 0;
  42. for( i = 0; i < n; i++ )
  43. diff |= a[i] ^ b[i];
  44. return( diff );
  45. }
  46. /** The data structure representing a key slot, containing key material
  47. * and metadata for one key.
  48. */
  49. typedef struct
  50. {
  51. psa_core_key_attributes_t attr;
  52. /*
  53. * Number of locks on the key slot held by the library.
  54. *
  55. * This counter is incremented by one each time a library function
  56. * retrieves through one of the dedicated internal API a pointer to the
  57. * key slot.
  58. *
  59. * This counter is decremented by one each time a library function stops
  60. * accessing the key slot and states it by calling the
  61. * psa_unlock_key_slot() API.
  62. *
  63. * This counter is used to prevent resetting the key slot while the library
  64. * may access it. For example, such control is needed in the following
  65. * scenarios:
  66. * . In case of key slot starvation, all key slots contain the description
  67. * of a key, and the library asks for the description of a persistent
  68. * key not present in the key slots, the key slots currently accessed by
  69. * the library cannot be reclaimed to free a key slot to load the
  70. * persistent key.
  71. * . In case of a multi-threaded application where one thread asks to close
  72. * or purge or destroy a key while it is in used by the library through
  73. * another thread.
  74. */
  75. size_t lock_count;
  76. /* Dynamically allocated key data buffer.
  77. * Format as specified in psa_export_key(). */
  78. struct key_data
  79. {
  80. uint8_t *data;
  81. size_t bytes;
  82. } key;
  83. } psa_key_slot_t;
  84. /* A mask of key attribute flags used only internally.
  85. * Currently there aren't any. */
  86. #define PSA_KA_MASK_INTERNAL_ONLY ( \
  87. 0 )
  88. /** Test whether a key slot is occupied.
  89. *
  90. * A key slot is occupied iff the key type is nonzero. This works because
  91. * no valid key can have 0 as its key type.
  92. *
  93. * \param[in] slot The key slot to test.
  94. *
  95. * \return 1 if the slot is occupied, 0 otherwise.
  96. */
  97. static inline int psa_is_key_slot_occupied( const psa_key_slot_t *slot )
  98. {
  99. return( slot->attr.type != 0 );
  100. }
  101. /** Test whether a key slot is locked.
  102. *
  103. * A key slot is locked iff its lock counter is strictly greater than 0.
  104. *
  105. * \param[in] slot The key slot to test.
  106. *
  107. * \return 1 if the slot is locked, 0 otherwise.
  108. */
  109. static inline int psa_is_key_slot_locked( const psa_key_slot_t *slot )
  110. {
  111. return( slot->lock_count > 0 );
  112. }
  113. /** Retrieve flags from psa_key_slot_t::attr::core::flags.
  114. *
  115. * \param[in] slot The key slot to query.
  116. * \param mask The mask of bits to extract.
  117. *
  118. * \return The key attribute flags in the given slot,
  119. * bitwise-anded with \p mask.
  120. */
  121. static inline uint16_t psa_key_slot_get_flags( const psa_key_slot_t *slot,
  122. uint16_t mask )
  123. {
  124. return( slot->attr.flags & mask );
  125. }
  126. /** Set flags in psa_key_slot_t::attr::core::flags.
  127. *
  128. * \param[in,out] slot The key slot to modify.
  129. * \param mask The mask of bits to modify.
  130. * \param value The new value of the selected bits.
  131. */
  132. static inline void psa_key_slot_set_flags( psa_key_slot_t *slot,
  133. uint16_t mask,
  134. uint16_t value )
  135. {
  136. slot->attr.flags = ( ( ~mask & slot->attr.flags ) |
  137. ( mask & value ) );
  138. }
  139. /** Turn on flags in psa_key_slot_t::attr::core::flags.
  140. *
  141. * \param[in,out] slot The key slot to modify.
  142. * \param mask The mask of bits to set.
  143. */
  144. static inline void psa_key_slot_set_bits_in_flags( psa_key_slot_t *slot,
  145. uint16_t mask )
  146. {
  147. slot->attr.flags |= mask;
  148. }
  149. /** Turn off flags in psa_key_slot_t::attr::core::flags.
  150. *
  151. * \param[in,out] slot The key slot to modify.
  152. * \param mask The mask of bits to clear.
  153. */
  154. static inline void psa_key_slot_clear_bits( psa_key_slot_t *slot,
  155. uint16_t mask )
  156. {
  157. slot->attr.flags &= ~mask;
  158. }
  159. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  160. /** Get the SE slot number of a key from the key slot storing its description.
  161. *
  162. * \param[in] slot The key slot to query. This must be a key slot storing
  163. * the description of a key of a dynamically registered
  164. * secure element, otherwise the behaviour is undefined.
  165. */
  166. static inline psa_key_slot_number_t psa_key_slot_get_slot_number(
  167. const psa_key_slot_t *slot )
  168. {
  169. return( *( (psa_key_slot_number_t *)( slot->key.data ) ) );
  170. }
  171. #endif
  172. /** Completely wipe a slot in memory, including its policy.
  173. *
  174. * Persistent storage is not affected.
  175. *
  176. * \param[in,out] slot The key slot to wipe.
  177. *
  178. * \retval #PSA_SUCCESS
  179. * Success. This includes the case of a key slot that was
  180. * already fully wiped.
  181. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  182. */
  183. psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot );
  184. /** Try to allocate a buffer to an empty key slot.
  185. *
  186. * \param[in,out] slot Key slot to attach buffer to.
  187. * \param[in] buffer_length Requested size of the buffer.
  188. *
  189. * \retval #PSA_SUCCESS
  190. * The buffer has been successfully allocated.
  191. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  192. * Not enough memory was available for allocation.
  193. * \retval #PSA_ERROR_ALREADY_EXISTS
  194. * Trying to allocate a buffer to a non-empty key slot.
  195. */
  196. psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
  197. size_t buffer_length );
  198. /** Wipe key data from a slot. Preserves metadata such as the policy. */
  199. psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot );
  200. /** Copy key data (in export format) into an empty key slot.
  201. *
  202. * This function assumes that the slot does not contain
  203. * any key material yet. On failure, the slot content is unchanged.
  204. *
  205. * \param[in,out] slot Key slot to copy the key into.
  206. * \param[in] data Buffer containing the key material.
  207. * \param data_length Size of the key buffer.
  208. *
  209. * \retval #PSA_SUCCESS
  210. * The key has been copied successfully.
  211. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  212. * Not enough memory was available for allocation of the
  213. * copy buffer.
  214. * \retval #PSA_ERROR_ALREADY_EXISTS
  215. * There was other key material already present in the slot.
  216. */
  217. psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
  218. const uint8_t *data,
  219. size_t data_length );
  220. /** Convert an mbed TLS error code to a PSA error code
  221. *
  222. * \note This function is provided solely for the convenience of
  223. * Mbed TLS and may be removed at any time without notice.
  224. *
  225. * \param ret An mbed TLS-thrown error code
  226. *
  227. * \return The corresponding PSA error code
  228. */
  229. psa_status_t mbedtls_to_psa_error( int ret );
  230. /** Get Mbed TLS cipher information given the cipher algorithm PSA identifier
  231. * as well as the PSA type and size of the key to be used with the cipher
  232. * algorithm.
  233. *
  234. * \param alg PSA cipher algorithm identifier
  235. * \param key_type PSA key type
  236. * \param key_bits Size of the key in bits
  237. * \param[out] cipher_id Mbed TLS cipher algorithm identifier
  238. *
  239. * \return The Mbed TLS cipher information of the cipher algorithm.
  240. * \c NULL if the PSA cipher algorithm is not supported.
  241. */
  242. const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
  243. psa_algorithm_t alg, psa_key_type_t key_type, size_t key_bits,
  244. mbedtls_cipher_id_t *cipher_id );
  245. /** Import a key in binary format.
  246. *
  247. * \note The signature of this function is that of a PSA driver
  248. * import_key entry point. This function behaves as an import_key
  249. * entry point as defined in the PSA driver interface specification for
  250. * transparent drivers.
  251. *
  252. * \param[in] attributes The attributes for the key to import.
  253. * \param[in] data The buffer containing the key data in import
  254. * format.
  255. * \param[in] data_length Size of the \p data buffer in bytes.
  256. * \param[out] key_buffer The buffer to contain the key data in output
  257. * format upon successful return.
  258. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes. This
  259. * size is greater or equal to \p data_length.
  260. * \param[out] key_buffer_length The length of the data written in \p
  261. * key_buffer in bytes.
  262. * \param[out] bits The key size in number of bits.
  263. *
  264. * \retval #PSA_SUCCESS The key was imported successfully.
  265. * \retval #PSA_ERROR_INVALID_ARGUMENT
  266. * The key data is not correctly formatted.
  267. * \retval #PSA_ERROR_NOT_SUPPORTED
  268. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  269. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  270. */
  271. psa_status_t psa_import_key_into_slot(
  272. const psa_key_attributes_t *attributes,
  273. const uint8_t *data, size_t data_length,
  274. uint8_t *key_buffer, size_t key_buffer_size,
  275. size_t *key_buffer_length, size_t *bits );
  276. /** Export a key in binary format
  277. *
  278. * \note The signature of this function is that of a PSA driver export_key
  279. * entry point. This function behaves as an export_key entry point as
  280. * defined in the PSA driver interface specification.
  281. *
  282. * \param[in] attributes The attributes for the key to export.
  283. * \param[in] key_buffer Material or context of the key to export.
  284. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  285. * \param[out] data Buffer where the key data is to be written.
  286. * \param[in] data_size Size of the \p data buffer in bytes.
  287. * \param[out] data_length On success, the number of bytes written in
  288. * \p data
  289. *
  290. * \retval #PSA_SUCCESS The key was exported successfully.
  291. * \retval #PSA_ERROR_NOT_SUPPORTED
  292. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  293. * \retval #PSA_ERROR_HARDWARE_FAILURE
  294. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  295. * \retval #PSA_ERROR_STORAGE_FAILURE
  296. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  297. */
  298. psa_status_t psa_export_key_internal(
  299. const psa_key_attributes_t *attributes,
  300. const uint8_t *key_buffer, size_t key_buffer_size,
  301. uint8_t *data, size_t data_size, size_t *data_length );
  302. /** Export a public key or the public part of a key pair in binary format.
  303. *
  304. * \note The signature of this function is that of a PSA driver
  305. * export_public_key entry point. This function behaves as an
  306. * export_public_key entry point as defined in the PSA driver interface
  307. * specification.
  308. *
  309. * \param[in] attributes The attributes for the key to export.
  310. * \param[in] key_buffer Material or context of the key to export.
  311. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  312. * \param[out] data Buffer where the key data is to be written.
  313. * \param[in] data_size Size of the \p data buffer in bytes.
  314. * \param[out] data_length On success, the number of bytes written in
  315. * \p data
  316. *
  317. * \retval #PSA_SUCCESS The public key was exported successfully.
  318. * \retval #PSA_ERROR_NOT_SUPPORTED
  319. * \retval #PSA_ERROR_COMMUNICATION_FAILURE
  320. * \retval #PSA_ERROR_HARDWARE_FAILURE
  321. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  322. * \retval #PSA_ERROR_STORAGE_FAILURE
  323. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  324. */
  325. psa_status_t psa_export_public_key_internal(
  326. const psa_key_attributes_t *attributes,
  327. const uint8_t *key_buffer, size_t key_buffer_size,
  328. uint8_t *data, size_t data_size, size_t *data_length );
  329. /**
  330. * \brief Generate a key.
  331. *
  332. * \note The signature of the function is that of a PSA driver generate_key
  333. * entry point.
  334. *
  335. * \param[in] attributes The attributes for the key to generate.
  336. * \param[out] key_buffer Buffer where the key data is to be written.
  337. * \param[in] key_buffer_size Size of \p key_buffer in bytes.
  338. * \param[out] key_buffer_length On success, the number of bytes written in
  339. * \p key_buffer.
  340. *
  341. * \retval #PSA_SUCCESS
  342. * The key was generated successfully.
  343. * \retval #PSA_ERROR_INVALID_ARGUMENT
  344. * \retval #PSA_ERROR_NOT_SUPPORTED
  345. * Key size in bits or type not supported.
  346. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  347. * The size of \p key_buffer is too small.
  348. */
  349. psa_status_t psa_generate_key_internal( const psa_key_attributes_t *attributes,
  350. uint8_t *key_buffer,
  351. size_t key_buffer_size,
  352. size_t *key_buffer_length );
  353. /** Sign a message with a private key. For hash-and-sign algorithms,
  354. * this includes the hashing step.
  355. *
  356. * \note The signature of this function is that of a PSA driver
  357. * sign_message entry point. This function behaves as a sign_message
  358. * entry point as defined in the PSA driver interface specification for
  359. * transparent drivers.
  360. *
  361. * \note This function will call the driver for psa_sign_hash
  362. * and go through driver dispatch again.
  363. *
  364. * \param[in] attributes The attributes of the key to use for the
  365. * operation.
  366. * \param[in] key_buffer The buffer containing the key context.
  367. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  368. * \param[in] alg A signature algorithm that is compatible with
  369. * the type of the key.
  370. * \param[in] input The input message to sign.
  371. * \param[in] input_length Size of the \p input buffer in bytes.
  372. * \param[out] signature Buffer where the signature is to be written.
  373. * \param[in] signature_size Size of the \p signature buffer in bytes.
  374. * \param[out] signature_length On success, the number of bytes
  375. * that make up the returned signature value.
  376. *
  377. * \retval #PSA_SUCCESS
  378. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  379. * The size of the \p signature buffer is too small. You can
  380. * determine a sufficient buffer size by calling
  381. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  382. * where \c key_type and \c key_bits are the type and bit-size
  383. * respectively of the key.
  384. * \retval #PSA_ERROR_NOT_SUPPORTED
  385. * \retval #PSA_ERROR_INVALID_ARGUMENT
  386. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  387. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  388. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  389. */
  390. psa_status_t psa_sign_message_builtin(
  391. const psa_key_attributes_t *attributes,
  392. const uint8_t *key_buffer, size_t key_buffer_size,
  393. psa_algorithm_t alg, const uint8_t *input, size_t input_length,
  394. uint8_t *signature, size_t signature_size, size_t *signature_length );
  395. /** Verify the signature of a message with a public key, using
  396. * a hash-and-sign verification algorithm.
  397. *
  398. * \note The signature of this function is that of a PSA driver
  399. * verify_message entry point. This function behaves as a verify_message
  400. * entry point as defined in the PSA driver interface specification for
  401. * transparent drivers.
  402. *
  403. * \note This function will call the driver for psa_verify_hash
  404. * and go through driver dispatch again.
  405. *
  406. * \param[in] attributes The attributes of the key to use for the
  407. * operation.
  408. * \param[in] key_buffer The buffer containing the key context.
  409. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  410. * \param[in] alg A signature algorithm that is compatible with
  411. * the type of the key.
  412. * \param[in] input The message whose signature is to be verified.
  413. * \param[in] input_length Size of the \p input buffer in bytes.
  414. * \param[in] signature Buffer containing the signature to verify.
  415. * \param[in] signature_length Size of the \p signature buffer in bytes.
  416. *
  417. * \retval #PSA_SUCCESS
  418. * The signature is valid.
  419. * \retval #PSA_ERROR_INVALID_SIGNATURE
  420. * The calculation was performed successfully, but the passed
  421. * signature is not a valid signature.
  422. * \retval #PSA_ERROR_NOT_SUPPORTED
  423. * \retval #PSA_ERROR_INVALID_ARGUMENT
  424. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  425. */
  426. psa_status_t psa_verify_message_builtin(
  427. const psa_key_attributes_t *attributes,
  428. const uint8_t *key_buffer, size_t key_buffer_size,
  429. psa_algorithm_t alg, const uint8_t *input, size_t input_length,
  430. const uint8_t *signature, size_t signature_length );
  431. /** Sign an already-calculated hash with a private key.
  432. *
  433. * \note The signature of this function is that of a PSA driver
  434. * sign_hash entry point. This function behaves as a sign_hash
  435. * entry point as defined in the PSA driver interface specification for
  436. * transparent drivers.
  437. *
  438. * \param[in] attributes The attributes of the key to use for the
  439. * operation.
  440. * \param[in] key_buffer The buffer containing the key context.
  441. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  442. * \param[in] alg A signature algorithm that is compatible with
  443. * the type of the key.
  444. * \param[in] hash The hash or message to sign.
  445. * \param[in] hash_length Size of the \p hash buffer in bytes.
  446. * \param[out] signature Buffer where the signature is to be written.
  447. * \param[in] signature_size Size of the \p signature buffer in bytes.
  448. * \param[out] signature_length On success, the number of bytes
  449. * that make up the returned signature value.
  450. *
  451. * \retval #PSA_SUCCESS
  452. * \retval #PSA_ERROR_BUFFER_TOO_SMALL
  453. * The size of the \p signature buffer is too small. You can
  454. * determine a sufficient buffer size by calling
  455. * #PSA_SIGN_OUTPUT_SIZE(\c key_type, \c key_bits, \p alg)
  456. * where \c key_type and \c key_bits are the type and bit-size
  457. * respectively of the key.
  458. * \retval #PSA_ERROR_NOT_SUPPORTED
  459. * \retval #PSA_ERROR_INVALID_ARGUMENT
  460. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  461. * \retval #PSA_ERROR_CORRUPTION_DETECTED
  462. * \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
  463. */
  464. psa_status_t psa_sign_hash_builtin(
  465. const psa_key_attributes_t *attributes,
  466. const uint8_t *key_buffer, size_t key_buffer_size,
  467. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  468. uint8_t *signature, size_t signature_size, size_t *signature_length );
  469. /**
  470. * \brief Verify the signature a hash or short message using a public key.
  471. *
  472. * \note The signature of this function is that of a PSA driver
  473. * verify_hash entry point. This function behaves as a verify_hash
  474. * entry point as defined in the PSA driver interface specification for
  475. * transparent drivers.
  476. *
  477. * \param[in] attributes The attributes of the key to use for the
  478. * operation.
  479. * \param[in] key_buffer The buffer containing the key context.
  480. * \param[in] key_buffer_size Size of the \p key_buffer buffer in bytes.
  481. * \param[in] alg A signature algorithm that is compatible with
  482. * the type of the key.
  483. * \param[in] hash The hash or message whose signature is to be
  484. * verified.
  485. * \param[in] hash_length Size of the \p hash buffer in bytes.
  486. * \param[in] signature Buffer containing the signature to verify.
  487. * \param[in] signature_length Size of the \p signature buffer in bytes.
  488. *
  489. * \retval #PSA_SUCCESS
  490. * The signature is valid.
  491. * \retval #PSA_ERROR_INVALID_SIGNATURE
  492. * The calculation was performed successfully, but the passed
  493. * signature is not a valid signature.
  494. * \retval #PSA_ERROR_NOT_SUPPORTED
  495. * \retval #PSA_ERROR_INVALID_ARGUMENT
  496. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
  497. */
  498. psa_status_t psa_verify_hash_builtin(
  499. const psa_key_attributes_t *attributes,
  500. const uint8_t *key_buffer, size_t key_buffer_size,
  501. psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
  502. const uint8_t *signature, size_t signature_length );
  503. #endif /* PSA_CRYPTO_CORE_H */