drm/ast: Only warn about unsupported TX chips on Gen4 and later
[drm/drm-misc.git] / crypto / seqiv.c
blob17e11d51ddc36370265f4b2a6e3bf3933895c005
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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>
9 */
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 || err == -EBUSY)
27 return;
29 if (err)
30 goto out;
32 geniv = crypto_aead_reqtfm(req);
33 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
35 out:
36 kfree_sensitive(subreq->iv);
39 static void seqiv_aead_encrypt_complete(void *data, int err)
41 struct aead_request *req = data;
43 seqiv_aead_encrypt_complete2(req, err);
44 aead_request_complete(req, err);
47 static int seqiv_aead_encrypt(struct aead_request *req)
49 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
50 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
51 struct aead_request *subreq = aead_request_ctx(req);
52 crypto_completion_t compl;
53 void *data;
54 u8 *info;
55 unsigned int ivsize = 8;
56 int err;
58 if (req->cryptlen < ivsize)
59 return -EINVAL;
61 aead_request_set_tfm(subreq, ctx->child);
63 compl = req->base.complete;
64 data = req->base.data;
65 info = req->iv;
67 if (req->src != req->dst) {
68 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
70 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
71 skcipher_request_set_callback(nreq, req->base.flags,
72 NULL, NULL);
73 skcipher_request_set_crypt(nreq, req->src, req->dst,
74 req->assoclen + req->cryptlen,
75 NULL);
77 err = crypto_skcipher_encrypt(nreq);
78 if (err)
79 return err;
82 if (unlikely(!IS_ALIGNED((unsigned long)info,
83 crypto_aead_alignmask(geniv) + 1))) {
84 info = kmemdup(req->iv, ivsize, req->base.flags &
85 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
86 GFP_ATOMIC);
87 if (!info)
88 return -ENOMEM;
90 compl = seqiv_aead_encrypt_complete;
91 data = req;
94 aead_request_set_callback(subreq, req->base.flags, compl, data);
95 aead_request_set_crypt(subreq, req->dst, req->dst,
96 req->cryptlen - ivsize, info);
97 aead_request_set_ad(subreq, req->assoclen + ivsize);
99 crypto_xor(info, ctx->salt, ivsize);
100 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
102 err = crypto_aead_encrypt(subreq);
103 if (unlikely(info != req->iv))
104 seqiv_aead_encrypt_complete2(req, err);
105 return err;
108 static int seqiv_aead_decrypt(struct aead_request *req)
110 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
111 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
112 struct aead_request *subreq = aead_request_ctx(req);
113 crypto_completion_t compl;
114 void *data;
115 unsigned int ivsize = 8;
117 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
118 return -EINVAL;
120 aead_request_set_tfm(subreq, ctx->child);
122 compl = req->base.complete;
123 data = req->base.data;
125 aead_request_set_callback(subreq, req->base.flags, compl, data);
126 aead_request_set_crypt(subreq, req->src, req->dst,
127 req->cryptlen - ivsize, req->iv);
128 aead_request_set_ad(subreq, req->assoclen + ivsize);
130 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
132 return crypto_aead_decrypt(subreq);
135 static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
137 struct aead_instance *inst;
138 int err;
140 inst = aead_geniv_alloc(tmpl, tb);
142 if (IS_ERR(inst))
143 return PTR_ERR(inst);
145 err = -EINVAL;
146 if (inst->alg.ivsize != sizeof(u64))
147 goto free_inst;
149 inst->alg.encrypt = seqiv_aead_encrypt;
150 inst->alg.decrypt = seqiv_aead_decrypt;
152 inst->alg.init = aead_init_geniv;
153 inst->alg.exit = aead_exit_geniv;
155 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
156 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
158 err = aead_register_instance(tmpl, inst);
159 if (err) {
160 free_inst:
161 inst->free(inst);
163 return err;
166 static struct crypto_template seqiv_tmpl = {
167 .name = "seqiv",
168 .create = seqiv_aead_create,
169 .module = THIS_MODULE,
172 static int __init seqiv_module_init(void)
174 return crypto_register_template(&seqiv_tmpl);
177 static void __exit seqiv_module_exit(void)
179 crypto_unregister_template(&seqiv_tmpl);
182 subsys_initcall(seqiv_module_init);
183 module_exit(seqiv_module_exit);
185 MODULE_LICENSE("GPL");
186 MODULE_DESCRIPTION("Sequence Number IV Generator");
187 MODULE_ALIAS_CRYPTO("seqiv");