From defb8348a79e499700cefaf2f4a63daefeaaac7e Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sat, 9 Mar 2024 22:51:23 +0000 Subject: Move the HMAC function to the encryption header. This will be useful for doing challenge authentication on InspIRCd. --- include/modules/encryption.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'include') diff --git a/include/modules/encryption.h b/include/modules/encryption.h index 6b13fcf41..0eec23ec3 100644 --- a/include/modules/encryption.h +++ b/include/modules/encryption.h @@ -73,6 +73,27 @@ namespace Encryption context->Update(std::forward(args)...); return context->Finalize(); } + + inline Anope::string HMAC(const Anope::string &key, const Anope::string &data) + { + if (!block_size) + return {}; + + auto keybuf = key.length() > block_size ? Encrypt(key) : key; + keybuf.resize(block_size); + + Anope::string hmac1; + Anope::string hmac2; + for (size_t i = 0; i < block_size; ++i) + { + hmac1.push_back(static_cast(keybuf[i] ^ 0x5C)); + hmac2.push_back(static_cast(keybuf[i] ^ 0x36)); + } + hmac2.append(data); + hmac1.append(Encrypt(hmac2)); + + return Encrypt(hmac1); + } }; /** Helper template for creating simple providers of encryption contexts. */ -- cgit