Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / crypto / sha256.c
index e8adf15..9d5ef67 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/module.h>
 #include <linux/mm.h>
 #include <linux/crypto.h>
+#include <linux/types.h>
 #include <asm/scatterlist.h>
 #include <asm/byteorder.h>
 
@@ -42,15 +43,10 @@ static inline u32 Maj(u32 x, u32 y, u32 z)
        return (x & y) | (z & (x | y));
 }
 
-static inline u32 RORu32(u32 x, u32 y)
-{
-       return (x >> y) | (x << (32 - y));
-}
-
-#define e0(x)       (RORu32(x, 2) ^ RORu32(x,13) ^ RORu32(x,22))
-#define e1(x)       (RORu32(x, 6) ^ RORu32(x,11) ^ RORu32(x,25))
-#define s0(x)       (RORu32(x, 7) ^ RORu32(x,18) ^ (x >> 3))
-#define s1(x)       (RORu32(x,17) ^ RORu32(x,19) ^ (x >> 10))
+#define e0(x)       (ror32(x, 2) ^ ror32(x,13) ^ ror32(x,22))
+#define e1(x)       (ror32(x, 6) ^ ror32(x,11) ^ ror32(x,25))
+#define s0(x)       (ror32(x, 7) ^ ror32(x,18) ^ (x >> 3))
+#define s1(x)       (ror32(x,17) ^ ror32(x,19) ^ (x >> 10))
 
 #define H0         0x6a09e667
 #define H1         0xbb67ae85
@@ -63,15 +59,7 @@ static inline u32 RORu32(u32 x, u32 y)
 
 static inline void LOAD_OP(int I, u32 *W, const u8 *input)
 {
-       u32 t1 = input[(4 * I)] & 0xff;
-
-       t1 <<= 8;
-       t1 |= input[(4 * I) + 1] & 0xff;
-       t1 <<= 8;
-       t1 |= input[(4 * I) + 2] & 0xff;
-       t1 <<= 8;
-       t1 |= input[(4 * I) + 3] & 0xff;
-       W[I] = t1;
+       W[I] = __be32_to_cpu( ((__be32*)(input))[I] );
 }
 
 static inline void BLEND_OP(int I, u32 *W)
@@ -292,22 +280,15 @@ static void sha256_update(void *ctx, const u8 *data, unsigned int len)
 static void sha256_final(void* ctx, u8 *out)
 {
        struct sha256_ctx *sctx = ctx;
-       u8 bits[8];
-       unsigned int index, pad_len, t;
-       int i, j;
+       __be32 *dst = (__be32 *)out;
+       __be32 bits[2];
+       unsigned int index, pad_len;
+       int i;
        static const u8 padding[64] = { 0x80, };
 
        /* Save number of bits */
-       t = sctx->count[0];
-       bits[7] = t; t >>= 8;
-       bits[6] = t; t >>= 8;
-       bits[5] = t; t >>= 8;
-       bits[4] = t;
-       t = sctx->count[1];
-       bits[3] = t; t >>= 8;
-       bits[2] = t; t >>= 8;
-       bits[1] = t; t >>= 8;
-       bits[0] = t;
+       bits[1] = cpu_to_be32(sctx->count[0]);
+       bits[0] = cpu_to_be32(sctx->count[1]);
 
        /* Pad out to 56 mod 64. */
        index = (sctx->count[0] >> 3) & 0x3f;
@@ -315,16 +296,11 @@ static void sha256_final(void* ctx, u8 *out)
        sha256_update(sctx, padding, pad_len);
 
        /* Append length (before padding) */
-       sha256_update(sctx, bits, 8);
+       sha256_update(sctx, (const u8 *)bits, sizeof(bits));
 
        /* Store state in digest */
-       for (i = j = 0; i < 8; i++, j += 4) {
-               t = sctx->state[i];
-               out[j+3] = t; t >>= 8;
-               out[j+2] = t; t >>= 8;
-               out[j+1] = t; t >>= 8;
-               out[j  ] = t;
-       }
+       for (i = 0; i < 8; i++)
+               dst[i] = cpu_to_be32(sctx->state[i]);
 
        /* Zeroize sensitive information. */
        memset(sctx, 0, sizeof(*sctx));