1 /********************************************************************************
2 * Nepenthes
3 * - finest collection -
4 *
5 *
6 *
7 * Copyright (C) 2005 Paul Baecher & Markus Koetter
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 *
23 *
24 * contact nepenthesdev@users.sourceforge.net
25 *
26 *******************************************************************************/
27
28 /* $Id: Utilities.cpp 1358 2007-08-23 13:16:07Z common $ */
29
30 #ifdef WIN32
31 #include <memory.h>
32 #else
33 #endif
34
35
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
39 #include <sys/types.h>
40 #include <inttypes.h>
41 #include <cctype>
42
43 #include "Utilities.hpp"
44 #include "LogManager.hpp"
45 #include "Config.hpp"
46
47 using namespace nepenthes;
48
49
Utilities()50 Utilities::Utilities()
51 {
52
53 }
54
~Utilities()55 Utilities::~Utilities()
56 {
57 }
58
59 // START MD5Sum
60
61 /*------------ macros for storing/extracting msb first words -------------*/
62
63 #define GET_32BIT(cp) (((uint32_t)(unsigned char)(cp)[0] << 24) | \
64 ((uint32_t)(unsigned char)(cp)[1] << 16) | \
65 ((uint32_t)(unsigned char)(cp)[2] << 8) | \
66 ((uint32_t)(unsigned char)(cp)[3]))
67
68 #define GET_16BIT(cp) (((uint32_t)(unsigned char)(cp)[0] << 8) | \
69 ((uint32_t)(unsigned char)(cp)[1]))
70
71 #define PUT_32BIT(cp, value) do { \
72 (cp)[0] = (char)((value) >> 24); \
73 (cp)[1] = (char)((value) >> 16); \
74 (cp)[2] = (char)((value) >> 8); \
75 (cp)[3] = (char)(value); } while (0)
76
77 #define PUT_16BIT(cp, value) do { \
78 (cp)[0] = (value) >> 8; \
79 (cp)[1] = (value); } while (0)
80
81 /*------------ macros for storing/extracting lsb first words -------------*/
82
83 #define GET_32BIT_LSB_FIRST(cp) \
84 (((uint32_t)(unsigned char)(cp)[0]) | \
85 ((uint32_t)(unsigned char)(cp)[1] << 8) | \
86 ((uint32_t)(unsigned char)(cp)[2] << 16) | \
87 ((uint32_t)(unsigned char)(cp)[3] << 24))
88
89 #define GET_16BIT_LSB_FIRST(cp) \
90 (((uint32_t)(unsigned char)(cp)[0]) | \
91 ((uint32_t)(unsigned char)(cp)[1] << 8))
92
93 #define PUT_32BIT_LSB_FIRST(cp, value) do { \
94 (cp)[0] = (char)(value); \
95 (cp)[1] = (char)((value) >> 8); \
96 (cp)[2] = (char)((value) >> 16); \
97 (cp)[3] = (char)((value) >> 24); } while (0)
98
99 #define PUT_16BIT_LSB_FIRST(cp, value) do { \
100 (cp)[0] = (char)(value); \
101 (cp)[1] = (char)((value) >> 8); } while (0)
102
103
104
105 /* The four core functions - F1 is optimized somewhat */
106
107 /* #define F1(x, y, z) (x & y | ~x & z) */
108 #define F1(x, y, z) (z ^ (x & (y ^ z)))
109 #define F2(x, y, z) F1(z, x, y)
110 #define F3(x, y, z) (x ^ y ^ z)
111 #define F4(x, y, z) (y ^ (x | ~z))
112
113 /* This is the central step in the MD5 algorithm. */
114 #define MD5STEP(f, w, x, y, z, data, s) \
115 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
116
117
118 /*
119 * The core of the MD5 algorithm, this alters an existing MD5 hash to
120 * reflect the addition of 16 longwords of new data. MD5Update blocks
121 * the data and converts bytes into longwords for this routine.
122 */
MD5Transform(md5_uint32 buf[4],const unsigned char inext[64])123 void Utilities::MD5Transform(md5_uint32 buf[4], const unsigned char inext[64])
124 {
125 register word32 a, b, c, d, i;
126 word32 in[16];
127
128 for (i = 0; i < 16; i++)
129 in[i] = GET_32BIT_LSB_FIRST(inext + 4 * i);
130
131 a = buf[0];
132 b = buf[1];
133 c = buf[2];
134 d = buf[3];
135
136 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
137 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
138 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
139 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
140 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
141 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
142 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
143 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
144 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
145 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
146 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
147 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
148 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
149 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
150 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
151 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
152
153 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
154 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
155 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
156 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
157 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
158 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
159 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
160 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
161 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
162 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
163 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
164 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
165 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
166 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
167 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
168 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
169
170 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
171 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
172 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
173 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
174 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
175 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
176 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
177 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
178 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
179 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
180 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
181 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
182 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
183 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
184 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
185 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
186
187 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
188 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
189 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
190 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
191 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
192 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
193 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
194 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
195 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
196 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
197 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
198 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
199 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
200 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
201 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
202 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
203
204 buf[0] += a;
205 buf[1] += b;
206 buf[2] += c;
207 buf[3] += d;
208 }
209
210
211 /*
212 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
213 * initialization constants.
214 */
MD5Init(struct MD5Context * ctx)215 void Utilities::MD5Init(struct MD5Context *ctx)
216 {
217 ctx->buf[0] = 0x67452301;
218 ctx->buf[1] = 0xefcdab89;
219 ctx->buf[2] = 0x98badcfe;
220 ctx->buf[3] = 0x10325476;
221
222 ctx->bits[0] = 0;
223 ctx->bits[1] = 0;
224 }
225
226 /*
227 * Update context to reflect the concatenation of another buffer full
228 * of bytes.
229 */
MD5Update(struct MD5Context * ctx,unsigned char const * buf,unsigned len)230 void Utilities::MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
231 {
232 md5_uint32 t;
233
234 /* Update bitcount */
235
236 t = ctx->bits[0];
237 if ((ctx->bits[0] = (t + ((md5_uint32)len << 3)) & 0xffffffff) < t)
238 ctx->bits[1]++; /* Carry from low to high */
239 ctx->bits[1] += len >> 29;
240
241 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
242
243 /* Handle any leading odd-sized chunks */
244
245 if (t) {
246 unsigned char *p = ctx->in + t;
247
248 t = 64 - t;
249 if (len < t) {
250 memcpy(p, buf, len);
251 return;
252 }
253 memcpy(p, buf, t);
254 MD5Transform(ctx->buf, ctx->in);
255 buf += t;
256 len -= t;
257 }
258 /* Process data in 64-byte chunks */
259
260 while (len >= 64) {
261 memcpy(ctx->in, buf, 64);
262 MD5Transform(ctx->buf, ctx->in);
263 buf += 64;
264 len -= 64;
265 }
266
267 /* Handle any remaining bytes of data. */
268
269 memcpy(ctx->in, buf, len);
270 }
271
272 /*
273 * Final wrapup - pad to 64-byte boundary with the bit pattern
274 * 1 0* (64-bit count of bits processed, MSB-first)
275 */
MD5Final(unsigned char digest[16],struct MD5Context * ctx)276 void Utilities::MD5Final(unsigned char digest[16], struct MD5Context *ctx)
277 {
278 unsigned count;
279 unsigned char *p;
280
281 /* Compute number of bytes mod 64 */
282 count = (ctx->bits[0] >> 3) & 0x3F;
283
284 /* Set the first char of padding to 0x80. This is safe since there is
285 always at least one byte free */
286 p = ctx->in + count;
287 *p++ = 0x80;
288
289 /* Bytes of padding needed to make 64 bytes */
290 count = 64 - 1 - count;
291
292 /* Pad out to 56 mod 64 */
293 if (count < 8) {
294 /* Two lots of padding: Pad the first block to 64 bytes */
295 memset(p, 0, count);
296 MD5Transform(ctx->buf, ctx->in);
297
298 /* Now fill the next block with 56 bytes */
299 memset(ctx->in, 0, 56);
300 } else {
301 /* Pad block to 56 bytes */
302 memset(p, 0, count - 8);
303 }
304
305 /* Append length in bits and transform */
306 PUT_32BIT_LSB_FIRST(ctx->in + 56, ctx->bits[0]);
307 PUT_32BIT_LSB_FIRST(ctx->in + 60, ctx->bits[1]);
308
309 MD5Transform(ctx->buf, ctx->in);
310 PUT_32BIT_LSB_FIRST(digest, ctx->buf[0]);
311 PUT_32BIT_LSB_FIRST(digest + 4, ctx->buf[1]);
312 PUT_32BIT_LSB_FIRST(digest + 8, ctx->buf[2]);
313 PUT_32BIT_LSB_FIRST(digest + 12, ctx->buf[3]);
314 memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
315 }
316
317
318
md5sum(char * msg,int32_t len)319 string Utilities::md5sum(char *msg, int32_t len)
320 {
321
322 struct MD5Context MD5Struct;
323 MD5Init(&MD5Struct);
324
325 unsigned char MD5Result[16];
326 MD5Update(&MD5Struct,(const unsigned char *)msg,len);
327 MD5Final(MD5Result,&MD5Struct);
328
329 string MD5Sum = "";
330
331 //char sum[32];
332 //int32_t i;
333
334 for(uint32_t i = 0; i < 16; ++i)
335 {
336 MD5Sum += ((MD5Result[i] >> 4) < 10 ? (MD5Result[i] >> 4) + '0' : (MD5Result[i] >> 4) + ('a' - 10));
337 MD5Sum += ((MD5Result[i] & 0xF) < 10 ? (MD5Result[i] & 0xF) + '0' : (MD5Result[i] & 0xF) + ('a' - 10));
338 }
339 return MD5Sum;
340 }
341
342 // ENDOF MD5Sum
343
344
345
hexdump(byte * data,uint32_t len)346 void Utilities::hexdump(byte *data, uint32_t len)
347 {
348 hexdump(l_spam, data, len);
349 }
350
hexdump(uint32_t mask,byte * data,uint32_t len)351 void Utilities::hexdump(uint32_t mask, byte *data, uint32_t len)
352 {
353
354 if (m_HexdumpPath.size() == 0)
355 {
356 m_HexdumpPath = g_Nepenthes->getConfig()->getValString("nepenthes.utilities.hexdump_path");
357 }
358 string md5 = m_HexdumpPath;
359 md5.append(md5sum((char *)data, len));
360 md5.append(".bin");
361
362 FILE *f;
363
364
365 if( !len )
366 {
367 g_Nepenthes->getLogMgr()->log(mask,"Ignoring zero-length hexdump.\n");
368 return;
369 }
370
371 if( (f = fopen(md5.c_str(), "wb")) )
372 {
373 fwrite((const void *)data, len, 1, f);
374 fclose(f);
375
376 g_Nepenthes->getLogMgr()->logf(mask,"Stored Hexdump %s (0x%08x , 0x%08x).\n", md5.c_str(), (uint32_t)((intptr_t)data), len);
377 }else
378 {
379 g_Nepenthes->getLogMgr()->logf(l_crit, "Could not open %s (%s)\n", md5.c_str(), strerror(errno));
380 }
381
382
383 #ifdef HAVE_DEBUG_LOGGING
384 char conv[] = "0123456789abcdef";
385 g_Nepenthes->getLogMgr()->logf(mask,"=------------------[ hexdump(0x%08x , 0x%08x) ]-------------------=\n", (uint32_t)((intptr_t)data), len);
386 for( uint32_t i = 0; i < len; i += 0x10 )
387 {
388 string line;
389 // printf("0x%04x ", i);
390 line = "";
391
392 uint32_t j;
393 for( j = 0; j < 0x10; j++ )
394 {
395 if( i + j < len )
396 {
397 // printf("%c%c ",conv[((data[i + j] & 0xFF) >> 4)],conv[((data[i + j] & 0xff) & 0x0F)]);
398 line += conv[((data[i + j] & 0xFF) >> 4)];
399 line += conv[((data[i + j] & 0xff) & 0x0F)];
400 line += " ";
401 }
402 else
403 // printf(" ");
404 line += " ";
405
406 if( j == 7 )
407 // printf(" ");
408 line += " ";
409 }
410
411 // printf(" ");
412 line += " ";
413
414 for( j = 0; j < 0x10; j++ )
415 {
416 if( i + j < len )
417 // printf("%c", isprint(data[i + j]) ? data[i + j] : '.');
418 line += isprint(data[i + j]) ? data[i + j] : '.';
419 else
420 // printf(" ");
421 line += " ";
422 if( j == 7 )
423 // printf(" ");
424 line += " ";
425 }
426
427 line += "\n";
428 // printf("\n");
429 g_Nepenthes->getLogMgr()->logf(mask,"0x%04x %s",i,line.c_str());
430
431 }
432 g_Nepenthes->getLogMgr()->log(mask,"=-------------------------------------------------------------------------=\n");
433 #endif
434 // printf("=-------------------------------------------------------------------------=\n");
435
436 }
437
438
439
440
441
442
443
444 // START BASE64
445
446 static unsigned char b64e[] = {
447 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', /* 0- 7 */
448 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', /* 8-15 */
449 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', /* 16-23 */
450 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 24-31 */
451 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 32-39 */
452 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', /* 40-47 */
453 'w', 'x', 'y', 'z', '0', '1', '2', '3', /* 48-55 */
454 '4', '5', '6', '7', '8', '9', '+', '/' /* 56-63 */
455 };
456
457 static unsigned char b64d[] = {
458 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0x00-0x0F */
459 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, /* 0x10-0x1F */
460 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3e,0x00,0x00,0x00,0x3f, /* 0x20-0x2F */
461 0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x00,0x00,0x00,0x00,0x00,0x00, /* 0x30-0x3F */
462 0x00,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e, /* 0x40-0x4F */
463 0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x00,0x00,0x00,0x00,0x00, /* 0x50-0x5F */
464 0x00,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, /* 0x60-0x6F */
465 0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x30,0x31,0x32,0x33,0x00,0x00,0x00,0x00,0x00 /* 0x70-0x7F */
466 };
467
b64enc(unsigned char * in,int32_t inlen,unsigned char * out)468 void Utilities::b64enc(unsigned char *in, int32_t inlen, unsigned char *out)
469 {
470 unsigned char t0 = (inlen > 0) ? in[0] : 0;
471 unsigned char t1 = (inlen > 1) ? in[1] : 0;
472 unsigned char t2 = (inlen > 2) ? in[2] : 0;
473
474 if ( inlen <= 0 ) return;
475 out[0] = b64e[(t0 >> 2) & 0x3f];
476 out[1] = b64e[((t0 << 4) & 0x30) | (t1 >> 4)];
477
478 if ( inlen <= 1 ) return;
479 out[2] = b64e[((t1 << 2) & 0x3c) | (t2 >> 6)];
480
481 if ( inlen <= 2 ) return;
482 out[3] = b64e[t2 & 0x3f];
483 }
484
b64dec(unsigned char * in,int32_t inlen,unsigned char * out)485 void Utilities::b64dec(unsigned char *in, int32_t inlen, unsigned char *out)
486 {
487 unsigned char t0 = (inlen > 0) ? b64d[in[0] & 0x7f] : 0;
488 unsigned char t1 = (inlen > 1) ? b64d[in[1] & 0x7f] : 0;
489 unsigned char t2 = (inlen > 2) ? b64d[in[2] & 0x7f] : 0;
490 unsigned char t3 = (inlen > 3) ? b64d[in[3] & 0x7f] : 0;
491
492 if ( inlen <= 0 ) return;
493 out[0] = (t0 << 2) | (t1 >> 4);
494 if ( inlen <= 1 )
495 {
496 out[1] = 0;
497 return;
498 }
499 out[1] = (t1 << 4) | ((t2 & 0x3c) >> 2);
500 if ( inlen <= 2 )
501 {
502 out[2] = 0;
503 return;
504 }
505 out[2] = (t2 << 6) | t3;
506 }
507
b64encode_len(unsigned char * in)508 int32_t Utilities::b64encode_len(unsigned char *in)
509 {
510 int32_t l = strlen((char *)in);
511 return 4*((l+2)/3);
512 }
513
b64encode_len(unsigned char * in,int32_t l)514 int32_t Utilities::b64encode_len(unsigned char *in, int32_t l)
515 {
516 return 4*((l+2)/3);
517 }
518
519
b64decode_len(unsigned char * in)520 int32_t Utilities::b64decode_len(unsigned char *in)
521 {
522 int32_t l = strlen((char *)in);
523 return 3*((l+3)/4);
524 }
525
b64encode_alloc(unsigned char * in)526 unsigned char *Utilities::b64encode_alloc(unsigned char *in)
527 {
528 int32_t l = b64encode_len(in);
529 unsigned char *n = (unsigned char *) malloc(l+1);
530 if ( n != NULL )
531 {
532 n[l--] = 0;
533 while ( l >= 0 )
534 {
535 n[l--] = '=';
536 }
537 b64encode(in,n);
538 }
539 return n;
540 }
541
b64encode_alloc(unsigned char * in,int32_t inlen)542 unsigned char *Utilities::b64encode_alloc(unsigned char *in, int32_t inlen)
543 {
544 int32_t l = b64encode_len(in,inlen);
545 unsigned char *n = (unsigned char *) malloc(l+1);
546 if ( n != NULL )
547 {
548 n[l--] = 0;
549 while ( l >= 0 )
550 {
551 n[l--] = '=';
552 }
553 b64encode(in,inlen,n);
554 }
555 return n;
556 }
557
558
b64encode(unsigned char * in,unsigned char * out)559 void Utilities::b64encode(unsigned char *in, unsigned char *out)
560 {
561 int32_t inlen = strlen((char *)in);
562 while ( inlen > 0 )
563 {
564 b64enc(in, inlen, out);
565 inlen -= 3;
566 in += 3;
567 out += 4;
568 }
569 }
570
b64encode(unsigned char * in,int32_t inlen,unsigned char * out)571 void Utilities::b64encode(unsigned char *in,int32_t inlen, unsigned char *out)
572 {
573 while ( inlen > 0 )
574 {
575 b64enc(in, inlen, out);
576 inlen -= 3;
577 in += 3;
578 out += 4;
579 }
580 }
581
582
b64decode_alloc(unsigned char * in)583 unsigned char *Utilities::b64decode_alloc(unsigned char *in)
584 {
585 int32_t l = b64decode_len(in);
586 unsigned char *n = (unsigned char *) malloc(l+1);
587 if ( n != NULL )
588 {
589 while ( l >= 0 )
590 {
591 n[l--] = 0;
592 }
593 b64decode(in,n);
594 }
595 return n;
596 }
597
b64decode(unsigned char * in,unsigned char * out)598 void Utilities::b64decode(unsigned char *in, unsigned char *out)
599 {
600 int32_t inlen = strlen((char *)in);
601 while ( inlen > 0 )
602 {
603 b64dec(in, inlen, out);
604 inlen -= 4;
605 in += 4;
606 out += 3;
607 }
608 }
609
610 // ENDOF BASE64
611
612
613
614 // START HMAC_MD5
615
616 /*
617 ** Function: hmac_md5
618 */
619
hmac_md5(unsigned char * text,int32_t text_len,unsigned char * key,int32_t key_len,unsigned char * digest)620 void Utilities::hmac_md5(
621 unsigned char* text, /* pointer to data stream */
622 int32_t text_len, /* length of data stream */
623 unsigned char* key, /* pointer to authentication key */
624 int32_t key_len, /* length of authentication key */
625 unsigned char *digest) /* caller digest to be filled in */
626
627 {
628 MD5_CTX context;
629 unsigned char k_ipad[65]; /* inner padding -
630 * key XORd with ipad
631 */
632 unsigned char k_opad[65]; /* outer padding -
633 * key XORd with opad
634 */
635 unsigned char tk[16];
636 int32_t i;
637 /* if key is longer than 64 bytes reset it to key=MD5(key) */
638 if (key_len > 64) {
639
640 MD5_CTX tctx;
641
642 MD5Init(&tctx);
643 MD5Update(&tctx, key, key_len);
644 MD5Final(tk, &tctx);
645
646 key = tk;
647 key_len = 16;
648 }
649
650 /*
651 * the HMAC_MD5 transform looks like:
652 *
653 * MD5(K XOR opad, MD5(K XOR ipad, text))
654 *
655 * where K is an n byte key
656 * ipad is the byte 0x36 repeated 64 times
657 * opad is the byte 0x5c repeated 64 times
658 * and text is the data being protected
659 */
660
661 /* start out by storing key in pads */
662
663 #ifdef WIN32
664 memset( k_ipad, 0, sizeof k_ipad);
665 memset( k_opad, 0, sizeof k_opad);
666 memcpy( k_ipad, key, key_len);
667 memcpy( k_opad, key, key_len);
668 #else
669 bzero( k_ipad, sizeof k_ipad);
670 bzero( k_opad, sizeof k_opad);
671 bcopy( key, k_ipad, key_len);
672 bcopy( key, k_opad, key_len);
673 #endif
674 /* XOR key with ipad and opad values */
675 for (i=0; i<64; i++) {
676 k_ipad[i] ^= 0x36;
677 k_opad[i] ^= 0x5c;
678 }
679 /*
680 * perform inner MD5
681 */
682 MD5Init(&context); /* init context for 1st pass */
683 MD5Update(&context, k_ipad, 64); /* start with inner pad */
684 MD5Update(&context, text, text_len); /* then text of datagram */
685 MD5Final(digest, &context); /* finish up 1st pass */
686 /*
687 * perform outer MD5
688 */
689 MD5Init(&context); /* init context for 2nd
690 * pass */
691 MD5Update(&context, k_opad, 64); /* start with outer pad */
692 MD5Update(&context, digest, 16); /* then results of 1st
693 * hash */
694 MD5Final(digest, &context); /* finish up 2nd pass */
695 }
696
hmac_md5(unsigned char * text,int32_t text_len,unsigned char * key,int32_t key_len)697 string Utilities::hmac_md5(
698 unsigned char* text, /* pointer to data stream */
699 int32_t text_len, /* length of data stream */
700 unsigned char* key, /* pointer to authentication key */
701 int32_t key_len) /* length of authentication key */
702 {
703
704 unsigned char MD5Result[16];
705 string MD5Sum;
706 hmac_md5(text, text_len, key, key_len,MD5Result);
707 for(uint32_t i = 0; i < 16; ++i)
708 {
709 MD5Sum += ((MD5Result[i] >> 4) < 10 ? (MD5Result[i] >> 4) + '0' : (MD5Result[i] >> 4) + ('a' - 10));
710 MD5Sum += ((MD5Result[i] & 0xF) < 10 ? (MD5Result[i] & 0xF) + '0' : (MD5Result[i] & 0xF) + ('a' - 10));
711 }
712 return MD5Sum;
713
714 }
715
716
717
718 // ENDOF HMAC_MD5
719
720
721 // START SHA512
722
723 #define SHFR(x, n) (x >> n)
724 #define ROTR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n)))
725 #define ROTL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n)))
726 #define CH(x, y, z) ((x & y) ^ (~x & z))
727 #define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
728
729 #define SHA256_F1(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
730 #define SHA256_F2(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
731 #define SHA256_F3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHFR(x, 3))
732 #define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10))
733
734 #define SHA512_F1(x) (ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
735 #define SHA512_F2(x) (ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
736 #define SHA512_F3(x) (ROTR(x, 1) ^ ROTR(x, 8) ^ SHFR(x, 7))
737 #define SHA512_F4(x) (ROTR(x, 19) ^ ROTR(x, 61) ^ SHFR(x, 6))
738
739 #define UNPACK32(x, str) \
740 { \
741 *((str) + 3) = (uint8_t) ((x) ); \
742 *((str) + 2) = (uint8_t) ((x) >> 8); \
743 *((str) + 1) = (uint8_t) ((x) >> 16); \
744 *((str) + 0) = (uint8_t) ((x) >> 24); \
745 }
746
747 #define PACK32(str, x) \
748 { \
749 *(x) = ((uint32_t) *((str) + 3) ) \
750 | ((uint32_t) *((str) + 2) << 8) \
751 | ((uint32_t) *((str) + 1) << 16) \
752 | ((uint32_t) *((str) + 0) << 24); \
753 }
754
755 #define UNPACK64(x, str) \
756 { \
757 *((str) + 7) = (uint8_t) ((x) ); \
758 *((str) + 6) = (uint8_t) ((x) >> 8); \
759 *((str) + 5) = (uint8_t) ((x) >> 16); \
760 *((str) + 4) = (uint8_t) ((x) >> 24); \
761 *((str) + 3) = (uint8_t) ((x) >> 32); \
762 *((str) + 2) = (uint8_t) ((x) >> 40); \
763 *((str) + 1) = (uint8_t) ((x) >> 48); \
764 *((str) + 0) = (uint8_t) ((x) >> 56); \
765 }
766
767 #define PACK64(str, x) \
768 { \
769 *(x) = ((uint64_t) *((str) + 7) ) \
770 | ((uint64_t) *((str) + 6) << 8) \
771 | ((uint64_t) *((str) + 5) << 16) \
772 | ((uint64_t) *((str) + 4) << 24) \
773 | ((uint64_t) *((str) + 3) << 32) \
774 | ((uint64_t) *((str) + 2) << 40) \
775 | ((uint64_t) *((str) + 1) << 48) \
776 | ((uint64_t) *((str) + 0) << 56); \
777 }
778
779 /* Macros used for loops unrolling */
780
781 #define SHA256_SCR(i) \
782 { \
783 w[i] = SHA256_F4(w[i - 2]) + w[i - 7] \
784 + SHA256_F3(w[i - 15]) + w[i - 16]; \
785 }
786
787 #define SHA512_SCR(i) \
788 { \
789 w[i] = SHA512_F4(w[i - 2]) + w[i - 7] \
790 + SHA512_F3(w[i - 15]) + w[i - 16]; \
791 }
792
793 #define SHA256_EXP(a, b, c, d, e, f, g, h, j) \
794 { \
795 t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
796 + sha256_k[j] + w[j]; \
797 t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
798 wv[d] += t1; \
799 wv[h] = t1 + t2; \
800 }
801
802 #define SHA512_EXP(a, b, c, d, e, f, g ,h, j) \
803 { \
804 t1 = wv[h] + SHA512_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
805 + sha512_k[j] + w[j]; \
806 t2 = SHA512_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]); \
807 wv[d] += t1; \
808 wv[h] = t1 + t2; \
809 }
810
811 uint32_t sha224_h0[8] =
812 {0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
813 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4};
814
815 uint32_t sha256_h0[8] =
816 {0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
817 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19};
818
819 uint64_t sha384_h0[8] =
820 {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL,
821 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
822 0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL,
823 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL};
824
825 uint64_t sha512_h0[8] =
826 {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
827 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
828 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
829 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
830
831 uint32_t sha256_k[64] =
832 {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
833 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
834 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
835 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
836 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
837 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
838 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
839 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
840 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
841 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
842 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
843 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
844 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
845 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
846 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
847 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
848
849 uint64_t sha512_k[80] =
850 {0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL,
851 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
852 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL,
853 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
854 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
855 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
856 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL,
857 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
858 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL,
859 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
860 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL,
861 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
862 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL,
863 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
864 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
865 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
866 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL,
867 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
868 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL,
869 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
870 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL,
871 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
872 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL,
873 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
874 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
875 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
876 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL,
877 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
878 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL,
879 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
880 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL,
881 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
882 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL,
883 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
884 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
885 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
886 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL,
887 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
888 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL,
889 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
890
891 /* SHA-256 functions */
892
sha256_transf(sha256_ctx * ctx,unsigned char * message,uint32_t block_nb)893 void Utilities::sha256_transf(sha256_ctx *ctx, unsigned char *message,
894 uint32_t block_nb)
895 {
896 uint32_t w[64];
897 uint32_t wv[8];
898 uint32_t t1, t2;
899 unsigned char *sub_block;
900 uint32_t i;
901
902 #ifndef UNROLL_LOOPS
903 int32_t j;
904 #endif
905
906 for (i = 1; i <= block_nb; i++) {
907 sub_block = message + ((i - 1) << 6);
908
909 #ifndef UNROLL_LOOPS
910 for (j = 0; j < 16; j++) {
911 PACK32(&sub_block[j << 2], &w[j]);
912 }
913
914 for (j = 16; j < 64; j++) {
915 SHA256_SCR(j);
916 }
917
918 for (j = 0; j < 8; j++) {
919 wv[j] = ctx->h[j];
920 }
921
922 for (j = 0; j < 64; j++) {
923 t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
924 + sha256_k[j] + w[j];
925 t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
926 wv[7] = wv[6];
927 wv[6] = wv[5];
928 wv[5] = wv[4];
929 wv[4] = wv[3] + t1;
930 wv[3] = wv[2];
931 wv[2] = wv[1];
932 wv[1] = wv[0];
933 wv[0] = t1 + t2;
934 }
935
936 for (j = 0; j < 8; j++) {
937 ctx->h[j] += wv[j];
938 }
939 #else
940 PACK32(&sub_block[ 0], &w[ 0]); PACK32(&sub_block[ 4], &w[ 1]);
941 PACK32(&sub_block[ 8], &w[ 2]); PACK32(&sub_block[12], &w[ 3]);
942 PACK32(&sub_block[16], &w[ 4]); PACK32(&sub_block[20], &w[ 5]);
943 PACK32(&sub_block[24], &w[ 6]); PACK32(&sub_block[28], &w[ 7]);
944 PACK32(&sub_block[32], &w[ 8]); PACK32(&sub_block[36], &w[ 9]);
945 PACK32(&sub_block[40], &w[10]); PACK32(&sub_block[44], &w[11]);
946 PACK32(&sub_block[48], &w[12]); PACK32(&sub_block[52], &w[13]);
947 PACK32(&sub_block[56], &w[14]); PACK32(&sub_block[60], &w[15]);
948
949 SHA256_SCR(16); SHA256_SCR(17); SHA256_SCR(18); SHA256_SCR(19);
950 SHA256_SCR(20); SHA256_SCR(21); SHA256_SCR(22); SHA256_SCR(23);
951 SHA256_SCR(24); SHA256_SCR(25); SHA256_SCR(26); SHA256_SCR(27);
952 SHA256_SCR(28); SHA256_SCR(29); SHA256_SCR(30); SHA256_SCR(31);
953 SHA256_SCR(32); SHA256_SCR(33); SHA256_SCR(34); SHA256_SCR(35);
954 SHA256_SCR(36); SHA256_SCR(37); SHA256_SCR(38); SHA256_SCR(39);
955 SHA256_SCR(40); SHA256_SCR(41); SHA256_SCR(42); SHA256_SCR(43);
956 SHA256_SCR(44); SHA256_SCR(45); SHA256_SCR(46); SHA256_SCR(47);
957 SHA256_SCR(48); SHA256_SCR(49); SHA256_SCR(50); SHA256_SCR(51);
958 SHA256_SCR(52); SHA256_SCR(53); SHA256_SCR(54); SHA256_SCR(55);
959 SHA256_SCR(56); SHA256_SCR(57); SHA256_SCR(58); SHA256_SCR(59);
960 SHA256_SCR(60); SHA256_SCR(61); SHA256_SCR(62); SHA256_SCR(63);
961
962 wv[0] = ctx->h[0]; wv[1] = ctx->h[1];
963 wv[2] = ctx->h[2]; wv[3] = ctx->h[3];
964 wv[4] = ctx->h[4]; wv[5] = ctx->h[5];
965 wv[6] = ctx->h[6]; wv[7] = ctx->h[7];
966
967 SHA256_EXP(0,1,2,3,4,5,6,7, 0); SHA256_EXP(7,0,1,2,3,4,5,6, 1);
968 SHA256_EXP(6,7,0,1,2,3,4,5, 2); SHA256_EXP(5,6,7,0,1,2,3,4, 3);
969 SHA256_EXP(4,5,6,7,0,1,2,3, 4); SHA256_EXP(3,4,5,6,7,0,1,2, 5);
970 SHA256_EXP(2,3,4,5,6,7,0,1, 6); SHA256_EXP(1,2,3,4,5,6,7,0, 7);
971 SHA256_EXP(0,1,2,3,4,5,6,7, 8); SHA256_EXP(7,0,1,2,3,4,5,6, 9);
972 SHA256_EXP(6,7,0,1,2,3,4,5,10); SHA256_EXP(5,6,7,0,1,2,3,4,11);
973 SHA256_EXP(4,5,6,7,0,1,2,3,12); SHA256_EXP(3,4,5,6,7,0,1,2,13);
974 SHA256_EXP(2,3,4,5,6,7,0,1,14); SHA256_EXP(1,2,3,4,5,6,7,0,15);
975 SHA256_EXP(0,1,2,3,4,5,6,7,16); SHA256_EXP(7,0,1,2,3,4,5,6,17);
976 SHA256_EXP(6,7,0,1,2,3,4,5,18); SHA256_EXP(5,6,7,0,1,2,3,4,19);
977 SHA256_EXP(4,5,6,7,0,1,2,3,20); SHA256_EXP(3,4,5,6,7,0,1,2,21);
978 SHA256_EXP(2,3,4,5,6,7,0,1,22); SHA256_EXP(1,2,3,4,5,6,7,0,23);
979 SHA256_EXP(0,1,2,3,4,5,6,7,24); SHA256_EXP(7,0,1,2,3,4,5,6,25);
980 SHA256_EXP(6,7,0,1,2,3,4,5,26); SHA256_EXP(5,6,7,0,1,2,3,4,27);
981 SHA256_EXP(4,5,6,7,0,1,2,3,28); SHA256_EXP(3,4,5,6,7,0,1,2,29);
982 SHA256_EXP(2,3,4,5,6,7,0,1,30); SHA256_EXP(1,2,3,4,5,6,7,0,31);
983 SHA256_EXP(0,1,2,3,4,5,6,7,32); SHA256_EXP(7,0,1,2,3,4,5,6,33);
984 SHA256_EXP(6,7,0,1,2,3,4,5,34); SHA256_EXP(5,6,7,0,1,2,3,4,35);
985 SHA256_EXP(4,5,6,7,0,1,2,3,36); SHA256_EXP(3,4,5,6,7,0,1,2,37);
986 SHA256_EXP(2,3,4,5,6,7,0,1,38); SHA256_EXP(1,2,3,4,5,6,7,0,39);
987 SHA256_EXP(0,1,2,3,4,5,6,7,40); SHA256_EXP(7,0,1,2,3,4,5,6,41);
988 SHA256_EXP(6,7,0,1,2,3,4,5,42); SHA256_EXP(5,6,7,0,1,2,3,4,43);
989 SHA256_EXP(4,5,6,7,0,1,2,3,44); SHA256_EXP(3,4,5,6,7,0,1,2,45);
990 SHA256_EXP(2,3,4,5,6,7,0,1,46); SHA256_EXP(1,2,3,4,5,6,7,0,47);
991 SHA256_EXP(0,1,2,3,4,5,6,7,48); SHA256_EXP(7,0,1,2,3,4,5,6,49);
992 SHA256_EXP(6,7,0,1,2,3,4,5,50); SHA256_EXP(5,6,7,0,1,2,3,4,51);
993 SHA256_EXP(4,5,6,7,0,1,2,3,52); SHA256_EXP(3,4,5,6,7,0,1,2,53);
994 SHA256_EXP(2,3,4,5,6,7,0,1,54); SHA256_EXP(1,2,3,4,5,6,7,0,55);
995 SHA256_EXP(0,1,2,3,4,5,6,7,56); SHA256_EXP(7,0,1,2,3,4,5,6,57);
996 SHA256_EXP(6,7,0,1,2,3,4,5,58); SHA256_EXP(5,6,7,0,1,2,3,4,59);
997 SHA256_EXP(4,5,6,7,0,1,2,3,60); SHA256_EXP(3,4,5,6,7,0,1,2,61);
998 SHA256_EXP(2,3,4,5,6,7,0,1,62); SHA256_EXP(1,2,3,4,5,6,7,0,63);
999
1000 ctx->h[0] += wv[0]; ctx->h[1] += wv[1];
1001 ctx->h[2] += wv[2]; ctx->h[3] += wv[3];
1002 ctx->h[4] += wv[4]; ctx->h[5] += wv[5];
1003 ctx->h[6] += wv[6]; ctx->h[7] += wv[7];
1004 #endif /* !UNROLL_LOOPS */
1005 }
1006 }
1007
sha256(unsigned char * message,uint32_t len,unsigned char * digest)1008 void Utilities::sha256(unsigned char *message, uint32_t len, unsigned char *digest)
1009 {
1010 sha256_ctx ctx;
1011
1012 sha256_init(&ctx);
1013 sha256_update(&ctx, message, len);
1014 sha256_final(&ctx, digest);
1015 }
1016
sha256_init(sha256_ctx * ctx)1017 void Utilities::sha256_init(sha256_ctx *ctx)
1018 {
1019 #ifndef UNROLL_LOOPS
1020 int32_t i;
1021 for (i = 0; i < 8; i++) {
1022 ctx->h[i] = sha256_h0[i];
1023 }
1024 #else
1025 ctx->h[0] = sha256_h0[0]; ctx->h[1] = sha256_h0[1];
1026 ctx->h[2] = sha256_h0[2]; ctx->h[3] = sha256_h0[3];
1027 ctx->h[4] = sha256_h0[4]; ctx->h[5] = sha256_h0[5];
1028 ctx->h[6] = sha256_h0[6]; ctx->h[7] = sha256_h0[7];
1029 #endif /* !UNROLL_LOOPS */
1030
1031 ctx->len = 0;
1032 ctx->tot_len = 0;
1033 }
1034
sha256_update(sha256_ctx * ctx,unsigned char * message,uint32_t len)1035 void Utilities::sha256_update(sha256_ctx *ctx, unsigned char *message, uint32_t len)
1036 {
1037 uint32_t block_nb;
1038 uint32_t new_len, rem_len;
1039 unsigned char *shifted_message;
1040
1041 rem_len = SHA256_BLOCK_SIZE - ctx->len;
1042
1043 memcpy(&ctx->block[ctx->len], message, rem_len);
1044
1045 if (ctx->len + len < SHA256_BLOCK_SIZE) {
1046 ctx->len += len;
1047 return;
1048 }
1049
1050 new_len = len - rem_len;
1051 block_nb = new_len / SHA256_BLOCK_SIZE;
1052
1053 shifted_message = message + rem_len;
1054
1055 sha256_transf(ctx, ctx->block, 1);
1056 sha256_transf(ctx, shifted_message, block_nb);
1057
1058 rem_len = new_len % SHA256_BLOCK_SIZE;
1059
1060 memcpy(ctx->block, &shifted_message[block_nb << 6],
1061 rem_len);
1062
1063 ctx->len = rem_len;
1064 ctx->tot_len += (block_nb + 1) << 6;
1065 }
1066
sha256_final(sha256_ctx * ctx,unsigned char * digest)1067 void Utilities::sha256_final(sha256_ctx *ctx, unsigned char *digest)
1068 {
1069 uint32_t block_nb;
1070 uint32_t pm_len;
1071 uint32_t len_b;
1072
1073 #ifndef UNROLL_LOOPS
1074 int32_t i;
1075 #endif
1076
1077 block_nb = (1 + ((SHA256_BLOCK_SIZE - 9)
1078 < (ctx->len % SHA256_BLOCK_SIZE)));
1079
1080 len_b = (ctx->tot_len + ctx->len) << 3;
1081 pm_len = block_nb << 6;
1082
1083 memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
1084 ctx->block[ctx->len] = 0x80;
1085 UNPACK32(len_b, ctx->block + pm_len - 4);
1086
1087 sha256_transf(ctx, ctx->block, block_nb);
1088
1089 #ifndef UNROLL_LOOPS
1090 for (i = 0 ; i < 8; i++) {
1091 UNPACK32(ctx->h[i], &digest[i << 2]);
1092 }
1093 #else
1094 UNPACK32(ctx->h[0], &digest[ 0]);
1095 UNPACK32(ctx->h[1], &digest[ 4]);
1096 UNPACK32(ctx->h[2], &digest[ 8]);
1097 UNPACK32(ctx->h[3], &digest[12]);
1098 UNPACK32(ctx->h[4], &digest[16]);
1099 UNPACK32(ctx->h[5], &digest[20]);
1100 UNPACK32(ctx->h[6], &digest[24]);
1101 UNPACK32(ctx->h[7], &digest[28]);
1102 #endif /* !UNROLL_LOOPS */
1103 }
1104
1105 /* SHA 512 functions*/
1106
sha512_transf(sha512_ctx * ctx,unsigned char * message,uint32_t block_nb)1107 void Utilities::sha512_transf(sha512_ctx *ctx, unsigned char *message,
1108 uint32_t block_nb)
1109 {
1110 uint64_t w[80];
1111 uint64_t wv[8];
1112 uint64_t t1, t2;
1113 unsigned char *sub_block;
1114 uint32_t i;
1115
1116 #ifndef UNROLL_LOOPS
1117 int32_t j;
1118 #endif
1119
1120 for (i = 1; i <= block_nb; i++) {
1121 sub_block = message + ((i - 1) << 7);
1122
1123 #ifndef UNROLL_LOOPS
1124 for (j = 0; j < 16; j++) {
1125 PACK64(&sub_block[j << 3], &w[j]);
1126 }
1127
1128 for (j = 16; j < 80; j++) {
1129 SHA512_SCR(j);
1130 }
1131
1132 for (j = 0; j < 8; j++) {
1133 wv[j] = ctx->h[j];
1134 }
1135
1136 for (j = 0; j < 80; j++) {
1137 t1 = wv[7] + SHA512_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
1138 + sha512_k[j] + w[j];
1139 t2 = SHA512_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
1140 wv[7] = wv[6];
1141 wv[6] = wv[5];
1142 wv[5] = wv[4];
1143 wv[4] = wv[3] + t1;
1144 wv[3] = wv[2];
1145 wv[2] = wv[1];
1146 wv[1] = wv[0];
1147 wv[0] = t1 + t2;
1148 }
1149
1150 for (j = 0; j < 8; j++) {
1151 ctx->h[j] += wv[j];
1152 }
1153 #else
1154 PACK64(&sub_block[ 0], &w[ 0]); PACK64(&sub_block[ 8], &w[ 1]);
1155 PACK64(&sub_block[ 16], &w[ 2]); PACK64(&sub_block[ 24], &w[ 3]);
1156 PACK64(&sub_block[ 32], &w[ 4]); PACK64(&sub_block[ 40], &w[ 5]);
1157 PACK64(&sub_block[ 48], &w[ 6]); PACK64(&sub_block[ 56], &w[ 7]);
1158 PACK64(&sub_block[ 64], &w[ 8]); PACK64(&sub_block[ 72], &w[ 9]);
1159 PACK64(&sub_block[ 80], &w[10]); PACK64(&sub_block[ 88], &w[11]);
1160 PACK64(&sub_block[ 96], &w[12]); PACK64(&sub_block[104], &w[13]);
1161 PACK64(&sub_block[112], &w[14]); PACK64(&sub_block[120], &w[15]);
1162
1163 SHA512_SCR(16); SHA512_SCR(17); SHA512_SCR(18); SHA512_SCR(19);
1164 SHA512_SCR(20); SHA512_SCR(21); SHA512_SCR(22); SHA512_SCR(23);
1165 SHA512_SCR(24); SHA512_SCR(25); SHA512_SCR(26); SHA512_SCR(27);
1166 SHA512_SCR(28); SHA512_SCR(29); SHA512_SCR(30); SHA512_SCR(31);
1167 SHA512_SCR(32); SHA512_SCR(33); SHA512_SCR(34); SHA512_SCR(35);
1168 SHA512_SCR(36); SHA512_SCR(37); SHA512_SCR(38); SHA512_SCR(39);
1169 SHA512_SCR(40); SHA512_SCR(41); SHA512_SCR(42); SHA512_SCR(43);
1170 SHA512_SCR(44); SHA512_SCR(45); SHA512_SCR(46); SHA512_SCR(47);
1171 SHA512_SCR(48); SHA512_SCR(49); SHA512_SCR(50); SHA512_SCR(51);
1172 SHA512_SCR(52); SHA512_SCR(53); SHA512_SCR(54); SHA512_SCR(55);
1173 SHA512_SCR(56); SHA512_SCR(57); SHA512_SCR(58); SHA512_SCR(59);
1174 SHA512_SCR(60); SHA512_SCR(61); SHA512_SCR(62); SHA512_SCR(63);
1175 SHA512_SCR(64); SHA512_SCR(65); SHA512_SCR(66); SHA512_SCR(67);
1176 SHA512_SCR(68); SHA512_SCR(69); SHA512_SCR(70); SHA512_SCR(71);
1177 SHA512_SCR(72); SHA512_SCR(73); SHA512_SCR(74); SHA512_SCR(75);
1178 SHA512_SCR(76); SHA512_SCR(77); SHA512_SCR(78); SHA512_SCR(79);
1179
1180 wv[0] = ctx->h[0]; wv[1] = ctx->h[1];
1181 wv[2] = ctx->h[2]; wv[3] = ctx->h[3];
1182 wv[4] = ctx->h[4]; wv[5] = ctx->h[5];
1183 wv[6] = ctx->h[6]; wv[7] = ctx->h[7];
1184
1185 SHA512_EXP(0,1,2,3,4,5,6,7, 0); SHA512_EXP(7,0,1,2,3,4,5,6, 1);
1186 SHA512_EXP(6,7,0,1,2,3,4,5, 2); SHA512_EXP(5,6,7,0,1,2,3,4, 3);
1187 SHA512_EXP(4,5,6,7,0,1,2,3, 4); SHA512_EXP(3,4,5,6,7,0,1,2, 5);
1188 SHA512_EXP(2,3,4,5,6,7,0,1, 6); SHA512_EXP(1,2,3,4,5,6,7,0, 7);
1189 SHA512_EXP(0,1,2,3,4,5,6,7, 8); SHA512_EXP(7,0,1,2,3,4,5,6, 9);
1190 SHA512_EXP(6,7,0,1,2,3,4,5,10); SHA512_EXP(5,6,7,0,1,2,3,4,11);
1191 SHA512_EXP(4,5,6,7,0,1,2,3,12); SHA512_EXP(3,4,5,6,7,0,1,2,13);
1192 SHA512_EXP(2,3,4,5,6,7,0,1,14); SHA512_EXP(1,2,3,4,5,6,7,0,15);
1193 SHA512_EXP(0,1,2,3,4,5,6,7,16); SHA512_EXP(7,0,1,2,3,4,5,6,17);
1194 SHA512_EXP(6,7,0,1,2,3,4,5,18); SHA512_EXP(5,6,7,0,1,2,3,4,19);
1195 SHA512_EXP(4,5,6,7,0,1,2,3,20); SHA512_EXP(3,4,5,6,7,0,1,2,21);
1196 SHA512_EXP(2,3,4,5,6,7,0,1,22); SHA512_EXP(1,2,3,4,5,6,7,0,23);
1197 SHA512_EXP(0,1,2,3,4,5,6,7,24); SHA512_EXP(7,0,1,2,3,4,5,6,25);
1198 SHA512_EXP(6,7,0,1,2,3,4,5,26); SHA512_EXP(5,6,7,0,1,2,3,4,27);
1199 SHA512_EXP(4,5,6,7,0,1,2,3,28); SHA512_EXP(3,4,5,6,7,0,1,2,29);
1200 SHA512_EXP(2,3,4,5,6,7,0,1,30); SHA512_EXP(1,2,3,4,5,6,7,0,31);
1201 SHA512_EXP(0,1,2,3,4,5,6,7,32); SHA512_EXP(7,0,1,2,3,4,5,6,33);
1202 SHA512_EXP(6,7,0,1,2,3,4,5,34); SHA512_EXP(5,6,7,0,1,2,3,4,35);
1203 SHA512_EXP(4,5,6,7,0,1,2,3,36); SHA512_EXP(3,4,5,6,7,0,1,2,37);
1204 SHA512_EXP(2,3,4,5,6,7,0,1,38); SHA512_EXP(1,2,3,4,5,6,7,0,39);
1205 SHA512_EXP(0,1,2,3,4,5,6,7,40); SHA512_EXP(7,0,1,2,3,4,5,6,41);
1206 SHA512_EXP(6,7,0,1,2,3,4,5,42); SHA512_EXP(5,6,7,0,1,2,3,4,43);
1207 SHA512_EXP(4,5,6,7,0,1,2,3,44); SHA512_EXP(3,4,5,6,7,0,1,2,45);
1208 SHA512_EXP(2,3,4,5,6,7,0,1,46); SHA512_EXP(1,2,3,4,5,6,7,0,47);
1209 SHA512_EXP(0,1,2,3,4,5,6,7,48); SHA512_EXP(7,0,1,2,3,4,5,6,49);
1210 SHA512_EXP(6,7,0,1,2,3,4,5,50); SHA512_EXP(5,6,7,0,1,2,3,4,51);
1211 SHA512_EXP(4,5,6,7,0,1,2,3,52); SHA512_EXP(3,4,5,6,7,0,1,2,53);
1212 SHA512_EXP(2,3,4,5,6,7,0,1,54); SHA512_EXP(1,2,3,4,5,6,7,0,55);
1213 SHA512_EXP(0,1,2,3,4,5,6,7,56); SHA512_EXP(7,0,1,2,3,4,5,6,57);
1214 SHA512_EXP(6,7,0,1,2,3,4,5,58); SHA512_EXP(5,6,7,0,1,2,3,4,59);
1215 SHA512_EXP(4,5,6,7,0,1,2,3,60); SHA512_EXP(3,4,5,6,7,0,1,2,61);
1216 SHA512_EXP(2,3,4,5,6,7,0,1,62); SHA512_EXP(1,2,3,4,5,6,7,0,63);
1217 SHA512_EXP(0,1,2,3,4,5,6,7,64); SHA512_EXP(7,0,1,2,3,4,5,6,65);
1218 SHA512_EXP(6,7,0,1,2,3,4,5,66); SHA512_EXP(5,6,7,0,1,2,3,4,67);
1219 SHA512_EXP(4,5,6,7,0,1,2,3,68); SHA512_EXP(3,4,5,6,7,0,1,2,69);
1220 SHA512_EXP(2,3,4,5,6,7,0,1,70); SHA512_EXP(1,2,3,4,5,6,7,0,71);
1221 SHA512_EXP(0,1,2,3,4,5,6,7,72); SHA512_EXP(7,0,1,2,3,4,5,6,73);
1222 SHA512_EXP(6,7,0,1,2,3,4,5,74); SHA512_EXP(5,6,7,0,1,2,3,4,75);
1223 SHA512_EXP(4,5,6,7,0,1,2,3,76); SHA512_EXP(3,4,5,6,7,0,1,2,77);
1224 SHA512_EXP(2,3,4,5,6,7,0,1,78); SHA512_EXP(1,2,3,4,5,6,7,0,79);
1225
1226 ctx->h[0] += wv[0]; ctx->h[1] += wv[1];
1227 ctx->h[2] += wv[2]; ctx->h[3] += wv[3];
1228 ctx->h[4] += wv[4]; ctx->h[5] += wv[5];
1229 ctx->h[6] += wv[6]; ctx->h[7] += wv[7];
1230 #endif /* !UNROLL_LOOPS */
1231 }
1232 }
1233
sha512(unsigned char * message,uint32_t len,unsigned char * digest)1234 void Utilities::sha512(unsigned char *message, uint32_t len, unsigned char *digest)
1235 {
1236 sha512_ctx ctx;
1237
1238 sha512_init(&ctx);
1239 sha512_update(&ctx, message, len);
1240 sha512_final(&ctx, digest);
1241 }
1242
sha512_init(sha512_ctx * ctx)1243 void Utilities::sha512_init(sha512_ctx *ctx)
1244 {
1245 #ifndef UNROLL_LOOPS
1246 int32_t i;
1247 for (i = 0; i < 8; i++) {
1248 ctx->h[i] = sha512_h0[i];
1249 }
1250 #else
1251 ctx->h[0] = sha512_h0[0]; ctx->h[1] = sha512_h0[1];
1252 ctx->h[2] = sha512_h0[2]; ctx->h[3] = sha512_h0[3];
1253 ctx->h[4] = sha512_h0[4]; ctx->h[5] = sha512_h0[5];
1254 ctx->h[6] = sha512_h0[6]; ctx->h[7] = sha512_h0[7];
1255 #endif /* !UNROLL_LOOPS */
1256
1257 ctx->len = 0;
1258 ctx->tot_len = 0;
1259 }
1260
sha512_update(sha512_ctx * ctx,unsigned char * message,uint32_t len)1261 void Utilities::sha512_update(sha512_ctx *ctx, unsigned char *message, uint32_t len)
1262 {
1263 uint32_t block_nb;
1264 uint32_t new_len, rem_len;
1265 unsigned char *shifted_message;
1266
1267 rem_len = SHA512_BLOCK_SIZE - ctx->len;
1268
1269 memcpy(&ctx->block[ctx->len], message, rem_len);
1270
1271 if (ctx->len + len < SHA512_BLOCK_SIZE) {
1272 ctx->len += len;
1273 return;
1274 }
1275
1276 new_len = len - rem_len;
1277 block_nb = new_len / SHA512_BLOCK_SIZE;
1278
1279 shifted_message = message + rem_len;
1280
1281 sha512_transf(ctx, ctx->block, 1);
1282 sha512_transf(ctx, shifted_message, block_nb);
1283
1284 rem_len = new_len % SHA512_BLOCK_SIZE;
1285
1286 memcpy(ctx->block, &shifted_message[block_nb << 7],
1287 rem_len);
1288
1289 ctx->len = rem_len;
1290 ctx->tot_len += (block_nb + 1) << 7;
1291 }
1292
sha512_final(sha512_ctx * ctx,unsigned char * digest)1293 void Utilities::sha512_final(sha512_ctx *ctx, unsigned char *digest)
1294 {
1295 uint32_t block_nb;
1296 uint32_t pm_len;
1297 uint32_t len_b;
1298
1299 #ifndef UNROLL_LOOPS
1300 int32_t i;
1301 #endif
1302
1303 block_nb = 1 + ((SHA512_BLOCK_SIZE - 17)
1304 < (ctx->len % SHA512_BLOCK_SIZE)) ;
1305
1306 len_b = (ctx->tot_len + ctx->len) << 3;
1307 pm_len = block_nb << 7;
1308
1309 memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
1310 ctx->block[ctx->len] = 0x80;
1311 UNPACK32(len_b, ctx->block + pm_len - 4);
1312
1313 sha512_transf(ctx, ctx->block, block_nb);
1314
1315 #ifndef UNROLL_LOOPS
1316 for (i = 0 ; i < 8; i++) {
1317 UNPACK64(ctx->h[i], &digest[i << 3]);
1318 }
1319 #else
1320 UNPACK64(ctx->h[0], &digest[ 0]);
1321 UNPACK64(ctx->h[1], &digest[ 8]);
1322 UNPACK64(ctx->h[2], &digest[16]);
1323 UNPACK64(ctx->h[3], &digest[24]);
1324 UNPACK64(ctx->h[4], &digest[32]);
1325 UNPACK64(ctx->h[5], &digest[40]);
1326 UNPACK64(ctx->h[6], &digest[48]);
1327 UNPACK64(ctx->h[7], &digest[56]);
1328 #endif /* !UNROLL_LOOPS */
1329 }
1330
1331 /* SHA-384 functions */
1332
sha384(unsigned char * message,uint32_t len,unsigned char * digest)1333 void Utilities::sha384(unsigned char *message, uint32_t len, unsigned char *digest)
1334 {
1335 sha384_ctx ctx;
1336
1337 sha384_init(&ctx);
1338 sha384_update(&ctx, message, len);
1339 sha384_final(&ctx, digest);
1340 }
1341
sha384_init(sha384_ctx * ctx)1342 void Utilities::sha384_init(sha384_ctx *ctx)
1343 {
1344 #ifndef UNROLL_LOOPS
1345 int32_t i;
1346 for (i = 0; i < 8; i++) {
1347 ctx->h[i] = sha384_h0[i];
1348 }
1349 #else
1350 ctx->h[0] = sha384_h0[0]; ctx->h[1] = sha384_h0[1];
1351 ctx->h[2] = sha384_h0[2]; ctx->h[3] = sha384_h0[3];
1352 ctx->h[4] = sha384_h0[4]; ctx->h[5] = sha384_h0[5];
1353 ctx->h[6] = sha384_h0[6]; ctx->h[7] = sha384_h0[7];
1354 #endif /* !UNROLL_LOOPS */
1355
1356 ctx->len = 0;
1357 ctx->tot_len = 0;
1358 }
1359
sha384_update(sha384_ctx * ctx,unsigned char * message,uint32_t len)1360 void Utilities::sha384_update(sha384_ctx *ctx, unsigned char *message, uint32_t len)
1361 {
1362 uint32_t block_nb;
1363 uint32_t new_len, rem_len;
1364 unsigned char *shifted_message;
1365
1366 rem_len = SHA384_BLOCK_SIZE - ctx->len;
1367
1368 memcpy(&ctx->block[ctx->len], message, rem_len);
1369
1370 if (ctx->len + len < SHA384_BLOCK_SIZE) {
1371 ctx->len += len;
1372 return;
1373 }
1374
1375 new_len = len - rem_len;
1376 block_nb = new_len / SHA384_BLOCK_SIZE;
1377
1378 shifted_message = message + rem_len;
1379
1380 sha512_transf(ctx, ctx->block, 1);
1381 sha512_transf(ctx, shifted_message, block_nb);
1382
1383 rem_len = new_len % SHA384_BLOCK_SIZE;
1384
1385 memcpy(ctx->block, &shifted_message[block_nb << 7],
1386 rem_len);
1387
1388 ctx->len = rem_len;
1389 ctx->tot_len += (block_nb + 1) << 7;
1390 }
1391
sha384_final(sha384_ctx * ctx,unsigned char * digest)1392 void Utilities::sha384_final(sha384_ctx *ctx, unsigned char *digest)
1393 {
1394 uint32_t block_nb;
1395 uint32_t pm_len;
1396 uint32_t len_b;
1397
1398 #ifndef UNROLL_LOOPS
1399 int32_t i;
1400 #endif
1401
1402 block_nb = (1 + ((SHA384_BLOCK_SIZE - 17)
1403 < (ctx->len % SHA384_BLOCK_SIZE)));
1404
1405 len_b = (ctx->tot_len + ctx->len) << 3;
1406 pm_len = block_nb << 7;
1407
1408 memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
1409 ctx->block[ctx->len] = 0x80;
1410 UNPACK32(len_b, ctx->block + pm_len - 4);
1411
1412 sha512_transf(ctx, ctx->block, block_nb);
1413
1414 #ifndef UNROLL_LOOPS
1415 for (i = 0 ; i < 6; i++) {
1416 UNPACK64(ctx->h[i], &digest[i << 3]);
1417 }
1418 #else
1419 UNPACK64(ctx->h[0], &digest[ 0]);
1420 UNPACK64(ctx->h[1], &digest[ 8]);
1421 UNPACK64(ctx->h[2], &digest[16]);
1422 UNPACK64(ctx->h[3], &digest[24]);
1423 UNPACK64(ctx->h[4], &digest[32]);
1424 UNPACK64(ctx->h[5], &digest[40]);
1425 #endif /* !UNROLL_LOOPS */
1426 }
1427
1428 /* SHA-224 functions */
1429
sha224(unsigned char * message,uint32_t len,unsigned char * digest)1430 void Utilities::sha224(unsigned char *message, uint32_t len, unsigned char *digest)
1431 {
1432 sha224_ctx ctx;
1433
1434 sha224_init(&ctx);
1435 sha224_update(&ctx, message, len);
1436 sha224_final(&ctx, digest);
1437 }
1438
sha224_init(sha224_ctx * ctx)1439 void Utilities::sha224_init(sha224_ctx *ctx)
1440 {
1441 #ifndef UNROLL_LOOPS
1442 int32_t i;
1443 for (i = 0; i < 8; i++) {
1444 ctx->h[i] = sha224_h0[i];
1445 }
1446 #else
1447 ctx->h[0] = sha224_h0[0]; ctx->h[1] = sha224_h0[1];
1448 ctx->h[2] = sha224_h0[2]; ctx->h[3] = sha224_h0[3];
1449 ctx->h[4] = sha224_h0[4]; ctx->h[5] = sha224_h0[5];
1450 ctx->h[6] = sha224_h0[6]; ctx->h[7] = sha224_h0[7];
1451 #endif /* !UNROLL_LOOPS */
1452
1453 ctx->len = 0;
1454 ctx->tot_len = 0;
1455 }
1456
sha224_update(sha224_ctx * ctx,unsigned char * message,uint32_t len)1457 void Utilities::sha224_update(sha224_ctx *ctx, unsigned char *message, uint32_t len)
1458 {
1459 uint32_t block_nb;
1460 uint32_t new_len, rem_len;
1461 unsigned char *shifted_message;
1462
1463 rem_len = SHA224_BLOCK_SIZE - ctx->len;
1464
1465 memcpy(&ctx->block[ctx->len], message, rem_len);
1466
1467 if (ctx->len + len < SHA224_BLOCK_SIZE) {
1468 ctx->len += len;
1469 return;
1470 }
1471
1472 new_len = len - rem_len;
1473 block_nb = new_len / SHA224_BLOCK_SIZE;
1474
1475 shifted_message = message + rem_len;
1476
1477 sha256_transf(ctx, ctx->block, 1);
1478 sha256_transf(ctx, shifted_message, block_nb);
1479
1480 rem_len = new_len % SHA224_BLOCK_SIZE;
1481
1482 memcpy(ctx->block, &shifted_message[block_nb << 6],
1483 rem_len);
1484
1485 ctx->len = rem_len;
1486 ctx->tot_len += (block_nb + 1) << 6;
1487 }
1488
sha224_final(sha224_ctx * ctx,unsigned char * digest)1489 void Utilities::sha224_final(sha224_ctx *ctx, unsigned char *digest)
1490 {
1491 uint32_t block_nb;
1492 uint32_t pm_len;
1493 uint32_t len_b;
1494
1495 #ifndef UNROLL_LOOPS
1496 int32_t i;
1497 #endif
1498
1499 block_nb = (1 + ((SHA224_BLOCK_SIZE - 9)
1500 < (ctx->len % SHA224_BLOCK_SIZE)));
1501
1502 len_b = (ctx->tot_len + ctx->len) << 3;
1503 pm_len = block_nb << 6;
1504
1505 memset(ctx->block + ctx->len, 0, pm_len - ctx->len);
1506 ctx->block[ctx->len] = 0x80;
1507 UNPACK32(len_b, ctx->block + pm_len - 4);
1508
1509 sha256_transf(ctx, ctx->block, block_nb);
1510
1511 #ifndef UNROLL_LOOPS
1512 for (i = 0 ; i < 7; i++) {
1513 UNPACK32(ctx->h[i], &digest[i << 2]);
1514 }
1515 #else
1516 UNPACK32(ctx->h[0], &digest[ 0]);
1517 UNPACK32(ctx->h[1], &digest[ 4]);
1518 UNPACK32(ctx->h[2], &digest[ 8]);
1519 UNPACK32(ctx->h[3], &digest[12]);
1520 UNPACK32(ctx->h[4], &digest[16]);
1521 UNPACK32(ctx->h[5], &digest[20]);
1522 UNPACK32(ctx->h[6], &digest[24]);
1523 #endif /* !UNROLL_LOOPS */
1524 }
1525
1526
sha512sum(unsigned char * msg,uint32_t len)1527 string Utilities::sha512sum(unsigned char *msg, uint32_t len)
1528 {
1529 unsigned char SHA512Result[64];
1530 sha512(msg,len,SHA512Result);
1531 string SHA512Sum = "";
1532
1533 for(uint32_t i = 0; i < 64; ++i)
1534 {
1535 SHA512Sum += ((SHA512Result[i] >> 4) < 10 ? (SHA512Result[i] >> 4) + '0' : (SHA512Result[i] >> 4) + ('a' - 10));
1536 SHA512Sum += ((SHA512Result[i] & 0xF) < 10 ? (SHA512Result[i] & 0xF) + '0' : (SHA512Result[i] & 0xF) + ('a' - 10));
1537 }
1538 return SHA512Sum;
1539
1540 }
1541
1542 // ENDOF SHA512
1543
escapeXMLString(char * str)1544 string Utilities::escapeXMLString(char *str)
1545 {
1546 string s = "";
1547 if (str == NULL )
1548 {
1549 return s;
1550 }
1551
1552 uint32_t i;
1553 uint32_t len = strlen(str);
1554
1555 for ( i = 0; i < len; i++ )
1556 {
1557 if ( str[i] == '<' )
1558 {
1559 s+="<";
1560 }else
1561 if ( str[i] == '>' )
1562 {
1563 s+=">";
1564 }else
1565 if ( str[i] == '&' )
1566 {
1567 s+="&";
1568 }else
1569 {
1570 s+= str[i];
1571 }
1572 }
1573
1574 return s;
1575 }
1576
1577
1578
1579
1580