|
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 % 64 == 0. */
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
__sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
const uint32_t *words = buffer;
|
|
Packit |
6c4009 |
size_t nwords = len / sizeof (uint32_t);
|
|
Packit |
6c4009 |
uint32_t a = ctx->H[0];
|
|
Packit |
6c4009 |
uint32_t b = ctx->H[1];
|
|
Packit |
6c4009 |
uint32_t c = ctx->H[2];
|
|
Packit |
6c4009 |
uint32_t d = ctx->H[3];
|
|
Packit |
6c4009 |
uint32_t e = ctx->H[4];
|
|
Packit |
6c4009 |
uint32_t f = ctx->H[5];
|
|
Packit |
6c4009 |
uint32_t g = ctx->H[6];
|
|
Packit |
6c4009 |
uint32_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^64 bits. Here we only compute the
|
|
Packit |
6c4009 |
number of bytes. */
|
|
Packit |
6c4009 |
ctx->total64 += len;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Process all bytes in the buffer with 64 bytes in each round of
|
|
Packit |
6c4009 |
the loop. */
|
|
Packit |
6c4009 |
while (nwords > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
uint32_t W[64];
|
|
Packit |
6c4009 |
uint32_t a_save = a;
|
|
Packit |
6c4009 |
uint32_t b_save = b;
|
|
Packit |
6c4009 |
uint32_t c_save = c;
|
|
Packit |
6c4009 |
uint32_t d_save = d;
|
|
Packit |
6c4009 |
uint32_t e_save = e;
|
|
Packit |
6c4009 |
uint32_t f_save = f;
|
|
Packit |
6c4009 |
uint32_t g_save = g;
|
|
Packit |
6c4009 |
uint32_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, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
|
|
Packit |
6c4009 |
#define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
|
|
Packit |
6c4009 |
#define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
|
|
Packit |
6c4009 |
#define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
|
|
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 << (32 - s)))
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Compute the message schedule according to FIPS 180-2:6.2.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 < 64; ++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.2.2 step 3. */
|
|
Packit |
6c4009 |
for (unsigned int t = 0; t < 64; ++t)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
uint32_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
|
|
Packit |
6c4009 |
uint32_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.2.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 |
}
|