2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2020 Daniel Vassdal <shutter@canternet.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /// $CompilerFlags: find_compiler_flags("libargon2" "")
21 /// $LinkerFlags: find_linker_flags("libargon2" "-largon2")
23 /// $PackageInfo: require_system("arch") argon2 pkgconf
24 /// $PackageInfo: require_system("darwin") argon2 pkg-config
25 /// $PackageInfo: require_system("debian" "9.0") libargon2-0 pkg-config
26 /// $PackageInfo: require_system("ubuntu" "18.04") libargon2-0-dev pkg-config
30 #include "modules/hash.h"
33 # pragma GCC diagnostic push
36 // Fix warnings about the use of `long long` on C++03
38 # pragma clang diagnostic ignored "-Wc++11-long-long"
39 #elif defined __GNUC__
40 # pragma GCC diagnostic ignored "-Wlong-long"
48 static Argon2_version SanitizeArgon2Version(unsigned long version)
50 // Note, 10 is 0x10, and 13 is 0x13. Referring to it as
51 // dec 10 or 13 in the config file, for the name to
56 return ARGON2_VERSION_10;
58 return ARGON2_VERSION_13;
61 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Unknown Argon2 version (%lu) specified; assuming 13",
63 return ARGON2_VERSION_13;
73 Argon2_version version;
77 // Nothing interesting happens here.
80 ProviderConfig(const std::string& tagname, ProviderConfig* def)
82 ConfigTag* tag = ServerInstance->Config->ConfValue(tagname);
84 uint32_t def_iterations = def ? def->iterations : 3;
85 this->iterations = tag->getUInt("iterations", def_iterations, 1);
87 uint32_t def_lanes = def ? def->lanes : 1;
88 this->lanes = tag->getUInt("lanes", def_lanes, ARGON2_MIN_LANES, ARGON2_MAX_LANES);
90 uint32_t def_memory = def ? def->memory : 131072; // 128 MiB
91 this->memory = tag->getUInt("memory", def_memory, ARGON2_MIN_MEMORY, ARGON2_MAX_MEMORY);
93 uint32_t def_outlen = def ? def->outlen : 32;
94 this->outlen = tag->getUInt("length", def_outlen, ARGON2_MIN_OUTLEN, ARGON2_MAX_OUTLEN);
96 uint32_t def_saltlen = def ? def->saltlen : 16;
97 this->saltlen = tag->getUInt("saltlength", def_saltlen, ARGON2_MIN_SALT_LENGTH, ARGON2_MAX_SALT_LENGTH);
99 uint32_t def_threads = def ? def->threads : 1;
100 this->threads = tag->getUInt("threads", def_threads, ARGON2_MIN_THREADS, ARGON2_MAX_THREADS);
102 uint32_t def_version = def ? def->version : 13;
103 this->version = SanitizeArgon2Version(tag->getUInt("version", def_version));
107 class HashArgon2 : public HashProvider
110 const Argon2_type argon2Type;
113 ProviderConfig config;
115 bool Compare(const std::string& input, const std::string& hash) CXX11_OVERRIDE
117 int result = argon2_verify(
123 return result == ARGON2_OK;
126 std::string GenerateRaw(const std::string& data) CXX11_OVERRIDE
128 const std::string salt = ServerInstance->GenRandomStr(config.saltlen, false);
130 size_t encodedLen = argon2_encodedlen(
138 std::vector<char> raw_data(config.outlen);
139 std::vector<char> encoded_data(encodedLen + 1);
141 int argonResult = argon2_hash(
156 if (argonResult != ARGON2_OK)
157 throw ModuleException("Argon2 hashing failed!: " + std::string(argon2_error_message(argonResult)));
159 // This isn't the raw version, but we don't have
160 // the facilities to juggle around the extra state required
161 // to do anything useful with them if we don't encode them.
162 // So we pretend this is the raw version, and instead make
163 // ToPrintable return its input.
164 return std::string(&encoded_data[0], encoded_data.size());
167 std::string ToPrintable(const std::string& raw) CXX11_OVERRIDE
172 HashArgon2(Module* parent, const std::string& hashName, Argon2_type type)
173 : HashProvider(parent, hashName)
180 class ModuleArgon2 : public Module
189 : argon2i(this, "argon2i", Argon2_i)
190 , argon2d(this, "argon2d", Argon2_d)
191 , argon2id(this, "argon2id", Argon2_id)
195 Version GetVersion() CXX11_OVERRIDE
197 return Version("Allows other modules to generate Argon2 hashes.", VF_VENDOR);
200 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
202 ProviderConfig defaultConfig("argon2", NULL);
203 argon2i.config = ProviderConfig("argon2i", &defaultConfig);
204 argon2d.config = ProviderConfig("argon2d", &defaultConfig);
205 argon2id.config = ProviderConfig("argon2id", &defaultConfig);
209 MODULE_INIT(ModuleArgon2)
211 // This needs to be down here because of warnings from macros.
213 # pragma GCC diagnostic pop