Skip to content

Commit a874f59

Browse files
ebiggersherbertx
authored andcommitted
crypto: xts - prefix function and struct names with "xts"
Overly-generic names can cause problems like naming collisions, confusing crash reports, and reduced grep-ability. E.g. see commit d099ea6 ("crypto - Avoid free() namespace collision"). Clean this up for the xts template by prefixing the names with "xts_". (I didn't use "crypto_xts_" instead because that seems overkill.) Also constify the tfm context in a couple places, and make xts_free_instance() use the instance context structure so that it doesn't just assume the crypto_skcipher_spawn is at the beginning. Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 10f33d3 commit a874f59

1 file changed

Lines changed: 72 additions & 65 deletions

File tree

‎crypto/xts.c‎

Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <crypto/b128ops.h>
2121
#include <crypto/gf128mul.h>
2222

23-
struct priv {
23+
struct xts_tfm_ctx {
2424
struct crypto_skcipher *child;
2525
struct crypto_cipher *tweak;
2626
};
@@ -30,17 +30,17 @@ struct xts_instance_ctx {
3030
char name[CRYPTO_MAX_ALG_NAME];
3131
};
3232

33-
struct rctx {
33+
struct xts_request_ctx {
3434
le128 t;
3535
struct scatterlist *tail;
3636
struct scatterlist sg[2];
3737
struct skcipher_request subreq;
3838
};
3939

40-
static int setkey(struct crypto_skcipher *parent, const u8 *key,
41-
unsigned int keylen)
40+
static int xts_setkey(struct crypto_skcipher *parent, const u8 *key,
41+
unsigned int keylen)
4242
{
43-
struct priv *ctx = crypto_skcipher_ctx(parent);
43+
struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(parent);
4444
struct crypto_skcipher *child;
4545
struct crypto_cipher *tweak;
4646
int err;
@@ -78,9 +78,10 @@ static int setkey(struct crypto_skcipher *parent, const u8 *key,
7878
* mutliple calls to the 'ecb(..)' instance, which usually would be slower than
7979
* just doing the gf128mul_x_ble() calls again.
8080
*/
81-
static int xor_tweak(struct skcipher_request *req, bool second_pass, bool enc)
81+
static int xts_xor_tweak(struct skcipher_request *req, bool second_pass,
82+
bool enc)
8283
{
83-
struct rctx *rctx = skcipher_request_ctx(req);
84+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
8485
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
8586
const bool cts = (req->cryptlen % XTS_BLOCK_SIZE);
8687
const int bs = XTS_BLOCK_SIZE;
@@ -128,23 +129,23 @@ static int xor_tweak(struct skcipher_request *req, bool second_pass, bool enc)
128129
return err;
129130
}
130131

131-
static int xor_tweak_pre(struct skcipher_request *req, bool enc)
132+
static int xts_xor_tweak_pre(struct skcipher_request *req, bool enc)
132133
{
133-
return xor_tweak(req, false, enc);
134+
return xts_xor_tweak(req, false, enc);
134135
}
135136

136-
static int xor_tweak_post(struct skcipher_request *req, bool enc)
137+
static int xts_xor_tweak_post(struct skcipher_request *req, bool enc)
137138
{
138-
return xor_tweak(req, true, enc);
139+
return xts_xor_tweak(req, true, enc);
139140
}
140141

141-
static void cts_done(struct crypto_async_request *areq, int err)
142+
static void xts_cts_done(struct crypto_async_request *areq, int err)
142143
{
143144
struct skcipher_request *req = areq->data;
144145
le128 b;
145146

146147
if (!err) {
147-
struct rctx *rctx = skcipher_request_ctx(req);
148+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
148149

149150
scatterwalk_map_and_copy(&b, rctx->tail, 0, XTS_BLOCK_SIZE, 0);
150151
le128_xor(&b, &rctx->t, &b);
@@ -154,12 +155,13 @@ static void cts_done(struct crypto_async_request *areq, int err)
154155
skcipher_request_complete(req, err);
155156
}
156157

157-
static int cts_final(struct skcipher_request *req,
158-
int (*crypt)(struct skcipher_request *req))
158+
static int xts_cts_final(struct skcipher_request *req,
159+
int (*crypt)(struct skcipher_request *req))
159160
{
160-
struct priv *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
161+
const struct xts_tfm_ctx *ctx =
162+
crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
161163
int offset = req->cryptlen & ~(XTS_BLOCK_SIZE - 1);
162-
struct rctx *rctx = skcipher_request_ctx(req);
164+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
163165
struct skcipher_request *subreq = &rctx->subreq;
164166
int tail = req->cryptlen % XTS_BLOCK_SIZE;
165167
le128 b[2];
@@ -177,7 +179,8 @@ static int cts_final(struct skcipher_request *req,
177179
scatterwalk_map_and_copy(b, rctx->tail, 0, XTS_BLOCK_SIZE + tail, 1);
178180

179181
skcipher_request_set_tfm(subreq, ctx->child);
180-
skcipher_request_set_callback(subreq, req->base.flags, cts_done, req);
182+
skcipher_request_set_callback(subreq, req->base.flags, xts_cts_done,
183+
req);
181184
skcipher_request_set_crypt(subreq, rctx->tail, rctx->tail,
182185
XTS_BLOCK_SIZE, NULL);
183186

@@ -192,18 +195,18 @@ static int cts_final(struct skcipher_request *req,
192195
return 0;
193196
}
194197

195-
static void encrypt_done(struct crypto_async_request *areq, int err)
198+
static void xts_encrypt_done(struct crypto_async_request *areq, int err)
196199
{
197200
struct skcipher_request *req = areq->data;
198201

199202
if (!err) {
200-
struct rctx *rctx = skcipher_request_ctx(req);
203+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
201204

202205
rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
203-
err = xor_tweak_post(req, true);
206+
err = xts_xor_tweak_post(req, true);
204207

205208
if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
206-
err = cts_final(req, crypto_skcipher_encrypt);
209+
err = xts_cts_final(req, crypto_skcipher_encrypt);
207210
if (err == -EINPROGRESS)
208211
return;
209212
}
@@ -212,18 +215,18 @@ static void encrypt_done(struct crypto_async_request *areq, int err)
212215
skcipher_request_complete(req, err);
213216
}
214217

215-
static void decrypt_done(struct crypto_async_request *areq, int err)
218+
static void xts_decrypt_done(struct crypto_async_request *areq, int err)
216219
{
217220
struct skcipher_request *req = areq->data;
218221

219222
if (!err) {
220-
struct rctx *rctx = skcipher_request_ctx(req);
223+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
221224

222225
rctx->subreq.base.flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
223-
err = xor_tweak_post(req, false);
226+
err = xts_xor_tweak_post(req, false);
224227

225228
if (!err && unlikely(req->cryptlen % XTS_BLOCK_SIZE)) {
226-
err = cts_final(req, crypto_skcipher_decrypt);
229+
err = xts_cts_final(req, crypto_skcipher_decrypt);
227230
if (err == -EINPROGRESS)
228231
return;
229232
}
@@ -232,10 +235,12 @@ static void decrypt_done(struct crypto_async_request *areq, int err)
232235
skcipher_request_complete(req, err);
233236
}
234237

235-
static int init_crypt(struct skcipher_request *req, crypto_completion_t compl)
238+
static int xts_init_crypt(struct skcipher_request *req,
239+
crypto_completion_t compl)
236240
{
237-
struct priv *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
238-
struct rctx *rctx = skcipher_request_ctx(req);
241+
const struct xts_tfm_ctx *ctx =
242+
crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
243+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
239244
struct skcipher_request *subreq = &rctx->subreq;
240245

241246
if (req->cryptlen < XTS_BLOCK_SIZE)
@@ -252,45 +257,45 @@ static int init_crypt(struct skcipher_request *req, crypto_completion_t compl)
252257
return 0;
253258
}
254259

255-
static int encrypt(struct skcipher_request *req)
260+
static int xts_encrypt(struct skcipher_request *req)
256261
{
257-
struct rctx *rctx = skcipher_request_ctx(req);
262+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
258263
struct skcipher_request *subreq = &rctx->subreq;
259264
int err;
260265

261-
err = init_crypt(req, encrypt_done) ?:
262-
xor_tweak_pre(req, true) ?:
266+
err = xts_init_crypt(req, xts_encrypt_done) ?:
267+
xts_xor_tweak_pre(req, true) ?:
263268
crypto_skcipher_encrypt(subreq) ?:
264-
xor_tweak_post(req, true);
269+
xts_xor_tweak_post(req, true);
265270

266271
if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
267272
return err;
268273

269-
return cts_final(req, crypto_skcipher_encrypt);
274+
return xts_cts_final(req, crypto_skcipher_encrypt);
270275
}
271276

272-
static int decrypt(struct skcipher_request *req)
277+
static int xts_decrypt(struct skcipher_request *req)
273278
{
274-
struct rctx *rctx = skcipher_request_ctx(req);
279+
struct xts_request_ctx *rctx = skcipher_request_ctx(req);
275280
struct skcipher_request *subreq = &rctx->subreq;
276281
int err;
277282

278-
err = init_crypt(req, decrypt_done) ?:
279-
xor_tweak_pre(req, false) ?:
283+
err = xts_init_crypt(req, xts_decrypt_done) ?:
284+
xts_xor_tweak_pre(req, false) ?:
280285
crypto_skcipher_decrypt(subreq) ?:
281-
xor_tweak_post(req, false);
286+
xts_xor_tweak_post(req, false);
282287

283288
if (err || likely((req->cryptlen % XTS_BLOCK_SIZE) == 0))
284289
return err;
285290

286-
return cts_final(req, crypto_skcipher_decrypt);
291+
return xts_cts_final(req, crypto_skcipher_decrypt);
287292
}
288293

289-
static int init_tfm(struct crypto_skcipher *tfm)
294+
static int xts_init_tfm(struct crypto_skcipher *tfm)
290295
{
291296
struct skcipher_instance *inst = skcipher_alg_instance(tfm);
292297
struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
293-
struct priv *ctx = crypto_skcipher_ctx(tfm);
298+
struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
294299
struct crypto_skcipher *child;
295300
struct crypto_cipher *tweak;
296301

@@ -309,26 +314,28 @@ static int init_tfm(struct crypto_skcipher *tfm)
309314
ctx->tweak = tweak;
310315

311316
crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) +
312-
sizeof(struct rctx));
317+
sizeof(struct xts_request_ctx));
313318

314319
return 0;
315320
}
316321

317-
static void exit_tfm(struct crypto_skcipher *tfm)
322+
static void xts_exit_tfm(struct crypto_skcipher *tfm)
318323
{
319-
struct priv *ctx = crypto_skcipher_ctx(tfm);
324+
struct xts_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
320325

321326
crypto_free_skcipher(ctx->child);
322327
crypto_free_cipher(ctx->tweak);
323328
}
324329

325-
static void crypto_xts_free(struct skcipher_instance *inst)
330+
static void xts_free_instance(struct skcipher_instance *inst)
326331
{
327-
crypto_drop_skcipher(skcipher_instance_ctx(inst));
332+
struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
333+
334+
crypto_drop_skcipher(&ictx->spawn);
328335
kfree(inst);
329336
}
330337

331-
static int create(struct crypto_template *tmpl, struct rtattr **tb)
338+
static int xts_create(struct crypto_template *tmpl, struct rtattr **tb)
332339
{
333340
struct skcipher_instance *inst;
334341
struct xts_instance_ctx *ctx;
@@ -416,43 +423,43 @@ static int create(struct crypto_template *tmpl, struct rtattr **tb)
416423
inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) * 2;
417424
inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) * 2;
418425

419-
inst->alg.base.cra_ctxsize = sizeof(struct priv);
426+
inst->alg.base.cra_ctxsize = sizeof(struct xts_tfm_ctx);
420427

421-
inst->alg.init = init_tfm;
422-
inst->alg.exit = exit_tfm;
428+
inst->alg.init = xts_init_tfm;
429+
inst->alg.exit = xts_exit_tfm;
423430

424-
inst->alg.setkey = setkey;
425-
inst->alg.encrypt = encrypt;
426-
inst->alg.decrypt = decrypt;
431+
inst->alg.setkey = xts_setkey;
432+
inst->alg.encrypt = xts_encrypt;
433+
inst->alg.decrypt = xts_decrypt;
427434

428-
inst->free = crypto_xts_free;
435+
inst->free = xts_free_instance;
429436

430437
err = skcipher_register_instance(tmpl, inst);
431438
if (err) {
432439
err_free_inst:
433-
crypto_xts_free(inst);
440+
xts_free_instance(inst);
434441
}
435442
return err;
436443
}
437444

438-
static struct crypto_template crypto_tmpl = {
445+
static struct crypto_template xts_tmpl = {
439446
.name = "xts",
440-
.create = create,
447+
.create = xts_create,
441448
.module = THIS_MODULE,
442449
};
443450

444-
static int __init crypto_module_init(void)
451+
static int __init xts_module_init(void)
445452
{
446-
return crypto_register_template(&crypto_tmpl);
453+
return crypto_register_template(&xts_tmpl);
447454
}
448455

449-
static void __exit crypto_module_exit(void)
456+
static void __exit xts_module_exit(void)
450457
{
451-
crypto_unregister_template(&crypto_tmpl);
458+
crypto_unregister_template(&xts_tmpl);
452459
}
453460

454-
subsys_initcall(crypto_module_init);
455-
module_exit(crypto_module_exit);
461+
subsys_initcall(xts_module_init);
462+
module_exit(xts_module_exit);
456463

457464
MODULE_LICENSE("GPL");
458465
MODULE_DESCRIPTION("XTS block cipher mode");

0 commit comments

Comments
 (0)