psa_crypto_storage.h 15 KB

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