b715916675469595c0955a15dc0bfde7e6508052
[git/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
86 #else
87
88 #define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r)))
89 #define SHA_ROL(X,n) SHA_ROT(X,n,32-(n))
90 #define SHA_ROR(X,n) SHA_ROT(X,32-(n),n)
91
92 #endif
93
94 /* This "rolls" over the 512-bit array */
95 #define W(x) (array[(x)&15])
96
97 /*
98 * Where do we get the source from? The first 16 iterations get it from
99 * the input data, the next mix it from the 512-bit array.
100 */
101 #define SHA_SRC(t) htonl(data[t])
102 #define SHA_MIX(t) SHA_ROL(W(t+13) ^ W(t+8) ^ W(t+2) ^ W(t), 1)
103
104 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
105 unsigned int TEMP = input(t); W(t) = TEMP; \
106 TEMP += E + SHA_ROL(A,5) + (fn) + (constant); \
107 B = SHA_ROR(B, 2); E = TEMP; } while (0)
108
109 #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 )
110 #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 )
111 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
112 #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 )
113 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
114
115 static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data)
116 {
117 unsigned int A,B,C,D,E;
118 unsigned int array[16];
119
120 A = ctx->H[0];
121 B = ctx->H[1];
122 C = ctx->H[2];
123 D = ctx->H[3];
124 E = ctx->H[4];
125
126 /* Round 1 - iterations 0-16 take their input from 'data' */
127 T_0_15( 0, A, B, C, D, E);
128 T_0_15( 1, E, A, B, C, D);
129 T_0_15( 2, D, E, A, B, C);
130 T_0_15( 3, C, D, E, A, B);
131 T_0_15( 4, B, C, D, E, A);
132 T_0_15( 5, A, B, C, D, E);
133 T_0_15( 6, E, A, B, C, D);
134 T_0_15( 7, D, E, A, B, C);
135 T_0_15( 8, C, D, E, A, B);
136 T_0_15( 9, B, C, D, E, A);
137 T_0_15(10, A, B, C, D, E);
138 T_0_15(11, E, A, B, C, D);
139 T_0_15(12, D, E, A, B, C);
140 T_0_15(13, C, D, E, A, B);
141 T_0_15(14, B, C, D, E, A);
142 T_0_15(15, A, B, C, D, E);
143
144 /* Round 1 - tail. Input from 512-bit mixing array */
145 T_16_19(16, E, A, B, C, D);
146 T_16_19(17, D, E, A, B, C);
147 T_16_19(18, C, D, E, A, B);
148 T_16_19(19, B, C, D, E, A);
149
150 /* Round 2 */
151 T_20_39(20, A, B, C, D, E);
152 T_20_39(21, E, A, B, C, D);
153 T_20_39(22, D, E, A, B, C);
154 T_20_39(23, C, D, E, A, B);
155 T_20_39(24, B, C, D, E, A);
156 T_20_39(25, A, B, C, D, E);
157 T_20_39(26, E, A, B, C, D);
158 T_20_39(27, D, E, A, B, C);
159 T_20_39(28, C, D, E, A, B);
160 T_20_39(29, B, C, D, E, A);
161 T_20_39(30, A, B, C, D, E);
162 T_20_39(31, E, A, B, C, D);
163 T_20_39(32, D, E, A, B, C);
164 T_20_39(33, C, D, E, A, B);
165 T_20_39(34, B, C, D, E, A);
166 T_20_39(35, A, B, C, D, E);
167 T_20_39(36, E, A, B, C, D);
168 T_20_39(37, D, E, A, B, C);
169 T_20_39(38, C, D, E, A, B);
170 T_20_39(39, B, C, D, E, A);
171
172 /* Round 3 */
173 T_40_59(40, A, B, C, D, E);
174 T_40_59(41, E, A, B, C, D);
175 T_40_59(42, D, E, A, B, C);
176 T_40_59(43, C, D, E, A, B);
177 T_40_59(44, B, C, D, E, A);
178 T_40_59(45, A, B, C, D, E);
179 T_40_59(46, E, A, B, C, D);
180 T_40_59(47, D, E, A, B, C);
181 T_40_59(48, C, D, E, A, B);
182 T_40_59(49, B, C, D, E, A);
183 T_40_59(50, A, B, C, D, E);
184 T_40_59(51, E, A, B, C, D);
185 T_40_59(52, D, E, A, B, C);
186 T_40_59(53, C, D, E, A, B);
187 T_40_59(54, B, C, D, E, A);
188 T_40_59(55, A, B, C, D, E);
189 T_40_59(56, E, A, B, C, D);
190 T_40_59(57, D, E, A, B, C);
191 T_40_59(58, C, D, E, A, B);
192 T_40_59(59, B, C, D, E, A);
193
194 /* Round 4 */
195 T_60_79(60, A, B, C, D, E);
196 T_60_79(61, E, A, B, C, D);
197 T_60_79(62, D, E, A, B, C);
198 T_60_79(63, C, D, E, A, B);
199 T_60_79(64, B, C, D, E, A);
200 T_60_79(65, A, B, C, D, E);
201 T_60_79(66, E, A, B, C, D);
202 T_60_79(67, D, E, A, B, C);
203 T_60_79(68, C, D, E, A, B);
204 T_60_79(69, B, C, D, E, A);
205 T_60_79(70, A, B, C, D, E);
206 T_60_79(71, E, A, B, C, D);
207 T_60_79(72, D, E, A, B, C);
208 T_60_79(73, C, D, E, A, B);
209 T_60_79(74, B, C, D, E, A);
210 T_60_79(75, A, B, C, D, E);
211 T_60_79(76, E, A, B, C, D);
212 T_60_79(77, D, E, A, B, C);
213 T_60_79(78, C, D, E, A, B);
214 T_60_79(79, B, C, D, E, A);
215
216 ctx->H[0] += A;
217 ctx->H[1] += B;
218 ctx->H[2] += C;
219 ctx->H[3] += D;
220 ctx->H[4] += E;
221 }