Check supported DF_1_XXX bits
[glibc.git] / crypt / sha256-block.c
bloba163e25865cf06f4a3fed285a7e5f29533eaf522
1 /* Process LEN bytes of BUFFER, accumulating context into CTX.
2 It is assumed that LEN % 64 == 0. */
3 void
4 sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
6 const uint32_t *words = buffer;
7 size_t nwords = len / sizeof (uint32_t);
8 uint32_t a = ctx->H[0];
9 uint32_t b = ctx->H[1];
10 uint32_t c = ctx->H[2];
11 uint32_t d = ctx->H[3];
12 uint32_t e = ctx->H[4];
13 uint32_t f = ctx->H[5];
14 uint32_t g = ctx->H[6];
15 uint32_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^64 bits. Here we only compute the
19 number of bytes. */
20 ctx->total64 += len;
22 /* Process all bytes in the buffer with 64 bytes in each round of
23 the loop. */
24 while (nwords > 0)
26 uint32_t W[64];
27 uint32_t a_save = a;
28 uint32_t b_save = b;
29 uint32_t c_save = c;
30 uint32_t d_save = d;
31 uint32_t e_save = e;
32 uint32_t f_save = f;
33 uint32_t g_save = g;
34 uint32_t h_save = h;
36 /* Operators defined in FIPS 180-2:4.1.2. */
37 #define Ch(x, y, z) ((x & y) ^ (~x & z))
38 #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
39 #define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
40 #define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
41 #define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
42 #define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
44 /* It is unfortunate that C does not provide an operator for
45 cyclic rotation. Hope the C compiler is smart enough. */
46 #define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
48 /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */
49 for (unsigned int t = 0; t < 16; ++t)
51 W[t] = SWAP (*words);
52 ++words;
54 for (unsigned int t = 16; t < 64; ++t)
55 W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
57 /* The actual computation according to FIPS 180-2:6.2.2 step 3. */
58 for (unsigned int t = 0; t < 64; ++t)
60 uint32_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
61 uint32_t T2 = S0 (a) + Maj (a, b, c);
62 h = g;
63 g = f;
64 f = e;
65 e = d + T1;
66 d = c;
67 c = b;
68 b = a;
69 a = T1 + T2;
72 /* Add the starting values of the context according to FIPS 180-2:6.2.2
73 step 4. */
74 a += a_save;
75 b += b_save;
76 c += c_save;
77 d += d_save;
78 e += e_save;
79 f += f_save;
80 g += g_save;
81 h += h_save;
83 /* Prepare for the next round. */
84 nwords -= 16;
87 /* Put checksum in context given as argument. */
88 ctx->H[0] = a;
89 ctx->H[1] = b;
90 ctx->H[2] = c;
91 ctx->H[3] = d;
92 ctx->H[4] = e;
93 ctx->H[5] = f;
94 ctx->H[6] = g;
95 ctx->H[7] = h;