1 /*
2  * Based on the git SHA1 Implementation.
3  *
4  * Copyright (C) 2009-2015, Linus Torvalds and others.
5  *
6  * SHA1 routine optimized to do word accesses rather than byte accesses,
7  * and to avoid unnecessary copies into the context array.
8  *
9  * This was initially based on the Mozilla SHA1 implementation, although
10  * none of the original Mozilla code remains.
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation, version 2.1
15  * exclusively.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
25  */
26 
27 /* this is only to get definitions for memcpy(), ntohl() and htonl() */
28 #include <string.h>
29 #include <inttypes.h>
30 #include <arpa/inet.h>
31 
32 #include <import/sha1.h>
33 
34 /*
35  * Performance might be improved if the CPU architecture is OK with
36  * unaligned 32-bit loads and a fast ntohl() is available.
37  * Otherwise fall back to byte loads and shifts which is portable,
38  * and is faster on architectures with memory alignment issues.
39  */
40 
41 #if defined(__i386__) || defined(__x86_64__) || \
42     defined(__ppc__) || defined(__ppc64__) || \
43     defined(__powerpc__) || defined(__powerpc64__) || \
44     defined(__s390__) || defined(__s390x__)
45 
46 #define get_be32(p)	ntohl(*(unsigned int *)(p))
47 #define put_be32(p, v)	do { *(unsigned int *)(p) = htonl(v); } while (0)
48 
49 #else
50 
get_be32(const void * ptr)51 static inline uint32_t get_be32(const void *ptr)
52 {
53 	const unsigned char *p = ptr;
54 	return	(uint32_t)p[0] << 24 |
55 		(uint32_t)p[1] << 16 |
56 		(uint32_t)p[2] <<  8 |
57 		(uint32_t)p[3] <<  0;
58 }
59 
put_be32(void * ptr,uint32_t value)60 static inline void put_be32(void *ptr, uint32_t value)
61 {
62 	unsigned char *p = ptr;
63 	p[0] = value >> 24;
64 	p[1] = value >> 16;
65 	p[2] = value >>  8;
66 	p[3] = value >>  0;
67 }
68 
69 #endif
70 
71 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
72 
73 /*
74  * Force usage of rol or ror by selecting the one with the smaller constant.
75  * It _can_ generate slightly smaller code (a constant of 1 is special), but
76  * perhaps more importantly it's possibly faster on any uarch that does a
77  * rotate with a loop.
78  */
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 /*
93  * If you have 32 registers or more, the compiler can (and should)
94  * try to change the array[] accesses into registers. However, on
95  * machines with less than ~25 registers, that won't really work,
96  * and at least gcc will make an unholy mess of it.
97  *
98  * So to avoid that mess which just slows things down, we force
99  * the stores to memory to actually happen (we might be better off
100  * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
101  * suggested by Artur Skawina - that will also make gcc unable to
102  * try to do the silly "optimize away loads" part because it won't
103  * see what the value will be).
104  *
105  * Ben Herrenschmidt reports that on PPC, the C version comes close
106  * to the optimized asm with this (ie on PPC you don't want that
107  * 'volatile', since there are lots of registers).
108  *
109  * On ARM we get the best code generation by forcing a full memory barrier
110  * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
111  * the stack frame size simply explode and performance goes down the drain.
112  */
113 
114 #if defined(__i386__) || defined(__x86_64__)
115   #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
116 #elif defined(__GNUC__) && defined(__arm__)
117   #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
118 #else
119   #define setW(x, val) (W(x) = (val))
120 #endif
121 
122 /* This "rolls" over the 512-bit array */
123 #define W(x) (array[(x)&15])
124 
125 /*
126  * Where do we get the source from? The first 16 iterations get it from
127  * the input data, the next mix it from the 512-bit array.
128  */
129 #define SHA_SRC(t) get_be32((unsigned char *) block + (t)*4)
130 #define SHA_MIX(t) SHA_ROL(W((t)+13) ^ W((t)+8) ^ W((t)+2) ^ W(t), 1);
131 
132 #define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
133 	unsigned int TEMP = input(t); setW(t, TEMP); \
134 	E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
135 	B = SHA_ROR(B, 2); } while (0)
136 
137 #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 )
138 #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 )
139 #define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
140 #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 )
141 #define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) ,  0xca62c1d6, A, B, C, D, E )
142 
blk_SHA1_Block(blk_SHA_CTX * ctx,const void * block)143 static void blk_SHA1_Block(blk_SHA_CTX *ctx, const void *block)
144 {
145 	unsigned int A,B,C,D,E;
146 	unsigned int array[16];
147 
148 	A = ctx->H[0];
149 	B = ctx->H[1];
150 	C = ctx->H[2];
151 	D = ctx->H[3];
152 	E = ctx->H[4];
153 
154 	/* Round 1 - iterations 0-16 take their input from 'block' */
155 	T_0_15( 0, A, B, C, D, E);
156 	T_0_15( 1, E, A, B, C, D);
157 	T_0_15( 2, D, E, A, B, C);
158 	T_0_15( 3, C, D, E, A, B);
159 	T_0_15( 4, B, C, D, E, A);
160 	T_0_15( 5, A, B, C, D, E);
161 	T_0_15( 6, E, A, B, C, D);
162 	T_0_15( 7, D, E, A, B, C);
163 	T_0_15( 8, C, D, E, A, B);
164 	T_0_15( 9, B, C, D, E, A);
165 	T_0_15(10, A, B, C, D, E);
166 	T_0_15(11, E, A, B, C, D);
167 	T_0_15(12, D, E, A, B, C);
168 	T_0_15(13, C, D, E, A, B);
169 	T_0_15(14, B, C, D, E, A);
170 	T_0_15(15, A, B, C, D, E);
171 
172 	/* Round 1 - tail. Input from 512-bit mixing array */
173 	T_16_19(16, E, A, B, C, D);
174 	T_16_19(17, D, E, A, B, C);
175 	T_16_19(18, C, D, E, A, B);
176 	T_16_19(19, B, C, D, E, A);
177 
178 	/* Round 2 */
179 	T_20_39(20, A, B, C, D, E);
180 	T_20_39(21, E, A, B, C, D);
181 	T_20_39(22, D, E, A, B, C);
182 	T_20_39(23, C, D, E, A, B);
183 	T_20_39(24, B, C, D, E, A);
184 	T_20_39(25, A, B, C, D, E);
185 	T_20_39(26, E, A, B, C, D);
186 	T_20_39(27, D, E, A, B, C);
187 	T_20_39(28, C, D, E, A, B);
188 	T_20_39(29, B, C, D, E, A);
189 	T_20_39(30, A, B, C, D, E);
190 	T_20_39(31, E, A, B, C, D);
191 	T_20_39(32, D, E, A, B, C);
192 	T_20_39(33, C, D, E, A, B);
193 	T_20_39(34, B, C, D, E, A);
194 	T_20_39(35, A, B, C, D, E);
195 	T_20_39(36, E, A, B, C, D);
196 	T_20_39(37, D, E, A, B, C);
197 	T_20_39(38, C, D, E, A, B);
198 	T_20_39(39, B, C, D, E, A);
199 
200 	/* Round 3 */
201 	T_40_59(40, A, B, C, D, E);
202 	T_40_59(41, E, A, B, C, D);
203 	T_40_59(42, D, E, A, B, C);
204 	T_40_59(43, C, D, E, A, B);
205 	T_40_59(44, B, C, D, E, A);
206 	T_40_59(45, A, B, C, D, E);
207 	T_40_59(46, E, A, B, C, D);
208 	T_40_59(47, D, E, A, B, C);
209 	T_40_59(48, C, D, E, A, B);
210 	T_40_59(49, B, C, D, E, A);
211 	T_40_59(50, A, B, C, D, E);
212 	T_40_59(51, E, A, B, C, D);
213 	T_40_59(52, D, E, A, B, C);
214 	T_40_59(53, C, D, E, A, B);
215 	T_40_59(54, B, C, D, E, A);
216 	T_40_59(55, A, B, C, D, E);
217 	T_40_59(56, E, A, B, C, D);
218 	T_40_59(57, D, E, A, B, C);
219 	T_40_59(58, C, D, E, A, B);
220 	T_40_59(59, B, C, D, E, A);
221 
222 	/* Round 4 */
223 	T_60_79(60, A, B, C, D, E);
224 	T_60_79(61, E, A, B, C, D);
225 	T_60_79(62, D, E, A, B, C);
226 	T_60_79(63, C, D, E, A, B);
227 	T_60_79(64, B, C, D, E, A);
228 	T_60_79(65, A, B, C, D, E);
229 	T_60_79(66, E, A, B, C, D);
230 	T_60_79(67, D, E, A, B, C);
231 	T_60_79(68, C, D, E, A, B);
232 	T_60_79(69, B, C, D, E, A);
233 	T_60_79(70, A, B, C, D, E);
234 	T_60_79(71, E, A, B, C, D);
235 	T_60_79(72, D, E, A, B, C);
236 	T_60_79(73, C, D, E, A, B);
237 	T_60_79(74, B, C, D, E, A);
238 	T_60_79(75, A, B, C, D, E);
239 	T_60_79(76, E, A, B, C, D);
240 	T_60_79(77, D, E, A, B, C);
241 	T_60_79(78, C, D, E, A, B);
242 	T_60_79(79, B, C, D, E, A);
243 
244 	ctx->H[0] += A;
245 	ctx->H[1] += B;
246 	ctx->H[2] += C;
247 	ctx->H[3] += D;
248 	ctx->H[4] += E;
249 }
250 
blk_SHA1_Init(blk_SHA_CTX * ctx)251 void blk_SHA1_Init(blk_SHA_CTX *ctx)
252 {
253 	ctx->size = 0;
254 
255 	/* Initialize H with the magic constants (see FIPS180 for constants) */
256 	ctx->H[0] = 0x67452301;
257 	ctx->H[1] = 0xefcdab89;
258 	ctx->H[2] = 0x98badcfe;
259 	ctx->H[3] = 0x10325476;
260 	ctx->H[4] = 0xc3d2e1f0;
261 }
262 
blk_SHA1_Update(blk_SHA_CTX * ctx,const void * data,unsigned long len)263 void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
264 {
265 	unsigned int lenW = ctx->size & 63;
266 
267 	ctx->size += len;
268 
269 	/* Read the data into W and process blocks as they get full */
270 	if (lenW) {
271 		unsigned int left = 64 - lenW;
272 		if (len < left)
273 			left = len;
274 		memcpy(lenW + (char *)ctx->W, data, left);
275 		lenW = (lenW + left) & 63;
276 		len -= left;
277 		data = ((const char *)data + left);
278 		if (lenW)
279 			return;
280 		blk_SHA1_Block(ctx, ctx->W);
281 	}
282 	while (len >= 64) {
283 		blk_SHA1_Block(ctx, data);
284 		data = ((const char *)data + 64);
285 		len -= 64;
286 	}
287 	if (len)
288 		memcpy(ctx->W, data, len);
289 }
290 
blk_SHA1_Final(unsigned char hashout[20],blk_SHA_CTX * ctx)291 void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
292 {
293 	static const unsigned char pad[64] = { 0x80 };
294 	unsigned int padlen[2];
295 	int i;
296 
297 	/* Pad with a binary 1 (ie 0x80), then zeroes, then length */
298 	padlen[0] = htonl((uint32_t)(ctx->size >> 29));
299 	padlen[1] = htonl((uint32_t)(ctx->size << 3));
300 
301 	i = ctx->size & 63;
302 	blk_SHA1_Update(ctx, pad, 1 + (63 & (55 - i)));
303 	blk_SHA1_Update(ctx, padlen, 8);
304 
305 	/* Output hash */
306 	for (i = 0; i < 5; i++)
307 		put_be32(hashout + i * 4, ctx->H[i]);
308 }
309