summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2024-03-10 16:14:22 +0000
committerSadie Powell <sadie@witchery.services>2024-03-10 20:20:24 +0000
commitf919bb0748fe1ba09114f22841efb5af7c5bb37d (patch)
tree109cf55b73ad6c11d47233429d0c98723fea165a
parent3b85a8071f4d7238088834b4924af79b57cb36d1 (diff)
Add self-tests to the encryption providers.
-rw-r--r--include/modules/encryption.h19
-rw-r--r--modules/encryption/enc_md5.cpp5
-rw-r--r--modules/encryption/enc_sha1.cpp5
-rw-r--r--modules/encryption/enc_sha2.cpp16
-rw-r--r--modules/extra/enc_argon2.cpp7
5 files changed, 51 insertions, 1 deletions
diff --git a/include/modules/encryption.h b/include/modules/encryption.h
index b8ec8ff58..c475f4fbd 100644
--- a/include/modules/encryption.h
+++ b/include/modules/encryption.h
@@ -66,7 +66,18 @@ namespace Encryption
/** Checks whether a plain text value matches a hash created by this provider. */
virtual bool Compare(const Anope::string &hash, const Anope::string &plain)
{
- return hash.equals_cs(plain);
+ return !hash.empty() && hash.equals_cs(ToPrintable(Encrypt(plain)));
+ }
+
+ /** Called on initialising a encryption provider to check it works properly. */
+ void Check(const Anope::map<Anope::string> &checks)
+ {
+ for (const auto &[hash, plain] : checks)
+ {
+ if (!Compare(hash, plain))
+ throw ModuleException("BUG: unable to generate " + this->name + " hashes safely! Please report this!");
+ }
+ Log(LOG_DEBUG) << "The " << this->name << " encryption provider appears to be working correctly.";
}
/** Creates a new encryption context. */
@@ -102,6 +113,12 @@ namespace Encryption
return Encrypt(hmac1);
}
+
+ /** Converts a hash to its printable form. */
+ virtual Anope::string ToPrintable(const Anope::string &hash)
+ {
+ return Anope::Hex(hash);
+ }
};
/** Helper template for creating simple providers of encryption contexts. */
diff --git a/modules/encryption/enc_md5.cpp b/modules/encryption/enc_md5.cpp
index 69665f3bc..1582c5224 100644
--- a/modules/encryption/enc_md5.cpp
+++ b/modules/encryption/enc_md5.cpp
@@ -54,6 +54,11 @@ public:
{
if (ModuleManager::FindFirstOf(ENCRYPTION) == this)
throw ModuleException("enc_md5 is deprecated and can not be used as a primary encryption method");
+
+ md5provider.Check({
+ { "d41d8cd98f00b204e9800998ecf8427e", "" },
+ { "9e107d9d372bb6826bd81d3542a419d6", "The quick brown fox jumps over the lazy dog" },
+ });
}
EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) override
diff --git a/modules/encryption/enc_sha1.cpp b/modules/encryption/enc_sha1.cpp
index 4f5ca1955..c603d4d0e 100644
--- a/modules/encryption/enc_sha1.cpp
+++ b/modules/encryption/enc_sha1.cpp
@@ -178,6 +178,11 @@ public:
{
if (ModuleManager::FindFirstOf(ENCRYPTION) == this)
throw ModuleException("enc_sha1 is deprecated and can not be used as a primary encryption method");
+
+ sha1provider.Check({
+ { "da39a3ee5e6b4b0d3255bfef95601890afd80709", "" },
+ { "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", "The quick brown fox jumps over the lazy dog" },
+ });
}
EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) override
diff --git a/modules/encryption/enc_sha2.cpp b/modules/encryption/enc_sha2.cpp
index 31b055c6b..4dbbcd9de 100644
--- a/modules/encryption/enc_sha2.cpp
+++ b/modules/encryption/enc_sha2.cpp
@@ -108,6 +108,22 @@ public:
, sha384provider(this, "sha384", SHA384_BLOCK_SIZE, SHA384_DIGEST_SIZE)
, sha512provider(this, "sha512", SHA512_BLOCK_SIZE, SHA512_DIGEST_SIZE)
{
+ sha224provider.Check({
+ { "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f", "" },
+ { "730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525", "The quick brown fox jumps over the lazy dog" },
+ });
+ sha256provider.Check({
+ { "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "" },
+ { "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592", "The quick brown fox jumps over the lazy dog" },
+ });
+ sha384provider.Check({
+ { "38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b", "" },
+ { "ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1", "The quick brown fox jumps over the lazy dog" },
+ });
+ sha512provider.Check({
+ { "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e", "" },
+ { "07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6", "The quick brown fox jumps over the lazy dog" },
+ });
}
void OnReload(Configuration::Conf *conf) override
diff --git a/modules/extra/enc_argon2.cpp b/modules/extra/enc_argon2.cpp
index 6454411eb..266a80d30 100644
--- a/modules/extra/enc_argon2.cpp
+++ b/modules/extra/enc_argon2.cpp
@@ -104,6 +104,13 @@ public:
{
return std::make_unique<Argon2Context>(this->type);
}
+
+ Anope::string ToPrintable(const Anope::string &hash) override
+ {
+ // We have no way to make this printable without the creating context
+ // so we always return the printed form.
+ return hash;
+ }
};