internal.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Cryptographic API.
  4. *
  5. * Copyright (c) 2002 James Morris <[email protected]>
  6. * Copyright (c) 2005 Herbert Xu <[email protected]>
  7. */
  8. #ifndef _CRYPTO_INTERNAL_H
  9. #define _CRYPTO_INTERNAL_H
  10. #include <crypto/algapi.h>
  11. #include <linux/completion.h>
  12. #include <linux/jump_label.h>
  13. #include <linux/list.h>
  14. #include <linux/module.h>
  15. #include <linux/notifier.h>
  16. #include <linux/numa.h>
  17. #include <linux/refcount.h>
  18. #include <linux/rwsem.h>
  19. #include <linux/sched.h>
  20. #include <linux/types.h>
  21. struct crypto_instance;
  22. struct crypto_template;
  23. struct crypto_larval {
  24. struct crypto_alg alg;
  25. struct crypto_alg *adult;
  26. struct completion completion;
  27. u32 mask;
  28. bool test_started;
  29. };
  30. enum {
  31. CRYPTOA_UNSPEC,
  32. CRYPTOA_ALG,
  33. CRYPTOA_TYPE,
  34. __CRYPTOA_MAX,
  35. };
  36. #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
  37. /* Maximum number of (rtattr) parameters for each template. */
  38. #define CRYPTO_MAX_ATTRS 32
  39. extern struct list_head crypto_alg_list;
  40. extern struct rw_semaphore crypto_alg_sem;
  41. extern struct blocking_notifier_head crypto_chain;
  42. #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
  43. static inline bool crypto_boot_test_finished(void)
  44. {
  45. return true;
  46. }
  47. static inline void set_crypto_boot_test_finished(void)
  48. {
  49. }
  50. #else
  51. DECLARE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
  52. static inline bool crypto_boot_test_finished(void)
  53. {
  54. return static_branch_likely(&__crypto_boot_test_finished);
  55. }
  56. static inline void set_crypto_boot_test_finished(void)
  57. {
  58. static_branch_enable(&__crypto_boot_test_finished);
  59. }
  60. #endif /* !CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */
  61. #ifdef CONFIG_PROC_FS
  62. void __init crypto_init_proc(void);
  63. void __exit crypto_exit_proc(void);
  64. #else
  65. static inline void crypto_init_proc(void)
  66. { }
  67. static inline void crypto_exit_proc(void)
  68. { }
  69. #endif
  70. static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg)
  71. {
  72. return alg->cra_ctxsize;
  73. }
  74. static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg)
  75. {
  76. return alg->cra_ctxsize;
  77. }
  78. struct crypto_alg *crypto_mod_get(struct crypto_alg *alg);
  79. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask);
  80. struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask);
  81. void crypto_larval_kill(struct crypto_alg *alg);
  82. void crypto_wait_for_test(struct crypto_larval *larval);
  83. void crypto_alg_tested(const char *name, int err);
  84. void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
  85. struct crypto_alg *nalg);
  86. void crypto_remove_final(struct list_head *list);
  87. void crypto_shoot_alg(struct crypto_alg *alg);
  88. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  89. u32 mask);
  90. void *crypto_create_tfm_node(struct crypto_alg *alg,
  91. const struct crypto_type *frontend, int node);
  92. static inline void *crypto_create_tfm(struct crypto_alg *alg,
  93. const struct crypto_type *frontend)
  94. {
  95. return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE);
  96. }
  97. struct crypto_alg *crypto_find_alg(const char *alg_name,
  98. const struct crypto_type *frontend,
  99. u32 type, u32 mask);
  100. void *crypto_alloc_tfm_node(const char *alg_name,
  101. const struct crypto_type *frontend, u32 type, u32 mask,
  102. int node);
  103. static inline void *crypto_alloc_tfm(const char *alg_name,
  104. const struct crypto_type *frontend, u32 type, u32 mask)
  105. {
  106. return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE);
  107. }
  108. int crypto_probing_notify(unsigned long val, void *v);
  109. unsigned int crypto_alg_extsize(struct crypto_alg *alg);
  110. int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
  111. u32 type, u32 mask);
  112. static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
  113. {
  114. refcount_inc(&alg->cra_refcnt);
  115. return alg;
  116. }
  117. static inline void crypto_alg_put(struct crypto_alg *alg)
  118. {
  119. if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
  120. alg->cra_destroy(alg);
  121. }
  122. static inline int crypto_tmpl_get(struct crypto_template *tmpl)
  123. {
  124. return try_module_get(tmpl->module);
  125. }
  126. static inline void crypto_tmpl_put(struct crypto_template *tmpl)
  127. {
  128. module_put(tmpl->module);
  129. }
  130. static inline int crypto_is_larval(struct crypto_alg *alg)
  131. {
  132. return alg->cra_flags & CRYPTO_ALG_LARVAL;
  133. }
  134. static inline int crypto_is_dead(struct crypto_alg *alg)
  135. {
  136. return alg->cra_flags & CRYPTO_ALG_DEAD;
  137. }
  138. static inline int crypto_is_moribund(struct crypto_alg *alg)
  139. {
  140. return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING);
  141. }
  142. static inline void crypto_notify(unsigned long val, void *v)
  143. {
  144. blocking_notifier_call_chain(&crypto_chain, val, v);
  145. }
  146. static inline void crypto_yield(u32 flags)
  147. {
  148. if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
  149. cond_resched();
  150. }
  151. static inline int crypto_is_test_larval(struct crypto_larval *larval)
  152. {
  153. return larval->alg.cra_driver_name[0];
  154. }
  155. #endif /* _CRYPTO_INTERNAL_H */