|
|
1.1.1.2 ! root 1: /* ! 2: * Checksumming functions. ! 3: * ! 4: * Source of the SHA1 code: xyssl.org ! 5: * ! 6: * FIPS-180-1 compliant SHA-1 implementation ! 7: * ! 8: * Copyright (C) 2006-2007 Christophe Devine ! 9: */ 1.1 root 10: 11: #include "sysconfig.h" 12: #include "sysdeps.h" 13: 1.1.1.2 ! root 14: #include "crc32.h" ! 15: ! 16: static unsigned long crc_table32[256]; ! 17: static unsigned short crc_table16[256]; 1.1 root 18: static void make_crc_table() 19: { 20: unsigned long c; 1.1.1.2 ! root 21: unsigned short w; 1.1 root 22: int n, k; 1.1.1.2 ! root 23: for (n = 0; n < 256; n++) { 1.1 root 24: c = (unsigned long)n; 1.1.1.2 ! root 25: w = n << 8; ! 26: for (k = 0; k < 8; k++) { ! 27: c = (c >> 1) ^ (c & 1 ? 0xedb88320 : 0); ! 28: w = (w << 1) ^ ((w & 0x8000) ? 0x1021 : 0); ! 29: } ! 30: crc_table32[n] = c; ! 31: crc_table16[n] = w; 1.1 root 32: } 33: } 1.1.1.2 ! root 34: 1.1 root 35: uae_u32 get_crc32 (uae_u8 *buf, int len) 36: { 37: uae_u32 crc; 1.1.1.2 ! root 38: if (!crc_table32[1]) ! 39: make_crc_table (); 1.1 root 40: crc = 0xffffffff; 41: while (len-- > 0) { 1.1.1.2 ! root 42: crc = crc_table32[(crc ^ (*buf++)) & 0xff] ^ (crc >> 8); 1.1 root 43: } 44: return crc ^ 0xffffffff; 45: } 1.1.1.2 ! root 46: ! 47: uae_u16 get_crc16 (uae_u8 *buf, int len) ! 48: { ! 49: uae_u16 crc; ! 50: if (!crc_table32[1]) ! 51: make_crc_table (); ! 52: crc = 0xffff; ! 53: while (len-- > 0) ! 54: crc = (crc << 8) ^ crc_table16[((crc >> 8) ^ (*buf++)) & 0xff]; ! 55: return crc; ! 56: } ! 57: ! 58: #ifndef GET_UINT32_BE ! 59: #define GET_UINT32_BE(n,b,i) \ ! 60: { \ ! 61: (n) = ( (unsigned long) (b)[(i) ] << 24 ) \ ! 62: | ( (unsigned long) (b)[(i) + 1] << 16 ) \ ! 63: | ( (unsigned long) (b)[(i) + 2] << 8 ) \ ! 64: | ( (unsigned long) (b)[(i) + 3] ); \ ! 65: } ! 66: #endif ! 67: #ifndef PUT_UINT32_BE ! 68: #define PUT_UINT32_BE(n,b,i) \ ! 69: { \ ! 70: (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ ! 71: (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ ! 72: (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ ! 73: (b)[(i) + 3] = (unsigned char) ( (n) ); \ ! 74: } ! 75: #endif ! 76: ! 77: typedef struct ! 78: { ! 79: unsigned long total[2]; /*!< number of bytes processed */ ! 80: unsigned long state[5]; /*!< intermediate digest state */ ! 81: unsigned char buffer[64]; /*!< data block being processed */ ! 82: } ! 83: sha1_context; ! 84: ! 85: static void sha1_starts( sha1_context *ctx ) ! 86: { ! 87: ctx->total[0] = 0; ! 88: ctx->total[1] = 0; ! 89: ! 90: ctx->state[0] = 0x67452301; ! 91: ctx->state[1] = 0xEFCDAB89; ! 92: ctx->state[2] = 0x98BADCFE; ! 93: ctx->state[3] = 0x10325476; ! 94: ctx->state[4] = 0xC3D2E1F0; ! 95: } ! 96: ! 97: static void sha1_process( sha1_context *ctx, unsigned char data[64] ) ! 98: { ! 99: unsigned long temp, W[16], A, B, C, D, E; ! 100: ! 101: GET_UINT32_BE( W[0], data, 0 ); ! 102: GET_UINT32_BE( W[1], data, 4 ); ! 103: GET_UINT32_BE( W[2], data, 8 ); ! 104: GET_UINT32_BE( W[3], data, 12 ); ! 105: GET_UINT32_BE( W[4], data, 16 ); ! 106: GET_UINT32_BE( W[5], data, 20 ); ! 107: GET_UINT32_BE( W[6], data, 24 ); ! 108: GET_UINT32_BE( W[7], data, 28 ); ! 109: GET_UINT32_BE( W[8], data, 32 ); ! 110: GET_UINT32_BE( W[9], data, 36 ); ! 111: GET_UINT32_BE( W[10], data, 40 ); ! 112: GET_UINT32_BE( W[11], data, 44 ); ! 113: GET_UINT32_BE( W[12], data, 48 ); ! 114: GET_UINT32_BE( W[13], data, 52 ); ! 115: GET_UINT32_BE( W[14], data, 56 ); ! 116: GET_UINT32_BE( W[15], data, 60 ); ! 117: ! 118: #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) ! 119: ! 120: #define R(t) \ ! 121: ( \ ! 122: temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \ ! 123: W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \ ! 124: ( W[t & 0x0F] = S(temp,1) ) \ ! 125: ) ! 126: ! 127: #define P(a,b,c,d,e,x) \ ! 128: { \ ! 129: e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ ! 130: } ! 131: ! 132: A = ctx->state[0]; ! 133: B = ctx->state[1]; ! 134: C = ctx->state[2]; ! 135: D = ctx->state[3]; ! 136: E = ctx->state[4]; ! 137: ! 138: #define F(x,y,z) (z ^ (x & (y ^ z))) ! 139: #define K 0x5A827999 ! 140: ! 141: P( A, B, C, D, E, W[0] ); ! 142: P( E, A, B, C, D, W[1] ); ! 143: P( D, E, A, B, C, W[2] ); ! 144: P( C, D, E, A, B, W[3] ); ! 145: P( B, C, D, E, A, W[4] ); ! 146: P( A, B, C, D, E, W[5] ); ! 147: P( E, A, B, C, D, W[6] ); ! 148: P( D, E, A, B, C, W[7] ); ! 149: P( C, D, E, A, B, W[8] ); ! 150: P( B, C, D, E, A, W[9] ); ! 151: P( A, B, C, D, E, W[10] ); ! 152: P( E, A, B, C, D, W[11] ); ! 153: P( D, E, A, B, C, W[12] ); ! 154: P( C, D, E, A, B, W[13] ); ! 155: P( B, C, D, E, A, W[14] ); ! 156: P( A, B, C, D, E, W[15] ); ! 157: P( E, A, B, C, D, R(16) ); ! 158: P( D, E, A, B, C, R(17) ); ! 159: P( C, D, E, A, B, R(18) ); ! 160: P( B, C, D, E, A, R(19) ); ! 161: ! 162: #undef K ! 163: #undef F ! 164: ! 165: #define F(x,y,z) (x ^ y ^ z) ! 166: #define K 0x6ED9EBA1 ! 167: ! 168: P( A, B, C, D, E, R(20) ); ! 169: P( E, A, B, C, D, R(21) ); ! 170: P( D, E, A, B, C, R(22) ); ! 171: P( C, D, E, A, B, R(23) ); ! 172: P( B, C, D, E, A, R(24) ); ! 173: P( A, B, C, D, E, R(25) ); ! 174: P( E, A, B, C, D, R(26) ); ! 175: P( D, E, A, B, C, R(27) ); ! 176: P( C, D, E, A, B, R(28) ); ! 177: P( B, C, D, E, A, R(29) ); ! 178: P( A, B, C, D, E, R(30) ); ! 179: P( E, A, B, C, D, R(31) ); ! 180: P( D, E, A, B, C, R(32) ); ! 181: P( C, D, E, A, B, R(33) ); ! 182: P( B, C, D, E, A, R(34) ); ! 183: P( A, B, C, D, E, R(35) ); ! 184: P( E, A, B, C, D, R(36) ); ! 185: P( D, E, A, B, C, R(37) ); ! 186: P( C, D, E, A, B, R(38) ); ! 187: P( B, C, D, E, A, R(39) ); ! 188: ! 189: #undef K ! 190: #undef F ! 191: ! 192: #define F(x,y,z) ((x & y) | (z & (x | y))) ! 193: #define K 0x8F1BBCDC ! 194: ! 195: P( A, B, C, D, E, R(40) ); ! 196: P( E, A, B, C, D, R(41) ); ! 197: P( D, E, A, B, C, R(42) ); ! 198: P( C, D, E, A, B, R(43) ); ! 199: P( B, C, D, E, A, R(44) ); ! 200: P( A, B, C, D, E, R(45) ); ! 201: P( E, A, B, C, D, R(46) ); ! 202: P( D, E, A, B, C, R(47) ); ! 203: P( C, D, E, A, B, R(48) ); ! 204: P( B, C, D, E, A, R(49) ); ! 205: P( A, B, C, D, E, R(50) ); ! 206: P( E, A, B, C, D, R(51) ); ! 207: P( D, E, A, B, C, R(52) ); ! 208: P( C, D, E, A, B, R(53) ); ! 209: P( B, C, D, E, A, R(54) ); ! 210: P( A, B, C, D, E, R(55) ); ! 211: P( E, A, B, C, D, R(56) ); ! 212: P( D, E, A, B, C, R(57) ); ! 213: P( C, D, E, A, B, R(58) ); ! 214: P( B, C, D, E, A, R(59) ); ! 215: ! 216: #undef K ! 217: #undef F ! 218: ! 219: #define F(x,y,z) (x ^ y ^ z) ! 220: #define K 0xCA62C1D6 ! 221: ! 222: P( A, B, C, D, E, R(60) ); ! 223: P( E, A, B, C, D, R(61) ); ! 224: P( D, E, A, B, C, R(62) ); ! 225: P( C, D, E, A, B, R(63) ); ! 226: P( B, C, D, E, A, R(64) ); ! 227: P( A, B, C, D, E, R(65) ); ! 228: P( E, A, B, C, D, R(66) ); ! 229: P( D, E, A, B, C, R(67) ); ! 230: P( C, D, E, A, B, R(68) ); ! 231: P( B, C, D, E, A, R(69) ); ! 232: P( A, B, C, D, E, R(70) ); ! 233: P( E, A, B, C, D, R(71) ); ! 234: P( D, E, A, B, C, R(72) ); ! 235: P( C, D, E, A, B, R(73) ); ! 236: P( B, C, D, E, A, R(74) ); ! 237: P( A, B, C, D, E, R(75) ); ! 238: P( E, A, B, C, D, R(76) ); ! 239: P( D, E, A, B, C, R(77) ); ! 240: P( C, D, E, A, B, R(78) ); ! 241: P( B, C, D, E, A, R(79) ); ! 242: ! 243: #undef K ! 244: #undef F ! 245: ! 246: ctx->state[0] += A; ! 247: ctx->state[1] += B; ! 248: ctx->state[2] += C; ! 249: ctx->state[3] += D; ! 250: ctx->state[4] += E; ! 251: } ! 252: ! 253: /* ! 254: * SHA-1 process buffer ! 255: */ ! 256: static void sha1_update( sha1_context *ctx, unsigned char *input, int ilen ) ! 257: { ! 258: int fill; ! 259: unsigned long left; ! 260: ! 261: if( ilen <= 0 ) ! 262: return; ! 263: ! 264: left = ctx->total[0] & 0x3F; ! 265: fill = 64 - left; ! 266: ! 267: ctx->total[0] += ilen; ! 268: ctx->total[0] &= 0xFFFFFFFF; ! 269: ! 270: if( ctx->total[0] < (unsigned long) ilen ) ! 271: ctx->total[1]++; ! 272: ! 273: if( left && ilen >= fill ) ! 274: { ! 275: memcpy( (void *) (ctx->buffer + left), ! 276: (void *) input, fill ); ! 277: sha1_process( ctx, ctx->buffer ); ! 278: input += fill; ! 279: ilen -= fill; ! 280: left = 0; ! 281: } ! 282: ! 283: while( ilen >= 64 ) ! 284: { ! 285: sha1_process( ctx, input ); ! 286: input += 64; ! 287: ilen -= 64; ! 288: } ! 289: ! 290: if( ilen > 0 ) ! 291: { ! 292: memcpy( (void *) (ctx->buffer + left), ! 293: (void *) input, ilen ); ! 294: } ! 295: } ! 296: ! 297: static const unsigned char sha1_padding[64] = ! 298: { ! 299: 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ! 300: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ! 301: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ! 302: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ! 303: }; ! 304: ! 305: /* ! 306: * SHA-1 final digest ! 307: */ ! 308: static void sha1_finish( sha1_context *ctx, unsigned char output[20] ) ! 309: { ! 310: unsigned long last, padn; ! 311: unsigned long high, low; ! 312: unsigned char msglen[8]; ! 313: ! 314: high = ( ctx->total[0] >> 29 ) ! 315: | ( ctx->total[1] << 3 ); ! 316: low = ( ctx->total[0] << 3 ); ! 317: ! 318: PUT_UINT32_BE( high, msglen, 0 ); ! 319: PUT_UINT32_BE( low, msglen, 4 ); ! 320: ! 321: last = ctx->total[0] & 0x3F; ! 322: padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); ! 323: ! 324: sha1_update( ctx, (unsigned char *) sha1_padding, padn ); ! 325: sha1_update( ctx, msglen, 8 ); ! 326: ! 327: PUT_UINT32_BE( ctx->state[0], output, 0 ); ! 328: PUT_UINT32_BE( ctx->state[1], output, 4 ); ! 329: PUT_UINT32_BE( ctx->state[2], output, 8 ); ! 330: PUT_UINT32_BE( ctx->state[3], output, 12 ); ! 331: PUT_UINT32_BE( ctx->state[4], output, 16 ); ! 332: } ! 333: ! 334: void get_sha1(uae_u8 *input, int len, uae_u8 *out) ! 335: { ! 336: sha1_context ctx; ! 337: ! 338: sha1_starts( &ctx ); ! 339: sha1_update( &ctx, input, len ); ! 340: sha1_finish( &ctx, out ); ! 341: } ! 342: char *get_sha1_txt(uae_u8 *input, int len) ! 343: { ! 344: static char outtxt[SHA1_SIZE * 2 + 1]; ! 345: uae_u8 out[SHA1_SIZE]; ! 346: int i; ! 347: char *p; ! 348: ! 349: p = outtxt; ! 350: get_sha1(input, len, out); ! 351: for (i = 0; i < SHA1_SIZE; i++) { ! 352: sprintf(p, "%02X", out[i]); ! 353: p += 2; ! 354: } ! 355: *p = 0; ! 356: return outtxt; ! 357: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.