1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * seqiv: Sequence Number IV Generator
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.
8 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
11 #include <crypto/internal/geniv.h>
12 #include <crypto/scatterwalk.h>
13 #include <crypto/skcipher.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
21 static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
23 struct aead_request *subreq = aead_request_ctx(req);
24 struct crypto_aead *geniv;
26 if (err == -EINPROGRESS)
32 geniv = crypto_aead_reqtfm(req);
33 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
36 kfree_sensitive(subreq->iv);
39 static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
42 struct aead_request *req = base->data;
44 seqiv_aead_encrypt_complete2(req, err);
45 aead_request_complete(req, err);
48 static int seqiv_aead_encrypt(struct aead_request *req)
50 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
51 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
52 struct aead_request *subreq = aead_request_ctx(req);
53 crypto_completion_t compl;
56 unsigned int ivsize = 8;
59 if (req->cryptlen < ivsize)
62 aead_request_set_tfm(subreq, ctx->child);
64 compl = req->base.complete;
65 data = req->base.data;
68 if (req->src != req->dst) {
69 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
71 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
72 skcipher_request_set_callback(nreq, req->base.flags,
74 skcipher_request_set_crypt(nreq, req->src, req->dst,
75 req->assoclen + req->cryptlen,
78 err = crypto_skcipher_encrypt(nreq);
83 if (unlikely(!IS_ALIGNED((unsigned long)info,
84 crypto_aead_alignmask(geniv) + 1))) {
85 info = kmemdup(req->iv, ivsize, req->base.flags &
86 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
91 compl = seqiv_aead_encrypt_complete;
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);
98 aead_request_set_ad(subreq, req->assoclen + ivsize);
100 crypto_xor(info, ctx->salt, ivsize);
101 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
103 err = crypto_aead_encrypt(subreq);
104 if (unlikely(info != req->iv))
105 seqiv_aead_encrypt_complete2(req, err);
109 static int seqiv_aead_decrypt(struct aead_request *req)
111 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
112 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
113 struct aead_request *subreq = aead_request_ctx(req);
114 crypto_completion_t compl;
116 unsigned int ivsize = 8;
118 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
121 aead_request_set_tfm(subreq, ctx->child);
123 compl = req->base.complete;
124 data = req->base.data;
126 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);
129 aead_request_set_ad(subreq, req->assoclen + ivsize);
131 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
133 return crypto_aead_decrypt(subreq);
136 static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
138 struct aead_instance *inst;
141 inst = aead_geniv_alloc(tmpl, tb);
144 return PTR_ERR(inst);
147 if (inst->alg.ivsize != sizeof(u64))
150 inst->alg.encrypt = seqiv_aead_encrypt;
151 inst->alg.decrypt = seqiv_aead_decrypt;
153 inst->alg.init = aead_init_geniv;
154 inst->alg.exit = aead_exit_geniv;
156 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
157 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
159 err = aead_register_instance(tmpl, inst);
167 static struct crypto_template seqiv_tmpl = {
169 .create = seqiv_aead_create,
170 .module = THIS_MODULE,
173 static int __init seqiv_module_init(void)
175 return crypto_register_template(&seqiv_tmpl);
178 static void __exit seqiv_module_exit(void)
180 crypto_unregister_template(&seqiv_tmpl);
183 subsys_initcall(seqiv_module_init);
184 module_exit(seqiv_module_exit);
186 MODULE_LICENSE("GPL");
187 MODULE_DESCRIPTION("Sequence Number IV Generator");
188 MODULE_ALIAS_CRYPTO("seqiv");