algapi.h 7.6 KB

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