blob: 16dda72fc4f88cc45f6b66f91b2ee1e1ccd5957e [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
Herbert Xu806d1832007-12-05 12:10:53 +110065static 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;
Mark Rustad3e3dc252014-07-25 02:53:38 -070071 crypto_completion_t compl;
Herbert Xu806d1832007-12-05 12:10:53 +110072 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;
Mark Rustad3e3dc252014-07-25 02:53:38 -070089 compl = req->creq.base.complete;
Herbert Xu806d1832007-12-05 12:10:53 +110090 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);
Mark Rustad3e3dc252014-07-25 02:53:38 -0700104 compl = eseqiv_complete;
Herbert Xu806d1832007-12-05 12:10:53 +1100105 data = req;
106 }
107
Mark Rustad3e3dc252014-07-25 02:53:38 -0700108 ablkcipher_request_set_callback(subreq, req->creq.base.flags, compl,
Herbert Xu806d1832007-12-05 12:10:53 +1100109 data);
110
111 sg_init_table(reqctx->src, 2);
112 sg_set_buf(reqctx->src, giv, ivsize);
Steffen Klassertc920fa62010-11-22 11:26:54 +0100113 scatterwalk_crypto_chain(reqctx->src, osrc, vsrc == giv + ivsize, 2);
Herbert Xu806d1832007-12-05 12:10:53 +1100114
115 dst = reqctx->src;
116 if (osrc != odst) {
117 sg_init_table(reqctx->dst, 2);
118 sg_set_buf(reqctx->dst, giv, ivsize);
Steffen Klassertc920fa62010-11-22 11:26:54 +0100119 scatterwalk_crypto_chain(reqctx->dst, odst, vdst == giv + ivsize, 2);
Herbert Xu806d1832007-12-05 12:10:53 +1100120
121 dst = reqctx->dst;
122 }
123
124 ablkcipher_request_set_crypt(subreq, reqctx->src, dst,
Herbert Xu46f81532008-04-29 21:57:01 +0800125 req->creq.nbytes + ivsize,
126 req->creq.info);
Herbert Xu806d1832007-12-05 12:10:53 +1100127
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
Steffen Klassertabe5fa782009-04-15 20:45:03 +0800142 if (giv != req->giv)
143 eseqiv_complete2(req);
Herbert Xu806d1832007-12-05 12:10:53 +1100144
145out:
146 return err;
147}
148
Herbert Xu806d1832007-12-05 12:10:53 +1100149static 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;
Herbert Xu055906d2015-06-21 19:11:47 +0800155 int err;
Herbert Xu806d1832007-12-05 12:10:53 +1100156
157 spin_lock_init(&ctx->lock);
158
159 alignmask = crypto_tfm_ctx_alignment() - 1;
160 reqsize = sizeof(struct eseqiv_request_ctx);
161
162 if (alignmask & reqsize) {
163 alignmask &= reqsize;
164 alignmask--;
165 }
166
167 alignmask = ~alignmask;
168 alignmask &= crypto_ablkcipher_alignmask(geniv);
169
170 reqsize += alignmask;
171 reqsize += crypto_ablkcipher_ivsize(geniv);
172 reqsize = ALIGN(reqsize, crypto_tfm_ctx_alignment());
173
174 ctx->reqoff = reqsize - sizeof(struct eseqiv_request_ctx);
175
176 tfm->crt_ablkcipher.reqsize = reqsize +
177 sizeof(struct ablkcipher_request);
178
Herbert Xu055906d2015-06-21 19:11:47 +0800179 err = 0;
180 if (!crypto_get_default_rng()) {
181 crypto_ablkcipher_crt(geniv)->givencrypt = eseqiv_givencrypt;
182 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
183 crypto_ablkcipher_ivsize(geniv));
184 crypto_put_default_rng();
185 }
186
187 return err ?: skcipher_geniv_init(tfm);
Herbert Xu806d1832007-12-05 12:10:53 +1100188}
189
190static struct crypto_template eseqiv_tmpl;
191
192static struct crypto_instance *eseqiv_alloc(struct rtattr **tb)
193{
194 struct crypto_instance *inst;
195 int err;
196
197 inst = skcipher_geniv_alloc(&eseqiv_tmpl, tb, 0, 0);
198 if (IS_ERR(inst))
Herbert Xu055906d2015-06-21 19:11:47 +0800199 goto out;
Herbert Xu806d1832007-12-05 12:10:53 +1100200
201 err = -EINVAL;
202 if (inst->alg.cra_ablkcipher.ivsize != inst->alg.cra_blocksize)
203 goto free_inst;
204
Herbert Xu806d1832007-12-05 12:10:53 +1100205 inst->alg.cra_init = eseqiv_init;
206 inst->alg.cra_exit = skcipher_geniv_exit;
207
208 inst->alg.cra_ctxsize = sizeof(struct eseqiv_ctx);
209 inst->alg.cra_ctxsize += inst->alg.cra_ablkcipher.ivsize;
210
211out:
212 return inst;
213
214free_inst:
215 skcipher_geniv_free(inst);
216 inst = ERR_PTR(err);
217 goto out;
218}
219
220static struct crypto_template eseqiv_tmpl = {
221 .name = "eseqiv",
222 .alloc = eseqiv_alloc,
Herbert Xu055906d2015-06-21 19:11:47 +0800223 .free = skcipher_geniv_free,
Herbert Xu806d1832007-12-05 12:10:53 +1100224 .module = THIS_MODULE,
225};
226
Herbert Xu5be5e662008-08-17 18:04:30 +1000227static int __init eseqiv_module_init(void)
Herbert Xu806d1832007-12-05 12:10:53 +1100228{
229 return crypto_register_template(&eseqiv_tmpl);
230}
231
Herbert Xu5be5e662008-08-17 18:04:30 +1000232static void __exit eseqiv_module_exit(void)
Herbert Xu806d1832007-12-05 12:10:53 +1100233{
234 crypto_unregister_template(&eseqiv_tmpl);
235}
Herbert Xu5be5e662008-08-17 18:04:30 +1000236
237module_init(eseqiv_module_init);
238module_exit(eseqiv_module_exit);
239
240MODULE_LICENSE("GPL");
241MODULE_DESCRIPTION("Encrypted Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800242MODULE_ALIAS_CRYPTO("eseqiv");