From 46e333d32d71966d51b3deef23dcbda2945c417b Mon Sep 17 00:00:00 2001 From: JinmingHu Date: Mon, 12 Jul 2021 10:07:26 +0800 Subject: [PATCH] Remove sha256 hash from storage internal code (#2585) --- .../test/ut/blob_container_client_test.cpp | 4 +- .../inc/azure/storage/common/crypt.hpp | 1 - .../azure-storage-common/src/crypt.cpp | 60 +------------------ .../test/crypt_functions_test.cpp | 10 ---- 4 files changed, 4 insertions(+), 71 deletions(-) diff --git a/sdk/storage/azure-storage-blobs/test/ut/blob_container_client_test.cpp b/sdk/storage/azure-storage-blobs/test/ut/blob_container_client_test.cpp index 329843b0d..f39d181ff 100644 --- a/sdk/storage/azure-storage-blobs/test/ut/blob_container_client_test.cpp +++ b/sdk/storage/azure-storage-blobs/test/ut/blob_container_client_test.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -566,7 +567,8 @@ namespace Azure { namespace Storage { namespace Test { aes256Key.resize(32); RandomBuffer(&aes256Key[0], aes256Key.size()); key.Key = Azure::Core::Convert::Base64Encode(aes256Key); - key.KeyHash = _internal::Sha256(aes256Key); + key.KeyHash = Azure::Core::Cryptography::_internal::Sha256Hash().Final( + aes256Key.data(), aes256Key.size()); key.Algorithm = Blobs::Models::EncryptionAlgorithmType::Aes256; return key; }; diff --git a/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp b/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp index e6afda269..2fa745b49 100644 --- a/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp +++ b/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp @@ -37,7 +37,6 @@ namespace Azure { namespace Storage { }; namespace _internal { - std::vector Sha256(const std::vector& data); std::vector HmacSha256( const std::vector& data, const std::vector& key); diff --git a/sdk/storage/azure-storage-common/src/crypt.cpp b/sdk/storage/azure-storage-common/src/crypt.cpp index d81500443..8a622a994 100644 --- a/sdk/storage/azure-storage-common/src/crypt.cpp +++ b/sdk/storage/azure-storage-common/src/crypt.cpp @@ -85,7 +85,6 @@ namespace Azure { namespace Storage { enum class AlgorithmType { HmacSha256, - Sha256, }; struct AlgorithmProviderInstance final @@ -97,7 +96,7 @@ namespace Azure { namespace Storage { AlgorithmProviderInstance(AlgorithmType type) { const wchar_t* algorithmId = nullptr; - if (type == AlgorithmType::HmacSha256 || type == AlgorithmType::Sha256) + if (type == AlgorithmType::HmacSha256) { algorithmId = BCRYPT_SHA256_ALGORITHM; } @@ -149,53 +148,6 @@ namespace Azure { namespace Storage { ~AlgorithmProviderInstance() { BCryptCloseAlgorithmProvider(Handle, 0); } }; - std::vector Sha256(const std::vector& data) - { - AZURE_ASSERT_MSG(data.size() <= std::numeric_limits::max(), "Data size is too big."); - - static AlgorithmProviderInstance AlgorithmProvider(AlgorithmType::Sha256); - - std::string context; - context.resize(AlgorithmProvider.ContextSize); - - BCRYPT_HASH_HANDLE hashHandle; - NTSTATUS status = BCryptCreateHash( - AlgorithmProvider.Handle, - &hashHandle, - reinterpret_cast(&context[0]), - static_cast(context.size()), - nullptr, - 0, - 0); - if (!BCRYPT_SUCCESS(status)) - { - throw std::runtime_error("BCryptCreateHash failed."); - } - - status = BCryptHashData( - hashHandle, - reinterpret_cast(const_cast(data.data())), - static_cast(data.size()), - 0); - if (!BCRYPT_SUCCESS(status)) - { - throw std::runtime_error("BCryptHashData failed."); - } - - std::vector hash; - hash.resize(AlgorithmProvider.HashLength); - status = BCryptFinishHash( - hashHandle, reinterpret_cast(&hash[0]), static_cast(hash.size()), 0); - if (!BCRYPT_SUCCESS(status)) - { - throw std::runtime_error("BCryptFinishHash failed."); - } - - BCryptDestroyHash(hashHandle); - - return hash; - } - std::vector HmacSha256( const std::vector& data, const std::vector& key) @@ -250,16 +202,6 @@ namespace Azure { namespace Storage { namespace _internal { - std::vector Sha256(const std::vector& data) - { - SHA256_CTX context; - SHA256_Init(&context); - SHA256_Update(&context, data.data(), data.size()); - unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256_Final(hash, &context); - return std::vector(std::begin(hash), std::end(hash)); - } - std::vector HmacSha256( const std::vector& data, const std::vector& key) diff --git a/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp b/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp index 11ae81402..1fff37ac1 100644 --- a/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp +++ b/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp @@ -15,16 +15,6 @@ namespace Azure { namespace Storage { namespace Test { return std::vector(start, start + strlen(text)); } - TEST(CryptFunctionsTest, Sha256) - { - EXPECT_EQ( - Azure::Core::Convert::Base64Encode(_internal::Sha256(ToBinaryVector(""))), - "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="); - EXPECT_EQ( - Azure::Core::Convert::Base64Encode(_internal::Sha256(ToBinaryVector("Hello Azure!"))), - "Mjzwx2mqGHb9FSgjm33ShNmXYndkgvwA6tQmEiskOHg="); - } - TEST(CryptFunctionsTest, HmacSha256) { std::string key = "8CwtGFF1mGR4bPEP9eZ0x1fxKiQ3Ca5N";