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