base/asm-common.h (x86), and knock-on: Add macros for full-size regs.
[catacomb] / symm / sha256.c
index cecab25..7f91ff7 100644 (file)
 void sha256_compress(sha256_ctx *ctx, const void *sbuf)
 {
   uint32 a, b, c, d, e, f, g, h;
-  uint32 buf[64];
+  uint32 m[16];
+  const octet *p;
+  int i;
 
-  /* --- Fetch the chaining variables --- */
-
-  a = ctx->a;
-  b = ctx->b;
-  c = ctx->c;
-  d = ctx->d;
-  e = ctx->e;
-  f = ctx->f;
-  g = ctx->g;
-  h = ctx->h;
+  a  = ctx->a; b  = ctx->b; c  = ctx->c; d  = ctx->d;
+  e  = ctx->e; f  = ctx->f; g  = ctx->g; h  = ctx->h;
+  for (p = sbuf, i = 0; i < 16; i++, p += 4) m[i] = LOAD32(p);
 
   /* --- Definitions for round functions --- */
 
@@ -73,100 +68,85 @@ void sha256_compress(sha256_ctx *ctx, const void *sbuf)
 #define s1(x) (ROR32((x), 17) ^ ROR32((x), 19) ^ LSR32((x), 10))
 
 #define T(a, b, c, d, e, f, g, h, i, k) do {                           \
-  uint32 t1 = h + S1(e) + CH(e, f, g) + k + buf[i];                    \
+  uint32 t1 = h + S1(e) + CH(e, f, g) + k + m[i];                      \
   uint32 t2 = S0(a) + MAJ(a, b, c);                                    \
   d += t1; h = t1 + t2;                                                        \
 } while (0)
 
-  /* --- Fetch and expand the buffer contents --- */
-
-  {
-    int i;
-    const octet *p;
-
-    for (i = 0, p = sbuf; i < 16; i++, p += 4)
-      buf[i] = LOAD32(p);
-    for (i = 16; i < 64; i++)
-      buf[i] = s1(buf[i - 2]) + buf[i - 7] + s0(buf[i - 15]) + buf[i - 16];
-  }
+#define M(i, i2, i7, i15)                                              \
+  do { m[i] += s1(m[i2]) + m[i7] + s0(m[i15]); } while (0)
 
   /* --- The main compression function --- */
 
