#ifndef TGCALLS_CRYPTO_HELPER_H #define TGCALLS_CRYPTO_HELPER_H extern "C" { #include #include #ifndef OPENSSL_IS_BORINGSSL #include #endif #include #include } // extern "C" #include namespace tgcalls { struct MemorySpan { MemorySpan(const void *data, size_t size) : data(data), size(size) { } const void *data = nullptr; size_t size = 0; }; struct AesKeyIv { std::array key; std::array iv; }; constexpr auto kSha256Size = size_t(SHA256_DIGEST_LENGTH); template void SHA256Update(SHA256_CTX*, Parts &&...parts); inline void SHA256Update(SHA256_CTX*) { } template inline void SHA256Update(SHA256_CTX *context, First &&span, Others &&...others) { static_assert( std::is_same, MemorySpan>::value, "Pass some MemorySpan-s here."); SHA256_Update(context, span.data, span.size); SHA256Update(context, std::forward(others)...); } template inline std::array ConcatSHA256(Parts &&... parts) { static_assert(sizeof...(parts) > 0, "empty list"); auto result = std::array(); auto context = SHA256_CTX(); SHA256_Init(&context); SHA256Update(&context, std::forward(parts)...); SHA256_Final(result.data(), &context); return result; } AesKeyIv PrepareAesKeyIv(const uint8_t *key, const uint8_t *msgKey, int x); void AesProcessCtr(MemorySpan from, void *to, AesKeyIv &&aesKeyIv); } // namespace tgcalls #endif