blob: 96d222c32accce34dc35a584a87be595e8725ba9 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Herbert Xu0a270322007-11-30 21:38:37 +11002/*
3 * seqiv: Sequence Number IV Generator
4 *
5 * This generator generates an IV based on a sequence number by xoring it
6 * with a salt. This algorithm is mainly useful for CTR and similar modes.
7 *
8 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
Herbert Xu0a270322007-11-30 21:38:37 +11009 */
10
Herbert Xu661cfd02015-05-27 14:37:37 +080011#include <crypto/internal/geniv.h>
Herbert Xu856e3f402015-05-21 15:11:13 +080012#include <crypto/scatterwalk.h>
Herbert Xu3a01d0e2016-07-12 13:17:50 +080013#include <crypto/skcipher.h>
Herbert Xu0a270322007-11-30 21:38:37 +110014#include <linux/err.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Herbert Xu0a270322007-11-30 21:38:37 +110019#include <linux/string.h>
20
Herbert Xu06771572015-05-23 15:41:51 +080021static void seqiv_free(struct crypto_instance *inst);
22
Herbert Xu856e3f402015-05-21 15:11:13 +080023static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
24{
25 struct aead_request *subreq = aead_request_ctx(req);
26 struct crypto_aead *geniv;
27
28 if (err == -EINPROGRESS)
29 return;
30
31 if (err)
32 goto out;
33
34 geniv = crypto_aead_reqtfm(req);
35 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
36
37out:
38 kzfree(subreq->iv);
39}
40
41static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
42 int err)
43{
44 struct aead_request *req = base->data;
45
46 seqiv_aead_encrypt_complete2(req, err);
47 aead_request_complete(req, err);
48}
49
Herbert Xu856e3f402015-05-21 15:11:13 +080050static int seqiv_aead_encrypt(struct aead_request *req)
51{
52 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +080053 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +080054 struct aead_request *subreq = aead_request_ctx(req);
55 crypto_completion_t compl;
56 void *data;
57 u8 *info;
Herbert Xudd044462015-05-23 15:41:56 +080058 unsigned int ivsize = 8;
Herbert Xu856e3f402015-05-21 15:11:13 +080059 int err;
60
Herbert Xudd044462015-05-23 15:41:56 +080061 if (req->cryptlen < ivsize)
62 return -EINVAL;
63
Herbert Xu659e7f52015-08-13 17:28:59 +080064 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +080065
66 compl = req->base.complete;
67 data = req->base.data;
68 info = req->iv;
69
Herbert Xu856e3f402015-05-21 15:11:13 +080070 if (req->src != req->dst) {
Kees Cook8d605392018-09-18 19:10:51 -070071 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
Herbert Xu856e3f402015-05-21 15:11:13 +080072
Kees Cook8d605392018-09-18 19:10:51 -070073 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
Herbert Xuef228712016-07-12 13:17:45 +080074 skcipher_request_set_callback(nreq, req->base.flags,
75 NULL, NULL);
76 skcipher_request_set_crypt(nreq, req->src, req->dst,
77 req->assoclen + req->cryptlen,
78 NULL);
79
80 err = crypto_skcipher_encrypt(nreq);
Herbert Xu856e3f402015-05-21 15:11:13 +080081 if (err)
82 return err;
83 }
84
85 if (unlikely(!IS_ALIGNED((unsigned long)info,
86 crypto_aead_alignmask(geniv) + 1))) {
YueHaibing7e33d4d2019-01-21 07:57:00 +000087 info = kmemdup(req->iv, ivsize, req->base.flags &
88 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
89 GFP_ATOMIC);
Herbert Xu856e3f402015-05-21 15:11:13 +080090 if (!info)
91 return -ENOMEM;
92
Herbert Xu856e3f402015-05-21 15:11:13 +080093 compl = seqiv_aead_encrypt_complete;
94 data = req;
95 }
96
97 aead_request_set_callback(subreq, req->base.flags, compl, data);
98 aead_request_set_crypt(subreq, req->dst, req->dst,
99 req->cryptlen - ivsize, info);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800100 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800101
102 crypto_xor(info, ctx->salt, ivsize);
103 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
104
105 err = crypto_aead_encrypt(subreq);
106 if (unlikely(info != req->iv))
107 seqiv_aead_encrypt_complete2(req, err);
108 return err;
109}
110
Herbert Xu856e3f402015-05-21 15:11:13 +0800111static int seqiv_aead_decrypt(struct aead_request *req)
112{
113 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +0800114 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +0800115 struct aead_request *subreq = aead_request_ctx(req);
116 crypto_completion_t compl;
117 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800118 unsigned int ivsize = 8;
119
120 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
121 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800122
Herbert Xu659e7f52015-08-13 17:28:59 +0800123 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800124
125 compl = req->base.complete;
126 data = req->base.data;
127
Herbert Xu856e3f402015-05-21 15:11:13 +0800128 aead_request_set_callback(subreq, req->base.flags, compl, data);
129 aead_request_set_crypt(subreq, req->src, req->dst,
130 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800131 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800132
133 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
Herbert Xu856e3f402015-05-21 15:11:13 +0800134
135 return crypto_aead_decrypt(subreq);
136}
137
Herbert Xu06771572015-05-23 15:41:51 +0800138static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800139{
Herbert Xu856e3f402015-05-21 15:11:13 +0800140 struct aead_instance *inst;
Herbert Xu06771572015-05-23 15:41:51 +0800141 int err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800142
Herbert Xu06771572015-05-23 15:41:51 +0800143 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800144
145 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800146 return PTR_ERR(inst);
147
Herbert Xu06771572015-05-23 15:41:51 +0800148 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800149 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800150 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100151
Herbert Xub7dcfab2015-06-03 14:49:27 +0800152 inst->alg.encrypt = seqiv_aead_encrypt;
Herbert Xu856e3f402015-05-21 15:11:13 +0800153 inst->alg.decrypt = seqiv_aead_decrypt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800154
Herbert Xu659e7f52015-08-13 17:28:59 +0800155 inst->alg.init = aead_init_geniv;
156 inst->alg.exit = aead_exit_geniv;
Herbert Xu856e3f402015-05-21 15:11:13 +0800157
Herbert Xu659e7f52015-08-13 17:28:59 +0800158 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
Herbert Xu5964f262015-07-09 07:17:22 +0800159 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xu856e3f402015-05-21 15:11:13 +0800160
Herbert Xu06771572015-05-23 15:41:51 +0800161 err = aead_register_instance(tmpl, inst);
162 if (err)
163 goto free_inst;
164
Herbert Xu14df4d82007-12-12 12:27:26 +0800165out:
Herbert Xu06771572015-05-23 15:41:51 +0800166 return err;
167
168free_inst:
169 aead_geniv_free(inst);
170 goto out;
Herbert Xu14df4d82007-12-12 12:27:26 +0800171}
172
Herbert Xu06771572015-05-23 15:41:51 +0800173static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800174{
175 struct crypto_attr_type *algt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800176
177 algt = crypto_get_attr_type(tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800178 if (IS_ERR(algt))
Herbert Xu06771572015-05-23 15:41:51 +0800179 return PTR_ERR(algt);
Herbert Xu14df4d82007-12-12 12:27:26 +0800180
181 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800182 return -EINVAL;
Herbert Xu14df4d82007-12-12 12:27:26 +0800183
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800184 return seqiv_aead_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800185}
186
187static void seqiv_free(struct crypto_instance *inst)
188{
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800189 aead_geniv_free(aead_instance(inst));
Herbert Xu14df4d82007-12-12 12:27:26 +0800190}
191
Herbert Xu0a270322007-11-30 21:38:37 +1100192static struct crypto_template seqiv_tmpl = {
193 .name = "seqiv",
Herbert Xu06771572015-05-23 15:41:51 +0800194 .create = seqiv_create,
Herbert Xu14df4d82007-12-12 12:27:26 +0800195 .free = seqiv_free,
Herbert Xu0a270322007-11-30 21:38:37 +1100196 .module = THIS_MODULE,
197};
198
199static int __init seqiv_module_init(void)
200{
Herbert Xu8a2cd1c2015-08-13 17:28:53 +0800201 return crypto_register_template(&seqiv_tmpl);
Herbert Xu0a270322007-11-30 21:38:37 +1100202}
203
204static void __exit seqiv_module_exit(void)
205{
206 crypto_unregister_template(&seqiv_tmpl);
207}
208
Eric Biggersc4741b22019-04-11 21:57:42 -0700209subsys_initcall(seqiv_module_init);
Herbert Xu0a270322007-11-30 21:38:37 +1100210module_exit(seqiv_module_exit);
211
212MODULE_LICENSE("GPL");
213MODULE_DESCRIPTION("Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800214MODULE_ALIAS_CRYPTO("seqiv");