From a6bc4cab9d0b10a46690cffb15f87b7528e5bb62 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sat, 9 Mar 2024 17:08:26 +0000 Subject: Misc improvements to the encryption API. --- include/modules/encryption.h | 79 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/modules/encryption.h b/include/modules/encryption.h index 39eab638b..6b13fcf41 100644 --- a/include/modules/encryption.h +++ b/include/modules/encryption.h @@ -13,26 +13,89 @@ namespace Encryption { - typedef std::pair Hash; - typedef std::pair IV; - + /** Base class for encryption contexts. */ class Context { public: virtual ~Context() = default; + + /** Updates the encryption context with the specified data. + * @param str The data to update the context with. + */ + inline void Update(const Anope::string &str) + { + Update(reinterpret_cast(str.c_str()), str.length()); + } + + /** Updates the encryption context with the specified data. + * @param data The data to update the context with. + * @param len The length of the data. + */ virtual void Update(const unsigned char *data, size_t len) = 0; - virtual void Finalize() = 0; - virtual Hash GetFinalizedHash() = 0; + + /** Finalises the encryption context and returns the digest. */ + virtual Anope::string Finalize() = 0; }; + /** Provider of encryption contexts. */ class Provider : public Service { public: - Provider(Module *creator, const Anope::string &sname) : Service(creator, "Encryption::Provider", sname) { } + /** The byte size of the block cipher. */ + const size_t block_size; + + /** The byte size of the resulting digest. */ + const size_t digest_size; + + /** Creates a provider of encryption contexts. + * @param creator The module that created this provider. + * @param algorithm The name of the encryption algorithm. + * @param bs The byte size of the block cipher. + * @param ds The byte size of the resulting digest. + */ + Provider(Module *creator, const Anope::string &algorithm, size_t bs, size_t ds) + : Service(creator, "Encryption::Provider", algorithm) + , block_size(bs) + , digest_size(ds) + { + } + virtual ~Provider() = default; - virtual Context *CreateContext(IV * = NULL) = 0; - virtual IV GetDefaultIV() = 0; + /** Creates a new encryption context. */ + virtual std::unique_ptr CreateContext() = 0; + + template + Anope::string Encrypt(Args &&...args) + { + auto context = CreateContext(); + context->Update(std::forward(args)...); + return context->Finalize(); + } + }; + + /** Helper template for creating simple providers of encryption contexts. */ + template + class SimpleProvider final + : public Provider + { + public: + /** Creates a simple provider of encryption contexts. + * @param creator The module that created this provider. + * @param algorithm The name of the encryption algorithm. + * @param bs The byte size of the block cipher. + * @param ds The byte size of the resulting digest. + */ + SimpleProvider(Module *creator, const Anope::string &algorithm, size_t bs, size_t ds) + : Provider(creator, algorithm, bs, ds) + { + } + + /** @copydoc Encryption::Provider::CreateContext. */ + std::unique_ptr CreateContext() override + { + return std::make_unique(); + } }; } -- cgit