From f53eb8e260d174153bb3fc24ff1fff7966dcfbee Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Mon, 27 Nov 2017 09:10:17 +0200 Subject: SIMD speed optimization for Kuznyechik cipher implementation (up to 2x speedup). Based on https://github.com/aprelev/libgost15. --- src/Volume/Cipher.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'src/Volume/Cipher.cpp') diff --git a/src/Volume/Cipher.cpp b/src/Volume/Cipher.cpp index de351ad4..32f61b76 100644 --- a/src/Volume/Cipher.cpp +++ b/src/Volume/Cipher.cpp @@ -462,5 +462,53 @@ namespace VeraCrypt { kuznyechik_set_key (key, (kuznyechik_kds *) ScheduledKey.Ptr()); } + void CipherKuznyechik::EncryptBlocks (byte *data, size_t blockCount) const + { + if (!Initialized) + throw NotInitialized (SRC_POS); + +#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE + if ((blockCount >= 4) + && IsHwSupportAvailable()) + { + kuznyechik_encrypt_blocks (data, data, blockCount, (kuznyechik_kds *) ScheduledKey.Ptr()); + } + else +#endif + Cipher::EncryptBlocks (data, blockCount); + } + + void CipherKuznyechik::DecryptBlocks (byte *data, size_t blockCount) const + { + if (!Initialized) + throw NotInitialized (SRC_POS); + +#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE + if ((blockCount >= 4) + && IsHwSupportAvailable()) + { + kuznyechik_decrypt_blocks (data, data, blockCount, (kuznyechik_kds *) ScheduledKey.Ptr()); + } + else +#endif + Cipher::DecryptBlocks (data, blockCount); + } + + bool CipherKuznyechik::IsHwSupportAvailable () const + { +#if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE + static bool state = false; + static bool stateValid = false; + + if (!stateValid) + { + state = HasSSE2() ? true : false; + stateValid = true; + } + return state; +#else + return false; +#endif + } bool Cipher::HwSupportEnabled = true; } -- cgit v1.2.3