blob: 0899d527c2845b648bc1872daf5409ca1c4a8792 [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:
Waiman Long453431a2020-08-06 23:18:13 -070036 kfree_sensitive(subreq->iv);
Herbert Xu856e3f402015-05-21 15:11:13 +080037}
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
Eric Biggerse72b48c2020-07-09 23:20:36 -0700141 inst = aead_geniv_alloc(tmpl, tb);
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 Xu0a270322007-11-30 21:38:37 +1100167static struct crypto_template seqiv_tmpl = {
168 .name = "seqiv",
Eric Biggers46881112020-07-09 23:20:37 -0700169 .create = seqiv_aead_create,
Herbert Xu0a270322007-11-30 21:38:37 +1100170 .module = THIS_MODULE,
171};
172
173static int __init seqiv_module_init(void)
174{
Herbert Xu8a2cd1c2015-08-13 17:28:53 +0800175 return crypto_register_template(&seqiv_tmpl);
Herbert Xu0a270322007-11-30 21:38:37 +1100176}
177
178static void __exit seqiv_module_exit(void)
179{
180 crypto_unregister_template(&seqiv_tmpl);
181}
182
Eric Biggersc4741b22019-04-11 21:57:42 -0700183subsys_initcall(seqiv_module_init);
Herbert Xu0a270322007-11-30 21:38:37 +1100184module_exit(seqiv_module_exit);
185
186MODULE_LICENSE("GPL");
187MODULE_DESCRIPTION("Sequence Number IV Generator");
Kees Cook4943ba12014-11-24 16:32:38 -0800188MODULE_ALIAS_CRYPTO("seqiv");