psa_crypto_storage.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * \file psa_crypto_storage.h
  3. *
  4. * \brief PSA cryptography module: Mbed TLS key storage
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. */
  10. #ifndef PSA_CRYPTO_STORAGE_H
  11. #define PSA_CRYPTO_STORAGE_H
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include "psa/crypto.h"
  16. #include "psa/crypto_se_driver.h"
  17. #include <stdint.h>
  18. #include <string.h>
  19. /* Limit the maximum key size in storage. This should have no effect
  20. * since the key size is limited in memory. */
  21. #define PSA_CRYPTO_MAX_STORAGE_SIZE (PSA_BITS_TO_BYTES(PSA_MAX_KEY_BITS))
  22. /* Sanity check: a file size must fit in 32 bits. Allow a generous
  23. * 64kB of metadata. */
  24. #if PSA_CRYPTO_MAX_STORAGE_SIZE > 0xffff0000
  25. #error "PSA_CRYPTO_MAX_STORAGE_SIZE > 0xffff0000"
  26. #endif
  27. /** The maximum permitted persistent slot number.
  28. *
  29. * In Mbed Crypto 0.1.0b:
  30. * - Using the file backend, all key ids are ok except 0.
  31. * - Using the ITS backend, all key ids are ok except 0xFFFFFF52
  32. * (#PSA_CRYPTO_ITS_RANDOM_SEED_UID) for which the file contains the
  33. * device's random seed (if this feature is enabled).
  34. * - Only key ids from 1 to #MBEDTLS_PSA_KEY_SLOT_COUNT are actually used.
  35. *
  36. * Since we need to preserve the random seed, avoid using that key slot.
  37. * Reserve a whole range of key slots just in case something else comes up.
  38. *
  39. * This limitation will probably become moot when we implement client
  40. * separation for key storage.
  41. */
  42. #define PSA_MAX_PERSISTENT_KEY_IDENTIFIER PSA_KEY_ID_VENDOR_MAX
  43. /**
  44. * \brief Checks if persistent data is stored for the given key slot number
  45. *
  46. * This function checks if any key data or metadata exists for the key slot in
  47. * the persistent storage.
  48. *
  49. * \param key Persistent identifier to check.
  50. *
  51. * \retval 0
  52. * No persistent data present for slot number
  53. * \retval 1
  54. * Persistent data present for slot number
  55. */
  56. int psa_is_key_present_in_storage(const mbedtls_svc_key_id_t key);
  57. /**
  58. * \brief Format key data and metadata and save to a location for given key
  59. * slot.
  60. *
  61. * This function formats the key data and metadata and saves it to a
  62. * persistent storage backend. The storage location corresponding to the
  63. * key slot must be empty, otherwise this function will fail. This function
  64. * should be called after loading the key into an internal slot to ensure the
  65. * persistent key is not saved into a storage location corresponding to an
  66. * already occupied non-persistent key, as well as ensuring the key data is
  67. * validated.
  68. *
  69. * Note: This function will only succeed for key buffers which are not
  70. * empty. If passed a NULL pointer or zero-length, the function will fail
  71. * with #PSA_ERROR_INVALID_ARGUMENT.
  72. *
  73. * \param[in] attr The attributes of the key to save.
  74. * The key identifier field in the attributes
  75. * determines the key's location.
  76. * \param[in] data Buffer containing the key data.
  77. * \param data_length The number of bytes that make up the key data.
  78. *
  79. * \retval #PSA_SUCCESS \emptydescription
  80. * \retval #PSA_ERROR_INVALID_ARGUMENT \emptydescription
  81. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  82. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  83. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  84. * \retval #PSA_ERROR_ALREADY_EXISTS \emptydescription
  85. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  86. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  87. */
  88. psa_status_t psa_save_persistent_key(const psa_key_attributes_t *attr,
  89. const uint8_t *data,
  90. const size_t data_length);
  91. /**
  92. * \brief Parses key data and metadata and load persistent key for given
  93. * key slot number.
  94. *
  95. * This function reads from a storage backend, parses the key data and
  96. * metadata and writes them to the appropriate output parameters.
  97. *
  98. * Note: This function allocates a buffer and returns a pointer to it through
  99. * the data parameter. On successful return, the pointer is guaranteed to be
  100. * valid and the buffer contains at least one byte of data.
  101. * psa_free_persistent_key_data() must be called on the data buffer
  102. * afterwards to zeroize and free this buffer.
  103. *
  104. * \param[in,out] attr On input, the key identifier field identifies
  105. * the key to load. Other fields are ignored.
  106. * On success, the attribute structure contains
  107. * the key metadata that was loaded from storage.
  108. * \param[out] data Pointer to an allocated key data buffer on return.
  109. * \param[out] data_length The number of bytes that make up the key data.
  110. *
  111. * \retval #PSA_SUCCESS \emptydescription
  112. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  113. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  114. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  115. * \retval #PSA_ERROR_DOES_NOT_EXIST \emptydescription
  116. */
  117. psa_status_t psa_load_persistent_key(psa_key_attributes_t *attr,
  118. uint8_t **data,
  119. size_t *data_length);
  120. /**
  121. * \brief Remove persistent data for the given key slot number.
  122. *
  123. * \param key Persistent identifier of the key to remove
  124. * from persistent storage.
  125. *
  126. * \retval #PSA_SUCCESS
  127. * The key was successfully removed,
  128. * or the key did not exist.
  129. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  130. */
  131. psa_status_t psa_destroy_persistent_key(const mbedtls_svc_key_id_t key);
  132. /**
  133. * \brief Free the temporary buffer allocated by psa_load_persistent_key().
  134. *
  135. * This function must be called at some point after psa_load_persistent_key()
  136. * to zeroize and free the memory allocated to the buffer in that function.
  137. *
  138. * \param key_data Buffer for the key data.
  139. * \param key_data_length Size of the key data buffer.
  140. *
  141. */
  142. void psa_free_persistent_key_data(uint8_t *key_data, size_t key_data_length);
  143. /**
  144. * \brief Formats key data and metadata for persistent storage
  145. *
  146. * \param[in] data Buffer containing the key data.
  147. * \param data_length Length of the key data buffer.
  148. * \param[in] attr The core attributes of the key.
  149. * \param[out] storage_data Output buffer for the formatted data.
  150. *
  151. */
  152. void psa_format_key_data_for_storage(const uint8_t *data,
  153. const size_t data_length,
  154. const psa_key_attributes_t *attr,
  155. uint8_t *storage_data);
  156. /**
  157. * \brief Parses persistent storage data into key data and metadata
  158. *
  159. * \param[in] storage_data Buffer for the storage data.
  160. * \param storage_data_length Length of the storage data buffer
  161. * \param[out] key_data On output, pointer to a newly allocated buffer
  162. * containing the key data. This must be freed
  163. * using psa_free_persistent_key_data()
  164. * \param[out] key_data_length Length of the key data buffer
  165. * \param[out] attr On success, the attribute structure is filled
  166. * with the loaded key metadata.
  167. *
  168. * \retval #PSA_SUCCESS \emptydescription
  169. * \retval #PSA_ERROR_INSUFFICIENT_MEMORY \emptydescription
  170. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  171. */
  172. psa_status_t psa_parse_key_data_from_storage(const uint8_t *storage_data,
  173. size_t storage_data_length,
  174. uint8_t **key_data,
  175. size_t *key_data_length,
  176. psa_key_attributes_t *attr);
  177. #if defined(MBEDTLS_PSA_CRYPTO_SE_C)
  178. /** This symbol is defined if transaction support is required. */
  179. #define PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS 1
  180. #endif
  181. #if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
  182. /** The type of transaction that is in progress.
  183. */
  184. /* This is an integer type rather than an enum for two reasons: to support
  185. * unknown values when loading a transaction file, and to ensure that the
  186. * type has a known size.
  187. */
  188. typedef uint16_t psa_crypto_transaction_type_t;
  189. /** No transaction is in progress.
  190. *
  191. * This has the value 0, so zero-initialization sets a transaction's type to
  192. * this value.
  193. */
  194. #define PSA_CRYPTO_TRANSACTION_NONE ((psa_crypto_transaction_type_t) 0x0000)
  195. /** A key creation transaction.
  196. *
  197. * This is only used for keys in an external cryptoprocessor (secure element).
  198. * Keys in RAM or in internal storage are created atomically in storage
  199. * (simple file creation), so they do not need a transaction mechanism.
  200. */
  201. #define PSA_CRYPTO_TRANSACTION_CREATE_KEY ((psa_crypto_transaction_type_t) 0x0001)
  202. /** A key destruction transaction.
  203. *
  204. * This is only used for keys in an external cryptoprocessor (secure element).
  205. * Keys in RAM or in internal storage are destroyed atomically in storage
  206. * (simple file deletion), so they do not need a transaction mechanism.
  207. */
  208. #define PSA_CRYPTO_TRANSACTION_DESTROY_KEY ((psa_crypto_transaction_type_t) 0x0002)
  209. /** Transaction data.
  210. *
  211. * This type is designed to be serialized by writing the memory representation
  212. * and reading it back on the same device.
  213. *
  214. * \note The transaction mechanism is not thread-safe. There can only be one
  215. * single active transaction at a time.
  216. * The transaction object is #psa_crypto_transaction.
  217. *
  218. * \note If an API call starts a transaction, it must complete this transaction
  219. * before returning to the application.
  220. *
  221. * The lifetime of a transaction is the following (note that only one
  222. * transaction may be active at a time):
  223. *
  224. * -# Call psa_crypto_prepare_transaction() to initialize the transaction
  225. * object in memory and declare the type of transaction that is starting.
  226. * -# Fill in the type-specific fields of #psa_crypto_transaction.
  227. * -# Call psa_crypto_save_transaction() to start the transaction. This
  228. * saves the transaction data to internal storage.
  229. * -# Perform the work of the transaction by modifying files, contacting
  230. * external entities, or whatever needs doing. Note that the transaction
  231. * may be interrupted by a power failure, so you need to have a way
  232. * recover from interruptions either by undoing what has been done
  233. * so far or by resuming where you left off.
  234. * -# If there are intermediate stages in the transaction, update
  235. * the fields of #psa_crypto_transaction and call
  236. * psa_crypto_save_transaction() again when each stage is reached.
  237. * -# When the transaction is over, call psa_crypto_stop_transaction() to
  238. * remove the transaction data in storage and in memory.
  239. *
  240. * If the system crashes while a transaction is in progress, psa_crypto_init()
  241. * calls psa_crypto_load_transaction() and takes care of completing or
  242. * rewinding the transaction. This is done in psa_crypto_recover_transaction()
  243. * in psa_crypto.c. If you add a new type of transaction, be
  244. * sure to add code for it in psa_crypto_recover_transaction().
  245. */
  246. typedef union {
  247. /* Each element of this union must have the following properties
  248. * to facilitate serialization and deserialization:
  249. *
  250. * - The element is a struct.
  251. * - The first field of the struct is `psa_crypto_transaction_type_t type`.
  252. * - Elements of the struct are arranged such a way that there is
  253. * no padding.
  254. */
  255. struct psa_crypto_transaction_unknown_s {
  256. psa_crypto_transaction_type_t type;
  257. uint16_t unused1;
  258. uint32_t unused2;
  259. uint64_t unused3;
  260. uint64_t unused4;
  261. } unknown;
  262. /* ::type is #PSA_CRYPTO_TRANSACTION_CREATE_KEY or
  263. * #PSA_CRYPTO_TRANSACTION_DESTROY_KEY. */
  264. struct psa_crypto_transaction_key_s {
  265. psa_crypto_transaction_type_t type;
  266. uint16_t unused1;
  267. psa_key_lifetime_t lifetime;
  268. psa_key_slot_number_t slot;
  269. mbedtls_svc_key_id_t id;
  270. } key;
  271. } psa_crypto_transaction_t;
  272. /** The single active transaction.
  273. */
  274. extern psa_crypto_transaction_t psa_crypto_transaction;
  275. /** Prepare for a transaction.
  276. *
  277. * There must not be an ongoing transaction.
  278. *
  279. * \param type The type of transaction to start.
  280. */
  281. static inline void psa_crypto_prepare_transaction(
  282. psa_crypto_transaction_type_t type)
  283. {
  284. psa_crypto_transaction.unknown.type = type;
  285. }
  286. /** Save the transaction data to storage.
  287. *
  288. * You may call this function multiple times during a transaction to
  289. * atomically update the transaction state.
  290. *
  291. * \retval #PSA_SUCCESS \emptydescription
  292. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  293. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  294. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  295. */
  296. psa_status_t psa_crypto_save_transaction(void);
  297. /** Load the transaction data from storage, if any.
  298. *
  299. * This function is meant to be called from psa_crypto_init() to recover
  300. * in case a transaction was interrupted by a system crash.
  301. *
  302. * \retval #PSA_SUCCESS
  303. * The data about the ongoing transaction has been loaded to
  304. * #psa_crypto_transaction.
  305. * \retval #PSA_ERROR_DOES_NOT_EXIST
  306. * There is no ongoing transaction.
  307. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  308. * \retval #PSA_ERROR_DATA_INVALID \emptydescription
  309. * \retval #PSA_ERROR_DATA_CORRUPT \emptydescription
  310. */
  311. psa_status_t psa_crypto_load_transaction(void);
  312. /** Indicate that the current transaction is finished.
  313. *
  314. * Call this function at the very end of transaction processing.
  315. * This function does not "commit" or "abort" the transaction: the storage
  316. * subsystem has no concept of "commit" and "abort", just saving and
  317. * removing the transaction information in storage.
  318. *
  319. * This function erases the transaction data in storage (if any) and
  320. * resets the transaction data in memory.
  321. *
  322. * \retval #PSA_SUCCESS
  323. * There was transaction data in storage.
  324. * \retval #PSA_ERROR_DOES_NOT_EXIST
  325. * There was no transaction data in storage.
  326. * \retval #PSA_ERROR_STORAGE_FAILURE
  327. * It was impossible to determine whether there was transaction data
  328. * in storage, or the transaction data could not be erased.
  329. */
  330. psa_status_t psa_crypto_stop_transaction(void);
  331. /** The ITS file identifier for the transaction data.
  332. *
  333. * 0xffffffNN = special file; 0x74 = 't' for transaction.
  334. */
  335. #define PSA_CRYPTO_ITS_TRANSACTION_UID ((psa_key_id_t) 0xffffff74)
  336. #endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
  337. #if defined(MBEDTLS_PSA_INJECT_ENTROPY)
  338. /** Backend side of mbedtls_psa_inject_entropy().
  339. *
  340. * This function stores the supplied data into the entropy seed file.
  341. *
  342. * \retval #PSA_SUCCESS
  343. * Success
  344. * \retval #PSA_ERROR_STORAGE_FAILURE \emptydescription
  345. * \retval #PSA_ERROR_INSUFFICIENT_STORAGE \emptydescription
  346. * \retval #PSA_ERROR_NOT_PERMITTED
  347. * The entropy seed file already exists.
  348. */
  349. psa_status_t mbedtls_psa_storage_inject_entropy(const unsigned char *seed,
  350. size_t seed_size);
  351. #endif /* MBEDTLS_PSA_INJECT_ENTROPY */
  352. #ifdef __cplusplus
  353. }
  354. #endif
  355. #endif /* PSA_CRYPTO_STORAGE_H */