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