1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <crypto/internal/skcipher.h>
19#include <crypto/rng.h>
20#include <crypto/scatterwalk.h>
21#include <linux/err.h>
22#include <linux/init.h>
23#include <linux/kernel.h>
24#include <linux/mm.h>
25#include <linux/module.h>
26#include <linux/scatterlist.h>
27#include <linux/spinlock.h>
28#include <linux/string.h>
29
30struct eseqiv_request_ctx {
31 struct scatterlist src[2];
32 struct scatterlist dst[2];
33 char tail[];
34};
35
36struct eseqiv_ctx {
37 spinlock_t lock;
38 unsigned int reqoff;
39 char salt[];
40};
41
42static void eseqiv_complete2(struct skcipher_givcrypt_request *req)
43{
44 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
45 struct eseqiv_request_ctx *reqctx = skcipher_givcrypt_reqctx(req);
46
47 memcpy(req->giv, PTR_ALIGN((u8 *)reqctx->tail,
48 crypto_ablkcipher_alignmask(geniv) + 1),
49 crypto_ablkcipher_ivsize(geniv));
50}
51
52static void eseqiv_complete(struct crypto_async_request *base, int err)
53{
54 struct skcipher_givcrypt_request *req = base->data;
55
56 if (err)
57 goto out;
58
59 eseqiv_complete2(req);
60
61out:
62 skcipher_givcrypt_complete(req, err);
63}
64
65static int eseqiv_givencrypt(struct skcipher_givcrypt_request *req)
66{
67 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
68 struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
69 struct eseqiv_request_ctx *reqctx = skcipher_givcrypt_reqctx(req);
70 struct ablkcipher_request *subreq;
71 crypto_completion_t compl;
72 void *data;
73 struct scatterlist *osrc, *odst;
74 struct scatterlist *dst;
75 struct page *srcp;
76 struct page *dstp;
77 u8 *giv;
78 u8 *vsrc;
79 u8 *vdst;
80 __be64 seq;
81 unsigned int ivsize;
82 unsigned int len;
83 int err;
84
85 subreq = (void *)(reqctx->tail + ctx->reqoff);
86 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
87
88 giv = req->giv;
89 compl = req->creq.base.complete;
90 data = req->creq.base.data;
91
92 osrc = req->creq.src;
93 odst = req->creq.dst;
94 srcp = sg_page(osrc);
95 dstp = sg_page(odst);
96 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + osrc->offset;
97 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + odst->offset;
98
99 ivsize = crypto_ablkcipher_ivsize(geniv);
100
101 if (vsrc != giv + ivsize && vdst != giv + ivsize) {
102 giv = PTR_ALIGN((u8 *)reqctx->tail,
103 crypto_ablkcipher_alignmask(geniv) + 1);
104 compl = eseqiv_complete;
105 data = req;
106 }
107
108 ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
109 data);
110
111 sg_init_table(reqctx->src, 2);
112 sg_set_buf(reqctx->src, giv, ivsize);
113 scatterwalk_crypto_chain(reqctx->src, osrc, vsrc == giv + ivsize, 2);
114
115 dst = reqctx->src;
116 if (osrc != odst) {
117 sg_init_table(reqctx->dst, 2);
118 sg_set_buf(reqctx->dst, giv, ivsize);
119 scatterwalk_crypto_chain(reqctx->dst, odst, vdst == giv + ivsize, 2);
120
121 dst = reqctx->dst;
122 }
123
124 ablkcipher_request_set_crypt(subreq, reqctx->src, dst,
125 req->creq.nbytes + ivsize,
126 req->creq.info);
127
128 memcpy(req->creq.info, ctx->salt, ivsize);
129
130 len = ivsize;
131 if (ivsize > sizeof(u64)) {
132 memset(req->giv, 0, ivsize - sizeof(u64));
133 len = sizeof(u64);
134 }
135 seq = cpu_to_be64(req->seq);
136 memcpy(req->giv + ivsize - len, &seq, len);
137
138 err = crypto_ablkcipher_encrypt(subreq);
139 if (err)
140 goto out;
141
142 if (giv != req->giv)
143 eseqiv_complete2(req);
144
145out:
146 return err;
147}
148
149static int eseqiv_init(struct crypto_tfm *tfm)
150{
151 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
152 struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
153 unsigned long alignmask;
154 unsigned int reqsize;
155
156 spin_lock_init(&ctx->lock);
157
158 alignmask = crypto_tfm_ctx_alignment() - 1;
159 reqsize = sizeof(struct eseqiv_request_ctx);
160
161 if (alignmask & reqsize) {
162 alignmask &= reqsize;
163 alignmask--;
164 }
165
166 alignmask = ~alignmask;
167 alignmask &= crypto_ablkcipher_alignmask(geniv);
168
169 reqsize += alignmask;
170 reqsize += crypto_ablkcipher_ivsize(geniv);
171 reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment());
172
173 ctx->reqoff = reqsize - sizeof(struct eseqiv_request_ctx);
174
175 tfm->crt_ablkcipher.reqsize = reqsize +
176 sizeof(struct ablkcipher_request);
177
178 return crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
179 crypto_ablkcipher_ivsize(geniv)) ?:
180 skcipher_geniv_init(tfm);
181}
182
183static struct crypto_template eseqiv_tmpl;
184
185static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
186{
187 struct crypto_instance *inst;
188 int err;
189
190 err = crypto_get_default_rng();
191 if (err)
192 return ERR_PTR(err);
193
194 inst = skcipher_geniv_alloc(&eseqiv_tmpl, tb, 0, 0);
195 if (IS_ERR(inst))
196 goto put_rng;
197
198 err = -EINVAL;
199 if (inst->alg.cra_ablkcipher.ivsize != inst->alg.cra_blocksize)
200 goto free_inst;
201
202 inst->alg.cra_ablkcipher.givencrypt = eseqiv_givencrypt;
203
204 inst->alg.cra_init = eseqiv_init;
205 inst->alg.cra_exit = skcipher_geniv_exit;
206
207 inst->alg.cra_ctxsize = sizeof(struct eseqiv_ctx);
208 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
209
210out:
211 return inst;
212
213free_inst:
214 skcipher_geniv_free(inst);
215 inst = ERR_PTR(err);
216put_rng:
217 crypto_put_default_rng();
218 goto out;
219}
220
221static void eseqiv_free(struct crypto_instance *inst)
222{
223 skcipher_geniv_free(inst);
224 crypto_put_default_rng();
225}
226
227static struct crypto_template eseqiv_tmpl = {
228 .name = "eseqiv",
229 .alloc = eseqiv_alloc,
230 .free = eseqiv_free,
231 .module = THIS_MODULE,
232};
233
234static int __init eseqiv_module_init(void)
235{
236 return crypto_register_template(&eseqiv_tmpl);
237}
238
239static void __exit eseqiv_module_exit(void)
240{
241 crypto_unregister_template(&eseqiv_tmpl);
242}
243
244module_init(eseqiv_module_init);
245module_exit(eseqiv_module_exit);
246
247MODULE_LICENSE("GPL");
248MODULE_DESCRIPTION("Encrypted Sequence Number IV Generator");
249MODULE_ALIAS_CRYPTO("eseqiv");
250