2 * SHA1 routine optimized to do word accesses rather than byte accesses,
3 * and to avoid unnecessary copies into the context array.
5 * This was initially based on the Mozilla SHA1 implementation, although
6 * none of the original Mozilla code remains.
9 /* this is only to get definitions for memcpy(), ntohl() and htonl() */
10 #include "../git-compat-util.h"
14 #define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r)))
15 #define SHA_ROL(X,n) SHA_ROT(X,n,32-(n))
16 #define SHA_ROR(X,n) SHA_ROT(X,32-(n),n)
19 * If you have 32 registers or more, the compiler can (and should)
20 * try to change the array[] accesses into registers. However, on
21 * machines with less than ~25 registers, that won't really work,
22 * and at least gcc will make an unholy mess of it.
24 * So to avoid that mess which just slows things down, we force
25 * the stores to memory to actually happen (we might be better off
26 * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
27 * suggested by Artur Skawina - that will also make gcc unable to
28 * try to do the silly "optimize away loads" part because it won't
29 * see what the value will be).
31 * Ben Herrenschmidt reports that on PPC, the C version comes close
32 * to the optimized asm with this (ie on PPC you don't want that
33 * 'volatile', since there are lots of registers).
35 * On ARM we get the best code generation by forcing a full memory barrier
36 * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
37 * the stack frame size simply explode and performance goes down the drain.
40 #if defined(__i386__) || defined(__x86_64__)
41 #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
42 #elif defined(__GNUC__) && defined(__arm__)
43 #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
45 #define setW(x, val) (W(x) = (val))
48 /* This "rolls" over the 512-bit array */
49 #define W(x) (array[(x)&15])
52 * Where do we get the source from? The first 16 iterations get it from
53 * the input data, the next mix it from the 512-bit array.
55 #define SHA_SRC(t) get_be32((unsigned char *) block + (t)*4)
56 #define SHA_MIX(t) SHA_ROL(W((t)+13) ^ W((t)+8) ^ W((t)+2) ^ W(t), 1)
58 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
59 unsigned int TEMP = input(t); setW(t, TEMP); \
60 E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
61 B = SHA_ROR(B, 2); } while (0)
63 #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 )
64 #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 )
65 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
66 #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 )
67 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
69 static void blk_SHA1_Block(blk_SHA_CTX
*ctx
, const void *block
)
71 unsigned int A
,B
,C
,D
,E
;
72 unsigned int array
[16];
80 /* Round 1 - iterations 0-16 take their input from 'block' */
81 T_0_15( 0, A
, B
, C
, D
, E
);
82 T_0_15( 1, E
, A
, B
, C
, D
);
83 T_0_15( 2, D
, E
, A
, B
, C
);
84 T_0_15( 3, C
, D
, E
, A
, B
);
85 T_0_15( 4, B
, C
, D
, E
, A
);
86 T_0_15( 5, A
, B
, C
, D
, E
);
87 T_0_15( 6, E
, A
, B
, C
, D
);
88 T_0_15( 7, D
, E
, A
, B
, C
);
89 T_0_15( 8, C
, D
, E
, A
, B
);
90 T_0_15( 9, B
, C
, D
, E
, A
);
91 T_0_15(10, A
, B
, C
, D
, E
);
92 T_0_15(11, E
, A
, B
, C
, D
);
93 T_0_15(12, D
, E
, A
, B
, C
);
94 T_0_15(13, C
, D
, E
, A
, B
);
95 T_0_15(14, B
, C
, D
, E
, A
);
96 T_0_15(15, A
, B
, C
, D
, E
);
98 /* Round 1 - tail. Input from 512-bit mixing array */
99 T_16_19(16, E
, A
, B
, C
, D
);
100 T_16_19(17, D
, E
, A
, B
, C
);
101 T_16_19(18, C
, D
, E
, A
, B
);
102 T_16_19(19, B
, C
, D
, E
, A
);
105 T_20_39(20, A
, B
, C
, D
, E
);
106 T_20_39(21, E
, A
, B
, C
, D
);
107 T_20_39(22, D
, E
, A
, B
, C
);
108 T_20_39(23, C
, D
, E
, A
, B
);
109 T_20_39(24, B
, C
, D
, E
, A
);
110 T_20_39(25, A
, B
, C
, D
, E
);
111 T_20_39(26, E
, A
, B
, C
, D
);
112 T_20_39(27, D
, E
, A
, B
, C
);
113 T_20_39(28, C
, D
, E
, A
, B
);
114 T_20_39(29, B
, C
, D
, E
, A
);
115 T_20_39(30, A
, B
, C
, D
, E
);
116 T_20_39(31, E
, A
, B
, C
, D
);
117 T_20_39(32, D
, E
, A
, B
, C
);
118 T_20_39(33, C
, D
, E
, A
, B
);
119 T_20_39(34, B
, C
, D
, E
, A
);
120 T_20_39(35, A
, B
, C
, D
, E
);
121 T_20_39(36, E
, A
, B
, C
, D
);
122 T_20_39(37, D
, E
, A
, B
, C
);
123 T_20_39(38, C
, D
, E
, A
, B
);
124 T_20_39(39, B
, C
, D
, E
, A
);
127 T_40_59(40, A
, B
, C
, D
, E
);
128 T_40_59(41, E
, A
, B
, C
, D
);
129 T_40_59(42, D
, E
, A
, B
, C
);
130 T_40_59(43, C
, D
, E
, A
, B
);
131 T_40_59(44, B
, C
, D
, E
, A
);
132 T_40_59(45, A
, B
, C
, D
, E
);
133 T_40_59(46, E
, A
, B
, C
, D
);
134 T_40_59(47, D
, E
, A
, B
, C
);
135 T_40_59(48, C
, D
, E
, A
, B
);
136 T_40_59(49, B
, C
, D
, E
, A
);
137 T_40_59(50, A
, B
, C
, D
, E
);
138 T_40_59(51, E
, A
, B
, C
, D
);
139 T_40_59(52, D
, E
, A
, B
, C
);
140 T_40_59(53, C
, D
, E
, A
, B
);
141 T_40_59(54, B
, C
, D
, E
, A
);
142 T_40_59(55, A
, B
, C
, D
, E
);
143 T_40_59(56, E
, A
, B
, C
, D
);
144 T_40_59(57, D
, E
, A
, B
, C
);
145 T_40_59(58, C
, D
, E
, A
, B
);
146 T_40_59(59, B
, C
, D
, E
, A
);
149 T_60_79(60, A
, B
, C
, D
, E
);
150 T_60_79(61, E
, A
, B
, C
, D
);
151 T_60_79(62, D
, E
, A
, B
, C
);
152 T_60_79(63, C
, D
, E
, A
, B
);
153 T_60_79(64, B
, C
, D
, E
, A
);
154 T_60_79(65, A
, B
, C
, D
, E
);
155 T_60_79(66, E
, A
, B
, C
, D
);
156 T_60_79(67, D
, E
, A
, B
, C
);
157 T_60_79(68, C
, D
, E
, A
, B
);
158 T_60_79(69, B
, C
, D
, E
, A
);
159 T_60_79(70, A
, B
, C
, D
, E
);
160 T_60_79(71, E
, A
, B
, C
, D
);
161 T_60_79(72, D
, E
, A
, B
, C
);
162 T_60_79(73, C
, D
, E
, A
, B
);
163 T_60_79(74, B
, C
, D
, E
, A
);
164 T_60_79(75, A
, B
, C
, D
, E
);
165 T_60_79(76, E
, A
, B
, C
, D
);
166 T_60_79(77, D
, E
, A
, B
, C
);
167 T_60_79(78, C
, D
, E
, A
, B
);
168 T_60_79(79, B
, C
, D
, E
, A
);
177 void blk_SHA1_Init(blk_SHA_CTX
*ctx
)
181 /* Initialize H with the magic constants (see FIPS180 for constants) */
182 ctx
->H
[0] = 0x67452301;
183 ctx
->H
[1] = 0xefcdab89;
184 ctx
->H
[2] = 0x98badcfe;
185 ctx
->H
[3] = 0x10325476;
186 ctx
->H
[4] = 0xc3d2e1f0;
189 void blk_SHA1_Update(blk_SHA_CTX
*ctx
, const void *data
, size_t len
)
191 unsigned int lenW
= ctx
->size
& 63;
195 /* Read the data into W and process blocks as they get full */
197 unsigned int left
= 64 - lenW
;
200 memcpy(lenW
+ (char *)ctx
->W
, data
, left
);
201 lenW
= (lenW
+ left
) & 63;
203 data
= ((const char *)data
+ left
);
206 blk_SHA1_Block(ctx
, ctx
->W
);
209 blk_SHA1_Block(ctx
, data
);
210 data
= ((const char *)data
+ 64);
214 memcpy(ctx
->W
, data
, len
);
217 void blk_SHA1_Final(unsigned char hashout
[20], blk_SHA_CTX
*ctx
)
219 static const unsigned char pad
[64] = { 0x80 };
220 unsigned int padlen
[2];
223 /* Pad with a binary 1 (ie 0x80), then zeroes, then length */
224 padlen
[0] = htonl((uint32_t)(ctx
->size
>> 29));
225 padlen
[1] = htonl((uint32_t)(ctx
->size
<< 3));
228 blk_SHA1_Update(ctx
, pad
, 1 + (63 & (55 - i
)));
229 blk_SHA1_Update(ctx
, padlen
, 8);
232 for (i
= 0; i
< 5; i
++)
233 put_be32(hashout
+ i
* 4, ctx
->H
[i
]);