internal.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  6. * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  7. */
  8. #ifndef _CRYPTO_INTERNAL_H
  9. #define _CRYPTO_INTERNAL_H
  10. #include <crypto/algapi.h>
  11. #include <linux/completion.h>
  12. #include <linux/err.h>
  13. #include <linux/jump_label.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/notifier.h>
  17. #include <linux/numa.h>
  18. #include <linux/refcount.h>
  19. #include <linux/rwsem.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/sched.h>
  22. #include <linux/types.h>
  23. struct crypto_instance;
  24. struct crypto_template;
  25. struct crypto_larval {
  26. struct crypto_alg alg;
  27. struct crypto_alg *adult;
  28. struct completion completion;
  29. u32 mask;
  30. bool test_started;
  31. };
  32. struct crypto_type {
  33. unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
  34. unsigned int (*extsize)(struct crypto_alg *alg);
  35. int (*init_tfm)(struct crypto_tfm *tfm);
  36. void (*show)(struct seq_file *m, struct crypto_alg *alg);
  37. int (*report)(struct sk_buff *skb, struct crypto_alg *alg);
  38. void (*free)(struct crypto_instance *inst);
  39. void (*destroy)(struct crypto_alg *alg);
  40. unsigned int type;
  41. unsigned int maskclear;
  42. unsigned int maskset;
  43. unsigned int tfmsize;
  44. unsigned int algsize;
  45. };
  46. enum {
  47. CRYPTOA_UNSPEC,
  48. CRYPTOA_ALG,
  49. CRYPTOA_TYPE,
  50. __CRYPTOA_MAX,
  51. };
  52. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  53. /* Maximum number of (rtattr) parameters for each template. */
  54. #define CRYPTO_MAX_ATTRS 32
  55. extern struct rw_semaphore crypto_alg_sem;
  56. extern struct list_head crypto_alg_list __guarded_by(&crypto_alg_sem);
  57. extern struct blocking_notifier_head crypto_chain;
  58. int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
  59. #if !IS_BUILTIN(CONFIG_CRYPTO_ALGAPI) || !IS_ENABLED(CONFIG_CRYPTO_SELFTESTS)
  60. static inline bool crypto_boot_test_finished(void)
  61. {
  62. return true;
  63. }
  64. static inline void set_crypto_boot_test_finished(void)
  65. {
  66. }
  67. #else
  68. DECLARE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
  69. static inline bool crypto_boot_test_finished(void)
  70. {
  71. return static_branch_likely(&__crypto_boot_test_finished);
  72. }
  73. static inline void set_crypto_boot_test_finished(void)
  74. {
  75. static_branch_enable(&__crypto_boot_test_finished);
  76. }
  77. #endif /* !IS_BUILTIN(CONFIG_CRYPTO_ALGAPI) ||
  78. * !IS_ENABLED(CONFIG_CRYPTO_SELFTESTS)
  79. */
  80. #ifdef CONFIG_PROC_FS
  81. void __init crypto_init_proc(void);
  82. void __exit crypto_exit_proc(void);
  83. #else
  84. static inline void crypto_init_proc(void)
  85. { }
  86. static inline void crypto_exit_proc(void)
  87. { }
  88. #endif
  89. static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg)
  90. {
  91. return alg->cra_ctxsize;
  92. }
  93. static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg)
  94. {
  95. return alg->cra_ctxsize;
  96. }
  97. struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
  98. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
  99. struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
  100. void crypto_schedule_test(struct crypto_larval *larval);
  101. void crypto_alg_tested(const char *name, int err);
  102. void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
  103. struct crypto_alg *nalg);
  104. void crypto_remove_final(struct list_head *list);
  105. void crypto_shoot_alg(struct crypto_alg *alg);
  106. struct crypto_tfm *__crypto_alloc_tfmgfp(struct crypto_alg *alg, u32 type,
  107. u32 mask, gfp_t gfp);
  108. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  109. u32 mask);
  110. void *crypto_create_tfm_node(struct crypto_alg *alg,
  111. const struct crypto_type *frontend, int node);
  112. void *crypto_clone_tfm(const struct crypto_type *frontend,
  113. struct crypto_tfm *otfm);
  114. static inline void *crypto_create_tfm(struct crypto_alg *alg,
  115. const struct crypto_type *frontend)
  116. {
  117. return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE);
  118. }
  119. struct crypto_alg *crypto_find_alg(const char *alg_name,
  120. const struct crypto_type *frontend,
  121. u32 type, u32 mask);
  122. void *crypto_alloc_tfm_node(const char *alg_name,
  123. const struct crypto_type *frontend, u32 type, u32 mask,
  124. int node);
  125. static inline void *crypto_alloc_tfm(const char *alg_name,
  126. const struct crypto_type *frontend, u32 type, u32 mask)
  127. {
  128. return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE);
  129. }
  130. int crypto_probing_notify(unsigned long val, void *v);
  131. unsigned int crypto_alg_extsize(struct crypto_alg *alg);
  132. int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
  133. u32 type, u32 mask);
  134. static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
  135. {
  136. refcount_inc(&alg->cra_refcnt);
  137. return alg;
  138. }
  139. void crypto_destroy_alg(struct crypto_alg *alg);
  140. static inline void crypto_alg_put(struct crypto_alg *alg)
  141. {
  142. if (refcount_dec_and_test(&alg->cra_refcnt))
  143. crypto_destroy_alg(alg);
  144. }
  145. static inline int crypto_tmpl_get(struct crypto_template *tmpl)
  146. {
  147. return try_module_get(tmpl->module);
  148. }
  149. static inline void crypto_tmpl_put(struct crypto_template *tmpl)
  150. {
  151. module_put(tmpl->module);
  152. }
  153. static inline int crypto_is_larval(struct crypto_alg *alg)
  154. {
  155. return alg->cra_flags & CRYPTO_ALG_LARVAL;
  156. }
  157. static inline int crypto_is_dead(struct crypto_alg *alg)
  158. {
  159. return alg->cra_flags & CRYPTO_ALG_DEAD;
  160. }
  161. static inline int crypto_is_moribund(struct crypto_alg *alg)
  162. {
  163. return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING);
  164. }
  165. static inline void crypto_notify(unsigned long val, void *v)
  166. {
  167. blocking_notifier_call_chain(&crypto_chain, val, v);
  168. }
  169. static inline void crypto_yield(u32 flags)
  170. {
  171. if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  172. cond_resched();
  173. }
  174. static inline int crypto_is_test_larval(struct crypto_larval *larval)
  175. {
  176. return larval->alg.cra_driver_name[0];
  177. }
  178. static inline struct crypto_tfm *crypto_tfm_get(struct crypto_tfm *tfm)
  179. {
  180. return refcount_inc_not_zero(&tfm->refcnt) ? tfm : ERR_PTR(-EOVERFLOW);
  181. }
  182. #endif /* _CRYPTO_INTERNAL_H */