algapi.h 10 KB

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