forked from TrenchBoot/secure-kernel-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha1sum.c
216 lines (181 loc) · 5.32 KB
/
sha1sum.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* sha1sum.c - print SHA-1 Message-Digest Algorithm
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
* Copyright (C) 2004 g10 Code GmbH
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* SHA-1 coden take from gnupg 1.3.92.
Note, that this is a simple tool to be used for MS Windows.
*/
#include <byteswap.h>
#include <defs.h>
#include <types.h>
#include <errno-base.h>
#include <sha1sum.h>
#include <string.h>
static inline u32 rol( u32 x, int n)
{
return (x << n) | (x >> (-n & 31));
}
typedef struct {
u32 count;
union {
struct {
u32 h0, h1, h2, h3, h4;
};
u32 h[5];
};
unsigned char buf[64];
} SHA1_CONTEXT;
static void sha1_init( SHA1_CONTEXT *hd )
{
*hd = (SHA1_CONTEXT){
.h0 = 0x67452301,
.h1 = 0xefcdab89,
.h2 = 0x98badcfe,
.h3 = 0x10325476,
.h4 = 0xc3d2e1f0,
};
}
static u32 sha1_blend(u32 *x, unsigned int i)
{
#define X(i) x[(i) & 15]
return X(i) = rol(X(i) ^ X(i - 14) ^ X(i - 8) ^ X(i - 3), 1);
#undef X
}
/****************
* Transform the message X which consists of 16 32-bit-words
*/
static void sha1_transform(SHA1_CONTEXT *hd, const void *_data)
{
const u32 *data = _data;
u32 a,b,c,d,e;
u32 x[16];
int i;
/* get values from the chaining vars */
a = hd->h0;
b = hd->h1;
c = hd->h2;
d = hd->h3;
e = hd->h4;
for ( i = 0; i < 16; ++i )
x[i] = cpu_to_be32(data[i]);
#define K1 0x5A827999L
#define K2 0x6ED9EBA1L
#define K3 0x8F1BBCDCL
#define K4 0xCA62C1D6L
#define F1(x,y,z) ( z ^ ( x & ( y ^ z ) ) )
#define F2(x,y,z) ( x ^ y ^ z )
#define F3(x,y,z) ( ( x & y ) | ( z & ( x | y ) ) )
#define F4(x,y,z) ( x ^ y ^ z )
#define M(i) sha1_blend(x, i)
#define R(a,b,c,d,e,f,k,m) do { e += rol( a, 5 ) \
+ f( b, c, d ) \
+ k \
+ m; \
b = rol( b, 30 ); \
} while(0)
for ( i = 0; i < 15; i += 5 )
{
R(a, b, c, d, e, F1, K1, x[i + 0]);
R(e, a, b, c, d, F1, K1, x[i + 1]);
R(d, e, a, b, c, F1, K1, x[i + 2]);
R(c, d, e, a, b, F1, K1, x[i + 3]);
R(b, c, d, e, a, F1, K1, x[i + 4]);
}
R( a, b, c, d, e, F1, K1, x[15] );
R( e, a, b, c, d, F1, K1, M(16) );
R( d, e, a, b, c, F1, K1, M(17) );
R( c, d, e, a, b, F1, K1, M(18) );
R( b, c, d, e, a, F1, K1, M(19) );
for ( i = 20; i < 40; i += 5 )
{
R(a, b, c, d, e, F2, K2, M(i + 0));
R(e, a, b, c, d, F2, K2, M(i + 1));
R(d, e, a, b, c, F2, K2, M(i + 2));
R(c, d, e, a, b, F2, K2, M(i + 3));
R(b, c, d, e, a, F2, K2, M(i + 4));
}
for ( ; i < 60; i += 5 )
{
R(a, b, c, d, e, F3, K3, M(i + 0));
R(e, a, b, c, d, F3, K3, M(i + 1));
R(d, e, a, b, c, F3, K3, M(i + 2));
R(c, d, e, a, b, F3, K3, M(i + 3));
R(b, c, d, e, a, F3, K3, M(i + 4));
}
for ( ; i < 80; i += 5 )
{
R(a, b, c, d, e, F4, K4, M(i + 0));
R(e, a, b, c, d, F4, K4, M(i + 1));
R(d, e, a, b, c, F4, K4, M(i + 2));
R(c, d, e, a, b, F4, K4, M(i + 3));
R(b, c, d, e, a, F4, K4, M(i + 4));
}
/* Update chaining vars */
hd->h0 += a;
hd->h1 += b;
hd->h2 += c;
hd->h3 += d;
hd->h4 += e;
}
static void sha1_once(SHA1_CONTEXT *hd, const void *data, u32 len)
{
hd->count = len;
for ( ; len >= 64; data += 64, len -= 64 )
sha1_transform(hd, data);
memcpy(hd->buf, data, len);
}
/* The routine final terminates the computation and
* returns the digest.
* The handle is prepared for a new cycle, but adding bytes to the
* handle will the destroy the returned buffer.
* Returns: 20 bytes representing the digest.
*/
static void
sha1_final(SHA1_CONTEXT *hd, u8 hash[SHA1_DIGEST_SIZE])
{
unsigned int partial = hd->count & 0x3f;
/* Start padding */
hd->buf[partial++] = 0x80;
if ( partial > 56 )
{
/* Need one extra block - pad to 64 */
memset(hd->buf + partial, 0, 64 - partial);
sha1_transform(hd, hd->buf);
partial = 0;
}
/* Pad to 56 */
memset(hd->buf + partial, 0, 56 - partial);
/* append the 64 bit count */
u64 *count = (void *)&hd->buf[56];
*count = cpu_to_be64((u64)hd->count << 3);
sha1_transform(hd, hd->buf);
u32 *p = (void *)hash;
for ( int i = 0; i < 5; ++i )
p[i] = be32_to_cpu(hd->h[i]);
}
void sha1sum(u8 hash[static SHA1_DIGEST_SIZE], const void *ptr, u32 len)
{
SHA1_CONTEXT ctx;
sha1_init(&ctx);
sha1_once(&ctx, ptr, len);
sha1_final(&ctx, hash);
}
/*
Local Variables:
compile-command: "cc -Wall -g -o sha1sum sha1sum.c"
End:
*/