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