aboutsummaryrefslogtreecommitdiff
path: root/modules/hash_bcrypt.cpp
blob: bacca2493d4f4ed64349f2aa2d15ee708d3a0db6 (about) (plain) (blame)
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
/*
 * 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/>.
 */

/// BEGIN CMAKE
/// target_link_libraries(${TARGET} PRIVATE "vendored_bcrypt")
/// END CMAKE


#include "inspircd.h"
#include "modules/hash.h"

extern "C"
{
#include <bcrypt/crypt_blowfish.h>
}

class BCryptContext final
	: public Hash::Context
{
private:
	std::string buffer;

	std::string GenerateSalt()
	{
		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)))
		{
			ServerInstance->Logs.Debug("HASH", "Unable to generate a bcrypt salt: {}", strerror(errno));
			return {};
		}
		return salt;
	}

public:
	static unsigned long rounds;

	static std::string Hash(const std::string& data, const std::string& salt)
	{
		char hash[64];
		if (!_crypt_blowfish_rn(data.c_str(), salt.c_str(), hash, sizeof(hash)))
		{
			ServerInstance->Logs.Debug("HASH", "Unable to generate a bcrypt hash: {}", strerror(errno));
			return {};
		}
		return hash;
	}

	void Update(const unsigned char* data, size_t len) override
	{
		buffer.append(reinterpret_cast<const char *>(data), len);
	}

	std::string Finalize() override
	{
		auto salt = GenerateSalt();
		if (salt.empty())
			return {};
		return Hash(this->buffer, salt);
	}
};

unsigned long BCryptContext::rounds = 10;

class BCryptProvider final
	: public Hash::Provider
{
public:
	BCryptProvider(const WeakModulePtr& mod)
		: Hash::Provider(mod, "bcrypt", 60)
	{
	}

	bool Compare(const std::string& hash, const std::string& plain) override
	{
		auto newhash = BCryptContext::Hash(plain, hash);
		return !newhash.empty() && InspIRCd::TimingSafeCompare(hash, newhash);
	}

	std::unique_ptr<Hash::Context> CreateContext() override
	{
		return std::make_unique<BCryptContext>();
	}

	std::string ToPrintable(const std::string& hash) override
	{
		// The crypt_blowfish library does not expose a raw form.
		return hash;
	}
};

class ModuleHashBCrypt final
	: public Module
{
private:
	BCryptProvider bcryptalgo;

public:
	ModuleHashBCrypt()
		: Module(VF_VENDOR, "Allows other modules to generate bcrypt hashes.")
		, bcryptalgo(weak_from_this())
	{
	}

	void init() override
	{
		bcryptalgo.Check({
			{ "$2a$10$c9lUAuJmTYXEfNuLOiyIp.lZTMM.Rw5qsSAyZhvGT9EC3JevkUuOu", "" },
			{ "$2a$10$YV4jDSGs0ZtQbpL6IHtNO.lt5Q.uzghIohCcnERQVBGyw7QJMfyhe", "The quick brown fox jumps over the lazy dog" },
		});
	}

	void ReadConfig(ConfigStatus& status) override
	{
		const auto& conf = ServerInstance->Config->ConfValue("bcrypt");
		BCryptContext::rounds = conf->getNum<unsigned long>("rounds", 10, 1);
	}
};

MODULE_INIT(ModuleHashBCrypt)