skcipher.h 7.8 KB

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