00001
00002
00003
00004
00005 #include <string.h>
00006 #include <stdlib.h>
00007
00008 #if defined(__GNUC__) || defined(__GNU_LIBRARY__)
00009 #include <byteswap.h>
00010 #include <endian.h>
00011 #endif
00012
00013 #include "sha1.h"
00014
00015 #define SHA_LITTLE_ENDIAN 1234
00016 #define SHA_BIG_ENDIAN 4321
00017
00018 #if !defined(PLATFORM_BYTE_ORDER)
00019 #if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
00020 # if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
00021 # if defined(BYTE_ORDER)
00022 # if (BYTE_ORDER == LITTLE_ENDIAN)
00023 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00024 # elif (BYTE_ORDER == BIG_ENDIAN)
00025 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00026 # endif
00027 # endif
00028 # elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
00029 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00030 # elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
00031 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00032 # endif
00033 #elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
00034 # if defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
00035 # if defined(_BYTE_ORDER)
00036 # if (_BYTE_ORDER == _LITTLE_ENDIAN)
00037 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00038 # elif (_BYTE_ORDER == _BIG_ENDIAN)
00039 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00040 # endif
00041 # endif
00042 # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
00043 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00044 # elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
00045 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00046 # endif
00047 #elif 0
00048 #define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00049 #elif 0
00050 #define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00051 #elif (('1234' >> 24) == '1')
00052 # define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
00053 #elif (('4321' >> 24) == '1')
00054 # define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
00055 #endif
00056 #endif
00057
00058 #if !defined(PLATFORM_BYTE_ORDER)
00059 # error Please set undetermined byte order (lines 87 or 89 of sha1.c).
00060 #endif
00061
00062 #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
00063
00064 #if (PLATFORM_BYTE_ORDER == SHA_BIG_ENDIAN)
00065 #define swap_b32(x) (x)
00066 #elif defined(bswap_32)
00067 #define swap_b32(x) bswap_32(x)
00068 #else
00069 #define swap_b32(x) ((rotl32((x), 8) & 0x00ff00ff) | (rotl32((x), 24) & 0xff00ff00))
00070 #endif
00071
00072 #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
00073
00074 #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
00075 #define parity(x,y,z) ((x) ^ (y) ^ (z))
00076 #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
00077
00078 #define rnd(f,k) \
00079 t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
00080 e = d; d = c; c = rotl32(b, 30); b = t
00081
00082 void sha1_compile(sha1_ctx ctx[1])
00083 { sha1_32t w[80], i, a, b, c, d, e, t;
00084
00085
00086
00087
00088 for(i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
00089 w[i] = swap_b32(ctx->wbuf[i]);
00090
00091 for(i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
00092 w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
00093
00094 a = ctx->hash[0];
00095 b = ctx->hash[1];
00096 c = ctx->hash[2];
00097 d = ctx->hash[3];
00098 e = ctx->hash[4];
00099
00100 for(i = 0; i < 20; ++i)
00101 {
00102 rnd(ch, 0x5a827999);
00103 }
00104
00105 for(i = 20; i < 40; ++i)
00106 {
00107 rnd(parity, 0x6ed9eba1);
00108 }
00109
00110 for(i = 40; i < 60; ++i)
00111 {
00112 rnd(maj, 0x8f1bbcdc);
00113 }
00114
00115 for(i = 60; i < 80; ++i)
00116 {
00117 rnd(parity, 0xca62c1d6);
00118 }
00119
00120 ctx->hash[0] += a;
00121 ctx->hash[1] += b;
00122 ctx->hash[2] += c;
00123 ctx->hash[3] += d;
00124 ctx->hash[4] += e;
00125 }
00126
00127 void sha1_begin(sha1_ctx ctx[1])
00128 {
00129 ctx->count[0] = ctx->count[1] = 0;
00130 ctx->hash[0] = 0x67452301;
00131 ctx->hash[1] = 0xefcdab89;
00132 ctx->hash[2] = 0x98badcfe;
00133 ctx->hash[3] = 0x10325476;
00134 ctx->hash[4] = 0xc3d2e1f0;
00135 }
00136
00137 void sha1_hash(const unsigned char data[], unsigned int len, sha1_ctx ctx[1])
00138 { sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK),
00139 space = SHA1_BLOCK_SIZE - pos;
00140 const unsigned char *sp = data;
00141
00142 if((ctx->count[0] += len) < len)
00143 ++(ctx->count[1]);
00144
00145 while(len >= space)
00146 {
00147 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
00148 sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
00149 sha1_compile(ctx);
00150 }
00151
00152 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
00153 }
00154
00155 #if (PLATFORM_BYTE_ORDER == SHA_LITTLE_ENDIAN)
00156 static sha1_32t mask[4] =
00157 { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
00158 static sha1_32t bits[4] =
00159 { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
00160 #else
00161 static sha1_32t mask[4] =
00162 { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
00163 static sha1_32t bits[4] =
00164 { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
00165 #endif
00166
00167 void sha1_end(unsigned char hval[], sha1_ctx ctx[1])
00168 { sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1_MASK);
00169
00170
00171
00172
00173
00174
00175
00176
00177 ctx->wbuf[i >> 2] = (ctx->wbuf[i >> 2] & mask[i & 3]) | bits[i & 3];
00178
00179
00180
00181
00182 if(i > SHA1_BLOCK_SIZE - 9)
00183 {
00184 if(i < 60) ctx->wbuf[15] = 0;
00185 sha1_compile(ctx);
00186 i = 0;
00187 }
00188 else
00189 i = (i >> 2) + 1;
00190
00191 while(i < 14)
00192 ctx->wbuf[i++] = 0;
00193
00194
00195 ctx->wbuf[14] = swap_b32((ctx->count[1] << 3) | (ctx->count[0] >> 29));
00196 ctx->wbuf[15] = swap_b32(ctx->count[0] << 3);
00197
00198 sha1_compile(ctx);
00199
00200
00201
00202 for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
00203 hval[i] = (unsigned char)(ctx->hash[i >> 2] >> 8 * (~i & 3));
00204 }
00205
00206 void sha1(unsigned char hval[], const unsigned char data[], unsigned int len)
00207 { sha1_ctx cx[1];
00208
00209 sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
00210 }