]> git.ipfire.org Git - thirdparty/git.git/blame - block-sha1/sha1.c
Sync with 2.35.8
[thirdparty/git.git] / block-sha1 / sha1.c
CommitLineData
d7c208a9 1/*
30ae47b4 2 * SHA1 routine optimized to do word accesses rather than byte accesses,
d7c208a9 3 * and to avoid unnecessary copies into the context array.
30ae47b4
NP
4 *
5 * This was initially based on the Mozilla SHA1 implementation, although
6 * none of the original Mozilla code remains.
d7c208a9
LT
7 */
8
51ea5519
NP
9/* this is only to get definitions for memcpy(), ntohl() and htonl() */
10#include "../git-compat-util.h"
d7c208a9
LT
11
12#include "sha1.h"
13
fd536d34 14#define SHA_ROT(X,l,r) (((X) << (l)) | ((X) >> (r)))
b8e48a89
LT
15#define SHA_ROL(X,n) SHA_ROT(X,n,32-(n))
16#define SHA_ROR(X,n) SHA_ROT(X,32-(n),n)
17
926172c5
LT
18/*
19 * If you have 32 registers or more, the compiler can (and should)
20 * try to change the array[] accesses into registers. However, on
21 * machines with less than ~25 registers, that won't really work,
22 * and at least gcc will make an unholy mess of it.
23 *
24 * So to avoid that mess which just slows things down, we force
25 * the stores to memory to actually happen (we might be better off
26 * with a 'W(t)=(val);asm("":"+m" (W(t))' there instead, as
27 * suggested by Artur Skawina - that will also make gcc unable to
28 * try to do the silly "optimize away loads" part because it won't
29 * see what the value will be).
30 *
31 * Ben Herrenschmidt reports that on PPC, the C version comes close
32 * to the optimized asm with this (ie on PPC you don't want that
33 * 'volatile', since there are lots of registers).
dc52fd29
NP
34 *
35 * On ARM we get the best code generation by forcing a full memory barrier
36 * between each SHA_ROUND, otherwise gcc happily get wild with spilling and
37 * the stack frame size simply explode and performance goes down the drain.
926172c5 38 */
dc52fd29
NP
39
40#if defined(__i386__) || defined(__x86_64__)
926172c5 41 #define setW(x, val) (*(volatile unsigned int *)&W(x) = (val))
e9c5dcd1 42#elif defined(__GNUC__) && defined(__arm__)
dc52fd29 43 #define setW(x, val) do { W(x) = (val); __asm__("":::"memory"); } while (0)
926172c5
LT
44#else
45 #define setW(x, val) (W(x) = (val))
46#endif
ab14c823 47
dc52fd29
NP
48/* This "rolls" over the 512-bit array */
49#define W(x) (array[(x)&15])
50
ab14c823
LT
51/*
52 * Where do we get the source from? The first 16 iterations get it from
53 * the input data, the next mix it from the 512-bit array.
54 */
23119ffb 55#define SHA_SRC(t) get_be32((unsigned char *) block + (t)*4)
3d8cbbf2 56#define SHA_MIX(t) SHA_ROL(W((t)+13) ^ W((t)+8) ^ W((t)+2) ^ W(t), 1)
ab14c823 57
30d12d4c 58#define SHA_ROUND(t, input, fn, constant, A, B, C, D, E) do { \
66c9c6c0
LT
59 unsigned int TEMP = input(t); setW(t, TEMP); \
60 E += TEMP + SHA_ROL(A,5) + (fn) + (constant); \
61 B = SHA_ROR(B, 2); } while (0)
ab14c823 62
30d12d4c
LT
63#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 )
64#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 )
65#define T_20_39(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0x6ed9eba1, A, B, C, D, E )
66#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 )
67#define T_60_79(t, A, B, C, D, E) SHA_ROUND(t, SHA_MIX, (B^C^D) , 0xca62c1d6, A, B, C, D, E )
ab14c823 68
5f6a1125 69static void blk_SHA1_Block(blk_SHA_CTX *ctx, const void *block)
d7c208a9 70{
30d12d4c 71 unsigned int A,B,C,D,E;
7b5075fc 72 unsigned int array[16];
d7c208a9 73
d7c208a9
LT
74 A = ctx->H[0];
75 B = ctx->H[1];
76 C = ctx->H[2];
77 D = ctx->H[3];
78 E = ctx->H[4];
79
5f6a1125 80 /* Round 1 - iterations 0-16 take their input from 'block' */
30d12d4c
LT
81 T_0_15( 0, A, B, C, D, E);
82 T_0_15( 1, E, A, B, C, D);
83 T_0_15( 2, D, E, A, B, C);
84 T_0_15( 3, C, D, E, A, B);
85 T_0_15( 4, B, C, D, E, A);
86 T_0_15( 5, A, B, C, D, E);
87 T_0_15( 6, E, A, B, C, D);
88 T_0_15( 7, D, E, A, B, C);
89 T_0_15( 8, C, D, E, A, B);
90 T_0_15( 9, B, C, D, E, A);
91 T_0_15(10, A, B, C, D, E);
92 T_0_15(11, E, A, B, C, D);
93 T_0_15(12, D, E, A, B, C);
94 T_0_15(13, C, D, E, A, B);
95 T_0_15(14, B, C, D, E, A);
96 T_0_15(15, A, B, C, D, E);
139e3456 97
ab14c823 98 /* Round 1 - tail. Input from 512-bit mixing array */
30d12d4c
LT
99 T_16_19(16, E, A, B, C, D);
100 T_16_19(17, D, E, A, B, C);
101 T_16_19(18, C, D, E, A, B);
102 T_16_19(19, B, C, D, E, A);
d7c208a9 103
ab14c823 104 /* Round 2 */
30d12d4c
LT
105 T_20_39(20, A, B, C, D, E);
106 T_20_39(21, E, A, B, C, D);
107 T_20_39(22, D, E, A, B, C);
108 T_20_39(23, C, D, E, A, B);
109 T_20_39(24, B, C, D, E, A);
110 T_20_39(25, A, B, C, D, E);
111 T_20_39(26, E, A, B, C, D);
112 T_20_39(27, D, E, A, B, C);
113 T_20_39(28, C, D, E, A, B);
114 T_20_39(29, B, C, D, E, A);
115 T_20_39(30, A, B, C, D, E);
116 T_20_39(31, E, A, B, C, D);
117 T_20_39(32, D, E, A, B, C);
118 T_20_39(33, C, D, E, A, B);
119 T_20_39(34, B, C, D, E, A);
120 T_20_39(35, A, B, C, D, E);
121 T_20_39(36, E, A, B, C, D);
122 T_20_39(37, D, E, A, B, C);
123 T_20_39(38, C, D, E, A, B);
124 T_20_39(39, B, C, D, E, A);
d7c208a9 125
ab14c823 126 /* Round 3 */
30d12d4c
LT
127 T_40_59(40, A, B, C, D, E);
128 T_40_59(41, E, A, B, C, D);
129 T_40_59(42, D, E, A, B, C);
130 T_40_59(43, C, D, E, A, B);
131 T_40_59(44, B, C, D, E, A);
132 T_40_59(45, A, B, C, D, E);
133 T_40_59(46, E, A, B, C, D);
134 T_40_59(47, D, E, A, B, C);
135 T_40_59(48, C, D, E, A, B);
136 T_40_59(49, B, C, D, E, A);
137 T_40_59(50, A, B, C, D, E);
138 T_40_59(51, E, A, B, C, D);
139 T_40_59(52, D, E, A, B, C);
140 T_40_59(53, C, D, E, A, B);
141 T_40_59(54, B, C, D, E, A);
142 T_40_59(55, A, B, C, D, E);
143 T_40_59(56, E, A, B, C, D);
144 T_40_59(57, D, E, A, B, C);
145 T_40_59(58, C, D, E, A, B);
146 T_40_59(59, B, C, D, E, A);
d7c208a9 147
ab14c823 148 /* Round 4 */
30d12d4c
LT
149 T_60_79(60, A, B, C, D, E);
150 T_60_79(61, E, A, B, C, D);
151 T_60_79(62, D, E, A, B, C);
152 T_60_79(63, C, D, E, A, B);
153 T_60_79(64, B, C, D, E, A);
154 T_60_79(65, A, B, C, D, E);
155 T_60_79(66, E, A, B, C, D);
156 T_60_79(67, D, E, A, B, C);
157 T_60_79(68, C, D, E, A, B);
158 T_60_79(69, B, C, D, E, A);
159 T_60_79(70, A, B, C, D, E);
160 T_60_79(71, E, A, B, C, D);
161 T_60_79(72, D, E, A, B, C);
162 T_60_79(73, C, D, E, A, B);
163 T_60_79(74, B, C, D, E, A);
164 T_60_79(75, A, B, C, D, E);
165 T_60_79(76, E, A, B, C, D);
166 T_60_79(77, D, E, A, B, C);
167 T_60_79(78, C, D, E, A, B);
168 T_60_79(79, B, C, D, E, A);
d7c208a9
LT
169
170 ctx->H[0] += A;
171 ctx->H[1] += B;
172 ctx->H[2] += C;
173 ctx->H[3] += D;
174 ctx->H[4] += E;
175}
30ba0de7
NP
176
177void blk_SHA1_Init(blk_SHA_CTX *ctx)
178{
179 ctx->size = 0;
180
181 /* Initialize H with the magic constants (see FIPS180 for constants) */
182 ctx->H[0] = 0x67452301;
183 ctx->H[1] = 0xefcdab89;
184 ctx->H[2] = 0x98badcfe;
185 ctx->H[3] = 0x10325476;
186 ctx->H[4] = 0xc3d2e1f0;
187}
188
9bb4542b 189void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, size_t len)
30ba0de7 190{
9eafa120 191 unsigned int lenW = ctx->size & 63;
30ba0de7
NP
192
193 ctx->size += len;
194
195 /* Read the data into W and process blocks as they get full */
196 if (lenW) {
9eafa120 197 unsigned int left = 64 - lenW;
30ba0de7
NP
198 if (len < left)
199 left = len;
200 memcpy(lenW + (char *)ctx->W, data, left);
201 lenW = (lenW + left) & 63;
202 len -= left;
a1221857 203 data = ((const char *)data + left);
30ba0de7
NP
204 if (lenW)
205 return;
206 blk_SHA1_Block(ctx, ctx->W);
207 }
208 while (len >= 64) {
209 blk_SHA1_Block(ctx, data);
a1221857 210 data = ((const char *)data + 64);
30ba0de7
NP
211 len -= 64;
212 }
213 if (len)
214 memcpy(ctx->W, data, len);
215}
216
217void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
218{
219 static const unsigned char pad[64] = { 0x80 };
220 unsigned int padlen[2];
221 int i;
222
223 /* Pad with a binary 1 (ie 0x80), then zeroes, then length */
9eafa120
RJ
224 padlen[0] = htonl((uint32_t)(ctx->size >> 29));
225 padlen[1] = htonl((uint32_t)(ctx->size << 3));
30ba0de7
NP
226
227 i = ctx->size & 63;
6b2dd0e5 228 blk_SHA1_Update(ctx, pad, 1 + (63 & (55 - i)));
30ba0de7
NP
229 blk_SHA1_Update(ctx, padlen, 8);
230
231 /* Output hash */
232 for (i = 0; i < 5; i++)
6b2dd0e5 233 put_be32(hashout + i * 4, ctx->H[i]);
30ba0de7 234}