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