kpp.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <[email protected]>
  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_set_reqsize(struct crypto_kpp *kpp,
  50. unsigned int reqsize)
  51. {
  52. crypto_kpp_alg(kpp)->reqsize = reqsize;
  53. }
  54. static inline void *kpp_tfm_ctx(struct crypto_kpp *tfm)
  55. {
  56. return tfm->base.__crt_ctx;
  57. }
  58. static inline void kpp_request_complete(struct kpp_request *req, int err)
  59. {
  60. req->base.complete(&req->base, err);
  61. }
  62. static inline const char *kpp_alg_name(struct crypto_kpp *tfm)
  63. {
  64. return crypto_kpp_tfm(tfm)->__crt_alg->cra_name;
  65. }
  66. /*
  67. * Template instance internal helpers.
  68. */
  69. /**
  70. * kpp_crypto_instance() - Cast a &struct kpp_instance to the corresponding
  71. * generic &struct crypto_instance.
  72. * @inst: Pointer to the &struct kpp_instance to be cast.
  73. * Return: A pointer to the &struct crypto_instance embedded in @inst.
  74. */
  75. static inline struct crypto_instance *kpp_crypto_instance(
  76. struct kpp_instance *inst)
  77. {
  78. return &inst->s.base;
  79. }
  80. /**
  81. * kpp_instance() - Cast a generic &struct crypto_instance to the corresponding
  82. * &struct kpp_instance.
  83. * @inst: Pointer to the &struct crypto_instance to be cast.
  84. * Return: A pointer to the &struct kpp_instance @inst is embedded in.
  85. */
  86. static inline struct kpp_instance *kpp_instance(struct crypto_instance *inst)
  87. {
  88. return container_of(inst, struct kpp_instance, s.base);
  89. }
  90. /**
  91. * kpp_alg_instance() - Get the &struct kpp_instance a given KPP transform has
  92. * been instantiated from.
  93. * @kpp: The KPP transform instantiated from some &struct kpp_instance.
  94. * Return: The &struct kpp_instance associated with @kpp.
  95. */
  96. static inline struct kpp_instance *kpp_alg_instance(struct crypto_kpp *kpp)
  97. {
  98. return kpp_instance(crypto_tfm_alg_instance(&kpp->base));
  99. }
  100. /**
  101. * kpp_instance_ctx() - Get a pointer to a &struct kpp_instance's implementation
  102. * specific context data.
  103. * @inst: The &struct kpp_instance whose context data to access.
  104. *
  105. * A KPP template implementation may allocate extra memory beyond the
  106. * end of a &struct kpp_instance instantiated from &crypto_template.create().
  107. * This function provides a means to obtain a pointer to this area.
  108. *
  109. * Return: A pointer to the implementation specific context data.
  110. */
  111. static inline void *kpp_instance_ctx(struct kpp_instance *inst)
  112. {
  113. return crypto_instance_ctx(kpp_crypto_instance(inst));
  114. }
  115. /*
  116. * KPP algorithm (un)registration functions.
  117. */
  118. /**
  119. * crypto_register_kpp() -- Register key-agreement protocol primitives algorithm
  120. *
  121. * Function registers an implementation of a key-agreement protocol primitive
  122. * algorithm
  123. *
  124. * @alg: algorithm definition
  125. *
  126. * Return: zero on success; error code in case of error
  127. */
  128. int crypto_register_kpp(struct kpp_alg *alg);
  129. /**
  130. * crypto_unregister_kpp() -- Unregister key-agreement protocol primitive
  131. * algorithm
  132. *
  133. * Function unregisters an implementation of a key-agreement protocol primitive
  134. * algorithm
  135. *
  136. * @alg: algorithm definition
  137. */
  138. void crypto_unregister_kpp(struct kpp_alg *alg);
  139. /**
  140. * kpp_register_instance() - Register a KPP template instance.
  141. * @tmpl: The instantiating template.
  142. * @inst: The KPP template instance to be registered.
  143. * Return: %0 on success, negative error code otherwise.
  144. */
  145. int kpp_register_instance(struct crypto_template *tmpl,
  146. struct kpp_instance *inst);
  147. /*
  148. * KPP spawn related functions.
  149. */
  150. /**
  151. * crypto_grab_kpp() - Look up a KPP algorithm and bind a spawn to it.
  152. * @spawn: The KPP spawn to bind.
  153. * @inst: The template instance owning @spawn.
  154. * @name: The KPP algorithm name to look up.
  155. * @type: The type bitset to pass on to the lookup.
  156. * @mask: The mask bismask to pass on to the lookup.
  157. * Return: %0 on success, a negative error code otherwise.
  158. */
  159. int crypto_grab_kpp(struct crypto_kpp_spawn *spawn,
  160. struct crypto_instance *inst,
  161. const char *name, u32 type, u32 mask);
  162. /**
  163. * crypto_drop_kpp() - Release a spawn previously bound via crypto_grab_kpp().
  164. * @spawn: The spawn to release.
  165. */
  166. static inline void crypto_drop_kpp(struct crypto_kpp_spawn *spawn)
  167. {
  168. crypto_drop_spawn(&spawn->base);
  169. }
  170. /**
  171. * crypto_spawn_kpp_alg() - Get the algorithm a KPP spawn has been bound to.
  172. * @spawn: The spawn to get the referenced &struct kpp_alg for.
  173. *
  174. * This function as well as the returned result are safe to use only
  175. * after @spawn has been successfully bound via crypto_grab_kpp() and
  176. * up to until the template instance owning @spawn has either been
  177. * registered successfully or the spawn has been released again via
  178. * crypto_drop_spawn().
  179. *
  180. * Return: A pointer to the &struct kpp_alg referenced from the spawn.
  181. */
  182. static inline struct kpp_alg *crypto_spawn_kpp_alg(
  183. struct crypto_kpp_spawn *spawn)
  184. {
  185. return container_of(spawn->base.alg, struct kpp_alg, base);
  186. }
  187. /**
  188. * crypto_spawn_kpp() - Create a transform from a KPP spawn.
  189. * @spawn: The spawn previously bound to some &struct kpp_alg via
  190. * crypto_grab_kpp().
  191. *
  192. * Once a &struct crypto_kpp_spawn has been successfully bound to a
  193. * &struct kpp_alg via crypto_grab_kpp(), transforms for the latter
  194. * may get instantiated from the former by means of this function.
  195. *
  196. * Return: A pointer to the freshly created KPP transform on success
  197. * or an ``ERR_PTR()`` otherwise.
  198. */
  199. static inline struct crypto_kpp *crypto_spawn_kpp(
  200. struct crypto_kpp_spawn *spawn)
  201. {
  202. return crypto_spawn_tfm2(&spawn->base);
  203. }
  204. #endif