Skip to content

Commit e55318c

Browse files
WOnder93herbertx
authored andcommitted
crypto: gf128mul - switch gf128mul_x_ble to le128
Currently, gf128mul_x_ble works with pointers to be128, even though it actually interprets the words as little-endian. Consequently, it uses cpu_to_le64/le64_to_cpu on fields of type __be64, which is incorrect. This patch fixes that by changing the function to accept pointers to le128 and updating all users accordingly. Signed-off-by: Ondrej Mosnacek <omosnacek@gmail.com> Reviewd-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent acb9b15 commit e55318c

6 files changed

Lines changed: 30 additions & 30 deletions

File tree

‎arch/x86/crypto/camellia_glue.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
15221522
struct scatterlist *src, unsigned int nbytes)
15231523
{
15241524
struct camellia_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
1525-
be128 buf[2 * 4];
1525+
le128 buf[2 * 4];
15261526
struct xts_crypt_req req = {
15271527
.tbuf = buf,
15281528
.tbuflen = sizeof(buf),
@@ -1540,7 +1540,7 @@ static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
15401540
struct scatterlist *src, unsigned int nbytes)
15411541
{
15421542
struct camellia_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
1543-
be128 buf[2 * 4];
1543+
le128 buf[2 * 4];
15441544
struct xts_crypt_req req = {
15451545
.tbuf = buf,
15461546
.tbuflen = sizeof(buf),

‎arch/x86/crypto/serpent_sse2_glue.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
328328
struct scatterlist *src, unsigned int nbytes)
329329
{
330330
struct serpent_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
331-
be128 buf[SERPENT_PARALLEL_BLOCKS];
331+
le128 buf[SERPENT_PARALLEL_BLOCKS];
332332
struct crypt_priv crypt_ctx = {
333333
.ctx = &ctx->crypt_ctx,
334334
.fpu_enabled = false,
@@ -355,7 +355,7 @@ static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
355355
struct scatterlist *src, unsigned int nbytes)
356356
{
357357
struct serpent_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
358-
be128 buf[SERPENT_PARALLEL_BLOCKS];
358+
le128 buf[SERPENT_PARALLEL_BLOCKS];
359359
struct crypt_priv crypt_ctx = {
360360
.ctx = &ctx->crypt_ctx,
361361
.fpu_enabled = false,

‎arch/x86/crypto/twofish_glue_3way.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static int xts_encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
296296
struct scatterlist *src, unsigned int nbytes)
297297
{
298298
struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
299-
be128 buf[3];
299+
le128 buf[3];
300300
struct xts_crypt_req req = {
301301
.tbuf = buf,
302302
.tbuflen = sizeof(buf),
@@ -314,7 +314,7 @@ static int xts_decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
314314
struct scatterlist *src, unsigned int nbytes)
315315
{
316316
struct twofish_xts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
317-
be128 buf[3];
317+
le128 buf[3];
318318
struct xts_crypt_req req = {
319319
.tbuf = buf,
320320
.tbuflen = sizeof(buf),

‎crypto/xts.c‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ struct xts_instance_ctx {
3939
};
4040

4141
struct rctx {
42-
be128 buf[XTS_BUFFER_SIZE / sizeof(be128)];
42+
le128 buf[XTS_BUFFER_SIZE / sizeof(le128)];
4343

44-
be128 t;
44+
le128 t;
4545

46-
be128 *ext;
46+
le128 *ext;
4747

4848
struct scatterlist srcbuf[2];
4949
struct scatterlist dstbuf[2];
@@ -99,7 +99,7 @@ static int setkey(struct crypto_skcipher *parent, const u8 *key,
9999
static int post_crypt(struct skcipher_request *req)
100100
{
101101
struct rctx *rctx = skcipher_request_ctx(req);
102-
be128 *buf = rctx->ext ?: rctx->buf;
102+
le128 *buf = rctx->ext ?: rctx->buf;
103103
struct skcipher_request *subreq;
104104
const int bs = XTS_BLOCK_SIZE;
105105
struct skcipher_walk w;
@@ -112,12 +112,12 @@ static int post_crypt(struct skcipher_request *req)
112112

113113
while (w.nbytes) {
114114
unsigned int avail = w.nbytes;
115-
be128 *wdst;
115+
le128 *wdst;
116116

117117
wdst = w.dst.virt.addr;
118118

119119
do {
120-
be128_xor(wdst, buf++, wdst);
120+
le128_xor(wdst, buf++, wdst);
121121
wdst++;
122122
} while ((avail -= bs) >= bs);
123123

@@ -150,7 +150,7 @@ static int post_crypt(struct skcipher_request *req)
150150
static int pre_crypt(struct skcipher_request *req)
151151
{
152152
struct rctx *rctx = skcipher_request_ctx(req);
153-
be128 *buf = rctx->ext ?: rctx->buf;
153+
le128 *buf = rctx->ext ?: rctx->buf;
154154
struct skcipher_request *subreq;
155155
const int bs = XTS_BLOCK_SIZE;
156156
struct skcipher_walk w;
@@ -174,15 +174,15 @@ static int pre_crypt(struct skcipher_request *req)
174174

175175
while (w.nbytes) {
176176
unsigned int avail = w.nbytes;
177-
be128 *wsrc;
178-
be128 *wdst;
177+
le128 *wsrc;
178+
le128 *wdst;
179179

180180
wsrc = w.src.virt.addr;
181181
wdst = w.dst.virt.addr;
182182

183183
do {
184184
*buf++ = rctx->t;
185-
be128_xor(wdst++, &rctx->t, wsrc++);
185+
le128_xor(wdst++, &rctx->t, wsrc++);
186186
gf128mul_x_ble(&rctx->t, &rctx->t);
187187
} while ((avail -= bs) >= bs);
188188

@@ -353,8 +353,8 @@ int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
353353
const unsigned int max_blks = req->tbuflen / bsize;
354354
struct blkcipher_walk walk;
355355
unsigned int nblocks;
356-
be128 *src, *dst, *t;
357-
be128 *t_buf = req->tbuf;
356+
le128 *src, *dst, *t;
357+
le128 *t_buf = req->tbuf;
358358
int err, i;
359359

360360
BUG_ON(max_blks < 1);
@@ -367,8 +367,8 @@ int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
367367
return err;
368368

369369
nblocks = min(nbytes / bsize, max_blks);
370-
src = (be128 *)walk.src.virt.addr;
371-
dst = (be128 *)walk.dst.virt.addr;
370+
src = (le128 *)walk.src.virt.addr;
371+
dst = (le128 *)walk.dst.virt.addr;
372372

373373
/* calculate first value of T */
374374
req->tweak_fn(req->tweak_ctx, (u8 *)&t_buf[0], walk.iv);
@@ -384,7 +384,7 @@ int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
384384
t = &t_buf[i];
385385

386386
/* PP <- T xor P */
387-
be128_xor(dst + i, t, src + i);
387+
le128_xor(dst + i, t, src + i);
388388
}
389389

390390
/* CC <- E(Key2,PP) */
@@ -393,24 +393,24 @@ int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
393393

394394
/* C <- T xor CC */
395395
for (i = 0; i < nblocks; i++)
396-
be128_xor(dst + i, dst + i, &t_buf[i]);
396+
le128_xor(dst + i, dst + i, &t_buf[i]);
397397

398398
src += nblocks;
399399
dst += nblocks;
400400
nbytes -= nblocks * bsize;
401401
nblocks = min(nbytes / bsize, max_blks);
402402
} while (nblocks > 0);
403403

404-
*(be128 *)walk.iv = *t;
404+
*(le128 *)walk.iv = *t;
405405

406406
err = blkcipher_walk_done(desc, &walk, nbytes);
407407
nbytes = walk.nbytes;
408408
if (!nbytes)
409409
break;
410410

411411
nblocks = min(nbytes / bsize, max_blks);
412-
src = (be128 *)walk.src.virt.addr;
413-
dst = (be128 *)walk.dst.virt.addr;
412+
src = (le128 *)walk.src.virt.addr;
413+
dst = (le128 *)walk.dst.virt.addr;
414414
}
415415

416416
return err;

‎include/crypto/gf128mul.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,16 @@ static inline void gf128mul_x_bbe(be128 *r, const be128 *x)
205205
}
206206

207207
/* needed by XTS */
208-
static inline void gf128mul_x_ble(be128 *r, const be128 *x)
208+
static inline void gf128mul_x_ble(le128 *r, const le128 *x)
209209
{
210210
u64 a = le64_to_cpu(x->a);
211211
u64 b = le64_to_cpu(x->b);
212212

213213
/* equivalent to gf128mul_table_be[b >> 63] (see crypto/gf128mul.c): */
214-
u64 _tt = gf128mul_mask_from_bit(b, 63) & 0x87;
214+
u64 _tt = gf128mul_mask_from_bit(a, 63) & 0x87;
215215

216-
r->a = cpu_to_le64((a << 1) ^ _tt);
217-
r->b = cpu_to_le64((b << 1) | (a >> 63));
216+
r->a = cpu_to_le64((a << 1) | (b >> 63));
217+
r->b = cpu_to_le64((b << 1) ^ _tt);
218218
}
219219

220220
/* 4k table optimization */

‎include/crypto/xts.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct blkcipher_desc;
1111
#define XTS_BLOCK_SIZE 16
1212

1313
struct xts_crypt_req {
14-
be128 *tbuf;
14+
le128 *tbuf;
1515
unsigned int tbuflen;
1616

1717
void *tweak_ctx;

0 commit comments

Comments
 (0)