kpp.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Key-agreement Protocol Primitives (KPP)
  4. *
  5. * Copyright (c) 2016, Intel Corporation
  6. * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
  7. */
  8. #ifndef _CRYPTO_KPP_INT_H
  9. #define _CRYPTO_KPP_INT_H
  10. #include <crypto/kpp.h>
  11. #include <crypto/algapi.h>
  12. /**
  13. * struct kpp_instance - KPP template instance
  14. * @free: Callback getting invoked upon instance destruction. Must be set.
  15. * @s: Internal. Generic crypto core instance state properly layout
  16. * to alias with @alg as needed.
  17. * @alg: The &struct kpp_alg implementation provided by the instance.
  18. */
  19. struct kpp_instance {
  20. void (*free)(struct kpp_instance *inst);
  21. union {
  22. struct {
  23. char head[offsetof(struct kpp_alg, base)];
  24. struct crypto_instance base;
  25. } s;
  26. struct kpp_alg alg;
  27. };
  28. };
  29. /**
  30. * struct crypto_kpp_spawn - KPP algorithm spawn
  31. * @base: Internal. Generic crypto core spawn state.
  32. *
  33. * Template instances can get a hold on some inner KPP algorithm by
  34. * binding a &struct crypto_kpp_spawn via
  35. * crypto_grab_kpp(). Transforms may subsequently get instantiated
  36. * from the referenced inner &struct kpp_alg by means of
  37. * crypto_spawn_kpp().
  38. */
  39. struct crypto_kpp_spawn {
  40. struct crypto_spawn base;
  41. };
  42. /*
  43. * Transform internal helpers.
  44. */
  45. static inline void *kpp_request_ctx(struct kpp_request *req)
  46. {
  47. return req->__ctx;
  48. }
  49. static inline void *kpp_request_ctx_dma(struct kpp_request *req)
  50. {
  51. unsigned int align = crypto_dma_align();
  52. if (align <= crypto_tfm_ctx_alignment())
  53. align = 1;
  54. return PTR_ALIGN(kpp_request_ctx(req), align);
  55. }
  56. static inline void kpp_set_reqsize(struct crypto_kpp *kpp,
  57. unsigned int reqsize)
  58. {
  59. kpp->reqsize = reqsize;
  60. }
  61. static inline void kpp_set_reqsize_dma(struct crypto_kpp *kpp,
  62. unsigned int reqsize)
  63. {
  64. reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
  65. kpp->reqsize = reqsize;
  66. }
  67. static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
  68. {
  69. return crypto_tfm_ctx(&tfm->base);
  70. }
  71. static inline void *kpp_tfm_ctx_dma(struct crypto_kpp *tfm)
  72. {
  73. return crypto_tfm_ctx_dma(&tfm->base);
  74. }
  75. static inline void kpp_request_complete(struct kpp_request *req, int err)
  76. {
  77. crypto_request_complete(&req->base, err);
  78. }
  79. static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
  80. {
  81. return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
  82. }
  83. /*
  84. * Template instance internal helpers.
  85. */
  86. /**
  87. * kpp_crypto_instance() - Cast a &struct kpp_instance to the corresponding
  88. * generic &struct crypto_instance.
  89. * @inst: Pointer to the &struct kpp_instance to be cast.
  90. * Return: A pointer to the &struct crypto_instance embedded in @inst.
  91. */
  92. static inline struct crypto_instance *kpp_crypto_instance(
  93. struct kpp_instance *inst)
  94. {
  95. return &inst->s.base;
  96. }
  97. /**
  98. * kpp_instance() - Cast a generic &struct crypto_instance to the corresponding
  99. * &struct kpp_instance.
  100. * @inst: Pointer to the &struct crypto_instance to be cast.
  101. * Return: A pointer to the &struct kpp_instance @inst is embedded in.
  102. */
  103. static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)
  104. {
  105. return container_of(inst, struct kpp_instance, s.base);
  106. }
  107. /**
  108. * kpp_alg_instance() - Get the &struct kpp_instance a given KPP transform has
  109. * been instantiated from.
  110. * @kpp: The KPP transform instantiated from some &struct kpp_instance.
  111. * Return: The &struct kpp_instance associated with @kpp.
  112. */
  113. static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)
  114. {
  115. return kpp_instance(crypto_tfm_alg_instance(&kpp->base));
  116. }
  117. /**
  118. * kpp_instance_ctx() - Get a pointer to a &struct kpp_instance's implementation
  119. * specific context data.
  120. * @inst: The &struct kpp_instance whose context data to access.
  121. *
  122. * A KPP template implementation may allocate extra memory beyond the
  123. * end of a &struct kpp_instance instantiated from &crypto_template.create().
  124. * This function provides a means to obtain a pointer to this area.
  125. *
  126. * Return: A pointer to the implementation specific context data.
  127. */
  128. static inline void *kpp_instance_ctx(struct kpp_instance *inst)
  129. {
  130. return crypto_instance_ctx(kpp_crypto_instance(inst));
  131. }
  132. /*
  133. * KPP algorithm (un)registration functions.
  134. */
  135. /**
  136. * crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
  137. *
  138. * Function registers an implementation of a key-agreement protocol primitive
  139. * algorithm
  140. *
  141. * @alg: algorithm definition
  142. *
  143. * Return: zero on success; error code in case of error
  144. */
  145. int crypto_register_kpp(struct kpp_alg *alg);
  146. /**
  147. * crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
  148. * algorithm
  149. *
  150. * Function unregisters an implementation of a key-agreement protocol primitive
  151. * algorithm
  152. *
  153. * @alg: algorithm definition
  154. */
  155. void crypto_unregister_kpp(struct kpp_alg *alg);
  156. /**
  157. * kpp_register_instance() - Register a KPP template instance.
  158. * @tmpl: The instantiating template.
  159. * @inst: The KPP template instance to be registered.
  160. * Return: %0 on success, negative error code otherwise.
  161. */
  162. int kpp_register_instance(struct crypto_template *tmpl,
  163. struct kpp_instance *inst);
  164. /*
  165. * KPP spawn related functions.
  166. */
  167. /**
  168. * crypto_grab_kpp() - Look up a KPP algorithm and bind a spawn to it.
  169. * @spawn: The KPP spawn to bind.
  170. * @inst: The template instance owning @spawn.
  171. * @name: The KPP algorithm name to look up.
  172. * @type: The type bitset to pass on to the lookup.
  173. * @mask: The mask bismask to pass on to the lookup.
  174. * Return: %0 on success, a negative error code otherwise.
  175. */
  176. int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
  177. struct crypto_instance *inst,
  178. const char *name, u32 type, u32 mask);
  179. /**
  180. * crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().
  181. * @spawn: The spawn to release.
  182. */
  183. static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)
  184. {
  185. crypto_drop_spawn(&spawn->base);
  186. }
  187. /**
  188. * crypto_spawn_kpp_alg() - Get the algorithm a KPP spawn has been bound to.
  189. * @spawn: The spawn to get the referenced &struct kpp_alg for.
  190. *
  191. * This function as well as the returned result are safe to use only
  192. * after @spawn has been successfully bound via crypto_grab_kpp() and
  193. * up to until the template instance owning @spawn has either been
  194. * registered successfully or the spawn has been released again via
  195. * crypto_drop_spawn().
  196. *
  197. * Return: A pointer to the &struct kpp_alg referenced from the spawn.
  198. */
  199. static inline struct kpp_alg *crypto_spawn_kpp_alg(
  200. struct crypto_kpp_spawn *spawn)
  201. {
  202. return container_of(spawn->base.alg, struct kpp_alg, base);
  203. }
  204. /**
  205. * crypto_spawn_kpp() - Create a transform from a KPP spawn.
  206. * @spawn: The spawn previously bound to some &struct kpp_alg via
  207. * crypto_grab_kpp().
  208. *
  209. * Once a &struct crypto_kpp_spawn has been successfully bound to a
  210. * &struct kpp_alg via crypto_grab_kpp(), transforms for the latter
  211. * may get instantiated from the former by means of this function.
  212. *
  213. * Return: A pointer to the freshly created KPP transform on success
  214. * or an ``ERR_PTR()`` otherwise.
  215. */
  216. static inline struct crypto_kpp *crypto_spawn_kpp(
  217. struct crypto_kpp_spawn *spawn)
  218. {
  219. return crypto_spawn_tfm2(&spawn->base);
  220. }
  221. #endif