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