2012-12-19 Steve Ellcey <sellcey@mips.com>
[glibc.git] / crypt / sha512-block.c
blobe7c5cfd7f5cef48024e0ccb6db6eff8dbcf7ef98
1 /* Process LEN bytes of BUFFER, accumulating context into CTX.
2 It is assumed that LEN % 128 == 0. */
3 void
4 sha512_process_block (const void *buffer, size_t len, struct sha512_ctx *ctx)
6 const uint64_t *words = buffer;
7 size_t nwords = len / sizeof (uint64_t);
8 uint64_t a = ctx->H[0];
9 uint64_t b = ctx->H[1];
10 uint64_t c = ctx->H[2];
11 uint64_t d = ctx->H[3];
12 uint64_t e = ctx->H[4];
13 uint64_t f = ctx->H[5];
14 uint64_t g = ctx->H[6];
15 uint64_t h = ctx->H[7];
17 /* First increment the byte count. FIPS 180-2 specifies the possible
18 length of the file up to 2^128 bits. Here we only compute the
19 number of bytes. Do a double word increment. */
20 #ifdef USE_TOTAL128
21 ctx->total128 += len;
22 #else
23 uint64_t lolen = len;
24 ctx->total[TOTAL128_low] += lolen;
25 ctx->total[TOTAL128_high] += ((len >> 31 >> 31 >> 2)
26 + (ctx->total[TOTAL128_low] < lolen));
27 #endif
29 /* Process all bytes in the buffer with 128 bytes in each round of
30 the loop. */
31 while (nwords > 0)
33 uint64_t W[80];
34 uint64_t a_save = a;
35 uint64_t b_save = b;
36 uint64_t c_save = c;
37 uint64_t d_save = d;
38 uint64_t e_save = e;
39 uint64_t f_save = f;
40 uint64_t g_save = g;
41 uint64_t h_save = h;
43 /* Operators defined in FIPS 180-2:4.1.2. */
44 #define Ch(x, y, z) ((x & y) ^ (~x & z))
45 #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
46 #define S0(x) (CYCLIC (x, 28) ^ CYCLIC (x, 34) ^ CYCLIC (x, 39))
47 #define S1(x) (CYCLIC (x, 14) ^ CYCLIC (x, 18) ^ CYCLIC (x, 41))
48 #define R0(x) (CYCLIC (x, 1) ^ CYCLIC (x, 8) ^ (x >> 7))
49 #define R1(x) (CYCLIC (x, 19) ^ CYCLIC (x, 61) ^ (x >> 6))
51 /* It is unfortunate that C does not provide an operator for
52 cyclic rotation. Hope the C compiler is smart enough. */
53 #define CYCLIC(w, s) ((w >> s) | (w << (64 - s)))
55 /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */
56 for (unsigned int t = 0; t < 16; ++t)
58 W[t] = SWAP (*words);
59 ++words;
61 for (unsigned int t = 16; t < 80; ++t)
62 W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
64 /* The actual computation according to FIPS 180-2:6.3.2 step 3. */
65 for (unsigned int t = 0; t < 80; ++t)
67 uint64_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
68 uint64_t T2 = S0 (a) + Maj (a, b, c);
69 h = g;
70 g = f;
71 f = e;
72 e = d + T1;
73 d = c;
74 c = b;
75 b = a;
76 a = T1 + T2;
79 /* Add the starting values of the context according to FIPS 180-2:6.3.2
80 step 4. */
81 a += a_save;
82 b += b_save;
83 c += c_save;
84 d += d_save;
85 e += e_save;
86 f += f_save;
87 g += g_save;
88 h += h_save;
90 /* Prepare for the next round. */
91 nwords -= 16;
94 /* Put checksum in context given as argument. */
95 ctx->H[0] = a;
96 ctx->H[1] = b;
97 ctx->H[2] = c;
98 ctx->H[3] = d;
99 ctx->H[4] = e;
100 ctx->H[5] = f;
101 ctx->H[6] = g;
102 ctx->H[7] = h;