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