algapi.h 10 KB

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