]> asedeno.scripts.mit.edu Git - git.git/blob - block-sha1/sha1.c
block-sha1: improve code on large-register-set machines
[git.git] / block-sha1 / sha1.c
1 /*
2  * Based on the Mozilla SHA1 (see mozilla-sha1/sha1.c),
3  * optimized to do word accesses rather than byte accesses,
4  * and to avoid unnecessary copies into the context array.
5  */
6
7 #include <string.h>
8 #include <arpa/inet.h>
9
10 #include "sha1.h"
11
12 /* Hash one 64-byte block of data */
13 static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data);
14
15 void blk_SHA1_Init(blk_SHA_CTX *ctx)
16 {
17         ctx->size = 0;
18
19         /* Initialize H with the magic constants (see FIPS180 for constants)
20          */
21         ctx->H[0] = 0x67452301;
22         ctx->H[1] = 0xefcdab89;
23         ctx->H[2] = 0x98badcfe;
24         ctx->H[3] = 0x10325476;
25         ctx->H[4] = 0xc3d2e1f0;
26 }
27
28
29 void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
30 {
31         int lenW = ctx->size & 63;
32
33         ctx->size += len;
34
35         /* Read the data into W and process blocks as they get full
36          */
37         if (lenW) {
38                 int left = 64 - lenW;
39                 if (len < left)
40                         left = len;
41                 memcpy(lenW + (char *)ctx->W, data, left);
42                 lenW = (lenW + left) & 63;
43                 len -= left;
44                 data += left;
45                 if (lenW)
46                         return;
47                 blk_SHA1Block(ctx, ctx->W);
48         }
49         while (len >= 64) {
50                 blk_SHA1Block(ctx, data);
51                 data += 64;
52                 len -= 64;
53         }
54         if (len)
55                 memcpy(ctx->W, data, len);
56 }
57
58
59 void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
60 {
61         static const unsigned char pad[64] = { 0x80 };
62         unsigned int padlen[2];
63         int i;
64
65         /* Pad with a binary 1 (ie 0x80), then zeroes, then length
66          */
67         padlen[0] = htonl(ctx->size >> 29);
68         padlen[1] = htonl(ctx->size << 3);
69
70         i = ctx->size & 63;
71         blk_SHA1_Update(ctx, pad, 1+ (63 & (55 - i)));
72         blk_SHA1_Update(ctx, padlen, 8);
73
74         /* Output hash
75          */
76         for (i = 0; i < 5; i++)
77                 ((unsigned int *)hashout)[i] = htonl(ctx->H[i]);
78 }
79
80 #if defined(__i386__) || defined(__x86_64__)
81
82 #define SHA_ASM(op, x, n) ({ unsigned int __res; __asm__(op " %1,%0":"=r" (__res):"i" (n), "0" (x)); __res; })
83 #define SHA_ROL(x,n)    SHA_ASM("rol", x, n)
84 #define SHA_ROR(x,n)    SHA_ASM("ror", x, n)
85 #define SMALL_REGISTER_SET
86
87 #else
88
89 #define SHA_ROT(X,l,r)  (((X) << (l)) | ((X) >> (r)))
90 #define SHA_ROL(X,n)    SHA_ROT(X,n,32-(n))
91 #define SHA_ROR(X,n)    SHA_ROT(X,32-(n),n)
92
93 #endif
94
95 /* This "rolls" over the 512-bit array */
96 #define W(x) (array[(x)&15])
97
98 /*
99  * If you have 32 registers or more, the compiler can (and should)
100  * try to change the array[] accesses into registers. However, on
101  * machines with less than ~25 registers, that won't really work,
102  * and at least gcc will make an unholy mess of it.
103  *
104  * So to avoid that mess which just slows things down, we force
105  * the stores to memory to actually happen (we might be better off
106  * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
107  * suggested by Artur Skawina - that will also make gcc unable to
108  * try to do the silly "optimize away loads" part because it won't
109  * see what the value will be).
110  *
111  * Ben Herrenschmidt reports that on PPC, the C version comes close
112  * to the optimized asm with this (ie on PPC you don't want that
113  * 'volatile', since there are lots of registers).
114  */
115 #ifdef SMALL_REGISTER_SET
116   #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
117 #else
118   #define setW(x, val) (W(x) = (val))
119 #endif
120
121 /*
122  * Where do we get the source from? The first 16 iterations get it from
123  * the input data, the next mix it from the 512-bit array.
124  */
125 #define SHA_SRC(t) htonl(data[t])
126 #define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
127
128 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
129         unsigned int TEMP = input(t); setW(t, TEMP); \
130         E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
131         B = SHA_ROR(B, 2); } while (0)
132
133 #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 )
134 #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 )
135 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
136 #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 )
137 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) ,  0xca62c1d6, A, B, C, D, E )
138
139 static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data)
140 {
141         unsigned int A,B,C,D,E;
142         unsigned int array[16];
143
144         A = ctx->H[0];
145         B = ctx->H[1];
146         C = ctx->H[2];
147         D = ctx->H[3];
148         E = ctx->H[4];
149
150         /* Round 1 - iterations 0-16 take their input from 'data' */
151         T_0_15( 0, A, B, C, D, E);
152         T_0_15( 1, E, A, B, C, D);
153         T_0_15( 2, D, E, A, B, C);
154         T_0_15( 3, C, D, E, A, B);
155         T_0_15( 4, B, C, D, E, A);
156         T_0_15( 5, A, B, C, D, E);
157         T_0_15( 6, E, A, B, C, D);
158         T_0_15( 7, D, E, A, B, C);
159         T_0_15( 8, C, D, E, A, B);
160         T_0_15( 9, B, C, D, E, A);
161         T_0_15(10, A, B, C, D, E);
162         T_0_15(11, E, A, B, C, D);
163         T_0_15(12, D, E, A, B, C);
164         T_0_15(13, C, D, E, A, B);
165         T_0_15(14, B, C, D, E, A);
166         T_0_15(15, A, B, C, D, E);
167
168         /* Round 1 - tail. Input from 512-bit mixing array */
169         T_16_19(16, E, A, B, C, D);
170         T_16_19(17, D, E, A, B, C);
171         T_16_19(18, C, D, E, A, B);
172         T_16_19(19, B, C, D, E, A);
173
174         /* Round 2 */
175         T_20_39(20, A, B, C, D, E);
176         T_20_39(21, E, A, B, C, D);
177         T_20_39(22, D, E, A, B, C);
178         T_20_39(23, C, D, E, A, B);
179         T_20_39(24, B, C, D, E, A);
180         T_20_39(25, A, B, C, D, E);
181         T_20_39(26, E, A, B, C, D);
182         T_20_39(27, D, E, A, B, C);
183         T_20_39(28, C, D, E, A, B);
184         T_20_39(29, B, C, D, E, A);
185         T_20_39(30, A, B, C, D, E);
186         T_20_39(31, E, A, B, C, D);
187         T_20_39(32, D, E, A, B, C);
188         T_20_39(33, C, D, E, A, B);
189         T_20_39(34, B, C, D, E, A);
190         T_20_39(35, A, B, C, D, E);
191         T_20_39(36, E, A, B, C, D);
192         T_20_39(37, D, E, A, B, C);
193         T_20_39(38, C, D, E, A, B);
194         T_20_39(39, B, C, D, E, A);
195
196         /* Round 3 */
197         T_40_59(40, A, B, C, D, E);
198         T_40_59(41, E, A, B, C, D);
199         T_40_59(42, D, E, A, B, C);
200         T_40_59(43, C, D, E, A, B);
201         T_40_59(44, B, C, D, E, A);
202         T_40_59(45, A, B, C, D, E);
203         T_40_59(46, E, A, B, C, D);
204         T_40_59(47, D, E, A, B, C);
205         T_40_59(48, C, D, E, A, B);
206         T_40_59(49, B, C, D, E, A);
207         T_40_59(50, A, B, C, D, E);
208         T_40_59(51, E, A, B, C, D);
209         T_40_59(52, D, E, A, B, C);
210         T_40_59(53, C, D, E, A, B);
211         T_40_59(54, B, C, D, E, A);
212         T_40_59(55, A, B, C, D, E);
213         T_40_59(56, E, A, B, C, D);
214         T_40_59(57, D, E, A, B, C);
215         T_40_59(58, C, D, E, A, B);
216         T_40_59(59, B, C, D, E, A);
217
218         /* Round 4 */
219         T_60_79(60, A, B, C, D, E);
220         T_60_79(61, E, A, B, C, D);
221         T_60_79(62, D, E, A, B, C);
222         T_60_79(63, C, D, E, A, B);
223         T_60_79(64, B, C, D, E, A);
224         T_60_79(65, A, B, C, D, E);
225         T_60_79(66, E, A, B, C, D);
226         T_60_79(67, D, E, A, B, C);
227         T_60_79(68, C, D, E, A, B);
228         T_60_79(69, B, C, D, E, A);
229         T_60_79(70, A, B, C, D, E);
230         T_60_79(71, E, A, B, C, D);
231         T_60_79(72, D, E, A, B, C);
232         T_60_79(73, C, D, E, A, B);
233         T_60_79(74, B, C, D, E, A);
234         T_60_79(75, A, B, C, D, E);
235         T_60_79(76, E, A, B, C, D);
236         T_60_79(77, D, E, A, B, C);
237         T_60_79(78, C, D, E, A, B);
238         T_60_79(79, B, C, D, E, A);
239
240         ctx->H[0] += A;
241         ctx->H[1] += B;
242         ctx->H[2] += C;
243         ctx->H[3] += D;
244         ctx->H[4] += E;
245 }