blob: 45819e6015bf408c927f940024f22661bdbc7e06 [file] [log] [blame]
Herbert Xua10f5542015-05-21 15:11:15 +08001/*
2 * echainiv: Encrypted Chain IV Generator
3 *
Herbert Xu53a5d5d2016-09-07 18:42:08 +08004 * 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
Herbert Xua10f5542015-05-21 15:11:15 +08006 * 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/scatterwalk.h>
Herbert Xu0e8bff42016-07-12 13:17:43 +080023#include <crypto/skcipher.h>
Herbert Xua10f5542015-05-21 15:11:15 +080024#include <linux/err.h>
25#include <linux/init.h>
26#include <linux/kernel.h>
Herbert Xua10f5542015-05-21 15:11:15 +080027#include <linux/module.h>
Herbert Xu53a5d5d2016-09-07 18:42:08 +080028#include <linux/slab.h>
Herbert Xua10f5542015-05-21 15:11:15 +080029#include <linux/string.h>
30
Herbert Xua10f5542015-05-21 15:11:15 +080031static int echainiv_encrypt(struct aead_request *req)
32{
33 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu376e0d62015-08-13 17:29:01 +080034 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xua10f5542015-05-21 15:11:15 +080035 struct aead_request *subreq = aead_request_ctx(req);
Herbert Xu53a5d5d2016-09-07 18:42:08 +080036 __be64 nseqno;
37 u64 seqno;
Herbert Xua10f5542015-05-21 15:11:15 +080038 u8 *info;
Herbert Xu823655c2015-05-23 15:41:54 +080039 unsigned int ivsize = crypto_aead_ivsize(geniv);
Herbert Xua10f5542015-05-21 15:11:15 +080040 int err;
41
Herbert Xu823655c2015-05-23 15:41:54 +080042 if (req->cryptlen < ivsize)
43 return -EINVAL;
44
Herbert Xu376e0d62015-08-13 17:29:01 +080045 aead_request_set_tfm(subreq, ctx->child);
Herbert Xua10f5542015-05-21 15:11:15 +080046
Herbert Xua10f5542015-05-21 15:11:15 +080047 info = req->iv;
48
Herbert Xua10f5542015-05-21 15:11:15 +080049 if (req->src != req->dst) {
Herbert Xu0e8bff42016-07-12 13:17:43 +080050 SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
Herbert Xua10f5542015-05-21 15:11:15 +080051
Herbert Xu0e8bff42016-07-12 13:17:43 +080052 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);
Herbert Xua10f5542015-05-21 15:11:15 +080060 if (err)
61 return err;
62 }
63
Herbert Xu53a5d5d2016-09-07 18:42:08 +080064 aead_request_set_callback(subreq, req->base.flags,
65 req->base.complete, req->base.data);
Herbert Xua10f5542015-05-21 15:11:15 +080066 aead_request_set_crypt(subreq, req->dst, req->dst,
Herbert Xu5499b1a2015-07-09 07:17:20 +080067 req->cryptlen, info);
68 aead_request_set_ad(subreq, req->assoclen);
Herbert Xua10f5542015-05-21 15:11:15 +080069
Herbert Xu53a5d5d2016-09-07 18:42:08 +080070 memcpy(&nseqno, info + ivsize - 8, 8);
71 seqno = be64_to_cpu(nseqno);
72 memset(info, 0, ivsize);
Herbert Xua10f5542015-05-21 15:11:15 +080073
Herbert Xu53a5d5d2016-09-07 18:42:08 +080074 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
75
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);
Herbert Xua10f5542015-05-21 15:11:15 +080088}
89
Herbert Xua10f5542015-05-21 15:11:15 +080090static int echainiv_decrypt(struct aead_request *req)
91{
92 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu376e0d62015-08-13 17:29:01 +080093 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xua10f5542015-05-21 15:11:15 +080094 struct aead_request *subreq = aead_request_ctx(req);
95 crypto_completion_t compl;
96 void *data;
Herbert Xu823655c2015-05-23 15:41:54 +080097 unsigned int ivsize = crypto_aead_ivsize(geniv);
98
Herbert Xu5499b1a2015-07-09 07:17:20 +080099 if (req->cryptlen < ivsize)
Herbert Xu823655c2015-05-23 15:41:54 +0800100 return -EINVAL;
Herbert Xua10f5542015-05-21 15:11:15 +0800101
Herbert Xu376e0d62015-08-13 17:29:01 +0800102 aead_request_set_tfm(subreq, ctx->child);
Herbert Xua10f5542015-05-21 15:11:15 +0800103
104 compl = req->base.complete;
105 data = req->base.data;
106
Herbert Xua10f5542015-05-21 15:11:15 +0800107 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);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800110 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xua10f5542015-05-21 15:11:15 +0800111
112 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
Herbert Xua10f5542015-05-21 15:11:15 +0800113
114 return crypto_aead_decrypt(subreq);
115}
116
Herbert Xu1e419c72015-05-23 15:41:52 +0800117static int echainiv_aead_create(struct crypto_template *tmpl,
118 struct rtattr **tb)
Herbert Xua10f5542015-05-21 15:11:15 +0800119{
120 struct aead_instance *inst;
Herbert Xu1e419c72015-05-23 15:41:52 +0800121 int err;
Herbert Xua10f5542015-05-21 15:11:15 +0800122
Herbert Xu1e419c72015-05-23 15:41:52 +0800123 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xua10f5542015-05-21 15:11:15 +0800124
125 if (IS_ERR(inst))
Herbert Xu1e419c72015-05-23 15:41:52 +0800126 return PTR_ERR(inst);
Herbert Xua10f5542015-05-21 15:11:15 +0800127
Herbert Xud97de472015-05-27 14:37:33 +0800128 err = -EINVAL;
Herbert Xu53a5d5d2016-09-07 18:42:08 +0800129 if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
Herbert Xud97de472015-05-27 14:37:33 +0800130 goto free_inst;
131
Herbert Xuf261c5f2015-06-03 14:49:24 +0800132 inst->alg.encrypt = echainiv_encrypt;
Herbert Xua10f5542015-05-21 15:11:15 +0800133 inst->alg.decrypt = echainiv_decrypt;
134
Herbert Xu376e0d62015-08-13 17:29:01 +0800135 inst->alg.init = aead_init_geniv;
136 inst->alg.exit = aead_exit_geniv;
Herbert Xua10f5542015-05-21 15:11:15 +0800137
Herbert Xu376e0d62015-08-13 17:29:01 +0800138 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
Herbert Xu9d03aee2015-05-27 14:37:34 +0800139 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xua10f5542015-05-21 15:11:15 +0800140
Herbert Xu5499b1a2015-07-09 07:17:20 +0800141 inst->free = aead_geniv_free;
142
Herbert Xu1e419c72015-05-23 15:41:52 +0800143 err = aead_register_instance(tmpl, inst);
144 if (err)
145 goto free_inst;
146
Herbert Xua10f5542015-05-21 15:11:15 +0800147out:
Herbert Xu1e419c72015-05-23 15:41:52 +0800148 return err;
149
150free_inst:
151 aead_geniv_free(inst);
152 goto out;
Herbert Xua10f5542015-05-21 15:11:15 +0800153}
154
Herbert Xua10f5542015-05-21 15:11:15 +0800155static void echainiv_free(struct crypto_instance *inst)
156{
157 aead_geniv_free(aead_instance(inst));
Herbert Xua10f5542015-05-21 15:11:15 +0800158}
159
160static struct crypto_template echainiv_tmpl = {
161 .name = "echainiv",
Herbert Xu9fcc7042015-06-21 19:11:50 +0800162 .create = echainiv_aead_create,
Herbert Xua10f5542015-05-21 15:11:15 +0800163 .free = echainiv_free,
164 .module = THIS_MODULE,
165};
166
167static int __init echainiv_module_init(void)
168{
169 return crypto_register_template(&echainiv_tmpl);
170}
171
172static void __exit echainiv_module_exit(void)
173{
174 crypto_unregister_template(&echainiv_tmpl);
175}
176
177module_init(echainiv_module_init);
178module_exit(echainiv_module_exit);
179
180MODULE_LICENSE("GPL");
181MODULE_DESCRIPTION("Encrypted Chain IV Generator");
182MODULE_ALIAS_CRYPTO("echainiv");