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