blob: 2a342c8e52b385b34f2602b0c8dfb4d465e9b573 [file] [log] [blame]
Herbert Xu806d1832007-12-05 12:10:53 +11001/*
2 * eseqiv: Encrypted Sequence Number IV Generator
3 *
4 * This generator generates an IV based on a sequence number by xoring it
5 * with a salt and then encrypting it with the same key as used to encrypt
6 * the plain text. This algorithm requires that the block size be equal
7 * to the IV size. It is mainly useful for CBC.
8 *
9 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 */
17
18#include <crypto/internal/skcipher.h>
Herbert Xua0f000e2008-08-14 22:21:31 +100019#include <crypto/rng.h>
Herbert Xu806d1832007-12-05 12:10:53 +110020#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>
Herbert Xu806d1832007-12-05 12:10:53 +110026#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 void eseqiv_chain(struct scatterlist *head, struct scatterlist *sg,
66 int chain)
67{
68 if (chain) {
69 head->length += sg->length;
70 sg = scatterwalk_sg_next(sg);
71 }
72
73 if (sg)
74 scatterwalk_sg_chain(head, 2, sg);
75 else
76 sg_mark_end(head);
77}
78
79static int eseqiv_givencrypt(struct skcipher_givcrypt_request *req)
80{
81 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
82 struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
83 struct eseqiv_request_ctx *reqctx = skcipher_givcrypt_reqctx(req);
84 struct ablkcipher_request *subreq;
85 crypto_completion_t complete;
86 void *data;
87 struct scatterlist *osrc, *odst;
88 struct scatterlist *dst;
89 struct page *srcp;
90 struct page *dstp;
91 u8 *giv;
92 u8 *vsrc;
93 u8 *vdst;
94 __be64 seq;
95 unsigned int ivsize;
96 unsigned int len;
97 int err;
98
99 subreq = (void *)(reqctx->tail + ctx->reqoff);
100 ablkcipher_request_set_tfm(subreq, skcipher_geniv_cipher(geniv));
101
102 giv = req->giv;
103 complete = req->creq.base.complete;
104 data = req->creq.base.data;
105
106 osrc = req->creq.src;
107 odst = req->creq.dst;
108 srcp = sg_page(osrc);
109 dstp = sg_page(odst);
110 vsrc = PageHighMem(srcp) ? NULL : page_address(srcp) + osrc->offset;
111 vdst = PageHighMem(dstp) ? NULL : page_address(dstp) + odst->offset;
112
113 ivsize = crypto_ablkcipher_ivsize(geniv);
114
115 if (vsrc != giv + ivsize && vdst != giv + ivsize) {
116 giv = PTR_ALIGN((u8 *)reqctx->tail,
117 crypto_ablkcipher_alignmask(geniv) + 1);
118 complete = eseqiv_complete;
119 data = req;
120 }
121
122 ablkcipher_request_set_callback(subreq, req->creq.base.flags, complete,
123 data);
124
125 sg_init_table(reqctx->src, 2);
126 sg_set_buf(reqctx->src, giv, ivsize);
127 eseqiv_chain(reqctx->src, osrc, vsrc == giv + ivsize);
128
129 dst = reqctx->src;
130 if (osrc != odst) {
131 sg_init_table(reqctx->dst, 2);
132 sg_set_buf(reqctx->dst, giv, ivsize);
133 eseqiv_chain(reqctx->dst, odst, vdst == giv + ivsize);
134
135 dst = reqctx->dst;
136 }
137
138 ablkcipher_request_set_crypt(subreq, reqctx->src, dst,
Herbert Xu46f81532008-04-29 21:57:01 +0800139 req->creq.nbytes + ivsize,
140 req->creq.info);
Herbert Xu806d1832007-12-05 12:10:53 +1100141
142 memcpy(req->creq.info, ctx->salt, ivsize);
143
144 len = ivsize;
145 if (ivsize > sizeof(u64)) {
146 memset(req->giv, 0, ivsize - sizeof(u64));
147 len = sizeof(u64);
148 }
149 seq = cpu_to_be64(req->seq);
150 memcpy(req->giv + ivsize - len, &seq, len);
151
152 err = crypto_ablkcipher_encrypt(subreq);
153 if (err)
154 goto out;
155
156 eseqiv_complete2(req);
157
158out:
159 return err;
160}
161
162static int eseqiv_givencrypt_first(struct skcipher_givcrypt_request *req)
163{
164 struct crypto_ablkcipher *geniv = skcipher_givcrypt_reqtfm(req);
165 struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
Herbert Xua0f000e2008-08-14 22:21:31 +1000166 int err = 0;
Herbert Xu806d1832007-12-05 12:10:53 +1100167
168 spin_lock_bh(&ctx->lock);
169 if (crypto_ablkcipher_crt(geniv)->givencrypt != eseqiv_givencrypt_first)
170 goto unlock;
171
172 crypto_ablkcipher_crt(geniv)->givencrypt = eseqiv_givencrypt;
Herbert Xua0f000e2008-08-14 22:21:31 +1000173 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
174 crypto_ablkcipher_ivsize(geniv));
Herbert Xu806d1832007-12-05 12:10:53 +1100175
176unlock:
177 spin_unlock_bh(&ctx->lock);
178
Herbert Xua0f000e2008-08-14 22:21:31 +1000179 if (err)
180 return err;
181
Herbert Xu806d1832007-12-05 12:10:53 +1100182 return eseqiv_givencrypt(req);
183}
184
185static int eseqiv_init(struct crypto_tfm *tfm)
186{
187 struct crypto_ablkcipher *geniv = __crypto_ablkcipher_cast(tfm);
188 struct eseqiv_ctx *ctx = crypto_ablkcipher_ctx(geniv);
189 unsigned long alignmask;
190 unsigned int reqsize;
191
192 spin_lock_init(&ctx->lock);
193
194 alignmask = crypto_tfm_ctx_alignment() - 1;
195 reqsize = sizeof(struct eseqiv_request_ctx);
196
197 if (alignmask & reqsize) {
198 alignmask &= reqsize;
199 alignmask--;
200 }
201
202 alignmask = ~alignmask;
203 alignmask &= crypto_ablkcipher_alignmask(geniv);
204
205 reqsize += alignmask;
206 reqsize += crypto_ablkcipher_ivsize(geniv);
207 reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment());
208
209 ctx->reqoff = reqsize - sizeof(struct eseqiv_request_ctx);
210
211 tfm->crt_ablkcipher.reqsize = reqsize +
212 sizeof(struct ablkcipher_request);
213
214 return skcipher_geniv_init(tfm);
215}
216
217static struct crypto_template eseqiv_tmpl;
218
219static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
220{
221 struct crypto_instance *inst;
222 int err;
223
Herbert Xua0f000e2008-08-14 22:21:31 +1000224 err = crypto_get_default_rng();
225 if (err)
226 return ERR_PTR(err);
227
Herbert Xu806d1832007-12-05 12:10:53 +1100228 inst = skcipher_geniv_alloc(&eseqiv_tmpl, tb, 0, 0);
229 if (IS_ERR(inst))
Herbert Xua0f000e2008-08-14 22:21:31 +1000230 goto put_rng;
Herbert Xu806d1832007-12-05 12:10:53 +1100231
232 err = -EINVAL;
233 if (inst->alg.cra_ablkcipher.ivsize != inst->alg.cra_blocksize)
234 goto free_inst;
235
236 inst->alg.cra_ablkcipher.givencrypt = eseqiv_givencrypt_first;
237
238 inst->alg.cra_init = eseqiv_init;
239 inst->alg.cra_exit = skcipher_geniv_exit;
240
241 inst->alg.cra_ctxsize = sizeof(struct eseqiv_ctx);
242 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
243
244out:
245 return inst;
246
247free_inst:
248 skcipher_geniv_free(inst);
249 inst = ERR_PTR(err);
Herbert Xua0f000e2008-08-14 22:21:31 +1000250put_rng:
251 crypto_put_default_rng();
Herbert Xu806d1832007-12-05 12:10:53 +1100252 goto out;
253}
254
Herbert Xua0f000e2008-08-14 22:21:31 +1000255static void eseqiv_free(struct crypto_instance *inst)
256{
257 skcipher_geniv_free(inst);
258 crypto_put_default_rng();
259}
260
Herbert Xu806d1832007-12-05 12:10:53 +1100261static struct crypto_template eseqiv_tmpl = {
262 .name = "eseqiv",
263 .alloc = eseqiv_alloc,
Herbert Xua0f000e2008-08-14 22:21:31 +1000264 .free = eseqiv_free,
Herbert Xu806d1832007-12-05 12:10:53 +1100265 .module = THIS_MODULE,
266};
267
Herbert Xu5be5e662008-08-17 18:04:30 +1000268static int __init eseqiv_module_init(void)
Herbert Xu806d1832007-12-05 12:10:53 +1100269{
270 return crypto_register_template(&eseqiv_tmpl);
271}
272
Herbert Xu5be5e662008-08-17 18:04:30 +1000273static void __exit eseqiv_module_exit(void)
Herbert Xu806d1832007-12-05 12:10:53 +1100274{
275 crypto_unregister_template(&eseqiv_tmpl);
276}
Herbert Xu5be5e662008-08-17 18:04:30 +1000277
278module_init(eseqiv_module_init);
279module_exit(eseqiv_module_exit);
280
281MODULE_LICENSE("GPL");
282MODULE_DESCRIPTION("Encrypted Sequence Number IV Generator");