skcipher.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Symmetric key ciphers.
  4. *
  5. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #ifndef _CRYPTO_INTERNAL_SKCIPHER_H
  8. #define _CRYPTO_INTERNAL_SKCIPHER_H
  9. #include <crypto/algapi.h>
  10. #include <crypto/internal/cipher.h>
  11. #include <crypto/skcipher.h>
  12. #include <linux/list.h>
  13. #include <linux/types.h>
  14. /*
  15. * Set this if your algorithm is sync but needs a reqsize larger
  16. * than MAX_SYNC_SKCIPHER_REQSIZE.
  17. *
  18. * Reuse bit that is specific to hash algorithms.
  19. */
  20. #define CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE CRYPTO_ALG_OPTIONAL_KEY
  21. struct aead_request;
  22. struct rtattr;
  23. struct skcipher_instance {
  24. void (*free)(struct skcipher_instance *inst);
  25. union {
  26. struct {
  27. char head[offsetof(struct skcipher_alg, base)];
  28. struct crypto_instance base;
  29. } s;
  30. struct skcipher_alg alg;
  31. };
  32. };
  33. struct lskcipher_instance {
  34. void (*free)(struct lskcipher_instance *inst);
  35. union {
  36. struct {
  37. char head[offsetof(struct lskcipher_alg, co.base)];
  38. struct crypto_instance base;
  39. } s;
  40. struct lskcipher_alg alg;
  41. };
  42. };
  43. struct crypto_skcipher_spawn {
  44. struct crypto_spawn base;
  45. };
  46. struct crypto_lskcipher_spawn {
  47. struct crypto_spawn base;
  48. };
  49. struct skcipher_walk {
  50. union {
  51. struct {
  52. struct page *page;
  53. unsigned long offset;
  54. } phys;
  55. struct {
  56. u8 *page;
  57. void *addr;
  58. } virt;
  59. } src, dst;
  60. struct scatter_walk in;
  61. unsigned int nbytes;
  62. struct scatter_walk out;
  63. unsigned int total;
  64. struct list_head buffers;
  65. u8 *page;
  66. u8 *buffer;
  67. u8 *oiv;
  68. void *iv;
  69. unsigned int ivsize;
  70. int flags;
  71. unsigned int blocksize;
  72. unsigned int stride;
  73. unsigned int alignmask;
  74. };
  75. static inline struct crypto_instance *skcipher_crypto_instance(
  76. struct skcipher_instance *inst)
  77. {
  78. return &inst->s.base;
  79. }
  80. static inline struct crypto_instance *lskcipher_crypto_instance(
  81. struct lskcipher_instance *inst)
  82. {
  83. return &inst->s.base;
  84. }
  85. static inline struct skcipher_instance *skcipher_alg_instance(
  86. struct crypto_skcipher *skcipher)
  87. {
  88. return container_of(crypto_skcipher_alg(skcipher),
  89. struct skcipher_instance, alg);
  90. }
  91. static inline struct lskcipher_instance *lskcipher_alg_instance(
  92. struct crypto_lskcipher *lskcipher)
  93. {
  94. return container_of(crypto_lskcipher_alg(lskcipher),
  95. struct lskcipher_instance, alg);
  96. }
  97. static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
  98. {
  99. return crypto_instance_ctx(skcipher_crypto_instance(inst));
  100. }
  101. static inline void *lskcipher_instance_ctx(struct lskcipher_instance *inst)
  102. {
  103. return crypto_instance_ctx(lskcipher_crypto_instance(inst));
  104. }
  105. static inline void skcipher_request_complete(struct skcipher_request *req, int err)
  106. {
  107. crypto_request_complete(&req->base, err);
  108. }
  109. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
  110. struct crypto_instance *inst,
  111. const char *name, u32 type, u32 mask);
  112. int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn,
  113. struct crypto_instance *inst,
  114. const char *name, u32 type, u32 mask);
  115. static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
  116. {
  117. crypto_drop_spawn(&spawn->base);
  118. }
  119. static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn *spawn)
  120. {
  121. crypto_drop_spawn(&spawn->base);
  122. }
  123. static inline struct lskcipher_alg *crypto_lskcipher_spawn_alg(
  124. struct crypto_lskcipher_spawn *spawn)
  125. {
  126. return container_of(spawn->base.alg, struct lskcipher_alg, co.base);
  127. }
  128. static inline struct skcipher_alg_common *crypto_spawn_skcipher_alg_common(
  129. struct crypto_skcipher_spawn *spawn)
  130. {
  131. return container_of(spawn->base.alg, struct skcipher_alg_common, base);
  132. }
  133. static inline struct lskcipher_alg *crypto_spawn_lskcipher_alg(
  134. struct crypto_lskcipher_spawn *spawn)
  135. {
  136. return crypto_lskcipher_spawn_alg(spawn);
  137. }
  138. static inline struct crypto_skcipher *crypto_spawn_skcipher(
  139. struct crypto_skcipher_spawn *spawn)
  140. {
  141. return crypto_spawn_tfm2(&spawn->base);
  142. }
  143. static inline struct crypto_lskcipher *crypto_spawn_lskcipher(
  144. struct crypto_lskcipher_spawn *spawn)
  145. {
  146. return crypto_spawn_tfm2(&spawn->base);
  147. }
  148. static inline void crypto_skcipher_set_reqsize(
  149. struct crypto_skcipher *skcipher, unsigned int reqsize)
  150. {
  151. skcipher->reqsize = reqsize;
  152. }
  153. static inline void crypto_skcipher_set_reqsize_dma(
  154. struct crypto_skcipher *skcipher, unsigned int reqsize)
  155. {
  156. reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
  157. skcipher->reqsize = reqsize;
  158. }
  159. int crypto_register_skcipher(struct skcipher_alg *alg);
  160. void crypto_unregister_skcipher(struct skcipher_alg *alg);
  161. int crypto_register_skciphers(struct skcipher_alg *algs, int count);
  162. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
  163. int skcipher_register_instance(struct crypto_template *tmpl,
  164. struct skcipher_instance *inst);
  165. int crypto_register_lskcipher(struct lskcipher_alg *alg);
  166. void crypto_unregister_lskcipher(struct lskcipher_alg *alg);
  167. int crypto_register_lskciphers(struct lskcipher_alg *algs, int count);
  168. void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count);
  169. int lskcipher_register_instance(struct crypto_template *tmpl,
  170. struct lskcipher_instance *inst);
  171. int skcipher_walk_done(struct skcipher_walk *walk, int err);
  172. int skcipher_walk_virt(struct skcipher_walk *walk,
  173. struct skcipher_request *req,
  174. bool atomic);
  175. int skcipher_walk_async(struct skcipher_walk *walk,
  176. struct skcipher_request *req);
  177. int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
  178. struct aead_request *req, bool atomic);
  179. int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
  180. struct aead_request *req, bool atomic);
  181. void skcipher_walk_complete(struct skcipher_walk *walk, int err);
  182. static inline void skcipher_walk_abort(struct skcipher_walk *walk)
  183. {
  184. skcipher_walk_done(walk, -ECANCELED);
  185. }
  186. static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
  187. {
  188. return crypto_tfm_ctx(&tfm->base);
  189. }
  190. static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher *tfm)
  191. {
  192. return crypto_tfm_ctx(&tfm->base);
  193. }
  194. static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm)
  195. {
  196. return crypto_tfm_ctx_dma(&tfm->base);
  197. }
  198. static inline void *skcipher_request_ctx(struct skcipher_request *req)
  199. {
  200. return req->__ctx;
  201. }
  202. static inline void *skcipher_request_ctx_dma(struct skcipher_request *req)
  203. {
  204. unsigned int align = crypto_dma_align();
  205. if (align <= crypto_tfm_ctx_alignment())
  206. align = 1;
  207. return PTR_ALIGN(skcipher_request_ctx(req), align);
  208. }
  209. static inline u32 skcipher_request_flags(struct skcipher_request *req)
  210. {
  211. return req->base.flags;
  212. }
  213. /* Helpers for simple block cipher modes of operation */
  214. struct skcipher_ctx_simple {
  215. struct crypto_cipher *cipher; /* underlying block cipher */
  216. };
  217. static inline struct crypto_cipher *
  218. skcipher_cipher_simple(struct crypto_skcipher *tfm)
  219. {
  220. struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
  221. return ctx->cipher;
  222. }
  223. struct skcipher_instance *skcipher_alloc_instance_simple(
  224. struct crypto_template *tmpl, struct rtattr **tb);
  225. static inline struct crypto_alg *skcipher_ialg_simple(
  226. struct skcipher_instance *inst)
  227. {
  228. struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
  229. return crypto_spawn_cipher_alg(spawn);
  230. }
  231. static inline struct crypto_lskcipher *lskcipher_cipher_simple(
  232. struct crypto_lskcipher *tfm)
  233. {
  234. struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
  235. return *ctx;
  236. }
  237. struct lskcipher_instance *lskcipher_alloc_instance_simple(
  238. struct crypto_template *tmpl, struct rtattr **tb);
  239. static inline struct lskcipher_alg *lskcipher_ialg_simple(
  240. struct lskcipher_instance *inst)
  241. {
  242. struct crypto_lskcipher_spawn *spawn = lskcipher_instance_ctx(inst);
  243. return crypto_lskcipher_spawn_alg(spawn);
  244. }
  245. #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */