internal.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 akcipher_request;
  24. struct crypto_akcipher;
  25. struct crypto_instance;
  26. struct crypto_template;
  27. struct crypto_larval {
  28. struct crypto_alg alg;
  29. struct crypto_alg *adult;
  30. struct completion completion;
  31. u32 mask;
  32. bool test_started;
  33. };
  34. struct crypto_akcipher_sync_data {
  35. struct crypto_akcipher *tfm;
  36. const void *src;
  37. void *dst;
  38. unsigned int slen;
  39. unsigned int dlen;
  40. struct akcipher_request *req;
  41. struct crypto_wait cwait;
  42. struct scatterlist sg;
  43. u8 *buf;
  44. };
  45. enum {
  46. CRYPTOA_UNSPEC,
  47. CRYPTOA_ALG,
  48. CRYPTOA_TYPE,
  49. __CRYPTOA_MAX,
  50. };
  51. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  52. /* Maximum number of (rtattr) parameters for each template. */
  53. #define CRYPTO_MAX_ATTRS 32
  54. extern struct list_head crypto_alg_list;
  55. extern struct rw_semaphore crypto_alg_sem;
  56. extern struct blocking_notifier_head crypto_chain;
  57. int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
  58. #if !IS_BUILTIN(CONFIG_CRYPTO_ALGAPI) || \
  59. IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)
  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_MANAGER_DISABLE_TESTS)
  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. int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data);
  115. int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data, int err);
  116. int crypto_init_akcipher_ops_sig(struct crypto_tfm *tfm);
  117. static inline void *crypto_create_tfm(struct crypto_alg *alg,
  118. const struct crypto_type *frontend)
  119. {
  120. return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE);
  121. }
  122. struct crypto_alg *crypto_find_alg(const char *alg_name,
  123. const struct crypto_type *frontend,
  124. u32 type, u32 mask);
  125. void *crypto_alloc_tfm_node(const char *alg_name,
  126. const struct crypto_type *frontend, u32 type, u32 mask,
  127. int node);
  128. static inline void *crypto_alloc_tfm(const char *alg_name,
  129. const struct crypto_type *frontend, u32 type, u32 mask)
  130. {
  131. return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE);
  132. }
  133. int crypto_probing_notify(unsigned long val, void *v);
  134. unsigned int crypto_alg_extsize(struct crypto_alg *alg);
  135. int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
  136. u32 type, u32 mask);
  137. static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
  138. {
  139. refcount_inc(&alg->cra_refcnt);
  140. return alg;
  141. }
  142. static inline void crypto_alg_put(struct crypto_alg *alg)
  143. {
  144. if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
  145. alg->cra_destroy(alg);
  146. }
  147. static inline int crypto_tmpl_get(struct crypto_template *tmpl)
  148. {
  149. return try_module_get(tmpl->module);
  150. }
  151. static inline void crypto_tmpl_put(struct crypto_template *tmpl)
  152. {
  153. module_put(tmpl->module);
  154. }
  155. static inline int crypto_is_larval(struct crypto_alg *alg)
  156. {
  157. return alg->cra_flags & CRYPTO_ALG_LARVAL;
  158. }
  159. static inline int crypto_is_dead(struct crypto_alg *alg)
  160. {
  161. return alg->cra_flags & CRYPTO_ALG_DEAD;
  162. }
  163. static inline int crypto_is_moribund(struct crypto_alg *alg)
  164. {
  165. return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING);
  166. }
  167. static inline void crypto_notify(unsigned long val, void *v)
  168. {
  169. blocking_notifier_call_chain(&crypto_chain, val, v);
  170. }
  171. static inline void crypto_yield(u32 flags)
  172. {
  173. if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  174. cond_resched();
  175. }
  176. static inline int crypto_is_test_larval(struct crypto_larval *larval)
  177. {
  178. return larval->alg.cra_driver_name[0];
  179. }
  180. static inline struct crypto_tfm *crypto_tfm_get(struct crypto_tfm *tfm)
  181. {
  182. return refcount_inc_not_zero(&tfm->refcnt) ? tfm : ERR_PTR(-EOVERFLOW);
  183. }
  184. #endif /* _CRYPTO_INTERNAL_H */