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 * On ARM we get the best code generation by forcing a full memory barrier
32 * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
33 * the stack frame size simply explode and performance goes down the drain.
36 #if defined(__i386__) || defined(__x86_64__)
37 #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
38 #elif defined(__GNUC__) && defined(__arm__)
39 #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
41 #define setW(x, val) (W(x) = (val))
44 /* This "rolls" over the 512-bit array */
45 #define W(x) (array[(x)&15])
48 * Where do we get the source from? The first 16 iterations get it from
49 * the input data, the next mix it from the 512-bit array.
51 #define SHA_SRC(t) get_be32((unsigned char *) block + (t)*4)
52 #define SHA_MIX(t) SHA_ROL(W((t)+13) ^ W((t)+8) ^ W((t)+2) ^ W(t), 1)
54 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
55 unsigned int TEMP = input(t); setW(t, TEMP); \
56 E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
57 B = SHA_ROR(B, 2); } while (0)
59 #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 )
60 #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 )
61 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
62 #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 )
63 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
65 static void blk_SHA1_Block(blk_SHA_CTX
*ctx
, const void *block
)
67 unsigned int A
,B
,C
,D
,E
;
68 unsigned int array
[16];
76 /* Round 1 - iterations 0-16 take their input from 'block' */
77 T_0_15( 0, A
, B
, C
, D
, E
);
78 T_0_15( 1, E
, A
, B
, C
, D
);
79 T_0_15( 2, D
, E
, A
, B
, C
);
80 T_0_15( 3, C
, D
, E
, A
, B
);
81 T_0_15( 4, B
, C
, D
, E
, A
);
82 T_0_15( 5, A
, B
, C
, D
, E
);
83 T_0_15( 6, E
, A
, B
, C
, D
);
84 T_0_15( 7, D
, E
, A
, B
, C
);
85 T_0_15( 8, C
, D
, E
, A
, B
);
86 T_0_15( 9, B
, C
, D
, E
, A
);
87 T_0_15(10, A
, B
, C
, D
, E
);
88 T_0_15(11, E
, A
, B
, C
, D
);
89 T_0_15(12, D
, E
, A
, B
, C
);
90 T_0_15(13, C
, D
, E
, A
, B
);
91 T_0_15(14, B
, C
, D
, E
, A
);
92 T_0_15(15, A
, B
, C
, D
, E
);
94 /* Round 1 - tail. Input from 512-bit mixing array */
95 T_16_19(16, E
, A
, B
, C
, D
);
96 T_16_19(17, D
, E
, A
, B
, C
);
97 T_16_19(18, C
, D
, E
, A
, B
);
98 T_16_19(19, B
, C
, D
, E
, A
);
101 T_20_39(20, A
, B
, C
, D
, E
);
102 T_20_39(21, E
, A
, B
, C
, D
);
103 T_20_39(22, D
, E
, A
, B
, C
);
104 T_20_39(23, C
, D
, E
, A
, B
);
105 T_20_39(24, B
, C
, D
, E
, A
);
106 T_20_39(25, A
, B
, C
, D
, E
);
107 T_20_39(26, E
, A
, B
, C
, D
);
108 T_20_39(27, D
, E
, A
, B
, C
);
109 T_20_39(28, C
, D
, E
, A
, B
);
110 T_20_39(29, B
, C
, D
, E
, A
);
111 T_20_39(30, A
, B
, C
, D
, E
);
112 T_20_39(31, E
, A
, B
, C
, D
);
113 T_20_39(32, D
, E
, A
, B
, C
);
114 T_20_39(33, C
, D
, E
, A
, B
);
115 T_20_39(34, B
, C
, D
, E
, A
);
116 T_20_39(35, A
, B
, C
, D
, E
);
117 T_20_39(36, E
, A
, B
, C
, D
);
118 T_20_39(37, D
, E
, A
, B
, C
);
119 T_20_39(38, C
, D
, E
, A
, B
);
120 T_20_39(39, B
, C
, D
, E
, A
);
123 T_40_59(40, A
, B
, C
, D
, E
);
124 T_40_59(41, E
, A
, B
, C
, D
);
125 T_40_59(42, D
, E
, A
, B
, C
);
126 T_40_59(43, C
, D
, E
, A
, B
);
127 T_40_59(44, B
, C
, D
, E
, A
);
128 T_40_59(45, A
, B
, C
, D
, E
);
129 T_40_59(46, E
, A
, B
, C
, D
);
130 T_40_59(47, D
, E
, A
, B
, C
);
131 T_40_59(48, C
, D
, E
, A
, B
);
132 T_40_59(49, B
, C
, D
, E
, A
);
133 T_40_59(50, A
, B
, C
, D
, E
);
134 T_40_59(51, E
, A
, B
, C
, D
);
135 T_40_59(52, D
, E
, A
, B
, C
);
136 T_40_59(53, C
, D
, E
, A
, B
);
137 T_40_59(54, B
, C
, D
, E
, A
);
138 T_40_59(55, A
, B
, C
, D
, E
);
139 T_40_59(56, E
, A
, B
, C
, D
);
140 T_40_59(57, D
, E
, A
, B
, C
);
141 T_40_59(58, C
, D
, E
, A
, B
);
142 T_40_59(59, B
, C
, D
, E
, A
);
145 T_60_79(60, A
, B
, C
, D
, E
);
146 T_60_79(61, E
, A
, B
, C
, D
);
147 T_60_79(62, D
, E
, A
, B
, C
);
148 T_60_79(63, C
, D
, E
, A
, B
);
149 T_60_79(64, B
, C
, D
, E
, A
);
150 T_60_79(65, A
, B
, C
, D
, E
);
151 T_60_79(66, E
, A
, B
, C
, D
);
152 T_60_79(67, D
, E
, A
, B
, C
);
153 T_60_79(68, C
, D
, E
, A
, B
);
154 T_60_79(69, B
, C
, D
, E
, A
);
155 T_60_79(70, A
, B
, C
, D
, E
);
156 T_60_79(71, E
, A
, B
, C
, D
);
157 T_60_79(72, D
, E
, A
, B
, C
);
158 T_60_79(73, C
, D
, E
, A
, B
);
159 T_60_79(74, B
, C
, D
, E
, A
);
160 T_60_79(75, A
, B
, C
, D
, E
);
161 T_60_79(76, E
, A
, B
, C
, D
);
162 T_60_79(77, D
, E
, A
, B
, C
);
163 T_60_79(78, C
, D
, E
, A
, B
);
164 T_60_79(79, B
, C
, D
, E
, A
);
173 void blk_SHA1_Init(blk_SHA_CTX
*ctx
)
177 /* Initialize H with the magic constants (see FIPS180 for constants) */
178 ctx
->H
[0] = 0x67452301;
179 ctx
->H
[1] = 0xefcdab89;
180 ctx
->H
[2] = 0x98badcfe;
181 ctx
->H
[3] = 0x10325476;
182 ctx
->H
[4] = 0xc3d2e1f0;
185 void blk_SHA1_Update(blk_SHA_CTX
*ctx
, const void *data
, size_t len
)
187 unsigned int lenW
= ctx
->size
& 63;
191 /* Read the data into W and process blocks as they get full */
193 unsigned int left
= 64 - lenW
;
196 memcpy(lenW
+ (char *)ctx
->W
, data
, left
);
197 lenW
= (lenW
+ left
) & 63;
199 data
= ((const char *)data
+ left
);
202 blk_SHA1_Block(ctx
, ctx
->W
);
205 blk_SHA1_Block(ctx
, data
);
206 data
= ((const char *)data
+ 64);
210 memcpy(ctx
->W
, data
, len
);
213 void blk_SHA1_Final(unsigned char hashout
[20], blk_SHA_CTX
*ctx
)
215 static const unsigned char pad
[64] = { 0x80 };
216 unsigned int padlen
[2];
219 /* Pad with a binary 1 (ie 0x80), then zeroes, then length */
220 padlen
[0] = htonl((uint32_t)(ctx
->size
>> 29));
221 padlen
[1] = htonl((uint32_t)(ctx
->size
<< 3));
224 blk_SHA1_Update(ctx
, pad
, 1 + (63 & (55 - i
)));
225 blk_SHA1_Update(ctx
, padlen
, 8);
228 for (i
= 0; i
< 5; i
++)
229 put_be32(hashout
+ i
* 4, ctx
->H
[i
]);