00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "inspircd.h"
00018 #ifdef HAS_STDINT
00019 #include <stdint.h>
00020 #endif
00021 #include "m_hash.h"
00022
00023
00024 #define F1(x, y, z) (z ^ (x & (y ^ z)))
00025 #define F2(x, y, z) F1(z, x, y)
00026 #define F3(x, y, z) (x ^ y ^ z)
00027 #define F4(x, y, z) (y ^ (x | ~z))
00028
00029
00030 #define MD5STEP(f,w,x,y,z,in,s) \
00031 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
00032
00033 #ifndef HAS_STDINT
00034 typedef unsigned int uint32_t;
00035 #endif
00036
00037 typedef uint32_t word32;
00038 typedef unsigned char byte;
00039
00042 class MD5Context : public classbase
00043 {
00044 public:
00045 word32 buf[4];
00046 word32 bytes[2];
00047 word32 in[16];
00048 };
00049
00050 class ModuleMD5 : public Module
00051 {
00052 void byteSwap(word32 *buf, unsigned words)
00053 {
00054 byte *p = (byte *)buf;
00055
00056 do
00057 {
00058 *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
00059 ((unsigned)p[1] << 8 | p[0]);
00060 p += 4;
00061 } while (--words);
00062 }
00063
00064 void MD5Init(MD5Context *ctx, unsigned int* ikey = NULL)
00065 {
00066
00067 if (!ikey)
00068 {
00069 ctx->buf[0] = 0x67452301;
00070 ctx->buf[1] = 0xefcdab89;
00071 ctx->buf[2] = 0x98badcfe;
00072 ctx->buf[3] = 0x10325476;
00073 }
00074 else
00075 {
00076 ctx->buf[0] = ikey[0];
00077 ctx->buf[1] = ikey[1];
00078 ctx->buf[2] = ikey[2];
00079 ctx->buf[3] = ikey[3];
00080 }
00081
00082 ctx->bytes[0] = 0;
00083 ctx->bytes[1] = 0;
00084 }
00085
00086 void MD5Update(MD5Context *ctx, byte const *buf, int len)
00087 {
00088 word32 t;
00089
00090
00091
00092 t = ctx->bytes[0];
00093 if ((ctx->bytes[0] = t + len) < t)
00094 ctx->bytes[1]++;
00095
00096 t = 64 - (t & 0x3f);
00097 if ((unsigned)t > (unsigned)len)
00098 {
00099 memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, len);
00100 return;
00101 }
00102
00103 memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, (unsigned)t);
00104 byteSwap(ctx->in, 16);
00105 MD5Transform(ctx->buf, ctx->in);
00106 buf += (unsigned)t;
00107 len -= (unsigned)t;
00108
00109
00110 while (len >= 64)
00111 {
00112 memcpy(ctx->in, buf, 64);
00113 byteSwap(ctx->in, 16);
00114 MD5Transform(ctx->buf, ctx->in);
00115 buf += 64;
00116 len -= 64;
00117 }
00118
00119
00120 memcpy(ctx->in, buf, len);
00121 }
00122
00123 void MD5Final(byte digest[16], MD5Context *ctx)
00124 {
00125 int count = (int)(ctx->bytes[0] & 0x3f);
00126 byte *p = (byte *)ctx->in + count;
00127
00128
00129 *p++ = 0x80;
00130
00131
00132 count = 56 - 1 - count;
00133
00134 if (count < 0)
00135 {
00136 memset(p, 0, count+8);
00137 byteSwap(ctx->in, 16);
00138 MD5Transform(ctx->buf, ctx->in);
00139 p = (byte *)ctx->in;
00140 count = 56;
00141 }
00142 memset(p, 0, count+8);
00143 byteSwap(ctx->in, 14);
00144
00145
00146 ctx->in[14] = ctx->bytes[0] << 3;
00147 ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
00148 MD5Transform(ctx->buf, ctx->in);
00149
00150 byteSwap(ctx->buf, 4);
00151 memcpy(digest, ctx->buf, 16);
00152 memset(ctx, 0, sizeof(ctx));
00153 }
00154
00155 void MD5Transform(word32 buf[4], word32 const in[16])
00156 {
00157 register word32 a, b, c, d;
00158
00159 a = buf[0];
00160 b = buf[1];
00161 c = buf[2];
00162 d = buf[3];
00163
00164 MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
00165 MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
00166 MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
00167 MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
00168 MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
00169 MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
00170 MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
00171 MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
00172 MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
00173 MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
00174 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
00175 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
00176 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
00177 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
00178 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
00179 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
00180
00181 MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
00182 MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
00183 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
00184 MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
00185 MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
00186 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
00187 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
00188 MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
00189 MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
00190 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
00191 MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
00192 MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
00193 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
00194 MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
00195 MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
00196 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
00197
00198 MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
00199 MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
00200 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
00201 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
00202 MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
00203 MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
00204 MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
00205 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
00206 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
00207 MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
00208 MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
00209 MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
00210 MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
00211 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
00212 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
00213 MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
00214
00215 MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
00216 MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
00217 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
00218 MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
00219 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
00220 MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
00221 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
00222 MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
00223 MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
00224 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
00225 MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
00226 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
00227 MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
00228 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
00229 MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
00230 MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
00231
00232 buf[0] += a;
00233 buf[1] += b;
00234 buf[2] += c;
00235 buf[3] += d;
00236 }
00237
00238
00239 void MyMD5(void *dest, void *orig, int len, unsigned int* ikey)
00240 {
00241 MD5Context context;
00242 MD5Init(&context, ikey);
00243 MD5Update(&context, (const unsigned char*)orig, len);
00244 MD5Final((unsigned char*)dest, &context);
00245 }
00246
00247
00248 void GenHash(const char* src, char* dest, const char* xtab, unsigned int* ikey, size_t srclen)
00249 {
00250 unsigned char bytes[16];
00251
00252 MyMD5((char*)bytes, (void*)src, srclen, ikey);
00253
00254 for (int i = 0; i < 16; i++)
00255 {
00256 *dest++ = xtab[bytes[i] / 16];
00257 *dest++ = xtab[bytes[i] % 16];
00258 }
00259 *dest++ = 0;
00260 }
00261
00262 unsigned int *key;
00263 char* chars;
00264
00265 public:
00266
00267 ModuleMD5(InspIRCd* Me)
00268 : Module(Me), key(NULL), chars(NULL)
00269 {
00270 ServerInstance->Modules->PublishInterface("HashRequest", this);
00271 Implementation eventlist[] = { I_OnRequest };
00272 ServerInstance->Modules->Attach(eventlist, this, 1);
00273 }
00274
00275 virtual ~ModuleMD5()
00276 {
00277 ServerInstance->Modules->UnpublishInterface("HashRequest", this);
00278 }
00279
00280
00281 virtual const char* OnRequest(Request* request)
00282 {
00283 HashRequest* MD5 = (HashRequest*)request;
00284
00285 if (strcmp("KEY", request->GetId()) == 0)
00286 {
00287 this->key = (unsigned int*)MD5->GetKeyData();
00288 }
00289 else if (strcmp("HEX", request->GetId()) == 0)
00290 {
00291 this->chars = (char*)MD5->GetOutputs();
00292 }
00293 else if (strcmp("SUM", request->GetId()) == 0)
00294 {
00295 static char data[MAXBUF];
00296 GenHash(MD5->GetHashData().data(), data, chars ? chars : "0123456789abcdef", key, MD5->GetHashData().length());
00297 return data;
00298 }
00299 else if (strcmp("NAME", request->GetId()) == 0)
00300 {
00301 return "md5";
00302 }
00303 else if (strcmp("RESET", request->GetId()) == 0)
00304 {
00305 this->chars = NULL;
00306 this->key = NULL;
00307 }
00308 return NULL;
00309 }
00310
00311 virtual Version GetVersion()
00312 {
00313 return Version("$Id: m_md5.cpp 10291 2008-08-25 20:35:51Z w00t $",VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
00314 }
00315 };
00316
00317 MODULE_INIT(ModuleMD5)