Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
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>
9 */
10
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>
20
21static void seqiv_free(struct crypto_instance *inst);
22
23static 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
50static int seqiv_aead_encrypt(struct aead_request *req)
51{
52 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
53 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
54 struct aead_request *subreq = aead_request_ctx(req);
55 crypto_completion_t compl;
56 void *data;
57 u8 *info;
58 unsigned int ivsize = 8;
59 int err;
60
61 if (req->cryptlen < ivsize)
62 return -EINVAL;
63
64 aead_request_set_tfm(subreq, ctx->child);
65
66 compl = req->base.complete;
67 data = req->base.data;
68 info = req->iv;
69
70 if (req->src != req->dst) {
71 SYNC_SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
72
73 skcipher_request_set_sync_tfm(nreq, ctx->sknull);
74 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);
81 if (err)
82 return err;
83 }
84
85 if (unlikely(!IS_ALIGNED((unsigned long)info,
86 crypto_aead_alignmask(geniv) + 1))) {
87 info = kmemdup(req->iv, ivsize, req->base.flags &
88 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
89 GFP_ATOMIC);
90 if (!info)
91 return -ENOMEM;
92
93 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);
100 aead_request_set_ad(subreq, req->assoclen + ivsize);
101
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
111static int seqiv_aead_decrypt(struct aead_request *req)
112{
113 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
114 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
115 struct aead_request *subreq = aead_request_ctx(req);
116 crypto_completion_t compl;
117 void *data;
118 unsigned int ivsize = 8;
119
120 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
121 return -EINVAL;
122
123 aead_request_set_tfm(subreq, ctx->child);
124
125 compl = req->base.complete;
126 data = req->base.data;
127
128 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);
131 aead_request_set_ad(subreq, req->assoclen + ivsize);
132
133 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
134
135 return crypto_aead_decrypt(subreq);
136}
137
138static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
139{
140 struct aead_instance *inst;
141 int err;
142
143 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
144
145 if (IS_ERR(inst))
146 return PTR_ERR(inst);
147
148 err = -EINVAL;
149 if (inst->alg.ivsize != sizeof(u64))
150 goto free_inst;
151
152 inst->alg.encrypt = seqiv_aead_encrypt;
153 inst->alg.decrypt = seqiv_aead_decrypt;
154
155 inst->alg.init = aead_init_geniv;
156 inst->alg.exit = aead_exit_geniv;
157
158 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
159 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
160
161 err = aead_register_instance(tmpl, inst);
162 if (err)
163 goto free_inst;
164
165out:
166 return err;
167
168free_inst:
169 aead_geniv_free(inst);
170 goto out;
171}
172
173static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
174{
175 struct crypto_attr_type *algt;
176
177 algt = crypto_get_attr_type(tb);
178 if (IS_ERR(algt))
179 return PTR_ERR(algt);
180
181 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
182 return -EINVAL;
183
184 return seqiv_aead_create(tmpl, tb);
185}
186
187static void seqiv_free(struct crypto_instance *inst)
188{
189 aead_geniv_free(aead_instance(inst));
190}
191
192static struct crypto_template seqiv_tmpl = {
193 .name = "seqiv",
194 .create = seqiv_create,
195 .free = seqiv_free,
196 .module = THIS_MODULE,
197};
198
199static int __init seqiv_module_init(void)
200{
201 return crypto_register_template(&seqiv_tmpl);
202}
203
204static void __exit seqiv_module_exit(void)
205{
206 crypto_unregister_template(&seqiv_tmpl);
207}
208
209subsys_initcall(seqiv_module_init);
210module_exit(seqiv_module_exit);
211
212MODULE_LICENSE("GPL");
213MODULE_DESCRIPTION("Sequence Number IV Generator");
214MODULE_ALIAS_CRYPTO("seqiv");
1/*
2 * seqiv: Sequence Number IV Generator
3 *
4 * This generator generates an IV based on a sequence number by xoring it
5 * with a salt. This algorithm is mainly useful for CTR and similar modes.
6 *
7 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 */
15
16#include <crypto/internal/geniv.h>
17#include <crypto/scatterwalk.h>
18#include <crypto/skcipher.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/string.h>
25
26static void seqiv_free(struct crypto_instance *inst);
27
28static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
29{
30 struct aead_request *subreq = aead_request_ctx(req);
31 struct crypto_aead *geniv;
32
33 if (err == -EINPROGRESS)
34 return;
35
36 if (err)
37 goto out;
38
39 geniv = crypto_aead_reqtfm(req);
40 memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));
41
42out:
43 kzfree(subreq->iv);
44}
45
46static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
47 int err)
48{
49 struct aead_request *req = base->data;
50
51 seqiv_aead_encrypt_complete2(req, err);
52 aead_request_complete(req, err);
53}
54
55static int seqiv_aead_encrypt(struct aead_request *req)
56{
57 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
58 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
59 struct aead_request *subreq = aead_request_ctx(req);
60 crypto_completion_t compl;
61 void *data;
62 u8 *info;
63 unsigned int ivsize = 8;
64 int err;
65
66 if (req->cryptlen < ivsize)
67 return -EINVAL;
68
69 aead_request_set_tfm(subreq, ctx->child);
70
71 compl = req->base.complete;
72 data = req->base.data;
73 info = req->iv;
74
75 if (req->src != req->dst) {
76 SKCIPHER_REQUEST_ON_STACK(nreq, ctx->sknull);
77
78 skcipher_request_set_tfm(nreq, ctx->sknull);
79 skcipher_request_set_callback(nreq, req->base.flags,
80 NULL, NULL);
81 skcipher_request_set_crypt(nreq, req->src, req->dst,
82 req->assoclen + req->cryptlen,
83 NULL);
84
85 err = crypto_skcipher_encrypt(nreq);
86 if (err)
87 return err;
88 }
89
90 if (unlikely(!IS_ALIGNED((unsigned long)info,
91 crypto_aead_alignmask(geniv) + 1))) {
92 info = kmalloc(ivsize, req->base.flags &
93 CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL:
94 GFP_ATOMIC);
95 if (!info)
96 return -ENOMEM;
97
98 memcpy(info, req->iv, ivsize);
99 compl = seqiv_aead_encrypt_complete;
100 data = req;
101 }
102
103 aead_request_set_callback(subreq, req->base.flags, compl, data);
104 aead_request_set_crypt(subreq, req->dst, req->dst,
105 req->cryptlen - ivsize, info);
106 aead_request_set_ad(subreq, req->assoclen + ivsize);
107
108 crypto_xor(info, ctx->salt, ivsize);
109 scatterwalk_map_and_copy(info, req->dst, req->assoclen, ivsize, 1);
110
111 err = crypto_aead_encrypt(subreq);
112 if (unlikely(info != req->iv))
113 seqiv_aead_encrypt_complete2(req, err);
114 return err;
115}
116
117static int seqiv_aead_decrypt(struct aead_request *req)
118{
119 struct crypto_aead *geniv = crypto_aead_reqtfm(req);
120 struct aead_geniv_ctx *ctx = crypto_aead_ctx(geniv);
121 struct aead_request *subreq = aead_request_ctx(req);
122 crypto_completion_t compl;
123 void *data;
124 unsigned int ivsize = 8;
125
126 if (req->cryptlen < ivsize + crypto_aead_authsize(geniv))
127 return -EINVAL;
128
129 aead_request_set_tfm(subreq, ctx->child);
130
131 compl = req->base.complete;
132 data = req->base.data;
133
134 aead_request_set_callback(subreq, req->base.flags, compl, data);
135 aead_request_set_crypt(subreq, req->src, req->dst,
136 req->cryptlen - ivsize, req->iv);
137 aead_request_set_ad(subreq, req->assoclen + ivsize);
138
139 scatterwalk_map_and_copy(req->iv, req->src, req->assoclen, ivsize, 0);
140
141 return crypto_aead_decrypt(subreq);
142}
143
144static int seqiv_aead_create(struct crypto_template *tmpl, struct rtattr **tb)
145{
146 struct aead_instance *inst;
147 int err;
148
149 inst = aead_geniv_alloc(tmpl, tb, 0, 0);
150
151 if (IS_ERR(inst))
152 return PTR_ERR(inst);
153
154 err = -EINVAL;
155 if (inst->alg.ivsize != sizeof(u64))
156 goto free_inst;
157
158 inst->alg.encrypt = seqiv_aead_encrypt;
159 inst->alg.decrypt = seqiv_aead_decrypt;
160
161 inst->alg.init = aead_init_geniv;
162 inst->alg.exit = aead_exit_geniv;
163
164 inst->alg.base.cra_ctxsize = sizeof(struct aead_geniv_ctx);
165 inst->alg.base.cra_ctxsize += inst->alg.ivsize;
166
167 err = aead_register_instance(tmpl, inst);
168 if (err)
169 goto free_inst;
170
171out:
172 return err;
173
174free_inst:
175 aead_geniv_free(inst);
176 goto out;
177}
178
179static int seqiv_create(struct crypto_template *tmpl, struct rtattr **tb)
180{
181 struct crypto_attr_type *algt;
182
183 algt = crypto_get_attr_type(tb);
184 if (IS_ERR(algt))
185 return PTR_ERR(algt);
186
187 if ((algt->type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK)
188 return -EINVAL;
189
190 return seqiv_aead_create(tmpl, tb);
191}
192
193static void seqiv_free(struct crypto_instance *inst)
194{
195 aead_geniv_free(aead_instance(inst));
196}
197
198static struct crypto_template seqiv_tmpl = {
199 .name = "seqiv",
200 .create = seqiv_create,
201 .free = seqiv_free,
202 .module = THIS_MODULE,
203};
204
205static int __init seqiv_module_init(void)
206{
207 return crypto_register_template(&seqiv_tmpl);
208}
209
210static void __exit seqiv_module_exit(void)
211{
212 crypto_unregister_template(&seqiv_tmpl);
213}
214
215module_init(seqiv_module_init);
216module_exit(seqiv_module_exit);
217
218MODULE_LICENSE("GPL");
219MODULE_DESCRIPTION("Sequence Number IV Generator");
220MODULE_ALIAS_CRYPTO("seqiv");