1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2025 Sadie Powell <sadie@witchery.services>
*
* 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"
namespace
{
// NOTE: We use an extremely low iteration count in the checks to
// avoid blocking for a long time when starting up.
const std::map<std::string, std::map<std::string, std::string>> checks = {
{ "sha1", {
{ "100:bsOVI55xIWFZxO7Ox4lb8Q:B9baArs7lJR9kFVLFAbpEA", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha224", {
{ "100:7mCEabsDkJmv7yphDJ00Zg:ZpVPEq7DshpaCwPpE6WiTA", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha256", {
{ "100:eV0OGnSmBtvC1Y7pUc5Odg:I4yDKQWppE2rrHJDX01V2g", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha384", {
{ "100:uj0W7gdL6BlHRst3jJwaFA:8FtaHvqYgZwsSLc2a2Fiig", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha512", {
{ "100:OOjcsv69e87ItbWU6+TX0w:5VoQbRnulaCeJlbA44UMFw", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha3-224", {
{ "100:iqGZHw2Yl6uZm4FDpa/VIA:iP7f+O2eQahVbgTSqWzC6A", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha3-256", {
{ "100:Fd/ckU57sD6Q3PVx0CueWw:U6tmB9q9t55cxNK3I/cHcg", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha3-384", {
{ "100:93wezDugG6v2bB/4dqkhGw:1DVYoIRZELyC/Ed0FyzfuA", "The quick brown fox jumps over the lazy dog" },
}},
{ "sha3-512", {
{ "100:7jK7DPg5yLqNzCRbqvAxYA:hhPbmAnTi0l7xs1aiu4JAw", "The quick brown fox jumps over the lazy dog" },
}},
};
std::string PBKDF2(Hash::Provider* provider, const std::string& plain, const std::string& salt, uint32_t iterations, size_t length)
{
if (!provider || provider->IsKDF())
return {};
std::string output;
auto blocks = static_cast<size_t>(std::ceil(static_cast<double>(length) / provider->digest_size));
for (size_t block = 1; block <= blocks; ++block)
{
auto block_data = salt;
for (size_t i = 0; i < 4; ++i)
block_data.push_back(block >> (24 - i * 8) & 0x0F);
auto block_hash = Hash::HMAC(provider, plain, block_data);
auto prevhash = block_hash;
for (size_t iteration = 1; iteration < iterations; ++iteration)
{
auto hash = Hash::HMAC(provider, plain, prevhash);
for (size_t i = 0; i < provider->digest_size; ++i)
block_hash[i] ^= hash[i];
prevhash = hash;
}
output += block_hash;
}
output.erase(length);
return output;
}
}
struct PBKDF2Config final
{
uint32_t iterations = 0;
size_t key_length = 0;
size_t salt_length = 0;
void ReadConfig(const std::shared_ptr<ConfigTag>& tag, PBKDF2Config* def = nullptr)
{
iterations = tag->getNum<uint32_t>("iterations", def ? def->iterations : 15'000, 1000);
key_length = tag->getNum<size_t>("length", def ? def->key_length : 32, 1, 1024);
salt_length = tag->getNum<size_t>("saltlength", def ? def->salt_length : key_length, 1, 1024);
}
};
class PBKDF2Context final
: public Hash::Context
{
private:
std::string buffer;
const PBKDF2Config& config;
Hash::ProviderRef provider;
std::string GenerateSalt()
{
std::vector<char> salt(config.salt_length);
ServerInstance->GenRandom(salt.data(), salt.size());
return std::string(salt.data(), salt.size());
}
public:
PBKDF2Context(const PBKDF2Config& c, const Hash::ProviderRef& prov)
: config(c)
, provider(prov)
{
}
void Update(const unsigned char *data, size_t len) override
{
buffer.append(reinterpret_cast<const char *>(data), len);
}
std::string Finalize() override
{
if (!provider)
return {}; // No underlying hash (should never happen).
auto salt = this->GenerateSalt();
auto hash = PBKDF2(*provider, buffer, salt, config.iterations, config.key_length);
if (hash.empty())
return {};
this->buffer.clear();
return FMT::format("{}:{}:{}", config.iterations, Base64::Encode(hash), Base64::Encode(salt));
}
};
class PBKDF2Provider final
: public Hash::Provider
{
private:
Hash::ProviderRef provider;
public:
PBKDF2Config config;
PBKDF2Provider(Module* mod, const std::string& algorithm)
: Hash::Provider(mod, FMT::format("pbkdf2-hmac-{}", algorithm))
, provider(mod, algorithm)
{
DisableAutoRegister();
}
bool Compare(const std::string& hash, const std::string& plain) override
{
if (!provider)
return false; // No underlying hash (should never happen).
uint32_t iterations;
std::string key, salt;
irc::sepstream stream(hash, ':');
if (!stream.GetNumericToken(iterations) || !iterations || !stream.GetToken(key) || !stream.GetToken(salt))
return false; // Malformed hash.
auto rawkey = Base64::Decode(key);
auto rawsalt = Base64::Decode(salt);
auto expected = PBKDF2(*provider, plain, rawsalt, iterations, rawkey.length());
return !expected.empty() && InspIRCd::TimingSafeCompare(rawkey, expected);
}
std::unique_ptr<Hash::Context> CreateContext() override
{
return std::make_unique<PBKDF2Context>(config, provider);
}
std::string ToPrintable(const std::string &hash) override
{
// We have no way to make this printable without the creating context
// so we always return the printed form.
return hash;
}
};
class ModuleHashPBKDF2 final
: public Module
{
private:
insp::flat_map<std::string, PBKDF2Provider*> algos;
insp::flat_map<std::string, PBKDF2Config> configs;
PBKDF2Config defaultconfig;
void Configure(PBKDF2Provider* algo)
{
auto it = configs.find(algo->GetAlgorithm());
if (it == configs.end())
algo->config = defaultconfig;
else
algo->config = it->second;
}
public:
ModuleHashPBKDF2()
: Module(VF_VENDOR, "Allows other modules to generate PBKDF2 hashes.")
{
}
~ModuleHashPBKDF2() override
{
for (const auto &[_, algo] : algos)
delete algo;
}
void init() override
{
// Let ourself know about any existing services.
for (const auto& [_, service] : ServerInstance->Modules.DataProviders)
OnServiceAdd(*service);
}
void ReadConfig(ConfigStatus& status) override
{
// Read the config.
PBKDF2Config newdefaultconfig;
newdefaultconfig.ReadConfig(ServerInstance->Config->ConfValue("pbkdf2"));
insp::flat_map<std::string, PBKDF2Config> newconfigs;
for (const auto& [_, tag] : ServerInstance->Config->ConfTags("pbkdf2prov"))
{
const auto hash = tag->getString("hash");
if (hash.empty())
throw ModuleException(this, "<pbkdf2prov:name> must not be empty, at {}", tag->source.str());
PBKDF2Config newprovconfig;
newprovconfig.ReadConfig(tag, &newdefaultconfig);
if (!newconfigs.emplace(hash, newprovconfig).second)
throw ModuleException(this, "<pbkdf2prov:name> must be unique, at {}", tag->source.str());
}
// Apply the config.
std::swap(defaultconfig, newdefaultconfig);
std::swap(configs, newconfigs);
// Apply the config to the providers.
for (const auto &[_, algo] : algos)
Configure(algo);
}
void OnServiceAdd(ServiceProvider& service) override
{
if (!service.name.starts_with("hash/"))
return; // Not a hash provider.
auto* hp = static_cast<Hash::Provider*>(&service);
if (hp->IsKDF())
return; // Can't use PBKDF2 with a KDF.
if (algos.find(hp->GetAlgorithm()) != algos.end())
return; // Already created.
auto* algo = new PBKDF2Provider(this, hp->GetAlgorithm());
Configure(algo);
ServerInstance->Logs.Debug("HASH", "The {} algorithm was added by {}, also adding {}",
hp->GetAlgorithm(), hp->creator->ModuleFile, algo->GetAlgorithm());
try
{
auto check = checks.find(hp->GetAlgorithm());
if (check != checks.end())
{
algo->Check(check->second);
}
else
{
ServerInstance->Logs.Debug("HASH", "The {} algorithm lacks runtime checks, unable to verify integrity.",
algo->GetAlgorithm());
}
}
catch (const ModuleException& err)
{
ServerInstance->Logs.Critical("HASH", "{}", err.GetReason());
delete algo;
return; // Broken algorithm.
}
algos.emplace(hp->GetAlgorithm(), algo);
ServerInstance->Modules.AddService(*algo);
}
void OnServiceDel(ServiceProvider& service) override
{
if (!service.name.starts_with("hash/"))
return; // Not a hash provider.
const auto& hp = static_cast<Hash::Provider&>(service);
auto it = algos.find(hp.GetAlgorithm());
if (it != algos.end())
{
ServerInstance->Logs.Debug("HASH", "The {} algorithm was deleted by {}, also deleting {}",
hp.GetAlgorithm(), hp.creator->ModuleFile, it->second->GetAlgorithm());
delete it->second;
algos.erase(it);
}
}
};
MODULE_INIT(ModuleHashPBKDF2)
|