Commit 32bd78e0 authored by Harvey Harrison's avatar Harvey Harrison Committed by Herbert Xu

crypto: camellia - use kernel-provided bitops, unaligned access

Remove the private implementation of 32-bit rotation and unaligned
access with byteswapping.

As a bonus, fixes sparse warnings:
crypto/camellia.c:602:2: warning: cast to restricted __be32
crypto/camellia.c:603:2: warning: cast to restricted __be32
crypto/camellia.c:604:2: warning: cast to restricted __be32
crypto/camellia.c:605:2: warning: cast to restricted __be32
crypto/camellia.c:710:2: warning: cast to restricted __be32
crypto/camellia.c:711:2: warning: cast to restricted __be32
crypto/camellia.c:712:2: warning: cast to restricted __be32
crypto/camellia.c:713:2: warning: cast to restricted __be32
crypto/camellia.c:714:2: warning: cast to restricted __be32
crypto/camellia.c:715:2: warning: cast to restricted __be32
crypto/camellia.c:716:2: warning: cast to restricted __be32
crypto/camellia.c:717:2: warning: cast to restricted __be32

[Thanks to Tomoyuki Okazaki for spotting the typo]
Tested-by: default avatarCarlo E. Prelz <fluido@fluido.as>
Signed-off-by: default avatarHarvey Harrison <harvey.harrison@gmail.com>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent d12d6b6d
...@@ -35,6 +35,8 @@ ...@@ -35,6 +35,8 @@
#include <linux/init.h> #include <linux/init.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/bitops.h>
#include <asm/unaligned.h>
static const u32 camellia_sp1110[256] = { static const u32 camellia_sp1110[256] = {
0x70707000,0x82828200,0x2c2c2c00,0xececec00, 0x70707000,0x82828200,0x2c2c2c00,0xececec00,
...@@ -335,20 +337,6 @@ static const u32 camellia_sp4404[256] = { ...@@ -335,20 +337,6 @@ static const u32 camellia_sp4404[256] = {
/* /*
* macros * macros
*/ */
#define GETU32(v, pt) \
do { \
/* latest breed of gcc is clever enough to use move */ \
memcpy(&(v), (pt), 4); \
(v) = be32_to_cpu(v); \
} while(0)
/* rotation right shift 1byte */
#define ROR8(x) (((x) >> 8) + ((x) << 24))
/* rotation left shift 1bit */
#define ROL1(x) (((x) << 1) + ((x) >> 31))
/* rotation left shift 1byte */
#define ROL8(x) (((x) << 8) + ((x) >> 24))
#define ROLDQ(ll, lr, rl, rr, w0, w1, bits) \ #define ROLDQ(ll, lr, rl, rr, w0, w1, bits) \
do { \ do { \
w0 = ll; \ w0 = ll; \
...@@ -383,7 +371,7 @@ static const u32 camellia_sp4404[256] = { ...@@ -383,7 +371,7 @@ static const u32 camellia_sp4404[256] = {
^ camellia_sp3033[(u8)(il >> 8)] \ ^ camellia_sp3033[(u8)(il >> 8)] \
^ camellia_sp4404[(u8)(il )]; \ ^ camellia_sp4404[(u8)(il )]; \
yl ^= yr; \ yl ^= yr; \
yr = ROR8(yr); \ yr = ror32(yr, 8); \
yr ^= yl; \ yr ^= yl; \
} while(0) } while(0)
...@@ -405,7 +393,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -405,7 +393,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
subL[7] ^= subL[1]; subR[7] ^= subR[1]; subL[7] ^= subL[1]; subR[7] ^= subR[1];
subL[1] ^= subR[1] & ~subR[9]; subL[1] ^= subR[1] & ~subR[9];
dw = subL[1] & subL[9], dw = subL[1] & subL[9],
subR[1] ^= ROL1(dw); /* modified for FLinv(kl2) */ subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl2) */
/* round 8 */ /* round 8 */
subL[11] ^= subL[1]; subR[11] ^= subR[1]; subL[11] ^= subL[1]; subR[11] ^= subR[1];
/* round 10 */ /* round 10 */
...@@ -414,7 +402,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -414,7 +402,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
subL[15] ^= subL[1]; subR[15] ^= subR[1]; subL[15] ^= subL[1]; subR[15] ^= subR[1];
subL[1] ^= subR[1] & ~subR[17]; subL[1] ^= subR[1] & ~subR[17];
dw = subL[1] & subL[17], dw = subL[1] & subL[17],
subR[1] ^= ROL1(dw); /* modified for FLinv(kl4) */ subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl4) */
/* round 14 */ /* round 14 */
subL[19] ^= subL[1]; subR[19] ^= subR[1]; subL[19] ^= subL[1]; subR[19] ^= subR[1];
/* round 16 */ /* round 16 */
...@@ -430,7 +418,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -430,7 +418,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
} else { } else {
subL[1] ^= subR[1] & ~subR[25]; subL[1] ^= subR[1] & ~subR[25];
dw = subL[1] & subL[25], dw = subL[1] & subL[25],
subR[1] ^= ROL1(dw); /* modified for FLinv(kl6) */ subR[1] ^= rol32(dw, 1); /* modified for FLinv(kl6) */
/* round 20 */ /* round 20 */
subL[27] ^= subL[1]; subR[27] ^= subR[1]; subL[27] ^= subL[1]; subR[27] ^= subR[1];
/* round 22 */ /* round 22 */
...@@ -450,7 +438,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -450,7 +438,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
subL[26] ^= kw4l; subR[26] ^= kw4r; subL[26] ^= kw4l; subR[26] ^= kw4r;
kw4l ^= kw4r & ~subR[24]; kw4l ^= kw4r & ~subR[24];
dw = kw4l & subL[24], dw = kw4l & subL[24],
kw4r ^= ROL1(dw); /* modified for FL(kl5) */ kw4r ^= rol32(dw, 1); /* modified for FL(kl5) */
} }
/* round 17 */ /* round 17 */
subL[22] ^= kw4l; subR[22] ^= kw4r; subL[22] ^= kw4l; subR[22] ^= kw4r;
...@@ -460,7 +448,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -460,7 +448,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
subL[18] ^= kw4l; subR[18] ^= kw4r; subL[18] ^= kw4l; subR[18] ^= kw4r;
kw4l ^= kw4r & ~subR[16]; kw4l ^= kw4r & ~subR[16];
dw = kw4l & subL[16], dw = kw4l & subL[16],
kw4r ^= ROL1(dw); /* modified for FL(kl3) */ kw4r ^= rol32(dw, 1); /* modified for FL(kl3) */
/* round 11 */ /* round 11 */
subL[14] ^= kw4l; subR[14] ^= kw4r; subL[14] ^= kw4l; subR[14] ^= kw4r;
/* round 9 */ /* round 9 */
...@@ -469,7 +457,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -469,7 +457,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
subL[10] ^= kw4l; subR[10] ^= kw4r; subL[10] ^= kw4l; subR[10] ^= kw4r;
kw4l ^= kw4r & ~subR[8]; kw4l ^= kw4r & ~subR[8];
dw = kw4l & subL[8], dw = kw4l & subL[8],
kw4r ^= ROL1(dw); /* modified for FL(kl1) */ kw4r ^= rol32(dw, 1); /* modified for FL(kl1) */
/* round 5 */ /* round 5 */
subL[6] ^= kw4l; subR[6] ^= kw4r; subL[6] ^= kw4l; subR[6] ^= kw4r;
/* round 3 */ /* round 3 */
...@@ -494,7 +482,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -494,7 +482,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
SUBKEY_R(6) = subR[5] ^ subR[7]; SUBKEY_R(6) = subR[5] ^ subR[7];
tl = subL[10] ^ (subR[10] & ~subR[8]); tl = subL[10] ^ (subR[10] & ~subR[8]);
dw = tl & subL[8], /* FL(kl1) */ dw = tl & subL[8], /* FL(kl1) */
tr = subR[10] ^ ROL1(dw); tr = subR[10] ^ rol32(dw, 1);
SUBKEY_L(7) = subL[6] ^ tl; /* round 6 */ SUBKEY_L(7) = subL[6] ^ tl; /* round 6 */
SUBKEY_R(7) = subR[6] ^ tr; SUBKEY_R(7) = subR[6] ^ tr;
SUBKEY_L(8) = subL[8]; /* FL(kl1) */ SUBKEY_L(8) = subL[8]; /* FL(kl1) */
...@@ -503,7 +491,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -503,7 +491,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
SUBKEY_R(9) = subR[9]; SUBKEY_R(9) = subR[9];
tl = subL[7] ^ (subR[7] & ~subR[9]); tl = subL[7] ^ (subR[7] & ~subR[9]);
dw = tl & subL[9], /* FLinv(kl2) */ dw = tl & subL[9], /* FLinv(kl2) */
tr = subR[7] ^ ROL1(dw); tr = subR[7] ^ rol32(dw, 1);
SUBKEY_L(10) = tl ^ subL[11]; /* round 7 */ SUBKEY_L(10) = tl ^ subL[11]; /* round 7 */
SUBKEY_R(10) = tr ^ subR[11]; SUBKEY_R(10) = tr ^ subR[11];
SUBKEY_L(11) = subL[10] ^ subL[12]; /* round 8 */ SUBKEY_L(11) = subL[10] ^ subL[12]; /* round 8 */
...@@ -516,7 +504,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -516,7 +504,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
SUBKEY_R(14) = subR[13] ^ subR[15]; SUBKEY_R(14) = subR[13] ^ subR[15];
tl = subL[18] ^ (subR[18] & ~subR[16]); tl = subL[18] ^ (subR[18] & ~subR[16]);
dw = tl & subL[16], /* FL(kl3) */ dw = tl & subL[16], /* FL(kl3) */
tr = subR[18] ^ ROL1(dw); tr = subR[18] ^ rol32(dw, 1);
SUBKEY_L(15) = subL[14] ^ tl; /* round 12 */ SUBKEY_L(15) = subL[14] ^ tl; /* round 12 */
SUBKEY_R(15) = subR[14] ^ tr; SUBKEY_R(15) = subR[14] ^ tr;
SUBKEY_L(16) = subL[16]; /* FL(kl3) */ SUBKEY_L(16) = subL[16]; /* FL(kl3) */
...@@ -525,7 +513,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -525,7 +513,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
SUBKEY_R(17) = subR[17]; SUBKEY_R(17) = subR[17];
tl = subL[15] ^ (subR[15] & ~subR[17]); tl = subL[15] ^ (subR[15] & ~subR[17]);
dw = tl & subL[17], /* FLinv(kl4) */ dw = tl & subL[17], /* FLinv(kl4) */
tr = subR[15] ^ ROL1(dw); tr = subR[15] ^ rol32(dw, 1);
SUBKEY_L(18) = tl ^ subL[19]; /* round 13 */ SUBKEY_L(18) = tl ^ subL[19]; /* round 13 */
SUBKEY_R(18) = tr ^ subR[19]; SUBKEY_R(18) = tr ^ subR[19];
SUBKEY_L(19) = subL[18] ^ subL[20]; /* round 14 */ SUBKEY_L(19) = subL[18] ^ subL[20]; /* round 14 */
...@@ -544,7 +532,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -544,7 +532,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
} else { } else {
tl = subL[26] ^ (subR[26] & ~subR[24]); tl = subL[26] ^ (subR[26] & ~subR[24]);
dw = tl & subL[24], /* FL(kl5) */ dw = tl & subL[24], /* FL(kl5) */
tr = subR[26] ^ ROL1(dw); tr = subR[26] ^ rol32(dw, 1);
SUBKEY_L(23) = subL[22] ^ tl; /* round 18 */ SUBKEY_L(23) = subL[22] ^ tl; /* round 18 */
SUBKEY_R(23) = subR[22] ^ tr; SUBKEY_R(23) = subR[22] ^ tr;
SUBKEY_L(24) = subL[24]; /* FL(kl5) */ SUBKEY_L(24) = subL[24]; /* FL(kl5) */
...@@ -553,7 +541,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -553,7 +541,7 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
SUBKEY_R(25) = subR[25]; SUBKEY_R(25) = subR[25];
tl = subL[23] ^ (subR[23] & ~subR[25]); tl = subL[23] ^ (subR[23] & ~subR[25]);
dw = tl & subL[25], /* FLinv(kl6) */ dw = tl & subL[25], /* FLinv(kl6) */
tr = subR[23] ^ ROL1(dw); tr = subR[23] ^ rol32(dw, 1);
SUBKEY_L(26) = tl ^ subL[27]; /* round 19 */ SUBKEY_L(26) = tl ^ subL[27]; /* round 19 */
SUBKEY_R(26) = tr ^ subR[27]; SUBKEY_R(26) = tr ^ subR[27];
SUBKEY_L(27) = subL[26] ^ subL[28]; /* round 20 */ SUBKEY_L(27) = subL[26] ^ subL[28]; /* round 20 */
...@@ -573,17 +561,17 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max) ...@@ -573,17 +561,17 @@ static void camellia_setup_tail(u32 *subkey, u32 *subL, u32 *subR, int max)
/* apply the inverse of the last half of P-function */ /* apply the inverse of the last half of P-function */
i = 2; i = 2;
do { do {
dw = SUBKEY_L(i + 0) ^ SUBKEY_R(i + 0); dw = ROL8(dw);/* round 1 */ dw = SUBKEY_L(i + 0) ^ SUBKEY_R(i + 0); dw = rol32(dw, 8);/* round 1 */
SUBKEY_R(i + 0) = SUBKEY_L(i + 0) ^ dw; SUBKEY_L(i + 0) = dw; SUBKEY_R(i + 0) = SUBKEY_L(i + 0) ^ dw; SUBKEY_L(i + 0) = dw;
dw = SUBKEY_L(i + 1) ^ SUBKEY_R(i + 1); dw = ROL8(dw);/* round 2 */ dw = SUBKEY_L(i + 1) ^ SUBKEY_R(i + 1); dw = rol32(dw, 8);/* round 2 */
SUBKEY_R(i + 1) = SUBKEY_L(i + 1) ^ dw; SUBKEY_L(i + 1) = dw; SUBKEY_R(i + 1) = SUBKEY_L(i + 1) ^ dw; SUBKEY_L(i + 1) = dw;
dw = SUBKEY_L(i + 2) ^ SUBKEY_R(i + 2); dw = ROL8(dw);/* round 3 */ dw = SUBKEY_L(i + 2) ^ SUBKEY_R(i + 2); dw = rol32(dw, 8);/* round 3 */
SUBKEY_R(i + 2) = SUBKEY_L(i + 2) ^ dw; SUBKEY_L(i + 2) = dw; SUBKEY_R(i + 2) = SUBKEY_L(i + 2) ^ dw; SUBKEY_L(i + 2) = dw;
dw = SUBKEY_L(i + 3) ^ SUBKEY_R(i + 3); dw = ROL8(dw);/* round 4 */ dw = SUBKEY_L(i + 3) ^ SUBKEY_R(i + 3); dw = rol32(dw, 8);/* round 4 */
SUBKEY_R(i + 3) = SUBKEY_L(i + 3) ^ dw; SUBKEY_L(i + 3) = dw; SUBKEY_R(i + 3) = SUBKEY_L(i + 3) ^ dw; SUBKEY_L(i + 3) = dw;
dw = SUBKEY_L(i + 4) ^ SUBKEY_R(i + 4); dw = ROL8(dw);/* round 5 */ dw = SUBKEY_L(i + 4) ^ SUBKEY_R(i + 4); dw = rol32(dw, 8);/* round 5 */
SUBKEY_R(i + 4) = SUBKEY_L(i + 4) ^ dw; SUBKEY_L(i + 4) = dw; SUBKEY_R(i + 4) = SUBKEY_L(i + 4) ^ dw; SUBKEY_L(i + 4) = dw;
dw = SUBKEY_L(i + 5) ^ SUBKEY_R(i + 5); dw = ROL8(dw);/* round 6 */ dw = SUBKEY_L(i + 5) ^ SUBKEY_R(i + 5); dw = rol32(dw, 8);/* round 6 */
SUBKEY_R(i + 5) = SUBKEY_L(i + 5) ^ dw; SUBKEY_L(i + 5) = dw; SUBKEY_R(i + 5) = SUBKEY_L(i + 5) ^ dw; SUBKEY_L(i + 5) = dw;
i += 8; i += 8;
} while (i < max); } while (i < max);
...@@ -599,10 +587,10 @@ static void camellia_setup128(const unsigned char *key, u32 *subkey) ...@@ -599,10 +587,10 @@ static void camellia_setup128(const unsigned char *key, u32 *subkey)
/** /**
* k == kll || klr || krl || krr (|| is concatenation) * k == kll || klr || krl || krr (|| is concatenation)
*/ */
GETU32(kll, key ); kll = get_unaligned_be32(key);
GETU32(klr, key + 4); klr = get_unaligned_be32(key + 4);
GETU32(krl, key + 8); krl = get_unaligned_be32(key + 8);
GETU32(krr, key + 12); krr = get_unaligned_be32(key + 12);
/* generate KL dependent subkeys */ /* generate KL dependent subkeys */
/* kw1 */ /* kw1 */
...@@ -707,14 +695,14 @@ static void camellia_setup256(const unsigned char *key, u32 *subkey) ...@@ -707,14 +695,14 @@ static void camellia_setup256(const unsigned char *key, u32 *subkey)
* key = (kll || klr || krl || krr || krll || krlr || krrl || krrr) * key = (kll || klr || krl || krr || krll || krlr || krrl || krrr)
* (|| is concatenation) * (|| is concatenation)
*/ */
GETU32(kll, key ); kll = get_unaligned_be32(key);
GETU32(klr, key + 4); klr = get_unaligned_be32(key + 4);
GETU32(krl, key + 8); krl = get_unaligned_be32(key + 8);
GETU32(krr, key + 12); krr = get_unaligned_be32(key + 12);
GETU32(krll, key + 16); krll = get_unaligned_be32(key + 16);
GETU32(krlr, key + 20); krlr = get_unaligned_be32(key + 20);
GETU32(krrl, key + 24); krrl = get_unaligned_be32(key + 24);
GETU32(krrr, key + 28); krrr = get_unaligned_be32(key + 28);
/* generate KL dependent subkeys */ /* generate KL dependent subkeys */
/* kw1 */ /* kw1 */
...@@ -870,13 +858,13 @@ static void camellia_setup192(const unsigned char *key, u32 *subkey) ...@@ -870,13 +858,13 @@ static void camellia_setup192(const unsigned char *key, u32 *subkey)
t0 &= ll; \ t0 &= ll; \
t2 |= rr; \ t2 |= rr; \
rl ^= t2; \ rl ^= t2; \
lr ^= ROL1(t0); \ lr ^= rol32(t0, 1); \
t3 = krl; \ t3 = krl; \
t1 = klr; \ t1 = klr; \
t3 &= rl; \ t3 &= rl; \
t1 |= lr; \ t1 |= lr; \
ll ^= t1; \ ll ^= t1; \
rr ^= ROL1(t3); \ rr ^= rol32(t3, 1); \
} while(0) } while(0)
#define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir) \ #define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir) \
...@@ -892,7 +880,7 @@ static void camellia_setup192(const unsigned char *key, u32 *subkey) ...@@ -892,7 +880,7 @@ static void camellia_setup192(const unsigned char *key, u32 *subkey)
il ^= kl; \ il ^= kl; \
ir ^= il ^ kr; \ ir ^= il ^ kr; \
yl ^= ir; \ yl ^= ir; \
yr ^= ROR8(il) ^ ir; \ yr ^= ror32(il, 8) ^ ir; \
} while(0) } while(0)
/* max = 24: 128bit encrypt, max = 32: 256bit encrypt */ /* max = 24: 128bit encrypt, max = 32: 256bit encrypt */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment