1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 
8 #include "generic.h"
9 
10 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
11 
12 /*
13  * Force usage of rol or ror by selecting the one with the smaller constant.
14  * It _can_ generate slightly smaller code (a constant of 1 is special), but
15  * perhaps more importantly it's possibly faster on any uarch that does a
16  * rotate with a loop.
17  */
18 
19 #define SHA_ASM(op, x, n) (__extension__ ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; }))
20 #define SHA_ROL(x,n)	SHA_ASM("rol", x, n)
21 #define SHA_ROR(x,n)	SHA_ASM("ror", x, n)
22 
23 #else
24 
25 #define SHA_ROT(X,l,r)	(((X) << (l)) | ((X) >> (r)))
26 #define SHA_ROL(X,n)	SHA_ROT(X,n,32-(n))
27 #define SHA_ROR(X,n)	SHA_ROT(X,32-(n),n)
28 
29 #endif
30 
31 /*
32  * If you have 32 registers or more, the compiler can (and should)
33  * try to change the array[] accesses into registers. However, on
34  * machines with less than ~25 registers, that won't really work,
35  * and at least gcc will make an unholy mess of it.
36  *
37  * So to avoid that mess which just slows things down, we force
38  * the stores to memory to actually happen (we might be better off
39  * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
40  * suggested by Artur Skawina - that will also make gcc unable to
41  * try to do the silly "optimize away loads" part because it won't
42  * see what the value will be).
43  *
44  * Ben Herrenschmidt reports that on PPC, the C version comes close
45  * to the optimized asm with this (ie on PPC you don't want that
46  * 'volatile', since there are lots of registers).
47  *
48  * On ARM we get the best code generation by forcing a full memory barrier
49  * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
50  * the stack frame size simply explode and performance goes down the drain.
51  */
52 
53 #if defined(__i386__) || defined(__x86_64__)
54  #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
55 #elif defined(__GNUC__) && defined(__arm__)
56  #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
57 #else
58  #define setW(x, val) (W(x) = (val))
59 #endif
60 
61 /*
62  * Performance might be improved if the CPU architecture is OK with
63  * unaligned 32-bit loads and a fast ntohl() is available.
64  * Otherwise fall back to byte loads and shifts which is portable,
65  * and is faster on architectures with memory alignment issues.
66  */
67 
68 #if defined(__i386__) || defined(__x86_64__) || \
69 	defined(_M_IX86) || defined(_M_X64) || \
70 	defined(__ppc__) || defined(__ppc64__) || \
71 	defined(__powerpc__) || defined(__powerpc64__) || \
72 	defined(__s390__) || defined(__s390x__)
73 
74 #define get_be32(p)	ntohl(*(const unsigned int *)(p))
75 #define put_be32(p, v)	do { *(unsigned int *)(p) = htonl(v); } while (0)
76 
77 #else
78 
79 #define get_be32(p)	( \
80 	(*((const unsigned char *)(p) + 0) << 24) | \
81 	(*((const unsigned char *)(p) + 1) << 16) | \
82 	(*((const unsigned char *)(p) + 2) << 8) | \
83 	(*((const unsigned char *)(p) + 3) << 0) )
84 #define put_be32(p, v)	do { \
85 	unsigned int __v = (v); \
86 	*((unsigned char *)(p) + 0) = __v >> 24; \
87 	*((unsigned char *)(p) + 1) = __v >> 16; \
88 	*((unsigned char *)(p) + 2) = __v >> 8; \
89 	*((unsigned char *)(p) + 3) = __v >> 0; } while (0)
90 
91 #endif
92 
93 /* This "rolls" over the 512-bit array */
94 #define W(x) (array[(x)&15])
95 
96 /*
97  * Where do we get the source from? The first 16 iterations get it from
98  * the input data, the next mix it from the 512-bit array.
99  */
100 #define SHA_SRC(t) get_be32(data + t)
101 #define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
102 
103 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
104 	unsigned int TEMP = input(t); setW(t, TEMP); \
105 	E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
106 	B = SHA_ROR(B, 2); } while (0)
107 
108 #define T_0_15(t, A, B, C, D, E) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
109 #define T_16_19(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
110 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
111 #define T_40_59(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, ((B&C)+(D&(B^C))) , 0x8f1bbcdc, A, B, C, D, E )
112 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
113 
hash__block(git_hash_sha1_ctx * ctx,const unsigned int * data)114 static void hash__block(git_hash_sha1_ctx *ctx, const unsigned int *data)
115 {
116 	unsigned int A,B,C,D,E;
117 	unsigned int array[16];
118 
119 	A = ctx->H[0];
120 	B = ctx->H[1];
121 	C = ctx->H[2];
122 	D = ctx->H[3];
123 	E = ctx->H[4];
124 
125 	/* Round 1 - iterations 0-16 take their input from 'data' */
126 	T_0_15( 0, A, B, C, D, E);
127 	T_0_15( 1, E, A, B, C, D);
128 	T_0_15( 2, D, E, A, B, C);
129 	T_0_15( 3, C, D, E, A, B);
130 	T_0_15( 4, B, C, D, E, A);
131 	T_0_15( 5, A, B, C, D, E);
132 	T_0_15( 6, E, A, B, C, D);
133 	T_0_15( 7, D, E, A, B, C);
134 	T_0_15( 8, C, D, E, A, B);
135 	T_0_15( 9, B, C, D, E, A);
136 	T_0_15(10, A, B, C, D, E);
137 	T_0_15(11, E, A, B, C, D);
138 	T_0_15(12, D, E, A, B, C);
139 	T_0_15(13, C, D, E, A, B);
140 	T_0_15(14, B, C, D, E, A);
141 	T_0_15(15, A, B, C, D, E);
142 
143 	/* Round 1 - tail. Input from 512-bit mixing array */
144 	T_16_19(16, E, A, B, C, D);
145 	T_16_19(17, D, E, A, B, C);
146 	T_16_19(18, C, D, E, A, B);
147 	T_16_19(19, B, C, D, E, A);
148 
149 	/* Round 2 */
150 	T_20_39(20, A, B, C, D, E);
151 	T_20_39(21, E, A, B, C, D);
152 	T_20_39(22, D, E, A, B, C);
153 	T_20_39(23, C, D, E, A, B);
154 	T_20_39(24, B, C, D, E, A);
155 	T_20_39(25, A, B, C, D, E);
156 	T_20_39(26, E, A, B, C, D);
157 	T_20_39(27, D, E, A, B, C);
158 	T_20_39(28, C, D, E, A, B);
159 	T_20_39(29, B, C, D, E, A);
160 	T_20_39(30, A, B, C, D, E);
161 	T_20_39(31, E, A, B, C, D);
162 	T_20_39(32, D, E, A, B, C);
163 	T_20_39(33, C, D, E, A, B);
164 	T_20_39(34, B, C, D, E, A);
165 	T_20_39(35, A, B, C, D, E);
166 	T_20_39(36, E, A, B, C, D);
167 	T_20_39(37, D, E, A, B, C);
168 	T_20_39(38, C, D, E, A, B);
169 	T_20_39(39, B, C, D, E, A);
170 
171 	/* Round 3 */
172 	T_40_59(40, A, B, C, D, E);
173 	T_40_59(41, E, A, B, C, D);
174 	T_40_59(42, D, E, A, B, C);
175 	T_40_59(43, C, D, E, A, B);
176 	T_40_59(44, B, C, D, E, A);
177 	T_40_59(45, A, B, C, D, E);
178 	T_40_59(46, E, A, B, C, D);
179 	T_40_59(47, D, E, A, B, C);
180 	T_40_59(48, C, D, E, A, B);
181 	T_40_59(49, B, C, D, E, A);
182 	T_40_59(50, A, B, C, D, E);
183 	T_40_59(51, E, A, B, C, D);
184 	T_40_59(52, D, E, A, B, C);
185 	T_40_59(53, C, D, E, A, B);
186 	T_40_59(54, B, C, D, E, A);
187 	T_40_59(55, A, B, C, D, E);
188 	T_40_59(56, E, A, B, C, D);
189 	T_40_59(57, D, E, A, B, C);
190 	T_40_59(58, C, D, E, A, B);
191 	T_40_59(59, B, C, D, E, A);
192 
193 	/* Round 4 */
194 	T_60_79(60, A, B, C, D, E);
195 	T_60_79(61, E, A, B, C, D);
196 	T_60_79(62, D, E, A, B, C);
197 	T_60_79(63, C, D, E, A, B);
198 	T_60_79(64, B, C, D, E, A);
199 	T_60_79(65, A, B, C, D, E);
200 	T_60_79(66, E, A, B, C, D);
201 	T_60_79(67, D, E, A, B, C);
202 	T_60_79(68, C, D, E, A, B);
203 	T_60_79(69, B, C, D, E, A);
204 	T_60_79(70, A, B, C, D, E);
205 	T_60_79(71, E, A, B, C, D);
206 	T_60_79(72, D, E, A, B, C);
207 	T_60_79(73, C, D, E, A, B);
208 	T_60_79(74, B, C, D, E, A);
209 	T_60_79(75, A, B, C, D, E);
210 	T_60_79(76, E, A, B, C, D);
211 	T_60_79(77, D, E, A, B, C);
212 	T_60_79(78, C, D, E, A, B);
213 	T_60_79(79, B, C, D, E, A);
214 
215 	ctx->H[0] += A;
216 	ctx->H[1] += B;
217 	ctx->H[2] += C;
218 	ctx->H[3] += D;
219 	ctx->H[4] += E;
220 }
221 
git_hash_sha1_global_init(void)222 int git_hash_sha1_global_init(void)
223 {
224 	return 0;
225 }
226 
git_hash_sha1_ctx_init(git_hash_sha1_ctx * ctx)227 int git_hash_sha1_ctx_init(git_hash_sha1_ctx *ctx)
228 {
229 	return git_hash_sha1_init(ctx);
230 }
231 
git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx * ctx)232 void git_hash_sha1_ctx_cleanup(git_hash_sha1_ctx *ctx)
233 {
234 	GIT_UNUSED(ctx);
235 }
236 
git_hash_sha1_init(git_hash_sha1_ctx * ctx)237 int git_hash_sha1_init(git_hash_sha1_ctx *ctx)
238 {
239 	ctx->size = 0;
240 
241 	/* Initialize H with the magic constants (see FIPS180 for constants) */
242 	ctx->H[0] = 0x67452301;
243 	ctx->H[1] = 0xefcdab89;
244 	ctx->H[2] = 0x98badcfe;
245 	ctx->H[3] = 0x10325476;
246 	ctx->H[4] = 0xc3d2e1f0;
247 
248 	return 0;
249 }
250 
git_hash_sha1_update(git_hash_sha1_ctx * ctx,const void * data,size_t len)251 int git_hash_sha1_update(git_hash_sha1_ctx *ctx, const void *data, size_t len)
252 {
253 	unsigned int lenW = ctx->size & 63;
254 
255 	ctx->size += len;
256 
257 	/* Read the data into W and process blocks as they get full */
258 	if (lenW) {
259 		unsigned int left = 64 - lenW;
260 		if (len < left)
261 			left = (unsigned int)len;
262 		memcpy(lenW + (char *)ctx->W, data, left);
263 		lenW = (lenW + left) & 63;
264 		len -= left;
265 		data = ((const char *)data + left);
266 		if (lenW)
267 			return 0;
268 		hash__block(ctx, ctx->W);
269 	}
270 	while (len >= 64) {
271 		hash__block(ctx, data);
272 		data = ((const char *)data + 64);
273 		len -= 64;
274 	}
275 	if (len)
276 		memcpy(ctx->W, data, len);
277 
278 	return 0;
279 }
280 
git_hash_sha1_final(git_oid * out,git_hash_sha1_ctx * ctx)281 int git_hash_sha1_final(git_oid *out, git_hash_sha1_ctx *ctx)
282 {
283 	static const unsigned char pad[64] = { 0x80 };
284 	unsigned int padlen[2];
285 	int i;
286 
287 	/* Pad with a binary 1 (ie 0x80), then zeroes, then length */
288 	padlen[0] = htonl((uint32_t)(ctx->size >> 29));
289 	padlen[1] = htonl((uint32_t)(ctx->size << 3));
290 
291 	i = ctx->size & 63;
292 	git_hash_sha1_update(ctx, pad, 1+ (63 & (55 - i)));
293 	git_hash_sha1_update(ctx, padlen, 8);
294 
295 	/* Output hash */
296 	for (i = 0; i < 5; i++)
297 		put_be32(out->id + i*4, ctx->H[i]);
298 
299 	return 0;
300 }
301