keyslot-manager.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. /**
  6. * DOC: The Keyslot Manager
  7. *
  8. * Many devices with inline encryption support have a limited number of "slots"
  9. * into which encryption contexts may be programmed, and requests can be tagged
  10. * with a slot number to specify the key to use for en/decryption.
  11. *
  12. * As the number of slots are limited, and programming keys is expensive on
  13. * many inline encryption hardware, we don't want to program the same key into
  14. * multiple slots - if multiple requests are using the same key, we want to
  15. * program just one slot with that key and use that slot for all requests.
  16. *
  17. * The keyslot manager manages these keyslots appropriately, and also acts as
  18. * an abstraction between the inline encryption hardware and the upper layers.
  19. *
  20. * Lower layer devices will set up a keyslot manager in their request queue
  21. * and tell it how to perform device specific operations like programming/
  22. * evicting keys from keyslots.
  23. *
  24. * Upper layers will call keyslot_manager_get_slot_for_key() to program a
  25. * key into some slot in the inline encryption hardware.
  26. */
  27. #include <crypto/algapi.h>
  28. #include <linux/keyslot-manager.h>
  29. #include <linux/atomic.h>
  30. #include <linux/mutex.h>
  31. #include <linux/pm_runtime.h>
  32. #include <linux/wait.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/overflow.h>
  35. struct keyslot {
  36. atomic_t slot_refs;
  37. struct list_head idle_slot_node;
  38. struct hlist_node hash_node;
  39. struct blk_crypto_key key;
  40. };
  41. struct keyslot_manager {
  42. unsigned int num_slots;
  43. struct keyslot_mgmt_ll_ops ksm_ll_ops;
  44. unsigned int features;
  45. unsigned int crypto_mode_supported[BLK_ENCRYPTION_MODE_MAX];
  46. unsigned int max_dun_bytes_supported;
  47. void *ll_priv_data;
  48. #ifdef CONFIG_PM
  49. /* Device for runtime power management (NULL if none) */
  50. struct device *dev;
  51. #endif
  52. /* Protects programming and evicting keys from the device */
  53. struct rw_semaphore lock;
  54. /*
  55. * Above rw_semaphore maybe nested when used a dm stack layer
  56. * which is with inline encryption
  57. */
  58. unsigned int lock_flags;
  59. /* List of idle slots, with least recently used slot at front */
  60. wait_queue_head_t idle_slots_wait_queue;
  61. struct list_head idle_slots;
  62. spinlock_t idle_slots_lock;
  63. /*
  64. * Hash table which maps key hashes to keyslots, so that we can find a
  65. * key's keyslot in O(1) time rather than O(num_slots). Protected by
  66. * 'lock'. A cryptographic hash function is used so that timing attacks
  67. * can't leak information about the raw keys.
  68. */
  69. struct hlist_head *slot_hashtable;
  70. unsigned int slot_hashtable_size;
  71. /* Per-keyslot data */
  72. struct keyslot slots[];
  73. };
  74. static inline bool keyslot_manager_is_passthrough(struct keyslot_manager *ksm)
  75. {
  76. return ksm->num_slots == 0;
  77. }
  78. #ifdef CONFIG_PM
  79. static inline void keyslot_manager_set_dev(struct keyslot_manager *ksm,
  80. struct device *dev)
  81. {
  82. ksm->dev = dev;
  83. }
  84. /* If there's an underlying device and it's suspended, resume it. */
  85. static inline void keyslot_manager_pm_get(struct keyslot_manager *ksm)
  86. {
  87. if (ksm->dev)
  88. pm_runtime_get_sync(ksm->dev);
  89. }
  90. static inline void keyslot_manager_pm_put(struct keyslot_manager *ksm)
  91. {
  92. if (ksm->dev)
  93. pm_runtime_put_sync(ksm->dev);
  94. }
  95. #else /* CONFIG_PM */
  96. static inline void keyslot_manager_set_dev(struct keyslot_manager *ksm,
  97. struct device *dev)
  98. {
  99. }
  100. static inline void keyslot_manager_pm_get(struct keyslot_manager *ksm)
  101. {
  102. }
  103. static inline void keyslot_manager_pm_put(struct keyslot_manager *ksm)
  104. {
  105. }
  106. #endif /* !CONFIG_PM */
  107. static inline void keyslot_manager_hw_enter(struct keyslot_manager *ksm)
  108. {
  109. /*
  110. * Calling into the driver requires ksm->lock held and the device
  111. * resumed. But we must resume the device first, since that can acquire
  112. * and release ksm->lock via keyslot_manager_reprogram_all_keys().
  113. */
  114. keyslot_manager_pm_get(ksm);
  115. if (!ksm->lock_flags)
  116. down_write(&ksm->lock);
  117. else
  118. down_write_nested(&ksm->lock, ksm->lock_flags);
  119. }
  120. static inline void keyslot_manager_hw_exit(struct keyslot_manager *ksm)
  121. {
  122. up_write(&ksm->lock);
  123. keyslot_manager_pm_put(ksm);
  124. }
  125. /**
  126. * keyslot_manager_create() - Create a keyslot manager
  127. * @dev: Device for runtime power management (NULL if none)
  128. * @num_slots: The number of key slots to manage.
  129. * @ksm_ll_ops: The struct keyslot_mgmt_ll_ops for the device that this keyslot
  130. * manager will use to perform operations like programming and
  131. * evicting keys.
  132. * @features: The supported features as a bitmask of BLK_CRYPTO_FEATURE_* flags.
  133. * Most drivers should set BLK_CRYPTO_FEATURE_STANDARD_KEYS here.
  134. * @crypto_mode_supported: Array of size BLK_ENCRYPTION_MODE_MAX of
  135. * bitmasks that represents whether a crypto mode
  136. * and data unit size are supported. The i'th bit
  137. * of crypto_mode_supported[crypto_mode] is set iff
  138. * a data unit size of (1 << i) is supported. We
  139. * only support data unit sizes that are powers of
  140. * 2.
  141. * @ll_priv_data: Private data passed as is to the functions in ksm_ll_ops.
  142. *
  143. * Allocate memory for and initialize a keyslot manager. Called by e.g.
  144. * storage drivers to set up a keyslot manager in their request_queue.
  145. *
  146. * Context: May sleep
  147. * Return: Pointer to constructed keyslot manager or NULL on error.
  148. */
  149. struct keyslot_manager *keyslot_manager_create(
  150. struct device *dev,
  151. unsigned int num_slots,
  152. const struct keyslot_mgmt_ll_ops *ksm_ll_ops,
  153. unsigned int features,
  154. const unsigned int crypto_mode_supported[BLK_ENCRYPTION_MODE_MAX],
  155. void *ll_priv_data)
  156. {
  157. struct keyslot_manager *ksm;
  158. unsigned int slot;
  159. unsigned int i;
  160. if (num_slots == 0)
  161. return NULL;
  162. /* Check that all ops are specified */
  163. if (ksm_ll_ops->keyslot_program == NULL ||
  164. ksm_ll_ops->keyslot_evict == NULL)
  165. return NULL;
  166. ksm = kvzalloc(struct_size(ksm, slots, num_slots), GFP_KERNEL);
  167. if (!ksm)
  168. return NULL;
  169. ksm->num_slots = num_slots;
  170. ksm->ksm_ll_ops = *ksm_ll_ops;
  171. ksm->features = features;
  172. memcpy(ksm->crypto_mode_supported, crypto_mode_supported,
  173. sizeof(ksm->crypto_mode_supported));
  174. ksm->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
  175. ksm->ll_priv_data = ll_priv_data;
  176. keyslot_manager_set_dev(ksm, dev);
  177. init_rwsem(&ksm->lock);
  178. init_waitqueue_head(&ksm->idle_slots_wait_queue);
  179. INIT_LIST_HEAD(&ksm->idle_slots);
  180. for (slot = 0; slot < num_slots; slot++) {
  181. list_add_tail(&ksm->slots[slot].idle_slot_node,
  182. &ksm->idle_slots);
  183. }
  184. spin_lock_init(&ksm->idle_slots_lock);
  185. ksm->slot_hashtable_size = roundup_pow_of_two(num_slots);
  186. ksm->slot_hashtable = kvmalloc_array(ksm->slot_hashtable_size,
  187. sizeof(ksm->slot_hashtable[0]),
  188. GFP_KERNEL);
  189. if (!ksm->slot_hashtable)
  190. goto err_free_ksm;
  191. for (i = 0; i < ksm->slot_hashtable_size; i++)
  192. INIT_HLIST_HEAD(&ksm->slot_hashtable[i]);
  193. return ksm;
  194. err_free_ksm:
  195. keyslot_manager_destroy(ksm);
  196. return NULL;
  197. }
  198. EXPORT_SYMBOL_GPL(keyslot_manager_create);
  199. void keyslot_manager_set_max_dun_bytes(struct keyslot_manager *ksm,
  200. unsigned int max_dun_bytes)
  201. {
  202. ksm->max_dun_bytes_supported = max_dun_bytes;
  203. }
  204. EXPORT_SYMBOL_GPL(keyslot_manager_set_max_dun_bytes);
  205. static inline struct hlist_head *
  206. hash_bucket_for_key(struct keyslot_manager *ksm,
  207. const struct blk_crypto_key *key)
  208. {
  209. return &ksm->slot_hashtable[blk_crypto_key_hash(key) &
  210. (ksm->slot_hashtable_size - 1)];
  211. }
  212. static void remove_slot_from_lru_list(struct keyslot_manager *ksm, int slot)
  213. {
  214. unsigned long flags;
  215. spin_lock_irqsave(&ksm->idle_slots_lock, flags);
  216. list_del(&ksm->slots[slot].idle_slot_node);
  217. spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
  218. }
  219. static int find_keyslot(struct keyslot_manager *ksm,
  220. const struct blk_crypto_key *key)
  221. {
  222. const struct hlist_head *head = hash_bucket_for_key(ksm, key);
  223. const struct keyslot *slotp;
  224. hlist_for_each_entry(slotp, head, hash_node) {
  225. if (slotp->key.hash == key->hash &&
  226. slotp->key.crypto_mode == key->crypto_mode &&
  227. slotp->key.size == key->size &&
  228. slotp->key.data_unit_size == key->data_unit_size &&
  229. !crypto_memneq(slotp->key.raw, key->raw, key->size))
  230. return slotp - ksm->slots;
  231. }
  232. return -ENOKEY;
  233. }
  234. static int find_and_grab_keyslot(struct keyslot_manager *ksm,
  235. const struct blk_crypto_key *key)
  236. {
  237. int slot;
  238. slot = find_keyslot(ksm, key);
  239. if (slot < 0)
  240. return slot;
  241. if (atomic_inc_return(&ksm->slots[slot].slot_refs) == 1) {
  242. /* Took first reference to this slot; remove it from LRU list */
  243. remove_slot_from_lru_list(ksm, slot);
  244. }
  245. return slot;
  246. }
  247. /**
  248. * keyslot_manager_get_slot_for_key() - Program a key into a keyslot.
  249. * @ksm: The keyslot manager to program the key into.
  250. * @key: Pointer to the key object to program, including the raw key, crypto
  251. * mode, and data unit size.
  252. *
  253. * Get a keyslot that's been programmed with the specified key. If one already
  254. * exists, return it with incremented refcount. Otherwise, wait for a keyslot
  255. * to become idle and program it.
  256. *
  257. * Context: Process context. Takes and releases ksm->lock.
  258. * Return: The keyslot on success, else a -errno value.
  259. */
  260. int keyslot_manager_get_slot_for_key(struct keyslot_manager *ksm,
  261. const struct blk_crypto_key *key)
  262. {
  263. int slot;
  264. int err;
  265. struct keyslot *idle_slot;
  266. if (keyslot_manager_is_passthrough(ksm))
  267. return 0;
  268. down_read(&ksm->lock);
  269. slot = find_and_grab_keyslot(ksm, key);
  270. up_read(&ksm->lock);
  271. if (slot != -ENOKEY)
  272. return slot;
  273. for (;;) {
  274. keyslot_manager_hw_enter(ksm);
  275. slot = find_and_grab_keyslot(ksm, key);
  276. if (slot != -ENOKEY) {
  277. keyslot_manager_hw_exit(ksm);
  278. return slot;
  279. }
  280. /*
  281. * If we're here, that means there wasn't a slot that was
  282. * already programmed with the key. So try to program it.
  283. */
  284. if (!list_empty(&ksm->idle_slots))
  285. break;
  286. keyslot_manager_hw_exit(ksm);
  287. wait_event(ksm->idle_slots_wait_queue,
  288. !list_empty(&ksm->idle_slots));
  289. }
  290. idle_slot = list_first_entry(&ksm->idle_slots, struct keyslot,
  291. idle_slot_node);
  292. slot = idle_slot - ksm->slots;
  293. err = ksm->ksm_ll_ops.keyslot_program(ksm, key, slot);
  294. if (err) {
  295. wake_up(&ksm->idle_slots_wait_queue);
  296. keyslot_manager_hw_exit(ksm);
  297. return err;
  298. }
  299. /* Move this slot to the hash list for the new key. */
  300. if (idle_slot->key.crypto_mode != BLK_ENCRYPTION_MODE_INVALID)
  301. hlist_del(&idle_slot->hash_node);
  302. hlist_add_head(&idle_slot->hash_node, hash_bucket_for_key(ksm, key));
  303. atomic_set(&idle_slot->slot_refs, 1);
  304. idle_slot->key = *key;
  305. remove_slot_from_lru_list(ksm, slot);
  306. keyslot_manager_hw_exit(ksm);
  307. return slot;
  308. }
  309. /**
  310. * keyslot_manager_get_slot() - Increment the refcount on the specified slot.
  311. * @ksm: The keyslot manager that we want to modify.
  312. * @slot: The slot to increment the refcount of.
  313. *
  314. * This function assumes that there is already an active reference to that slot
  315. * and simply increments the refcount. This is useful when cloning a bio that
  316. * already has a reference to a keyslot, and we want the cloned bio to also have
  317. * its own reference.
  318. *
  319. * Context: Any context.
  320. */
  321. void keyslot_manager_get_slot(struct keyslot_manager *ksm, unsigned int slot)
  322. {
  323. if (keyslot_manager_is_passthrough(ksm))
  324. return;
  325. if (WARN_ON(slot >= ksm->num_slots))
  326. return;
  327. WARN_ON(atomic_inc_return(&ksm->slots[slot].slot_refs) < 2);
  328. }
  329. /**
  330. * keyslot_manager_put_slot() - Release a reference to a slot
  331. * @ksm: The keyslot manager to release the reference from.
  332. * @slot: The slot to release the reference from.
  333. *
  334. * Context: Any context.
  335. */
  336. void keyslot_manager_put_slot(struct keyslot_manager *ksm, unsigned int slot)
  337. {
  338. unsigned long flags;
  339. if (keyslot_manager_is_passthrough(ksm))
  340. return;
  341. if (WARN_ON(slot >= ksm->num_slots))
  342. return;
  343. if (atomic_dec_and_lock_irqsave(&ksm->slots[slot].slot_refs,
  344. &ksm->idle_slots_lock, flags)) {
  345. list_add_tail(&ksm->slots[slot].idle_slot_node,
  346. &ksm->idle_slots);
  347. spin_unlock_irqrestore(&ksm->idle_slots_lock, flags);
  348. wake_up(&ksm->idle_slots_wait_queue);
  349. }
  350. }
  351. /**
  352. * keyslot_manager_crypto_mode_supported() - Find out if a crypto_mode /
  353. * data unit size / is_hw_wrapped_key
  354. * combination is supported by a ksm.
  355. * @ksm: The keyslot manager to check
  356. * @crypto_mode: The crypto mode to check for.
  357. * @dun_bytes: The number of bytes that will be used to specify the DUN
  358. * @data_unit_size: The data_unit_size for the mode.
  359. * @is_hw_wrapped_key: Whether a hardware-wrapped key will be used.
  360. *
  361. * Calls and returns the result of the crypto_mode_supported function specified
  362. * by the ksm.
  363. *
  364. * Context: Process context.
  365. * Return: Whether or not this ksm supports the specified crypto settings.
  366. */
  367. bool keyslot_manager_crypto_mode_supported(struct keyslot_manager *ksm,
  368. enum blk_crypto_mode_num crypto_mode,
  369. unsigned int dun_bytes,
  370. unsigned int data_unit_size,
  371. bool is_hw_wrapped_key)
  372. {
  373. if (!ksm)
  374. return false;
  375. if (WARN_ON(crypto_mode >= BLK_ENCRYPTION_MODE_MAX))
  376. return false;
  377. if (WARN_ON(!is_power_of_2(data_unit_size)))
  378. return false;
  379. if (is_hw_wrapped_key) {
  380. if (!(ksm->features & BLK_CRYPTO_FEATURE_WRAPPED_KEYS))
  381. return false;
  382. } else {
  383. if (!(ksm->features & BLK_CRYPTO_FEATURE_STANDARD_KEYS))
  384. return false;
  385. }
  386. if (!(ksm->crypto_mode_supported[crypto_mode] & data_unit_size))
  387. return false;
  388. return ksm->max_dun_bytes_supported >= dun_bytes;
  389. }
  390. /**
  391. * keyslot_manager_evict_key() - Evict a key from the lower layer device.
  392. * @ksm: The keyslot manager to evict from
  393. * @key: The key to evict
  394. *
  395. * Find the keyslot that the specified key was programmed into, and evict that
  396. * slot from the lower layer device if that slot is not currently in use.
  397. *
  398. * Context: Process context. Takes and releases ksm->lock.
  399. * Return: 0 on success, -EBUSY if the key is still in use, or another
  400. * -errno value on other error.
  401. */
  402. int keyslot_manager_evict_key(struct keyslot_manager *ksm,
  403. const struct blk_crypto_key *key)
  404. {
  405. int slot;
  406. int err;
  407. struct keyslot *slotp;
  408. if (keyslot_manager_is_passthrough(ksm)) {
  409. if (ksm->ksm_ll_ops.keyslot_evict) {
  410. keyslot_manager_hw_enter(ksm);
  411. err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, -1);
  412. keyslot_manager_hw_exit(ksm);
  413. return err;
  414. }
  415. return 0;
  416. }
  417. keyslot_manager_hw_enter(ksm);
  418. slot = find_keyslot(ksm, key);
  419. if (slot < 0) {
  420. err = slot;
  421. goto out_unlock;
  422. }
  423. slotp = &ksm->slots[slot];
  424. if (atomic_read(&slotp->slot_refs) != 0) {
  425. err = -EBUSY;
  426. goto out_unlock;
  427. }
  428. err = ksm->ksm_ll_ops.keyslot_evict(ksm, key, slot);
  429. if (err)
  430. goto out_unlock;
  431. hlist_del(&slotp->hash_node);
  432. memzero_explicit(&slotp->key, sizeof(slotp->key));
  433. err = 0;
  434. out_unlock:
  435. keyslot_manager_hw_exit(ksm);
  436. return err;
  437. }
  438. /**
  439. * keyslot_manager_reprogram_all_keys() - Re-program all keyslots.
  440. * @ksm: The keyslot manager
  441. *
  442. * Re-program all keyslots that are supposed to have a key programmed. This is
  443. * intended only for use by drivers for hardware that loses its keys on reset.
  444. *
  445. * Context: Process context. Takes and releases ksm->lock.
  446. */
  447. void keyslot_manager_reprogram_all_keys(struct keyslot_manager *ksm)
  448. {
  449. unsigned int slot;
  450. if (WARN_ON(keyslot_manager_is_passthrough(ksm)))
  451. return;
  452. /* This is for device initialization, so don't resume the device */
  453. down_write(&ksm->lock);
  454. for (slot = 0; slot < ksm->num_slots; slot++) {
  455. const struct keyslot *slotp = &ksm->slots[slot];
  456. int err;
  457. if (slotp->key.crypto_mode == BLK_ENCRYPTION_MODE_INVALID)
  458. continue;
  459. err = ksm->ksm_ll_ops.keyslot_program(ksm, &slotp->key, slot);
  460. WARN_ON(err);
  461. }
  462. up_write(&ksm->lock);
  463. }
  464. EXPORT_SYMBOL_GPL(keyslot_manager_reprogram_all_keys);
  465. /**
  466. * keyslot_manager_private() - return the private data stored with ksm
  467. * @ksm: The keyslot manager
  468. *
  469. * Returns the private data passed to the ksm when it was created.
  470. */
  471. void *keyslot_manager_private(struct keyslot_manager *ksm)
  472. {
  473. return ksm->ll_priv_data;
  474. }
  475. EXPORT_SYMBOL_GPL(keyslot_manager_private);
  476. void keyslot_manager_destroy(struct keyslot_manager *ksm)
  477. {
  478. if (ksm) {
  479. kvfree(ksm->slot_hashtable);
  480. memzero_explicit(ksm, struct_size(ksm, slots, ksm->num_slots));
  481. kvfree(ksm);
  482. }
  483. }
  484. EXPORT_SYMBOL_GPL(keyslot_manager_destroy);
  485. /**
  486. * keyslot_manager_create_passthrough() - Create a passthrough keyslot manager
  487. * @dev: Device for runtime power management (NULL if none)
  488. * @ksm_ll_ops: The struct keyslot_mgmt_ll_ops
  489. * @features: Bitmask of BLK_CRYPTO_FEATURE_* flags
  490. * @crypto_mode_supported: Bitmasks for supported encryption modes
  491. * @ll_priv_data: Private data passed as is to the functions in ksm_ll_ops.
  492. *
  493. * Allocate memory for and initialize a passthrough keyslot manager.
  494. * Called by e.g. storage drivers to set up a keyslot manager in their
  495. * request_queue, when the storage driver wants to manage its keys by itself.
  496. * This is useful for inline encryption hardware that don't have a small fixed
  497. * number of keyslots, and for layered devices.
  498. *
  499. * See keyslot_manager_create() for more details about the parameters.
  500. *
  501. * Context: This function may sleep
  502. * Return: Pointer to constructed keyslot manager or NULL on error.
  503. */
  504. struct keyslot_manager *keyslot_manager_create_passthrough(
  505. struct device *dev,
  506. const struct keyslot_mgmt_ll_ops *ksm_ll_ops,
  507. unsigned int features,
  508. const unsigned int crypto_mode_supported[BLK_ENCRYPTION_MODE_MAX],
  509. void *ll_priv_data)
  510. {
  511. struct keyslot_manager *ksm;
  512. ksm = kzalloc(sizeof(*ksm), GFP_KERNEL);
  513. if (!ksm)
  514. return NULL;
  515. ksm->ksm_ll_ops = *ksm_ll_ops;
  516. ksm->features = features;
  517. memcpy(ksm->crypto_mode_supported, crypto_mode_supported,
  518. sizeof(ksm->crypto_mode_supported));
  519. ksm->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
  520. ksm->ll_priv_data = ll_priv_data;
  521. keyslot_manager_set_dev(ksm, dev);
  522. init_rwsem(&ksm->lock);
  523. return ksm;
  524. }
  525. EXPORT_SYMBOL_GPL(keyslot_manager_create_passthrough);
  526. /**
  527. * keyslot_manager_intersect_modes() - restrict supported modes by child device
  528. * @parent: The keyslot manager for parent device
  529. * @child: The keyslot manager for child device, or NULL
  530. *
  531. * Clear any crypto mode support bits in @parent that aren't set in @child.
  532. * If @child is NULL, then all parent bits are cleared.
  533. *
  534. * Only use this when setting up the keyslot manager for a layered device,
  535. * before it's been exposed yet.
  536. */
  537. void keyslot_manager_intersect_modes(struct keyslot_manager *parent,
  538. const struct keyslot_manager *child)
  539. {
  540. if (child) {
  541. unsigned int i;
  542. parent->features &= child->features;
  543. parent->max_dun_bytes_supported =
  544. min(parent->max_dun_bytes_supported,
  545. child->max_dun_bytes_supported);
  546. for (i = 0; i < ARRAY_SIZE(child->crypto_mode_supported); i++) {
  547. parent->crypto_mode_supported[i] &=
  548. child->crypto_mode_supported[i];
  549. }
  550. } else {
  551. parent->features = 0;
  552. parent->max_dun_bytes_supported = 0;
  553. memset(parent->crypto_mode_supported, 0,
  554. sizeof(parent->crypto_mode_supported));
  555. }
  556. }
  557. EXPORT_SYMBOL_GPL(keyslot_manager_intersect_modes);
  558. /**
  559. * keyslot_manager_derive_raw_secret() - Derive software secret from wrapped key
  560. * @ksm: The keyslot manager
  561. * @wrapped_key: The wrapped key
  562. * @wrapped_key_size: Size of the wrapped key in bytes
  563. * @secret: (output) the software secret
  564. * @secret_size: (output) the number of secret bytes to derive
  565. *
  566. * Given a hardware-wrapped key, ask the hardware to derive a secret which
  567. * software can use for cryptographic tasks other than inline encryption. The
  568. * derived secret is guaranteed to be cryptographically isolated from the key
  569. * with which any inline encryption with this wrapped key would actually be
  570. * done. I.e., both will be derived from the unwrapped key.
  571. *
  572. * Return: 0 on success, -EOPNOTSUPP if hardware-wrapped keys are unsupported,
  573. * or another -errno code.
  574. */
  575. int keyslot_manager_derive_raw_secret(struct keyslot_manager *ksm,
  576. const u8 *wrapped_key,
  577. unsigned int wrapped_key_size,
  578. u8 *secret, unsigned int secret_size)
  579. {
  580. int err;
  581. if (ksm->ksm_ll_ops.derive_raw_secret) {
  582. keyslot_manager_hw_enter(ksm);
  583. err = ksm->ksm_ll_ops.derive_raw_secret(ksm, wrapped_key,
  584. wrapped_key_size,
  585. secret, secret_size);
  586. keyslot_manager_hw_exit(ksm);
  587. } else {
  588. err = -EOPNOTSUPP;
  589. }
  590. return err;
  591. }
  592. EXPORT_SYMBOL_GPL(keyslot_manager_derive_raw_secret);
  593. /**
  594. * ksm_lock() - set one-depth nesting of lock class
  595. * @flags: now, it's only support one depth
  596. *
  597. * Some scenarios ksm->lock will be nest such as DM stack layer,
  598. * although DM's is different with lower device driver's ksm->lock,
  599. * lockdep recognizes them as a same one, then will trigger deadlock
  600. * detection, set another lock sub-class could avoid it.
  601. *
  602. */
  603. inline void ksm_flock(struct keyslot_manager *ksm, unsigned int flags)
  604. {
  605. ksm->lock_flags = flags;
  606. }
  607. EXPORT_SYMBOL_GPL(ksm_flock);