2008-03-08 09:23:06

by Harvey Harrison

[permalink] [raw]
Subject: [PATCH 3/5] mac80211: replace Mk16 helper with le16_to_cpup

Opencoded byte-swapping could just use the kernel helper.

Signed-off-by: Harvey Harrison <[email protected]>
---
net/mac80211/tkip.c | 47 ++++++++++++++++++++++-------------------------
1 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/net/mac80211/tkip.c b/net/mac80211/tkip.c
index 90c11e7..9a36e83 100644
--- a/net/mac80211/tkip.c
+++ b/net/mac80211/tkip.c
@@ -60,13 +60,6 @@ static const u16 tkip_sbox[256] =
0x82C3, 0x29B0, 0x5A77, 0x1E11, 0x7BCB, 0xA8FC, 0x6DD6, 0x2C3A,
};

-
-static inline u16 Mk16(u8 x, u8 y)
-{
- return ((u16) x << 8) | (u16) y;
-}
-
-
static inline u8 Hi8(u16 v)
{
return v >> 8;
@@ -107,20 +100,22 @@ static void tkip_mixing_phase1(const u8 *ta, const u8 *tk, u32 tsc_IV32,
u16 *p1k)
{
int i, j;
+ __le16 *leptr = (__le16 *)ta;

p1k[0] = Lo16(tsc_IV32);
p1k[1] = Hi16(tsc_IV32);
- p1k[2] = Mk16(ta[1], ta[0]);
- p1k[3] = Mk16(ta[3], ta[2]);
- p1k[4] = Mk16(ta[5], ta[4]);
+ p1k[2] = le16_to_cpup(leptr++);
+ p1k[3] = le16_to_cpup(leptr++);
+ p1k[4] = le16_to_cpup(leptr++);

+ leptr = (__le16 *)tk;
for (i = 0; i < PHASE1_LOOP_COUNT; i++) {
- j = 2 * (i & 1);
- p1k[0] += tkip_S(p1k[4] ^ Mk16(tk[ 1 + j], tk[ 0 + j]));
- p1k[1] += tkip_S(p1k[0] ^ Mk16(tk[ 5 + j], tk[ 4 + j]));
- p1k[2] += tkip_S(p1k[1] ^ Mk16(tk[ 9 + j], tk[ 8 + j]));
- p1k[3] += tkip_S(p1k[2] ^ Mk16(tk[13 + j], tk[12 + j]));
- p1k[4] += tkip_S(p1k[3] ^ Mk16(tk[ 1 + j], tk[ 0 + j])) + i;
+ j = (i & 1);
+ p1k[0] += tkip_S(p1k[4] ^ le16_to_cpup(leptr + 0 + j));
+ p1k[1] += tkip_S(p1k[0] ^ le16_to_cpup(leptr + 2 + j));
+ p1k[2] += tkip_S(p1k[1] ^ le16_to_cpup(leptr + 4 + j));
+ p1k[3] += tkip_S(p1k[2] ^ le16_to_cpup(leptr + 6 + j));
+ p1k[4] += tkip_S(p1k[3] ^ le16_to_cpup(leptr + 0 + j)) + i;
}
}

@@ -130,6 +125,7 @@ static void tkip_mixing_phase2(const u16 *p1k, const u8 *tk, u16 tsc_IV16,
{
u16 ppk[6];
int i;
+ __le16 *leptr = (__le16 *)tk;

ppk[0] = p1k[0];
ppk[1] = p1k[1];
@@ -138,23 +134,24 @@ static void tkip_mixing_phase2(const u16 *p1k, const u8 *tk, u16 tsc_IV16,
ppk[4] = p1k[4];
ppk[5] = p1k[4] + tsc_IV16;

- ppk[0] += tkip_S(ppk[5] ^ Mk16(tk[ 1], tk[ 0]));
- ppk[1] += tkip_S(ppk[0] ^ Mk16(tk[ 3], tk[ 2]));
- ppk[2] += tkip_S(ppk[1] ^ Mk16(tk[ 5], tk[ 4]));
- ppk[3] += tkip_S(ppk[2] ^ Mk16(tk[ 7], tk[ 6]));
- ppk[4] += tkip_S(ppk[3] ^ Mk16(tk[ 9], tk[ 8]));
- ppk[5] += tkip_S(ppk[4] ^ Mk16(tk[11], tk[10]));
- ppk[0] += ror16(ppk[5] ^ Mk16(tk[13], tk[12]), 1);
- ppk[1] += ror16(ppk[0] ^ Mk16(tk[15], tk[14]), 1);
+ ppk[0] += tkip_S(ppk[5] ^ le16_to_cpup(leptr++));
+ ppk[1] += tkip_S(ppk[0] ^ le16_to_cpup(leptr++));
+ ppk[2] += tkip_S(ppk[1] ^ le16_to_cpup(leptr++));
+ ppk[3] += tkip_S(ppk[2] ^ le16_to_cpup(leptr++));
+ ppk[4] += tkip_S(ppk[3] ^ le16_to_cpup(leptr++));
+ ppk[5] += tkip_S(ppk[4] ^ le16_to_cpup(leptr++));
+ ppk[0] += ror16(ppk[5] ^ le16_to_cpup(leptr++), 1);
+ ppk[1] += ror16(ppk[0] ^ le16_to_cpup(leptr++), 1);
ppk[2] += ror16(ppk[1], 1);
ppk[3] += ror16(ppk[2], 1);
ppk[4] += ror16(ppk[3], 1);
ppk[5] += ror16(ppk[4], 1);

+ leptr = (__le16 *)tk;
rc4key[0] = Hi8(tsc_IV16);
rc4key[1] = (Hi8(tsc_IV16) | 0x20) & 0x7f;
rc4key[2] = Lo8(tsc_IV16);
- rc4key[3] = Lo8((ppk[5] ^ Mk16(tk[1], tk[0])) >> 1);
+ rc4key[3] = Lo8((ppk[5] ^ le16_to_cpup(leptr)) >> 1);

for (i = 0; i < 6; i++) {
rc4key[4 + 2 * i] = Lo8(ppk[i]);
--
1.5.4.GIT




2008-03-11 16:47:58

by Harvey Harrison

[permalink] [raw]
Subject: Re: [PATCH 3/5] mac80211: replace Mk16 helper with le16_to_cpup

On Tue, 2008-03-11 at 17:14 +0100, Johannes Berg wrote:
> On Sat, 2008-03-08 at 01:23 -0800, Harvey Harrison wrote:
> > Opencoded byte-swapping could just use the kernel helper.
>
> > + p1k[2] = le16_to_cpup(leptr++);
>
> I'm ok with this if architectures guarantee that these operations work
> on unaligned values, but I don't think that is the case.
>

I'm looking into this, but I'm pretty sure this isn't right as-is.

I wasn't thinking about alignment at the time, just ignore this.

Harvey


2008-03-11 16:14:44

by Johannes Berg

[permalink] [raw]
Subject: Re: [PATCH 3/5] mac80211: replace Mk16 helper with le16_to_cpup


On Sat, 2008-03-08 at 01:23 -0800, Harvey Harrison wrote:
> Opencoded byte-swapping could just use the kernel helper.

> + p1k[2] = le16_to_cpup(leptr++);

I'm ok with this if architectures guarantee that these operations work
on unaligned values, but I don't think that is the case.

johannes


Attachments:
signature.asc (828.00 B)
This is a digitally signed message part