blob: 08d33367801d090bb49d1cc44fcc87931f98cefb [file] [log] [blame]
Herbert Xua10f5542015-05-21 15:11:15 +08001/*
2 * echainiv: Encrypted Chain 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 * This generator can only be used by algorithms where authentication
10 * is performed after encryption (i.e., authenc).
11 *
12 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at your option)
17 * any later version.
18 *
19 */
20
Herbert Xud97de472015-05-27 14:37:33 +080021#include <crypto/internal/geniv.h>
Herbert Xua10f5542015-05-21 15:11:15 +080022#include <crypto/null.h>
23#include <crypto/rng.h>
24#include <crypto/scatterwalk.h>
25#include <linux/err.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/mm.h>
29#include <linux/module.h>
30#include <linux/percpu.h>
31#include <linux/spinlock.h>
32#include <linux/string.h>
33
34#define MAX_IV_SIZE 16
35
Herbert Xua10f5542015-05-21 15:11:15 +080036struct echainiv_ctx {
Herbert Xud97de472015-05-27 14:37:33 +080037 /* aead_geniv_ctx must be first the element */
38 struct aead_geniv_ctx geniv;
Herbert Xua10f5542015-05-21 15:11:15 +080039 struct crypto_blkcipher *null;
40 u8 salt[] __attribute__ ((aligned(__alignof__(u32))));
41};
42
43static DEFINE_PER_CPU(u32 [MAX_IV_SIZE / sizeof(u32)], echainiv_iv);
44
Herbert Xua10f5542015-05-21 15:11:15 +080045/* We don't care if we get preempted and read/write IVs from the next CPU. */
Wu Fengguang622ff872015-05-23 11:22:47 +080046static void echainiv_read_iv(u8 *dst, unsigned size)
Herbert Xua10f5542015-05-21 15:11:15 +080047{
48 u32 *a = (u32 *)dst;
49 u32 __percpu *b = echainiv_iv;
50
51 for (; size >= 4; size -= 4) {
52 *a++ = this_cpu_read(*b);
53 b++;
54 }
55}
56
Wu Fengguang622ff872015-05-23 11:22:47 +080057static void echainiv_write_iv(const u8 *src, unsigned size)
Herbert Xua10f5542015-05-21 15:11:15 +080058{
59 const u32 *a = (const u32 *)src;
60 u32 __percpu *b = echainiv_iv;
61
62 for (; size >= 4; size -= 4) {
63 this_cpu_write(*b, *a);
64 a++;
65 b++;
66 }
67}
68
Herbert Xua10f5542015-05-21 15:11:15 +080069static void echainiv_encrypt_complete2(struct aead_request *req, int err)
70{
71 struct aead_request *subreq = aead_request_ctx(req);
72 struct crypto_aead *geniv;
73 unsigned int ivsize;
74
75 if (err == -EINPROGRESS)
76 return;
77
78 if (err)
79 goto out;
80
81 geniv = crypto_aead_reqtfm(req);
82 ivsize = crypto_aead_ivsize(geniv);
83
84 echainiv_write_iv(subreq->iv, ivsize);
85
86 if (req->iv != subreq->iv)
87 memcpy(req->iv, subreq->iv, ivsize);
88
89out:
90 if (req->iv != subreq->iv)
91 kzfree(subreq->iv);
92}
93
94static void echainiv_encrypt_complete(struct crypto_async_request *base,
95 int err)
96{
97 struct aead_request *req = base->data;
98
99 echainiv_encrypt_complete2(req, err);
100 aead_request_complete(req, err);
101}
102
Herbert Xua10f5542015-05-21 15:11:15 +0800103static int echainiv_encrypt(struct aead_request *req)
104{
105 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
106 struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
107 struct aead_request *subreq = aead_request_ctx(req);
108 crypto_completion_t compl;
109 void *data;
110 u8 *info;
Herbert Xu823655c2015-05-23 15:41:54 +0800111 unsigned int ivsize = crypto_aead_ivsize(geniv);
Herbert Xua10f5542015-05-21 15:11:15 +0800112 int err;
113
Herbert Xu823655c2015-05-23 15:41:54 +0800114 if (req->cryptlen < ivsize)
115 return -EINVAL;
116
Herbert Xud97de472015-05-27 14:37:33 +0800117 aead_request_set_tfm(subreq, ctx->geniv.child);
Herbert Xua10f5542015-05-21 15:11:15 +0800118
119 compl = echainiv_encrypt_complete;
120 data = req;
121 info = req->iv;
122
Herbert Xua10f5542015-05-21 15:11:15 +0800123 if (req->src != req->dst) {
Herbert Xua10f5542015-05-21 15:11:15 +0800124 struct blkcipher_desc desc = {
125 .tfm = ctx->null,
126 };
127
128 err = crypto_blkcipher_encrypt(
Herbert Xu838c9d562015-05-27 14:37:31 +0800129 &desc, req->dst, req->src,
130 req->assoclen + req->cryptlen);
Herbert Xua10f5542015-05-21 15:11:15 +0800131 if (err)
132 return err;
133 }
134
135 if (unlikely(!IS_ALIGNED((unsigned long)info,
136 crypto_aead_alignmask(geniv) + 1))) {
137 info = kmalloc(ivsize, req->base.flags &
138 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
139 GFP_ATOMIC);
140 if (!info)
141 return -ENOMEM;
142
143 memcpy(info, req->iv, ivsize);
144 }
145
146 aead_request_set_callback(subreq, req->base.flags, compl, data);
147 aead_request_set_crypt(subreq, req->dst, req->dst,
148 req->cryptlen - ivsize, info);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800149 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xua10f5542015-05-21 15:11:15 +0800150
151 crypto_xor(info, ctx->salt, ivsize);
152 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
153 echainiv_read_iv(info, ivsize);
154
155 err = crypto_aead_encrypt(subreq);
156 echainiv_encrypt_complete2(req, err);
157 return err;
158}
159
Herbert Xua10f5542015-05-21 15:11:15 +0800160static int echainiv_decrypt(struct aead_request *req)
161{
162 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
163 struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
164 struct aead_request *subreq = aead_request_ctx(req);
165 crypto_completion_t compl;
166 void *data;
Herbert Xu823655c2015-05-23 15:41:54 +0800167 unsigned int ivsize = crypto_aead_ivsize(geniv);
168
169 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
170 return -EINVAL;
Herbert Xua10f5542015-05-21 15:11:15 +0800171
Herbert Xud97de472015-05-27 14:37:33 +0800172 aead_request_set_tfm(subreq, ctx->geniv.child);
Herbert Xua10f5542015-05-21 15:11:15 +0800173
174 compl = req->base.complete;
175 data = req->base.data;
176
Herbert Xua10f5542015-05-21 15:11:15 +0800177 aead_request_set_callback(subreq, req->base.flags, compl, data);
178 aead_request_set_crypt(subreq, req->src, req->dst,
179 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800180 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xua10f5542015-05-21 15:11:15 +0800181
182 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
183 if (req->src != req->dst)
184 scatterwalk_map_and_copy(req->iv, req->dst,
185 req->assoclen, ivsize, 1);
186
187 return crypto_aead_decrypt(subreq);
188}
189
Herbert Xua10f5542015-05-21 15:11:15 +0800190static int echainiv_init(struct crypto_tfm *tfm)
191{
192 struct crypto_aead *geniv = __crypto_aead_cast(tfm);
193 struct echainiv_ctx *ctx = crypto_aead_ctx(geniv);
194 int err;
195
Herbert Xud97de472015-05-27 14:37:33 +0800196 spin_lock_init(&ctx->geniv.lock);
Herbert Xua10f5542015-05-21 15:11:15 +0800197
198 crypto_aead_set_reqsize(geniv, sizeof(struct aead_request));
199
Herbert Xuf261c5f2015-06-03 14:49:24 +0800200 err = crypto_rng_get_bytes(crypto_default_rng, ctx->salt,
201 crypto_aead_ivsize(geniv));
202 if (err)
203 goto out;
204
Herbert Xua10f5542015-05-21 15:11:15 +0800205 ctx->null = crypto_get_default_null_skcipher();
206 err = PTR_ERR(ctx->null);
207 if (IS_ERR(ctx->null))
208 goto out;
209
210 err = aead_geniv_init(tfm);
211 if (err)
212 goto drop_null;
213
Herbert Xud97de472015-05-27 14:37:33 +0800214 ctx->geniv.child = geniv->child;
Herbert Xua10f5542015-05-21 15:11:15 +0800215 geniv->child = geniv;
216
217out:
218 return err;
219
220drop_null:
221 crypto_put_default_null_skcipher();
222 goto out;
223}
224
Herbert Xua10f5542015-05-21 15:11:15 +0800225static void echainiv_exit(struct crypto_tfm *tfm)
226{
227 struct echainiv_ctx *ctx = crypto_tfm_ctx(tfm);
228
Herbert Xud97de472015-05-27 14:37:33 +0800229 crypto_free_aead(ctx->geniv.child);
Herbert Xua10f5542015-05-21 15:11:15 +0800230 crypto_put_default_null_skcipher();
231}
232
Herbert Xu1e419c72015-05-23 15:41:52 +0800233static int echainiv_aead_create(struct crypto_template *tmpl,
234 struct rtattr **tb)
Herbert Xua10f5542015-05-21 15:11:15 +0800235{
236 struct aead_instance *inst;
237 struct crypto_aead_spawn *spawn;
238 struct aead_alg *alg;
Herbert Xu1e419c72015-05-23 15:41:52 +0800239 int err;
Herbert Xua10f5542015-05-21 15:11:15 +0800240
Herbert Xu1e419c72015-05-23 15:41:52 +0800241 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xua10f5542015-05-21 15:11:15 +0800242
243 if (IS_ERR(inst))
Herbert Xu1e419c72015-05-23 15:41:52 +0800244 return PTR_ERR(inst);
Herbert Xua10f5542015-05-21 15:11:15 +0800245
Herbert Xua10f5542015-05-21 15:11:15 +0800246 spawn = aead_instance_ctx(inst);
247 alg = crypto_spawn_aead_alg(spawn);
248
Herbert Xud97de472015-05-27 14:37:33 +0800249 if (alg->base.cra_aead.encrypt)
250 goto done;
251
252 err = -EINVAL;
253 if (inst->alg.ivsize & (sizeof(u32) - 1) ||
254 inst->alg.ivsize > MAX_IV_SIZE)
255 goto free_inst;
256
Herbert Xuf261c5f2015-06-03 14:49:24 +0800257 inst->alg.encrypt = echainiv_encrypt;
Herbert Xua10f5542015-05-21 15:11:15 +0800258 inst->alg.decrypt = echainiv_decrypt;
259
260 inst->alg.base.cra_init = echainiv_init;
261 inst->alg.base.cra_exit = echainiv_exit;
262
263 inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
264 inst->alg.base.cra_ctxsize = sizeof(struct echainiv_ctx);
Herbert Xu9d03aee2015-05-27 14:37:34 +0800265 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xua10f5542015-05-21 15:11:15 +0800266
Herbert Xud97de472015-05-27 14:37:33 +0800267done:
Herbert Xu1e419c72015-05-23 15:41:52 +0800268 err = aead_register_instance(tmpl, inst);
269 if (err)
270 goto free_inst;
271
Herbert Xua10f5542015-05-21 15:11:15 +0800272out:
Herbert Xu1e419c72015-05-23 15:41:52 +0800273 return err;
274
275free_inst:
276 aead_geniv_free(inst);
277 goto out;
Herbert Xua10f5542015-05-21 15:11:15 +0800278}
279
Herbert Xu1e419c72015-05-23 15:41:52 +0800280static int echainiv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xua10f5542015-05-21 15:11:15 +0800281{
Herbert Xua10f5542015-05-21 15:11:15 +0800282 int err;
283
284 err = crypto_get_default_rng();
285 if (err)
Herbert Xu1e419c72015-05-23 15:41:52 +0800286 goto out;
Herbert Xua10f5542015-05-21 15:11:15 +0800287
Herbert Xu1e419c72015-05-23 15:41:52 +0800288 err = echainiv_aead_create(tmpl, tb);
289 if (err)
Herbert Xua10f5542015-05-21 15:11:15 +0800290 goto put_rng;
291
292out:
Herbert Xu1e419c72015-05-23 15:41:52 +0800293 return err;
Herbert Xua10f5542015-05-21 15:11:15 +0800294
295put_rng:
296 crypto_put_default_rng();
297 goto out;
298}
299
300static void echainiv_free(struct crypto_instance *inst)
301{
302 aead_geniv_free(aead_instance(inst));
303 crypto_put_default_rng();
304}
305
306static struct crypto_template echainiv_tmpl = {
307 .name = "echainiv",
Herbert Xu1e419c72015-05-23 15:41:52 +0800308 .create = echainiv_create,
Herbert Xua10f5542015-05-21 15:11:15 +0800309 .free = echainiv_free,
310 .module = THIS_MODULE,
311};
312
313static int __init echainiv_module_init(void)
314{
315 return crypto_register_template(&echainiv_tmpl);
316}
317
318static void __exit echainiv_module_exit(void)
319{
320 crypto_unregister_template(&echainiv_tmpl);
321}
322
323module_init(echainiv_module_init);
324module_exit(echainiv_module_exit);
325
326MODULE_LICENSE("GPL");
327MODULE_DESCRIPTION("Encrypted Chain IV Generator");
328MODULE_ALIAS_CRYPTO("echainiv");