Skip to content

Commit 06ace7a

Browse files
herbertxDavid S. Miller
authored andcommitted
[CRYPTO] Use standard byte order macros wherever possible
A lot of crypto code needs to read/write a 32-bit/64-bit words in a specific gender. Many of them open code them by reading/writing one byte at a time. This patch converts all the applicable usages over to use the standard byte order macros. This is based on a previous patch by Denis Vlasenko. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 2df15ff commit 06ace7a

22 files changed

Lines changed: 285 additions & 441 deletions

File tree

‎arch/i386/crypto/aes.c‎

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
* Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
3737
*
3838
*/
39+
40+
#include <asm/byteorder.h>
3941
#include <linux/kernel.h>
4042
#include <linux/module.h>
4143
#include <linux/init.h>
@@ -59,7 +61,6 @@ struct aes_ctx {
5961
};
6062

6163
#define WPOLY 0x011b
62-
#define u32_in(x) le32_to_cpup((const __le32 *)(x))
6364
#define bytes2word(b0, b1, b2, b3) \
6465
(((u32)(b3) << 24) | ((u32)(b2) << 16) | ((u32)(b1) << 8) | (b0))
6566

@@ -393,13 +394,14 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
393394
int i;
394395
u32 ss[8];
395396
struct aes_ctx *ctx = ctx_arg;
397+
const __le32 *key = (const __le32 *)in_key;
396398

397399
/* encryption schedule */
398400

399-
ctx->ekey[0] = ss[0] = u32_in(in_key);
400-
ctx->ekey[1] = ss[1] = u32_in(in_key + 4);
401-
ctx->ekey[2] = ss[2] = u32_in(in_key + 8);
402-
ctx->ekey[3] = ss[3] = u32_in(in_key + 12);
401+
ctx->ekey[0] = ss[0] = le32_to_cpu(key[0]);
402+
ctx->ekey[1] = ss[1] = le32_to_cpu(key[1]);
403+
ctx->ekey[2] = ss[2] = le32_to_cpu(key[2]);
404+
ctx->ekey[3] = ss[3] = le32_to_cpu(key[3]);
403405

