Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
1da177e4 LT |
2 | /* |
3 | * Cryptographic API. | |
4 | * | |
5 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> | |
5cb1454b | 6 | * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au> |
1da177e4 LT |
7 | */ |
8 | #ifndef _CRYPTO_INTERNAL_H | |
9 | #define _CRYPTO_INTERNAL_H | |
cce9e06d HX |
10 | |
11 | #include <crypto/algapi.h> | |
2825982d | 12 | #include <linux/completion.h> |
ae131f49 | 13 | #include <linux/err.h> |
adad556e | 14 | #include <linux/jump_label.h> |
5cb1454b | 15 | #include <linux/list.h> |
4cc7720c | 16 | #include <linux/module.h> |
2825982d | 17 | #include <linux/notifier.h> |
1dbb920e HX |
18 | #include <linux/numa.h> |
19 | #include <linux/refcount.h> | |
5cb1454b | 20 | #include <linux/rwsem.h> |
6cb8815f | 21 | #include <linux/scatterlist.h> |
1dbb920e HX |
22 | #include <linux/sched.h> |
23 | #include <linux/types.h> | |
ccb778e1 | 24 | |
6cb8815f HX |
25 | struct akcipher_request; |
26 | struct crypto_akcipher; | |
4cc7720c HX |
27 | struct crypto_instance; |
28 | struct crypto_template; | |
29 | ||
2825982d HX |
30 | struct crypto_larval { |
31 | struct crypto_alg alg; | |
32 | struct crypto_alg *adult; | |
33 | struct completion completion; | |
492e2b63 | 34 | u32 mask; |
adad556e | 35 | bool test_started; |
2825982d HX |
36 | }; |
37 | ||
6cb8815f HX |
38 | struct crypto_akcipher_sync_data { |
39 | struct crypto_akcipher *tfm; | |
40 | const void *src; | |
41 | void *dst; | |
42 | unsigned int slen; | |
43 | unsigned int dlen; | |
44 | ||
45 | struct akcipher_request *req; | |
46 | struct crypto_wait cwait; | |
891ebfdf | 47 | struct scatterlist sg; |
6cb8815f HX |
48 | u8 *buf; |
49 | }; | |
50 | ||
5163ab50 HX |
51 | enum { |
52 | CRYPTOA_UNSPEC, | |
53 | CRYPTOA_ALG, | |
54 | CRYPTOA_TYPE, | |
55 | __CRYPTOA_MAX, | |
56 | }; | |
57 | ||
58 | #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) | |
59 | ||
60 | /* Maximum number of (rtattr) parameters for each template. */ | |
61 | #define CRYPTO_MAX_ATTRS 32 | |
62 | ||
5cb1454b HX |
63 | extern struct list_head crypto_alg_list; |
64 | extern struct rw_semaphore crypto_alg_sem; | |
2825982d | 65 | extern struct blocking_notifier_head crypto_chain; |
5cb1454b | 66 | |
01f727cd HX |
67 | int alg_test(const char *driver, const char *alg, u32 type, u32 mask); |
68 | ||
06bd9c96 EB |
69 | #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS |
70 | static inline bool crypto_boot_test_finished(void) | |
71 | { | |
72 | return true; | |
73 | } | |
74 | static inline void set_crypto_boot_test_finished(void) | |
75 | { | |
76 | } | |
77 | #else | |
78 | DECLARE_STATIC_KEY_FALSE(__crypto_boot_test_finished); | |
79 | static inline bool crypto_boot_test_finished(void) | |
80 | { | |
81 | return static_branch_likely(&__crypto_boot_test_finished); | |
82 | } | |
83 | static inline void set_crypto_boot_test_finished(void) | |
84 | { | |
85 | static_branch_enable(&__crypto_boot_test_finished); | |
86 | } | |
87 | #endif /* !CONFIG_CRYPTO_MANAGER_DISABLE_TESTS */ | |
adad556e | 88 | |
1da177e4 LT |
89 | #ifdef CONFIG_PROC_FS |
90 | void __init crypto_init_proc(void); | |
cce9e06d | 91 | void __exit crypto_exit_proc(void); |
1da177e4 LT |
92 | #else |
93 | static inline void crypto_init_proc(void) | |
94 | { } | |
cce9e06d HX |
95 | static inline void crypto_exit_proc(void) |
96 | { } | |
1da177e4 LT |
97 | #endif |
98 | ||
f1ddcaf3 | 99 | static inline unsigned int crypto_cipher_ctxsize(struct crypto_alg *alg) |
fbdae9f3 | 100 | { |
f1ddcaf3 | 101 | return alg->cra_ctxsize; |
fbdae9f3 HX |
102 | } |
103 | ||
f1ddcaf3 | 104 | static inline unsigned int crypto_compress_ctxsize(struct crypto_alg *alg) |
fbdae9f3 HX |
105 | { |
106 | return alg->cra_ctxsize; | |
107 | } | |
108 | ||
2825982d | 109 | struct crypto_alg *crypto_mod_get(struct crypto_alg *alg); |
492e2b63 | 110 | struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask); |
2825982d | 111 | |
73d3864a | 112 | struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask); |
b9c55aa4 | 113 | void crypto_larval_kill(struct crypto_alg *alg); |
adad556e | 114 | void crypto_wait_for_test(struct crypto_larval *larval); |
73d3864a | 115 | void crypto_alg_tested(const char *name, int err); |
2825982d | 116 | |
89b596ba SK |
117 | void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list, |
118 | struct crypto_alg *nalg); | |
22e5b20b | 119 | void crypto_remove_final(struct list_head *list); |
6603523b | 120 | void crypto_shoot_alg(struct crypto_alg *alg); |
fa3b3565 HX |
121 | struct crypto_tfm *__crypto_alloc_tfmgfp(struct crypto_alg *alg, u32 type, |
122 | u32 mask, gfp_t gfp); | |
27d2a330 HX |
123 | struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type, |
124 | u32 mask); | |
7bc13b5b BS |
125 | void *crypto_create_tfm_node(struct crypto_alg *alg, |
126 | const struct crypto_type *frontend, int node); | |
3c3a24cb HX |
127 | void *crypto_clone_tfm(const struct crypto_type *frontend, |
128 | struct crypto_tfm *otfm); | |
7bc13b5b | 129 | |
6cb8815f HX |
130 | int crypto_akcipher_sync_prep(struct crypto_akcipher_sync_data *data); |
131 | int crypto_akcipher_sync_post(struct crypto_akcipher_sync_data *data, int err); | |
132 | int crypto_init_akcipher_ops_sig(struct crypto_tfm *tfm); | |
133 | ||
7bc13b5b BS |
134 | static inline void *crypto_create_tfm(struct crypto_alg *alg, |
135 | const struct crypto_type *frontend) | |
136 | { | |
137 | return crypto_create_tfm_node(alg, frontend, NUMA_NO_NODE); | |
138 | } | |
139 | ||
d06854f0 HX |
140 | struct crypto_alg *crypto_find_alg(const char *alg_name, |
141 | const struct crypto_type *frontend, | |
142 | u32 type, u32 mask); | |
7bc13b5b BS |
143 | |
144 | void *crypto_alloc_tfm_node(const char *alg_name, | |
145 | const struct crypto_type *frontend, u32 type, u32 mask, | |
146 | int node); | |
147 | ||
148 | static inline void *crypto_alloc_tfm(const char *alg_name, | |
149 | const struct crypto_type *frontend, u32 type, u32 mask) | |
150 | { | |
151 | return crypto_alloc_tfm_node(alg_name, frontend, type, mask, NUMA_NO_NODE); | |
152 | } | |
4cc7720c | 153 | |
73d3864a | 154 | int crypto_probing_notify(unsigned long val, void *v); |
2825982d | 155 | |
38d21433 HX |
156 | unsigned int crypto_alg_extsize(struct crypto_alg *alg); |
157 | ||
f2aefdab HX |
158 | int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, |
159 | u32 type, u32 mask); | |
160 | ||
939e1779 HX |
161 | static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) |
162 | { | |
ce8614a3 | 163 | refcount_inc(&alg->cra_refcnt); |
939e1779 HX |
164 | return alg; |
165 | } | |
166 | ||
6bfd4809 HX |
167 | static inline void crypto_alg_put(struct crypto_alg *alg) |
168 | { | |
ce8614a3 | 169 | if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) |
6bfd4809 HX |
170 | alg->cra_destroy(alg); |
171 | } | |
172 | ||
4cc7720c HX |
173 | static inline int crypto_tmpl_get(struct crypto_template *tmpl) |
174 | { | |
175 | return try_module_get(tmpl->module); | |
176 | } | |
177 | ||
178 | static inline void crypto_tmpl_put(struct crypto_template *tmpl) | |
179 | { | |
180 | module_put(tmpl->module); | |
181 | } | |
182 | ||
2825982d HX |
183 | static inline int crypto_is_larval(struct crypto_alg *alg) |
184 | { | |
185 | return alg->cra_flags & CRYPTO_ALG_LARVAL; | |
186 | } | |
187 | ||
6bfd4809 HX |
188 | static inline int crypto_is_dead(struct crypto_alg *alg) |
189 | { | |
190 | return alg->cra_flags & CRYPTO_ALG_DEAD; | |
191 | } | |
192 | ||
193 | static inline int crypto_is_moribund(struct crypto_alg *alg) | |
194 | { | |
195 | return alg->cra_flags & (CRYPTO_ALG_DEAD | CRYPTO_ALG_DYING); | |
196 | } | |
197 | ||
73d3864a | 198 | static inline void crypto_notify(unsigned long val, void *v) |
2825982d | 199 | { |
73d3864a | 200 | blocking_notifier_call_chain(&crypto_chain, val, v); |
2825982d HX |
201 | } |
202 | ||
1dbb920e HX |
203 | static inline void crypto_yield(u32 flags) |
204 | { | |
205 | if (flags & CRYPTO_TFM_REQ_MAY_SLEEP) | |
206 | cond_resched(); | |
207 | } | |
208 | ||
adad556e HX |
209 | static inline int crypto_is_test_larval(struct crypto_larval *larval) |
210 | { | |
211 | return larval->alg.cra_driver_name[0]; | |
212 | } | |
213 | ||
ae131f49 HX |
214 | static inline struct crypto_tfm *crypto_tfm_get(struct crypto_tfm *tfm) |
215 | { | |
216 | return refcount_inc_not_zero(&tfm->refcnt) ? tfm : ERR_PTR(-EOVERFLOW); | |
217 | } | |
218 | ||
1da177e4 LT |
219 | #endif /* _CRYPTO_INTERNAL_H */ |
220 |