trusted_caam.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 Pengutronix, Ahmad Fatoum <[email protected]>
  4. */
  5. #include <keys/trusted_caam.h>
  6. #include <keys/trusted-type.h>
  7. #include <linux/build_bug.h>
  8. #include <linux/key-type.h>
  9. #include <soc/fsl/caam-blob.h>
  10. static struct caam_blob_priv *blobifier;
  11. #define KEYMOD "SECURE_KEY"
  12. static_assert(MAX_KEY_SIZE + CAAM_BLOB_OVERHEAD <= CAAM_BLOB_MAX_LEN);
  13. static_assert(MAX_BLOB_SIZE <= CAAM_BLOB_MAX_LEN);
  14. static int trusted_caam_seal(struct trusted_key_payload *p, char *datablob)
  15. {
  16. int ret;
  17. struct caam_blob_info info = {
  18. .input = p->key, .input_len = p->key_len,
  19. .output = p->blob, .output_len = MAX_BLOB_SIZE,
  20. .key_mod = KEYMOD, .key_mod_len = sizeof(KEYMOD) - 1,
  21. };
  22. ret = caam_encap_blob(blobifier, &info);
  23. if (ret)
  24. return ret;
  25. p->blob_len = info.output_len;
  26. return 0;
  27. }
  28. static int trusted_caam_unseal(struct trusted_key_payload *p, char *datablob)
  29. {
  30. int ret;
  31. struct caam_blob_info info = {
  32. .input = p->blob, .input_len = p->blob_len,
  33. .output = p->key, .output_len = MAX_KEY_SIZE,
  34. .key_mod = KEYMOD, .key_mod_len = sizeof(KEYMOD) - 1,
  35. };
  36. ret = caam_decap_blob(blobifier, &info);
  37. if (ret)
  38. return ret;
  39. p->key_len = info.output_len;
  40. return 0;
  41. }
  42. static int trusted_caam_init(void)
  43. {
  44. int ret;
  45. blobifier = caam_blob_gen_init();
  46. if (IS_ERR(blobifier))
  47. return PTR_ERR(blobifier);
  48. ret = register_key_type(&key_type_trusted);
  49. if (ret)
  50. caam_blob_gen_exit(blobifier);
  51. return ret;
  52. }
  53. static void trusted_caam_exit(void)
  54. {
  55. unregister_key_type(&key_type_trusted);
  56. caam_blob_gen_exit(blobifier);
  57. }
  58. struct trusted_key_ops trusted_key_caam_ops = {
  59. .migratable = 0, /* non-migratable */
  60. .init = trusted_caam_init,
  61. .seal = trusted_caam_seal,
  62. .unseal = trusted_caam_unseal,
  63. .exit = trusted_caam_exit,
  64. };