algapi.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Cryptographic API for algorithms (i.e., low-level API).
  3. *
  4. * Copyright (c) 2006 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_ALGAPI_H
  13. #define _CRYPTO_ALGAPI_H
  14. #include <linux/crypto.h>
  15. #include <linux/list.h>
  16. #include <linux/kernel.h>
  17. struct module;
  18. struct rtattr;
  19. struct seq_file;
  20. struct crypto_type {
  21. unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
  22. int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
  23. void (*exit)(struct crypto_tfm *tfm);
  24. void (*show)(struct seq_file *m, struct crypto_alg *alg);
  25. };
  26. struct crypto_instance {
  27. struct crypto_alg alg;
  28. struct crypto_template *tmpl;
  29. struct hlist_node list;
  30. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  31. };
  32. struct crypto_template {
  33. struct list_head list;
  34. struct hlist_head instances;
  35. struct module *module;
  36. struct crypto_instance *(*alloc)(struct rtattr **tb);
  37. void (*free)(struct crypto_instance *inst);
  38. char name[CRYPTO_MAX_ALG_NAME];
  39. };
  40. struct crypto_spawn {
  41. struct list_head list;
  42. struct crypto_alg *alg;
  43. struct crypto_instance *inst;
  44. u32 mask;
  45. };
  46. struct crypto_queue {
  47. struct list_head list;
  48. struct list_head *backlog;
  49. unsigned int qlen;
  50. unsigned int max_qlen;
  51. };
  52. struct scatter_walk {
  53. struct scatterlist *sg;
  54. unsigned int offset;
  55. };
  56. struct blkcipher_walk {
  57. union {
  58. struct {
  59. struct page *page;
  60. unsigned long offset;
  61. } phys;
  62. struct {
  63. u8 *page;
  64. u8 *addr;
  65. } virt;
  66. } src, dst;
  67. struct scatter_walk in;
  68. unsigned int nbytes;
  69. struct scatter_walk out;
  70. unsigned int total;
  71. void *page;
  72. u8 *buffer;
  73. u8 *iv;
  74. int flags;
  75. };
  76. extern const struct crypto_type crypto_ablkcipher_type;
  77. extern const struct crypto_type crypto_aead_type;
  78. extern const struct crypto_type crypto_blkcipher_type;
  79. extern const struct crypto_type crypto_hash_type;
  80. void crypto_mod_put(struct crypto_alg *alg);
  81. int crypto_register_template(struct crypto_template *tmpl);
  82. void crypto_unregister_template(struct crypto_template *tmpl);
  83. struct crypto_template *crypto_lookup_template(const char *name);
  84. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  85. struct crypto_instance *inst, u32 mask);
  86. void crypto_drop_spawn(struct crypto_spawn *spawn);
  87. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  88. u32 mask);
  89. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
  90. int crypto_check_attr_type(struct rtattr **tb, u32 type);
  91. struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask);
  92. int crypto_attr_u32(struct rtattr *rta, u32 *num);
  93. struct crypto_instance *crypto_alloc_instance(const char *name,
  94. struct crypto_alg *alg);
  95. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen);
  96. int crypto_enqueue_request(struct crypto_queue *queue,
  97. struct crypto_async_request *request);
  98. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue);
  99. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm);
  100. int blkcipher_walk_done(struct blkcipher_desc *desc,
  101. struct blkcipher_walk *walk, int err);
  102. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  103. struct blkcipher_walk *walk);
  104. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  105. struct blkcipher_walk *walk);
  106. static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
  107. {
  108. unsigned long addr = (unsigned long)crypto_tfm_ctx(tfm);
  109. unsigned long align = crypto_tfm_alg_alignmask(tfm);
  110. if (align <= crypto_tfm_ctx_alignment())
  111. align = 1;
  112. return (void *)ALIGN(addr, align);
  113. }
  114. static inline struct crypto_instance *crypto_tfm_alg_instance(
  115. struct crypto_tfm *tfm)
  116. {
  117. return container_of(tfm->__crt_alg, struct crypto_instance, alg);
  118. }
  119. static inline void *crypto_instance_ctx(struct crypto_instance *inst)
  120. {
  121. return inst->__ctx;
  122. }
  123. static inline struct ablkcipher_alg *crypto_ablkcipher_alg(
  124. struct crypto_ablkcipher *tfm)
  125. {
  126. return &crypto_ablkcipher_tfm(tfm)->__crt_alg->cra_ablkcipher;
  127. }
  128. static inline void *crypto_ablkcipher_ctx(struct crypto_ablkcipher *tfm)
  129. {
  130. return crypto_tfm_ctx(&tfm->base);
  131. }
  132. static inline void *crypto_ablkcipher_ctx_aligned(struct crypto_ablkcipher *tfm)
  133. {
  134. return crypto_tfm_ctx_aligned(&tfm->base);
  135. }
  136. static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
  137. {
  138. return &crypto_aead_tfm(tfm)->__crt_alg->cra_aead;
  139. }
  140. static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
  141. {
  142. return crypto_tfm_ctx(&tfm->base);
  143. }
  144. static inline struct crypto_instance *crypto_aead_alg_instance(
  145. struct crypto_aead *aead)
  146. {
  147. return crypto_tfm_alg_instance(&aead->base);
  148. }
  149. static inline struct crypto_ablkcipher *crypto_spawn_ablkcipher(
  150. struct crypto_spawn *spawn)
  151. {
  152. u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
  153. u32 mask = CRYPTO_ALG_TYPE_MASK;
  154. return __crypto_ablkcipher_cast(crypto_spawn_tfm(spawn, type, mask));
  155. }
  156. static inline struct crypto_blkcipher *crypto_spawn_blkcipher(
  157. struct crypto_spawn *spawn)
  158. {
  159. u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
  160. u32 mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC;
  161. return __crypto_blkcipher_cast(crypto_spawn_tfm(spawn, type, mask));
  162. }
  163. static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
  164. {
  165. return crypto_tfm_ctx(&tfm->base);
  166. }
  167. static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
  168. {
  169. return crypto_tfm_ctx_aligned(&tfm->base);
  170. }
  171. static inline struct crypto_cipher *crypto_spawn_cipher(
  172. struct crypto_spawn *spawn)
  173. {
  174. u32 type = CRYPTO_ALG_TYPE_CIPHER;
  175. u32 mask = CRYPTO_ALG_TYPE_MASK;
  176. return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask));
  177. }
  178. static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
  179. {
  180. return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
  181. }
  182. static inline struct crypto_hash *crypto_spawn_hash(struct crypto_spawn *spawn)
  183. {
  184. u32 type = CRYPTO_ALG_TYPE_HASH;
  185. u32 mask = CRYPTO_ALG_TYPE_HASH_MASK;
  186. return __crypto_hash_cast(crypto_spawn_tfm(spawn, type, mask));
  187. }
  188. static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm)
  189. {
  190. return crypto_tfm_ctx_aligned(&tfm->base);
  191. }
  192. static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
  193. struct scatterlist *dst,
  194. struct scatterlist *src,
  195. unsigned int nbytes)
  196. {
  197. walk->in.sg = src;
  198. walk->out.sg = dst;
  199. walk->total = nbytes;
  200. }
  201. static inline struct crypto_async_request *crypto_get_backlog(
  202. struct crypto_queue *queue)
  203. {
  204. return queue->backlog == &queue->list ? NULL :
  205. container_of(queue->backlog, struct crypto_async_request, list);
  206. }
  207. static inline int ablkcipher_enqueue_request(struct crypto_queue *queue,
  208. struct ablkcipher_request *request)
  209. {
  210. return crypto_enqueue_request(queue, &request->base);
  211. }
  212. static inline struct ablkcipher_request *ablkcipher_dequeue_request(
  213. struct crypto_queue *queue)
  214. {
  215. return ablkcipher_request_cast(crypto_dequeue_request(queue));
  216. }
  217. static inline void *ablkcipher_request_ctx(struct ablkcipher_request *req)
  218. {
  219. return req->__ctx;
  220. }
  221. static inline int ablkcipher_tfm_in_queue(struct crypto_queue *queue,
  222. struct crypto_ablkcipher *tfm)
  223. {
  224. return crypto_tfm_in_queue(queue, crypto_ablkcipher_tfm(tfm));
  225. }
  226. static inline void *aead_request_ctx(struct aead_request *req)
  227. {
  228. return req->__ctx;
  229. }
  230. static inline void aead_request_complete(struct aead_request *req, int err)
  231. {
  232. req->base.complete(&req->base, err);
  233. }
  234. static inline u32 aead_request_flags(struct aead_request *req)
  235. {
  236. return req->base.flags;
  237. }
  238. static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
  239. u32 type, u32 mask)
  240. {
  241. return crypto_attr_alg(tb[1], type, mask);
  242. }
  243. #endif /* _CRYPTO_ALGAPI_H */