caam-blob.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2020 Pengutronix, Ahmad Fatoum <[email protected]>
  4. */
  5. #ifndef __CAAM_BLOB_GEN
  6. #define __CAAM_BLOB_GEN
  7. #include <linux/types.h>
  8. #include <linux/errno.h>
  9. #define CAAM_BLOB_KEYMOD_LENGTH 16
  10. #define CAAM_BLOB_OVERHEAD (32 + 16)
  11. #define CAAM_BLOB_MAX_LEN 4096
  12. struct caam_blob_priv;
  13. /**
  14. * struct caam_blob_info - information for CAAM blobbing
  15. * @input: pointer to input buffer (must be DMAable)
  16. * @input_len: length of @input buffer in bytes.
  17. * @output: pointer to output buffer (must be DMAable)
  18. * @output_len: length of @output buffer in bytes.
  19. * @key_mod: key modifier
  20. * @key_mod_len: length of @key_mod in bytes.
  21. * May not exceed %CAAM_BLOB_KEYMOD_LENGTH
  22. */
  23. struct caam_blob_info {
  24. void *input;
  25. size_t input_len;
  26. void *output;
  27. size_t output_len;
  28. const void *key_mod;
  29. size_t key_mod_len;
  30. };
  31. /**
  32. * caam_blob_gen_init - initialize blob generation
  33. * Return: pointer to new &struct caam_blob_priv instance on success
  34. * and ``ERR_PTR(-ENODEV)`` if CAAM has no hardware blobbing support
  35. * or no job ring could be allocated.
  36. */
  37. struct caam_blob_priv *caam_blob_gen_init(void);
  38. /**
  39. * caam_blob_gen_exit - free blob generation resources
  40. * @priv: instance returned by caam_blob_gen_init()
  41. */
  42. void caam_blob_gen_exit(struct caam_blob_priv *priv);
  43. /**
  44. * caam_process_blob - encapsulate or decapsulate blob
  45. * @priv: instance returned by caam_blob_gen_init()
  46. * @info: pointer to blobbing info describing key, blob and
  47. * key modifier buffers.
  48. * @encap: true for encapsulation, false for decapsulation
  49. *
  50. * Return: %0 and sets ``info->output_len`` on success and a negative
  51. * error code otherwise.
  52. */
  53. int caam_process_blob(struct caam_blob_priv *priv,
  54. struct caam_blob_info *info, bool encap);
  55. /**
  56. * caam_encap_blob - encapsulate blob
  57. * @priv: instance returned by caam_blob_gen_init()
  58. * @info: pointer to blobbing info describing input key,
  59. * output blob and key modifier buffers.
  60. *
  61. * Return: %0 and sets ``info->output_len`` on success and
  62. * a negative error code otherwise.
  63. */
  64. static inline int caam_encap_blob(struct caam_blob_priv *priv,
  65. struct caam_blob_info *info)
  66. {
  67. if (info->output_len < info->input_len + CAAM_BLOB_OVERHEAD)
  68. return -EINVAL;
  69. return caam_process_blob(priv, info, true);
  70. }
  71. /**
  72. * caam_decap_blob - decapsulate blob
  73. * @priv: instance returned by caam_blob_gen_init()
  74. * @info: pointer to blobbing info describing output key,
  75. * input blob and key modifier buffers.
  76. *
  77. * Return: %0 and sets ``info->output_len`` on success and
  78. * a negative error code otherwise.
  79. */
  80. static inline int caam_decap_blob(struct caam_blob_priv *priv,
  81. struct caam_blob_info *info)
  82. {
  83. if (info->input_len < CAAM_BLOB_OVERHEAD ||
  84. info->output_len < info->input_len - CAAM_BLOB_OVERHEAD)
  85. return -EINVAL;
  86. return caam_process_blob(priv, info, false);
  87. }
  88. #endif