404406
switch(key_len) {
405407
case 16:
@@ -410,19 +412,19 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
410412
break;
411413

412414
case 24:
413-
ctx->ekey[4] = ss[4] = u32_in(in_key + 16);
414-
ctx->ekey[5] = ss[5] = u32_in(in_key + 20);
415+
ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
416+
ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
415417
for (i = 0; i < 7; i++)
416418
ke6(ctx->ekey, i);
417419
kel6(ctx->ekey, 7);
418420
ctx->rounds = 12;
419421
break;
420422

421423
case 32:
422-
ctx->ekey[4] = ss[4] = u32_in(in_key + 16);
423-
ctx->ekey[5] = ss[5] = u32_in(in_key + 20);
424-
ctx->ekey[6] = ss[6] = u32_in(in_key + 24);
425-
ctx->ekey[7] = ss[7] = u32_in(in_key + 28);
424+
ctx->ekey[4] = ss[4] = le32_to_cpu(key[4]);
425+
ctx->ekey[5] = ss[5] = le32_to_cpu(key[5]);
426+
ctx->ekey[6] = ss[6] = le32_to_cpu(key[6]);
427+
ctx->ekey[7] = ss[7] = le32_to_cpu(key[7]);
426428
for (i = 0; i < 6; i++)
427429
ke8(ctx->ekey, i);
428430
kel8(ctx->ekey, 6);
@@ -436,10 +438,10 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
436438

437439
/* decryption schedule */
438440

439-
ctx->dkey[0] = ss[0] = u32_in(in_key);
440-
ctx->dkey[1] = ss[1] = u32_in(in_key + 4);
441-
ctx->dkey[2] = ss[2] = u32_in(in_key + 8);
442-
ctx->dkey[3] = ss[3] = u32_in(in_key + 12);
441+
ctx->dkey[0] = ss[0] = le32_to_cpu(key[0]);
442+
ctx->dkey[1] = ss[1] = le32_to_cpu(key[1]);
443+
ctx->dkey[2] = ss[2] = le32_to_cpu(key[2]);
444+
ctx->dkey[3] = ss[3] = le32_to_cpu(key[3]);
443445

444446
switch (key_len) {
445447
case 16:
@@ -450,19 +452,19 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
450452
break;
451453

452454
case 24:
453-
ctx->dkey[4] = ff(ss[4] = u32_in(in_key + 16));
454-
ctx->dkey[5] = ff(ss[5] = u32_in(in_key + 20));
455+
ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
456+
ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
455457
kdf6(ctx->dkey, 0);
456458
for (i = 1; i < 7; i++)
457459
kd6(ctx->dkey, i);
458460
kdl6(ctx->dkey, 7);
459461
break;
460462

461463
case 32:
462-
ctx->dkey[4] = ff(ss[4] = u32_in(in_key + 16));
463-
ctx->dkey[5] = ff(ss[5] = u32_in(in_key + 20));
464-
ctx->dkey[6] = ff(ss[6] = u32_in(in_key + 24));
465-
ctx->dkey[7] = ff(ss[7] = u32_in(in_key + 28));
464+
ctx->dkey[4] = ff(ss[4] = le32_to_cpu(key[4]));
465+
ctx->dkey[5] = ff(ss[5] = le32_to_cpu(key[5]));
466+
ctx->dkey[6] = ff(ss[6] = le32_to_cpu(key[6]));
467+
ctx->dkey[7] = ff(ss[7] = le32_to_cpu(key[7]));
466468
kdf8(ctx->dkey, 0);
467469
for (i = 1; i < 6; i++)
468470
kd8(ctx->dkey, i);

‎arch/x86_64/crypto/aes.c‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ static inline u8 byte(const u32 x, const unsigned n)
7474
return x >> (n << 3);
7575
}
7676

77-
#define u32_in(x) le32_to_cpu(*(const __le32 *)(x))
78-
7977
struct aes_ctx
8078
{
8179
u32 key_length;
@@ -234,6 +232,7 @@ static int aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len,
234232
u32 *flags)
235233
{
236234
struct aes_ctx *ctx = ctx_arg;
235+
const __le32 *key = (const __le32 *)in_key;
237236
u32 i, j, t, u, v, w;
238237

239238
if (key_len != 16 && key_len != 24 && key_len != 32) {
@@ -243,10 +242,10 @@ static int aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len,
243242

244243
ctx->key_length = key_len;
245244

246-
D_KEY[key_len + 24] = E_KEY[0] = u32_in(in_key);
247-
D_KEY[key_len + 25] = E_KEY[1] = u32_in(in_key + 4);
248-
D_KEY[key_len + 26] = E_KEY[2] = u32_in(in_key + 8);
249-
D_KEY[key_len + 27] = E_KEY[3] = u32_in(in_key + 12);
245+
D_KEY[key_len + 24] = E_KEY[0] = le32_to_cpu(key[0]);
246+
D_KEY[key_len + 25] = E_KEY[1] = le32_to_cpu(key[1]);
247+
D_KEY[key_len + 26] = E_KEY[2] = le32_to_cpu(key[2]);
248+
D_KEY[key_len + 27] = E_KEY[3] = le32_to_cpu(key[3]);
250249

251250
switch (key_len) {
252251
case 16:
@@ -256,17 +255,17 @@ static int aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len,
256255
break;
257256

258257
case 24:
259-
E_KEY[4] = u32_in(in_key + 16);
260-
t = E_KEY[5] = u32_in(in_key + 20);
258+
E_KEY[4] = le32_to_cpu(key[4]);
259+
t = E_KEY[5] = le32_to_cpu(key[5]);
261260
for (i = 0; i < 8; ++i)
262261
loop6 (i);
263262
break;
264263

265264
case 32:
266-
E_KEY[4] = u32_in(in_key + 16);
267-
E_KEY[5] = u32_in(in_key + 20);
268-
E_KEY[6] = u32_in(in_key + 24);
269-
t = E_KEY[7] = u32_in(in_key + 28);
265+
E_KEY[4] = le32_to_cpu(key[4]);
266+
E_KEY[5] = le32_to_cpu(key[5]);
267+
E_KEY[6] = le32_to_cpu(key[6]);
268+
t = E_KEY[7] = le32_to_cpu(key[7]);
270269
for (i = 0; i < 7; ++i)
271270
loop8(i);
272271
break;

‎crypto/aes.c‎

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ byte(const u32 x, const unsigned n)
7373
return x >> (n << 3);
7474
}
7575

76-
#define u32_in(x) le32_to_cpu(*(const u32 *)(x))
77-
#define u32_out(to, from) (*(u32 *)(to) = cpu_to_le32(from))
78-
7976
struct aes_ctx {
8077
int key_length;
8178
u32 E[60];
@@ -256,6 +253,7 @@ static int
256253
aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
257254
{
258255
struct aes_ctx *ctx = ctx_arg;
256+
const __le32 *key = (const __le32 *)in_key;
259257
u32 i, t, u, v, w;
260258

261259
if (key_len != 16 && key_len != 24 && key_len != 32) {
@@ -265,10 +263,10 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
265263

266264
ctx->key_length = key_len;
267265

268-
E_KEY[0] = u32_in (in_key);
269-
E_KEY[1] = u32_in (in_key + 4);
270-
E_KEY[2] = u32_in (in_key + 8);
271-
E_KEY[3] = u32_in (in_key + 12);
266+
E_KEY[0] = le32_to_cpu(key[0]);
267+
E_KEY[1] = le32_to_cpu(key[1]);
268+
E_KEY[2] = le32_to_cpu(key[2]);
269+
E_KEY[3] = le32_to_cpu(key[3]);
272270

273271
switch (key_len) {
274272
case 16:
@@ -278,17 +276,17 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
278276
break;
279277

280278
case 24:
281-
E_KEY[4] = u32_in (in_key + 16);
282-
t = E_KEY[5] = u32_in (in_key + 20);
279+
E_KEY[4] = le32_to_cpu(key[4]);
280+
t = E_KEY[5] = le32_to_cpu(key[5]);
283281
for (i = 0; i < 8; ++i)
284282
loop6 (i);
285283
break;
286284

287285
case 32:
288-
E_KEY[4] = u32_in (in_key + 16);
289-
E_KEY[5] = u32_in (in_key + 20);
290-
E_KEY[6] = u32_in (in_key + 24);
291-
t = E_KEY[7] = u32_in (in_key + 28);
286+
E_KEY[4] = le32_to_cpu(key[4]);
287+
E_KEY[5] = le32_to_cpu(key[5]);
288+
E_KEY[6] = le32_to_cpu(key[6]);
289+
t = E_KEY[7] = le32_to_cpu(key[7]);
292290
for (i = 0; i < 7; ++i)
293291
loop8 (i);
294292
break;
@@ -324,13 +322,15 @@ aes_set_key(void *ctx_arg, const u8 *in_key, unsigned int key_len, u32 *flags)
324322
static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
325323
{
326324
const struct aes_ctx *ctx = ctx_arg;
325+
const __le32 *src = (const __le32 *)in;
326+
__le32 *dst = (__le32 *)out;
327327
u32 b0[4], b1[4];
328328
const u32 *kp = E_KEY + 4;
329329

330-
b0[0] = u32_in (in) ^ E_KEY[0];
331-
b0[1] = u32_in (in + 4) ^ E_KEY[1];
332-
b0[2] = u32_in (in + 8) ^ E_KEY[2];
333-
b0[3] = u32_in (in + 12) ^ E_KEY[3];
330+
b0[0] = le32_to_cpu(src[0]) ^ E_KEY[0];
331+
b0[1] = le32_to_cpu(src[1]) ^ E_KEY[1];
332+
b0[2] = le32_to_cpu(src[2]) ^ E_KEY[2];
333+
b0[3] = le32_to_cpu(src[3]) ^ E_KEY[3];
334334

335335
if (ctx->key_length > 24) {
336336
f_nround (b1, b0, kp);
@@ -353,10 +353,10 @@ static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
353353
f_nround (b1, b0, kp);
354354
f_lround (b0, b1, kp);
355355

356-
u32_out (out, b0[0]);
357-
u32_out (out + 4, b0[1]);
358-
u32_out (out + 8, b0[2]);
359-
u32_out (out + 12, b0[3]);
356+
dst[0] = cpu_to_le32(b0[0]);
357+
dst[1] = cpu_to_le32(b0[1]);
358+
dst[2] = cpu_to_le32(b0[2]);
359+
dst[3] = cpu_to_le32(b0[3]);
360360
}
361361

362362
/* decrypt a block of text */
@@ -377,14 +377,16 @@ static void aes_encrypt(void *ctx_arg, u8 *out, const u8 *in)
377377
static void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in)
378378
{
379379
const struct aes_ctx *ctx = ctx_arg;
380+
const __le32 *src = (const __le32 *)in;
381+
__le32 *dst = (__le32 *)out;
380382
u32 b0[4], b1[4];
381383
const int key_len = ctx->key_length;
382384
const u32 *kp = D_KEY + key_len + 20;
383385

384-
b0[0] = u32_in (in) ^ E_KEY[key_len + 24];
385-
b0[1] = u32_in (in + 4) ^ E_KEY[key_len + 25];
386-
b0[2] = u32_in (in + 8) ^ E_KEY[key_len + 26];
387-
b0[3] = u32_in (in + 12) ^ E_KEY[key_len + 27];
386+
b0[0] = le32_to_cpu(src[0]) ^ E_KEY[key_len + 24];
387+
b0[1] = le32_to_cpu(src[1]) ^ E_KEY[key_len + 25];
388+
b0[2] = le32_to_cpu(src[2]) ^ E_KEY[key_len + 26];
389+
b0[3] = le32_to_cpu(src[3]) ^ E_KEY[key_len + 27];
388390

389391
if (key_len > 24) {
390392
i_nround (b1, b0, kp);
@@ -407,10 +409,10 @@ static void aes_decrypt(void *ctx_arg, u8 *out, const u8 *in)
407409
i_nround (b1, b0, kp);
408410
i_lround (b0, b1, kp);
409411

410-
u32_out (out, b0[0]);
411-
u32_out (out + 4, b0[1]);
412-
u32_out (out + 8, b0[2]);
413-
u32_out (out + 12, b0[3]);
412+
dst[0] = cpu_to_le32(b0[0]);
413+
dst[1] = cpu_to_le32(b0[1]);
414+
dst[2] = cpu_to_le32(b0[2]);
415+
dst[3] = cpu_to_le32(b0[3]);
414416
}
415417

416418

‎crypto/anubis.c‎

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
#include <linux/init.h>
3333
#include <linux/module.h>
3434
#include <linux/mm.h>
35+
#include <asm/byteorder.h>
3536
#include <asm/scatterlist.h>
3637
#include <linux/crypto.h>
38+
#include <linux/types.h>
3739

3840
#define ANUBIS_MIN_KEY_SIZE 16
3941
#define ANUBIS_MAX_KEY_SIZE 40
@@ -461,8 +463,8 @@ static const u32 rc[] = {
461463
static int anubis_setkey(void *ctx_arg, const u8 *in_key,
462464
unsigned int key_len, u32 *flags)
463465
{
464-
465-
int N, R, i, pos, r;
466+
const __be32 *key = (const __be32 *)in_key;
467+
int N, R, i, r;
466468
u32 kappa[ANUBIS_MAX_N];
467469
u32 inter[ANUBIS_MAX_N];
468470

@@ -483,13 +485,8 @@ static int anubis_setkey(void *ctx_arg, const u8 *in_key,
483485
ctx->R = R = 8 + N;
484486

485487
/* * map cipher key to initial key state (mu): */
486-
for (i = 0, pos = 0; i < N; i++, pos += 4) {
487-
kappa[i] =
488-
(in_key[pos ] << 24) ^
489-
(in_key[pos + 1] << 16) ^
490-
(in_key[pos + 2] << 8) ^
491-
(in_key[pos + 3] );
492-
}
488+
for (i = 0; i < N; i++)
489+
kappa[i] = be32_to_cpu(key[i]);
493490

494491
/*
495492
* generate R + 1 round keys:
@@ -578,22 +575,18 @@ static int anubis_setkey(void *ctx_arg, const u8 *in_key,
578575
static void anubis_crypt(u32 roundKey[ANUBIS_MAX_ROUNDS + 1][4],
579576
u8 *ciphertext, const u8 *plaintext, const int R)
580577
{
581-
int i, pos, r;
578+
const __be32 *src = (const __be32 *)plaintext;
579+
__be32 *dst = (__be32 *)ciphertext;
580+
int i, r;
582581
u32 state[4];
583582
u32 inter[4];
584583

585584
/*
586585
* map plaintext block to cipher state (mu)
587586
* and add initial round key (sigma[K^0]):
588587
*/
589-
for (i = 0, pos = 0; i < 4; i++, pos += 4) {
590-
state[i] =
591-
(plaintext[pos ] << 24) ^
592-
(plaintext[pos + 1] << 16) ^
593-
(plaintext[pos + 2] << 8) ^
594-
(plaintext[pos + 3] ) ^
595-
roundKey[0][i];
596-
}
588+
for (i = 0; i < 4; i++)
589+
state[i] = be32_to_cpu(src[i]) ^ roundKey[0][i];
597590

598591
/*
599592
* R - 1 full rounds:
@@ -663,13 +656,8 @@ static void anubis_crypt(u32 roundKey[ANUBIS_MAX_ROUNDS + 1][4],
663656
* map cipher state to ciphertext block (mu^{-1}):
664657
*/
665658

666-
for (i = 0, pos = 0; i < 4; i++, pos += 4) {
667-
u32 w = inter[i];
668-
ciphertext[pos ] = (u8)(w >> 24);
669-
ciphertext[pos + 1] = (u8)(w >> 16);
670-
ciphertext[pos + 2] = (u8)(w >> 8);
671-
ciphertext[pos + 3] = (u8)(w );
672-
}
659+
for (i = 0; i < 4; i++)
660+
dst[i] = cpu_to_be32(inter[i]);
673661
}
674662

675663
static void anubis_encrypt(void *ctx_arg, u8 *dst, const u8 *src)

‎crypto/blowfish.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#include <linux/init.h>
2020
#include <linux/module.h>
2121
#include <linux/mm.h>
22+
#include <asm/byteorder.h>
2223
#include <asm/scatterlist.h>
2324
#include <linux/crypto.h>
25+
#include <linux/types.h>
2426

2527
#define BF_BLOCK_SIZE 8
2628
#define BF_MIN_KEY_SIZE 4

0 commit comments

Comments
 (0)