algapi.h 11 KB

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