-  T(a, b, c, d, e, f, g, h,  0, 0x428a2f98);
-  T(h, a, b, c, d, e, f, g,  1, 0x71374491);
-  T(g, h, a, b, c, d, e, f,  2, 0xb5c0fbcf);
-  T(f, g, h, a, b, c, d, e,  3, 0xe9b5dba5);
-  T(e, f, g, h, a, b, c, d,  4, 0x3956c25b);
-  T(d, e, f, g, h, a, b, c,  5, 0x59f111f1);
-  T(c, d, e, f, g, h, a, b,  6, 0x923f82a4);
-  T(b, c, d, e, f, g, h, a,  7, 0xab1c5ed5);
-  T(a, b, c, d, e, f, g, h,  8, 0xd807aa98);
-  T(h, a, b, c, d, e, f, g,  9, 0x12835b01);
-  T(g, h, a, b, c, d, e, f, 10, 0x243185be);
-  T(f, g, h, a, b, c, d, e, 11, 0x550c7dc3);
-  T(e, f, g, h, a, b, c, d, 12, 0x72be5d74);
-  T(d, e, f, g, h, a, b, c, 13, 0x80deb1fe);
-  T(c, d, e, f, g, h, a, b, 14, 0x9bdc06a7);
-  T(b, c, d, e, f, g, h, a, 15, 0xc19bf174);
-  T(a, b, c, d, e, f, g, h, 16, 0xe49b69c1);
-  T(h, a, b, c, d, e, f, g, 17, 0xefbe4786);
-  T(g, h, a, b, c, d, e, f, 18, 0x0fc19dc6);
-  T(f, g, h, a, b, c, d, e, 19, 0x240ca1cc);
-  T(e, f, g, h, a, b, c, d, 20, 0x2de92c6f);
-  T(d, e, f, g, h, a, b, c, 21, 0x4a7484aa);
-  T(c, d, e, f, g, h, a, b, 22, 0x5cb0a9dc);
-  T(b, c, d, e, f, g, h, a, 23, 0x76f988da);
-  T(a, b, c, d, e, f, g, h, 24, 0x983e5152);
-  T(h, a, b, c, d, e, f, g, 25, 0xa831c66d);
-  T(g, h, a, b, c, d, e, f, 26, 0xb00327c8);
-  T(f, g, h, a, b, c, d, e, 27, 0xbf597fc7);
-  T(e, f, g, h, a, b, c, d, 28, 0xc6e00bf3);
-  T(d, e, f, g, h, a, b, c, 29, 0xd5a79147);
-  T(c, d, e, f, g, h, a, b, 30, 0x06ca6351);
-  T(b, c, d, e, f, g, h, a, 31, 0x14292967);
-  T(a, b, c, d, e, f, g, h, 32, 0x27b70a85);
-  T(h, a, b, c, d, e, f, g, 33, 0x2e1b2138);
-  T(g, h, a, b, c, d, e, f, 34, 0x4d2c6dfc);
-  T(f, g, h, a, b, c, d, e, 35, 0x53380d13);
-  T(e, f, g, h, a, b, c, d, 36, 0x650a7354);
-  T(d, e, f, g, h, a, b, c, 37, 0x766a0abb);
-  T(c, d, e, f, g, h, a, b, 38, 0x81c2c92e);
-  T(b, c, d, e, f, g, h, a, 39, 0x92722c85);
-  T(a, b, c, d, e, f, g, h, 40, 0xa2bfe8a1);
-  T(h, a, b, c, d, e, f, g, 41, 0xa81a664b);
-  T(g, h, a, b, c, d, e, f, 42, 0xc24b8b70);
-  T(f, g, h, a, b, c, d, e, 43, 0xc76c51a3);
-  T(e, f, g, h, a, b, c, d, 44, 0xd192e819);
-  T(d, e, f, g, h, a, b, c, 45, 0xd6990624);
-  T(c, d, e, f, g, h, a, b, 46, 0xf40e3585);
-  T(b, c, d, e, f, g, h, a, 47, 0x106aa070);
-  T(a, b, c, d, e, f, g, h, 48, 0x19a4c116);
-  T(h, a, b, c, d, e, f, g, 49, 0x1e376c08);
-  T(g, h, a, b, c, d, e, f, 50, 0x2748774c);
-  T(f, g, h, a, b, c, d, e, 51, 0x34b0bcb5);
-  T(e, f, g, h, a, b, c, d, 52, 0x391c0cb3);
-  T(d, e, f, g, h, a, b, c, 53, 0x4ed8aa4a);
-  T(c, d, e, f, g, h, a, b, 54, 0x5b9cca4f);
-  T(b, c, d, e, f, g, h, a, 55, 0x682e6ff3);
-  T(a, b, c, d, e, f, g, h, 56, 0x748f82ee);
-  T(h, a, b, c, d, e, f, g, 57, 0x78a5636f);
-  T(g, h, a, b, c, d, e, f, 58, 0x84c87814);
-  T(f, g, h, a, b, c, d, e, 59, 0x8cc70208);
-  T(e, f, g, h, a, b, c, d, 60, 0x90befffa);
-  T(d, e, f, g, h, a, b, c, 61, 0xa4506ceb);
-  T(c, d, e, f, g, h, a, b, 62, 0xbef9a3f7);
-  T(b, c, d, e, f, g, h, a, 63, 0xc67178f2);
+  T(a, b, c, d, e, f, g, h,  0, 0x428a2f98);   M( 0, 14,  9,  1);
+  T(h, a, b, c, d, e, f, g,  1, 0x71374491);   M( 1, 15, 10,  2);
+  T(g, h, a, b, c, d, e, f,  2, 0xb5c0fbcf);   M( 2,  0, 11,  3);
+  T(f, g, h, a, b, c, d, e,  3, 0xe9b5dba5);   M( 3,  1, 12,  4);
+  T(e, f, g, h, a, b, c, d,  4, 0x3956c25b);   M( 4,  2, 13,  5);
+  T(d, e, f, g, h, a, b, c,  5, 0x59f111f1);   M( 5,  3, 14,  6);
+  T(c, d, e, f, g, h, a, b,  6, 0x923f82a4);   M( 6,  4, 15,  7);
+  T(b, c, d, e, f, g, h, a,  7, 0xab1c5ed5);   M( 7,  5,  0,  8);
+  T(a, b, c, d, e, f, g, h,  8, 0xd807aa98);   M( 8,  6,  1,  9);
+  T(h, a, b, c, d, e, f, g,  9, 0x12835b01);   M( 9,  7,  2, 10);
+  T(g, h, a, b, c, d, e, f, 10, 0x243185be);   M(10,  8,  3, 11);
+  T(f, g, h, a, b, c, d, e, 11, 0x550c7dc3);   M(11,  9,  4, 12);
+  T(e, f, g, h, a, b, c, d, 12, 0x72be5d74);   M(12, 10,  5, 13);
+  T(d, e, f, g, h, a, b, c, 13, 0x80deb1fe);   M(13, 11,  6, 14);
+  T(c, d, e, f, g, h, a, b, 14, 0x9bdc06a7);   M(14, 12,  7, 15);
+  T(b, c, d, e, f, g, h, a, 15, 0xc19bf174);   M(15, 13,  8,  0);
+  T(a, b, c, d, e, f, g, h,  0, 0xe49b69c1);   M( 0, 14,  9,  1);
+  T(h, a, b, c, d, e, f, g,  1, 0xefbe4786);   M( 1, 15, 10,  2);
+  T(g, h, a, b, c, d, e, f,  2, 0x0fc19dc6);   M( 2,  0, 11,  3);
+  T(f, g, h, a, b, c, d, e,  3, 0x240ca1cc);   M( 3,  1, 12,  4);
+  T(e, f, g, h, a, b, c, d,  4, 0x2de92c6f);   M( 4,  2, 13,  5);
+  T(d, e, f, g, h, a, b, c,  5, 0x4a7484aa);   M( 5,  3, 14,  6);
+  T(c, d, e, f, g, h, a, b,  6, 0x5cb0a9dc);   M( 6,  4, 15,  7);
+  T(b, c, d, e, f, g, h, a,  7, 0x76f988da);   M( 7,  5,  0,  8);
+  T(a, b, c, d, e, f, g, h,  8, 0x983e5152);   M( 8,  6,  1,  9);
+  T(h, a, b, c, d, e, f, g,  9, 0xa831c66d);   M( 9,  7,  2, 10);
+  T(g, h, a, b, c, d, e, f, 10, 0xb00327c8);   M(10,  8,  3, 11);
+  T(f, g, h, a, b, c, d, e, 11, 0xbf597fc7);   M(11,  9,  4, 12);
+  T(e, f, g, h, a, b, c, d, 12, 0xc6e00bf3);   M(12, 10,  5, 13);
+  T(d, e, f, g, h, a, b, c, 13, 0xd5a79147);   M(13, 11,  6, 14);
+  T(c, d, e, f, g, h, a, b, 14, 0x06ca6351);   M(14, 12,  7, 15);
+  T(b, c, d, e, f, g, h, a, 15, 0x14292967);   M(15, 13,  8,  0);
+  T(a, b, c, d, e, f, g, h,  0, 0x27b70a85);   M( 0, 14,  9,  1);
+  T(h, a, b, c, d, e, f, g,  1, 0x2e1b2138);   M( 1, 15, 10,  2);
+  T(g, h, a, b, c, d, e, f,  2, 0x4d2c6dfc);   M( 2,  0, 11,  3);
+  T(f, g, h, a, b, c, d, e,  3, 0x53380d13);   M( 3,  1, 12,  4);
+  T(e, f, g, h, a, b, c, d,  4, 0x650a7354);   M( 4,  2, 13,  5);
+  T(d, e, f, g, h, a, b, c,  5, 0x766a0abb);   M( 5,  3, 14,  6);
+  T(c, d, e, f, g, h, a, b,  6, 0x81c2c92e);   M( 6,  4, 15,  7);
+  T(b, c, d, e, f, g, h, a,  7, 0x92722c85);   M( 7,  5,  0,  8);
+  T(a, b, c, d, e, f, g, h,  8, 0xa2bfe8a1);   M( 8,  6,  1,  9);
+  T(h, a, b, c, d, e, f, g,  9, 0xa81a664b);   M( 9,  7,  2, 10);
+  T(g, h, a, b, c, d, e, f, 10, 0xc24b8b70);   M(10,  8,  3, 11);
+  T(f, g, h, a, b, c, d, e, 11, 0xc76c51a3);   M(11,  9,  4, 12);
+  T(e, f, g, h, a, b, c, d, 12, 0xd192e819);   M(12, 10,  5, 13);
+  T(d, e, f, g, h, a, b, c, 13, 0xd6990624);   M(13, 11,  6, 14);
+  T(c, d, e, f, g, h, a, b, 14, 0xf40e3585);   M(14, 12,  7, 15);
+  T(b, c, d, e, f, g, h, a, 15, 0x106aa070);   M(15, 13,  8,  0);
+  T(a, b, c, d, e, f, g, h,  0, 0x19a4c116);
+  T(h, a, b, c, d, e, f, g,  1, 0x1e376c08);
+  T(g, h, a, b, c, d, e, f,  2, 0x2748774c);
+  T(f, g, h, a, b, c, d, e,  3, 0x34b0bcb5);
+  T(e, f, g, h, a, b, c, d,  4, 0x391c0cb3);
+  T(d, e, f, g, h, a, b, c,  5, 0x4ed8aa4a);
+  T(c, d, e, f, g, h, a, b,  6, 0x5b9cca4f);
+  T(b, c, d, e, f, g, h, a,  7, 0x682e6ff3);
+  T(a, b, c, d, e, f, g, h,  8, 0x748f82ee);
+  T(h, a, b, c, d, e, f, g,  9, 0x78a5636f);
+  T(g, h, a, b, c, d, e, f, 10, 0x84c87814);
+  T(f, g, h, a, b, c, d, e, 11, 0x8cc70208);
+  T(e, f, g, h, a, b, c, d, 12, 0x90befffa);
+  T(d, e, f, g, h, a, b, c, 13, 0xa4506ceb);
+  T(c, d, e, f, g, h, a, b, 14, 0xbef9a3f7);
+  T(b, c, d, e, f, g, h, a, 15, 0xc67178f2);
 
   /* --- Update the chaining variables --- */
 
-  ctx->a += a;
-  ctx->b += b;
-  ctx->c += c;
-  ctx->d += d;
-  ctx->e += e;
-  ctx->f += f;
-  ctx->g += g;
-  ctx->h += h;
+  ctx->a += a; ctx->b += b; ctx->c += c; ctx->d += d;
+  ctx->e += e; ctx->f += f; ctx->g += g; ctx->h += h;
 }
 
 /* --- @sha256_init@, @sha224_init@ --- *
@@ -232,7 +212,7 @@ void sha256_set(sha256_ctx *ctx, const void *buf, unsigned long count)
   ctx->h = LOAD32(p + 28);
   ctx->off = 0;
   ctx->nl = U32(count);
-  ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
+  ctx->nh = U32(((count & ~(unsigned long)MASK32) >> 16) >> 16);
 }
 
 /* --- @sha256_hash@, @sha224_hash@ --- *