algapi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
  208. unsigned int head);
  209. struct crypto_instance *crypto_alloc_instance(const char *name,
  210. struct crypto_alg *alg);
  211. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen);
  212. int crypto_enqueue_request(struct crypto_queue *queue,
  213. struct crypto_async_request *request);
  214. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue);
  215. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm);
  216. static inline unsigned int crypto_queue_len(struct crypto_queue *queue)
  217. {
  218. return queue->qlen;
  219. }
  220. /* These functions require the input/output to be aligned as u32. */
  221. void crypto_inc(u8 *a, unsigned int size);
  222. void crypto_xor(u8 *dst, const u8 *src, unsigned int size);
  223. int blkcipher_walk_done(struct blkcipher_desc *desc,
  224. struct blkcipher_walk *walk, int err);
  225. int blkcipher_walk_virt(struct blkcipher_desc *desc,
  226. struct blkcipher_walk *walk);
  227. int blkcipher_walk_phys(struct blkcipher_desc *desc,
  228. struct blkcipher_walk *walk);
  229. int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
  230. struct blkcipher_walk *walk,
  231. unsigned int blocksize);
  232. int blkcipher_aead_walk_virt_block(struct blkcipher_desc *desc,
  233. struct blkcipher_walk *walk,
  234. struct crypto_aead *tfm,
  235. unsigned int blocksize);
  236. int ablkcipher_walk_done(struct ablkcipher_request *req,
  237. struct ablkcipher_walk *walk, int err);
  238. int ablkcipher_walk_phys(struct ablkcipher_request *req,
  239. struct ablkcipher_walk *walk);
  240. void __ablkcipher_walk_complete(struct ablkcipher_walk *walk);
  241. static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
  242. {
  243. return PTR_ALIGN(crypto_tfm_ctx(tfm),
  244. crypto_tfm_alg_alignmask(tfm) + 1);
  245. }
  246. static inline struct crypto_instance *crypto_tfm_alg_instance(
  247. struct crypto_tfm *tfm)
  248. {
  249. return container_of(tfm->__crt_alg, struct crypto_instance, alg);
  250. }
  251. static inline void *crypto_instance_ctx(struct crypto_instance *inst)
  252. {
  253. return inst->__ctx;
  254. }
  255. static inline struct ablkcipher_alg *crypto_ablkcipher_alg(
  256. struct crypto_ablkcipher *tfm)
  257. {
  258. return &crypto_ablkcipher_tfm(tfm)->__crt_alg->cra_ablkcipher;
  259. }
  260. static inline void *crypto_ablkcipher_ctx(struct crypto_ablkcipher *tfm)
  261. {
  262. return crypto_tfm_ctx(&tfm->base);
  263. }
  264. static inline void *crypto_ablkcipher_ctx_aligned(struct crypto_ablkcipher *tfm)
  265. {
  266. return crypto_tfm_ctx_aligned(&tfm->base);
  267. }
  268. static inline struct crypto_blkcipher *crypto_spawn_blkcipher(
  269. struct crypto_spawn *spawn)
  270. {
  271. u32 type = CRYPTO_ALG_TYPE_BLKCIPHER;
  272. u32 mask = CRYPTO_ALG_TYPE_MASK;
  273. return __crypto_blkcipher_cast(crypto_spawn_tfm(spawn, type, mask));
  274. }
  275. static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
  276. {
  277. return crypto_tfm_ctx(&tfm->base);
  278. }
  279. static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
  280. {
  281. return crypto_tfm_ctx_aligned(&tfm->base);
  282. }
  283. static inline struct crypto_cipher *crypto_spawn_cipher(
  284. struct crypto_spawn *spawn)
  285. {
  286. u32 type = CRYPTO_ALG_TYPE_CIPHER;
  287. u32 mask = CRYPTO_ALG_TYPE_MASK;
  288. return __crypto_cipher_cast(crypto_spawn_tfm(spawn, type, mask));
  289. }
  290. static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
  291. {
  292. return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
  293. }
  294. static inline struct crypto_hash *crypto_spawn_hash(struct crypto_spawn *spawn)
  295. {
  296. u32 type = CRYPTO_ALG_TYPE_HASH;
  297. u32 mask = CRYPTO_ALG_TYPE_HASH_MASK;
  298. return __crypto_hash_cast(crypto_spawn_tfm(spawn, type, mask));
  299. }
  300. static inline void *crypto_hash_ctx(struct crypto_hash *tfm)
  301. {
  302. return crypto_tfm_ctx(&tfm->base);
  303. }
  304. static inline void *crypto_hash_ctx_aligned(struct crypto_hash *tfm)
  305. {
  306. return crypto_tfm_ctx_aligned(&tfm->base);
  307. }
  308. static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
  309. struct scatterlist *dst,
  310. struct scatterlist *src,
  311. unsigned int nbytes)
  312. {
  313. walk->in.sg = src;
  314. walk->out.sg = dst;
  315. walk->total = nbytes;
  316. }
  317. static inline void ablkcipher_walk_init(struct ablkcipher_walk *walk,
  318. struct scatterlist *dst,
  319. struct scatterlist *src,
  320. unsigned int nbytes)
  321. {
  322. walk->in.sg = src;
  323. walk->out.sg = dst;
  324. walk->total = nbytes;
  325. INIT_LIST_HEAD(&walk->buffers);
  326. }
  327. static inline void ablkcipher_walk_complete(struct ablkcipher_walk *walk)
  328. {
  329. if (unlikely(!list_empty(&walk->buffers)))
  330. __ablkcipher_walk_complete(walk);
  331. }
  332. static inline struct crypto_async_request *crypto_get_backlog(
  333. struct crypto_queue *queue)
  334. {
  335. return queue->backlog == &queue->list ? NULL :
  336. container_of(queue->backlog, struct crypto_async_request, list);
  337. }
  338. static inline int ablkcipher_enqueue_request(struct crypto_queue *queue,
  339. struct ablkcipher_request *request)
  340. {
  341. return crypto_enqueue_request(queue, &request->base);
  342. }
  343. static inline struct ablkcipher_request *ablkcipher_dequeue_request(
  344. struct crypto_queue *queue)
  345. {
  346. return ablkcipher_request_cast(crypto_dequeue_request(queue));
  347. }
  348. static inline void *ablkcipher_request_ctx(struct ablkcipher_request *req)
  349. {
  350. return req->__ctx;
  351. }
  352. static inline int ablkcipher_tfm_in_queue(struct crypto_queue *queue,
  353. struct crypto_ablkcipher *tfm)
  354. {
  355. return crypto_tfm_in_queue(queue, crypto_ablkcipher_tfm(tfm));
  356. }
  357. static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
  358. u32 type, u32 mask)
  359. {
  360. return crypto_attr_alg(tb[1], type, mask);
  361. }
  362. /*
  363. * Returns CRYPTO_ALG_ASYNC if type/mask requires the use of sync algorithms.
  364. * Otherwise returns zero.
  365. */
  366. static inline int crypto_requires_sync(u32 type, u32 mask)
  367. {
  368. return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
  369. }
  370. noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
  371. /**
  372. * crypto_memneq - Compare two areas of memory without leaking
  373. * timing information.
  374. *
  375. * @a: One area of memory
  376. * @b: Another area of memory
  377. * @size: The size of the area.
  378. *
  379. * Returns 0 when data is equal, 1 otherwise.
  380. */
  381. static inline int crypto_memneq(const void *a, const void *b, size_t size)
  382. {
  383. return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
  384. }
  385. static inline void crypto_yield(u32 flags)
  386. {
  387. if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  388. cond_resched();
  389. }
  390. #endif /* _CRYPTO_ALGAPI_H */