From: Jude Taylor Date: Wed, 12 Apr 2017 16:40:55 +0000 (-0700) Subject: Provide a copy ctor for Digest that copies the current hash context X-Git-Tag: v2017.04.17.00~32 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=68a3d5416c14c122b2c40f216960b26acbf16c19;p=folly.git Provide a copy ctor for Digest that copies the current hash context Summary: opensource changes for t16702532 Reviewed By: yfeldblum Differential Revision: D4846899 fbshipit-source-id: 67a610ff399e95c7cf1c9c8a5950f79bfc3aabb9 --- diff --git a/folly/ssl/OpenSSLHash.h b/folly/ssl/OpenSSLHash.h index baf9ec7f..481a4f3e 100644 --- a/folly/ssl/OpenSSLHash.h +++ b/folly/ssl/OpenSSLHash.h @@ -33,6 +33,20 @@ class OpenSSLHash { public: Digest() : ctx_(EVP_MD_CTX_new()) {} + Digest(const Digest& other) { + ctx_ = EvpMdCtxUniquePtr(EVP_MD_CTX_new()); + if (other.md_ != nullptr) { + hash_init(other.md_); + check_libssl_result( + 1, EVP_MD_CTX_copy_ex(ctx_.get(), other.ctx_.get())); + } + } + + Digest& operator=(const Digest& other) { + this->~Digest(); + return *new (this) Digest(other); + } + void hash_init(const EVP_MD* md) { md_ = md; check_libssl_result(1, EVP_DigestInit_ex(ctx_.get(), md, nullptr)); @@ -54,6 +68,7 @@ class OpenSSLHash { check_libssl_result(size, int(len)); md_ = nullptr; } + private: const EVP_MD* md_ = nullptr; EvpMdCtxUniquePtr ctx_{nullptr}; diff --git a/folly/ssl/test/OpenSSLHashTest.cpp b/folly/ssl/test/OpenSSLHashTest.cpp index c63bfd86..b9f6a7b6 100644 --- a/folly/ssl/test/OpenSSLHashTest.cpp +++ b/folly/ssl/test/OpenSSLHashTest.cpp @@ -45,6 +45,39 @@ TEST_F(OpenSSLHashTest, sha256) { EXPECT_EQ(expected, out); } +TEST_F(OpenSSLHashTest, sha256_hashcopy) { + std::array expected, actual; + + OpenSSLHash::Digest digest; + digest.hash_init(EVP_sha256()); + digest.hash_update(ByteRange(StringPiece("foobar"))); + + OpenSSLHash::Digest copy(digest); + + digest.hash_final(range(expected)); + copy.hash_final(range(actual)); + + EXPECT_EQ(expected, actual); +} + +TEST_F(OpenSSLHashTest, sha256_hashcopy_intermediate) { + std::array expected, actual; + + OpenSSLHash::Digest digest; + digest.hash_init(EVP_sha256()); + digest.hash_update(ByteRange(StringPiece("foo"))); + + OpenSSLHash::Digest copy(digest); + + digest.hash_update(ByteRange(StringPiece("bar"))); + copy.hash_update(ByteRange(StringPiece("bar"))); + + digest.hash_final(range(expected)); + copy.hash_final(range(actual)); + + EXPECT_EQ(expected, actual); +} + TEST_F(OpenSSLHashTest, hmac_sha256) { auto key = ByteRange(StringPiece("qwerty"));