skcipher.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Symmetric key ciphers.
  3. *
  4. * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #ifndef _CRYPTO_INTERNAL_SKCIPHER_H
  13. #define _CRYPTO_INTERNAL_SKCIPHER_H
  14. #include <crypto/algapi.h>
  15. #include <crypto/skcipher.h>
  16. #include <linux/list.h>
  17. #include <linux/types.h>
  18. struct aead_request;
  19. struct rtattr;
  20. struct skcipher_instance {
  21. void (*free)(struct skcipher_instance *inst);
  22. union {
  23. struct {
  24. char head[offsetof(struct skcipher_alg, base)];
  25. struct crypto_instance base;
  26. } s;
  27. struct skcipher_alg alg;
  28. };
  29. };
  30. struct crypto_skcipher_spawn {
  31. struct crypto_spawn base;
  32. };
  33. struct skcipher_walk {
  34. union {
  35. struct {
  36. struct page *page;
  37. unsigned long offset;
  38. } phys;
  39. struct {
  40. u8 *page;
  41. void *addr;
  42. } virt;
  43. } src, dst;
  44. struct scatter_walk in;
  45. unsigned int nbytes;
  46. struct scatter_walk out;
  47. unsigned int total;
  48. struct list_head buffers;
  49. u8 *page;
  50. u8 *buffer;
  51. u8 *oiv;
  52. void *iv;
  53. unsigned int ivsize;
  54. int flags;
  55. unsigned int blocksize;
  56. unsigned int stride;
  57. unsigned int alignmask;
  58. };
  59. extern const struct crypto_type crypto_givcipher_type;
  60. static inline struct crypto_instance *skcipher_crypto_instance(
  61. struct skcipher_instance *inst)
  62. {
  63. return &inst->s.base;
  64. }
  65. static inline struct skcipher_instance *skcipher_alg_instance(
  66. struct crypto_skcipher *skcipher)
  67. {
  68. return container_of(crypto_skcipher_alg(skcipher),
  69. struct skcipher_instance, alg);
  70. }
  71. static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
  72. {
  73. return crypto_instance_ctx(skcipher_crypto_instance(inst));
  74. }
  75. static inline void skcipher_request_complete(struct skcipher_request *req, int err)
  76. {
  77. req->base.complete(&req->base, err);
  78. }
  79. static inline void crypto_set_skcipher_spawn(
  80. struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst)
  81. {
  82. crypto_set_spawn(&spawn->base, inst);
  83. }
  84. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  85. u32 type, u32 mask);
  86. static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
  87. {
  88. crypto_drop_spawn(&spawn->base);
  89. }
  90. static inline struct skcipher_alg *crypto_skcipher_spawn_alg(
  91. struct crypto_skcipher_spawn *spawn)
  92. {
  93. return container_of(spawn->base.alg, struct skcipher_alg, base);
  94. }
  95. static inline struct skcipher_alg *crypto_spawn_skcipher_alg(
  96. struct crypto_skcipher_spawn *spawn)
  97. {
  98. return crypto_skcipher_spawn_alg(spawn);
  99. }
  100. static inline struct crypto_skcipher *crypto_spawn_skcipher(
  101. struct crypto_skcipher_spawn *spawn)
  102. {
  103. return crypto_spawn_tfm2(&spawn->base);
  104. }
  105. static inline void crypto_skcipher_set_reqsize(
  106. struct crypto_skcipher *skcipher, unsigned int reqsize)
  107. {
  108. skcipher->reqsize = reqsize;
  109. }
  110. int crypto_register_skcipher(struct skcipher_alg *alg);
  111. void crypto_unregister_skcipher(struct skcipher_alg *alg);
  112. int crypto_register_skciphers(struct skcipher_alg *algs, int count);
  113. void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
  114. int skcipher_register_instance(struct crypto_template *tmpl,
  115. struct skcipher_instance *inst);
  116. int skcipher_walk_done(struct skcipher_walk *walk, int err);
  117. int skcipher_walk_virt(struct skcipher_walk *walk,
  118. struct skcipher_request *req,
  119. bool atomic);
  120. void skcipher_walk_atomise(struct skcipher_walk *walk);
  121. int skcipher_walk_async(struct skcipher_walk *walk,
  122. struct skcipher_request *req);
  123. int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
  124. bool atomic);
  125. int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
  126. struct aead_request *req, bool atomic);
  127. int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
  128. struct aead_request *req, bool atomic);
  129. void skcipher_walk_complete(struct skcipher_walk *walk, int err);
  130. static inline void ablkcipher_request_complete(struct ablkcipher_request *req,
  131. int err)
  132. {
  133. req->base.complete(&req->base, err);
  134. }
  135. static inline u32 ablkcipher_request_flags(struct ablkcipher_request *req)
  136. {
  137. return req->base.flags;
  138. }
  139. static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
  140. {
  141. return crypto_tfm_ctx(&tfm->base);
  142. }
  143. static inline void *skcipher_request_ctx(struct skcipher_request *req)
  144. {
  145. return req->__ctx;
  146. }
  147. static inline u32 skcipher_request_flags(struct skcipher_request *req)
  148. {
  149. return req->base.flags;
  150. }
  151. static inline unsigned int crypto_skcipher_alg_min_keysize(
  152. struct skcipher_alg *alg)
  153. {
  154. if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  155. CRYPTO_ALG_TYPE_BLKCIPHER)
  156. return alg->base.cra_blkcipher.min_keysize;
  157. if (alg->base.cra_ablkcipher.encrypt)
  158. return alg->base.cra_ablkcipher.min_keysize;
  159. return alg->min_keysize;
  160. }
  161. static inline unsigned int crypto_skcipher_alg_max_keysize(
  162. struct skcipher_alg *alg)
  163. {
  164. if ((alg->base.cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  165. CRYPTO_ALG_TYPE_BLKCIPHER)
  166. return alg->base.cra_blkcipher.max_keysize;
  167. if (alg->base.cra_ablkcipher.encrypt)
  168. return alg->base.cra_ablkcipher.max_keysize;
  169. return alg->max_keysize;
  170. }
  171. #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */