diff options
| author | 2025-04-05 19:24:49 +0100 | |
|---|---|---|
| committer | 2025-04-06 00:45:31 +0100 | |
| commit | 28073d2506685a508135a487b41ea3aed63bf522 (patch) | |
| tree | c2328a7a857a2910ac9849eaa6b0c9b9091711f0 /modules | |
| parent | Rewrite every single hash module for the new interface. (diff) | |
Delete the old hashing interface and modules.
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/bcrypt.cpp | 94 | ||||
| -rw-r--r-- | modules/extra/argon2.cpp | 170 | ||||
| -rw-r--r-- | modules/password_hash.cpp | 110 | ||||
| -rw-r--r-- | modules/pbkdf2.cpp | 264 | ||||
| -rw-r--r-- | modules/sha1.cpp | 197 | ||||
| -rw-r--r-- | modules/sha2.cpp | 77 |
6 files changed, 0 insertions, 912 deletions
diff --git a/modules/bcrypt.cpp b/modules/bcrypt.cpp deleted file mode 100644 index 131a4aaf2..000000000 --- a/modules/bcrypt.cpp +++ /dev/null @@ -1,94 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2021 Dominic Hamon - * Copyright (C) 2018-2024 Sadie Powell <sadie@witchery.services> - * Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org> - * - * 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/>. - */ - - -#include "inspircd.h" -#include "modules/hash.h" - -#include <bcrypt/crypt_blowfish.c> - -class BCryptProvider final - : public HashProvider -{ -private: - std::string Salt() - { - char entropy[16]; - ServerInstance->GenRandom(entropy, std::size(entropy)); - - char salt[32]; - if (!_crypt_gensalt_blowfish_rn("$2a$", rounds, entropy, sizeof(entropy), salt, sizeof(salt))) - throw ModuleException(creator, "Could not generate salt - this should never happen"); - - return salt; - } - -public: - unsigned long rounds = 10; - - static std::string Generate(const std::string& data, const std::string& salt) - { - char hash[64]; - _crypt_blowfish_rn(data.c_str(), salt.c_str(), hash, sizeof(hash)); - return hash; - } - - std::string GenerateRaw(const std::string& data) override - { - return Generate(data, Salt()); - } - - bool Compare(const std::string& input, const std::string& hash) override - { - return InspIRCd::TimingSafeCompare(Generate(input, hash), hash); - } - - std::string ToPrintable(const std::string& raw) override - { - return raw; - } - - BCryptProvider(Module* parent) - : HashProvider(parent, "bcrypt", 60) - { - } -}; - -class ModuleBCrypt final - : public Module -{ -private: - BCryptProvider bcrypt; - -public: - ModuleBCrypt() - : Module(VF_VENDOR, "Allows other modules to generate bcrypt hashes.") - , bcrypt(this) - { - } - - void ReadConfig(ConfigStatus& status) override - { - const auto& conf = ServerInstance->Config->ConfValue("bcrypt"); - bcrypt.rounds = conf->getNum<unsigned long>("rounds", 10, 1); - } -}; - -MODULE_INIT(ModuleBCrypt) diff --git a/modules/extra/argon2.cpp b/modules/extra/argon2.cpp deleted file mode 100644 index e402eaea7..000000000 --- a/modules/extra/argon2.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2020-2024 Sadie Powell <sadie@witchery.services> - * Copyright (C) 2020 Daniel Vassdal <shutter@canternet.org> - * - * 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/>. - */ - -/// $CompilerFlags: find_compiler_flags("libargon2") -/// $LinkerFlags: find_linker_flags("libargon2") - -/// $PackageInfo: require_system("alpine") argon2-dev pkgconf -/// $PackageInfo: require_system("arch") argon2 pkgconf -/// $PackageInfo: require_system("darwin") argon2 pkg-config -/// $PackageInfo: require_system("debian~") libargon2-dev pkg-config - - -#include "inspircd.h" -#include "modules/hash.h" - -#include <argon2.h> - -class ProviderConfig final -{ -public: - uint32_t iterations; - uint32_t memory; - uint32_t outlen; - uint32_t saltlen; - uint32_t threads; - - ProviderConfig() - { - // Nothing interesting happens here. - } - - ProviderConfig(const std::string& tagname, ProviderConfig* def) - { - const auto& tag = ServerInstance->Config->ConfValue(tagname); - - uint32_t def_iterations = def ? def->iterations : 3; - this->iterations = tag->getNum<uint32_t>("iterations", def_iterations, 1); - - uint32_t def_memory = def ? def->memory : 131072; // 128 MiB - this->memory = tag->getNum<uint32_t>("memory", def_memory, ARGON2_MIN_MEMORY, ARGON2_MAX_MEMORY); - - uint32_t def_outlen = def ? def->outlen : 32; - this->outlen = tag->getNum<int32_t>("length", def_outlen, ARGON2_MIN_OUTLEN, ARGON2_MAX_OUTLEN); - - uint32_t def_saltlen = def ? def->saltlen : 16; - this->saltlen = tag->getNum<uint32_t>("saltlength", def_saltlen, ARGON2_MIN_SALT_LENGTH, ARGON2_MAX_SALT_LENGTH); - - uint32_t def_threads = def ? def->threads : 1; - this->threads = tag->getNum<uint32_t>("threads", def_threads, ARGON2_MIN_THREADS, ARGON2_MAX_THREADS); - } -}; - -class HashArgon2 final - : public HashProvider -{ -private: - const Argon2_type argon2Type; - -public: - ProviderConfig config; - - bool Compare(const std::string& input, const std::string& hash) override - { - int result = argon2_verify( - hash.c_str(), - input.c_str(), - input.length(), - argon2Type); - - return result == ARGON2_OK; - } - - std::string GenerateRaw(const std::string& data) override - { - std::vector<char> salt(config.saltlen); - ServerInstance->GenRandom(salt.data(), salt.size()); - - size_t encodedLen = argon2_encodedlen( - config.iterations, - config.memory, - config.threads, - config.saltlen, - config.outlen, - argon2Type); - - std::vector<char> raw_data(config.outlen); - std::vector<char> encoded_data(encodedLen + 1); - - int argonResult = argon2_hash( - config.iterations, - config.memory, - config.threads, - data.c_str(), - data.length(), - salt.data(), - salt.size(), - raw_data.data(), - raw_data.size(), - encoded_data.data(), - encoded_data.size(), - argon2Type, - ARGON2_VERSION_NUMBER); - - if (argonResult != ARGON2_OK) - throw ModuleException(creator, "Argon2 hashing failed: " + std::string(argon2_error_message(argonResult))); - - // This isn't the raw version, but we don't have - // the facilities to juggle around the extra state required - // to do anything useful with them if we don't encode them. - // So we pretend this is the raw version, and instead make - // ToPrintable return its input. - return std::string(encoded_data.data(), encoded_data.size()); - } - - std::string ToPrintable(const std::string& raw) override - { - return raw; - } - - HashArgon2(Module* parent, const std::string& hashName, Argon2_type type) - : HashProvider(parent, hashName) - , argon2Type(type) - - { - } -}; - -class ModuleArgon2 final - : public Module -{ -private: - HashArgon2 argon2i; - HashArgon2 argon2d; - HashArgon2 argon2id; - -public: - ModuleArgon2() - : Module(VF_VENDOR, "Allows other modules to generate Argon2 hashes.") - , argon2i(this, "argon2i", Argon2_i) - , argon2d(this, "argon2d", Argon2_d) - , argon2id(this, "argon2id", Argon2_id) - { - } - - void ReadConfig(ConfigStatus& status) override - { - ProviderConfig defaultConfig("argon2", nullptr); - argon2i.config = ProviderConfig("argon2i", &defaultConfig); - argon2d.config = ProviderConfig("argon2d", &defaultConfig); - argon2id.config = ProviderConfig("argon2id", &defaultConfig); - } -}; - -MODULE_INIT(ModuleArgon2) diff --git a/modules/password_hash.cpp b/modules/password_hash.cpp deleted file mode 100644 index f204bfa9b..000000000 --- a/modules/password_hash.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org> - * Copyright (C) 2013, 2017, 2019-2024 Sadie Powell <sadie@witchery.services> - * Copyright (C) 2012, 2014 Attila Molnar <attilamolnar@hush.com> - * Copyright (C) 2012 Robby <robby@chatbelgie.be> - * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org> - * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net> - * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org> - * Copyright (C) 2006 Craig Edwards <brain@inspircd.org> - * - * 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/>. - */ - - -#include "inspircd.h" -#include "modules/newhash.h" - -class CommandMkpasswd final - : public Command -{ -public: - CommandMkpasswd(Module* Creator) - : Command(Creator, "MKPASSWD", 2) - { - penalty = 5000; - syntax = { "<hashtype> <plaintext>" }; - } - - CmdResult Handle(User* user, const Params& parameters) override - { - if (!parameters[0].compare(0, 5, "hmac-", 5)) - { - std::string type(parameters[0], 5); - auto* hp = ServerInstance->Modules.FindDataService<Hash::Provider>("hash/" + type); - if (!hp) - { - user->WriteNotice("Unknown hash type"); - return CmdResult::FAILURE; - } - - if (hp->IsKDF()) - { - user->WriteNotice(type + " does not support HMAC"); - return CmdResult::FAILURE; - } - - std::string salt(hp->digest_size, '\0'); - ServerInstance->GenRandom(salt.data(), salt.length()); - - std::string target = Hash::HMAC(hp, salt, parameters[1]); - std::string str = Base64::Encode(salt) + "$" + Base64::Encode(target, nullptr, 0); - - user->WriteNotice(parameters[0] + " hashed password for " + parameters[1] + " is " + str); - return CmdResult::SUCCESS; - } - - auto* hp = ServerInstance->Modules.FindDataService<Hash::Provider>("hash/" + parameters[0]); - if (!hp) - { - user->WriteNotice("Unknown hash type"); - return CmdResult::FAILURE; - } - - try - { - auto hexsum = hp->ToPrintable(hp->Hash(parameters[1])); - user->WriteNotice(parameters[0] + " hashed password for " + parameters[1] + " is " + hexsum); - return CmdResult::SUCCESS; - } - catch (const ModuleException& error) - { - user->WriteNotice("*** " + name + ": " + error.GetReason()); - return CmdResult::FAILURE; - } - } -}; - -class ModulePasswordHash final - : public Module -{ -private: - CommandMkpasswd cmd; - -public: - ModulePasswordHash() - : Module(VF_VENDOR, "Allows passwords to be hashed and adds the /MKPASSWD command which allows the generation of hashed passwords for use in the server configuration.") - , cmd(this) - { - } - - void ReadConfig(ConfigStatus& status) override - { - const auto& tag = ServerInstance->Config->ConfValue("mkpasswd"); - cmd.access_needed = tag->getBool("operonly") ? CmdAccess::OPERATOR : CmdAccess::NORMAL; - } -}; - -MODULE_INIT(ModulePasswordHash) diff --git a/modules/pbkdf2.cpp b/modules/pbkdf2.cpp deleted file mode 100644 index db63cea2d..000000000 --- a/modules/pbkdf2.cpp +++ /dev/null @@ -1,264 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2021 Dominic Hamon - * Copyright (C) 2018-2025 Sadie Powell <sadie@witchery.services> - * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com> - * Copyright (C) 2014, 2020 Daniel Vassdal <shutter@canternet.org> - * Copyright (C) 2014, 2016 Attila Molnar <attilamolnar@hush.com> - * - * 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/>. - */ - - -#include <cmath> - -#include "inspircd.h" -#include "modules/hash.h" - -// Format: -// Iterations:B64(Hash):B64(Salt) -// E.g. -// 10200:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA:BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB -class PBKDF2Hash final -{ -public: - unsigned long iterations; - size_t length; - std::string salt; - std::string hash; - - PBKDF2Hash(unsigned long itr, size_t dkl, const std::string& slt, const std::string& hsh = "") - : iterations(itr) - , length(dkl) - , salt(slt) - , hash(hsh) - { - } - - PBKDF2Hash(const std::string& data) - { - irc::sepstream ss(data, ':'); - std::string tok; - - ss.GetToken(tok); - this->iterations = ConvToNum<unsigned int>(tok); - - ss.GetToken(tok); - this->hash = Base64::Decode(tok); - - ss.GetToken(tok); - this->salt = Base64::Decode(tok); - - this->length = this->hash.length(); - } - - std::string ToString() const - { - if (!IsValid()) - return ""; - - return FMT::format("{}:{}:{}", this->iterations, Base64::Encode(this->hash), Base64::Encode(this->salt)); - } - - bool IsValid() const - { - return this->iterations && this->length && !this->salt.empty() && !this->hash.empty(); - } -}; - -class PBKDF2Provider final - : public HashProvider -{ -public: - HashProvider* provider; - unsigned long iterations; - size_t dkey_length; - - std::string PBKDF2(const std::string& pass, const std::string& salt, unsigned long itr = 0, size_t dkl = 0) const - { - size_t blocks = std::ceil((double)dkl / provider->out_size); - - std::string output; - std::string tmphash; - std::string salt_block = salt; - for (size_t block = 1; block <= blocks; block++) - { - char salt_data[4]; - for (size_t i = 0; i < sizeof(salt_data); i++) - salt_data[i] = block >> (24 - i * 8) & 0x0F; - - salt_block.erase(salt.length()); - salt_block.append(salt_data, sizeof(salt_data)); - - std::string blockdata = provider->hmac(pass, salt_block); - std::string lasthash = blockdata; - for (size_t iter = 1; iter < itr; iter++) - { - tmphash = provider->hmac(pass, lasthash); - for (size_t i = 0; i < provider->out_size; i++) - blockdata[i] ^= tmphash[i]; - - lasthash.swap(tmphash); - } - output += blockdata; - } - - output.erase(dkl); - return output; - } - - std::string GenerateRaw(const std::string& data) override - { - std::string salt(dkey_length, '\0'); - ServerInstance->GenRandom(salt.data(), salt.length()); - - PBKDF2Hash hs(this->iterations, this->dkey_length, salt); - hs.hash = PBKDF2(data, hs.salt, this->iterations, this->dkey_length); - return hs.ToString(); - } - - bool Compare(const std::string& input, const std::string& hash) override - { - PBKDF2Hash hs(hash); - if (!hs.IsValid()) - return false; - - std::string cmp = PBKDF2(input, hs.salt, hs.iterations, hs.length); - return InspIRCd::TimingSafeCompare(cmp, hs.hash); - } - - std::string ToPrintable(const std::string& raw) override - { - return raw; - } - - PBKDF2Provider(Module* mod, HashProvider* hp) - : HashProvider(mod, "pbkdf2-hmac-" + hp->name.substr(hp->name.find('/') + 1)) - , provider(hp) - { - DisableAutoRegister(); - } -}; - -struct ProviderConfig final -{ - unsigned long dkey_length; - unsigned long iterations; -}; - -typedef std::map<std::string, ProviderConfig> ProviderConfigMap; - -class ModulePBKDF2 final - : public Module -{ - std::vector<PBKDF2Provider*> providers; - ProviderConfig globalconfig; - ProviderConfigMap providerconfigs; - - ProviderConfig GetConfigForProvider(const std::string& name) const - { - ProviderConfigMap::const_iterator it = providerconfigs.find(name); - if (it == providerconfigs.end()) - return globalconfig; - - return it->second; - } - - void ConfigureProviders() - { - for (auto* pi : providers) - { - ProviderConfig config = GetConfigForProvider(pi->name); - pi->iterations = config.iterations; - pi->dkey_length = config.dkey_length; - } - } - - void ReadConfig(ConfigStatus& status) override - { - // First set the common values - const auto& tag = ServerInstance->Config->ConfValue("pbkdf2"); - ProviderConfig newglobal; - newglobal.iterations = tag->getNum<unsigned long>("iterations", 12288, 1); - newglobal.dkey_length = tag->getNum<size_t>("length", 32, 1, 1024); - - // Then the specific values - ProviderConfigMap newconfigs; - for (const auto& [_, ptag] : ServerInstance->Config->ConfTags("pbkdf2prov")) - { - std::string hash_name = "hash/" + ptag->getString("hash"); - ProviderConfig& config = newconfigs[hash_name]; - - config.iterations = ptag->getNum<unsigned long>("iterations", newglobal.iterations, 1); - config.dkey_length = ptag->getNum<size_t>("length", newglobal.dkey_length, 1, 1024); - } - - // Config is valid, apply it - providerconfigs.swap(newconfigs); - std::swap(globalconfig, newglobal); - ConfigureProviders(); - } - -public: - ModulePBKDF2() - : Module(VF_VENDOR, "Allows other modules to generate PBKDF2 hashes.") - { - } - - ~ModulePBKDF2() override - { - stdalgo::delete_all(providers); - } - - void init() override - { - // Let ourself know about any existing services. - for (const auto& [_, service] : ServerInstance->Modules.DataProviders) - OnServiceAdd(*service); - } - - void OnServiceAdd(ServiceProvider& provider) override - { - // Check if it's a hash provider - if (provider.name.compare(0, 5, "hash/")) - return; - - HashProvider* hp = static_cast<HashProvider*>(&provider); - if (hp->IsKDF()) - return; - - auto* prov = new PBKDF2Provider(this, hp); - providers.push_back(prov); - ServerInstance->Modules.AddService(*prov); - - ConfigureProviders(); - } - - void OnServiceDel(ServiceProvider& prov) override - { - for (std::vector<PBKDF2Provider*>::iterator i = providers.begin(); i != providers.end(); ++i) - { - PBKDF2Provider* item = *i; - if (item->provider != &prov) - continue; - - ServerInstance->Modules.DelService(*item); - delete item; - providers.erase(i); - break; - } - } -}; - -MODULE_INIT(ModulePBKDF2) diff --git a/modules/sha1.cpp b/modules/sha1.cpp deleted file mode 100644 index c18af9272..000000000 --- a/modules/sha1.cpp +++ /dev/null @@ -1,197 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2019-2021 Sadie Powell <sadie@witchery.services> - * Copyright (C) 2016 Attila Molnar <attilamolnar@hush.com> - * - * 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/>. - */ - -/* -SHA-1 in C -By Steve Reid <steve@edmweb.com> -100% Public Domain -*/ - -#include "inspircd.h" -#include "modules/hash.h" - -union CHAR64LONG16 -{ - unsigned char c[64]; - uint32_t l[16]; -}; - -inline static uint32_t rol(uint32_t value, uint32_t bits) { return (value << bits) | (value >> (32 - bits)); } - -// blk0() and blk() perform the initial expand. -// I got the idea of expanding during the round function from SSLeay -inline static uint32_t blk0(CHAR64LONG16& block, uint32_t i) -{ - if constexpr (std::endian::native == std::endian::big) - return block.l[i]; - else - return block.l[i] = (rol(block.l[i], 24) & 0xFF00FF00) | (rol(block.l[i], 8) & 0x00FF00FF); -} -inline static uint32_t blk(CHAR64LONG16 &block, uint32_t i) { return block.l[i & 15] = rol(block.l[(i + 13) & 15] ^ block.l[(i + 8) & 15] ^ block.l[(i + 2) & 15] ^ block.l[i & 15],1); } - -// (R0+R1), R2, R3, R4 are the different operations used in SHA1 -inline static void R0(CHAR64LONG16& block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += ((w & (x ^ y)) ^ y) + blk0(block, i) + 0x5A827999 + rol(v, 5); w = rol(w, 30); } -inline static void R1(CHAR64LONG16& block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += ((w & (x ^ y)) ^ y) + blk(block, i) + 0x5A827999 + rol(v, 5); w = rol(w, 30); } -inline static void R2(CHAR64LONG16& block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += (w ^ x ^ y) + blk(block, i) + 0x6ED9EBA1 + rol(v, 5); w = rol(w, 30); } -inline static void R3(CHAR64LONG16& block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += (((w | x) & y) | (w & x)) + blk(block, i) + 0x8F1BBCDC + rol(v, 5); w = rol(w, 30); } -inline static void R4(CHAR64LONG16& block, uint32_t v, uint32_t &w, uint32_t x, uint32_t y, uint32_t &z, uint32_t i) { z += (w ^ x ^ y) + blk(block, i) + 0xCA62C1D6 + rol(v, 5); w = rol(w, 30); } - -static const uint32_t sha1_iv[5] = -{ - 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 -}; - -class SHA1Context final -{ - uint32_t state[5]; - uint32_t count[2]; - unsigned char buffer[64]; - unsigned char digest[20]; - - void Transform(const unsigned char buf[64]) - { - uint32_t a, b, c, d, e; - - CHAR64LONG16 block; - memcpy(block.c, buf, 64); - - // Copy state[] to working vars - a = this->state[0]; - b = this->state[1]; - c = this->state[2]; - d = this->state[3]; - e = this->state[4]; - - // 4 rounds of 20 operations each. Loop unrolled. - R0(block, a, b, c, d, e, 0); R0(block, e, a, b, c, d, 1); R0(block, d, e, a, b, c, 2); R0(block, c, d, e, a, b, 3); - R0(block, b, c, d, e, a, 4); R0(block, a, b, c, d, e, 5); R0(block, e, a, b, c, d, 6); R0(block, d, e, a, b, c, 7); - R0(block, c, d, e, a, b, 8); R0(block, b, c, d, e, a, 9); R0(block, a, b, c, d, e, 10); R0(block, e, a, b, c, d, 11); - R0(block, d, e, a, b, c, 12); R0(block, c, d, e, a, b, 13); R0(block, b, c, d, e, a, 14); R0(block, a, b, c, d, e, 15); - R1(block, e, a, b, c, d, 16); R1(block, d, e, a, b, c, 17); R1(block, c, d, e, a, b, 18); R1(block, b, c, d, e, a, 19); - R2(block, a, b, c, d, e, 20); R2(block, e, a, b, c, d, 21); R2(block, d, e, a, b, c, 22); R2(block, c, d, e, a, b, 23); - R2(block, b, c, d, e, a, 24); R2(block, a, b, c, d, e, 25); R2(block, e, a, b, c, d, 26); R2(block, d, e, a, b, c, 27); - R2(block, c, d, e, a, b, 28); R2(block, b, c, d, e, a, 29); R2(block, a, b, c, d, e, 30); R2(block, e, a, b, c, d, 31); - R2(block, d, e, a, b, c, 32); R2(block, c, d, e, a, b, 33); R2(block, b, c, d, e, a, 34); R2(block, a, b, c, d, e, 35); - R2(block, e, a, b, c, d, 36); R2(block, d, e, a, b, c, 37); R2(block, c, d, e, a, b, 38); R2(block, b, c, d, e, a, 39); - R3(block, a, b, c, d, e, 40); R3(block, e, a, b, c, d, 41); R3(block, d, e, a, b, c, 42); R3(block, c, d, e, a, b, 43); - R3(block, b, c, d, e, a, 44); R3(block, a, b, c, d, e, 45); R3(block, e, a, b, c, d, 46); R3(block, d, e, a, b, c, 47); - R3(block, c, d, e, a, b, 48); R3(block, b, c, d, e, a, 49); R3(block, a, b, c, d, e, 50); R3(block, e, a, b, c, d, 51); - R3(block, d, e, a, b, c, 52); R3(block, c, d, e, a, b, 53); R3(block, b, c, d, e, a, 54); R3(block, a, b, c, d, e, 55); - R3(block, e, a, b, c, d, 56); R3(block, d, e, a, b, c, 57); R3(block, c, d, e, a, b, 58); R3(block, b, c, d, e, a, 59); - R4(block, a, b, c, d, e, 60); R4(block, e, a, b, c, d, 61); R4(block, d, e, a, b, c, 62); R4(block, c, d, e, a, b, 63); - R4(block, b, c, d, e, a, 64); R4(block, a, b, c, d, e, 65); R4(block, e, a, b, c, d, 66); R4(block, d, e, a, b, c, 67); - R4(block, c, d, e, a, b, 68); R4(block, b, c, d, e, a, 69); R4(block, a, b, c, d, e, 70); R4(block, e, a, b, c, d, 71); - R4(block, d, e, a, b, c, 72); R4(block, c, d, e, a, b, 73); R4(block, b, c, d, e, a, 74); R4(block, a, b, c, d, e, 75); - R4(block, e, a, b, c, d, 76); R4(block, d, e, a, b, c, 77); R4(block, c, d, e, a, b, 78); R4(block, b, c, d, e, a, 79); - // Add the working vars back into state[] - this->state[0] += a; - this->state[1] += b; - this->state[2] += c; - this->state[3] += d; - this->state[4] += e; - } - -public: - SHA1Context() - { - for (int i = 0; i < 5; ++i) - this->state[i] = sha1_iv[i]; - - this->count[0] = this->count[1] = 0; - memset(this->buffer, 0, sizeof(this->buffer)); - memset(this->digest, 0, sizeof(this->digest)); - } - - void Update(const unsigned char* data, size_t len) - { - uint32_t i, j; - - j = (this->count[0] >> 3) & 63; - if ((this->count[0] += len << 3) < (len << 3)) - ++this->count[1]; - this->count[1] += len >> 29; - if (j + len > 63) - { - memcpy(&this->buffer[j], data, (i = 64 - j)); - this->Transform(this->buffer); - for (; i + 63 < len; i += 64) - this->Transform(&data[i]); - j = 0; - } - else - i = 0; - memcpy(&this->buffer[j], &data[i], len - i); - } - - void Finalize() - { - uint32_t i; - unsigned char finalcount[8]; - - for (i = 0; i < 8; ++i) - finalcount[i] = static_cast<unsigned char>((this->count[i >= 4 ? 0 : 1] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */ - this->Update(reinterpret_cast<const unsigned char *>("\200"), 1); - while ((this->count[0] & 504) != 448) - this->Update(reinterpret_cast<const unsigned char *>("\0"), 1); - this->Update(finalcount, 8); // Should cause a SHA1Transform() - for (i = 0; i < 20; ++i) - this->digest[i] = static_cast<unsigned char>((this->state[i>>2] >> ((3 - (i & 3)) * 8)) & 255); - - this->Transform(this->buffer); - } - - std::string GetRaw() const - { - return std::string((const char*)digest, sizeof(digest)); - } -}; - -class SHA1HashProvider final - : public HashProvider -{ -public: - SHA1HashProvider(Module* mod) - : HashProvider(mod, "sha1", 20, 64) - { - } - - std::string GenerateRaw(const std::string& data) override - { - SHA1Context ctx; - ctx.Update(reinterpret_cast<const unsigned char*>(data.data()), data.length()); - ctx.Finalize(); - return ctx.GetRaw(); - } -}; - -class ModuleSHA1 final - : public Module -{ -private: - SHA1HashProvider sha1; - -public: - ModuleSHA1() - : Module(VF_VENDOR, "Allows other modules to generate SHA-1 hashes.") - , sha1(this) - { - } -}; - -MODULE_INIT(ModuleSHA1) diff --git a/modules/sha2.cpp b/modules/sha2.cpp deleted file mode 100644 index 50705ed46..000000000 --- a/modules/sha2.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2013, 2018-2022 Sadie Powell <sadie@witchery.services> - * Copyright (C) 2012 Robby <robby@chatbelgie.be> - * 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> - * - * 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/>. - */ - - -// Fix a collision between the Haiku uint64 typedef and the -// one from the sha2 library. -#ifdef __HAIKU__ -# define uint64 sha2_uint64 -#endif - -#include <sha2/sha2.c> - -#ifdef __HAIKU__ -# undef uint64 -#endif - -#include "inspircd.h" -#include "modules/hash.h" - -template<void (*SHA)(const unsigned char*, unsigned int, unsigned char*)> -class HashSHA2 final - : public HashProvider -{ -public: - HashSHA2(Module* parent, const std::string& Name, unsigned int osize, unsigned int bsize) - : HashProvider(parent, Name, osize, bsize) - { - } - - std::string GenerateRaw(const std::string& data) override - { - std::vector<char> bytes(out_size); - SHA(reinterpret_cast<const unsigned char*>(data.data()), static_cast<unsigned int>(data.size()), reinterpret_cast<unsigned char*>(bytes.data())); - return std::string(bytes.data(), bytes.size()); - } -}; - -class ModuleSHA2 final - : public Module -{ -private: - HashSHA2<sha224> sha224algo; - HashSHA2<sha256> sha256algo; - HashSHA2<sha384> sha384algo; - HashSHA2<sha512> sha512algo; - -public: - ModuleSHA2() - : Module(VF_VENDOR, "Allows other modules to generate SHA-2 hashes.") - , sha224algo(this, "sha224", SHA224_DIGEST_SIZE, SHA224_BLOCK_SIZE) - , sha256algo(this, "sha256", SHA256_DIGEST_SIZE, SHA256_BLOCK_SIZE) - , sha384algo(this, "sha384", SHA384_DIGEST_SIZE, SHA384_BLOCK_SIZE) - , sha512algo(this, "sha512", SHA512_DIGEST_SIZE, SHA512_BLOCK_SIZE) - { - } -}; - -MODULE_INIT(ModuleSHA2) |
