]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_sha256.cpp
Merge pull request #1162 from SaberUK/insp20+fix-deinstall
[user/henk/code/inspircd.git] / src / modules / m_sha256.cpp
index 6e1ac0e56aa9dc033e8728c45623687c25a41d2a..86970968a80740c43ef31204426e2e141854123c 100644 (file)
@@ -1,16 +1,25 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
  *
- * This program is free but copyrighted software; see
- *            the file COPYING for details.
+ * This file is part of InspIRCd.  InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
  *
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+
 /* m_sha256 - Based on m_opersha256 written by Special <john@yarbbles.com>
  * Modified and improved by Craig Edwards, December 2006.
  *
  */
 
 /* $ModDesc: Allows for SHA-256 encrypted oper passwords */
-/* $ModDep: m_hash.h */
 
-#include "inspircd_config.h"
+#include "inspircd.h"
 #ifdef HAS_STDINT
 #include <stdint.h>
 #endif
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
-#include "inspircd.h"
-
-#include "m_hash.h"
+#include "hash.h"
 
 #ifndef HAS_STDINT
 typedef unsigned int uint32_t;
 #endif
 
+#define SHA256_DIGEST_SIZE (256 / 8)
+#define SHA256_BLOCK_SIZE  (512 / 8)
+
 /** An sha 256 context, used by m_opersha256
  */
-class SHA256Context : public classbase
+class SHA256Context
 {
  public:
        unsigned int tot_len;
@@ -137,14 +143,14 @@ uint32_t sha256_k[64] =
        0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 };
 
