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 11 #include <crypto/algapi.h> 12 #include <linux/completion.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/sched.h> 21 #include <linux/types.h> 22 23 struct crypto_instance; 24 struct crypto_template; 25 26 struct crypto_larval { 27 struct crypto_alg alg; 28 struct crypto_alg *adult; 29 struct completion completion; 30 u32 mask; 31 bool test_started; 32 }; 33 34 enum { 35 CRYPTOA_UNSPEC, 36 CRYPTOA_ALG, 37 CRYPTOA_TYPE, 38 __CRYPTOA_MAX, 39 }; 40 41 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) 42 43 /* Maximum number of (rtattr) parameters for each template. */ 44 #define CRYPTO_MAX_ATTRS 32 45 46 extern struct list_head crypto_alg_list; 47 extern struct rw_semaphore crypto_alg_sem; 48 extern struct blocking_notifier_head crypto_chain; 49 50 int alg_test(const char *driver, const char *alg, u32 type, u32 mask); 51 52 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS 53 static inline bool crypto_boot_test_finished(void) 54 { 55 return true; 56 } 57 static inline void set_crypto_boot_test_finished(void) 58 { 59 } 60 #else 61 DECLARE_STATIC_KEY_FALSE(__crypto_boot_test_finished); 62 static inline bool crypto_boot_test_finished(void) 63 { 64 return static_branch_likely(&__crypto_boot_test_finished); 65 } 66 static inline void set_crypto_boot_test_finished(void) 67 { 68 static_branch_enable(&__crypto_boot_test_finished); 69 } 70 #endif /* !CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */ 71 72 #ifdef CONFIG_PROC_FS 73 void __init crypto_init_proc(void); 74 void __exit crypto_exit_proc(void); 75 #else 76 static inline void crypto_init_proc(void) 77 { } 78 static inline void crypto_exit_proc(void) 79 { } 80 #endif 81 82 static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg) 83 { 84 return alg->cra_ctxsize; 85 } 86 87 static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg) 88 { 89 return alg->cra_ctxsize; 90 } 91 92 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg); 93 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask); 94 95 struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask); 96 void crypto_larval_kill(struct crypto_alg *alg); 97 void crypto_wait_for_test(struct crypto_larval *larval); 98 void crypto_alg_tested(const char *name, int err); 99 100 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, 101 struct crypto_alg *nalg); 102 void crypto_remove_final(struct list_head *list); 103 void crypto_shoot_alg(struct crypto_alg *alg); 104 struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type, 105 u32 mask); 106 void *crypto_create_tfm_node(struct crypto_alg *alg, 107 const struct crypto_type *frontend, int node); 108 109 static inline void *crypto_create_tfm(struct crypto_alg *alg, 110 const struct crypto_type *frontend) 111 { 112 return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE); 113 } 114 115 struct crypto_alg *crypto_find_alg(const char *alg_name, 116 const struct crypto_type *frontend, 117 u32 type, u32 mask); 118 119 void *crypto_alloc_tfm_node(const char *alg_name, 120 const struct crypto_type *frontend, u32 type, u32 mask, 121 int node); 122 123 static inline void *crypto_alloc_tfm(const char *alg_name, 124 const struct crypto_type *frontend, u32 type, u32 mask) 125 { 126 return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE); 127 } 128 129 int crypto_probing_notify(unsigned long val, void *v); 130 131 unsigned int crypto_alg_extsize(struct crypto_alg *alg); 132 133 int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, 134 u32 type, u32 mask); 135 136 static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) 137 { 138 refcount_inc(&alg->cra_refcnt); 139 return alg; 140 } 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 148 static inline int crypto_tmpl_get(struct crypto_template *tmpl) 149 { 150 return try_module_get(tmpl->module); 151 } 152 153 static inline void crypto_tmpl_put(struct crypto_template *tmpl) 154 { 155 module_put(tmpl->module); 156 } 157 158 static inline int crypto_is_larval(struct crypto_alg *alg) 159 { 160 return alg->cra_flags & CRYPTO_ALG_LARVAL; 161 } 162 163 static inline int crypto_is_dead(struct crypto_alg *alg) 164 { 165 return alg->cra_flags & CRYPTO_ALG_DEAD; 166 } 167 168 static inline int crypto_is_moribund(struct crypto_alg *alg) 169 { 170 return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING); 171 } 172 173 static inline void crypto_notify(unsigned long val, void *v) 174 { 175 blocking_notifier_call_chain(&crypto_chain, val, v); 176 } 177 178 static inline void crypto_yield(u32 flags) 179 { 180 if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) 181 cond_resched(); 182 } 183 184 static inline int crypto_is_test_larval(struct crypto_larval *larval) 185 { 186 return larval->alg.cra_driver_name[0]; 187 } 188 189 #endif /* _CRYPTO_INTERNAL_H */ 190 191