Merge 4.4.68 into android-4.4
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / crypto / echainiv.c
CommitLineData
a10f554f
HX
1/*
2 * echainiv: Encrypted Chain IV Generator
3 *
2426cdb3
HX
4 * This generator generates an IV based on a sequence number by multiplying
5 * it with a salt and then encrypting it with the same key as used to encrypt
a10f554f
HX
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
d97de47c 21#include <crypto/internal/geniv.h>
a10f554f
HX
22#include <crypto/scatterwalk.h>
23#include <linux/err.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
a10f554f 26#include <linux/module.h>
2426cdb3 27#include <linux/slab.h>
a10f554f
HX
28#include <linux/string.h>
29
a10f554f
HX
30static int echainiv_encrypt(struct aead_request *req)
31{
32 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
376e0d69 33 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
a10f554f 34 struct aead_request *subreq = aead_request_ctx(req);
2426cdb3
HX
35 __be64 nseqno;
36 u64 seqno;
a10f554f 37 u8 *info;
823655c9 38 unsigned int ivsize = crypto_aead_ivsize(geniv);
a10f554f
HX
39 int err;
40
823655c9
HX
41 if (req->cryptlen < ivsize)
42 return -EINVAL;
43
376e0d69 44 aead_request_set_tfm(subreq, ctx->child);
a10f554f 45
a10f554f
HX
46 info = req->iv;
47
a10f554f 48 if (req->src != req->dst) {
a10f554f
HX
49 struct blkcipher_desc desc = {
50 .tfm = ctx->null,
51 };
52
53 err = crypto_blkcipher_encrypt(
838c9d56
HX
54 &desc, req->dst, req->src,
55 req->assoclen + req->cryptlen);
a10f554f
HX
56 if (err)
57 return err;
58 }
59
2426cdb3
HX
60 aead_request_set_callback(subreq, req->base.flags,
61 req->base.complete, req->base.data);
a10f554f 62 aead_request_set_crypt(subreq, req->dst, req->dst,
5499b1a7
HX
63 req->cryptlen, info);
64 aead_request_set_ad(subreq, req->assoclen);
a10f554f 65
2426cdb3
HX
66 memcpy(&nseqno, info + ivsize - 8, 8);
67 seqno = be64_to_cpu(nseqno);
68 memset(info, 0, ivsize);
69
a10f554f 70 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
a10f554f 71
2426cdb3
HX
72 do {
73 u64 a;
74
75 memcpy(&a, ctx->salt + ivsize - 8, 8);
76
77 a |= 1;
78 a *= seqno;
79
80 memcpy(info + ivsize - 8, &a, 8);
81 } while ((ivsize -= 8));
82
83 return crypto_aead_encrypt(subreq);
a10f554f
HX
84}
85
a10f554f
HX
86static int echainiv_decrypt(struct aead_request *req)
87{
88 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
376e0d69 89 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
a10f554f
HX
90 struct aead_request *subreq = aead_request_ctx(req);
91 crypto_completion_t compl;
92 void *data;
823655c9
HX
93 unsigned int ivsize = crypto_aead_ivsize(geniv);
94
5499b1a7 95 if (req->cryptlen < ivsize)
823655c9 96 return -EINVAL;
a10f554f 97
376e0d69 98 aead_request_set_tfm(subreq, ctx->child);
a10f554f
HX
99
100 compl = req->base.complete;
101 data = req->base.data;
102
a10f554f
HX
103 aead_request_set_callback(subreq, req->base.flags, compl, data);
104 aead_request_set_crypt(subreq, req->src, req->dst,
105 req->cryptlen - ivsize, req->iv);
374d4ad1 106 aead_request_set_ad(subreq, req->assoclen + ivsize);
a10f554f
HX
107
108 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
a10f554f
HX
109
110 return crypto_aead_decrypt(subreq);
111}
112
1e419c79
HX
113static int echainiv_aead_create(struct crypto_template *tmpl,
114 struct rtattr **tb)
a10f554f
HX
115{
116 struct aead_instance *inst;
117 struct crypto_aead_spawn *spawn;
118 struct aead_alg *alg;
1e419c79 119 int err;
a10f554f 120
1e419c79 121 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
a10f554f
HX
122
123 if (IS_ERR(inst))
1e419c79 124 return PTR_ERR(inst);
a10f554f 125
d97de47c
HX
126 spawn = aead_instance_ctx(inst);
127 alg = crypto_spawn_aead_alg(spawn);
128
1e419c79 129 err = -EINVAL;
2426cdb3 130 if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
1e419c79 131 goto free_inst;
a10f554f 132
f261c5fb 133 inst->alg.encrypt = echainiv_encrypt;
a10f554f
HX
134 inst->alg.decrypt = echainiv_decrypt;
135
376e0d69
HX
136 inst->alg.init = aead_init_geniv;
137 inst->alg.exit = aead_exit_geniv;
a10f554f 138
376e0d69 139 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
9d03aee1 140 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
a10f554f 141
5499b1a7
HX
142 inst->free = aead_geniv_free;
143
1e419c79
HX
144 err = aead_register_instance(tmpl, inst);
145 if (err)
146 goto free_inst;
147
a10f554f 148out:
1e419c79
HX
149 return err;
150
151free_inst:
152 aead_geniv_free(inst);
153 goto out;
a10f554f
HX
154}
155
a10f554f
HX
156static void echainiv_free(struct crypto_instance *inst)
157{
158 aead_geniv_free(aead_instance(inst));
a10f554f
HX
159}
160
161static struct crypto_template echainiv_tmpl = {
162 .name = "echainiv",
9fcc704d 163 .create = echainiv_aead_create,
a10f554f
HX
164 .free = echainiv_free,
165 .module = THIS_MODULE,
166};
167
168static int __init echainiv_module_init(void)
169{
170 return crypto_register_template(&echainiv_tmpl);
171}
172
173static void __exit echainiv_module_exit(void)
174{
175 crypto_unregister_template(&echainiv_tmpl);
176}
177
178module_init(echainiv_module_init);
179module_exit(echainiv_module_exit);
180
181MODULE_LICENSE("GPL");
182MODULE_DESCRIPTION("Encrypted Chain IV Generator");
183MODULE_ALIAS_CRYPTO("echainiv");