ecere/gui/Window: Prevent uninitialized values if base Window methods not overridden...
[sdk] / extras / sha256.ec
1 import "ecere"
2 /*
3         Copyright (c) 2009  Gabriel A. Petursson
4         All rights reserved.
5
6         Redistribution and use in source and binary forms, with or without
7         modification, are permitted provided that the following conditions
8         are met:
9         1. Redistributions of source code must retain the above copyright
10            notice, this list of conditions and the following disclaimer.
11         2. Redistributions in binary form must reproduce the above copyright
12            notice, this list of conditions and the following disclaimer in the
13            documentation and/or other materials provided with the distribution.
14         3. The name of the author may not be used to endorse or promote products
15            derived from this software without specific prior written permission.
16
17         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #define ROR(x, y) (((x) >> (y)) ^ ((x) << ((sizeof(x) << 3) - (y))))
30
31 #define UNPACK_32(x, str) { \
32         *((str) + 3) = (byte) (x); \
33         *((str) + 2) = (byte) ((x) >>  8); \
34         *((str) + 1) = (byte) ((x) >> 16); \
35         *((str))     = (byte) ((x) >> 24); \
36 }
37
38 #define UNPACK_64(x, str) { \
39         *((str) + 7) = (byte) (x); \
40         *((str) + 6) = (byte) ((x) >>  8); \
41         *((str) + 5) = (byte) ((x) >> 16); \
42         *((str) + 4) = (byte) ((x) >> 24); \
43         *((str) + 3) = (byte) ((x) >> 32); \
44         *((str) + 2) = (byte) ((x) >> 40); \
45         *((str) + 1) = (byte) ((x) >> 48); \
46         *((str))     = (byte) ((x) >> 56); \
47 }
48
49 #define PACK_32(str, x) { \
50         *(x) = ((uint32) *((str)    ) << 24) \
51              ^ ((uint32) *((str) + 1) << 16) \
52              ^ ((uint32) *((str) + 2) <<  8) \
53              ^ ((uint32) *((str) + 3)); \
54 }
55
56 #define CH(x, y, z) (z ^ (x & (y ^ z)))
57 #define MAJ(x, y, z) ((x & y) | (z & (x | y)))
58
59 #define SHA256_S0(x) (ROR(x,  7) ^ ROR(x, 18) ^ (x) >>  3)
60 #define SHA256_S1(x) (ROR(x, 17) ^ ROR(x, 19) ^ (x) >> 10)
61 #define SHA256_T0(x) (ROR(x, 2) ^ ROR(x, 13) ^ ROR(x, 22))
62 #define SHA256_T1(x) (ROR(x, 6) ^ ROR(x, 11) ^ ROR(x, 25))
63
64 #define SHA256_PRC(a, b, c, d, e, f, g, h, idx, key) { \
65         uint32 t1 = wv[h] + SHA256_T1(wv[e]) + CH(wv[e], wv[f], wv[g]) + key + idx; \
66         wv[d] += t1; \
67         wv[h]  = t1 + SHA256_T0(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
68 }
69
70 #define SHA256_EXT(i) ( \
71         w[i] += SHA256_S0(w[(i + 1) & 0x0F]) + SHA256_S1(w[(i - 2) & 0x0F]) + w[(i - 7) & 0x0F] \
72 )
73
74 struct AmpheckSHA256
75 {
76         uint32 h[8];
77         byte buffer[64];
78
79         uint64 length;
80
81    void Init()
82    {
83         h[0] = 0x6a09e667;
84         h[1] = 0xbb67ae85;
85         h[2] = 0x3c6ef372;
86         h[3] = 0xa54ff53a;
87         h[4] = 0x510e527f;
88         h[5] = 0x9b05688c;
89         h[6] = 0x1f83d9ab;
90         h[7] = 0x5be0cd19;
91    }
92
93    void TransformData(byte *data, uint64 blocks)
94    {
95       uint64 i;
96         for (i = 0; i < blocks; ++i)
97         {
98                 uint32 wv[8];
99                 uint32 w[16];
100
101                 PACK_32(&data[(i << 6)     ], &w[ 0]);
102                 PACK_32(&data[(i << 6) +  4], &w[ 1]);
103                 PACK_32(&data[(i << 6) +  8], &w[ 2]);
104                 PACK_32(&data[(i << 6) + 12], &w[ 3]);
105                 PACK_32(&data[(i << 6) + 16], &w[ 4]);
106                 PACK_32(&data[(i << 6) + 20], &w[ 5]);
107                 PACK_32(&data[(i << 6) + 24], &w[ 6]);
108                 PACK_32(&data[(i << 6) + 28], &w[ 7]);
109                 PACK_32(&data[(i << 6) + 32], &w[ 8]);
110                 PACK_32(&data[(i << 6) + 36], &w[ 9]);
111                 PACK_32(&data[(i << 6) + 40], &w[10]);
112                 PACK_32(&data[(i << 6) + 44], &w[11]);
113                 PACK_32(&data[(i << 6) + 48], &w[12]);
114                 PACK_32(&data[(i << 6) + 52], &w[13]);
115                 PACK_32(&data[(i << 6) + 56], &w[14]);
116                 PACK_32(&data[(i << 6) + 60], &w[15]);
117
118                 wv[0] = h[0];
119                 wv[1] = h[1];
120                 wv[2] = h[2];
121                 wv[3] = h[3];
122                 wv[4] = h[4];
123                 wv[5] = h[5];
124                 wv[6] = h[6];
125                 wv[7] = h[7];
126
127                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, w[ 0], 0x428a2f98);
128                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, w[ 1], 0x71374491);
129                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, w[ 2], 0xb5c0fbcf);
130                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, w[ 3], 0xe9b5dba5);
131                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, w[ 4], 0x3956c25b);
132                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, w[ 5], 0x59f111f1);
133                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, w[ 6], 0x923f82a4);
134                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, w[ 7], 0xab1c5ed5);
135                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, w[ 8], 0xd807aa98);
136                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, w[ 9], 0x12835b01);
137                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, w[10], 0x243185be);
138                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, w[11], 0x550c7dc3);
139                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, w[12], 0x72be5d74);
140                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, w[13], 0x80deb1fe);
141                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, w[14], 0x9bdc06a7);
142                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, w[15], 0xc19bf174);
143
144                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, SHA256_EXT( 0), 0xe49b69c1);
145                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, SHA256_EXT( 1), 0xefbe4786);
146                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, SHA256_EXT( 2), 0x0fc19dc6);
147                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, SHA256_EXT( 3), 0x240ca1cc);
148                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, SHA256_EXT( 4), 0x2de92c6f);
149                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, SHA256_EXT( 5), 0x4a7484aa);
150                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, SHA256_EXT( 6), 0x5cb0a9dc);
151                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, SHA256_EXT( 7), 0x76f988da);
152                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, SHA256_EXT( 8), 0x983e5152);
153                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, SHA256_EXT( 9), 0xa831c66d);
154                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, SHA256_EXT(10), 0xb00327c8);
155                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, SHA256_EXT(11), 0xbf597fc7);
156                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, SHA256_EXT(12), 0xc6e00bf3);
157                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, SHA256_EXT(13), 0xd5a79147);
158                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, SHA256_EXT(14), 0x06ca6351);
159                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, SHA256_EXT(15), 0x14292967);
160                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, SHA256_EXT( 0), 0x27b70a85);
161                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, SHA256_EXT( 1), 0x2e1b2138);
162                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, SHA256_EXT( 2), 0x4d2c6dfc);
163                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, SHA256_EXT( 3), 0x53380d13);
164                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, SHA256_EXT( 4), 0x650a7354);
165                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, SHA256_EXT( 5), 0x766a0abb);
166                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, SHA256_EXT( 6), 0x81c2c92e);
167                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, SHA256_EXT( 7), 0x92722c85);
168                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, SHA256_EXT( 8), 0xa2bfe8a1);
169                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, SHA256_EXT( 9), 0xa81a664b);
170                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, SHA256_EXT(10), 0xc24b8b70);
171                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, SHA256_EXT(11), 0xc76c51a3);
172                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, SHA256_EXT(12), 0xd192e819);
173                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, SHA256_EXT(13), 0xd6990624);
174                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, SHA256_EXT(14), 0xf40e3585);
175                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, SHA256_EXT(15), 0x106aa070);
176                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, SHA256_EXT( 0), 0x19a4c116);
177                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, SHA256_EXT( 1), 0x1e376c08);
178                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, SHA256_EXT( 2), 0x2748774c);
179                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, SHA256_EXT( 3), 0x34b0bcb5);
180                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, SHA256_EXT( 4), 0x391c0cb3);
181                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, SHA256_EXT( 5), 0x4ed8aa4a);
182                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, SHA256_EXT( 6), 0x5b9cca4f);
183                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, SHA256_EXT( 7), 0x682e6ff3);
184                 SHA256_PRC(0, 1, 2, 3, 4, 5, 6, 7, SHA256_EXT( 8), 0x748f82ee);
185                 SHA256_PRC(7, 0, 1, 2, 3, 4, 5, 6, SHA256_EXT( 9), 0x78a5636f);
186                 SHA256_PRC(6, 7, 0, 1, 2, 3, 4, 5, SHA256_EXT(10), 0x84c87814);
187                 SHA256_PRC(5, 6, 7, 0, 1, 2, 3, 4, SHA256_EXT(11), 0x8cc70208);
188                 SHA256_PRC(4, 5, 6, 7, 0, 1, 2, 3, SHA256_EXT(12), 0x90befffa);
189                 SHA256_PRC(3, 4, 5, 6, 7, 0, 1, 2, SHA256_EXT(13), 0xa4506ceb);
190                 SHA256_PRC(2, 3, 4, 5, 6, 7, 0, 1, SHA256_EXT(14), 0xbef9a3f7);
191                 SHA256_PRC(1, 2, 3, 4, 5, 6, 7, 0, SHA256_EXT(15), 0xc67178f2);
192
193                 h[0] += wv[0];
194                 h[1] += wv[1];
195                 h[2] += wv[2];
196                 h[3] += wv[3];
197                 h[4] += wv[4];
198                 h[5] += wv[5];
199                 h[6] += wv[6];
200                 h[7] += wv[7];
201         }
202    }
203
204    void Update(byte *data, uint size)
205    {
206         if (size >= 64 - length % 64)
207         {
208                 memcpy(&buffer[length % 64], data, (uint32)(64 - length % 64));
209
210                 TransformData(buffer, 1);
211                 //TransformData(&data[64 - length % 64], size / 64);
212          TransformData(&data[64 - length % 64], (size - (64 - length % 64)) / 64);
213         }
214         else
215         {
216                 memcpy(&buffer[length % 64], data, size);
217         }
218
219         length += size;
220    }
221
222    void Finish(byte *digest)
223    {
224         AmpheckSHA256 tmp { };
225
226         tmp.h[0] = h[0];
227         tmp.h[1] = h[1];
228         tmp.h[2] = h[2];
229         tmp.h[3] = h[3];
230         tmp.h[4] = h[4];
231         tmp.h[5] = h[5];
232         tmp.h[6] = h[6];
233         tmp.h[7] = h[7];
234
235         memcpy(tmp.buffer, buffer, (uint32)(length % 64));
236         tmp.buffer[length % 64] = 0x80;
237
238         if (length % 64 < 56)
239         {
240                 memset(&tmp.buffer[length % 64 + 1], 0x00, (uint32)(55 - length % 64));
241         }
242         else
243         {
244                 memset(&tmp.buffer[length % 64 + 1], 0x00, (uint32)(63 - length % 64));
245                 tmp.TransformData(tmp.buffer, 1);
246
247                 memset(tmp.buffer, 0x00, 56);
248         }
249
250         UNPACK_64(length % 64 * 8, &tmp.buffer[56]);
251         tmp.TransformData(tmp.buffer, 1);
252
253         UNPACK_32(tmp.h[0], &digest[ 0]);
254         UNPACK_32(tmp.h[1], &digest[ 4]);
255         UNPACK_32(tmp.h[2], &digest[ 8]);
256         UNPACK_32(tmp.h[3], &digest[12]);
257         UNPACK_32(tmp.h[4], &digest[16]);
258         UNPACK_32(tmp.h[5], &digest[20]);
259         UNPACK_32(tmp.h[6], &digest[24]);
260         UNPACK_32(tmp.h[7], &digest[28]);
261    }
262 };