Line data Source code
1 : /*
2 : * Copyright (c) 2006, 2010 Kungliga Tekniska Högskolan
3 : * (Royal Institute of Technology, Stockholm, Sweden).
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions
8 : * are met:
9 : *
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : *
13 : * 2. Redistributions in binary form must reproduce the above copyright
14 : * notice, this list of conditions and the following disclaimer in the
15 : * documentation and/or other materials provided with the distribution.
16 : *
17 : * 3. Neither the name of the Institute nor the names of its contributors
18 : * may be used to endorse or promote products derived from this software
19 : * without specific prior written permission.
20 : *
21 : * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 : * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 : * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 : * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 : * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 : * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 : * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 : * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 : * SUCH DAMAGE.
32 : */
33 :
34 : #include <config.h>
35 : #include <roken.h>
36 :
37 : #include "hash.h"
38 : #include "sha.h"
39 :
40 : #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
41 : #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
42 :
43 : #define ROTR(x,n) (((x)>>(n)) | ((x) << (64 - (n))))
44 :
45 : #define Sigma0(x) (ROTR(x,28) ^ ROTR(x,34) ^ ROTR(x,39))
46 : #define Sigma1(x) (ROTR(x,14) ^ ROTR(x,18) ^ ROTR(x,41))
47 : #define sigma0(x) (ROTR(x,1) ^ ROTR(x,8) ^ ((x)>>7))
48 : #define sigma1(x) (ROTR(x,19) ^ ROTR(x,61) ^ ((x)>>6))
49 :
50 : #define A m->counter[0]
51 : #define B m->counter[1]
52 : #define C m->counter[2]
53 : #define D m->counter[3]
54 : #define E m->counter[4]
55 : #define F m->counter[5]
56 : #define G m->counter[6]
57 : #define H m->counter[7]
58 :
59 : static const uint64_t constant_512[80] = {
60 : 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
61 : 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
62 : 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
63 : 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
64 : 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
65 : 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
66 : 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
67 : 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
68 : 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
69 : 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
70 : 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
71 : 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
72 : 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
73 : 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
74 : 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
75 : 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
76 : 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
77 : 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
78 : 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
79 : 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
80 : 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
81 : 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
82 : 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
83 : 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
84 : 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
85 : 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
86 : 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
87 : 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
88 : 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
89 : 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
90 : 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
91 : 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
92 : 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
93 : 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
94 : 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
95 : 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
96 : 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
97 : 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
98 : 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
99 : 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
100 : };
101 :
102 : int
103 150 : SHA512_Init (SHA512_CTX *m)
104 : {
105 150 : m->sz[0] = 0;
106 150 : m->sz[1] = 0;
107 150 : A = 0x6a09e667f3bcc908ULL;
108 150 : B = 0xbb67ae8584caa73bULL;
109 150 : C = 0x3c6ef372fe94f82bULL;
110 150 : D = 0xa54ff53a5f1d36f1ULL;
111 150 : E = 0x510e527fade682d1ULL;
112 150 : F = 0x9b05688c2b3e6c1fULL;
113 150 : G = 0x1f83d9abfb41bd6bULL;
114 150 : H = 0x5be0cd19137e2179ULL;
115 150 : return 1;
116 : }
117 :
118 : static void
119 404 : calc (SHA512_CTX *m, uint64_t *in)
120 : {
121 0 : uint64_t AA, BB, CC, DD, EE, FF, GG, HH;
122 0 : uint64_t data[80];
123 0 : int i;
124 :
125 404 : AA = A;
126 404 : BB = B;
127 404 : CC = C;
128 404 : DD = D;
129 404 : EE = E;
130 404 : FF = F;
131 404 : GG = G;
132 404 : HH = H;
133 :
134 6868 : for (i = 0; i < 16; ++i)
135 6464 : data[i] = in[i];
136 26260 : for (i = 16; i < 80; ++i)
137 25856 : data[i] = sigma1(data[i-2]) + data[i-7] +
138 25856 : sigma0(data[i-15]) + data[i - 16];
139 :
140 32724 : for (i = 0; i < 80; i++) {
141 0 : uint64_t T1, T2;
142 :
143 32320 : T1 = HH + Sigma1(EE) + Ch(EE, FF, GG) + constant_512[i] + data[i];
144 32320 : T2 = Sigma0(AA) + Maj(AA,BB,CC);
145 :
146 32320 : HH = GG;
147 32320 : GG = FF;
148 32320 : FF = EE;
149 32320 : EE = DD + T1;
150 32320 : DD = CC;
151 32320 : CC = BB;
152 32320 : BB = AA;
153 32320 : AA = T1 + T2;
154 : }
155 :
156 404 : A += AA;
157 404 : B += BB;
158 404 : C += CC;
159 404 : D += DD;
160 404 : E += EE;
161 404 : F += FF;
162 404 : G += GG;
163 404 : H += HH;
164 404 : }
165 :
166 : /*
167 : * From `Performance analysis of MD5' by Joseph D. Touch <touch@isi.edu>
168 : */
169 :
170 : #if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
171 : static inline uint64_t
172 6464 : swap_uint64_t (uint64_t t)
173 : {
174 0 : uint64_t temp;
175 :
176 6464 : temp = cshift64(t, 32);
177 6464 : temp = ((temp & 0xff00ff00ff00ff00ULL) >> 8) |
178 6464 : ((temp & 0x00ff00ff00ff00ffULL) << 8);
179 12928 : return ((temp & 0xffff0000ffff0000ULL) >> 16) |
180 6464 : ((temp & 0x0000ffff0000ffffULL) << 16);
181 : }
182 :
183 : struct x64{
184 : uint64_t a;
185 : uint64_t b;
186 : };
187 : #endif
188 :
189 : int
190 300 : SHA512_Update (SHA512_CTX *m, const void *v, size_t len)
191 : {
192 300 : const unsigned char *p = v;
193 300 : size_t old_sz = m->sz[0];
194 0 : size_t offset;
195 :
196 300 : m->sz[0] += len * 8;
197 300 : if (m->sz[0] < old_sz)
198 0 : ++m->sz[1];
199 300 : offset = (old_sz / 8) % 128;
200 854 : while(len > 0){
201 554 : size_t l = min(len, 128 - offset);
202 554 : memcpy(m->save + offset, p, l);
203 554 : offset += l;
204 554 : p += l;
205 554 : len -= l;
206 554 : if(offset == 128){
207 : #if !defined(WORDS_BIGENDIAN) || defined(_CRAY)
208 : int i;
209 : uint64_t current[16];
210 404 : struct x64 *us = (struct x64*)m->save;
211 3636 : for(i = 0; i < 8; i++){
212 3232 : current[2*i+0] = swap_uint64_t(us[i].a);
213 3232 : current[2*i+1] = swap_uint64_t(us[i].b);
214 : }
215 404 : calc(m, current);
216 : #else
217 : calc(m, (uint64_t*)m->save);
218 : #endif
219 404 : offset = 0;
220 : }
221 : }
222 300 : return 1;
223 : }
224 :
225 : int
226 150 : SHA512_Final (void *res, SHA512_CTX *m)
227 : {
228 0 : unsigned char zeros[128 + 16];
229 150 : unsigned offset = (m->sz[0] / 8) % 128;
230 150 : unsigned int dstart = (240 - offset - 1) % 128 + 1;
231 :
232 150 : *zeros = 0x80;
233 150 : memset (zeros + 1, 0, sizeof(zeros) - 1);
234 150 : zeros[dstart+15] = (m->sz[0] >> 0) & 0xff;
235 150 : zeros[dstart+14] = (m->sz[0] >> 8) & 0xff;
236 150 : zeros[dstart+13] = (m->sz[0] >> 16) & 0xff;
237 150 : zeros[dstart+12] = (m->sz[0] >> 24) & 0xff;
238 150 : zeros[dstart+11] = (m->sz[0] >> 32) & 0xff;
239 150 : zeros[dstart+10] = (m->sz[0] >> 40) & 0xff;
240 150 : zeros[dstart+9] = (m->sz[0] >> 48) & 0xff;
241 150 : zeros[dstart+8] = (m->sz[0] >> 56) & 0xff;
242 :
243 150 : zeros[dstart+7] = (m->sz[1] >> 0) & 0xff;
244 150 : zeros[dstart+6] = (m->sz[1] >> 8) & 0xff;
245 150 : zeros[dstart+5] = (m->sz[1] >> 16) & 0xff;
246 150 : zeros[dstart+4] = (m->sz[1] >> 24) & 0xff;
247 150 : zeros[dstart+3] = (m->sz[1] >> 32) & 0xff;
248 150 : zeros[dstart+2] = (m->sz[1] >> 40) & 0xff;
249 150 : zeros[dstart+1] = (m->sz[1] >> 48) & 0xff;
250 150 : zeros[dstart+0] = (m->sz[1] >> 56) & 0xff;
251 150 : SHA512_Update (m, zeros, dstart + 16);
252 : {
253 0 : int i;
254 150 : unsigned char *r = (unsigned char*)res;
255 :
256 1350 : for (i = 0; i < 8; ++i) {
257 1200 : r[8*i+7] = m->counter[i] & 0xFF;
258 1200 : r[8*i+6] = (m->counter[i] >> 8) & 0xFF;
259 1200 : r[8*i+5] = (m->counter[i] >> 16) & 0xFF;
260 1200 : r[8*i+4] = (m->counter[i] >> 24) & 0xFF;
261 1200 : r[8*i+3] = (m->counter[i] >> 32) & 0XFF;
262 1200 : r[8*i+2] = (m->counter[i] >> 40) & 0xFF;
263 1200 : r[8*i+1] = (m->counter[i] >> 48) & 0xFF;
264 1200 : r[8*i] = (m->counter[i] >> 56) & 0xFF;
265 : }
266 : }
267 150 : return 1;
268 : }
269 :
270 : int
271 0 : SHA384_Init(SHA384_CTX *m)
272 : {
273 0 : m->sz[0] = 0;
274 0 : m->sz[1] = 0;
275 0 : A = 0xcbbb9d5dc1059ed8ULL;
276 0 : B = 0x629a292a367cd507ULL;
277 0 : C = 0x9159015a3070dd17ULL;
278 0 : D = 0x152fecd8f70e5939ULL;
279 0 : E = 0x67332667ffc00b31ULL;
280 0 : F = 0x8eb44a8768581511ULL;
281 0 : G = 0xdb0c2e0d64f98fa7ULL;
282 0 : H = 0x47b5481dbefa4fa4ULL;
283 0 : return 1;
284 : }
285 :
286 : int
287 0 : SHA384_Update (SHA384_CTX *m, const void *v, size_t len)
288 : {
289 0 : SHA512_Update(m, v, len);
290 0 : return 1;
291 : }
292 :
293 : int
294 0 : SHA384_Final (void *res, SHA384_CTX *m)
295 : {
296 0 : unsigned char data[SHA512_DIGEST_LENGTH];
297 0 : SHA512_Final(data, m);
298 0 : memcpy(res, data, SHA384_DIGEST_LENGTH);
299 0 : return 1;
300 : }
301 :
|