algapi.h 10 KB

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