diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2005-10-30 05:25:15 -0500 |
---|---|---|
committer | David S. Miller <davem@sunset.davemloft.net> | 2006-01-09 17:15:34 -0500 |
commit | 06ace7a9bafeb9047352707eb79e8eaa0dfdf5f2 (patch) | |
tree | fa22bbc2e8ea5bee00b6aec353783144b6f8735a /crypto/cast6.c | |
parent | 2df15fffc612b53b2c8e4ff3c981a82441bc00ae (diff) |
[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>
Diffstat (limited to 'crypto/cast6.c')
-rw-r--r-- | crypto/cast6.c | 82 |
1 files changed, 32 insertions, 50 deletions
diff --git a/crypto/cast6.c b/crypto/cast6.c index 3eb081073423..d317fff6ea10 100644 --- a/crypto/cast6.c +++ b/crypto/cast6.c | |||
@@ -18,11 +18,13 @@ | |||
18 | */ | 18 | */ |
19 | 19 | ||
20 | 20 | ||
21 | #include <asm/byteorder.h> | ||
21 | #include <linux/init.h> | 22 | #include <linux/init.h> |
22 | #include <linux/crypto.h> | 23 | #include <linux/crypto.h> |
23 | #include <linux/module.h> | 24 | #include <linux/module.h> |
24 | #include <linux/errno.h> | 25 | #include <linux/errno.h> |
25 | #include <linux/string.h> | 26 | #include <linux/string.h> |
27 | #include <linux/types.h> | ||
26 | 28 | ||
27 | #define CAST6_BLOCK_SIZE 16 | 29 | #define CAST6_BLOCK_SIZE 16 |
28 | #define CAST6_MIN_KEY_SIZE 16 | 30 | #define CAST6_MIN_KEY_SIZE 16 |
@@ -384,7 +386,7 @@ cast6_setkey(void *ctx, const u8 * in_key, unsigned key_len, u32 * flags) | |||
384 | { | 386 | { |
385 | int i; | 387 | int i; |
386 | u32 key[8]; | 388 | u32 key[8]; |
387 | u8 p_key[32]; /* padded key */ | 389 | __be32 p_key[8]; /* padded key */ |
388 | struct cast6_ctx *c = (struct cast6_ctx *) ctx; | 390 | struct cast6_ctx *c = (struct cast6_ctx *) ctx; |
389 | 391 | ||
390 | if (key_len < 16 || key_len > 32 || key_len % 4 != 0) { | 392 | if (key_len < 16 || key_len > 32 || key_len % 4 != 0) { |
@@ -395,14 +397,14 @@ cast6_setkey(void *ctx, const u8 * in_key, unsigned key_len, u32 * flags) | |||
395 | memset (p_key, 0, 32); | 397 | memset (p_key, 0, 32); |
396 | memcpy (p_key, in_key, key_len); | 398 | memcpy (p_key, in_key, key_len); |
397 | 399 | ||
398 | key[0] = p_key[0] << 24 | p_key[1] << 16 | p_key[2] << 8 | p_key[3]; /* A */ | 400 | key[0] = be32_to_cpu(p_key[0]); /* A */ |
399 | key[1] = p_key[4] << 24 | p_key[5] << 16 | p_key[6] << 8 | p_key[7]; /* B */ | 401 | key[1] = be32_to_cpu(p_key[1]); /* B */ |
400 | key[2] = p_key[8] << 24 | p_key[9] << 16 | p_key[10] << 8 | p_key[11]; /* C */ | 402 | key[2] = be32_to_cpu(p_key[2]); /* C */ |
401 | key[3] = p_key[12] << 24 | p_key[13] << 16 | p_key[14] << 8 | p_key[15]; /* D */ | 403 | key[3] = be32_to_cpu(p_key[3]); /* D */ |
402 | key[4] = p_key[16] << 24 | p_key[17] << 16 | p_key[18] << 8 | p_key[19]; /* E */ | 404 | key[4] = be32_to_cpu(p_key[4]); /* E */ |
403 | key[5] = p_key[20] << 24 | p_key[21] << 16 | p_key[22] << 8 | p_key[23]; /* F */ | 405 | key[5] = be32_to_cpu(p_key[5]); /* F */ |
404 | key[6] = p_key[24] << 24 | p_key[25] << 16 | p_key[26] << 8 | p_key[27]; /* G */ | 406 | key[6] = be32_to_cpu(p_key[6]); /* G */ |
405 | key[7] = p_key[28] << 24 | p_key[29] << 16 | p_key[30] << 8 | p_key[31]; /* H */ | 407 | key[7] = be32_to_cpu(p_key[7]); /* H */ |
406 | 408 | ||
407 | 409 | ||
408 | 410 | ||
@@ -444,14 +446,16 @@ static inline void QBAR (u32 * block, u8 * Kr, u32 * Km) { | |||
444 | 446 | ||
445 | static void cast6_encrypt (void * ctx, u8 * outbuf, const u8 * inbuf) { | 447 | static void cast6_encrypt (void * ctx, u8 * outbuf, const u8 * inbuf) { |
446 | struct cast6_ctx * c = (struct cast6_ctx *)ctx; | 448 | struct cast6_ctx * c = (struct cast6_ctx *)ctx; |
449 | const __be32 *src = (const __be32 *)inbuf; | ||
450 | __be32 *dst = (__be32 *)outbuf; | ||
447 | u32 block[4]; | 451 | u32 block[4]; |
448 | u32 * Km; | 452 | u32 * Km; |
449 | u8 * Kr; | 453 | u8 * Kr; |
450 | 454 | ||
451 | block[0] = inbuf[0] << 24 | inbuf[1] << 16 | inbuf[2] << 8 | inbuf[3]; | 455 | block[0] = be32_to_cpu(src[0]); |
452 | block[1] = inbuf[4] << 24 | inbuf[5] << 16 | inbuf[6] << 8 | inbuf[7]; | 456 | block[1] = be32_to_cpu(src[1]); |
453 | block[2] = inbuf[8] << 24 | inbuf[9] << 16 | inbuf[10] << 8 | inbuf[11]; | 457 | block[2] = be32_to_cpu(src[2]); |
454 | block[3] = inbuf[12] << 24 | inbuf[13] << 16 | inbuf[14] << 8 | inbuf[15]; | 458 | block[3] = be32_to_cpu(src[3]); |
455 | 459 | ||
456 | Km = c->Km[0]; Kr = c->Kr[0]; Q (block, Kr, Km); | 460 | Km = c->Km[0]; Kr = c->Kr[0]; Q (block, Kr, Km); |
457 | Km = c->Km[1]; Kr = c->Kr[1]; Q (block, Kr, Km); | 461 | Km = c->Km[1]; Kr = c->Kr[1]; Q (block, Kr, Km); |
@@ -465,35 +469,25 @@ static void cast6_encrypt (void * ctx, u8 * outbuf, const u8 * inbuf) { | |||
465 | Km = c->Km[9]; Kr = c->Kr[9]; QBAR (block, Kr, Km); | 469 | Km = c->Km[9]; Kr = c->Kr[9]; QBAR (block, Kr, Km); |
466 | Km = c->Km[10]; Kr = c->Kr[10]; QBAR (block, Kr, Km); | 470 | Km = c->Km[10]; Kr = c->Kr[10]; QBAR (block, Kr, Km); |
467 | Km = c->Km[11]; Kr = c->Kr[11]; QBAR (block, Kr, Km); | 471 | Km = c->Km[11]; Kr = c->Kr[11]; QBAR (block, Kr, Km); |
468 | 472 | ||
469 | outbuf[0] = (block[0] >> 24) & 0xff; | 473 | dst[0] = cpu_to_be32(block[0]); |
470 | outbuf[1] = (block[0] >> 16) & 0xff; | 474 | dst[1] = cpu_to_be32(block[1]); |
471 | outbuf[2] = (block[0] >> 8) & 0xff; | 475 | dst[2] = cpu_to_be32(block[2]); |
472 | outbuf[3] = block[0] & 0xff; | 476 | dst[3] = cpu_to_be32(block[3]); |
473 | outbuf[4] = (block[1] >> 24) & 0xff; | ||
474 | outbuf[5] = (block[1] >> 16) & 0xff; | ||
475 | outbuf[6] = (block[1] >> 8) & 0xff; | ||
476 | outbuf[7] = block[1] & 0xff; | ||
477 | outbuf[8] = (block[2] >> 24) & 0xff; | ||
478 | outbuf[9] = (block[2] >> 16) & 0xff; | ||
479 | outbuf[10] = (block[2] >> 8) & 0xff; | ||
480 | outbuf[11] = block[2] & 0xff; | ||
481 | outbuf[12] = (block[3] >> 24) & 0xff; | ||
482 | outbuf[13] = (block[3] >> 16) & 0xff; | ||
483 | outbuf[14] = (block[3] >> 8) & 0xff; | ||
484 | outbuf[15] = block[3] & 0xff; | ||
485 | } | 477 | } |
486 | 478 | ||
487 | static void cast6_decrypt (void * ctx, u8 * outbuf, const u8 * inbuf) { | 479 | static void cast6_decrypt (void * ctx, u8 * outbuf, const u8 * inbuf) { |
488 | struct cast6_ctx * c = (struct cast6_ctx *)ctx; | 480 | struct cast6_ctx * c = (struct cast6_ctx *)ctx; |
481 | const __be32 *src = (const __be32 *)inbuf; | ||
482 | __be32 *dst = (__be32 *)outbuf; | ||
489 | u32 block[4]; | 483 | u32 block[4]; |
490 | u32 * Km; | 484 | u32 * Km; |
491 | u8 * Kr; | 485 | u8 * Kr; |
492 | 486 | ||
493 | block[0] = inbuf[0] << 24 | inbuf[1] << 16 | inbuf[2] << 8 | inbuf[3]; | 487 | block[0] = be32_to_cpu(src[0]); |
494 | block[1] = inbuf[4] << 24 | inbuf[5] << 16 | inbuf[6] << 8 | inbuf[7]; | 488 | block[1] = be32_to_cpu(src[1]); |
495 | block[2] = inbuf[8] << 24 | inbuf[9] << 16 | inbuf[10] << 8 | inbuf[11]; | 489 | block[2] = be32_to_cpu(src[2]); |
496 | block[3] = inbuf[12] << 24 | inbuf[13] << 16 | inbuf[14] << 8 | inbuf[15]; | 490 | block[3] = be32_to_cpu(src[3]); |
497 | 491 | ||
498 | Km = c->Km[11]; Kr = c->Kr[11]; Q (block, Kr, Km); | 492 | Km = c->Km[11]; Kr = c->Kr[11]; Q (block, Kr, Km); |
499 | Km = c->Km[10]; Kr = c->Kr[10]; Q (block, Kr, Km); | 493 | Km = c->Km[10]; Kr = c->Kr[10]; Q (block, Kr, Km); |
@@ -508,22 +502,10 @@ static void cast6_decrypt (void * ctx, u8 * outbuf, const u8 * inbuf) { | |||
508 | Km = c->Km[1]; Kr = c->Kr[1]; QBAR (block, Kr, Km); | 502 | Km = c->Km[1]; Kr = c->Kr[1]; QBAR (block, Kr, Km); |
509 | Km = c->Km[0]; Kr = c->Kr[0]; QBAR (block, Kr, Km); | 503 | Km = c->Km[0]; Kr = c->Kr[0]; QBAR (block, Kr, Km); |
510 | 504 | ||
511 | outbuf[0] = (block[0] >> 24) & 0xff; | 505 | dst[0] = cpu_to_be32(block[0]); |
512 | outbuf[1] = (block[0] >> 16) & 0xff; | 506 | dst[1] = cpu_to_be32(block[1]); |
513 | outbuf[2] = (block[0] >> 8) & 0xff; | 507 | dst[2] = cpu_to_be32(block[2]); |
514 | outbuf[3] = block[0] & 0xff; | 508 | dst[3] = cpu_to_be32(block[3]); |
515 | outbuf[4] = (block[1] >> 24) & 0xff; | ||
516 | outbuf[5] = (block[1] >> 16) & 0xff; | ||
517 | outbuf[6] = (block[1] >> 8) & 0xff; | ||
518 | outbuf[7] = block[1] & 0xff; | ||
519 | outbuf[8] = (block[2] >> 24) & 0xff; | ||
520 | outbuf[9] = (block[2] >> 16) & 0xff; | ||
521 | outbuf[10] = (block[2] >> 8) & 0xff; | ||
522 | outbuf[11] = block[2] & 0xff; | ||
523 | outbuf[12] = (block[3] >> 24) & 0xff; | ||
524 | outbuf[13] = (block[3] >> 16) & 0xff; | ||
525 | outbuf[14] = (block[3] >> 8) & 0xff; | ||
526 | outbuf[15] = block[3] & 0xff; | ||
527 | } | 509 | } |
528 | 510 | ||
529 | static struct crypto_alg alg = { | 511 | static struct crypto_alg alg = { |