aboutsummaryrefslogtreecommitdiff
path: root/zencore/include
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2022-02-07 13:00:29 +0100
committerPer Larsson <[email protected]>2022-02-07 13:00:29 +0100
commitaba862f45cbde3debe99e31bc8ec3e338c5bbf4a (patch)
treed47580f8027d90e53a084bbb19192e141f497637 /zencore/include
parentMissing override suffix compile fix (diff)
downloadzen-aba862f45cbde3debe99e31bc8ec3e338c5bbf4a.tar.xz
zen-aba862f45cbde3debe99e31bc8ec3e338c5bbf4a.zip
Replaced crypto transform abstraction with a concrete API.
Diffstat (limited to 'zencore/include')
-rw-r--r--zencore/include/zencore/crypto.h67
1 files changed, 50 insertions, 17 deletions
diff --git a/zencore/include/zencore/crypto.h b/zencore/include/zencore/crypto.h
index 44783cdeb..83d416b0f 100644
--- a/zencore/include/zencore/crypto.h
+++ b/zencore/include/zencore/crypto.h
@@ -7,36 +7,69 @@
#include <zencore/zencore.h>
#include <memory>
+#include <optional>
namespace zen {
-/**
- * Experimental interface for a symmetric encryption/decryption algorithm.
- * Currenlty only AES 256 bit CBC is supported using OpenSSL.
- */
-class SymmetricCipher
+template<size_t BitCount>
+struct CryptoBits
{
public:
- virtual ~SymmetricCipher() = default;
+ static constexpr size_t ByteCount = BitCount / 8;
- virtual bool Initialize(MemoryView Key, MemoryView InitVector) = 0;
+ CryptoBits() = default;
- struct CipherSettings
+ bool IsNull() const { return memcmp(&m_Bits, &Zero, ByteCount) == 0; }
+ bool IsValid() const { return IsNull() == false; }
+
+ size_t GetSize() const { return ByteCount; }
+ size_t GetBitCount() const { return BitCount; }
+
+ MemoryView GetView() const { return MemoryView(m_Bits, ByteCount); }
+
+ static CryptoBits FromMemoryView(MemoryView Bits)
{
- size_t KeySize = 0;
- size_t InitVectorSize = 0;
- size_t BlockSize = 0;
- };
+ if (Bits.GetSize() != ByteCount)
+ {
+ return CryptoBits();
+ }
- virtual CipherSettings Settings() = 0;
+ return CryptoBits(Bits);
+ }
+
+ static CryptoBits FromString(std::string_view Str) { return FromMemoryView(MakeMemoryView(Str)); }
+
+private:
+ CryptoBits(MemoryView Bits)
+ {
+ ZEN_ASSERT(Bits.GetSize() == GetSize());
+ memcpy(&m_Bits, Bits.GetData(), GetSize());
+ }
- virtual MemoryView Encrypt(MemoryView Data, MutableMemoryView EncryptionBuffer) = 0;
+ static constexpr uint8_t Zero[ByteCount] = {0};
- virtual MemoryView Decrypt(MemoryView Data, MutableMemoryView DecryptionBuffer) = 0;
+ uint8_t m_Bits[ByteCount] = {0};
+};
+
+using AesKey256Bit = CryptoBits<256>;
+using AesIV128Bit = CryptoBits<128>;
+
+class Aes
+{
+public:
+ static constexpr size_t BlockSize = 16;
- static std::unique_ptr<SymmetricCipher> CreateNoOp();
+ static MemoryView Encrypt(const AesKey256Bit& Key,
+ const AesIV128Bit& IV,
+ MemoryView In,
+ MutableMemoryView Out,
+ std::optional<std::string>& Reason);
- static std::unique_ptr<SymmetricCipher> CreateAes();
+ static MemoryView Decrypt(const AesKey256Bit& Key,
+ const AesIV128Bit& IV,
+ MemoryView In,
+ MutableMemoryView Out,
+ std::optional<std::string>& Reason);
};
void crypto_forcelink();