algapi.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. unsigned int (*extsize)(struct crypto_alg *alg);
  23. int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
  24. int (*init_tfm)(struct crypto_tfm *tfm);
  25. void (*show)(struct seq_file *m, struct crypto_alg *alg);
  26. struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask);
  27. unsigned int type;
  28. unsigned int maskclear;
  29. unsigned int maskset;
  30. unsigned int tfmsize;
  31. };
  32. struct crypto_instance {
  33. struct crypto_alg alg;
  34. struct crypto_template *tmpl;
  35. struct hlist_node list;
  36. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  37. };
  38. struct crypto_template {
  39. struct list_head list;
  40. struct hlist_head instances;
  41. struct module *module;
  42. struct crypto_instance *(*alloc)(struct rtattr **tb);
  43. void (*free)(struct crypto_instance *inst);
  44. int (*create)(struct crypto_template *tmpl, struct rtattr **tb);
  45. char name[CRYPTO_MAX_ALG_NAME];
  46. };
  47. struct crypto_spawn {
  48. struct list_head list;
  49. struct crypto_alg *alg;
  50. struct crypto_instance *inst;
  51. const struct crypto_type *frontend;
  52. u32 mask;
  53. };
  54. struct crypto_queue {
  55. struct list_head list;
  56. struct list_head *backlog;
  57. unsigned int qlen;
  58. unsigned int max_qlen;
  59. };
  60. struct scatter_walk {
  61. struct scatterlist *sg;
  62. unsigned int offset;
  63. };
  64. struct blkcipher_walk {
  65. union {
  66. struct {
  67. struct page *page;
  68. unsigned long offset;
  69. } phys;
  70. struct {
  71. u8 *page;
  72. u8 *addr;
  73. } virt;
  74. } src, dst;
  75. struct scatter_walk in;
  76. unsigned int nbytes;
  77. struct scatter_walk out;
  78. unsigned int total;
  79. void *page;
  80. u8 *buffer;
  81. u8 *iv;
  82. int flags;
  83. unsigned int blocksize;
  84. };
  85. extern const struct crypto_type crypto_ablkcipher_type;
  86. extern const struct crypto_type crypto_aead_type;
  87. extern const struct crypto_type crypto_blkcipher_type;
  88. extern const struct crypto_type crypto_hash_type;
  89. void crypto_mod_put(struct crypto_alg *alg);
  90. int crypto_register_template(struct crypto_template *tmpl);
  91. void crypto_unregister_template(struct crypto_template *tmpl);
  92. struct crypto_template *crypto_lookup_template(const char *name);
  93. int crypto_register_instance(struct crypto_template *tmpl,
  94. struct crypto_instance *inst);
  95. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  96. struct crypto_instance *inst, u32 mask);
  97. int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
  98. struct crypto_instance *inst,
  99. const struct crypto_type *frontend);
  100. void crypto_drop_spawn(struct crypto_spawn *spawn);
  101. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  102. u32 mask);
  103. void *crypto_spawn_tfm2(struct crypto_spawn *spawn);
  104. static inline void crypto_set_spawn(struct crypto_spawn *spawn,
  105. struct crypto_instance *inst)
  106. {
  107. spawn->inst = inst;
  108. }
  109. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
  110. int crypto_check_attr_type(struct rtattr **tb, u32 type);
  111. const char *crypto_attr_alg_name(struct rtattr *rta);
  112. struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
  113. const struct crypto_type *frontend,
  114. u32 type, u32 mask);
  115. static inline struct crypto_alg *crypto_attr_alg(struct rtattr *rta,
  116. u32 type, u32 mask)
  117. {
  118. return crypto_attr_alg2(rta, NULL, type, mask);
  119. }
  120. int crypto_attr_u32(struct rtattr *rta, u32 *num);
  121. void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
  122. unsigned int head);
  123. struct crypto_instance *crypto_alloc_instance(const char *name,
  124. struct crypto_alg *alg);
  125. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen);
  126. int crypto_enqueue_request(struct crypto_queue *queue,
  127. struct crypto_async_request *request);
  128. void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset);
  129. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue);
  130. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm);
  131. /* These functions require the input/output to be aligned as u32. */
  132. void crypto_inc(u8 *a, unsigned int size);
  133. void crypto_xor(u8 *dst, const u8 *src, unsigned int size);
  134. int blkcipher_walk_done(struct blkcipher_desc *desc,
  135. struct blkcipher_walk *walk, int err);
  136. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  137. struct blkcipher_walk *walk);
  138. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  139. struct blkcipher_walk *walk);
  140. int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
  141. struct blkcipher_walk *walk,
  142. unsigned int blocksize);
  143. static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
  144. {
  145. return PTR_ALIGN(crypto_tfm_ctx(tfm),
  146. crypto_tfm_alg_alignmask(tfm) + 1);
  147. }
  148. static inline struct crypto_instance *crypto_tfm_alg_instance(
  149. struct crypto_tfm *tfm)
  150. {
  151. return container_of(tfm->__crt_alg, struct crypto_instance, alg);
  152. }
  153. static inline void *crypto_instance_ctx(struct crypto_instance *inst)
  154. {
  155. return inst->__ctx;
  156. }
  157. static inline struct ablkcipher_alg *crypto_ablkcipher_alg(
  158. struct crypto_ablkcipher *tfm)
  159. {
  160. return &crypto_ablkcipher_tfm(tfm)->__crt_alg->cra_ablkcipher;
  161. }
  162. static inline void *crypto_ablkcipher_ctx(struct crypto_ablkcipher *tfm)
  163. {
  164. return crypto_tfm_ctx(&tfm->base);
  165. }
  166. static inline void *crypto_ablkcipher_ctx_aligned(struct crypto_ablkcipher *tfm)
  167. {
  168. return crypto_tfm_ctx_aligned(&tfm->base);
  169. }
  170. static inline struct aead_alg *crypto_aead_alg(struct crypto_aead *tfm)
  171. {
  172. return &crypto_aead_tfm(tfm)->__crt_alg->cra_aead;
  173. }
  174. static inline void *crypto_aead_ctx(struct crypto_aead *tfm)
  175. {
  176. return crypto_tfm_ctx(&tfm->base);
  177. }
  178. static inline struct crypto_instance *crypto_aead_alg_instance(
  179. struct crypto_aead *aead)
  180. {
  181. return crypto_tfm_alg_instance(&aead->base);
  182. }
  183. static inline struct crypto_blkcipher *crypto_spawn_blkcipher(
  184. struct crypto_spawn *spawn)
  185. {
  186. u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
  187. u32 mask = CRYPTO_ALG_TYPE_MASK;
  188. return __crypto_blkcipher_cast(crypto_spawn_tfm(spawn, type, mask));
  189. }
  190. static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
  191. {
  192. return crypto_tfm_ctx(&tfm->base);
  193. }
  194. static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
  195. {
  196. return crypto_tfm_ctx_aligned(&tfm->base);
  197. }
  198. static inline struct crypto_cipher *crypto_spawn_cipher(
  199. struct crypto_spawn *spawn)
  200. {
  201. u32 type = CRYPTO_ALG_TYPE_CIPHER;
  202. u32 mask = CRYPTO_ALG_TYPE_MASK;
  203. return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask));
  204. }
  205. static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
  206. {
  207. return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
  208. }
  209. static inline struct crypto_hash *crypto_spawn_hash(struct crypto_spawn *spawn)
  210. {
  211. u32 type = CRYPTO_ALG_TYPE_HASH;
  212. u32 mask = CRYPTO_ALG_TYPE_HASH_MASK;
  213. return __crypto_hash_cast(crypto_spawn_tfm(spawn, type, mask));
  214. }
  215. static inline void *crypto_hash_ctx(struct crypto_hash *tfm)
  216. {
  217. return crypto_tfm_ctx(&tfm->base);
  218. }
  219. static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm)
  220. {
  221. return crypto_tfm_ctx_aligned(&tfm->base);
  222. }
  223. static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
  224. struct scatterlist *dst,
  225. struct scatterlist *src,
  226. unsigned int nbytes)
  227. {
  228. walk->in.sg = src;
  229. walk->out.sg = dst;
  230. walk->total = nbytes;
  231. }
  232. static inline struct crypto_async_request *crypto_get_backlog(
  233. struct crypto_queue *queue)
  234. {
  235. return queue->backlog == &queue->list ? NULL :
  236. container_of(queue->backlog, struct crypto_async_request, list);
  237. }
  238. static inline int ablkcipher_enqueue_request(struct crypto_queue *queue,
  239. struct ablkcipher_request *request)
  240. {
  241. return crypto_enqueue_request(queue, &request->base);
  242. }
  243. static inline struct ablkcipher_request *ablkcipher_dequeue_request(
  244. struct crypto_queue *queue)
  245. {
  246. return ablkcipher_request_cast(crypto_dequeue_request(queue));
  247. }
  248. static inline void *ablkcipher_request_ctx(struct ablkcipher_request *req)
  249. {
  250. return req->__ctx;
  251. }
  252. static inline int ablkcipher_tfm_in_queue(struct crypto_queue *queue,
  253. struct crypto_ablkcipher *tfm)
  254. {
  255. return crypto_tfm_in_queue(queue, crypto_ablkcipher_tfm(tfm));
  256. }
  257. static inline void *aead_request_ctx(struct aead_request *req)
  258. {
  259. return req->__ctx;
  260. }
  261. static inline void aead_request_complete(struct aead_request *req, int err)
  262. {
  263. req->base.complete(&req->base, err);
  264. }
  265. static inline u32 aead_request_flags(struct aead_request *req)
  266. {
  267. return req->base.flags;
  268. }
  269. static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
  270. u32 type, u32 mask)
  271. {
  272. return crypto_attr_alg(tb[1], type, mask);
  273. }
  274. /*
  275. * Returns CRYPTO_ALG_ASYNC if type/mask requires the use of sync algorithms.
  276. * Otherwise returns zero.
  277. */
  278. static inline int crypto_requires_sync(u32 type, u32 mask)
  279. {
  280. return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
  281. }
  282. #endif /* _CRYPTO_ALGAPI_H */