]> git.sesse.net Git - bcachefs-tools-debian/blob - include/crypto/algapi.h
bcache in userspace; userspace fsck
[bcachefs-tools-debian] / include / crypto / algapi.h
1 /*
2  * Cryptographic API for algorithms (i.e., low-level API).
3  *
4  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option) 
9  * any later version.
10  *
11  */
12 #ifndef _CRYPTO_ALGAPI_H
13 #define _CRYPTO_ALGAPI_H
14
15 #include <linux/crypto.h>
16 #include <linux/device.h>
17 #include <linux/list.h>
18 #include <linux/kernel.h>
19 #include <linux/kthread.h>
20
21 struct crypto_aead;
22 struct crypto_instance;
23 struct module;
24 struct rtattr;
25 struct seq_file;
26 struct sk_buff;
27
28 struct crypto_type {
29         unsigned int (*ctxsize)(struct crypto_alg *alg, u32 type, u32 mask);
30         unsigned int (*extsize)(struct crypto_alg *alg);
31         int (*init)(struct crypto_tfm *tfm, u32 type, u32 mask);
32         int (*init_tfm)(struct crypto_tfm *tfm);
33         void (*show)(struct seq_file *m, struct crypto_alg *alg);
34         struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask);
35         void (*free)(struct crypto_instance *inst);
36
37         unsigned int type;
38         unsigned int maskclear;
39         unsigned int maskset;
40         unsigned int tfmsize;
41 };
42
43 struct crypto_instance {
44         struct crypto_alg alg;
45
46         struct crypto_template *tmpl;
47         struct hlist_node list;
48
49         void *__ctx[] CRYPTO_MINALIGN_ATTR;
50 };
51
52 struct crypto_template {
53         struct list_head list;
54         struct hlist_head instances;
55         struct module *module;
56
57         struct crypto_instance *(*alloc)(struct rtattr **tb);
58         void (*free)(struct crypto_instance *inst);
59         int (*create)(struct crypto_template *tmpl, struct rtattr **tb);
60
61         char name[CRYPTO_MAX_ALG_NAME];
62 };
63
64 struct scatter_walk {
65         struct scatterlist *sg;
66         unsigned int offset;
67 };
68
69 struct blkcipher_walk {
70         union {
71                 struct {
72                         struct page *page;
73                         unsigned long offset;
74                 } phys;
75
76                 struct {
77                         u8 *page;
78                         u8 *addr;
79                 } virt;
80         } src, dst;
81
82         struct scatter_walk in;
83         unsigned int nbytes;
84
85         struct scatter_walk out;
86         unsigned int total;
87
88         void *page;
89         u8 *buffer;
90         u8 *iv;
91         unsigned int ivsize;
92
93         int flags;
94         unsigned int walk_blocksize;
95         unsigned int cipher_blocksize;
96         unsigned int alignmask;
97 };
98
99 extern const struct crypto_type crypto_blkcipher_type;
100
101 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb);
102 int crypto_check_attr_type(struct rtattr **tb, u32 type);
103 const char *crypto_attr_alg_name(struct rtattr *rta);
104 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
105                                     const struct crypto_type *frontend,
106                                     u32 type, u32 mask);
107
108 static inline struct crypto_alg *crypto_attr_alg(struct rtattr *rta,
109                                                  u32 type, u32 mask)
110 {
111         return crypto_attr_alg2(rta, NULL, type, mask);
112 }
113
114 int crypto_attr_u32(struct rtattr *rta, u32 *num);
115
116 /* These functions require the input/output to be aligned as u32. */
117 void crypto_inc(u8 *a, unsigned int size);
118 void crypto_xor(u8 *dst, const u8 *src, unsigned int size);
119
120 int blkcipher_walk_done(struct blkcipher_desc *desc,
121                         struct blkcipher_walk *walk, int err);
122 int blkcipher_walk_virt(struct blkcipher_desc *desc,
123                         struct blkcipher_walk *walk);
124 int blkcipher_walk_phys(struct blkcipher_desc *desc,
125                         struct blkcipher_walk *walk);
126 int blkcipher_walk_virt_block(struct blkcipher_desc *desc,
127                               struct blkcipher_walk *walk,
128                               unsigned int blocksize);
129 int blkcipher_aead_walk_virt_block(struct blkcipher_desc *desc,
130                                    struct blkcipher_walk *walk,
131                                    struct crypto_aead *tfm,
132                                    unsigned int blocksize);
133
134 static inline void *crypto_tfm_ctx_aligned(struct crypto_tfm *tfm)
135 {
136         return PTR_ALIGN(crypto_tfm_ctx(tfm),
137                          crypto_tfm_alg_alignmask(tfm) + 1);
138 }
139
140 static inline struct crypto_instance *crypto_tfm_alg_instance(
141         struct crypto_tfm *tfm)
142 {
143         return container_of(tfm->__crt_alg, struct crypto_instance, alg);
144 }
145
146 static inline void *crypto_instance_ctx(struct crypto_instance *inst)
147 {
148         return inst->__ctx;
149 }
150
151 static inline void *crypto_blkcipher_ctx(struct crypto_blkcipher *tfm)
152 {
153         return crypto_tfm_ctx(&tfm->base);
154 }
155
156 static inline void *crypto_blkcipher_ctx_aligned(struct crypto_blkcipher *tfm)
157 {
158         return crypto_tfm_ctx_aligned(&tfm->base);
159 }
160
161 static inline struct cipher_alg *crypto_cipher_alg(struct crypto_cipher *tfm)
162 {
163         return &crypto_cipher_tfm(tfm)->__crt_alg->cra_cipher;
164 }
165
166 static inline void blkcipher_walk_init(struct blkcipher_walk *walk,
167                                        struct scatterlist *dst,
168                                        struct scatterlist *src,
169                                        unsigned int nbytes)
170 {
171         walk->in.sg = src;
172         walk->out.sg = dst;
173         walk->total = nbytes;
174 }
175
176 static inline struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb,
177                                                      u32 type, u32 mask)
178 {
179         return crypto_attr_alg(tb[1], type, mask);
180 }
181
182 static inline int crypto_requires_sync(u32 type, u32 mask)
183 {
184         return (type ^ CRYPTO_ALG_ASYNC) & mask & CRYPTO_ALG_ASYNC;
185 }
186
187 noinline unsigned long __crypto_memneq(const void *a, const void *b, size_t size);
188
189 /**
190  * crypto_memneq - Compare two areas of memory without leaking
191  *                 timing information.
192  *
193  * @a: One area of memory
194  * @b: Another area of memory
195  * @size: The size of the area.
196  *
197  * Returns 0 when data is equal, 1 otherwise.
198  */
199 static inline int crypto_memneq(const void *a, const void *b, size_t size)
200 {
201         return __crypto_memneq(a, b, size) != 0UL ? 1 : 0;
202 }
203
204 static inline void crypto_yield(u32 flags)
205 {
206 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PREEMPT_VOLUNTARY)
207         if (flags & CRYPTO_TFM_REQ_MAY_SLEEP)
208                 cond_resched();
209 #endif
210 }
211
212 #endif  /* _CRYPTO_ALGAPI_H */