aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2025-10-26 18:34:15 +0100
committerStefan Boberg <[email protected]>2025-10-26 18:34:15 +0100
commit8c90c6c9e92171c44a7a2d7899079f2a5701486b (patch)
treee898b89825db3d3cb04a15b989610316b1fa680d /src
parentin-tree: everything else (diff)
downloadzen-8c90c6c9e92171c44a7a2d7899079f2a5701486b.tar.xz
zen-8c90c6c9e92171c44a7a2d7899079f2a5701486b.zip
clang-format
Diffstat (limited to 'src')
-rw-r--r--src/zencore/crypto.cpp172
1 files changed, 84 insertions, 88 deletions
diff --git a/src/zencore/crypto.cpp b/src/zencore/crypto.cpp
index baf8a3a35..de03cbc6f 100644
--- a/src/zencore/crypto.cpp
+++ b/src/zencore/crypto.cpp
@@ -27,7 +27,7 @@ ZEN_THIRD_PARTY_INCLUDES_START
# include <openssl/err.h>
# include <openssl/evp.h>
#elif ZEN_USE_MBEDTLS
- #include <mbedtls/cipher.h>
+# include <mbedtls/cipher.h>
#else
# include <zencore/windows.h>
# include <bcrypt.h>
@@ -49,105 +49,101 @@ namespace crypto {
#if ZEN_USE_MBEDTLS
- class MbedCipherCtx
- {
- public:
- MbedCipherCtx() { mbedtls_cipher_init(&m_Ctx); }
- ~MbedCipherCtx() { mbedtls_cipher_free(&m_Ctx); }
-
- mbedtls_cipher_context_t* operator&() { return &m_Ctx; }
- mbedtls_cipher_context_t* get() { return &m_Ctx; }
-
- private:
- mbedtls_cipher_context_t m_Ctx;
- };
-
- MemoryView Transform(TransformMode Mode,
- MemoryView Key,
- MemoryView IV,
- MemoryView In,
- MutableMemoryView Out,
- std::optional<std::string>& Reason)
- {
- const mbedtls_cipher_info_t* CipherInfo = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC);
- if (CipherInfo == nullptr)
- {
- Reason = "failed to get mbedTLS cipher info"sv;
- return MemoryView();
- }
+ class MbedCipherCtx
+ {
+ public:
+ MbedCipherCtx() { mbedtls_cipher_init(&m_Ctx); }
+ ~MbedCipherCtx() { mbedtls_cipher_free(&m_Ctx); }
- MbedCipherCtx Ctx;
- int ret = mbedtls_cipher_setup(Ctx.get(), CipherInfo);
- if (ret != 0)
- {
- Reason = fmt::format("mbedTLS cipher setup failed, ret={}", ret);
- return MemoryView();
- }
+ mbedtls_cipher_context_t* operator&() { return &m_Ctx; }
+ mbedtls_cipher_context_t* get() { return &m_Ctx; }
- // key length in bits
- ret = mbedtls_cipher_setkey(Ctx.get(),
- reinterpret_cast<const unsigned char*>(Key.GetData()),
- static_cast<int>(Key.GetSize() * 8),
- (Mode == TransformMode::Encrypt) ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT);
- if (ret != 0)
- {
- Reason = fmt::format("mbedTLS setkey failed, ret={}", ret);
- return MemoryView();
- }
+ private:
+ mbedtls_cipher_context_t m_Ctx;
+ };
- ret = mbedtls_cipher_set_iv(Ctx.get(),
- reinterpret_cast<const unsigned char*>(IV.GetData()),
- static_cast<size_t>(IV.GetSize()));
- if (ret != 0)
- {
- Reason = fmt::format("mbedTLS set_iv failed, ret={}", ret);
- return MemoryView();
- }
+ MemoryView Transform(TransformMode Mode,
+ MemoryView Key,
+ MemoryView IV,
+ MemoryView In,
+ MutableMemoryView Out,
+ std::optional<std::string>& Reason)
+ {
+ const mbedtls_cipher_info_t* CipherInfo = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CBC);
+ if (CipherInfo == nullptr)
+ {
+ Reason = "failed to get mbedTLS cipher info"sv;
+ return MemoryView();
+ }
- ret = mbedtls_cipher_reset(Ctx.get());
- if (ret != 0)
- {
- Reason = fmt::format("mbedTLS reset failed, ret={}", ret);
- return MemoryView();
- }
+ MbedCipherCtx Ctx;
+ int ret = mbedtls_cipher_setup(Ctx.get(), CipherInfo);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS cipher setup failed, ret={}", ret);
+ return MemoryView();
+ }
- // Ensure output buffer is large enough: worst case = input + block size
- const size_t BlockSize = 16;
- if (Out.GetSize() < In.GetSize() + BlockSize)
- {
- Reason = "invalid output buffer size"sv;
- return MemoryView();
- }
+ // key length in bits
+ ret = mbedtls_cipher_setkey(Ctx.get(),
+ reinterpret_cast<const unsigned char*>(Key.GetData()),
+ static_cast<int>(Key.GetSize() * 8),
+ (Mode == TransformMode::Encrypt) ? MBEDTLS_ENCRYPT : MBEDTLS_DECRYPT);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS setkey failed, ret={}", ret);
+ return MemoryView();
+ }
- size_t olen = 0;
- size_t total = 0;
+ ret = mbedtls_cipher_set_iv(Ctx.get(), reinterpret_cast<const unsigned char*>(IV.GetData()), static_cast<size_t>(IV.GetSize()));
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS set_iv failed, ret={}", ret);
+ return MemoryView();
+ }
- ret = mbedtls_cipher_update(Ctx.get(),
- reinterpret_cast<const unsigned char*>(In.GetData()),
- static_cast<size_t>(In.GetSize()),
- reinterpret_cast<unsigned char*>(Out.GetData()),
- &olen);
- if (ret != 0)
- {
- Reason = fmt::format("mbedTLS update failed, ret={}", ret);
- return MemoryView();
- }
+ ret = mbedtls_cipher_reset(Ctx.get());
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS reset failed, ret={}", ret);
+ return MemoryView();
+ }
- total = olen;
+ // Ensure output buffer is large enough: worst case = input + block size
+ const size_t BlockSize = 16;
+ if (Out.GetSize() < In.GetSize() + BlockSize)
+ {
+ Reason = "invalid output buffer size"sv;
+ return MemoryView();
+ }
- ret = mbedtls_cipher_finish(Ctx.get(),
- reinterpret_cast<unsigned char*>(Out.GetData()) + total,
- &olen);
- if (ret != 0)
- {
- Reason = fmt::format("mbedTLS finish failed, ret={}", ret);
- return MemoryView();
- }
+ size_t olen = 0;
+ size_t total = 0;
+
+ ret = mbedtls_cipher_update(Ctx.get(),
+ reinterpret_cast<const unsigned char*>(In.GetData()),
+ static_cast<size_t>(In.GetSize()),
+ reinterpret_cast<unsigned char*>(Out.GetData()),
+ &olen);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS update failed, ret={}", ret);
+ return MemoryView();
+ }
- total += olen;
+ total = olen;
- return Out.Left(static_cast<size_t>(total));
+ ret = mbedtls_cipher_finish(Ctx.get(), reinterpret_cast<unsigned char*>(Out.GetData()) + total, &olen);
+ if (ret != 0)
+ {
+ Reason = fmt::format("mbedTLS finish failed, ret={}", ret);
+ return MemoryView();
}
+
+ total += olen;
+
+ return Out.Left(static_cast<size_t>(total));
+ }
#elif ZEN_USE_OPENSSL
class EvpContext
{