1 /*
2 * SHA-{384,512}
3 * (C) 1999-2011 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/sha2_64.h>
9 #include <botan/loadstor.h>
10 #include <botan/rotate.h>
11
12 namespace Botan {
13
14 namespace {
15
16 namespace SHA2_64 {
17
18 /*
19 * SHA-{384,512} Rho Function
20 */
rho(u64bit X,u32bit rot1,u32bit rot2,u32bit rot3)21 inline u64bit rho(u64bit X, u32bit rot1, u32bit rot2, u32bit rot3)
22 {
23 return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^
24 rotate_right(X, rot3));
25 }
26
27 /*
28 * SHA-{384,512} Sigma Function
29 */
sigma(u64bit X,u32bit rot1,u32bit rot2,u32bit shift)30 inline u64bit sigma(u64bit X, u32bit rot1, u32bit rot2, u32bit shift)
31 {
32 return (rotate_right(X, rot1) ^ rotate_right(X, rot2) ^ (X >> shift));
33 }
34
35 /*
36 * SHA-512 F1 Function
37 *
38 * Use a macro as many compilers won't inline a function this big,
39 * even though it is much faster if inlined.
40 */
41 #define SHA2_64_F(A, B, C, D, E, F, G, H, M1, M2, M3, M4, magic) \
42 do { \
43 H += magic + rho(E, 14, 18, 41) + ((E & F) ^ (~E & G)) + M1; \
44 D += H; \
45 H += rho(A, 28, 34, 39) + ((A & B) | ((A | B) & C)); \
46 M1 += sigma(M2, 19, 61, 6) + M3 + sigma(M4, 1, 8, 7); \
47 } while(0);
48
49 /*
50 * SHA-{384,512} Compression Function
51 */
compress(MemoryRegion<u64bit> & digest,const byte input[],size_t blocks)52 void compress(MemoryRegion<u64bit>& digest,
53 const byte input[], size_t blocks)
54 {
55 u64bit A = digest[0], B = digest[1], C = digest[2],
56 D = digest[3], E = digest[4], F = digest[5],
57 G = digest[6], H = digest[7];
58
59 for(size_t i = 0; i != blocks; ++i)
60 {
61 u64bit W00 = load_be<u64bit>(input, 0);
62 u64bit W01 = load_be<u64bit>(input, 1);
63 u64bit W02 = load_be<u64bit>(input, 2);
64 u64bit W03 = load_be<u64bit>(input, 3);
65 u64bit W04 = load_be<u64bit>(input, 4);
66 u64bit W05 = load_be<u64bit>(input, 5);
67 u64bit W06 = load_be<u64bit>(input, 6);
68 u64bit W07 = load_be<u64bit>(input, 7);
69 u64bit W08 = load_be<u64bit>(input, 8);
70 u64bit W09 = load_be<u64bit>(input, 9);
71 u64bit W10 = load_be<u64bit>(input, 10);
72 u64bit W11 = load_be<u64bit>(input, 11);
73 u64bit W12 = load_be<u64bit>(input, 12);
74 u64bit W13 = load_be<u64bit>(input, 13);
75 u64bit W14 = load_be<u64bit>(input, 14);
76 u64bit W15 = load_be<u64bit>(input, 15);
77
78 SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98D728AE22);
79 SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x7137449123EF65CD);
80 SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCFEC4D3B2F);
81 SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA58189DBBC);
82 SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25BF348B538);
83 SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1B605D019);
84 SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4AF194F9B);
85 SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5DA6D8118);
86 SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98A3030242);
87 SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B0145706FBE);
88 SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE4EE4B28C);
89 SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3D5FFB4E2);
90 SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74F27B896F);
91 SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE3B1696B1);
92 SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A725C71235);
93 SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174CF692694);
94 SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C19EF14AD2);
95 SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786384F25E3);
96 SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC68B8CD5B5);
97 SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC77AC9C65);
98 SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F592B0275);
99 SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA6EA6E483);
100 SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DCBD41FBD4);
101 SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA831153B5);
102 SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152EE66DFAB);
103 SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D2DB43210);
104 SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C898FB213F);
105 SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7BEEF0EE4);
106 SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF33DA88FC2);
107 SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147930AA725);
108 SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351E003826F);
109 SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x142929670A0E6E70);
110 SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A8546D22FFC);
111 SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B21385C26C926);
112 SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC5AC42AED);
113 SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D139D95B3DF);
114 SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A73548BAF63DE);
115 SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB3C77B2A8);
116 SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E47EDAEE6);
117 SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C851482353B);
118 SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A14CF10364);
119 SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664BBC423001);
120 SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70D0F89791);
121 SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A30654BE30);
122 SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819D6EF5218);
123 SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD69906245565A910);
124 SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E35855771202A);
125 SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA07032BBD1B8);
126 SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116B8D2D0C8);
127 SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C085141AB53);
128 SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774CDF8EEB99);
129 SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5E19B48A8);
130 SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3C5C95A63);
131 SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4AE3418ACB);
132 SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F7763E373);
133 SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3D6B2B8A3);
134 SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE5DEFB2FC);
135 SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F43172F60);
136 SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814A1F0AB72);
137 SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC702081A6439EC);
138 SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA23631E28);
139 SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEBDE82BDE9);
140 SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7B2C67915);
141 SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2E372532B);
142 SHA2_64_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xCA273ECEEA26619C);
143 SHA2_64_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xD186B8C721C0C207);
144 SHA2_64_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xEADA7DD6CDE0EB1E);
145 SHA2_64_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xF57D4F7FEE6ED178);
146 SHA2_64_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x06F067AA72176FBA);
147 SHA2_64_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x0A637DC5A2C898A6);
148 SHA2_64_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x113F9804BEF90DAE);
149 SHA2_64_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x1B710B35131C471B);
150 SHA2_64_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x28DB77F523047D84);
151 SHA2_64_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x32CAAB7B40C72493);
152 SHA2_64_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x3C9EBE0A15C9BEBC);
153 SHA2_64_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x431D67C49C100D4C);
154 SHA2_64_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x4CC5D4BECB3E42B6);
155 SHA2_64_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x597F299CFC657E2A);
156 SHA2_64_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x5FCB6FAB3AD6FAEC);
157 SHA2_64_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x6C44198C4A475817);
158
159 A = (digest[0] += A);
160 B = (digest[1] += B);
161 C = (digest[2] += C);
162 D = (digest[3] += D);
163 E = (digest[4] += E);
164 F = (digest[5] += F);
165 G = (digest[6] += G);
166 H = (digest[7] += H);
167
168 input += 128;
169 }
170 }
171
172 }
173
174 }
175
176 /*
177 * SHA-384 compression function
178 */
compress_n(const byte input[],size_t blocks)179 void SHA_384::compress_n(const byte input[], size_t blocks)
180 {
181 SHA2_64::compress(digest, input, blocks);
182 }
183
184 /*
185 * Copy out the digest
186 */
copy_out(byte output[])187 void SHA_384::copy_out(byte output[])
188 {
189 for(size_t i = 0; i != output_length(); i += 8)
190 store_be(digest[i/8], output + i);
191 }
192
193 /*
194 * Clear memory of sensitive data
195 */
clear()196 void SHA_384::clear()
197 {
198 MDx_HashFunction::clear();
199 digest[0] = 0xCBBB9D5DC1059ED8;
200 digest[1] = 0x629A292A367CD507;
201 digest[2] = 0x9159015A3070DD17;
202 digest[3] = 0x152FECD8F70E5939;
203 digest[4] = 0x67332667FFC00B31;
204 digest[5] = 0x8EB44A8768581511;
205 digest[6] = 0xDB0C2E0D64F98FA7;
206 digest[7] = 0x47B5481DBEFA4FA4;
207 }
208
209 /*
210 * SHA-512 compression function
211 */
compress_n(const byte input[],size_t blocks)212 void SHA_512::compress_n(const byte input[], size_t blocks)
213 {
214 SHA2_64::compress(digest, input, blocks);
215 }
216
217 /*
218 * Copy out the digest
219 */
copy_out(byte output[])220 void SHA_512::copy_out(byte output[])
221 {
222 for(size_t i = 0; i != output_length(); i += 8)
223 store_be(digest[i/8], output + i);
224 }
225
226 /*
227 * Clear memory of sensitive data
228 */
clear()229 void SHA_512::clear()
230 {
231 MDx_HashFunction::clear();
232 digest[0] = 0x6A09E667F3BCC908;
233 digest[1] = 0xBB67AE8584CAA73B;
234 digest[2] = 0x3C6EF372FE94F82B;
235 digest[3] = 0xA54FF53A5F1D36F1;
236 digest[4] = 0x510E527FADE682D1;
237 digest[5] = 0x9B05688C2B3E6C1F;
238 digest[6] = 0x1F83D9ABFB41BD6B;
239 digest[7] = 0x5BE0CD19137E2179;
240 }
241
242 }
243