summaryrefslogtreecommitdiffstats
path: root/sha1/sha1.c
blob: 089ca3a5a1001c87e1d828dad00e1dcec189d586 (plain)
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
/*
** Copyright 2001 Double Precision, Inc.
** See COPYING for distribution information.
*/

#define	SHA1_INTERNAL
#include	"sha1.h"

#include	<string.h>
#include	<stdlib.h>


#define	K0 0x5A827999
#define	K1 0x6ED9EBA1
#define	K2 0x8F1BBCDC
#define	K3 0XCA62C1D6

#define	K20(x)	x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x

static SHA1_WORD K[80] = { K20(K0), K20(K1), K20(K2), K20(K3) };

void sha1_context_init(struct SHA1_CONTEXT *c)
{
	if (sizeof(SHA1_WORD) != 4)
		abort();

	c->H[0] = 0x67452301;
	c->H[1] = 0xEFCDAB89;
	c->H[2] = 0x98BADCFE;
	c->H[3] = 0x10325476;
	c->H[4] = 0xC3D2E1F0;
	c->blk_ptr=0;
}

void sha1_context_hash(struct SHA1_CONTEXT *c,
		const unsigned char blk[SHA1_BLOCK_SIZE])
{
SHA1_WORD	A,B,C,D,E;
SHA1_WORD	TEMP;
SHA1_WORD	W[80];
unsigned	i, t;

#define f(t,B,C,D)	( \
	(t) < 20 ? ( (B) & (C) ) | ( (~(B)) & (D) ) : \
	(t) >= 40 && (t) < 60 ? ( (B) & (C) ) | ( (B) & (D) ) | ( (C) & (D) ):\
		(B) ^ (C) ^ (D) )

#define S(a,b) ( ((SHA1_WORD)(a) << (b)) | ((SHA1_WORD)(a) >> (32 - (b))))

	for (i=t=0; t<16; t++)
	{
		W[t]= blk[i]; i++;
		W[t] = (W[t] << 8) | blk[i]; i++;
		W[t] = (W[t] << 8) | blk[i]; i++;
		W[t] = (W[t] << 8) | blk[i]; i++;
	}

	for (t=16; t<80; t++)
	{
		TEMP= W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
		W[t]= S(TEMP, 1);
	}

	A=c->H[0];
	B=c->H[1];
	C=c->H[2];
	D=c->H[3];
	E=c->H[4];

	for (t=0; t<80; t++)
	{
		TEMP = S(A,5);
		TEMP += f(t, B, C, D);
		TEMP += E;
		TEMP += W[t];
		TEMP += K[t];

		E=D;
		D=C;
		C= S(B, 30);
		B=A;
		A=TEMP;
	}

	c->H[0] += A;
	c->H[1] += B;
	c->H[2] += C;
	c->H[3] += D;
	c->H[4] += E;
}

void sha1_context_hashstream(struct SHA1_CONTEXT *c, const void *p, unsigned l)
{
const unsigned char *cp=(const unsigned char *)p;
unsigned ll;

	while (l)
	{
		if (c->blk_ptr == 0 && l >= SHA1_BLOCK_SIZE)
		{
			sha1_context_hash(c, cp);
			cp += SHA1_BLOCK_SIZE;
			l -= SHA1_BLOCK_SIZE;
			continue;
		}

		ll=l;
		if (ll > SHA1_BLOCK_SIZE - c->blk_ptr)
			ll=SHA1_BLOCK_SIZE - c->blk_ptr;
		memcpy(c->blk + c->blk_ptr, cp, ll);
		c->blk_ptr += ll;
		cp += ll;
		l -= ll;
		if (c->blk_ptr >= SHA1_BLOCK_SIZE)
		{
			sha1_context_hash(c, c->blk);
			c->blk_ptr=0;
		}
	}
}

void sha1_context_endstream(struct SHA1_CONTEXT *c, unsigned long l)
{
	unsigned char buf[8];
	static const unsigned char zero[SHA1_BLOCK_SIZE-8];

	buf[0]=0x80;
	sha1_context_hashstream(c, &buf, 1);
	while (c->blk_ptr != SHA1_BLOCK_SIZE-8)
	{
		if (c->blk_ptr > SHA1_BLOCK_SIZE-8)
		{
			sha1_context_hashstream(c, zero,
				SHA1_BLOCK_SIZE - c->blk_ptr);
			continue;
		}
		sha1_context_hashstream(c, zero,
			SHA1_BLOCK_SIZE-8-c->blk_ptr);
	}

	l *= 8;
	buf[7] = l;
	buf[6] = (l >>= 8);
	buf[5] = (l >>= 8);
	buf[4] = (l >> 8);
	buf[3]=buf[2]=buf[1]=buf[0]=0;

	sha1_context_hashstream(c, buf, 8);
}

void sha1_context_digest(struct SHA1_CONTEXT *c, SHA1_DIGEST d)
{
unsigned char *dp=d + SHA1_DIGEST_SIZE;
unsigned i;

	for ( i=5; i; )
	{
	SHA1_WORD	w=c->H[--i];

		*--dp=w; w >>= 8;
		*--dp=w; w >>= 8;
		*--dp=w; w >>= 8;
		*--dp=w;
	}
}

void sha1_context_restore(struct SHA1_CONTEXT *c, const SHA1_DIGEST d)
{
const unsigned char *dp=d;
unsigned i;

	for (i=0; i<5; i++)
	{
	SHA1_WORD	w= *dp++;

		w=(w << 8) | *dp++;
		w=(w << 8) | *dp++;
		w=(w << 8) | *dp++;
		c->H[i]=w;
	}
	c->blk_ptr=0;
}

void sha1_digest(const void *msg, unsigned len, SHA1_DIGEST d)
{
struct SHA1_CONTEXT c;

	sha1_context_init( &c );
	sha1_context_hashstream(&c, msg, len);
	sha1_context_endstream(&c, len);
	sha1_context_digest( &c, d );
}