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