Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / sha512-compress.c
blob4f06fdb3a5af099dd6948d23847b4a90c12b1ed4
1 /* sha512-compress.c
3 * The compression function of the sha512 hash function.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001, 2010 Niels Möller
9 *
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #ifndef SHA512_DEBUG
31 # define SHA512_DEBUG 0
32 #endif
34 #if SHA512_DEBUG
35 # include <stdio.h>
36 # define DEBUG(i) \
37 fprintf(stderr, "%2d: %8lx %8lx %8lx %8lx\n %8lx %8lx %8lx %8lx\n", \
38 i, A, B, C, D ,E, F, G, H)
39 #else
40 # define DEBUG(i)
41 #endif
43 #include <assert.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include "sha2.h"
49 #include "macros.h"
51 /* A block, treated as a sequence of 64-bit words. */
52 #define SHA512_DATA_LENGTH 16
54 /* The SHA512 functions. The Choice function is the same as the SHA1
55 function f1, and the majority function is the same as the SHA1 f3
56 function, and the same as for SHA256. */
58 #define Choice(x,y,z) ( (z) ^ ( (x) & ( (y) ^ (z) ) ) )
59 #define Majority(x,y,z) ( ((x) & (y)) ^ ((z) & ((x) ^ (y))) )
61 #define S0(x) (ROTL64(36,(x)) ^ ROTL64(30,(x)) ^ ROTL64(25,(x)))
62 #define S1(x) (ROTL64(50,(x)) ^ ROTL64(46,(x)) ^ ROTL64(23,(x)))
64 #define s0(x) (ROTL64(63,(x)) ^ ROTL64(56,(x)) ^ ((x) >> 7))
65 #define s1(x) (ROTL64(45,(x)) ^ ROTL64(3,(x)) ^ ((x) >> 6))
67 /* The initial expanding function. The hash function is defined over
68 an 64-word expanded input array W, where the first 16 are copies of
69 the input data, and the remaining 64 are defined by
71 W[ t ] = s1(W[t-2]) + W[t-7] + s0(W[i-15]) + W[i-16]
73 This implementation generates these values on the fly in a circular
74 buffer.
77 #define EXPAND(W,i) \
78 ( W[(i) & 15 ] += (s1(W[((i)-2) & 15]) + W[((i)-7) & 15] + s0(W[((i)-15) & 15])) )
80 /* The prototype SHA sub-round. The fundamental sub-round is:
82 T1 = h + S1(e) + Choice(e,f,g) + K[t] + W[t]
83 T2 = S0(a) + Majority(a,b,c)
84 a' = T1+T2
85 b' = a
86 c' = b
87 d' = c
88 e' = d + T1
89 f' = e
90 g' = f
91 h' = g
93 but this is implemented by unrolling the loop 8 times and renaming
94 the variables
95 ( h, a, b, c, d, e, f, g ) = ( a, b, c, d, e, f, g, h ) each
96 iteration. This code is then replicated 8, using the next 8 values
97 from the W[] array each time */
99 /* It's crucial that DATA is only used once, as that argument will
100 * have side effects. */
101 #define ROUND(a,b,c,d,e,f,g,h,k,data) do { \
102 h += S1(e) + Choice(e,f,g) + k + data; \
103 d += h; \
104 h += S0(a) + Majority(a,b,c); \
105 } while (0)
107 void
108 _nettle_sha512_compress(uint64_t *state, const uint8_t *input, const uint64_t *k)
110 uint64_t data[SHA512_DATA_LENGTH];
111 uint64_t A, B, C, D, E, F, G, H; /* Local vars */
112 unsigned i;
113 uint64_t *d;
115 for (i = 0; i < SHA512_DATA_LENGTH; i++, input += 8)
117 data[i] = READ_UINT64(input);
120 /* Set up first buffer and local data buffer */
121 A = state[0];
122 B = state[1];
123 C = state[2];
124 D = state[3];
125 E = state[4];
126 F = state[5];
127 G = state[6];
128 H = state[7];
130 /* Heavy mangling */
131 /* First 16 subrounds that act on the original data */
133 DEBUG(-1);
134 for (i = 0, d = data; i<16; i+=8, k += 8, d+= 8)
136 ROUND(A, B, C, D, E, F, G, H, k[0], d[0]); DEBUG(i);
137 ROUND(H, A, B, C, D, E, F, G, k[1], d[1]); DEBUG(i+1);
138 ROUND(G, H, A, B, C, D, E, F, k[2], d[2]);
139 ROUND(F, G, H, A, B, C, D, E, k[3], d[3]);
140 ROUND(E, F, G, H, A, B, C, D, k[4], d[4]);
141 ROUND(D, E, F, G, H, A, B, C, k[5], d[5]);
142 ROUND(C, D, E, F, G, H, A, B, k[6], d[6]); DEBUG(i+6);
143 ROUND(B, C, D, E, F, G, H, A, k[7], d[7]); DEBUG(i+7);
146 for (; i<80; i += 16, k+= 16)
148 ROUND(A, B, C, D, E, F, G, H, k[ 0], EXPAND(data, 0)); DEBUG(i);
149 ROUND(H, A, B, C, D, E, F, G, k[ 1], EXPAND(data, 1)); DEBUG(i+1);
150 ROUND(G, H, A, B, C, D, E, F, k[ 2], EXPAND(data, 2)); DEBUG(i+2);
151 ROUND(F, G, H, A, B, C, D, E, k[ 3], EXPAND(data, 3));
152 ROUND(E, F, G, H, A, B, C, D, k[ 4], EXPAND(data, 4));
153 ROUND(D, E, F, G, H, A, B, C, k[ 5], EXPAND(data, 5));
154 ROUND(C, D, E, F, G, H, A, B, k[ 6], EXPAND(data, 6));
155 ROUND(B, C, D, E, F, G, H, A, k[ 7], EXPAND(data, 7));
156 ROUND(A, B, C, D, E, F, G, H, k[ 8], EXPAND(data, 8));
157 ROUND(H, A, B, C, D, E, F, G, k[ 9], EXPAND(data, 9));
158 ROUND(G, H, A, B, C, D, E, F, k[10], EXPAND(data, 10));
159 ROUND(F, G, H, A, B, C, D, E, k[11], EXPAND(data, 11));
160 ROUND(E, F, G, H, A, B, C, D, k[12], EXPAND(data, 12));
161 ROUND(D, E, F, G, H, A, B, C, k[13], EXPAND(data, 13));
162 ROUND(C, D, E, F, G, H, A, B, k[14], EXPAND(data, 14)); DEBUG(i+14);
163 ROUND(B, C, D, E, F, G, H, A, k[15], EXPAND(data, 15)); DEBUG(i+15);
166 /* Update state */
167 state[0] += A;
168 state[1] += B;
169 state[2] += C;
170 state[3] += D;
171 state[4] += E;
172 state[5] += F;
173 state[6] += G;
174 state[7] += H;
175 #if SHA512_DEBUG
176 fprintf(stderr, "99: %8lx %8lx %8lx %8lx\n %8lx %8lx %8lx %8lx\n",
177 state[0], state[1], state[2], state[3],
178 state[4], state[5], state[6], state[7]);
179 #endif