From: Christian Hofstaedtler Date: Sun, 3 Jan 2016 00:39:35 +0000 (+0100) Subject: Port sha.hh to OpenSSL X-Git-Tag: dnsdist-1.0.0-alpha2~61^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dd13333700b9e4af02620a8acd7e13b98aa6e467;p=pdns Port sha.hh to OpenSSL --- diff --git a/pdns/mbedtlssigners.cc b/pdns/mbedtlssigners.cc index 757352fa1..328293517 100644 --- a/pdns/mbedtlssigners.cc +++ b/pdns/mbedtlssigners.cc @@ -7,17 +7,20 @@ #include #include #include +#include +#include #else #include #include #include #include #include +#include +#include #include "mbedtlscompat.hh" #endif #include // for 'operator+=()' -#include "sha.hh" #include "dnssecinfra.hh" using namespace boost::assign; diff --git a/pdns/pkcs11signers.cc b/pdns/pkcs11signers.cc index 2276781d1..5a98b7470 100644 --- a/pdns/pkcs11signers.cc +++ b/pdns/pkcs11signers.cc @@ -830,29 +830,19 @@ std::string PKCS11DNSCryptoKeyEngine::hash(const std::string& msg) const { // FINE! I'll do this myself, then, shall I? switch(d_algorithm) { case 5: { - SHA1Summer sha; - sha.feed(msg); - return sha.get(); + return pdns_sha1sum(msg); } case 8: { - SHA256Summer sha; - sha.feed(msg); - return sha.get(); + return pdns_sha256sum(msg); } case 10: { - SHA512Summer sha; - sha.feed(msg); - return sha.get(); + return pdns_sha512sum(msg); } case 13: { - SHA256Summer sha; - sha.feed(msg); - return sha.get(); + return pdns_sha256sum(msg); } case 14: { - SHA384Summer sha; - sha.feed(msg); - return sha.get(); + return pdns_sha384sum(msg); } }; }; diff --git a/pdns/sha.hh b/pdns/sha.hh index c9dfaeb60..971bf5cc6 100644 --- a/pdns/sha.hh +++ b/pdns/sha.hh @@ -7,87 +7,67 @@ #include #include #include -#else +#elif defined(HAVE_MBEDTLS) #include #include #include #include "mbedtlscompat.hh" +#elif defined(HAVE_OPENSSL) +#include +#else +#error "No SHA implementation found" #endif -class SHA1Summer +inline std::string pdns_sha1sum(const std::string& input) { -public: - SHA1Summer() { mbedtls_sha1_starts(&d_context); }; - void feed(const std::string &str) { feed(str.c_str(), str.length()); }; - void feed(const char *ptr, size_t len) { mbedtls_sha1_update(&d_context, reinterpret_cast(ptr), len); }; - const std::string get() const { - mbedtls_sha1_context ctx2; - unsigned char result[20] = {0}; - ctx2=d_context; - mbedtls_sha1_finish(&ctx2, result); - return std::string(result, result + sizeof result); - }; - SHA1Summer(const SHA1Summer&) = delete; - SHA1Summer& operator=(const SHA1Summer&) = delete; -private: - mbedtls_sha1_context d_context; -}; + unsigned char result[20] = {0}; +#ifdef HAVE_MBEDTLS + mbedtls_sha1(reinterpret_cast(input.c_str()), input.length(), result); +#elif defined(HAVE_OPENSSL) + SHA1(reinterpret_cast(input.c_str()), input.length(), result); +#else +#error "No sha1 implementation found" +#endif + return std::string(result, result + sizeof result); +} -class SHA256Summer +inline std::string pdns_sha256sum(const std::string& input) { -public: - SHA256Summer() { mbedtls_sha256_starts(&d_context, 0); }; - void feed(const std::string &str) { feed(str.c_str(), str.length()); }; - void feed(const char *ptr, size_t len) { mbedtls_sha256_update(&d_context, reinterpret_cast(ptr), len); }; - const std::string get() const { - mbedtls_sha256_context ctx2; - unsigned char result[32] = {0}; - ctx2=d_context; - mbedtls_sha256_finish(&ctx2, result); - return std::string(result, result + 32); - }; - SHA256Summer(const SHA256Summer&) = delete; - SHA256Summer& operator=(const SHA256Summer&) = delete; -private: - mbedtls_sha256_context d_context; -}; + unsigned char result[32] = {0}; +#ifdef HAVE_MBEDTLS + mbedtls_sha256(reinterpret_cast(input.c_str()), input.length(), result, 0); +#elif defined(HAVE_OPENSSL) + SHA256(reinterpret_cast(input.c_str()), input.length(), result); +#else +#error "No sha256 implementation found" +#endif + return std::string(result, result + sizeof result); +} -class SHA384Summer +inline std::string pdns_sha384sum(const std::string& input) { -public: - SHA384Summer() { mbedtls_sha512_starts(&d_context, 1); }; - void feed(const std::string &str) { feed(str.c_str(), str.length()); }; - void feed(const char *ptr, size_t len) { mbedtls_sha512_update(&d_context, reinterpret_cast(ptr), len); }; - const std::string get() const { - mbedtls_sha512_context ctx2; - unsigned char result[64] = {0}; - ctx2 = d_context; - mbedtls_sha512_finish(&ctx2, result); - return std::string(result, result + 48); - }; - SHA384Summer(const SHA384Summer&) = delete; - SHA384Summer& operator=(const SHA384Summer&) = delete; -private: - mbedtls_sha512_context d_context; -}; + unsigned char result[48] = {0}; +#ifdef HAVE_MBEDTLS + mbedtls_sha512(reinterpret_cast(input.c_str()), input.length(), result, 1); +#elif defined(HAVE_OPENSSL) + SHA384(reinterpret_cast(input.c_str()), input.length(), result); +#else +#error "No sha384 implementation found" +#endif + return std::string(result, result + sizeof result); +} -class SHA512Summer +inline std::string pdns_sha512sum(const std::string& input) { -public: - SHA512Summer() { mbedtls_sha512_starts(&d_context, 0); }; - void feed(const std::string &str) { feed(str.c_str(), str.length()); }; - void feed(const char *ptr, size_t len) { mbedtls_sha512_update(&d_context, reinterpret_cast(ptr), len); }; - const std::string get() const { - mbedtls_sha512_context ctx2; - unsigned char result[64] = {0}; - ctx2=d_context; - mbedtls_sha512_finish(&ctx2, result); - return std::string(result, result + sizeof result); - }; - SHA512Summer(const SHA512Summer&) = delete; - SHA512Summer& operator=(const SHA512Summer&) = delete; -private: - mbedtls_sha512_context d_context; -}; + unsigned char result[64] = {0}; +#ifdef HAVE_MBEDTLS + mbedtls_sha512(reinterpret_cast(input.c_str()), input.length(), result, 0); +#elif defined(HAVE_OPENSSL) + SHA512(reinterpret_cast(input.c_str()), input.length(), result); +#else +#error "No sha512 implementation found" +#endif + return std::string(result, result + sizeof result); +} #endif /* sha.hh */ diff --git a/pdns/test-sha_hh.cc b/pdns/test-sha_hh.cc index b2a16d9dc..a0753bc9a 100644 --- a/pdns/test-sha_hh.cc +++ b/pdns/test-sha_hh.cc @@ -20,51 +20,43 @@ BOOST_AUTO_TEST_SUITE(test_sha_hh) typedef boost::tuple case_t; typedef std::vector cases_t; -BOOST_AUTO_TEST_CASE(test_sha1summer) { +BOOST_AUTO_TEST_CASE(test_sha1) { cases_t cases = list_of (case_t("abc", "a9 99 3e 36 47 06 81 6a ba 3e 25 71 78 50 c2 6c 9c d0 d8 9d ")) (case_t("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84 98 3e 44 1c 3b d2 6e ba ae 4a a1 f9 51 29 e5 e5 46 70 f1 ")); for(case_t& val : cases) { - SHA1Summer s; - s.feed(val.get<0>()); - BOOST_CHECK_EQUAL(makeHexDump(s.get()), val.get<1>()); + BOOST_CHECK_EQUAL(makeHexDump(pdns_sha1sum(val.get<0>())), val.get<1>()); } } -BOOST_AUTO_TEST_CASE(test_sha256summer) { +BOOST_AUTO_TEST_CASE(test_sha256) { cases_t cases = list_of (case_t("abc", "ba 78 16 bf 8f 01 cf ea 41 41 40 de 5d ae 22 23 b0 03 61 a3 96 17 7a 9c b4 10 ff 61 f2 00 15 ad ")) (case_t("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "24 8d 6a 61 d2 06 38 b8 e5 c0 26 93 0c 3e 60 39 a3 3c e4 59 64 ff 21 67 f6 ec ed d4 19 db 06 c1 ")); for(case_t& val : cases) { - SHA256Summer s; - s.feed(val.get<0>()); - BOOST_CHECK_EQUAL(makeHexDump(s.get()), val.get<1>()); + BOOST_CHECK_EQUAL(makeHexDump(pdns_sha256sum(val.get<0>())), val.get<1>()); } } -BOOST_AUTO_TEST_CASE(test_sha384summer) { +BOOST_AUTO_TEST_CASE(test_sha384) { cases_t cases = list_of (case_t("abc", "cb 00 75 3f 45 a3 5e 8b b5 a0 3d 69 9a c6 50 07 27 2c 32 ab 0e de d1 63 1a 8b 60 5a 43 ff 5b ed 80 86 07 2b a1 e7 cc 23 58 ba ec a1 34 c8 25 a7 ")) (case_t("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "33 91 fd dd fc 8d c7 39 37 07 a6 5b 1b 47 09 39 7c f8 b1 d1 62 af 05 ab fe 8f 45 0d e5 f3 6b c6 b0 45 5a 85 20 bc 4e 6f 5f e9 5b 1f e3 c8 45 2b ")); for(case_t& val : cases) { - SHA384Summer s; - s.feed(val.get<0>()); - BOOST_CHECK_EQUAL(makeHexDump(s.get()), val.get<1>()); + BOOST_CHECK_EQUAL(makeHexDump(pdns_sha384sum(val.get<0>())), val.get<1>()); } } -BOOST_AUTO_TEST_CASE(test_sha512summer) { +BOOST_AUTO_TEST_CASE(test_sha512) { cases_t cases = list_of (case_t("abc", "dd af 35 a1 93 61 7a ba cc 41 73 49 ae 20 41 31 12 e6 fa 4e 89 a9 7e a2 0a 9e ee e6 4b 55 d3 9a 21 92 99 2a 27 4f c1 a8 36 ba 3c 23 a3 fe eb bd 45 4d 44 23 64 3c e8 0e 2a 9a c9 4f a5 4c a4 9f ")) (case_t("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "20 4a 8f c6 dd a8 2f 0a 0c ed 7b eb 8e 08 a4 16 57 c1 6e f4 68 b2 28 a8 27 9b e3 31 a7 03 c3 35 96 fd 15 c1 3b 1b 07 f9 aa 1d 3b ea 57 78 9c a0 31 ad 85 c7 a7 1d d7 03 54 ec 63 12 38 ca 34 45 ")); for(case_t& val : cases) { - SHA512Summer s; - s.feed(val.get<0>()); - BOOST_CHECK_EQUAL(makeHexDump(s.get()), val.get<1>()); + BOOST_CHECK_EQUAL(makeHexDump(pdns_sha512sum(val.get<0>())), val.get<1>()); } }