diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2019-09-25 18:31:05 +0000 |
---|---|---|
committer | evergreen <evergreen@mongodb.com> | 2019-09-25 18:31:05 +0000 |
commit | c9b8e58587f9ca79bf3b00b8f79bfad691e5768f (patch) | |
tree | e3a4cc75433823b6a0fc5499c2cf822281fb942d | |
parent | 39daf64f53e984c92d0af615593975a723797f97 (diff) | |
download | mongo-c9b8e58587f9ca79bf3b00b8f79bfad691e5768f.tar.gz |
SERVER-43610 Change HashTraits signature to allow storing into a specifed buffer
-rw-r--r-- | src/mongo/crypto/hash_block.h | 4 | ||||
-rw-r--r-- | src/mongo/crypto/sha1_block.h | 2 | ||||
-rw-r--r-- | src/mongo/crypto/sha256_block.h | 2 | ||||
-rw-r--r-- | src/mongo/crypto/sha512_block.h | 6 | ||||
-rw-r--r-- | src/mongo/crypto/sha_block_apple.cpp | 24 | ||||
-rw-r--r-- | src/mongo/crypto/sha_block_openssl.cpp | 27 | ||||
-rw-r--r-- | src/mongo/crypto/sha_block_tom.cpp | 29 | ||||
-rw-r--r-- | src/mongo/crypto/sha_block_windows.cpp | 34 |
8 files changed, 58 insertions, 70 deletions
diff --git a/src/mongo/crypto/hash_block.h b/src/mongo/crypto/hash_block.h index 4d408e9d5e7..01726fdb6f4 100644 --- a/src/mongo/crypto/hash_block.h +++ b/src/mongo/crypto/hash_block.h @@ -81,7 +81,9 @@ public: * Computes a hash of 'input' from multiple contigous buffers. */ static HashBlock computeHash(std::initializer_list<ConstDataRange> input) { - return HashBlock{Traits::computeHash(input)}; + HashBlock ret; + Traits::computeHash(input, &(ret._hash)); + return ret; } /** diff --git a/src/mongo/crypto/sha1_block.h b/src/mongo/crypto/sha1_block.h index d38e8e81f1b..bf94ad112bd 100644 --- a/src/mongo/crypto/sha1_block.h +++ b/src/mongo/crypto/sha1_block.h @@ -43,7 +43,7 @@ struct SHA1BlockTraits { static constexpr StringData name = "SHA1Block"_sd; - static HashType computeHash(std::initializer_list<ConstDataRange> input); + static void computeHash(std::initializer_list<ConstDataRange> input, HashType* const output); static void computeHmac(const uint8_t* key, size_t keyLen, diff --git a/src/mongo/crypto/sha256_block.h b/src/mongo/crypto/sha256_block.h index e16ef5f2126..82f3f17f3df 100644 --- a/src/mongo/crypto/sha256_block.h +++ b/src/mongo/crypto/sha256_block.h @@ -43,7 +43,7 @@ struct SHA256BlockTraits { static constexpr StringData name = "SHA256Block"_sd; - static HashType computeHash(std::initializer_list<ConstDataRange> input); + static void computeHash(std::initializer_list<ConstDataRange> input, HashType* const output); static void computeHmac(const uint8_t* key, size_t keyLen, diff --git a/src/mongo/crypto/sha512_block.h b/src/mongo/crypto/sha512_block.h index 2630c8b83d3..75f2b6dd1bf 100644 --- a/src/mongo/crypto/sha512_block.h +++ b/src/mongo/crypto/sha512_block.h @@ -45,14 +45,12 @@ struct SHA512BlockTraits { static HashType computeHash(std::initializer_list<ConstDataRange> input); + static void computeHash(std::initializer_list<ConstDataRange> input, HashType* const output); + static void computeHmac(const uint8_t* key, size_t keyLen, std::initializer_list<ConstDataRange> input, HashType* const output); - - static HashType computeHmac(const uint8_t* key, - size_t keyLen, - std::initializer_list<ConstDataRange> input); }; using SHA512Block = HashBlock<SHA512BlockTraits>; diff --git a/src/mongo/crypto/sha_block_apple.cpp b/src/mongo/crypto/sha_block_apple.cpp index daa63eb2af9..b8be116c716 100644 --- a/src/mongo/crypto/sha_block_apple.cpp +++ b/src/mongo/crypto/sha_block_apple.cpp @@ -39,46 +39,40 @@ namespace mongo { using CDRinit = std::initializer_list<ConstDataRange>; -SHA1BlockTraits::HashType SHA1BlockTraits::computeHash(CDRinit input) { +void SHA1BlockTraits::computeHash(CDRinit input, HashType* const output) { CC_SHA1_CTX ctx; CC_SHA1_Init(&ctx); for (const auto& range : input) { CC_SHA1_Update(&ctx, range.data(), range.length()); } - SHA1BlockTraits::HashType ret; - static_assert(sizeof(ret) == CC_SHA1_DIGEST_LENGTH, + static_assert(sizeof(*output) == CC_SHA1_DIGEST_LENGTH, "SHA1 HashType size doesn't match expected digest output size"); - CC_SHA1_Final(ret.data(), &ctx); - return ret; + CC_SHA1_Final(output->data(), &ctx); } -SHA256BlockTraits::HashType SHA256BlockTraits::computeHash(CDRinit input) { +void SHA256BlockTraits::computeHash(CDRinit input, HashType* const output) { CC_SHA256_CTX ctx; CC_SHA256_Init(&ctx); for (const auto& range : input) { CC_SHA256_Update(&ctx, range.data(), range.length()); } - SHA256BlockTraits::HashType ret; - static_assert(sizeof(ret) == CC_SHA256_DIGEST_LENGTH, + static_assert(sizeof(*output) == CC_SHA256_DIGEST_LENGTH, "SHA256 HashType size doesn't match expected digest output size"); - CC_SHA256_Final(ret.data(), &ctx); - return ret; + CC_SHA256_Final(output->data(), &ctx); } -SHA512BlockTraits::HashType SHA512BlockTraits::computeHash(CDRinit input) { +void SHA512BlockTraits::computeHash(CDRinit input, HashType* const output) { CC_SHA512_CTX ctx; CC_SHA512_Init(&ctx); for (const auto& range : input) { CC_SHA512_Update(&ctx, range.data(), range.length()); } - SHA512BlockTraits::HashType ret; - static_assert(sizeof(ret) == CC_SHA512_DIGEST_LENGTH, + static_assert(sizeof(*output) == CC_SHA512_DIGEST_LENGTH, "SHA512 HashType size doesn't match expected digest output size"); - CC_SHA512_Final(ret.data(), &ctx); - return ret; + CC_SHA512_Final(output->data(), &ctx); } void SHA1BlockTraits::computeHmac(const uint8_t* key, diff --git a/src/mongo/crypto/sha_block_openssl.cpp b/src/mongo/crypto/sha_block_openssl.cpp index be2e9a67db9..80a752fbafa 100644 --- a/src/mongo/crypto/sha_block_openssl.cpp +++ b/src/mongo/crypto/sha_block_openssl.cpp @@ -91,9 +91,9 @@ namespace { * Computes a SHA hash of 'input'. */ template <typename HashType> -HashType computeHashImpl(const EVP_MD* md, std::initializer_list<ConstDataRange> input) { - HashType output; - +void computeHashImpl(const EVP_MD* md, + std::initializer_list<ConstDataRange> input, + HashType* const output) { std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)> digestCtx(EVP_MD_CTX_new(), EVP_MD_CTX_free); @@ -104,8 +104,7 @@ HashType computeHashImpl(const EVP_MD* md, std::initializer_list<ConstDataRange> [&](const auto& i) { return EVP_DigestUpdate(digestCtx.get(), i.data(), i.length()) == 1; }) && - EVP_DigestFinal_ex(digestCtx.get(), output.data(), nullptr) == 1); - return output; + EVP_DigestFinal_ex(digestCtx.get(), output->data(), nullptr) == 1); } template <typename HashType> @@ -130,19 +129,19 @@ void computeHmacImpl(const EVP_MD* md, } // namespace -SHA1BlockTraits::HashType SHA1BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA1BlockTraits::HashType>(EVP_sha1(), input); +void SHA1BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA1BlockTraits::HashType>(EVP_sha1(), input, output); } -SHA256BlockTraits::HashType SHA256BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA256BlockTraits::HashType>(EVP_sha256(), input); +void SHA256BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA256BlockTraits::HashType>(EVP_sha256(), input, output); } -SHA512BlockTraits::HashType SHA512BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA512BlockTraits::HashType>(EVP_sha512(), input); +void SHA512BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA512BlockTraits::HashType>(EVP_sha512(), input, output); } void SHA1BlockTraits::computeHmac(const uint8_t* key, diff --git a/src/mongo/crypto/sha_block_tom.cpp b/src/mongo/crypto/sha_block_tom.cpp index 8f477ad8fad..3fab520ca55 100644 --- a/src/mongo/crypto/sha_block_tom.cpp +++ b/src/mongo/crypto/sha_block_tom.cpp @@ -53,9 +53,7 @@ template <typename HashType, int (*Init)(hash_state*), int (*Process)(hash_state*, const unsigned char*, unsigned long), int (*Done)(hash_state*, unsigned char*)> -HashType computeHashImpl(std::initializer_list<ConstDataRange> input) { - HashType output; - +void computeHashImpl(std::initializer_list<ConstDataRange> input, HashType* const output) { hash_state hashState; fassert(40381, Init(&hashState) == CRYPT_OK && @@ -66,8 +64,7 @@ HashType computeHashImpl(std::initializer_list<ConstDataRange> input) { reinterpret_cast<const unsigned char*>(i.data()), i.length()) == CRYPT_OK; }) && - Done(&hashState, output.data()) == CRYPT_OK); - return output; + Done(&hashState, output->data()) == CRYPT_OK); } /* @@ -110,21 +107,21 @@ void computeHmacImpl(const ltc_hash_descriptor* desc, } // namespace -SHA1BlockTraits::HashType SHA1BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA1BlockTraits::HashType, sha1_init, sha1_process, sha1_done>(input); +void SHA1BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA1BlockTraits::HashType, sha1_init, sha1_process, sha1_done>(input, output); } -SHA256BlockTraits::HashType SHA256BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA256BlockTraits::HashType, sha256_init, sha256_process, sha256_done>( - input); +void SHA256BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA256BlockTraits::HashType, sha256_init, sha256_process, sha256_done>(input, + output); } -SHA512BlockTraits::HashType SHA512BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA512BlockTraits::HashType, sha512_init, sha512_process, sha512_done>( - input); +void SHA512BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA512BlockTraits::HashType, sha512_init, sha512_process, sha512_done>(input, + output); } void SHA1BlockTraits::computeHmac(const uint8_t* key, diff --git a/src/mongo/crypto/sha_block_windows.cpp b/src/mongo/crypto/sha_block_windows.cpp index ac20a66b563..0bd35a03ce1 100644 --- a/src/mongo/crypto/sha_block_windows.cpp +++ b/src/mongo/crypto/sha_block_windows.cpp @@ -116,9 +116,9 @@ static BCryptHashLoader& getBCryptHashLoader() { * Computes a SHA hash of 'input'. */ template <typename HashType> -HashType computeHashImpl(BCRYPT_ALG_HANDLE algo, std::initializer_list<ConstDataRange> input) { - HashType output; - +void computeHashImpl(BCRYPT_ALG_HANDLE algo, + std::initializer_list<ConstDataRange> input, + HashType* const output) { BCRYPT_HASH_HANDLE hHash; fassert(50725, @@ -134,11 +134,9 @@ HashType computeHashImpl(BCRYPT_ALG_HANDLE algo, std::initializer_list<ConstData 0) == STATUS_SUCCESS; }) && - BCryptFinishHash(hHash, output.data(), output.size(), 0) == STATUS_SUCCESS && + BCryptFinishHash(hHash, output->data(), output->size(), 0) == STATUS_SUCCESS && BCryptDestroyHash(hHash) == STATUS_SUCCESS); - - return output; } /** @@ -175,22 +173,22 @@ void computeHmacImpl(BCRYPT_ALG_HANDLE algo, } // namespace -SHA1BlockTraits::HashType SHA1BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA1BlockTraits::HashType>(getBCryptHashLoader().getAlgoSHA1(), - std::move(input)); +void SHA1BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA1BlockTraits::HashType>( + getBCryptHashLoader().getAlgoSHA1(), std::move(input), output); } -SHA256BlockTraits::HashType SHA256BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA256BlockTraits::HashType>(getBCryptHashLoader().getAlgoSHA256(), - std::move(input)); +void SHA256BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA256BlockTraits::HashType>( + getBCryptHashLoader().getAlgoSHA256(), std::move(input), output); } -SHA512BlockTraits::HashType SHA512BlockTraits::computeHash( - std::initializer_list<ConstDataRange> input) { - return computeHashImpl<SHA512BlockTraits::HashType>(getBCryptHashLoader().getAlgoSHA512(), - std::move(input)); +void SHA512BlockTraits::computeHash(std::initializer_list<ConstDataRange> input, + HashType* const output) { + computeHashImpl<SHA512BlockTraits::HashType>( + getBCryptHashLoader().getAlgoSHA512(), std::move(input), output); } void SHA1BlockTraits::computeHmac(const uint8_t* key, |