1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Symmetric key ciphers.
4  *
5  * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
6  */
7 
8 #ifndef _CRYPTO_INTERNAL_SKCIPHER_H
9 #define _CRYPTO_INTERNAL_SKCIPHER_H
10 
11 #include <crypto/algapi.h>
12 #include <crypto/skcipher.h>
13 #include <linux/list.h>
14 #include <linux/types.h>
15 
16 struct aead_request;
17 struct rtattr;
18 
19 struct skcipher_instance {
20 	void (*free)(struct skcipher_instance *inst);
21 	union {
22 		struct {
23 			char head[offsetof(struct skcipher_alg, base)];
24 			struct crypto_instance base;
25 		} s;
26 		struct skcipher_alg alg;
27 	};
28 };
29 
30 struct crypto_skcipher_spawn {
31 	struct crypto_spawn base;
32 };
33 
34 struct skcipher_walk {
35 	union {
36 		struct {
37 			struct page *page;
38 			unsigned long offset;
39 		} phys;
40 
41 		struct {
42 			u8 *page;
43 			void *addr;
44 		} virt;
45 	} src, dst;
46 
47 	struct scatter_walk in;
48 	unsigned int nbytes;
49 
50 	struct scatter_walk out;
51 	unsigned int total;
52 
53 	struct list_head buffers;
54 
55 	u8 *page;
56 	u8 *buffer;
57 	u8 *oiv;
58 	void *iv;
59 
60 	unsigned int ivsize;
61 
62 	int flags;
63 	unsigned int blocksize;
64 	unsigned int stride;
65 	unsigned int alignmask;
66 };
67 
68 static inline struct crypto_instance *skcipher_crypto_instance(
69 	struct skcipher_instance *inst)
70 {
71 	return &inst->s.base;
72 }
73 
74 static inline struct skcipher_instance *skcipher_alg_instance(
75 	struct crypto_skcipher *skcipher)
76 {
77 	return container_of(crypto_skcipher_alg(skcipher),
78 			    struct skcipher_instance, alg);
79 }
80 
81 static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
82 {
83 	return crypto_instance_ctx(skcipher_crypto_instance(inst));
84 }
85 
86 static inline void skcipher_request_complete(struct skcipher_request *req, int err)
87 {
88 	req->base.complete(&req->base, err);
89 }
90 
91 static inline void crypto_set_skcipher_spawn(
92 	struct crypto_skcipher_spawn *spawn, struct crypto_instance *inst)
93 {
94 	crypto_set_spawn(&spawn->base, inst);
95 }
96 
97 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
98 			 u32 type, u32 mask);
99 
100 static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
101 {
102 	crypto_drop_spawn(&spawn->base);
103 }
104 
105 static inline struct skcipher_alg *crypto_skcipher_spawn_alg(
106 	struct crypto_skcipher_spawn *spawn)
107 {
108 	return container_of(spawn->base.alg, struct skcipher_alg, base);
109 }
110 
111 static inline struct skcipher_alg *crypto_spawn_skcipher_alg(
112 	struct crypto_skcipher_spawn *spawn)
113 {
114 	return crypto_skcipher_spawn_alg(spawn);
115 }
116 
117 static inline struct crypto_skcipher *crypto_spawn_skcipher(
118 	struct crypto_skcipher_spawn *spawn)
119 {
120 	return crypto_spawn_tfm2(&spawn->base);
121 }
122 
123 static inline void crypto_skcipher_set_reqsize(
124 	struct crypto_skcipher *skcipher, unsigned int reqsize)
125 {
126 	skcipher->reqsize = reqsize;
127 }
128 
129 int crypto_register_skcipher(struct skcipher_alg *alg);
130 void crypto_unregister_skcipher(struct skcipher_alg *alg);
131 int crypto_register_skciphers(struct skcipher_alg *algs, int count);
132 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
133 int skcipher_register_instance(struct crypto_template *tmpl,
134 			       struct skcipher_instance *inst);
135 
136 int skcipher_walk_done(struct skcipher_walk *walk, int err);
137 int skcipher_walk_virt(struct skcipher_walk *walk,
138 		       struct skcipher_request *req,
139 		       bool atomic);
140 void skcipher_walk_atomise(struct skcipher_walk *walk);
141 int skcipher_walk_async(struct skcipher_walk *walk,
142 			struct skcipher_request *req);
143 int skcipher_walk_aead(struct skcipher_walk *walk, struct aead_request *req,
144 		       bool atomic);
145 int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
146 			       struct aead_request *req, bool atomic);
147 int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
148 			       struct aead_request *req, bool atomic);
149 void skcipher_walk_complete(struct skcipher_walk *walk, int err);
150 
151 static inline void skcipher_walk_abort(struct skcipher_walk *walk)
152 {
153 	skcipher_walk_done(walk, -ECANCELED);
154 }
155 
156 static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
157 {
158 	return crypto_tfm_ctx(&tfm->base);
159 }
160 
161 static inline void *skcipher_request_ctx(struct skcipher_request *req)
162 {
163 	return req->__ctx;
164 }
165 
166 static inline u32 skcipher_request_flags(struct skcipher_request *req)
167 {
168 	return req->base.flags;
169 }
170 
171 static inline unsigned int crypto_skcipher_alg_min_keysize(
172 	struct skcipher_alg *alg)
173 {
174 	return alg->min_keysize;
175 }
176 
177 static inline unsigned int crypto_skcipher_alg_max_keysize(
178 	struct skcipher_alg *alg)
179 {
180 	return alg->max_keysize;
181 }
182 
183 static inline unsigned int crypto_skcipher_alg_walksize(
184 	struct skcipher_alg *alg)
185 {
186 	return alg->walksize;
187 }
188 
189 /**
190  * crypto_skcipher_walksize() - obtain walk size
191  * @tfm: cipher handle
192  *
193  * In some cases, algorithms can only perform optimally when operating on
194  * multiple blocks in parallel. This is reflected by the walksize, which
195  * must be a multiple of the chunksize (or equal if the concern does not
196  * apply)
197  *
198  * Return: walk size in bytes
199  */
200 static inline unsigned int crypto_skcipher_walksize(
201 	struct crypto_skcipher *tfm)
202 {
203 	return crypto_skcipher_alg_walksize(crypto_skcipher_alg(tfm));
204 }
205 
206 /* Helpers for simple block cipher modes of operation */
207 struct skcipher_ctx_simple {
208 	struct crypto_cipher *cipher;	/* underlying block cipher */
209 };
210 static inline struct crypto_cipher *
211 skcipher_cipher_simple(struct crypto_skcipher *tfm)
212 {
213 	struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
214 
215 	return ctx->cipher;
216 }
217 struct skcipher_instance *
218 skcipher_alloc_instance_simple(struct crypto_template *tmpl, struct rtattr **tb,
219 			       struct crypto_alg **cipher_alg_ret);
220 
221 #endif	/* _CRYPTO_INTERNAL_SKCIPHER_H */
222 
223