blob: a49cbf7b09294c1eaa751f82b460e2634d740787 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xua10f5542015-05-21 15:11:15 +08002/*
3 * echainiv: Encrypted Chain IV Generator
4 *
Herbert Xu53a5d5d2016-09-07 18:42:08 +08005 * This generator generates an IV based on a sequence number by multiplying
6 * it with a salt and then encrypting it with the same key as used to encrypt
Herbert Xua10f5542015-05-21 15:11:15 +08007 * the plain text. This algorithm requires that the block size be equal
8 * to the IV size. It is mainly useful for CBC.
9 *
10 * This generator can only be used by algorithms where authentication
11 * is performed after encryption (i.e., authenc).
12 *
13 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xua10f5542015-05-21 15:11:15 +080014 */
15
Herbert Xud97de472015-05-27 14:37:33 +080016#include <crypto/internal/geniv.h>
Herbert Xua10f5542015-05-21 15:11:15 +080017#include <crypto/scatterwalk.h>
Herbert Xu0e8bff42016-07-12 13:17:43 +080018#include <crypto/skcipher.h>
Herbert Xua10f5542015-05-21 15:11:15 +080019#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
Herbert Xua10f5542015-05-21 15:11:15 +080022#include <linux/module.h>
Herbert Xu53a5d5d2016-09-07 18:42:08 +080023#include <linux/slab.h>
Herbert Xua10f5542015-05-21 15:11:15 +080024#include <linux/string.h>
25
Herbert Xua10f5542015-05-21 15:11:15 +080026static int echainiv_encrypt(struct aead_request *req)
27{
28 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu376e0d62015-08-13 17:29:01 +080029 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xua10f5542015-05-21 15:11:15 +080030 struct aead_request *subreq = aead_request_ctx(req);
Herbert Xu53a5d5d2016-09-07 18:42:08 +080031 __be64 nseqno;
32 u64 seqno;
Herbert Xua10f5542015-05-21 15:11:15 +080033 u8 *info;
Herbert Xu823655c2015-05-23 15:41:54 +080034 unsigned int ivsize = crypto_aead_ivsize(geniv);
Herbert Xua10f5542015-05-21 15:11:15 +080035 int err;
36
Herbert Xu823655c2015-05-23 15:41:54 +080037 if (req->cryptlen < ivsize)
38 return -EINVAL;
39
Herbert Xu376e0d62015-08-13 17:29:01 +080040 aead_request_set_tfm(subreq, ctx->child);
Herbert Xua10f5542015-05-21 15:11:15 +080041
Herbert Xua10f5542015-05-21 15:11:15 +080042 info = req->iv;
43
Herbert Xua10f5542015-05-21 15:11:15 +080044 if (req->src != req->dst) {
Kees Cook8d605392018-09-18 19:10:51 -070045 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
Herbert Xua10f5542015-05-21 15:11:15 +080046
Kees Cook8d605392018-09-18 19:10:51 -070047 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
Herbert Xu0e8bff42016-07-12 13:17:43 +080048 skcipher_request_set_callback(nreq, req->base.flags,
49 NULL, NULL);
50 skcipher_request_set_crypt(nreq, req->src, req->dst,
51 req->assoclen + req->cryptlen,
52 NULL);
53
54 err = crypto_skcipher_encrypt(nreq);
Herbert Xua10f5542015-05-21 15:11:15 +080055 if (err)
56 return err;
57 }
58
Herbert Xu53a5d5d2016-09-07 18:42:08 +080059 aead_request_set_callback(subreq, req->base.flags,
60 req->base.complete, req->base.data);
Herbert Xua10f5542015-05-21 15:11:15 +080061 aead_request_set_crypt(subreq, req->dst, req->dst,
Herbert Xu5499b1a2015-07-09 07:17:20 +080062 req->cryptlen, info);
63 aead_request_set_ad(subreq, req->assoclen);
Herbert Xua10f5542015-05-21 15:11:15 +080064
Herbert Xu53a5d5d2016-09-07 18:42:08 +080065 memcpy(&nseqno, info + ivsize - 8, 8);
66 seqno = be64_to_cpu(nseqno);
67 memset(info, 0, ivsize);
Herbert Xua10f5542015-05-21 15:11:15 +080068
Herbert Xu53a5d5d2016-09-07 18:42:08 +080069 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
70
71 do {
72 u64 a;
73
74 memcpy(&a, ctx->salt + ivsize - 8, 8);
75
76 a |= 1;
77 a *= seqno;
78
79 memcpy(info + ivsize - 8, &a, 8);
80 } while ((ivsize -= 8));
81
82 return crypto_aead_encrypt(subreq);
Herbert Xua10f5542015-05-21 15:11:15 +080083}
84
Herbert Xua10f5542015-05-21 15:11:15 +080085static int echainiv_decrypt(struct aead_request *req)
86{
87 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu376e0d62015-08-13 17:29:01 +080088 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xua10f5542015-05-21 15:11:15 +080089 struct aead_request *subreq = aead_request_ctx(req);
90 crypto_completion_t compl;
91 void *data;
Herbert Xu823655c2015-05-23 15:41:54 +080092 unsigned int ivsize = crypto_aead_ivsize(geniv);
93
Herbert Xu5499b1a2015-07-09 07:17:20 +080094 if (req->cryptlen < ivsize)
Herbert Xu823655c2015-05-23 15:41:54 +080095 return -EINVAL;
Herbert Xua10f5542015-05-21 15:11:15 +080096
Herbert Xu376e0d62015-08-13 17:29:01 +080097 aead_request_set_tfm(subreq, ctx->child);
Herbert Xua10f5542015-05-21 15:11:15 +080098
99 compl = req->base.complete;
100 data = req->base.data;
101
Herbert Xua10f5542015-05-21 15:11:15 +0800102 aead_request_set_callback(subreq, req->base.flags, compl, data);
103 aead_request_set_crypt(subreq, req->src, req->dst,
104 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800105 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xua10f5542015-05-21 15:11:15 +0800106
107 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
Herbert Xua10f5542015-05-21 15:11:15 +0800108
109 return crypto_aead_decrypt(subreq);
110}
111
Herbert Xu1e419c72015-05-23 15:41:52 +0800112static int echainiv_aead_create(struct crypto_template *tmpl,
113 struct rtattr **tb)
Herbert Xua10f5542015-05-21 15:11:15 +0800114{
115 struct aead_instance *inst;
Herbert Xu1e419c72015-05-23 15:41:52 +0800116 int err;
Herbert Xua10f5542015-05-21 15:11:15 +0800117
Herbert Xu1e419c72015-05-23 15:41:52 +0800118 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xua10f5542015-05-21 15:11:15 +0800119
120 if (IS_ERR(inst))
Herbert Xu1e419c72015-05-23 15:41:52 +0800121 return PTR_ERR(inst);
Herbert Xua10f5542015-05-21 15:11:15 +0800122
Herbert Xud97de472015-05-27 14:37:33 +0800123 err = -EINVAL;
Herbert Xu53a5d5d2016-09-07 18:42:08 +0800124 if (inst->alg.ivsize & (sizeof(u64) - 1) || !inst->alg.ivsize)
Herbert Xud97de472015-05-27 14:37:33 +0800125 goto free_inst;
126
Herbert Xuf261c5f2015-06-03 14:49:24 +0800127 inst->alg.encrypt = echainiv_encrypt;
Herbert Xua10f5542015-05-21 15:11:15 +0800128 inst->alg.decrypt = echainiv_decrypt;
129
Herbert Xu376e0d62015-08-13 17:29:01 +0800130 inst->alg.init = aead_init_geniv;
131 inst->alg.exit = aead_exit_geniv;
Herbert Xua10f5542015-05-21 15:11:15 +0800132
Herbert Xu376e0d62015-08-13 17:29:01 +0800133 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
Herbert Xu9d03aee2015-05-27 14:37:34 +0800134 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xua10f5542015-05-21 15:11:15 +0800135
Herbert Xu5499b1a2015-07-09 07:17:20 +0800136 inst->free = aead_geniv_free;
137
Herbert Xu1e419c72015-05-23 15:41:52 +0800138 err = aead_register_instance(tmpl, inst);
139 if (err)
140 goto free_inst;
141
Herbert Xua10f5542015-05-21 15:11:15 +0800142out:
Herbert Xu1e419c72015-05-23 15:41:52 +0800143 return err;
144
145free_inst:
146 aead_geniv_free(inst);
147 goto out;
Herbert Xua10f5542015-05-21 15:11:15 +0800148}
149
Herbert Xua10f5542015-05-21 15:11:15 +0800150static void echainiv_free(struct crypto_instance *inst)
151{
152 aead_geniv_free(aead_instance(inst));
Herbert Xua10f5542015-05-21 15:11:15 +0800153}
154
155static struct crypto_template echainiv_tmpl = {
156 .name = "echainiv",
Herbert Xu9fcc7042015-06-21 19:11:50 +0800157 .create = echainiv_aead_create,
Herbert Xua10f5542015-05-21 15:11:15 +0800158 .free = echainiv_free,
159 .module = THIS_MODULE,
160};
161
162static int __init echainiv_module_init(void)
163{
164 return crypto_register_template(&echainiv_tmpl);
165}
166
167static void __exit echainiv_module_exit(void)
168{
169 crypto_unregister_template(&echainiv_tmpl);
170}
171
Eric Biggersc4741b22019-04-11 21:57:42 -0700172subsys_initcall(echainiv_module_init);
Herbert Xua10f5542015-05-21 15:11:15 +0800173module_exit(echainiv_module_exit);
174
175MODULE_LICENSE("GPL");
176MODULE_DESCRIPTION("Encrypted Chain IV Generator");
177MODULE_ALIAS_CRYPTO("echainiv");