]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_argon2.cpp
Fix default linker flags in libargon2
[user/henk/code/inspircd.git] / src / modules / extra / m_argon2.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2020 Daniel Vassdal <shutter@canternet.org>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19 /// $CompilerFlags: find_compiler_flags("libargon2" "")
20
21 /// $LinkerFlags: find_linker_flags("libargon2" "-largon2")
22
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
27
28
29 #include "inspircd.h"
30 #include "modules/hash.h"
31
32 #ifdef __GNUC__
33 # pragma GCC diagnostic push
34 #endif
35
36 // Fix warnings about the use of `long long` on C++03
37 #if defined __clang__
38 # pragma clang diagnostic ignored "-Wc++11-long-long"
39 #elif defined __GNUC__
40 # pragma GCC diagnostic ignored "-Wlong-long"
41 #endif
42
43 #include <argon2.h>
44
45 class ProviderConfig
46 {
47  private:
48         static Argon2_version SanitizeArgon2Version(unsigned long version)
49         {
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
52                 // match better.
53                 switch (version)
54                 {
55                         case 10:
56                                 return ARGON2_VERSION_10;
57                         case 13:
58                                 return ARGON2_VERSION_13;
59                 }
60
61                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Unknown Argon2 version (%lu) specified; assuming 13",
62                         version);
63                 return ARGON2_VERSION_13;
64         }
65
66  public:
67         uint32_t iterations;
68         uint32_t lanes;
69         uint32_t memory;
70         uint32_t outlen;
71         uint32_t saltlen;
72         uint32_t threads;
73         Argon2_version version;
74
75         ProviderConfig()
76         {
77                 // Nothing interesting happens here.
78         }
79
80         ProviderConfig(const std::string& tagname, ProviderConfig* def)
81         {
82                 ConfigTag* tag = ServerInstance->Config->ConfValue(tagname);
83
84                 uint32_t def_iterations = def ? def->iterations : 3;
85                 this->iterations = tag->getUInt("iterations", def_iterations, 1);
86
87                 uint32_t def_lanes = def ? def->lanes : 1;
88                 this->lanes = tag->getUInt("lanes", def_lanes, ARGON2_MIN_LANES, ARGON2_MAX_LANES);
89
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);
92
93                 uint32_t def_outlen = def ? def->outlen : 32;
94                 this->outlen = tag->getUInt("length", def_outlen, ARGON2_MIN_OUTLEN, ARGON2_MAX_OUTLEN);
95
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);
98
99                 uint32_t def_threads = def ? def->threads : 1;
100                 this->threads = tag->getUInt("threads", def_threads, ARGON2_MIN_THREADS, ARGON2_MAX_THREADS);
101
102                 uint32_t def_version = def ? def->version : 13;
103                 this->version = SanitizeArgon2Version(tag->getUInt("version", def_version));
104         }
105 };
106
107 class HashArgon2 : public HashProvider
108 {
109  private:
110         const Argon2_type argon2Type;
111
112  public:
113         ProviderConfig config;
114
115         bool Compare(const std::string& input, const std::string& hash) CXX11_OVERRIDE
116         {
117                 int result = argon2_verify(
118                         hash.c_str(),
119                         input.c_str(),
120                         input.length(),
121                         argon2Type);
122
123                 return result == ARGON2_OK;
124         }
125
126         std::string GenerateRaw(const std::string& data) CXX11_OVERRIDE
127         {
128                 const std::string salt = ServerInstance->GenRandomStr(config.saltlen, false);
129
130                 size_t encodedLen = argon2_encodedlen(
131                         config.iterations,
132                         config.memory,
133                         config.lanes,
134                         config.saltlen,
135                         config.outlen,
136                         argon2Type);
137
138                 std::vector<char> raw_data(config.outlen);
139                 std::vector<char> encoded_data(encodedLen + 1);
140
141                 int argonResult = argon2_hash(
142                         config.iterations,
143                         config.memory,
144                         config.threads,
145                         data.c_str(),
146                         data.length(),
147                         salt.c_str(),
148                         salt.length(),
149                         &raw_data[0],
150                         raw_data.size(),
151                         &encoded_data[0],
152                         encoded_data.size(),
153                         argon2Type,
154                         config.version);
155
156                 if (argonResult != ARGON2_OK)
157                         throw ModuleException("Argon2 hashing failed!: " + std::string(argon2_error_message(argonResult)));
158
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());
165         }
166
167         std::string ToPrintable(const std::string& raw) CXX11_OVERRIDE
168         {
169                 return raw;
170         }
171
172         HashArgon2(Module* parent, const std::string& hashName, Argon2_type type)
173                 : HashProvider(parent, hashName)
174                 , argon2Type(type)
175
176         {
177         }
178 };
179
180 class ModuleArgon2 : public Module
181 {
182  private:
183         HashArgon2 argon2i;
184         HashArgon2 argon2d;
185         HashArgon2 argon2id;
186
187  public:
188         ModuleArgon2()
189                 : argon2i(this, "argon2i", Argon2_i)
190                 , argon2d(this, "argon2d", Argon2_d)
191                 , argon2id(this, "argon2id", Argon2_id)
192         {
193         }
194
195         Version GetVersion() CXX11_OVERRIDE
196         {
197                 return Version("Allows other modules to generate Argon2 hashes.", VF_VENDOR);
198         }
199
200         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
201         {
202                 ProviderConfig defaultConfig("argon2", NULL);
203                 argon2i.config = ProviderConfig("argon2i", &defaultConfig);
204                 argon2d.config = ProviderConfig("argon2d", &defaultConfig);
205                 argon2id.config = ProviderConfig("argon2id", &defaultConfig);
206         }
207 };
208
209 MODULE_INIT(ModuleArgon2)
210
211 // This needs to be down here because of warnings from macros.
212 #ifdef __GNUC__
213 # pragma GCC diagnostic pop
214 #endif