1 /* +------------------------------------+
\r * | Inspire Internet Relay Chat Daemon |
\r * +------------------------------------+
\r *
\r * InspIRCd: (C) 2002-2007 InspIRCd Development Team
\r * See: http://www.inspircd.org/wiki/index.php/Credits
\r *
\r * This program is free but copyrighted software; see
\r * the file COPYING for details.
\r *
\r * ---------------------------------------------------
\r */
\r\r/* $ModDesc: Allows for MD5 encrypted oper passwords */
\r/* $ModDep: m_hash.h */
\r\r#include "inspircd.h"
\r#ifdef HAS_STDINT
\r#include <stdint.h>
\r#endif
\r#include "users.h"
\r#include "channels.h"
\r#include "modules.h"
\r#include "m_hash.h"
\r\r/* The four core functions - F1 is optimized somewhat */
\r#define F1(x, y, z) (z ^ (x & (y ^ z)))
\r#define F2(x, y, z) F1(z, x, y)
\r#define F3(x, y, z) (x ^ y ^ z)
\r#define F4(x, y, z) (y ^ (x | ~z))
\r\r/* This is the central step in the MD5 algorithm. */
\r#define MD5STEP(f,w,x,y,z,in,s) \
\r (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
\r\r#ifndef HAS_STDINT
\rtypedef unsigned int uint32_t;
\r#endif
\r\rtypedef uint32_t word32; /* NOT unsigned long. We don't support 16 bit platforms, anyway. */
\rtypedef unsigned char byte;
\r\r/** An MD5 context, used by m_opermd5
\r */
\rclass MD5Context : public classbase
\r{
\r public:
\r word32 buf[4];
\r word32 bytes[2];
\r word32 in[16];
\r};
\r\rclass ModuleMD5 : public Module
\r{
\r void byteSwap(word32 *buf, unsigned words)
\r {
\r byte *p = (byte *)buf;
\r \r do
\r {
\r *buf++ = (word32)((unsigned)p[3] << 8 | p[2]) << 16 |
\r ((unsigned)p[1] << 8 | p[0]);
\r p += 4;
\r } while (--words);
\r }
\r\r void MD5Init(MD5Context *ctx, unsigned int* key = NULL)
\r {
\r /* These are the defaults for md5 */
\r if (!key)
\r {
\r ctx->buf[0] = 0x67452301;
\r ctx->buf[1] = 0xefcdab89;
\r ctx->buf[2] = 0x98badcfe;
\r ctx->buf[3] = 0x10325476;
\r }
\r else
\r {
\r ctx->buf[0] = key[0];
\r ctx->buf[1] = key[1];
\r ctx->buf[2] = key[2];
\r ctx->buf[3] = key[3];
\r }
\r \r ctx->bytes[0] = 0;
\r ctx->bytes[1] = 0;
\r }
\r\r void MD5Update(MD5Context *ctx, byte const *buf, int len)
\r {
\r word32 t;
\r \r /* Update byte count */
\r \r t = ctx->bytes[0];
\r if ((ctx->bytes[0] = t + len) < t)
\r ctx->bytes[1]++; /* Carry from low to high */
\r \r t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
\r if ((unsigned)t > (unsigned)len)
\r {
\r memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, len);
\r return;
\r }
\r /* First chunk is an odd size */
\r memcpy((byte *)ctx->in + 64 - (unsigned)t, buf, (unsigned)t);
\r byteSwap(ctx->in, 16);
\r MD5Transform(ctx->buf, ctx->in);
\r buf += (unsigned)t;
\r len -= (unsigned)t;
\r \r /* Process data in 64-byte chunks */
\r while (len >= 64)
\r {
\r memcpy(ctx->in, buf, 64);
\r byteSwap(ctx->in, 16);
\r MD5Transform(ctx->buf, ctx->in);
\r buf += 64;
\r len -= 64;
\r }
\r \r /* Handle any remaining bytes of data. */
\r memcpy(ctx->in, buf, len);
\r }
\r \r void MD5Final(byte digest[16], MD5Context *ctx)
\r {
\r int count = (int)(ctx->bytes[0] & 0x3f); /* Bytes in ctx->in */
\r byte *p = (byte *)ctx->in + count; /* First unused byte */
\r \r /* Set the first char of padding to 0x80. There is always room. */
\r *p++ = 0x80;
\r \r /* Bytes of padding needed to make 56 bytes (-8..55) */
\r count = 56 - 1 - count;
\r \r if (count < 0)
\r { /* Padding forces an extra block */
\r memset(p, 0, count+8);
\r byteSwap(ctx->in, 16);
\r MD5Transform(ctx->buf, ctx->in);
\r p = (byte *)ctx->in;
\r count = 56;
\r }
\r memset(p, 0, count+8);
\r byteSwap(ctx->in, 14);
\r \r /* Append length in bits and transform */
\r ctx->in[14] = ctx->bytes[0] << 3;
\r ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
\r MD5Transform(ctx->buf, ctx->in);
\r \r byteSwap(ctx->buf, 4);
\r memcpy(digest, ctx->buf, 16);
\r memset(ctx, 0, sizeof(ctx));
\r }
\r \r void MD5Transform(word32 buf[4], word32 const in[16])
\r {
\r register word32 a, b, c, d;
\r \r a = buf[0];
\r b = buf[1];
\r c = buf[2];
\r d = buf[3];
\r \r MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
\r MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
\r MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
\r MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
\r MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
\r MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
\r MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
\r MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
\r MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
\r MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
\r MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
\r MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
\r MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
\r MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
\r MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
\r MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
\r \r MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
\r MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
\r MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
\r MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
\r MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
\r MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
\r MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
\r MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
\r MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
\r MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
\r MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
\r MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
\r MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
\r MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
\r MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
\r MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
\r \r MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
\r MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
\r MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
\r MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
\r MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
\r MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
\r MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
\r MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
\r MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
\r MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
\r MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
\r MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
\r MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
\r MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
\r MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
\r MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
\r \r MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
\r MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
\r MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
\r MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
\r MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
\r MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
\r MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
\r MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
\r MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
\r MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
\r MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
\r MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
\r MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
\r MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
\r MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
\r MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
\r \r buf[0] += a;
\r buf[1] += b;
\r buf[2] += c;
\r buf[3] += d;
\r }
\r \r \r void MyMD5(void *dest, void *orig, int len, unsigned int* key)
\r {
\r MD5Context context;
\r MD5Init(&context, key);
\r MD5Update(&context, (const unsigned char*)orig, len);
\r MD5Final((unsigned char*)dest, &context);
\r }
\r \r \r void GenHash(const char* src, char* dest, const char* xtab, unsigned int* key)
\r {
\r unsigned char bytes[16];
\r\r MyMD5((char*)bytes, (void*)src, strlen(src), key);
\r\r for (int i = 0; i < 16; i++)
\r {
\r *dest++ = xtab[bytes[i] / 16];
\r *dest++ = xtab[bytes[i] % 16];
\r }
\r *dest++ = 0;
\r }
\r\r unsigned int *key;
\r char* chars;
\r\r public:
\r\r ModuleMD5(InspIRCd* Me)
\r : Module(Me), key(NULL), chars(NULL)
\r {
\r ServerInstance->PublishInterface("HashRequest", this);
\r }
\r \r virtual ~ModuleMD5()
\r {
\r ServerInstance->UnpublishInterface("HashRequest", this);
\r }
\r\r void Implements(char* List)
\r {
\r List[I_OnRequest] = 1;
\r }
\r \r virtual char* OnRequest(Request* request)
\r {
\r HashRequest* MD5 = (HashRequest*)request;
\r\r if (strcmp("KEY", request->GetId()) == 0)
\r {
\r this->key = (unsigned int*)MD5->GetKeyData();
\r }
\r else if (strcmp("HEX", request->GetId()) == 0)
\r {
\r this->chars = (char*)MD5->GetOutputs();
\r }
\r else if (strcmp("SUM", request->GetId()) == 0)
\r {
\r static char data[MAXBUF];
\r GenHash((const char*)MD5->GetHashData(), data, chars ? chars : "0123456789abcdef", key);
\r return data;
\r }
\r else if (strcmp("NAME", request->GetId()) == 0)
\r {
\r return "md5";
\r }
\r else if (strcmp("RESET", request->GetId()) == 0)
\r {
\r this->chars = NULL;
\r this->key = NULL;
\r }
\r return NULL;
\r }
\r\r virtual Version GetVersion()
\r {
\r return Version(1,1,0,1,VF_VENDOR|VF_SERVICEPROVIDER,API_VERSION);
\r }
\r};
\r\rMODULE_INIT(ModuleMD5)
\r