Blame crypt/sha512-block.c

Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
/* Process LEN bytes of BUFFER, accumulating context into CTX.
Packit 6c4009
   It is assumed that LEN % 128 == 0.  */
Packit 6c4009
void
Packit 6c4009
__sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
Packit 6c4009
{
Packit 6c4009
  const uint64_t *words = buffer;
Packit 6c4009
  size_t nwords = len / sizeof (uint64_t);
Packit 6c4009
  uint64_t a = ctx->H[0];
Packit 6c4009
  uint64_t b = ctx->H[1];
Packit 6c4009
  uint64_t c = ctx->H[2];
Packit 6c4009
  uint64_t d = ctx->H[3];
Packit 6c4009
  uint64_t e = ctx->H[4];
Packit 6c4009
  uint64_t f = ctx->H[5];
Packit 6c4009
  uint64_t g = ctx->H[6];
Packit 6c4009
  uint64_t h = ctx->H[7];
Packit 6c4009
Packit 6c4009
  /* First increment the byte count.  FIPS 180-2 specifies the possible
Packit 6c4009
     length of the file up to 2^128 bits.  Here we only compute the
Packit 6c4009
     number of bytes.  Do a double word increment.  */
Packit 6c4009
#ifdef USE_TOTAL128
Packit 6c4009
  ctx->total128 += len;
Packit 6c4009
#else
Packit 6c4009
  uint64_t lolen = len;
Packit 6c4009
  ctx->total[TOTAL128_low] += lolen;
Packit 6c4009
  ctx->total[TOTAL128_high] += ((len >> 31 >> 31 >> 2)
Packit 6c4009
				+ (ctx->total[TOTAL128_low] < lolen));
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  /* Process all bytes in the buffer with 128 bytes in each round of
Packit 6c4009
     the loop.  */
Packit 6c4009
  while (nwords > 0)
Packit 6c4009
    {
Packit 6c4009
      uint64_t W[80];
Packit 6c4009
      uint64_t a_save = a;
Packit 6c4009
      uint64_t b_save = b;
Packit 6c4009
      uint64_t c_save = c;
Packit 6c4009
      uint64_t d_save = d;
Packit 6c4009
      uint64_t e_save = e;
Packit 6c4009
      uint64_t f_save = f;
Packit 6c4009
      uint64_t g_save = g;
Packit 6c4009
      uint64_t h_save = h;
Packit 6c4009
Packit 6c4009
      /* Operators defined in FIPS 180-2:4.1.2.  */
Packit 6c4009
#define Ch(x, y, z) ((x & y) ^ (~x & z))
Packit 6c4009
#define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
Packit 6c4009
#define S0(x) (CYCLIC (x, 28) ^ CYCLIC (x, 34) ^ CYCLIC (x, 39))
Packit 6c4009
#define S1(x) (CYCLIC (x, 14) ^ CYCLIC (x, 18) ^ CYCLIC (x, 41))
Packit 6c4009
#define R0(x) (CYCLIC (x, 1) ^ CYCLIC (x, 8) ^ (x >> 7))
Packit 6c4009
#define R1(x) (CYCLIC (x, 19) ^ CYCLIC (x, 61) ^ (x >> 6))
Packit 6c4009
Packit 6c4009
      /* It is unfortunate that C does not provide an operator for
Packit 6c4009
	 cyclic rotation.  Hope the C compiler is smart enough.  */
Packit 6c4009
#define CYCLIC(w, s) ((w >> s) | (w << (64 - s)))
Packit 6c4009
Packit 6c4009
      /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2.  */
Packit 6c4009
      for (unsigned int t = 0; t < 16; ++t)
Packit 6c4009
	{
Packit 6c4009
	  W[t] = SWAP (*words);
Packit 6c4009
	  ++words;
Packit 6c4009
	}
Packit 6c4009
      for (unsigned int t = 16; t < 80; ++t)
Packit 6c4009
	W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
Packit 6c4009
Packit 6c4009
      /* The actual computation according to FIPS 180-2:6.3.2 step 3.  */
Packit 6c4009
      for (unsigned int t = 0; t < 80; ++t)
Packit 6c4009
	{
Packit 6c4009
	  uint64_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
Packit 6c4009
	  uint64_t T2 = S0 (a) + Maj (a, b, c);
Packit 6c4009
	  h = g;
Packit 6c4009
	  g = f;
Packit 6c4009
	  f = e;
Packit 6c4009
	  e = d + T1;
Packit 6c4009
	  d = c;
Packit 6c4009
	  c = b;
Packit 6c4009
	  b = a;
Packit 6c4009
	  a = T1 + T2;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Add the starting values of the context according to FIPS 180-2:6.3.2
Packit 6c4009
	 step 4.  */
Packit 6c4009
      a += a_save;
Packit 6c4009
      b += b_save;
Packit 6c4009
      c += c_save;
Packit 6c4009
      d += d_save;
Packit 6c4009
      e += e_save;
Packit 6c4009
      f += f_save;
Packit 6c4009
      g += g_save;
Packit 6c4009
      h += h_save;
Packit 6c4009
Packit 6c4009
      /* Prepare for the next round.  */
Packit 6c4009
      nwords -= 16;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Put checksum in context given as argument.  */
Packit 6c4009
  ctx->H[0] = a;
Packit 6c4009
  ctx->H[1] = b;
Packit 6c4009
  ctx->H[2] = c;
Packit 6c4009
  ctx->H[3] = d;
Packit 6c4009
  ctx->H[4] = e;
Packit 6c4009
  ctx->H[5] = f;
Packit 6c4009
  ctx->H[6] = g;
Packit 6c4009
  ctx->H[7] = h;
Packit 6c4009
}