Fix WinSock library version detection with MinGW-w64
[amule.git] / src / SHA.cpp
blob2d831a87114e8bebb1038dff4d5c7ad0ca85431b
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 Angel Vidal ( kry@amule.org )
5 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 // Kry - Modified version of the original SHA.cpp to work on linux and
26 // use wxWidgets. Original license follows.
30 ---------------------------------------------------------------------------
31 Copyright (c) 2002-2011 Dr Brian Gladman ( brg@gladman.me.uk )
32 All rights reserved.
34 LICENSE TERMS
36 The free distribution and use of this software in both source and binary
37 form is allowed (with or without changes) provided that:
39 1. distributions of this source code include the above copyright
40 notice, this list of conditions and the following disclaimer;
42 2. distributions in binary form include the above copyright
43 notice, this list of conditions and the following disclaimer
44 in the documentation and/or other associated materials;
46 3. the copyright holder's name is not used to endorse products
47 built using this software without specific written permission.
49 ALTERNATIVELY, provided that this notice is retained in full, this product
50 may be distributed under the terms of the GNU General Public License (GPL),
51 in which case the provisions of the GPL apply INSTEAD OF those given above.
53 DISCLAIMER
55 This software is provided 'as is' with no explicit or implied warranties
56 in respect of its properties, including, but not limited to, correctness
57 and/or fitness for purpose.
58 ---------------------------------------------------------------------------
59 Issue Date: 30/11/2002
61 This is a byte oriented version of SHA1 that operates on arrays of bytes
62 stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
65 #include "SHA.h"
68 CSHA::CSHA()
70 Reset();
74 To obtain the highest speed on processors with 32-bit words, this code
75 needs to determine the order in which bytes are packed into such words.
76 The following block of code is an attempt to capture the most obvious
77 ways in which various environemnts specify their endian definitions.
78 It may well fail, in which case the definitions will need to be set by
79 editing at the points marked **** EDIT HERE IF NECESSARY **** below.
81 #define SHA_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
82 #define SHA_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
84 #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
86 #if (wxBYTE_ORDER == wxBIG_ENDIAN)
87 #define swap_b32(x) (x)
88 #elif defined(bswap_32)
89 #define swap_b32(x) bswap_32(x)
90 #else
91 #define swap_b32(x) ((rotl32((x), 8) & 0x00ff00ff) | (rotl32((x), 24) & 0xff00ff00))
92 #endif
94 #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
96 /* reverse byte order in 32-bit words */
98 #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
99 #define parity(x,y,z) ((x) ^ (y) ^ (z))
100 #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
102 /* A normal version as set out in the FIPS. This version uses */
103 /* partial loop unrolling and is optimised for the Pentium 4 */
105 #define rnd(f,k) t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; e = d; d = c; c = rotl32(b, 30); b = t
107 void CSHA::Compile()
109 uint32 w[80], i, a, b, c, d, e, t;
111 /* note that words are compiled from the buffer into 32-bit */
112 /* words in big-endian order so an order reversal is needed */
113 /* here on little endian machines */
114 for(i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
115 w[i] = swap_b32(m_nBuffer[i]);
117 for(i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
118 w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
120 a = m_nHash[0];
121 b = m_nHash[1];
122 c = m_nHash[2];
123 d = m_nHash[3];
124 e = m_nHash[4];
126 for(i = 0; i < 20; ++i)
128 rnd(ch, 0x5a827999);
131 for(i = 20; i < 40; ++i)
133 rnd(parity, 0x6ed9eba1);
136 for(i = 40; i < 60; ++i)
138 rnd(maj, 0x8f1bbcdc);
141 for(i = 60; i < 80; ++i)
143 rnd(parity, 0xca62c1d6);
146 m_nHash[0] += a;
147 m_nHash[1] += b;
148 m_nHash[2] += c;
149 m_nHash[3] += d;
150 m_nHash[4] += e;
153 void CSHA::Reset()
155 m_nCount[0] = m_nCount[1] = 0;
156 m_nHash[0] = 0x67452301;
157 m_nHash[1] = 0xefcdab89;
158 m_nHash[2] = 0x98badcfe;
159 m_nHash[3] = 0x10325476;
160 m_nHash[4] = 0xc3d2e1f0;
163 void CSHA::GetHash(CAICHHash& Hash)
165 /* extract the hash value as bytes in case the hash buffer is */
166 /* misaligned for 32-bit words*/
168 wxASSERT( Hash.GetHashSize() == 20 );
169 for(int i = 0; i < SHA1_DIGEST_SIZE; ++i)
170 Hash.GetRawHash()[i] = (unsigned char)(m_nHash[i >> 2] >> 8 * (~i & 3));
173 /* SHA1 hash data in an array of bytes into hash buffer and call the */
174 /* hash_compile function as required. */
176 void CSHA::Add(const void* pData, uint32 nLength)
178 const unsigned char* data = (const unsigned char*)pData;
180 uint32 pos = (uint32)(m_nCount[0] & SHA1_MASK),
181 space = SHA1_BLOCK_SIZE - pos;
182 const unsigned char *sp = data;
184 if((m_nCount[0] += nLength) < nLength)
185 ++(m_nCount[1]);
187 while(nLength >= space) /* tranfer whole blocks while possible */
189 memcpy(((unsigned char*)m_nBuffer) + pos, sp, space);
190 sp += space; nLength -= space; space = SHA1_BLOCK_SIZE; pos = 0;
191 Compile();
194 memcpy(((unsigned char*)m_nBuffer) + pos, sp, nLength);
197 /* SHA1 final padding and digest calculation */
199 #if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
200 static uint32 mask[4] =
201 { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
202 static uint32 bits[4] =
203 { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
204 #else
205 static uint32 mask[4] =
206 { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
207 static uint32 bits[4] =
208 { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
209 #endif
211 void CSHA::Finish(CAICHHash& Hash)
213 uint32 i = (uint32)(m_nCount[0] & SHA1_MASK);
215 /* mask out the rest of any partial 32-bit word and then set */
216 /* the next byte to 0x80. On big-endian machines any bytes in */
217 /* the buffer will be at the top end of 32 bit words, on little */
218 /* endian machines they will be at the bottom. Hence the AND */
219 /* and OR masks above are reversed for little endian systems */
220 /* Note that we can always add the first padding byte at this */
221 /* because the buffer always contains at least one empty slot */
222 m_nBuffer[i >> 2] = (m_nBuffer[i >> 2] & mask[i & 3]) | bits[i & 3];
224 /* we need 9 or more empty positions, one for the padding byte */
225 /* (above) and eight for the length count. If there is not */
226 /* enough space pad and empty the buffer */
227 if(i > SHA1_BLOCK_SIZE - 9)
229 if(i < 60) m_nBuffer[15] = 0;
230 Compile();
231 i = 0;
233 else /* compute a word index for the empty buffer positions */
234 i = (i >> 2) + 1;
236 while(i < 14) /* and zero pad all but last two positions */
237 m_nBuffer[i++] = 0;
239 /* assemble the eight byte counter in in big-endian format */
240 m_nBuffer[14] = swap_b32((m_nCount[1] << 3) | (m_nCount[0] >> 29));
241 m_nBuffer[15] = swap_b32(m_nCount[0] << 3);
243 Compile();
244 GetHash(Hash);
246 // File_checked_for_headers