blob: f124b9b54e159267968a18ddd68fffe8e0642bf9 [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 Xu856e3f402015-05-21 15:11:13 +080021static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
22{
23 struct aead_request *subreq = aead_request_ctx(req);
24 struct crypto_aead *geniv;
25
26 if (err == -EINPROGRESS)
27 return;
28
29 if (err)
30 goto out;
31
32 geniv = crypto_aead_reqtfm(req);
33 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
34
35out:
36 kzfree(subreq->iv);
37}
38
39static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
40 int err)
41{
42 struct aead_request *req = base->data;
43
44 seqiv_aead_encrypt_complete2(req, err);
45 aead_request_complete(req, err);
46}
47
Herbert Xu856e3f402015-05-21 15:11:13 +080048static int seqiv_aead_encrypt(struct aead_request *req)
49{
50 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +080051 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +080052 struct aead_request *subreq = aead_request_ctx(req);
53 crypto_completion_t compl;
54 void *data;
55 u8 *info;
Herbert Xudd044462015-05-23 15:41:56 +080056 unsigned int ivsize = 8;
Herbert Xu856e3f402015-05-21 15:11:13 +080057 int err;
58
Herbert Xudd044462015-05-23 15:41:56 +080059 if (req->cryptlen < ivsize)
60 return -EINVAL;
61
Herbert Xu659e7f52015-08-13 17:28:59 +080062 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +080063
64 compl = req->base.complete;
65 data = req->base.data;
66 info = req->iv;
67
Herbert Xu856e3f402015-05-21 15:11:13 +080068 if (req->src != req->dst) {
Kees Cook8d605392018-09-18 19:10:51 -070069 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
Herbert Xu856e3f402015-05-21 15:11:13 +080070
Kees Cook8d605392018-09-18 19:10:51 -070071 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
Herbert Xuef228712016-07-12 13:17:45 +080072 skcipher_request_set_callback(nreq, req->base.flags,
73 NULL, NULL);
74 skcipher_request_set_crypt(nreq, req->src, req->dst,
75 req->assoclen + req->cryptlen,
76 NULL);
77
78 err = crypto_skcipher_encrypt(nreq);
Herbert Xu856e3f402015-05-21 15:11:13 +080079 if (err)
80 return err;
81 }
82
83 if (unlikely(!IS_ALIGNED((unsigned long)info,
84 crypto_aead_alignmask(geniv) + 1))) {
YueHaibing7e33d4d2019-01-21 07:57:00 +000085 info = kmemdup(req->iv, ivsize, req->base.flags &
86 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
87 GFP_ATOMIC);
Herbert Xu856e3f402015-05-21 15:11:13 +080088 if (!info)
89 return -ENOMEM;
90
Herbert Xu856e3f402015-05-21 15:11:13 +080091 compl = seqiv_aead_encrypt_complete;
92 data = req;
93 }
94
95 aead_request_set_callback(subreq, req->base.flags, compl, data);
96 aead_request_set_crypt(subreq, req->dst, req->dst,
97 req->cryptlen - ivsize, info);
Herbert Xu374d4ad2015-05-23 15:41:57 +080098 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +080099
100 crypto_xor(info, ctx->salt, ivsize);
101 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
102
103 err = crypto_aead_encrypt(subreq);
104 if (unlikely(info != req->iv))
105 seqiv_aead_encrypt_complete2(req, err);
106 return err;
107}
108
Herbert Xu856e3f402015-05-21 15:11:13 +0800109static int seqiv_aead_decrypt(struct aead_request *req)
110{
111 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
Herbert Xu659e7f52015-08-13 17:28:59 +0800112 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
Herbert Xu856e3f402015-05-21 15:11:13 +0800113 struct aead_request *subreq = aead_request_ctx(req);
114 crypto_completion_t compl;
115 void *data;
Herbert Xudd044462015-05-23 15:41:56 +0800116 unsigned int ivsize = 8;
117
118 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
119 return -EINVAL;
Herbert Xu856e3f402015-05-21 15:11:13 +0800120
Herbert Xu659e7f52015-08-13 17:28:59 +0800121 aead_request_set_tfm(subreq, ctx->child);
Herbert Xu856e3f402015-05-21 15:11:13 +0800122
123 compl = req->base.complete;
124 data = req->base.data;
125
Herbert Xu856e3f402015-05-21 15:11:13 +0800126 aead_request_set_callback(subreq, req->base.flags, compl, data);
127 aead_request_set_crypt(subreq, req->src, req->dst,
128 req->cryptlen - ivsize, req->iv);
Herbert Xu374d4ad2015-05-23 15:41:57 +0800129 aead_request_set_ad(subreq, req->assoclen + ivsize);
Herbert Xu856e3f402015-05-21 15:11:13 +0800130
131 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
Herbert Xu856e3f402015-05-21 15:11:13 +0800132
133 return crypto_aead_decrypt(subreq);
134}
135
Herbert Xu06771572015-05-23 15:41:51 +0800136static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800137{
Herbert Xu856e3f402015-05-21 15:11:13 +0800138 struct aead_instance *inst;
Herbert Xu06771572015-05-23 15:41:51 +0800139 int err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800140
Herbert Xu06771572015-05-23 15:41:51 +0800141 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
Herbert Xu14df4d82007-12-12 12:27:26 +0800142
143 if (IS_ERR(inst))
Herbert Xu06771572015-05-23 15:41:51 +0800144 return PTR_ERR(inst);
145
Herbert Xu06771572015-05-23 15:41:51 +0800146 err = -EINVAL;
Herbert Xudd044462015-05-23 15:41:56 +0800147 if (inst->alg.ivsize != sizeof(u64))
Herbert Xu06771572015-05-23 15:41:51 +0800148 goto free_inst;
Herbert Xuc0ecf892015-01-16 19:51:20 +1100149
Herbert Xub7dcfab2015-06-03 14:49:27 +0800150 inst->alg.encrypt = seqiv_aead_encrypt;
Herbert Xu856e3f402015-05-21 15:11:13 +0800151 inst->alg.decrypt = seqiv_aead_decrypt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800152
Herbert Xu659e7f52015-08-13 17:28:59 +0800153 inst->alg.init = aead_init_geniv;
154 inst->alg.exit = aead_exit_geniv;
Herbert Xu856e3f402015-05-21 15:11:13 +0800155
Herbert Xu659e7f52015-08-13 17:28:59 +0800156 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
Herbert Xu5964f262015-07-09 07:17:22 +0800157 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
Herbert Xu856e3f402015-05-21 15:11:13 +0800158
Herbert Xu06771572015-05-23 15:41:51 +0800159 err = aead_register_instance(tmpl, inst);
Eric Biggers0f8f6d82020-01-02 20:04:36 -0800160 if (err) {
Herbert Xu06771572015-05-23 15:41:51 +0800161free_inst:
Eric Biggers0f8f6d82020-01-02 20:04:36 -0800162 inst->free(inst);
163 }
164 return err;
Herbert Xu14df4d82007-12-12 12:27:26 +0800165}
166
Herbert Xu06771572015-05-23 15:41:51 +0800167static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
Herbert Xu14df4d82007-12-12 12:27:26 +0800168{
169 struct crypto_attr_type *algt;
Herbert Xu14df4d82007-12-12 12:27:26 +0800170
171 algt = crypto_get_attr_type(tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800172 if (IS_ERR(algt))
Herbert Xu06771572015-05-23 15:41:51 +0800173 return PTR_ERR(algt);
Herbert Xu14df4d82007-12-12 12:27:26 +0800174
175 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800176 return -EINVAL;
Herbert Xu14df4d82007-12-12 12:27:26 +0800177
Herbert Xu3a01d0e2016-07-12 13:17:50 +0800178 return seqiv_aead_create(tmpl, tb);
Herbert Xu14df4d82007-12-12 12:27:26 +0800179}
180
Herbert Xu0a270322007-11-30 21:38:37 +1100181static struct crypto_template seqiv_tmpl = {
182 .name = "seqiv",
Herbert Xu06771572015-05-23 15:41:51 +0800183 .create = seqiv_create,
Herbert Xu0a270322007-11-30 21:38:37 +1100184 .module = THIS_MODULE,
185};
186
187static int __init seqiv_module_init(void)
188{
Herbert Xu8a2cd1c2015-08-13 17:28:53 +0800189 return crypto_register_template(&seqiv_tmpl);
Herbert Xu0a270322007-11-30 21:38:37 +1100190}
191
192static void __exit seqiv_module_exit(void)
193{
194 crypto_unregister_template(&seqiv_tmpl);
195}
196
Eric Biggersc4741b22019-04-11 21:57:42 -0700197subsys_initcall(seqiv_module_init);
Herbert Xu0a270322007-11-30 21:38:37 +1100198module_exit(seqiv_module_exit);
199
200MODULE_LICENSE("GPL");
201MODULE_DESCRIPTION("Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800202MODULE_ALIAS_CRYPTO("seqiv");