algapi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/kthread.h>
  18. #include <linux/skbuff.h>
  19. struct crypto_aead;
  20. struct crypto_instance;
  21. struct module;
  22. struct rtattr;
  23. struct seq_file;
  24. struct crypto_type {
  25. unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
  26. unsigned int (*extsize)(struct crypto_alg *alg);
  27. int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
  28. int (*init_tfm)(struct crypto_tfm *tfm);
  29. void (*show)(struct seq_file *m, struct crypto_alg *alg);
  30. int (*report)(struct sk_buff *skb, struct crypto_alg *alg);
  31. struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask);
  32. void (*free)(struct crypto_instance *inst);
  33. unsigned int type;
  34. unsigned int maskclear;
  35. unsigned int maskset;
  36. unsigned int tfmsize;
  37. };
  38. struct crypto_instance {
  39. struct crypto_alg alg;
  40. struct crypto_template *tmpl;
  41. struct hlist_node list;
  42. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  43. };
  44. struct crypto_template {
  45. struct list_head list;
  46. struct hlist_head instances;
  47. struct module *module;
  48. struct crypto_instance *(*alloc)(struct rtattr **tb);
  49. void (*free)(struct crypto_instance *inst);
  50. int (*create)(struct crypto_template *tmpl, struct rtattr **tb);
  51. char name[CRYPTO_MAX_ALG_NAME];
  52. };
  53. struct crypto_spawn {
  54. struct list_head list;
  55. struct crypto_alg *alg;
  56. struct crypto_instance *inst;
  57. const struct crypto_type *frontend;
  58. u32 mask;
  59. };
  60. struct crypto_queue {
  61. struct list_head list;
  62. struct list_head *backlog;
  63. unsigned int qlen;
  64. unsigned int max_qlen;
  65. };
  66. struct scatter_walk {
  67. struct scatterlist *sg;
  68. unsigned int offset;
  69. };
  70. struct blkcipher_walk {
  71. union {
  72. struct {
  73. struct page *page;
  74. unsigned long offset;
  75. } phys;
  76. struct {
  77. u8 *page;
  78. u8 *addr;
  79. } virt;
  80. } src, dst;
  81. struct scatter_walk in;
  82. unsigned int nbytes;
  83. struct scatter_walk out;
  84. unsigned int total;
  85. void *page;
  86. u8 *buffer;
  87. u8 *iv;
  88. unsigned int ivsize;
  89. int flags;
  90. unsigned int walk_blocksize;
  91. unsigned int cipher_blocksize;
  92. unsigned int alignmask;
  93. };
  94. struct ablkcipher_walk {
  95. struct {
  96. struct page *page;
  97. unsigned int offset;
  98. } src, dst;
  99. struct scatter_walk in;
  100. unsigned int nbytes;
  101. struct scatter_walk out;
  102. unsigned int total;
  103. struct list_head buffers;
  104. u8 *iv_buffer;
  105. u8 *iv;
  106. int flags;
  107. unsigned int blocksize;
  108. };
  109. #define ENGINE_NAME_LEN 30
  110. /*
  111. * struct crypto_engine - crypto hardware engine
  112. * @name: the engine name
  113. * @idling: the engine is entering idle state
  114. * @busy: request pump is busy
  115. * @running: the engine is on working
  116. * @cur_req_prepared: current request is prepared
  117. * @list: link with the global crypto engine list
  118. * @queue_lock: spinlock to syncronise access to request queue
  119. * @queue: the crypto queue of the engine
  120. * @rt: whether this queue is set to run as a realtime task
  121. * @prepare_crypt_hardware: a request will soon arrive from the queue
  122. * so the subsystem requests the driver to prepare the hardware
  123. * by issuing this call
  124. * @unprepare_crypt_hardware: there are currently no more requests on the
  125. * queue so the subsystem notifies the driver that it may relax the
  126. * hardware by issuing this call
  127. * @prepare_request: do some prepare if need before handle the current request
  128. * @unprepare_request: undo any work done by prepare_message()
  129. * @crypt_one_request: do encryption for current request
  130. * @kworker: thread struct for request pump
  131. * @kworker_task: pointer to task for request pump kworker thread
  132. * @pump_requests: work struct for scheduling work to the request pump
  133. * @priv_data: the engine private data
  134. * @cur_req: the current request which is on processing
  135. */
  136. struct crypto_engine {
  137. char name[ENGINE_NAME_LEN];
  138. bool idling;
  139. bool busy;
  140. bool running;
  141. bool cur_req_prepared;
  142. struct list_head list;
  143. spinlock_t queue_lock;
  144. struct crypto_queue queue;
  145. bool rt;
  146. int (*prepare_crypt_hardware)(struct crypto_engine *engine);
  147. int (*unprepare_crypt_hardware)(struct crypto_engine *engine);
  148. int (*prepare_request)(struct crypto_engine *engine,
  149. struct ablkcipher_request *req);
  150. int (*unprepare_request)(struct crypto_engine *engine,
  151. struct ablkcipher_request *req);
  152. int (*crypt_one_request)(struct crypto_engine *engine,
  153. struct ablkcipher_request *req);
  154. struct kthread_worker kworker;
  155. struct task_struct *kworker_task;
  156. struct kthread_work pump_requests;
  157. void *priv_data;
  158. struct ablkcipher_request *cur_req;
  159. };
  160. int crypto_transfer_request(struct crypto_engine *engine,
  161. struct ablkcipher_request *req, bool need_pump);
  162. int crypto_transfer_request_to_engine(struct crypto_engine *engine,
  163. struct ablkcipher_request *req);
  164. void crypto_finalize_request(struct crypto_engine *engine,
  165. struct ablkcipher_request *req, int err);
  166. int crypto_engine_start(struct crypto_engine *engine);
  167. int crypto_engine_stop(struct crypto_engine *engine);
  168. struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt);
  169. int crypto_engine_exit(struct crypto_engine *engine);
  170. extern const struct crypto_type crypto_ablkcipher_type;
  171. extern const struct crypto_type crypto_blkcipher_type;
  172. void crypto_mod_put(struct crypto_alg *alg);
  173. int crypto_register_template(struct crypto_template *tmpl);
  174. void crypto_unregister_template(struct crypto_template *tmpl);
  175. struct crypto_template *crypto_lookup_template(const char *name);
  176. int crypto_register_instance(struct crypto_template *tmpl,
  177. struct crypto_instance *inst);
  178. int crypto_unregister_instance(struct crypto_instance *inst);
  179. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  180. struct crypto_instance *inst, u32 mask);
  181. int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
  182. struct crypto_instance *inst,
  183. const struct crypto_type *frontend);
  184. int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name,
  185. u32 type, u32 mask);
  186. void crypto_drop_spawn(struct crypto_spawn *spawn);
  187. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  188. u32 mask);
  189. void *crypto_spawn_tfm2(struct crypto_spawn *spawn);
  190. static inline void crypto_set_spawn(struct crypto_spawn *spawn,
  191. struct crypto_instance *inst)
  192. {
  193. spawn->inst = inst;
  194. }
  195. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
  196. int crypto_check_attr_type(struct rtattr **tb, u32 type);
  197. const char *crypto_attr_alg_name(struct rtattr *rta);
  198. struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
  199. const struct crypto_type *frontend,
  200. u32 type, u32 mask);
  201. static inline struct crypto_alg *crypto_attr_alg(struct rtattr *rta,
  202. u32 type, u32 mask)
  203. {
  204. return crypto_attr_alg2(rta, NULL, type, mask);
  205. }
  206. int crypto_attr_u32(struct rtattr *rta, u32 *num);
  207. int crypto_inst_setname(struct crypto_instance *inst, const char *name,
  208. struct crypto_alg *alg);
  209. void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
  210. unsigned int head);
  211. struct crypto_instance *crypto_alloc_instance(const char *name,
  212. struct crypto_alg *alg);
  213. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen);
  214. int crypto_enqueue_request(struct crypto_queue *queue,
  215. struct crypto_async_request *request);
  216. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue);
  217. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm);
  218. static inline unsigned int crypto_queue_len(struct crypto_queue *queue)
  219. {
  220. return queue->qlen;
  221. }
  222. /* These functions require the input/output to be aligned as u32. */
  223. void crypto_inc(u8 *a, unsigned int size);
  224. void crypto_xor(u8 *dst, const u8 *src, unsigned int size);
  225. int blkcipher_walk_done(struct blkcipher_desc *desc,
  226. struct blkcipher_walk *walk, int err);
  227. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  228. struct blkcipher_walk *walk);
  229. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  230. struct blkcipher_walk *walk);
  231. int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
  232. struct blkcipher_walk *walk,
  233. unsigned int blocksize);
  234. int blkcipher_aead_walk_virt_block(struct blkcipher_desc *desc,
  235. struct blkcipher_walk *walk,
  236. struct crypto_aead *tfm,
  237. unsigned int blocksize);
  238. int ablkcipher_walk_done(struct ablkcipher_request *req,
  239. struct ablkcipher_walk *walk, int err);
  240. int ablkcipher_walk_phys(struct ablkcipher_request *req,
  241. struct ablkcipher_walk *walk);
  242. void __ablkcipher_walk_complete(struct ablkcipher_walk *walk);
  243. static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
  244. {
  245. return PTR_ALIGN(crypto_tfm_ctx(tfm),
  246. crypto_tfm_alg_alignmask(tfm) + 1);
  247. }
  248. static inline struct crypto_instance *crypto_tfm_alg_instance(
  249. struct crypto_tfm *tfm)
  250. {
  251. return container_of(tfm->__crt_alg, struct crypto_instance, alg);
  252. }
  253. static inline void *crypto_instance_ctx(struct crypto_instance *inst)
  254. {
  255. return inst->__ctx;
  256. }
  257. static inline struct ablkcipher_alg *crypto_ablkcipher_alg(
  258. struct crypto_ablkcipher *tfm)
  259. {
  260. return &crypto_ablkcipher_tfm(tfm)->__crt_alg->cra_ablkcipher;
  261. }
  262. static inline void *crypto_ablkcipher_ctx(struct crypto_ablkcipher *tfm)
  263. {
  264. return crypto_tfm_ctx(&tfm->base);
  265. }
  266. static inline void *crypto_ablkcipher_ctx_aligned(struct crypto_ablkcipher *tfm)
  267. {
  268. return crypto_tfm_ctx_aligned(&tfm->base);
  269. }
  270. static inline struct crypto_blkcipher *crypto_spawn_blkcipher(
  271. struct crypto_spawn *spawn)
  272. {
  273. u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
  274. u32 mask = CRYPTO_ALG_TYPE_MASK;
  275. return __crypto_blkcipher_cast(crypto_spawn_tfm(spawn, type, mask));
  276. }
  277. static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
  278. {
  279. return crypto_tfm_ctx(&tfm->base);
  280. }
  281. static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
  282. {
  283. return crypto_tfm_ctx_aligned(&tfm->base);
  284. }
  285. static inline struct crypto_cipher *crypto_spawn_cipher(
  286. struct crypto_spawn *spawn)
  287. {
  288. u32 type = CRYPTO_ALG_TYPE_CIPHER;
  289. u32 mask = CRYPTO_ALG_TYPE_MASK;
  290. return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask));
  291. }
  292. static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
  293. {
  294. return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
  295. }
  296. static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
  297. struct scatterlist *dst,
  298. struct scatterlist *src,
  299. unsigned int nbytes)
  300. {
  301. walk->in.sg = src;
  302. walk->out.sg = dst;
  303. walk->total = nbytes;
  304. }
  305. static inline void ablkcipher_walk_init(struct ablkcipher_walk *walk,
  306. struct scatterlist *dst,
  307. struct scatterlist *src,
  308. unsigned int nbytes)
  309. {
  310. walk->in.sg = src;
  311. walk->out.sg = dst;
  312. walk->total = nbytes;
  313. INIT_LIST_HEAD(&walk->buffers);
  314. }
  315. static inline void ablkcipher_walk_complete(struct ablkcipher_walk *walk)
  316. {
  317. if (unlikely(!list_empty(&walk->buffers)))
  318. __ablkcipher_walk_complete(walk);
  319. }
  320. static inline struct crypto_async_request *crypto_get_backlog(
  321. struct crypto_queue *queue)
  322. {
  323. return queue->backlog == &queue->list ? NULL :
  324. container_of(queue->backlog, struct crypto_async_request, list);
  325. }
  326. static inline int ablkcipher_enqueue_request(struct crypto_queue *queue,
  327. struct ablkcipher_request *request)
  328. {
  329. return crypto_enqueue_request(queue, &request->base);
  330. }
  331. static inline struct ablkcipher_request *ablkcipher_dequeue_request(
  332. struct crypto_queue *queue)
  333. {
  334. return ablkcipher_request_cast(crypto_dequeue_request(queue));
  335. }
  336. static inline void *ablkcipher_request_ctx(struct ablkcipher_request *req)
  337. {
  338. return req->__ctx;
  339. }
  340. static inline int ablkcipher_tfm_in_queue(struct crypto_queue *queue,
  341. struct crypto_ablkcipher *tfm)
  342. {
  343. return crypto_tfm_in_queue(queue, crypto_ablkcipher_tfm(tfm));
  344. }
  345. static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
  346. u32 type, u32 mask)
  347. {
  348. return crypto_attr_alg(tb[1], type, mask);
  349. }
  350. /*
  351. * Returns CRYPTO_ALG_ASYNC if type/mask requires the use of sync algorithms.
  352. * Otherwise returns zero.
  353. */
  354. static inline int crypto_requires_sync(u32 type, u32 mask)
  355. {
  356. return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
  357. }
  358. noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
  359. /**
  360. * crypto_memneq - Compare two areas of memory without leaking
  361. * timing information.
  362. *
  363. * @a: One area of memory
  364. * @b: Another area of memory
  365. * @size: The size of the area.
  366. *
  367. * Returns 0 when data is equal, 1 otherwise.
  368. */
  369. static inline int crypto_memneq(const void *a, const void *b, size_t size)
  370. {
  371. return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
  372. }
  373. static inline void crypto_yield(u32 flags)
  374. {
  375. #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
  376. if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  377. cond_resched();
  378. #endif
  379. }
  380. #endif /* _CRYPTO_ALGAPI_H */