-
Notifications
You must be signed in to change notification settings - Fork 909
/
sha.c
251 lines (218 loc) · 5.46 KB
/
sha.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* 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 of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Gabber
* Copyright (C) 1999-2000 Dave Smith & Julian Missig
*
*/
/*! \file
* \ingroup xmpp
*/
/*
Implements the Secure Hash Algorithm (SHA1)
Copyright (C) 1999 Scott G. Miller
Released under the terms of the GNU General Public License v2
see file COPYING for details
Credits:
Robert Klep <robert@ilse.nl> -- Expansion function fix
Thomas "temas" Muldowney <temas@box5.net>:
-- shahash() for string fun
-- Will add the int32 stuff in a few
---
FIXME: This source takes int to be a 32 bit integer. This
may vary from system to system. I'd use autoconf if I was familiar
with it. Anyone want to help me out?
*/
//#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include "../../core/endianness.h"
#ifndef MACOS
#include <sys/stat.h>
#include <sys/types.h>
#endif
#ifndef WIN32
#include <unistd.h>
#define INT64 long long
#else
#define snprintf _snprintf
#define INT64 __int64
#endif
#define switch_endianness(x) \
(x << 24 & 0xff000000) | (x << 8 & 0x00ff0000) | (x >> 8 & 0x0000ff00) \
| (x >> 24 & 0x000000ff)
/* Initial hash values */
#define Ai 0x67452301
#define Bi 0xefcdab89
#define Ci 0x98badcfe
#define Di 0x10325476
#define Ei 0xc3d2e1f0
/* SHA1 round constants */
#define K1 0x5a827999
#define K2 0x6ed9eba1
#define K3 0x8f1bbcdc
#define K4 0xca62c1d6
/* Round functions. Note that f2() is used in both rounds 2 and 4 */
#define f1(B, C, D) ((B & C) | ((~B) & D))
#define f2(B, C, D) (B ^ C ^ D)
#define f3(B, C, D) ((B & C) | (B & D) | (C & D))
/* left circular shift functions (rotate left) */
#define rol1(x) ((x << 1) | ((x >> 31) & 1))
#define rol5(A) ((A << 5) | ((A >> 27) & 0x1f))
#define rol30(B) ((B << 30) | ((B >> 2) & 0x3fffffff))
/*
Hashes 'data', which should be a pointer to 512 bits of data (sixteen
32 bit ints), into the ongoing 160 bit hash value (five 32 bit ints)
'hash'
*/
int sha_hash(int *data, int *hash)
{
int W[80];
unsigned int A = hash[0], B = hash[1], C = hash[2], D = hash[3],
E = hash[4];
unsigned int t, x, TEMP;
for(t = 0; t < 16; t++) {
#ifndef __IS_BIG_ENDIAN
W[t] = switch_endianness(data[t]);
#else
W[t] = data[t];
#endif
}
/* SHA1 Data expansion */
for(t = 16; t < 80; t++) {
x = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
W[t] = rol1(x);
}
/* SHA1 main loop (t=0 to 79)
This is broken down into four subloops in order to use
the correct round function and constant */
for(t = 0; t < 20; t++) {
TEMP = rol5(A) + f1(B, C, D) + E + W[t] + K1;
E = D;
D = C;
C = rol30(B);
B = A;
A = TEMP;
}
for(; t < 40; t++) {
TEMP = rol5(A) + f2(B, C, D) + E + W[t] + K2;
E = D;
D = C;
C = rol30(B);
B = A;
A = TEMP;
}
for(; t < 60; t++) {
TEMP = rol5(A) + f3(B, C, D) + E + W[t] + K3;
E = D;
D = C;
C = rol30(B);
B = A;
A = TEMP;
}
for(; t < 80; t++) {
TEMP = rol5(A) + f2(B, C, D) + E + W[t] + K4;
E = D;
D = C;
C = rol30(B);
B = A;
A = TEMP;
}
hash[0] += A;
hash[1] += B;
hash[2] += C;
hash[3] += D;
hash[4] += E;
return 0;
}
/*
Takes a pointer to a 160 bit block of data (five 32 bit ints) and
initializes it to the start constants of the SHA1 algorithm. This
must be called before using hash in the call to sha_hash
*/
int sha_init(int *hash)
{
hash[0] = Ai;
hash[1] = Bi;
hash[2] = Ci;
hash[3] = Di;
hash[4] = Ei;
return 0;
}
int strprintsha(char *dest, int *hashval)
{
int x;
char *hashstr = dest;
for(x = 0; x < 5; x++) {
snprintf(hashstr, 9, "%08x", hashval[x]);
hashstr += 8;
}
/*old way */
//snprintf(hashstr++, 1, "\0");
/*new way - by bogdan*/
*hashstr = 0;
return 0;
}
char *shahash(const char *str)
{
char read_buffer[65];
//int read_buffer[64];
int c = 1, i;
INT64 length = 0;
int strsz;
static char final[40];
int *hashval;
hashval = (int *)malloc(20);
sha_init(hashval);
strsz = strlen(str);
if(strsz == 0) {
memset(read_buffer, 0, 65);
read_buffer[0] = 0x80;
sha_hash((int *)read_buffer, hashval);
}
while(strsz > 0) {
memset(read_buffer, 0, 65);
strncpy((char *)read_buffer, str, 64);
c = strlen((char *)read_buffer);
length += c;
strsz -= c;
if(strsz <= 0) {
length <<= 3;
read_buffer[c] = (char)0x80;
for(i = c + 1; i < 64; i++)
read_buffer[i] = 0;
if(c > 55) {
/* we need to do an entire new block */
sha_hash((int *)read_buffer, hashval);
for(i = 0; i < 14; i++)
((int *)read_buffer)[i] = 0;
}
#ifndef __IS_BIG_ENDIAN
for(i = 0; i < 8; i++) {
read_buffer[56 + i] = (char)(length >> (56 - (i * 8))) & 0xff;
}
#else
memcpy(read_buffer + 56, &length, 8);
#endif
}
sha_hash((int *)read_buffer, hashval);
str += 64;
}
strprintsha((char *) final, hashval);
free(hashval);
return (char *) final;
}