algapi.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Cryptographic API for algorithms (i.e., low-level API).
  4. *
  5. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  6. */
  7. #ifndef _CRYPTO_ALGAPI_H
  8. #define _CRYPTO_ALGAPI_H
  9. #include <crypto/utils.h>
  10. #include <linux/align.h>
  11. #include <linux/cache.h>
  12. #include <linux/crypto.h>
  13. #include <linux/list.h>
  14. #include <linux/types.h>
  15. #include <linux/workqueue.h>
  16. /*
  17. * Maximum values for blocksize and alignmask, used to allocate
  18. * static buffers that are big enough for any combination of
  19. * algs and architectures. Ciphers have a lower maximum size.
  20. */
  21. #define MAX_ALGAPI_BLOCKSIZE 160
  22. #define MAX_ALGAPI_ALIGNMASK 127
  23. #define MAX_CIPHER_BLOCKSIZE 16
  24. #define MAX_CIPHER_ALIGNMASK 15
  25. #ifdef ARCH_DMA_MINALIGN
  26. #define CRYPTO_DMA_ALIGN ARCH_DMA_MINALIGN
  27. #else
  28. #define CRYPTO_DMA_ALIGN CRYPTO_MINALIGN
  29. #endif
  30. #define CRYPTO_DMA_PADDING ((CRYPTO_DMA_ALIGN - 1) & ~(CRYPTO_MINALIGN - 1))
  31. /*
  32. * Autoloaded crypto modules should only use a prefixed name to avoid allowing
  33. * arbitrary modules to be loaded. Loading from userspace may still need the
  34. * unprefixed names, so retains those aliases as well.
  35. * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
  36. * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
  37. * expands twice on the same line. Instead, use a separate base name for the
  38. * alias.
  39. */
  40. #define MODULE_ALIAS_CRYPTO(name) \
  41. MODULE_INFO(alias, name); \
  42. MODULE_INFO(alias, "crypto-" name)
  43. struct crypto_aead;
  44. struct crypto_instance;
  45. struct module;
  46. struct notifier_block;
  47. struct rtattr;
  48. struct scatterlist;
  49. struct seq_file;
  50. struct sk_buff;
  51. union crypto_no_such_thing;
  52. struct crypto_instance {
  53. struct crypto_alg alg;
  54. struct crypto_template *tmpl;
  55. union {
  56. /* Node in list of instances after registration. */
  57. struct hlist_node list;
  58. /* List of attached spawns before registration. */
  59. struct crypto_spawn *spawns;
  60. };
  61. void *__ctx[] CRYPTO_MINALIGN_ATTR;
  62. };
  63. struct crypto_template {
  64. struct list_head list;
  65. struct hlist_head instances;
  66. struct hlist_head dead;
  67. struct module *module;
  68. struct work_struct free_work;
  69. int (*create)(struct crypto_template *tmpl, struct rtattr **tb);
  70. char name[CRYPTO_MAX_ALG_NAME];
  71. };
  72. struct crypto_spawn {
  73. struct list_head list;
  74. struct crypto_alg *alg;
  75. union {
  76. /* Back pointer to instance after registration.*/
  77. struct crypto_instance *inst;
  78. /* Spawn list pointer prior to registration. */
  79. struct crypto_spawn *next;
  80. };
  81. const struct crypto_type *frontend;
  82. u32 mask;
  83. bool dead;
  84. bool registered;
  85. };
  86. struct crypto_queue {
  87. struct list_head list;
  88. struct list_head *backlog;
  89. unsigned int qlen;
  90. unsigned int max_qlen;
  91. };
  92. struct scatter_walk {
  93. /* Must be the first member, see struct skcipher_walk. */
  94. union {
  95. void *const addr;
  96. /* Private API field, do not touch. */
  97. union crypto_no_such_thing *__addr;
  98. };
  99. struct scatterlist *sg;
  100. unsigned int offset;
  101. };
  102. struct crypto_attr_alg {
  103. char name[CRYPTO_MAX_ALG_NAME];
  104. };
  105. struct crypto_attr_type {
  106. u32 type;
  107. u32 mask;
  108. };
  109. /*
  110. * Algorithm registration interface.
  111. */
  112. int crypto_register_alg(struct crypto_alg *alg);
  113. void crypto_unregister_alg(struct crypto_alg *alg);
  114. int crypto_register_algs(struct crypto_alg *algs, int count);
  115. void crypto_unregister_algs(struct crypto_alg *algs, int count);
  116. void crypto_mod_put(struct crypto_alg *alg);
  117. int crypto_register_template(struct crypto_template *tmpl);
  118. int crypto_register_templates(struct crypto_template *tmpls, int count);
  119. void crypto_unregister_template(struct crypto_template *tmpl);
  120. void crypto_unregister_templates(struct crypto_template *tmpls, int count);
  121. struct crypto_template *crypto_lookup_template(const char *name);
  122. int crypto_register_instance(struct crypto_template *tmpl,
  123. struct crypto_instance *inst);
  124. void crypto_unregister_instance(struct crypto_instance *inst);
  125. int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
  126. const char *name, u32 type, u32 mask);
  127. void crypto_drop_spawn(struct crypto_spawn *spawn);
  128. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  129. u32 mask);
  130. void *crypto_spawn_tfm2(struct crypto_spawn *spawn);
  131. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
  132. int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret);
  133. const char *crypto_attr_alg_name(struct rtattr *rta);
  134. int __crypto_inst_setname(struct crypto_instance *inst, const char *name,
  135. const char *driver, struct crypto_alg *alg);
  136. #define crypto_inst_setname(inst, name, ...) \
  137. CONCATENATE(crypto_inst_setname_, COUNT_ARGS(__VA_ARGS__))( \
  138. inst, name, ##__VA_ARGS__)
  139. #define crypto_inst_setname_1(inst, name, alg) \
  140. __crypto_inst_setname(inst, name, name, alg)
  141. #define crypto_inst_setname_2(inst, name, driver, alg) \
  142. __crypto_inst_setname(inst, name, driver, alg)
  143. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen);
  144. int crypto_enqueue_request(struct crypto_queue *queue,
  145. struct crypto_async_request *request);
  146. void crypto_enqueue_request_head(struct crypto_queue *queue,
  147. struct crypto_async_request *request);
  148. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue);
  149. static inline unsigned int crypto_queue_len(struct crypto_queue *queue)
  150. {
  151. return queue->qlen;
  152. }
  153. void crypto_inc(u8 *a, unsigned int size);
  154. static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
  155. {
  156. return tfm->__crt_ctx;
  157. }
  158. static inline void *crypto_tfm_ctx_align(struct crypto_tfm *tfm,
  159. unsigned int align)
  160. {
  161. if (align <= crypto_tfm_ctx_alignment())
  162. align = 1;
  163. return PTR_ALIGN(crypto_tfm_ctx(tfm), align);
  164. }
  165. static inline unsigned int crypto_dma_align(void)
  166. {
  167. return CRYPTO_DMA_ALIGN;
  168. }
  169. static inline unsigned int crypto_dma_padding(void)
  170. {
  171. return (crypto_dma_align() - 1) & ~(crypto_tfm_ctx_alignment() - 1);
  172. }
  173. static inline void *crypto_tfm_ctx_dma(struct crypto_tfm *tfm)
  174. {
  175. return crypto_tfm_ctx_align(tfm, crypto_dma_align());
  176. }
  177. static inline struct crypto_instance *crypto_tfm_alg_instance(
  178. struct crypto_tfm *tfm)
  179. {
  180. return container_of(tfm->__crt_alg, struct crypto_instance, alg);
  181. }
  182. static inline void *crypto_instance_ctx(struct crypto_instance *inst)
  183. {
  184. return inst->__ctx;
  185. }
  186. static inline struct crypto_async_request *crypto_get_backlog(
  187. struct crypto_queue *queue)
  188. {
  189. return queue->backlog == &queue->list ? NULL :
  190. container_of(queue->backlog, struct crypto_async_request, list);
  191. }
  192. static inline u32 crypto_requires_off(struct crypto_attr_type *algt, u32 off)
  193. {
  194. return (algt->type ^ off) & algt->mask & off;
  195. }
  196. /*
  197. * When an algorithm uses another algorithm (e.g., if it's an instance of a
  198. * template), these are the flags that should always be set on the "outer"
  199. * algorithm if any "inner" algorithm has them set.
  200. */
  201. #define CRYPTO_ALG_INHERITED_FLAGS \
  202. (CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK | \
  203. CRYPTO_ALG_ALLOCATES_MEMORY)
  204. /*
  205. * Given the type and mask that specify the flags restrictions on a template
  206. * instance being created, return the mask that should be passed to
  207. * crypto_grab_*() (along with type=0) to honor any request the user made to
  208. * have any of the CRYPTO_ALG_INHERITED_FLAGS clear.
  209. */
  210. static inline u32 crypto_algt_inherited_mask(struct crypto_attr_type *algt)
  211. {
  212. return crypto_requires_off(algt, CRYPTO_ALG_INHERITED_FLAGS);
  213. }
  214. int crypto_register_notifier(struct notifier_block *nb);
  215. int crypto_unregister_notifier(struct notifier_block *nb);
  216. /* Crypto notification events. */
  217. enum {
  218. CRYPTO_MSG_ALG_REQUEST,
  219. CRYPTO_MSG_ALG_REGISTER,
  220. CRYPTO_MSG_ALG_LOADED,
  221. };
  222. static inline void crypto_request_complete(struct crypto_async_request *req,
  223. int err)
  224. {
  225. req->complete(req->data, err);
  226. }
  227. static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
  228. {
  229. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
  230. }
  231. static inline bool crypto_tfm_req_virt(struct crypto_tfm *tfm)
  232. {
  233. return tfm->__crt_alg->cra_flags & CRYPTO_ALG_REQ_VIRT;
  234. }
  235. static inline u32 crypto_request_flags(struct crypto_async_request *req)
  236. {
  237. return req->flags & ~CRYPTO_TFM_REQ_ON_STACK;
  238. }
  239. #endif /* _CRYPTO_ALGAPI_H */