argp: Don't rely on undefined behaviour of _tolower().
[glibc.git] / crypt / sha256-block.c
bloba44fe01d5b95d02930d4c7b1f68442056ea1ea8f
1 #include <stdint.h>
3 /* Process LEN bytes of BUFFER, accumulating context into CTX.
4 It is assumed that LEN % 64 == 0. */
5 void
6 __sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
8 const uint32_t *words = buffer;
9 size_t nwords = len / sizeof (uint32_t);
10 uint32_t a = ctx->H[0];
11 uint32_t b = ctx->H[1];
12 uint32_t c = ctx->H[2];
13 uint32_t d = ctx->H[3];
14 uint32_t e = ctx->H[4];
15 uint32_t f = ctx->H[5];
16 uint32_t g = ctx->H[6];
17 uint32_t h = ctx->H[7];
19 /* First increment the byte count. FIPS 180-2 specifies the possible
20 length of the file up to 2^64 bits. Here we only compute the
21 number of bytes. */
22 ctx->total64 += len;
24 /* Process all bytes in the buffer with 64 bytes in each round of
25 the loop. */
26 while (nwords > 0)
28 uint32_t W[64];
29 uint32_t a_save = a;
30 uint32_t b_save = b;
31 uint32_t c_save = c;
32 uint32_t d_save = d;
33 uint32_t e_save = e;
34 uint32_t f_save = f;
35 uint32_t g_save = g;
36 uint32_t h_save = h;
38 /* Operators defined in FIPS 180-2:4.1.2. */
39 #define Ch(x, y, z) ((x & y) ^ (~x & z))
40 #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
41 #define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
42 #define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
43 #define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
44 #define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
46 /* It is unfortunate that C does not provide an operator for
47 cyclic rotation. Hope the C compiler is smart enough. */
48 #define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
50 /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
51 for (unsigned int t = 0; t < 16; ++t)
53 W[t] = SWAP (*words);
54 ++words;
56 for (unsigned int t = 16; t < 64; ++t)
57 W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
59 /* The actual computation according to FIPS 180-2:6.2.2 step 3. */
60 for (unsigned int t = 0; t < 64; ++t)
62 uint32_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
63 uint32_t T2 = S0 (a) + Maj (a, b, c);
64 h = g;
65 g = f;
66 f = e;
67 e = d + T1;
68 d = c;
69 c = b;
70 b = a;
71 a = T1 + T2;
74 /* Add the starting values of the context according to FIPS 180-2:6.2.2
75 step 4. */
76 a += a_save;
77 b += b_save;
78 c += c_save;
79 d += d_save;
80 e += e_save;
81 f += f_save;
82 g += g_save;
83 h += h_save;
85 /* Prepare for the next round. */
86 nwords -= 16;
89 /* Put checksum in context given as argument. */
90 ctx->H[0] = a;
91 ctx->H[1] = b;
92 ctx->H[2] = c;
93 ctx->H[3] = d;
94 ctx->H[4] = e;
95 ctx->H[5] = f;
96 ctx->H[6] = g;
97 ctx->H[7] = h;