3 * The compression function of the sha512 hash function.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001, 2010 Niels Möller
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,
31 # define SHA512_DEBUG 0
37 fprintf(stderr, "%2d: %8lx %8lx %8lx %8lx\n %8lx %8lx %8lx %8lx\n", \
38 i, A, B, C, D ,E, F, G, 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
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)
93 but this is implemented by unrolling the loop 8 times and renaming
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; \
104 h += S0(a) + Majority(a,b,c); \
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 */
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 */
131 /* First 16 subrounds that act on the original data */
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);
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]);