|
|
1.1.1.2 ! root 1: /* ! 2: --------------------------------------------------------------------------- ! 3: Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved. 1.1 root 4: 1.1.1.2 ! root 5: LICENSE TERMS 1.1 root 6: 1.1.1.2 ! root 7: The free distribution and use of this software in both source and binary ! 8: form is allowed (with or without changes) provided that: 1.1 root 9: 1.1.1.2 ! root 10: 1. distributions of this source code include the above copyright ! 11: notice, this list of conditions and the following disclaimer; 1.1 root 12: 1.1.1.2 ! root 13: 2. distributions in binary form include the above copyright ! 14: notice, this list of conditions and the following disclaimer ! 15: in the documentation and/or other associated materials; 1.1 root 16: 1.1.1.2 ! root 17: 3. the copyright holder's name is not used to endorse products ! 18: built using this software without specific written permission. 1.1 root 19: 1.1.1.2 ! root 20: ALTERNATIVELY, provided that this notice is retained in full, this product ! 21: may be distributed under the terms of the GNU General Public License (GPL), ! 22: in which case the provisions of the GPL apply INSTEAD OF those given above. ! 23: ! 24: DISCLAIMER ! 25: ! 26: This software is provided 'as is' with no explicit or implied warranties ! 27: in respect of its properties, including, but not limited to, correctness ! 28: and/or fitness for purpose. ! 29: --------------------------------------------------------------------------- ! 30: Issue Date: 18/06/2004 ! 31: ! 32: This is a byte oriented version of SHA1 that operates on arrays of bytes ! 33: stored in memory. ! 34: */ ! 35: ! 36: #include <string.h> /* for memcpy() etc. */ ! 37: #include <stdlib.h> /* for _lrotl with VC++ */ ! 38: ! 39: #include "Sha1.h" ! 40: ! 41: #if defined(__cplusplus) ! 42: extern "C" ! 43: { ! 44: #endif ! 45: ! 46: /* ! 47: To obtain the highest speed on processors with 32-bit words, this code ! 48: needs to determine the order in which bytes are packed into such words. ! 49: The following block of code is an attempt to capture the most obvious ! 50: ways in which various environemnts specify their endian definitions. ! 51: It may well fail, in which case the definitions will need to be set by ! 52: editing at the points marked **** EDIT HERE IF NECESSARY **** below. ! 53: */ ! 54: ! 55: /* PLATFORM SPECIFIC INCLUDES */ ! 56: ! 57: /* Original byte order detection removed */ ! 58: #include "../Common/Endian.h" ! 59: ! 60: #define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */ ! 61: #define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */ ! 62: ! 63: #if BYTE_ORDER == LITTLE_ENDIAN ! 64: # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN ! 65: #endif ! 66: ! 67: #if BYTE_ORDER == BIG_ENDIAN ! 68: # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN ! 69: #endif ! 70: ! 71: #ifdef _MSC_VER ! 72: #pragma intrinsic(memcpy) ! 73: #endif ! 74: ! 75: #if 1 && defined(_MSC_VER) && !defined(_DEBUG) ! 76: #define rotl32 _rotl ! 77: #define rotr32 _rotr ! 78: #else ! 79: #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n))) ! 80: #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n))) ! 81: #endif ! 82: ! 83: #if !defined(bswap_32) ! 84: #define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00) ! 85: #endif ! 86: ! 87: #if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN) ! 88: #define SWAP_BYTES 1.1 root 89: #else 1.1.1.2 ! root 90: #undef SWAP_BYTES 1.1 root 91: #endif 92: 1.1.1.2 ! root 93: #if defined(SWAP_BYTES) ! 94: #define bsw_32(p,n) \ ! 95: { int _i = (n); while(_i--) ((sha1_32t*)p)[_i] = bswap_32(((sha1_32t*)p)[_i]); } ! 96: #else ! 97: #define bsw_32(p,n) ! 98: #endif 1.1 root 99: 1.1.1.2 ! root 100: #define SHA1_MASK (SHA1_BLOCK_SIZE - 1) 1.1 root 101: 1.1.1.2 ! root 102: #if 0 1.1 root 103: 1.1.1.2 ! root 104: #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z))) ! 105: #define parity(x,y,z) ((x) ^ (y) ^ (z)) ! 106: #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) ! 107: ! 108: #else /* Discovered by Rich Schroeppel and Colin Plumb */ 1.1 root 109: 1.1.1.2 ! root 110: #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) ! 111: #define parity(x,y,z) ((x) ^ (y) ^ (z)) ! 112: #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y)))) 1.1 root 113: 1.1.1.2 ! root 114: #endif ! 115: ! 116: /* Compile 64 bytes of hash data into SHA1 context. Note */ ! 117: /* that this routine assumes that the byte order in the */ ! 118: /* ctx->wbuf[] at this point is in such an order that low */ ! 119: /* address bytes in the ORIGINAL byte stream will go in */ ! 120: /* this buffer to the high end of 32-bit words on BOTH big */ ! 121: /* and little endian systems */ ! 122: ! 123: #ifdef ARRAY ! 124: #define q(v,n) v[n] ! 125: #else ! 126: #define q(v,n) v##n ! 127: #endif ! 128: ! 129: #define one_cycle(v,a,b,c,d,e,f,k,h) \ ! 130: q(v,e) += rotr32(q(v,a),27) + \ ! 131: f(q(v,b),q(v,c),q(v,d)) + k + h; \ ! 132: q(v,b) = rotr32(q(v,b), 2) ! 133: ! 134: #define five_cycle(v,f,k,i) \ ! 135: one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \ ! 136: one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \ ! 137: one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \ ! 138: one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \ ! 139: one_cycle(v, 1,2,3,4,0, f,k,hf(i+4)) ! 140: ! 141: void sha1_compile(sha1_ctx ctx[1]) ! 142: { sha1_32t *w = ctx->wbuf; ! 143: ! 144: #ifdef ARRAY ! 145: sha1_32t v[5]; ! 146: memcpy(v, ctx->hash, 5 * sizeof(sha1_32t)); ! 147: #else ! 148: sha1_32t v0, v1, v2, v3, v4; ! 149: v0 = ctx->hash[0]; v1 = ctx->hash[1]; ! 150: v2 = ctx->hash[2]; v3 = ctx->hash[3]; ! 151: v4 = ctx->hash[4]; ! 152: #endif ! 153: ! 154: #define hf(i) w[i] ! 155: ! 156: five_cycle(v, ch, 0x5a827999, 0); ! 157: five_cycle(v, ch, 0x5a827999, 5); ! 158: five_cycle(v, ch, 0x5a827999, 10); ! 159: one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \ ! 160: ! 161: #undef hf ! 162: #define hf(i) (w[(i) & 15] = rotl32( \ ! 163: w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \ ! 164: ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1)) ! 165: ! 166: one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16)); ! 167: one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17)); ! 168: one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18)); ! 169: one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19)); ! 170: ! 171: five_cycle(v, parity, 0x6ed9eba1, 20); ! 172: five_cycle(v, parity, 0x6ed9eba1, 25); ! 173: five_cycle(v, parity, 0x6ed9eba1, 30); ! 174: five_cycle(v, parity, 0x6ed9eba1, 35); ! 175: ! 176: five_cycle(v, maj, 0x8f1bbcdc, 40); ! 177: five_cycle(v, maj, 0x8f1bbcdc, 45); ! 178: five_cycle(v, maj, 0x8f1bbcdc, 50); ! 179: five_cycle(v, maj, 0x8f1bbcdc, 55); ! 180: ! 181: five_cycle(v, parity, 0xca62c1d6, 60); ! 182: five_cycle(v, parity, 0xca62c1d6, 65); ! 183: five_cycle(v, parity, 0xca62c1d6, 70); ! 184: five_cycle(v, parity, 0xca62c1d6, 75); ! 185: ! 186: #ifdef ARRAY ! 187: ctx->hash[0] += v[0]; ctx->hash[1] += v[1]; ! 188: ctx->hash[2] += v[2]; ctx->hash[3] += v[3]; ! 189: ctx->hash[4] += v[4]; ! 190: #else ! 191: ctx->hash[0] += v0; ctx->hash[1] += v1; ! 192: ctx->hash[2] += v2; ctx->hash[3] += v3; ! 193: ctx->hash[4] += v4; ! 194: #endif ! 195: } ! 196: ! 197: void sha1_begin(sha1_ctx ctx[1]) 1.1 root 198: { 1.1.1.2 ! root 199: ctx->count[0] = ctx->count[1] = 0; ! 200: ctx->hash[0] = 0x67452301; ! 201: ctx->hash[1] = 0xefcdab89; ! 202: ctx->hash[2] = 0x98badcfe; ! 203: ctx->hash[3] = 0x10325476; ! 204: ctx->hash[4] = 0xc3d2e1f0; 1.1 root 205: } 206: 1.1.1.2 ! root 207: /* SHA1 hash data in an array of bytes into hash buffer and */ ! 208: /* call the hash_compile function as required. */ 1.1 root 209: 1.1.1.2 ! root 210: void sha1_hash(const unsigned char data[], unsigned __int32 len, sha1_ctx ctx[1]) ! 211: { sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK), ! 212: space = SHA1_BLOCK_SIZE - pos; ! 213: const unsigned char *sp = data; 1.1 root 214: 1.1.1.2 ! root 215: if((ctx->count[0] += len) < len) ! 216: ++(ctx->count[1]); 1.1 root 217: 1.1.1.2 ! root 218: while(len >= space) /* tranfer whole blocks if possible */ 1.1 root 219: { 1.1.1.2 ! root 220: memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space); ! 221: sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0; ! 222: bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2); ! 223: sha1_compile(ctx); 1.1 root 224: } 225: 1.1.1.2 ! root 226: memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len); ! 227: } 1.1 root 228: 1.1.1.2 ! root 229: /* SHA1 final padding and digest calculation */ 1.1 root 230: 1.1.1.2 ! root 231: void sha1_end(unsigned char hval[], sha1_ctx ctx[1]) ! 232: { sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1_MASK); 1.1 root 233: 1.1.1.2 ! root 234: /* put bytes in the buffer in an order in which references to */ ! 235: /* 32-bit words will put bytes with lower addresses into the */ ! 236: /* top of 32 bit words on BOTH big and little endian machines */ ! 237: bsw_32(ctx->wbuf, (i + 3) >> 2); ! 238: ! 239: /* we now need to mask valid bytes and add the padding which is */ ! 240: /* a single 1 bit and as many zero bits as necessary. Note that */ ! 241: /* we can always add the first padding byte here because the */ ! 242: /* buffer always has at least one empty slot */ ! 243: ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3); ! 244: ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3); ! 245: ! 246: /* we need 9 or more empty positions, one for the padding byte */ ! 247: /* (above) and eight for the length count. If there is not */ ! 248: /* enough space, pad and empty the buffer */ ! 249: if(i > SHA1_BLOCK_SIZE - 9) 1.1 root 250: { 1.1.1.2 ! root 251: if(i < 60) ctx->wbuf[15] = 0; ! 252: sha1_compile(ctx); ! 253: i = 0; 1.1 root 254: } 1.1.1.2 ! root 255: else /* compute a word index for the empty buffer positions */ ! 256: i = (i >> 2) + 1; ! 257: ! 258: while(i < 14) /* and zero pad all but last two positions */ ! 259: ctx->wbuf[i++] = 0; ! 260: ! 261: /* the following 32-bit length fields are assembled in the */ ! 262: /* wrong byte order on little endian machines but this is */ ! 263: /* corrected later since they are only ever used as 32-bit */ ! 264: /* word values. */ ! 265: ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29); ! 266: ctx->wbuf[15] = ctx->count[0] << 3; ! 267: sha1_compile(ctx); ! 268: ! 269: /* extract the hash value as bytes in case the hash buffer is */ ! 270: /* misaligned for 32-bit words */ ! 271: for(i = 0; i < SHA1_DIGEST_SIZE; ++i) ! 272: hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3))); ! 273: } 1.1 root 274: 1.1.1.2 ! root 275: void sha1(unsigned char hval[], const unsigned char data[], unsigned __int32 len) ! 276: { sha1_ctx cx[1]; 1.1 root 277: 1.1.1.2 ! root 278: sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx); 1.1 root 279: } 1.1.1.2 ! root 280: ! 281: #if defined(__cplusplus) ! 282: } ! 283: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.