-class ModuleSHA256 : public Module
+class HashSHA256 : public HashProvider
 {
-       void SHA256Init(SHA256Context *ctx, const unsigned int* key)
+       void SHA256Init(SHA256Context *ctx, const unsigned int* ikey)
        {
-               if (key)
+               if (ikey)
                {
                        for (int i = 0; i < 8; i++)
-                               ctx->h[i] = key[i];
+                               ctx->h[i] = ikey[i];
                }
                else
                {
@@ -164,7 +170,7 @@ class ModuleSHA256 : public Module
                {
                        int j;
                        sub_block = message + ((i - 1) << 6);
-       
+
                        for (j = 0; j < 16; j++)
                                PACK32(&sub_block[j << 2], &w[j]);
                        for (j = 16; j < 64; j++)
@@ -188,10 +194,29 @@ class ModuleSHA256 : public Module
                                ctx->h[j] += wv[j];
                }
        }
-       
+
        void SHA256Update(SHA256Context *ctx, unsigned char *message, unsigned int len)
        {
-               unsigned int rem_len = SHA256_BLOCK_SIZE - ctx->len;
+               /*
+                * XXX here be dragons!
+                * After many hours of pouring over this, I think I've found the problem.
+                * When Special created our module from the reference one, he used:
+                *
+                *     unsigned int rem_len = SHA256_BLOCK_SIZE - ctx->len;
+                *
+                * instead of the reference's version of:
+                *
+                *     unsigned int tmp_len = SHA256_BLOCK_SIZE - ctx->len;
+                *     unsigned int rem_len = len < tmp_len ? len : tmp_len;
+                *
+                * I've changed back to the reference version of this code, and it seems to work with no errors.
+                * So I'm inclined to believe this was the problem..
+                *             -- w00t (January 06, 2008)
+                */
+               unsigned int tmp_len = SHA256_BLOCK_SIZE - ctx->len;
+               unsigned int rem_len = len < tmp_len ? len : tmp_len;
+
+
                memcpy(&ctx->block[ctx->len], message, rem_len);
                if (ctx->len + len < SHA256_BLOCK_SIZE)
                {
@@ -208,7 +233,7 @@ class ModuleSHA256 : public Module
                ctx->len = rem_len;
                ctx->tot_len += (block_nb + 1) << 6;
        }
-       
+
        void SHA256Final(SHA256Context *ctx, unsigned char *digest)
        {
                unsigned int block_nb = (1 + ((SHA256_BLOCK_SIZE - 9) < (ctx->len % SHA256_BLOCK_SIZE)));
@@ -221,99 +246,44 @@ class ModuleSHA256 : public Module
                for (int i = 0 ; i < 8; i++)
                        UNPACK32(ctx->h[i], &digest[i << 2]);
        }
-       
-       void SHA256(const char *src, char *dest, int len, const char* hxc, const unsigned int* key = NULL)
+
+       void SHA256(const char *src, unsigned char *dest, unsigned int len)
        {
-               // Generate the hash
-               unsigned char bytehash[SHA256_DIGEST_SIZE];
                SHA256Context ctx;
-               SHA256Init(&ctx, key);
-               SHA256Update(&ctx, (unsigned char *)src, (unsigned int)len);
-               SHA256Final(&ctx, bytehash);
-               // Convert it to hex
-               for (int i = 0, j = 0; i < SHA256_DIGEST_SIZE; i++)
-               {
-                       dest[j++] = hxc[bytehash[i] / 16];
-                       dest[j++] = hxc[bytehash[i] % 16];
-                       dest[j] = '\0';
-               }
+               SHA256Init(&ctx, NULL);
+               SHA256Update(&ctx, (unsigned char *)src, len);
+               SHA256Final(&ctx, dest);
        }
 
-       unsigned int* key;
-       char* chars;
-
  public:
-
-       ModuleSHA256(InspIRCd* Me) : Module(Me), key(NULL), chars(NULL)
-       {
-               ServerInstance->PublishInterface("HashRequest", this);
-       }
-
-       virtual ~ModuleSHA256()
-       {
-               ServerInstance->UnpublishInterface("HashRequest", this);
-       }
-
-       void Implements(char *List)
+       std::string sum(const std::string& data)
        {
-               List[I_OnRequest] = 1;
+               unsigned char bytes[SHA256_DIGEST_SIZE];
+               SHA256(data.data(), bytes, data.length());
+               return std::string((char*)bytes, SHA256_DIGEST_SIZE);
        }
 
-       virtual char* OnRequest(Request* request)
+       std::string sumIV(unsigned int* IV, const char* HexMap, const std::string &sdata)
        {
-               HashRequest* SHA = (HashRequest*)request;
-               if (strcmp("KEY", request->GetId()) == 0)
-               {
-                       this->key = (unsigned int*)SHA->GetKeyData();
-               }
-               else if (strcmp("HEX", request->GetId()) == 0)
-               {
-                       this->chars = (char*)SHA->GetOutputs();
-               }
-               else if (strcmp("SUM", request->GetId()) == 0)
-               {
-                       static char data[MAXBUF];
-                       SHA256((const char*)SHA->GetHashData(), data, strlen(SHA->GetHashData()), chars ? chars : "0123456789abcdef", key);
-                       return data;
-               }
-               else if (strcmp("NAME", request->GetId()) == 0)
-               {
-                       return "sha256";
-               }
-               else if (strcmp("RESET", request->GetId()) == 0)
-               {
-                       this->chars = NULL;
-                       this->key = NULL;
-               }
-               return NULL;
+               return "";
        }
 
-       virtual Version GetVersion()
-       {
-               return Version(1, 1, 0, 1, VF_VENDOR|VF_SERVICEPROVIDER, API_VERSION);
-       }
+       HashSHA256(Module* parent) : HashProvider(parent, "hash/sha256", 32, 64) {}
 };
 
-
-class ModuleSHA256Factory : public ModuleFactory
+class ModuleSHA256 : public Module
 {
-public:
-       ModuleSHA256Factory()
-       {
-       }
-
-       ~ModuleSHA256Factory()
+       HashSHA256 sha;
+ public:
+       ModuleSHA256() : sha(this)
        {
+               ServerInstance->Modules->AddService(sha);
        }
 
-       virtual Module *CreateModule(InspIRCd* Me)
+       Version GetVersion()
        {
-               return new ModuleSHA256(Me);
+               return Version("Implements SHA-256 hashing", VF_VENDOR);
        }
-
 };
 
-extern "C" DllExport void * init_module( void )
-{
-       return new ModuleSHA256Factory;
-}
+MODULE_INIT(ModuleSHA256)