aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/source/Irrlicht/aesGladman/sha1.cpp
diff options
context:
space:
mode:
authorDavid Walter Seikel2016-03-28 22:28:34 +1000
committerDavid Walter Seikel2016-03-28 22:28:34 +1000
commit7028cbe09c688437910a25623098762bf0fa592d (patch)
tree10b5af58277d9880380c2251f109325542c4e6eb /src/others/irrlicht-1.8.1/source/Irrlicht/aesGladman/sha1.cpp
parentMove lemon to the src/others directory. (diff)
downloadSledjHamr-7028cbe09c688437910a25623098762bf0fa592d.zip
SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.gz
SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.bz2
SledjHamr-7028cbe09c688437910a25623098762bf0fa592d.tar.xz
Move Irrlicht to src/others.
Diffstat (limited to 'src/others/irrlicht-1.8.1/source/Irrlicht/aesGladman/sha1.cpp')
-rw-r--r--src/others/irrlicht-1.8.1/source/Irrlicht/aesGladman/sha1.cpp237
1 files changed, 237 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/source/Irrlicht/aesGladman/sha1.cpp b/src/others/irrlicht-1.8.1/source/Irrlicht/aesGladman/sha1.cpp
new file mode 100644
index 0000000..8a91768
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/source/Irrlicht/aesGladman/sha1.cpp
@@ -0,0 +1,237 @@
1/*
2 ---------------------------------------------------------------------------
3 Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
4 All rights reserved.
5
6 LICENSE TERMS
7
8 The free distribution and use of this software in both source and binary
9 form is allowed (with or without changes) provided that:
10
11 1. distributions of this source code include the above copyright
12 notice, this list of conditions and the following disclaimer;
13
14 2. distributions in binary form include the above copyright
15 notice, this list of conditions and the following disclaimer
16 in the documentation and/or other associated materials;
17
18 3. the copyright holder's name is not used to endorse products
19 built using this software without specific written permission.
20
21 ALTERNATIVELY, provided that this notice is retained in full, this product
22 may be distributed under the terms of the GNU General Public License (GPL),
23 in which case the provisions of the GPL apply INSTEAD OF those given above.
24
25 DISCLAIMER
26
27 This software is provided 'as is' with no explicit or implied warranties
28 in respect of its properties, including, but not limited to, correctness
29 and/or fitness for purpose.
30 ---------------------------------------------------------------------------
31 Issue Date: 26/08/2003
32
33 This is a byte oriented version of SHA1 that operates on arrays of bytes
34 stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
35*/
36
37#include <string.h> /* for memcpy() etc. */
38#include <stdlib.h> /* for _lrotl with VC++ */
39
40#include "sha1.h"
41#include "../os.h"
42
43/*
44 To obtain the highest speed on processors with 32-bit words, this code
45 needs to determine the order in which bytes are packed into such words.
46 The following block of code is an attempt to capture the most obvious
47 ways in which various environemnts specify their endian definitions.
48 It may well fail, in which case the definitions will need to be set by
49 editing at the points marked **** EDIT HERE IF NECESSARY **** below.
50*/
51
52/* BYTE ORDER IN 32-BIT WORDS
53
54 To obtain the highest speed on processors with 32-bit words, this code
55 needs to determine the byte order of the target machine. The following
56 block of code is an attempt to capture the most obvious ways in which
57 various environemnts define byte order. It may well fail, in which case
58 the definitions will need to be set by editing at the points marked
59 **** EDIT HERE IF NECESSARY **** below. My thanks to Peter Gutmann for
60 some of these defines (from cryptlib).
61*/
62
63#define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
64#define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
65
66#ifdef __BIG_ENDIAN__
67#define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
68#else
69#define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
70#endif
71
72#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
73
74#if (PLATFORM_BYTE_ORDER == BRG_BIG_ENDIAN)
75#define swap_b32(x) (x)
76#else
77#define swap_b32(x) irr::os::Byteswap::byteswap(x)
78#endif
79
80#define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
81
82#if 1
83
84#define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
85#define parity(x,y,z) ((x) ^ (y) ^ (z))
86#define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
87
88#else /* Discovered Rich Schroeppel and Colin Plumb */
89
90#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
91#define parity(x,y,z) ((x) ^ (y) ^ (z))
92#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
93
94#endif
95
96/* A normal version as set out in the FIPS */
97
98#define rnd(f,k) \
99 t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
100 e = d; d = c; c = rotl32(b, 30); b = t
101
102void sha1_compile(sha1_ctx ctx[1])
103{ sha1_32t w[80], i, a, b, c, d, e, t;
104
105 /* note that words are compiled from the buffer into 32-bit */
106 /* words in big-endian order so an order reversal is needed */
107 /* here on little endian machines */
108 for(i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
109 w[i] = swap_b32(ctx->wbuf[i]);
110
111 for(i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
112 w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
113
114 a = ctx->hash[0];
115 b = ctx->hash[1];
116 c = ctx->hash[2];
117 d = ctx->hash[3];
118 e = ctx->hash[4];
119
120 for(i = 0; i < 20; ++i)
121 {
122 rnd(ch, 0x5a827999);
123 }
124
125 for(i = 20; i < 40; ++i)
126 {
127 rnd(parity, 0x6ed9eba1);
128 }
129
130 for(i = 40; i < 60; ++i)
131 {
132 rnd(maj, 0x8f1bbcdc);
133 }
134
135 for(i = 60; i < 80; ++i)
136 {
137 rnd(parity, 0xca62c1d6);
138 }
139
140 ctx->hash[0] += a;
141 ctx->hash[1] += b;
142 ctx->hash[2] += c;
143 ctx->hash[3] += d;
144 ctx->hash[4] += e;
145}
146
147void sha1_begin(sha1_ctx ctx[1])
148{
149 ctx->count[0] = ctx->count[1] = 0;
150 ctx->hash[0] = 0x67452301;
151 ctx->hash[1] = 0xefcdab89;
152 ctx->hash[2] = 0x98badcfe;
153 ctx->hash[3] = 0x10325476;
154 ctx->hash[4] = 0xc3d2e1f0;
155}
156
157/* SHA1 hash data in an array of bytes into hash buffer and */
158/* call the hash_compile function as required. */
159
160void sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1])
161{ sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK),
162 space = SHA1_BLOCK_SIZE - pos;
163 const unsigned char *sp = data;
164
165 if((ctx->count[0] += len) < len)
166 ++(ctx->count[1]);
167
168 while(len >= space) /* tranfer whole blocks if possible */
169 {
170 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
171 sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
172 sha1_compile(ctx);
173 }
174
175 /*lint -e{803} conceivable data overrun */
176 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
177}
178
179/* SHA1 final padding and digest calculation */
180
181#if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)
182static sha1_32t mask[4] =
183 { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
184static sha1_32t bits[4] =
185 { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
186#else
187static sha1_32t mask[4] =
188 { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
189static sha1_32t bits[4] =
190 { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
191#endif
192
193void sha1_end(unsigned char hval[], sha1_ctx ctx[1])
194{ sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1_MASK);
195
196 /* mask out the rest of any partial 32-bit word and then set */
197 /* the next byte to 0x80. On big-endian machines any bytes in */
198 /* the buffer will be at the top end of 32 bit words, on little */
199 /* endian machines they will be at the bottom. Hence the AND */
200 /* and OR masks above are reversed for little endian systems */
201 /* Note that we can always add the first padding byte at this */
202 /* point because the buffer always has at least one empty slot */
203 ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & mask[i & 3]) | bits[i & 3];
204
205 /* we need 9 or more empty positions, one for the padding byte */
206 /* (above) and eight for the length count. If there is not */
207 /* enough space pad and empty the buffer */
208 if(i > SHA1_BLOCK_SIZE - 9)
209 {
210 if(i < 60) ctx->wbuf[15] = 0;
211 sha1_compile(ctx);
212 i = 0;
213 }
214 else /* compute a word index for the empty buffer positions */
215 i = (i >> 2) + 1;
216
217 while(i < 14) /* and zero pad all but last two positions */
218 ctx->wbuf[i++] = 0;
219
220 /* assemble the eight byte counter in in big-endian format */
221 ctx->wbuf[14] = swap_b32((ctx->count[1] << 3) | (ctx->count[0] >> 29));
222 ctx->wbuf[15] = swap_b32(ctx->count[0] << 3);
223
224 sha1_compile(ctx);
225
226 /* extract the hash value as bytes in case the hash buffer is */
227 /* misaligned for 32-bit words */
228 for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
229 hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
230}
231
232void sha1(unsigned char hval[], const unsigned char data[], unsigned long len)
233{ sha1_ctx cx[1];
234
235 sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
236}
237