Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
cce9e06d HX |
2 | /* |
3 | * Cryptographic API for algorithms (i.e., low-level API). | |
4 | * | |
5 | * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au> | |
cce9e06d HX |
6 | */ |
7 | #ifndef _CRYPTO_ALGAPI_H | |
8 | #define _CRYPTO_ALGAPI_H | |
9 | ||
244d22ff | 10 | #include <linux/align.h> |
cce9e06d | 11 | #include <linux/crypto.h> |
244d22ff | 12 | #include <linux/kconfig.h> |
b5b7f088 | 13 | #include <linux/list.h> |
244d22ff | 14 | #include <linux/types.h> |
cce9e06d | 15 | |
7976c149 AB |
16 | #include <asm/unaligned.h> |
17 | ||
13c935bb SM |
18 | /* |
19 | * Maximum values for blocksize and alignmask, used to allocate | |
20 | * static buffers that are big enough for any combination of | |
a9f7f88a | 21 | * algs and architectures. Ciphers have a lower maximum size. |
13c935bb | 22 | */ |
a9f7f88a KC |
23 | #define MAX_ALGAPI_BLOCKSIZE 160 |
24 | #define MAX_ALGAPI_ALIGNMASK 63 | |
13c935bb SM |
25 | #define MAX_CIPHER_BLOCKSIZE 16 |
26 | #define MAX_CIPHER_ALIGNMASK 15 | |
27 | ||
5d1d65f8 | 28 | struct crypto_aead; |
319382a6 | 29 | struct crypto_instance; |
4cc7720c | 30 | struct module; |
244d22ff | 31 | struct notifier_block; |
ebc610e5 | 32 | struct rtattr; |
e853c3cf | 33 | struct seq_file; |
0c3dc787 | 34 | struct sk_buff; |
e853c3cf HX |
35 | |
36 | struct crypto_type { | |
27d2a330 | 37 | unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask); |
2ca33da1 | 38 | unsigned int (*extsize)(struct crypto_alg *alg); |
27d2a330 | 39 | int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask); |
2ca33da1 | 40 | int (*init_tfm)(struct crypto_tfm *tfm); |
e853c3cf | 41 | void (*show)(struct seq_file *m, struct crypto_alg *alg); |
b6aa63c0 | 42 | int (*report)(struct sk_buff *skb, struct crypto_alg *alg); |
319382a6 | 43 | void (*free)(struct crypto_instance *inst); |
7b0bac64 HX |
44 | |
45 | unsigned int type; | |
46 | unsigned int maskclear; | |
47 | unsigned int maskset; | |
48 | unsigned int tfmsize; | |
e853c3cf | 49 | }; |
4cc7720c HX |
50 | |
51 | struct crypto_instance { | |
52 | struct crypto_alg alg; | |
53 | ||
54 | struct crypto_template *tmpl; | |
5f567fff HX |
55 | |
56 | union { | |
57 | /* Node in list of instances after registration. */ | |
58 | struct hlist_node list; | |
59 | /* List of attached spawns before registration. */ | |
60 | struct crypto_spawn *spawns; | |
61 | }; | |
4cc7720c HX |
62 | |
63 | void *__ctx[] CRYPTO_MINALIGN_ATTR; | |
64 | }; | |
65 | ||
66 | struct crypto_template { | |
67 | struct list_head list; | |
68 | struct hlist_head instances; | |
69 | struct module *module; | |
70 | ||
f2ac72e8 | 71 | int (*create)(struct crypto_template *tmpl, struct rtattr **tb); |
4cc7720c HX |
72 | |
73 | char name[CRYPTO_MAX_ALG_NAME]; | |
74 | }; | |
75 | ||
6bfd4809 HX |
76 | struct crypto_spawn { |
77 | struct list_head list; | |
78 | struct crypto_alg *alg; | |
5f567fff HX |
79 | union { |
80 | /* Back pointer to instance after registration.*/ | |
81 | struct crypto_instance *inst; | |
82 | /* Spawn list pointer prior to registration. */ | |
83 | struct crypto_spawn *next; | |
84 | }; | |
97eedce1 | 85 | const struct crypto_type *frontend; |
a73e6996 | 86 | u32 mask; |
4f87ee11 | 87 | bool dead; |
5f567fff | 88 | bool registered; |
6bfd4809 HX |
89 | }; |
90 | ||
b5b7f088 HX |
91 | struct crypto_queue { |
92 | struct list_head list; | |
93 | struct list_head *backlog; | |
94 | ||
95 | unsigned int qlen; | |
96 | unsigned int max_qlen; | |
97 | }; | |
98 | ||
5c64097a HX |
99 | struct scatter_walk { |
100 | struct scatterlist *sg; | |
101 | unsigned int offset; | |
102 | }; | |
103 | ||
5163ab50 HX |
104 | struct crypto_attr_alg { |
105 | char name[CRYPTO_MAX_ALG_NAME]; | |
106 | }; | |
107 | ||
108 | struct crypto_attr_type { | |
109 | u32 type; | |
110 | u32 mask; | |
111 | }; | |
112 | ||
db131ef9 HX |
113 | void crypto_mod_put(struct crypto_alg *alg); |
114 | ||
4cc7720c | 115 | int crypto_register_template(struct crypto_template *tmpl); |
9572442d | 116 | int crypto_register_templates(struct crypto_template *tmpls, int count); |
4cc7720c | 117 | void crypto_unregister_template(struct crypto_template *tmpl); |
9572442d | 118 | void crypto_unregister_templates(struct crypto_template *tmpls, int count); |
4cc7720c HX |
119 | struct crypto_template *crypto_lookup_template(const char *name); |
120 | ||
9cd899a3 HX |
121 | int crypto_register_instance(struct crypto_template *tmpl, |
122 | struct crypto_instance *inst); | |
c6d633a9 | 123 | void crypto_unregister_instance(struct crypto_instance *inst); |
9cd899a3 | 124 | |
de95c957 EB |
125 | int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst, |
126 | const char *name, u32 type, u32 mask); | |
6bfd4809 | 127 | void crypto_drop_spawn(struct crypto_spawn *spawn); |
2e306ee0 HX |
128 | struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type, |
129 | u32 mask); | |
97eedce1 | 130 | void *crypto_spawn_tfm2(struct crypto_spawn *spawn); |
6bfd4809 | 131 | |
ebc610e5 | 132 | struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb); |
7bcb2c99 | 133 | int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret); |
68b6c7d6 | 134 | const char *crypto_attr_alg_name(struct rtattr *rta); |
32f27c74 HX |
135 | int crypto_inst_setname(struct crypto_instance *inst, const char *name, |
136 | struct crypto_alg *alg); | |
7fed0bf2 | 137 | |
b5b7f088 HX |
138 | void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen); |
139 | int crypto_enqueue_request(struct crypto_queue *queue, | |
140 | struct crypto_async_request *request); | |
ec6e2bf3 IP |
141 | void crypto_enqueue_request_head(struct crypto_queue *queue, |
142 | struct crypto_async_request *request); | |
b5b7f088 | 143 | struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue); |
9f93a8a0 BW |
144 | static inline unsigned int crypto_queue_len(struct crypto_queue *queue) |
145 | { | |
146 | return queue->qlen; | |
147 | } | |
b5b7f088 | 148 | |
7613636d | 149 | void crypto_inc(u8 *a, unsigned int size); |
a7c391f0 | 150 | void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int size); |
db91af0f AB |
151 | |
152 | static inline void crypto_xor(u8 *dst, const u8 *src, unsigned int size) | |
153 | { | |
154 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && | |
155 | __builtin_constant_p(size) && | |
156 | (size % sizeof(unsigned long)) == 0) { | |
157 | unsigned long *d = (unsigned long *)dst; | |
158 | unsigned long *s = (unsigned long *)src; | |
7976c149 | 159 | unsigned long l; |
db91af0f AB |
160 | |
161 | while (size > 0) { | |
7976c149 AB |
162 | l = get_unaligned(d) ^ get_unaligned(s++); |
163 | put_unaligned(l, d++); | |
db91af0f AB |
164 | size -= sizeof(unsigned long); |
165 | } | |
166 | } else { | |
a7c391f0 | 167 | __crypto_xor(dst, dst, src, size); |
db91af0f AB |
168 | } |
169 | } | |
7613636d | 170 | |
45fe93df AB |
171 | static inline void crypto_xor_cpy(u8 *dst, const u8 *src1, const u8 *src2, |
172 | unsigned int size) | |
173 | { | |
174 | if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && | |
175 | __builtin_constant_p(size) && | |
176 | (size % sizeof(unsigned long)) == 0) { | |
177 | unsigned long *d = (unsigned long *)dst; | |
178 | unsigned long *s1 = (unsigned long *)src1; | |
179 | unsigned long *s2 = (unsigned long *)src2; | |
7976c149 | 180 | unsigned long l; |
45fe93df AB |
181 | |
182 | while (size > 0) { | |
7976c149 AB |
183 | l = get_unaligned(s1++) ^ get_unaligned(s2++); |
184 | put_unaligned(l, d++); | |
45fe93df AB |
185 | size -= sizeof(unsigned long); |
186 | } | |
187 | } else { | |
188 | __crypto_xor(dst, src1, src2, size); | |
189 | } | |
190 | } | |
191 | ||
5cde0af2 HX |
192 | static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm) |
193 | { | |
ab300465 HX |
194 | return PTR_ALIGN(crypto_tfm_ctx(tfm), |
195 | crypto_tfm_alg_alignmask(tfm) + 1); | |
5cde0af2 HX |
196 | } |
197 | ||
124b53d0 HX |
198 | static inline struct crypto_instance *crypto_tfm_alg_instance( |
199 | struct crypto_tfm *tfm) | |
200 | { | |
201 | return container_of(tfm->__crt_alg, struct crypto_instance, alg); | |
202 | } | |
203 | ||
4cc7720c HX |
204 | static inline void *crypto_instance_ctx(struct crypto_instance *inst) |
205 | { | |
206 | return inst->__ctx; | |
207 | } | |
208 | ||
b5b7f088 HX |
209 | static inline struct crypto_async_request *crypto_get_backlog( |
210 | struct crypto_queue *queue) | |
211 | { | |
212 | return queue->backlog == &queue->list ? NULL : | |
213 | container_of(queue->backlog, struct crypto_async_request, list); | |
214 | } | |
215 | ||
7bcb2c99 | 216 | static inline u32 crypto_requires_off(struct crypto_attr_type *algt, u32 off) |
016df0ab | 217 | { |
7bcb2c99 | 218 | return (algt->type ^ off) & algt->mask & off; |
016df0ab HX |
219 | } |
220 | ||
378f4f51 | 221 | /* |
7bcb2c99 EB |
222 | * When an algorithm uses another algorithm (e.g., if it's an instance of a |
223 | * template), these are the flags that should always be set on the "outer" | |
224 | * algorithm if any "inner" algorithm has them set. | |
378f4f51 | 225 | */ |
2eb27c11 | 226 | #define CRYPTO_ALG_INHERITED_FLAGS \ |
fbb6cda4 EB |
227 | (CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK | \ |
228 | CRYPTO_ALG_ALLOCATES_MEMORY) | |
7bcb2c99 EB |
229 | |
230 | /* | |
231 | * Given the type and mask that specify the flags restrictions on a template | |
232 | * instance being created, return the mask that should be passed to | |
233 | * crypto_grab_*() (along with type=0) to honor any request the user made to | |
234 | * have any of the CRYPTO_ALG_INHERITED_FLAGS clear. | |
235 | */ | |
236 | static inline u32 crypto_algt_inherited_mask(struct crypto_attr_type *algt) | |
378f4f51 | 237 | { |
7bcb2c99 | 238 | return crypto_requires_off(algt, CRYPTO_ALG_INHERITED_FLAGS); |
378f4f51 HX |
239 | } |
240 | ||
6bf37e5a JY |
241 | noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size); |
242 | ||
243 | /** | |
244 | * crypto_memneq - Compare two areas of memory without leaking | |
245 | * timing information. | |
246 | * | |
247 | * @a: One area of memory | |
248 | * @b: Another area of memory | |
249 | * @size: The size of the area. | |
250 | * | |
251 | * Returns 0 when data is equal, 1 otherwise. | |
252 | */ | |
253 | static inline int crypto_memneq(const void *a, const void *b, size_t size) | |
254 | { | |
255 | return __crypto_memneq(a, b, size) != 0UL ? 1 : 0; | |
256 | } | |
cce9e06d | 257 | |
dd8b083f MP |
258 | int crypto_register_notifier(struct notifier_block *nb); |
259 | int crypto_unregister_notifier(struct notifier_block *nb); | |
260 | ||
261 | /* Crypto notification events. */ | |
262 | enum { | |
263 | CRYPTO_MSG_ALG_REQUEST, | |
264 | CRYPTO_MSG_ALG_REGISTER, | |
265 | CRYPTO_MSG_ALG_LOADED, | |
266 | }; | |
267 | ||
6bf37e5a | 268 | #endif /* _CRYPTO_ALGAPI_H */ |