aboutsummaryrefslogtreecommitdiff
path: root/src/sha1.cpp
blob: 1d6ae76f55f2af9c61253f0e61928f8949a33943 (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
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

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

#include "bu/sha1.h"

Sha1::Sha1() :
	H0( 0x67452301 ),
	H1( 0xefcdab89 ),
	H2( 0x98badcfe ),
	H3( 0x10325476 ),
	H4( 0xc3d2e1f0 ),
	unprocessedBytes( 0 ),
	size( 0 )
{
}

Sha1::~Sha1()
{
}

void Sha1::process()
{
	int t;
	uint32_t a, b, c, d, e, K, f, W[80];

	// starting values
	a = H0;
	b = H1;
	c = H2;
	d = H3;
	e = H4;

	// copy and expand the message block
	for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24)
									+(bytes[t*4 + 1] << 16)
									+(bytes[t*4 + 2] << 8)
									+ bytes[t*4 + 3];
	for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 );
	
	/* main loop */
	uint32_t temp;
	for( t = 0; t < 80; t++ )
	{
		if( t < 20 ) {
			K = 0x5a827999;
			f = (b & c) | ((~b) & d);
		} else if( t < 40 ) {
			K = 0x6ed9eba1;
			f = b ^ c ^ d;
		} else if( t < 60 ) {
			K = 0x8f1bbcdc;
			f = (b & c) | (b & d) | (c & d);
		} else {
			K = 0xca62c1d6;
			f = b ^ c ^ d;
		}
		temp = lrot(a,5) + f + e + W[t] + K;
		e = d;
		d = c;
		c = lrot(b,30);
		b = a;
		a = temp;
		//printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
	}

	/* add variables */
	H0 += a;
	H1 += b;
	H2 += c;
	H3 += d;
	H4 += e;

	//printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
	/* all bytes have been processed */
	unprocessedBytes = 0;
}

void Sha1::update( const char* data, int num )
{
	// add these bytes to the running total
	size += num;

	// repeat until all data is processed
	while( num > 0 )
	{
		// number of bytes required to complete block
		int needed = 64 - unprocessedBytes;

		// number of bytes to copy (use smaller of two)
		int toCopy = (num < needed) ? num : needed;

		// Copy the bytes
		memcpy( bytes + unprocessedBytes, data, toCopy );

		// Bytes have been copied
		num -= toCopy;
		data += toCopy;
		unprocessedBytes += toCopy;
		
		// there is a full block
		if( unprocessedBytes == 64 ) process();
	}
}

unsigned char* Sha1::getDigest()
{
	// save the message size
	uint32_t totalBitsL = size << 3;
	uint32_t totalBitsH = size >> 29;

	// add 0x80 to the message
	update( "\x80", 1 );
	
	unsigned char footer[64] = {
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

	// block has no room for 8-byte filesize, so finish it
	if( unprocessedBytes > 56 )
		update( (char*)footer, 64 - unprocessedBytes);

	// how many zeros do we need
	int neededZeros = 56 - unprocessedBytes;

	// store file size (in bits) in big-endian format
	toBigEndian( totalBitsH, footer + neededZeros );
	toBigEndian( totalBitsL, footer + neededZeros + 4 );

	// finish the final block
	update( (char*)footer, neededZeros + 8 );

	// allocate memory for the digest bytes
	unsigned char* digest = new unsigned char[20];

	// copy the digest bytes
	toBigEndian( H0, digest );
	toBigEndian( H1, digest + 4 );
	toBigEndian( H2, digest + 8 );
	toBigEndian( H3, digest + 12 );
	toBigEndian( H4, digest + 16 );

	// return the digest
	return digest;
}

uint32_t Sha1::lrot( uint32_t x, int bits )
{
	return (x<<bits) | (x>>(32 - bits));
};

void Sha1::toBigEndian( uint32_t num, unsigned char* byte )
{
	byte[0] = (unsigned char)(num>>24);
	byte[1] = (unsigned char)(num>>16);
	byte[2] = (unsigned char)(num>>8);
	byte[3] = (unsigned char)num;
}