summaryrefslogtreecommitdiffstats
path: root/src/sha.c
blob: 04d566e8d309916024407035be76cb0e609fe801 (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
/* iksemel (XML parser for Jabber)
** Copyright (C) 2000-2003 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
*/

#include "common.h"
#include "iksemel.h"

static void sha_buffer (iksha *sha, const unsigned char *data, int len);
static void sha_calculate (iksha *sha);

struct iksha_struct {
	unsigned int hash[5];
	unsigned int buf[80];
	int blen;
	unsigned int lenhi, lenlo;
};

iksha *
iks_sha_new (void)
{
	iksha *sha;

	sha = iks_malloc (sizeof (iksha));
	if (!sha) return NULL;
	iks_sha_reset (sha);
	return sha;
}

void
iks_sha_reset (iksha *sha)
{
	memset (sha, 0, sizeof (iksha));
	sha->hash[0] = 0x67452301;
	sha->hash[1] = 0xefcdab89;
	sha->hash[2] = 0x98badcfe;
	sha->hash[3] = 0x10325476;
	sha->hash[4] = 0xc3d2e1f0;
}

void
iks_sha_hash (iksha *sha, const unsigned char *data, size_t len, int finish)
{
	unsigned char pad[8];
	unsigned char padc;

	if (data && len != 0) sha_buffer (sha, data, len);
	if (!finish) return;

	pad[0] = (unsigned char)((sha->lenhi >> 24) & 0xff);
	pad[1] = (unsigned char)((sha->lenhi >> 16) & 0xff);
	pad[2] = (unsigned char)((sha->lenhi >> 8) & 0xff);
	pad[3] = (unsigned char)(sha->lenhi & 0xff);
	pad[4] = (unsigned char)((sha->lenlo >> 24) & 0xff);
	pad[5] = (unsigned char)((sha->lenlo >> 16) & 0xff);
	pad[6] = (unsigned char)((sha->lenlo >> 8) & 0xff);
	pad[7] = (unsigned char)(sha->lenlo & 255);

	padc = 0x80;
	sha_buffer (sha, &padc, 1);

	padc = 0x00;
	while (sha->blen != 56)
		sha_buffer (sha, &padc, 1);

	sha_buffer (sha, pad, 8);
}

void
iks_sha_print (iksha *sha, char *hash)
{
	int i;

	for (i=0; i<5; i++)
	{
		sprintf (hash, "%08x", sha->hash[i]);
		hash += 8;
	}
}

void
iks_sha_delete (iksha *sha)
{
	iks_free (sha);
}

void
iks_sha (const char *data, char *hash)
{
	iksha *sha;

	sha = iks_sha_new ();
	iks_sha_hash (sha, (const unsigned char*)data, strlen (data), 1);
	iks_sha_print (sha, hash);
	iks_free (sha);
}

static void
sha_buffer (iksha *sha, const unsigned char *data, int len)
{
	int i;

	for (i=0; i<len; i++) {
		sha->buf[sha->blen / 4] <<= 8;
		sha->buf[sha->blen / 4] |= (unsigned int)data[i];
		if ((++sha->blen) % 64 == 0) {
			sha_calculate (sha);
			sha->blen = 0;
		}
		sha->lenlo += 8;
		sha->lenhi += (sha->lenlo < 8);
	}
}

#define SRL(x,y) (((x) << (y)) | ((x) >> (32-(y))))
#define SHA(a,b,f,c) \
	for (i= (a) ; i<= (b) ; i++) { \
		TMP = SRL(A,5) + ( (f) ) + E + sha->buf[i] + (c) ; \
		E = D; \
		D = C; \
		C = SRL(B,30); \
		B = A; \
		A = TMP; \
	}

static void
sha_calculate (iksha *sha)
{
	int i;
	unsigned int A, B, C, D, E, TMP;

	for (i=16; i<80; i++)
		sha->buf[i] = SRL (sha->buf[i-3] ^ sha->buf[i-8] ^ sha->buf[i-14] ^ sha->buf[i-16], 1);

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

	SHA (0,  19, ((C^D)&B)^D,     0x5a827999);
	SHA (20, 39, B^C^D,           0x6ed9eba1);
	SHA (40, 59, (B&C)|(D&(B|C)), 0x8f1bbcdc);
	SHA (60, 79, B^C^D,           0xca62c1d6);

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