diff options
author | Sadie Powell <sadie@witchery.services> | 2024-03-09 17:08:26 +0000 |
---|---|---|
committer | Sadie Powell <sadie@witchery.services> | 2024-03-09 22:22:56 +0000 |
commit | a6bc4cab9d0b10a46690cffb15f87b7528e5bb62 (patch) | |
tree | 44c49a2b7f0305af9ad835bdf232869730043617 /modules/encryption/enc_sha1.cpp | |
parent | ef37daf44a97e7ff715b61eadf83f1478365a720 (diff) |
Misc improvements to the encryption API.
Diffstat (limited to 'modules/encryption/enc_sha1.cpp')
-rw-r--r-- | modules/encryption/enc_sha1.cpp | 67 |
1 files changed, 14 insertions, 53 deletions
diff --git a/modules/encryption/enc_sha1.cpp b/modules/encryption/enc_sha1.cpp index 07d6d094d..4f5ca1955 100644 --- a/modules/encryption/enc_sha1.cpp +++ b/modules/encryption/enc_sha1.cpp @@ -60,7 +60,6 @@ class SHA1Context final uint32_t state[5]; uint32_t count[2]; unsigned char buffer[64]; - unsigned char digest[20]; void Transform(const unsigned char buf[64]) { @@ -108,22 +107,13 @@ class SHA1Context final } public: - SHA1Context(Encryption::IV *iv = NULL) + SHA1Context() { - if (iv != NULL) - { - if (iv->second != 5) - throw CoreException("Invalid IV size"); - for (int i = 0; i < 5; ++i) - this->state[i] = iv->first[i]; - } - else - for (int i = 0; i < 5; ++i) - this->state[i] = sha1_iv[i]; + for (int i = 0; i < 5; ++i) + this->state[i] = sha1_iv[i]; this->count[0] = this->count[1] = 0; memset(this->buffer, 0, sizeof(this->buffer)); - memset(this->digest, 0, sizeof(this->digest)); } void Update(const unsigned char *data, size_t len) override @@ -147,7 +137,7 @@ public: memcpy(&this->buffer[j], &data[i], len - i); } - void Finalize() override + Anope::string Finalize() override { uint32_t i; unsigned char finalcount[8]; @@ -158,8 +148,10 @@ public: while ((this->count[0] & 504) != 448) this->Update(reinterpret_cast<const unsigned char *>("\0"), 1); this->Update(finalcount, 8); /* Should cause a SHA1Transform() */ + unsigned char digest[20]; + memset(digest, 0, sizeof(digest)); for (i = 0; i < 20; ++i) - this->digest[i] = static_cast<unsigned char>((this->state[i>>2] >> ((3 - (i & 3)) * 8)) & 255); + digest[i] = static_cast<unsigned char>((this->state[i>>2] >> ((3 - (i & 3)) * 8)) & 255); /* Wipe variables */ memset(this->buffer, 0, sizeof(this->buffer)); @@ -168,45 +160,21 @@ public: memset(&finalcount, 0, sizeof(finalcount)); this->Transform(this->buffer); - } - - Encryption::Hash GetFinalizedHash() override - { - Encryption::Hash hash; - hash.first = this->digest; - hash.second = sizeof(this->digest); - return hash; - } -}; - -class SHA1Provider final - : public Encryption::Provider -{ -public: - SHA1Provider(Module *creator) : Encryption::Provider(creator, "sha1") { } - Encryption::Context *CreateContext(Encryption::IV *iv) override - { - return new SHA1Context(iv); - } - - Encryption::IV GetDefaultIV() override - { - Encryption::IV iv; - iv.first = sha1_iv; - iv.second = sizeof(sha1_iv) / sizeof(uint32_t); - return iv; + return Anope::string(reinterpret_cast<const char *>(&digest), sizeof(digest)); } }; class ESHA1 final : public Module { - SHA1Provider sha1provider; +private: + Encryption::SimpleProvider<SHA1Context> sha1provider; public: - ESHA1(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, ENCRYPTION | VENDOR), - sha1provider(this) + ESHA1(const Anope::string &modname, const Anope::string &creator) + : Module(modname, creator, ENCRYPTION | VENDOR) + , sha1provider(this, "sha1", 20, 64) { if (ModuleManager::FindFirstOf(ENCRYPTION) == this) throw ModuleException("enc_sha1 is deprecated and can not be used as a primary encryption method"); @@ -214,14 +182,7 @@ public: EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) override { - SHA1Context context; - - context.Update(reinterpret_cast<const unsigned char *>(src.c_str()), src.length()); - context.Finalize(); - - Encryption::Hash hash = context.GetFinalizedHash(); - - Anope::string buf = "sha1:" + Anope::Hex(reinterpret_cast<const char *>(hash.first), hash.second); + Anope::string buf = "sha1:" + Anope::Hex(sha1provider.Encrypt(src)); Log(LOG_DEBUG_2) << "(enc_sha1) hashed password from [" << src << "] to [" << buf << "]"; dest = buf; |