diff --git a/CMakeLists.txt b/CMakeLists.txt index 309fa3c..105bda2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,6 +85,7 @@ if (USE_OPENSSL) add_compile_options("-DOPENSSL") else() message(STATUS "Could not find OpenSSL. Using custom cryptographic algorithm implementation.") + set(USE_OPENSSL OFF) endif() endif() @@ -102,6 +103,7 @@ add_library(OpenPGP_static STATIC $ $ $ + $ $ $ $ @@ -119,6 +121,7 @@ add_library(OpenPGP_shared SHARED $ $ $ + $ $ $ $ diff --git a/include/Hashes/HashAlg.h b/include/Hashes/Alg.h similarity index 74% rename from include/Hashes/HashAlg.h rename to include/Hashes/Alg.h index 8e06430..e4f5a1e 100644 --- a/include/Hashes/HashAlg.h +++ b/include/Hashes/Alg.h @@ -1,5 +1,5 @@ /* -Hash.h +Alg.h Base class for inheritance Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com @@ -28,14 +28,18 @@ THE SOFTWARE. #include "common/includes.h" -class HashAlg{ - public: - HashAlg(); - virtual ~HashAlg(); - virtual std::string hexdigest() = 0; - std::string digest(); - virtual void update(const std::string & str) = 0; - virtual std::size_t digestsize() const = 0; // digest size in bits -}; +namespace OpenPGP { + namespace Hash { + class Alg{ + public: + Alg(); + virtual ~Alg(); + virtual std::string hexdigest() = 0; + std::string digest(); + virtual void update(const std::string & str) = 0; + virtual std::size_t digestsize() const = 0; // digest size in bits + }; + } +} #endif diff --git a/include/Hashes/CMakeLists.txt b/include/Hashes/CMakeLists.txt index b7eedd6..9d966eb 100644 --- a/include/Hashes/CMakeLists.txt +++ b/include/Hashes/CMakeLists.txt @@ -1,21 +1,28 @@ cmake_minimum_required(VERSION 3.6.0) install(FILES - HashAlg.h + Alg.h Hashes.h - MD5_Const.h - MD5.h MerkleDamgard.h - RIPEMD160_Const.h - RIPEMD160.h - RIPEMD_Const.h - SHA1.h - SHA224.h - SHA256_Const.h - SHA256.h - SHA2_Functions.h - SHA384.h - SHA512_Const.h - SHA512.h - DESTINATION include/Hashes) + +if (NOT USE_OPENSSL) + install(FILES + MD5_Const.h + MD5.h + RIPEMD160_Const.h + RIPEMD160.h + RIPEMD_Const.h + SHA1.h + SHA224.h + SHA256_Const.h + SHA256.h + SHA2_Functions.h + SHA384.h + SHA512_Const.h + SHA512.h + + DESTINATION include/Hashes) +else() + add_subdirectory(OpenSSL) +endif() \ No newline at end of file diff --git a/include/Hashes/Hashes.h b/include/Hashes/Hashes.h index 26c980d..98d588f 100644 --- a/include/Hashes/Hashes.h +++ b/include/Hashes/Hashes.h @@ -28,20 +28,30 @@ THE SOFTWARE. #include #include -#include -#include "HashAlg.h" - -#include "MD5.h" -#include "RIPEMD160.h" -#include "SHA1.h" -#include "SHA256.h" -#include "SHA224.h" -#include "SHA512.h" -#include "SHA384.h" +#include "Hashes/Alg.h" + +#ifdef OPENSSL +#include "Hashes/OpenSSL/MD5.h" +#include "Hashes/OpenSSL/RIPEMD160.h" +#include "Hashes/OpenSSL/SHA1.h" +#include "Hashes/OpenSSL/SHA256.h" +#include "Hashes/OpenSSL/SHA224.h" +#include "Hashes/OpenSSL/SHA512.h" +#include "Hashes/OpenSSL/SHA384.h" +#else +#include "Hashes/MD5.h" +#include "Hashes/RIPEMD160.h" +#include "Hashes/SHA1.h" +#include "Hashes/SHA256.h" +#include "Hashes/SHA224.h" +#include "Hashes/SHA512.h" +#include "Hashes/SHA384.h" +#endif namespace OpenPGP { namespace Hash { + // 9.4. Hash Algorithms // // ID Algorithm Text Name @@ -130,7 +140,7 @@ namespace OpenPGP { std::string use(const uint8_t alg, const std::string & data = ""); - typedef std::shared_ptr Instance; + typedef std::shared_ptr Instance; Instance get_instance(const uint8_t alg, const std::string & data = ""); } } diff --git a/include/Hashes/MD5.h b/include/Hashes/MD5.h index 5deabd2..b01e9d5 100644 --- a/include/Hashes/MD5.h +++ b/include/Hashes/MD5.h @@ -32,32 +32,36 @@ THE SOFTWARE. #include "MD5_Const.h" -class MD5 : public MerkleDamgard { - private: - struct context{ - uint32_t h0, h1, h2, h3; - context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3) : - h0(h0), - h1(h1), - h2(h2), - h3(h3) - {} - ~context(){ - h0 = h1 = h2 = h3 = 0; - } +namespace OpenPGP { + namespace Hash { + class MD5 : public MerkleDamgard { + private: + struct context{ + uint32_t h0, h1, h2, h3; + context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3) : + h0(h0), + h1(h1), + h2(h2), + h3(h3) + {} + ~context(){ + h0 = h1 = h2 = h3 = 0; + } + }; + context ctx; + + std::string to_little_end(const std::string & data) const; + void calc(const std::string & data, context & state) const; + + public: + MD5(); + MD5(const std::string & data); + void update(const std::string & data); + std::string hexdigest(); + std::size_t blocksize() const; + std::size_t digestsize() const; }; - context ctx; - - std::string to_little_end(const std::string & data) const; - void calc(const std::string & data, context & state) const; - - public: - MD5(); - MD5(const std::string & data); - void update(const std::string & data); - std::string hexdigest(); - std::size_t blocksize() const; - std::size_t digestsize() const; -}; + } +} #endif diff --git a/include/Hashes/MD5_Const.h b/include/Hashes/MD5_Const.h index 99d17de..1012eb7 100644 --- a/include/Hashes/MD5_Const.h +++ b/include/Hashes/MD5_Const.h @@ -26,18 +26,22 @@ THE SOFTWARE. #ifndef __MD5_CONST__ #define __MD5_CONST__ -const uint8_t MD5_R[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, - 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, - 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, - 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; - -const uint32_t MD5_K[64] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, - 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, - 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, - 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, - 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, - 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, - 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, - 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; +namespace OpenPGP { + namespace Hash { + const uint8_t MD5_R[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, + 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, + 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, + 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; + + const uint32_t MD5_K[64] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, + 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, + 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, + 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, + 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, + 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, + 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; + } +} #endif diff --git a/include/Hashes/MerkleDamgard.h b/include/Hashes/MerkleDamgard.h index 40869dc..e01ed07 100644 --- a/include/Hashes/MerkleDamgard.h +++ b/include/Hashes/MerkleDamgard.h @@ -26,17 +26,21 @@ THE SOFTWARE. #ifndef __MERKLE_DAMGARD__ #define __MERKLE_DAMGARD__ -#include "HashAlg.h" - -class MerkleDamgard : public HashAlg { - protected: - std::string stack; - uint64_t clen; - - public: - MerkleDamgard(); - virtual ~MerkleDamgard(); - virtual std::size_t blocksize() const = 0; // blocksize in bits -}; +#include "Hashes/Alg.h" + +namespace OpenPGP { + namespace Hash { + class MerkleDamgard : public Alg { + protected: + std::string stack; + uint64_t clen; + + public: + MerkleDamgard(); + virtual ~MerkleDamgard(); + virtual std::size_t blocksize() const = 0; // blocksize in bits + }; + } +} #endif diff --git a/include/Hashes/OpenSSL/CMakeLists.txt b/include/Hashes/OpenSSL/CMakeLists.txt new file mode 100644 index 0000000..385e5c4 --- /dev/null +++ b/include/Hashes/OpenSSL/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.6.0) + +if (USE_OPENSSL) + install(FILES + MD5.h + RIPEMD160.h + SHA1.h + SHA224.h + SHA256.h + SHA384.h + SHA512.h + + DESTINATION include/Hashes) +endif() \ No newline at end of file diff --git a/include/Hashes/OpenSSL/MD5.h b/include/Hashes/OpenSSL/MD5.h new file mode 100644 index 0000000..fe472e2 --- /dev/null +++ b/include/Hashes/OpenSSL/MD5.h @@ -0,0 +1,50 @@ +/* +MD5.h +MD5 hashing algorithm + +Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef __OPENSSL_MD5__ +#define __OPENSSL_MD5__ + +#include + +#include "Hashes/MerkleDamgard.h" + +namespace OpenPGP { + namespace Hash { + class MD5 : public MerkleDamgard { + private: + MD5_CTX ctx; + + public: + MD5(); + MD5(const std::string & data); + void update(const std::string & data); + std::string hexdigest(); + std::size_t blocksize() const; + std::size_t digestsize() const; + }; + } +} + +#endif diff --git a/include/Hashes/OpenSSL/RIPEMD160.h b/include/Hashes/OpenSSL/RIPEMD160.h new file mode 100644 index 0000000..c72f5ee --- /dev/null +++ b/include/Hashes/OpenSSL/RIPEMD160.h @@ -0,0 +1,51 @@ +/* +RIPEMD160.h +RIPEMD160 hash function +http://homes.esat.kuleuven.be/~bosselae/ripemd160.html + +Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef __OPENSSL_RIPEMD160__ +#define __OPENSSL_RIPEMD160__ + +#include + +#include "Hashes/MerkleDamgard.h" + +namespace OpenPGP { + namespace Hash { + class RIPEMD160 : public MerkleDamgard { + private: + RIPEMD160_CTX ctx; + + public: + RIPEMD160(); + RIPEMD160(const std::string & data); + void update(const std::string & data); + std::string hexdigest(); + std::size_t blocksize() const; + std::size_t digestsize() const; + }; + } +} + +#endif diff --git a/include/Hashes/OpenSSL/SHA1.h b/include/Hashes/OpenSSL/SHA1.h new file mode 100644 index 0000000..8f6dbb0 --- /dev/null +++ b/include/Hashes/OpenSSL/SHA1.h @@ -0,0 +1,50 @@ +/* +SHA1.h +Secure Hash Algorithm 1 + +Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef __OPENSSL_SHA1__ +#define __OPENSSL_SHA1__ + +#include + +#include "Hashes/MerkleDamgard.h" + +namespace OpenPGP { + namespace Hash { + class SHA1 : public MerkleDamgard { + protected: + SHA_CTX ctx; + + public: + SHA1(); + SHA1(const std::string & data); + void update(const std::string & str); + virtual std::string hexdigest(); + virtual std::size_t blocksize() const; + virtual std::size_t digestsize() const; + }; + } +} + +#endif diff --git a/include/Hashes/OpenSSL/SHA224.h b/include/Hashes/OpenSSL/SHA224.h new file mode 100644 index 0000000..fa7c3a4 --- /dev/null +++ b/include/Hashes/OpenSSL/SHA224.h @@ -0,0 +1,50 @@ +/* +SHA224.h +The SHA2 algorithm SHA-224 + +Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef __OPENSSL_SHA224__ +#define __OPENSSL_SHA224__ + +#include + +#include "Hashes/MerkleDamgard.h" + +namespace OpenPGP { + namespace Hash { + class SHA224 : public MerkleDamgard { + protected: + SHA256_CTX ctx; + + public: + SHA224(); + SHA224(const std::string & data); + void update(const std::string & str); + virtual std::string hexdigest(); + virtual std::size_t blocksize() const; + virtual std::size_t digestsize() const; + }; + } +} + +#endif diff --git a/include/Hashes/OpenSSL/SHA256.h b/include/Hashes/OpenSSL/SHA256.h new file mode 100644 index 0000000..4e2814d --- /dev/null +++ b/include/Hashes/OpenSSL/SHA256.h @@ -0,0 +1,50 @@ +/* +SHA256.h +The SHA2 algorithm SHA-256 + +Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef __OPENSSL_SHA256__ +#define __OPENSSL_SHA256__ + +#include + +#include "Hashes/MerkleDamgard.h" + +namespace OpenPGP { + namespace Hash { + class SHA256 : public MerkleDamgard { + protected: + SHA256_CTX ctx; + + public: + SHA256(); + SHA256(const std::string & data); + void update(const std::string & str); + virtual std::string hexdigest(); + virtual std::size_t blocksize() const; + virtual std::size_t digestsize() const; + }; + } +} + +#endif diff --git a/include/Hashes/OpenSSL/SHA384.h b/include/Hashes/OpenSSL/SHA384.h new file mode 100644 index 0000000..a539cb5 --- /dev/null +++ b/include/Hashes/OpenSSL/SHA384.h @@ -0,0 +1,50 @@ +/* +SHA384.h +The SHA2 algorithm SHA-384 + +Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef __OPENSSL_SHA384__ +#define __OPENSSL_SHA384__ + +#include + +#include "Hashes/MerkleDamgard.h" + +namespace OpenPGP { + namespace Hash { + class SHA384 : public MerkleDamgard { + protected: + SHA512_CTX ctx; + + public: + SHA384(); + SHA384(const std::string & data); + void update(const std::string & str); + virtual std::string hexdigest(); + virtual std::size_t blocksize() const; + virtual std::size_t digestsize() const; + }; + } +} + +#endif diff --git a/include/Hashes/OpenSSL/SHA512.h b/include/Hashes/OpenSSL/SHA512.h new file mode 100644 index 0000000..6de5df0 --- /dev/null +++ b/include/Hashes/OpenSSL/SHA512.h @@ -0,0 +1,50 @@ +/* +SHA512.h +The SHA2 algorithm SHA-512 + +Copyright (c) 2013 - 2019 Jason Lee @ calccrypto at gmail.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef __OPENSSL_SHA512__ +#define __OPENSSL_SHA512__ + +#include + +#include "Hashes/MerkleDamgard.h" + +namespace OpenPGP { + namespace Hash { + class SHA512 : public MerkleDamgard { + protected: + SHA512_CTX ctx; + + public: + SHA512(); + SHA512(const std::string & data); + void update(const std::string & str); + virtual std::string hexdigest(); + virtual std::size_t blocksize() const; + virtual std::size_t digestsize() const; + }; + } +} + +#endif diff --git a/include/Hashes/RIPEMD160.h b/include/Hashes/RIPEMD160.h index adf4a0b..c405190 100644 --- a/include/Hashes/RIPEMD160.h +++ b/include/Hashes/RIPEMD160.h @@ -34,35 +34,39 @@ THE SOFTWARE. #include "RIPEMD_Const.h" #include "RIPEMD160_Const.h" -class RIPEMD160 : public MerkleDamgard { - private: - struct context{ - uint32_t h0, h1, h2, h3, h4; - context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3, uint32_t h4) : - h0(h0), - h1(h1), - h2(h2), - h3(h3), - h4(h4) - {} - ~context(){ - h0 = h1 = h2 = h3 = h4 = 0; - } - }; - context ctx; +namespace OpenPGP { + namespace Hash { + class RIPEMD160 : public MerkleDamgard { + private: + struct context{ + uint32_t h0, h1, h2, h3, h4; + context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3, uint32_t h4) : + h0(h0), + h1(h1), + h2(h2), + h3(h3), + h4(h4) + {} + ~context(){ + h0 = h1 = h2 = h3 = h4 = 0; + } + }; + context ctx; - uint32_t F(const uint32_t & x, const uint32_t & y, const uint32_t & z, const uint8_t round) const; + uint32_t F(const uint32_t & x, const uint32_t & y, const uint32_t & z, const uint8_t round) const; - std::string to_little_end(const std::string & data) const; - void calc(const std::string & data, context & state) const; + std::string to_little_end(const std::string & data) const; + void calc(const std::string & data, context & state) const; - public: - RIPEMD160(); - RIPEMD160(const std::string & data); - void update(const std::string & data); - std::string hexdigest(); - std::size_t blocksize() const; - std::size_t digestsize() const; -}; + public: + RIPEMD160(); + RIPEMD160(const std::string & data); + void update(const std::string & data); + std::string hexdigest(); + std::size_t blocksize() const; + std::size_t digestsize() const; + }; + } +} #endif diff --git a/include/Hashes/RIPEMD160_Const.h b/include/Hashes/RIPEMD160_Const.h index 40541b4..ba6dec3 100644 --- a/include/Hashes/RIPEMD160_Const.h +++ b/include/Hashes/RIPEMD160_Const.h @@ -26,7 +26,11 @@ THE SOFTWARE. #ifndef __RIPEMD160_CONST__ #define __RIPEMD160_CONST__ -const uint32_t RIPEMD160_k[5] = {0, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E}; -const uint32_t RIPEMD160_K[5] = {0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0}; +namespace OpenPGP { + namespace Hash { + const uint32_t RIPEMD160_k[5] = {0, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E}; + const uint32_t RIPEMD160_K[5] = {0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0}; + } +} #endif diff --git a/include/Hashes/RIPEMD_Const.h b/include/Hashes/RIPEMD_Const.h index 589bd85..f5f0236 100644 --- a/include/Hashes/RIPEMD_Const.h +++ b/include/Hashes/RIPEMD_Const.h @@ -26,34 +26,38 @@ THE SOFTWARE. #ifndef __RIPEMD_CONST__ #define __RIPEMD_CONST__ -const uint32_t RIPEMD_H0 = 0x67452301; -const uint32_t RIPEMD_H1 = 0xEFCDAB89; -const uint32_t RIPEMD_H2 = 0x98BADCFE; -const uint32_t RIPEMD_H3 = 0x10325476; -const uint32_t RIPEMD_H4 = 0xC3D2E1F0; - -const uint8_t RIPEMD_s[80] = {11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, - 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, - 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, - 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, - 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6}; - -const uint8_t RIPEMD_S[80] = {8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, - 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, - 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, - 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, - 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11}; - -const uint8_t RIPEMD_r[80] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, - 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, - 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, - 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, - 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13}; - -const uint8_t RIPEMD_R[80] = {5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, - 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, - 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, - 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, - 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11}; +namespace OpenPGP { + namespace Hash { + const uint32_t RIPEMD_H0 = 0x67452301; + const uint32_t RIPEMD_H1 = 0xEFCDAB89; + const uint32_t RIPEMD_H2 = 0x98BADCFE; + const uint32_t RIPEMD_H3 = 0x10325476; + const uint32_t RIPEMD_H4 = 0xC3D2E1F0; + + const uint8_t RIPEMD_s[80] = {11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, + 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, + 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, + 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, + 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6}; + + const uint8_t RIPEMD_S[80] = {8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, + 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, + 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, + 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, + 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11}; + + const uint8_t RIPEMD_r[80] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, + 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, + 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, + 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, + 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13}; + + const uint8_t RIPEMD_R[80] = {5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, + 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, + 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, + 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, + 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11}; + } +} #endif diff --git a/include/Hashes/SHA1.h b/include/Hashes/SHA1.h index 6933b35..307516a 100644 --- a/include/Hashes/SHA1.h +++ b/include/Hashes/SHA1.h @@ -30,34 +30,38 @@ THE SOFTWARE. #include "common/includes.h" #include "MerkleDamgard.h" -class SHA1 : public MerkleDamgard { - private: - struct context{ - uint32_t h0, h1, h2, h3, h4; - - context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3, uint32_t h4) : - h0(h0), - h1(h1), - h2(h2), - h3(h3), - h4(h4) - {} - ~context(){ - h0 = h1 = h2 = h3 = h4 = 0; - } - }; +namespace OpenPGP { + namespace Hash { + class SHA1 : public MerkleDamgard { + private: + struct context{ + uint32_t h0, h1, h2, h3, h4; + + context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3, uint32_t h4) : + h0(h0), + h1(h1), + h2(h2), + h3(h3), + h4(h4) + {} + ~context(){ + h0 = h1 = h2 = h3 = h4 = 0; + } + }; - context ctx; + context ctx; - void calc(const std::string & data, context & state) const; + void calc(const std::string & data, context & state) const; - public: - SHA1(); - SHA1(const std::string & str); - void update(const std::string & str); - std::string hexdigest(); - std::size_t blocksize() const; - std::size_t digestsize() const; -}; + public: + SHA1(); + SHA1(const std::string & str); + void update(const std::string & str); + std::string hexdigest(); + std::size_t blocksize() const; + std::size_t digestsize() const; + }; + } +} #endif diff --git a/include/Hashes/SHA224.h b/include/Hashes/SHA224.h index e8ded60..c26441c 100644 --- a/include/Hashes/SHA224.h +++ b/include/Hashes/SHA224.h @@ -28,16 +28,20 @@ THE SOFTWARE. #include "SHA256.h" -class SHA224 : public SHA256 { - private: - void original_h(); - - public: - SHA224(); - SHA224(const std::string & data); - std::string hexdigest(); - std::size_t blocksize() const; - std::size_t digestsize() const; -}; +namespace OpenPGP { + namespace Hash { + class SHA224 : public SHA256 { + private: + void original_h(); + + public: + SHA224(); + SHA224(const std::string & data); + std::string hexdigest(); + std::size_t blocksize() const; + std::size_t digestsize() const; + }; + } +} #endif diff --git a/include/Hashes/SHA256.h b/include/Hashes/SHA256.h index cbcc51e..17c00d0 100644 --- a/include/Hashes/SHA256.h +++ b/include/Hashes/SHA256.h @@ -33,34 +33,38 @@ THE SOFTWARE. #include "SHA2_Functions.h" #include "SHA256_Const.h" -class SHA256 : public MerkleDamgard { - protected: - struct context{ - uint32_t h0, h1, h2, h3, h4, h5, h6, h7; +namespace OpenPGP { + namespace Hash { + class SHA256 : public MerkleDamgard { + protected: + struct context{ + uint32_t h0, h1, h2, h3, h4, h5, h6, h7; - ~context(){ - h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = 0; - } - }; - context ctx; + ~context(){ + h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = 0; + } + }; + context ctx; - uint32_t S0(const uint32_t & value) const; - uint32_t S1(const uint32_t & value) const; - uint32_t s0(const uint32_t & value) const; - uint32_t s1(const uint32_t & value) const; + uint32_t S0(const uint32_t & value) const; + uint32_t S1(const uint32_t & value) const; + uint32_t s0(const uint32_t & value) const; + uint32_t s1(const uint32_t & value) const; - virtual void original_h(); + virtual void original_h(); - void calc(const std::string & data, context & state) const; + void calc(const std::string & data, context & state) const; - public: - SHA256(); - SHA256(const std::string & data); + public: + SHA256(); + SHA256(const std::string & data); - void update(const std::string &str); - virtual std::string hexdigest(); - virtual std::size_t blocksize() const; - virtual std::size_t digestsize() const; -}; + void update(const std::string &str); + virtual std::string hexdigest(); + virtual std::size_t blocksize() const; + virtual std::size_t digestsize() const; + }; + } +} #endif diff --git a/include/Hashes/SHA256_Const.h b/include/Hashes/SHA256_Const.h index 270fc6f..9e57ea3 100644 --- a/include/Hashes/SHA256_Const.h +++ b/include/Hashes/SHA256_Const.h @@ -26,13 +26,17 @@ THE SOFTWARE. #ifndef __SHA256_CONST__ #define __SHA256_CONST__ -const uint32_t SHA256_K[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; +namespace OpenPGP { + namespace Hash { + const uint32_t SHA256_K[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + } +} #endif diff --git a/include/Hashes/SHA2_Functions.h b/include/Hashes/SHA2_Functions.h index bc033e0..222dedb 100644 --- a/include/Hashes/SHA2_Functions.h +++ b/include/Hashes/SHA2_Functions.h @@ -28,7 +28,11 @@ THE SOFTWARE. #include -uint64_t Ch(const uint64_t & m, const uint64_t & n, const uint64_t & o); -uint64_t Maj(const uint64_t & m, const uint64_t & n, const uint64_t & o); +namespace OpenPGP { + namespace Hash { + uint64_t Ch(const uint64_t & m, const uint64_t & n, const uint64_t & o); + uint64_t Maj(const uint64_t & m, const uint64_t & n, const uint64_t & o); + } +} #endif diff --git a/include/Hashes/SHA384.h b/include/Hashes/SHA384.h index 9033a8c..a398c4f 100644 --- a/include/Hashes/SHA384.h +++ b/include/Hashes/SHA384.h @@ -28,16 +28,20 @@ THE SOFTWARE. #include "SHA512.h" -class SHA384 : public SHA512 { - private: - void original_h(); - - public: - SHA384(); - SHA384(const std::string & data); - std::string hexdigest(); - std::size_t blocksize() const; - std::size_t digestsize() const; -}; +namespace OpenPGP { + namespace Hash { + class SHA384 : public SHA512 { + private: + void original_h(); + + public: + SHA384(); + SHA384(const std::string & data); + std::string hexdigest(); + std::size_t blocksize() const; + std::size_t digestsize() const; + }; + } +} #endif diff --git a/include/Hashes/SHA512.h b/include/Hashes/SHA512.h index 4728ced..9f867bc 100644 --- a/include/Hashes/SHA512.h +++ b/include/Hashes/SHA512.h @@ -33,32 +33,36 @@ THE SOFTWARE. #include "SHA2_Functions.h" #include "SHA512_Const.h" -class SHA512 : public MerkleDamgard { - protected: - struct context{ - uint64_t h0, h1, h2, h3, h4, h5, h6, h7; - ~context(){ - h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = 0; - } - }; - context ctx; +namespace OpenPGP { + namespace Hash { + class SHA512 : public MerkleDamgard { + protected: + struct context{ + uint64_t h0, h1, h2, h3, h4, h5, h6, h7; + ~context(){ + h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = 0; + } + }; + context ctx; - uint64_t S0(uint64_t & value) const; - uint64_t S1(uint64_t & value) const; - uint64_t s0(uint64_t & value) const; - uint64_t s1(uint64_t & value) const; + uint64_t S0(uint64_t & value) const; + uint64_t S1(uint64_t & value) const; + uint64_t s0(uint64_t & value) const; + uint64_t s1(uint64_t & value) const; - virtual void original_h(); + virtual void original_h(); - void calc(const std::string & data, context & state) const; + void calc(const std::string & data, context & state) const; - public: - SHA512(); - SHA512(const std::string & data); - void update(const std::string & str); - virtual std::string hexdigest(); - virtual std::size_t blocksize() const; - virtual std::size_t digestsize() const; -}; + public: + SHA512(); + SHA512(const std::string & data); + void update(const std::string & str); + virtual std::string hexdigest(); + virtual std::size_t blocksize() const; + virtual std::size_t digestsize() const; + }; + } +} #endif diff --git a/include/Hashes/SHA512_Const.h b/include/Hashes/SHA512_Const.h index 5b07541..218c0a6 100644 --- a/include/Hashes/SHA512_Const.h +++ b/include/Hashes/SHA512_Const.h @@ -26,25 +26,28 @@ THE SOFTWARE. #ifndef __SHA512_CONST__ #define __SHA512_CONST__ -const uint64_t SHA512_K[80] = { 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, - 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, - 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, - 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, - 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, - 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, - 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, - 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, - 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, - 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, - 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, - 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, - 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, - 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, - 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, - 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, - 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, - 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, - 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, - 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; - +namespace OpenPGP { + namespace Hash { + const uint64_t SHA512_K[80] = { 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, + 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, + 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, + 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, + 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, + 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, + 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, + 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, + 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, + 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, + 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, + 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, + 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, + 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, + 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, + 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, + 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, + 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, + 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, + 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; + } +} #endif diff --git a/src/Hashes/Alg.cpp b/src/Hashes/Alg.cpp new file mode 100644 index 0000000..7b18312 --- /dev/null +++ b/src/Hashes/Alg.cpp @@ -0,0 +1,15 @@ +#include "Hashes/Alg.h" + +namespace OpenPGP { +namespace Hash { + +Alg::Alg() {} + +Alg::~Alg() {} + +std::string Alg::digest() { + return unhexlify(hexdigest()); +} + +} +} \ No newline at end of file diff --git a/src/Hashes/CMakeLists.txt b/src/Hashes/CMakeLists.txt index 7e0cdbd..e6b73aa 100644 --- a/src/Hashes/CMakeLists.txt +++ b/src/Hashes/CMakeLists.txt @@ -2,15 +2,24 @@ cmake_minimum_required(VERSION 3.6.0) add_library(Hashes OBJECT Hashes.cpp - HashAlg.cpp + Alg.cpp MerkleDamgard.cpp - MD5.cpp - RIPEMD160.cpp - SHA1.cpp - SHA224.cpp - SHA256.cpp - SHA2_Functions.cpp - SHA384.cpp - SHA512.cpp) +) set_property(TARGET Hashes PROPERTY POSITION_INDEPENDENT_CODE ON) + +if (NOT USE_OPENSSL) + add_library(HashAlgs OBJECT + MD5.cpp + RIPEMD160.cpp + SHA1.cpp + SHA224.cpp + SHA256.cpp + SHA2_Functions.cpp + SHA384.cpp + SHA512.cpp) + + set_property(TARGET HashAlgs PROPERTY POSITION_INDEPENDENT_CODE ON) +else() + add_subdirectory(OpenSSL) +endif() \ No newline at end of file diff --git a/src/Hashes/HashAlg.cpp b/src/Hashes/HashAlg.cpp deleted file mode 100644 index b860ebc..0000000 --- a/src/Hashes/HashAlg.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "Hashes/HashAlg.h" - -HashAlg::HashAlg() {} - -HashAlg::~HashAlg() {} - -std::string HashAlg::digest() { - return unhexlify(hexdigest()); -} diff --git a/src/Hashes/Hashes.cpp b/src/Hashes/Hashes.cpp index a708749..9d2f244 100644 --- a/src/Hashes/Hashes.cpp +++ b/src/Hashes/Hashes.cpp @@ -10,8 +10,6 @@ std::string use(const uint8_t alg, const std::string & data) { Instance get_instance(const uint8_t alg, const std::string & data) { Instance ptr = nullptr; switch (alg) { - // case 0: // don't hash; not defined in standard - // return data; case ID::MD5: ptr = std::make_shared (data); break; diff --git a/src/Hashes/MD5.cpp b/src/Hashes/MD5.cpp index fd20741..65f9299 100644 --- a/src/Hashes/MD5.cpp +++ b/src/Hashes/MD5.cpp @@ -1,5 +1,8 @@ #include "Hashes/MD5.h" +namespace OpenPGP { +namespace Hash { + std::string MD5::to_little_end(const std::string & data) const { std::string result; for(unsigned int x = 0; x < (data.size() >> 2); x++) { @@ -85,4 +88,7 @@ std::size_t MD5::blocksize() const { std::size_t MD5::digestsize() const { return 128; +} + +} } \ No newline at end of file diff --git a/src/Hashes/MerkleDamgard.cpp b/src/Hashes/MerkleDamgard.cpp index 7d42f29..fb4c1f8 100644 --- a/src/Hashes/MerkleDamgard.cpp +++ b/src/Hashes/MerkleDamgard.cpp @@ -1,9 +1,15 @@ #include "Hashes/MerkleDamgard.h" +namespace OpenPGP { +namespace Hash { + MerkleDamgard::MerkleDamgard() - : HashAlg(), + : Alg(), stack(), clen(0) {} -MerkleDamgard::~MerkleDamgard() {} \ No newline at end of file +MerkleDamgard::~MerkleDamgard() {} + +} +} \ No newline at end of file diff --git a/src/Hashes/OpenSSL/CMakeLists.txt b/src/Hashes/OpenSSL/CMakeLists.txt new file mode 100644 index 0000000..addc38e --- /dev/null +++ b/src/Hashes/OpenSSL/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 3.6.0) + +if (USE_OPENSSL) + add_library(HashAlgs OBJECT + MD5.cpp + RIPEMD160.cpp + SHA1.cpp + SHA224.cpp + SHA256.cpp + SHA384.cpp + SHA512.cpp) + + set_property(TARGET HashAlgs PROPERTY POSITION_INDEPENDENT_CODE ON) +endif() \ No newline at end of file diff --git a/src/Hashes/OpenSSL/MD5.cpp b/src/Hashes/OpenSSL/MD5.cpp new file mode 100644 index 0000000..5a6a66f --- /dev/null +++ b/src/Hashes/OpenSSL/MD5.cpp @@ -0,0 +1,38 @@ +#include "Hashes/OpenSSL/MD5.h" + +namespace OpenPGP { +namespace Hash { + +MD5::MD5() : + MerkleDamgard(), + ctx() +{ + MD5_Init(&ctx); +} + +MD5::MD5(const std::string & str) : + MD5() +{ + update(str); +} + +void MD5::update(const std::string & str) { + MD5_Update(&ctx, str.c_str(), str.size()); +} + +std::string MD5::hexdigest() { + unsigned char buf[MD5_DIGEST_LENGTH]; + MD5_Final(buf, &ctx); + return hexlify(std::string((char *) buf, MD5_DIGEST_LENGTH)); +} + +std::size_t MD5::blocksize() const { + return 512; +} + +std::size_t MD5::digestsize() const { + return 128; +} + +} +} \ No newline at end of file diff --git a/src/Hashes/OpenSSL/RIPEMD160.cpp b/src/Hashes/OpenSSL/RIPEMD160.cpp new file mode 100644 index 0000000..95faeb4 --- /dev/null +++ b/src/Hashes/OpenSSL/RIPEMD160.cpp @@ -0,0 +1,38 @@ +#include "Hashes/OpenSSL/RIPEMD160.h" + +namespace OpenPGP { +namespace Hash { + +RIPEMD160::RIPEMD160() : + MerkleDamgard(), + ctx() +{ + RIPEMD160_Init(&ctx); +} + +RIPEMD160::RIPEMD160(const std::string & str) : + RIPEMD160() +{ + update(str); +} + +void RIPEMD160::update(const std::string & str) { + RIPEMD160_Update(&ctx, str.c_str(), str.size()); +} + +std::string RIPEMD160::hexdigest() { + unsigned char buf[RIPEMD160_DIGEST_LENGTH]; + RIPEMD160_Final(buf, &ctx); + return hexlify(std::string((char *) buf, RIPEMD160_DIGEST_LENGTH)); +} + +std::size_t RIPEMD160::blocksize() const { + return 512; +} + +std::size_t RIPEMD160::digestsize() const { + return 160; +} + +} +} \ No newline at end of file diff --git a/src/Hashes/OpenSSL/SHA1.cpp b/src/Hashes/OpenSSL/SHA1.cpp new file mode 100644 index 0000000..5f362cc --- /dev/null +++ b/src/Hashes/OpenSSL/SHA1.cpp @@ -0,0 +1,38 @@ +#include "Hashes/OpenSSL/SHA1.h" + +namespace OpenPGP { +namespace Hash { + +SHA1::SHA1() : + MerkleDamgard(), + ctx() +{ + SHA1_Init(&ctx); +} + +SHA1::SHA1(const std::string & str) : + SHA1() +{ + update(str); +} + +void SHA1::update(const std::string & str) { + SHA1_Update(&ctx, str.c_str(), str.size()); +} + +std::string SHA1::hexdigest() { + unsigned char buf[SHA_DIGEST_LENGTH]; + SHA1_Final(buf, &ctx); + return hexlify(std::string((char *) buf, SHA_DIGEST_LENGTH)); +} + +std::size_t SHA1::blocksize() const { + return 512; +} + +std::size_t SHA1::digestsize() const { + return 160; +} + +} +} \ No newline at end of file diff --git a/src/Hashes/OpenSSL/SHA224.cpp b/src/Hashes/OpenSSL/SHA224.cpp new file mode 100644 index 0000000..29ec4f2 --- /dev/null +++ b/src/Hashes/OpenSSL/SHA224.cpp @@ -0,0 +1,38 @@ +#include "Hashes/OpenSSL/SHA224.h" + +namespace OpenPGP { +namespace Hash { + +SHA224::SHA224() : + MerkleDamgard(), + ctx() +{ + SHA224_Init(&ctx); +} + +SHA224::SHA224(const std::string & str) : + SHA224() +{ + update(str); +} + +void SHA224::update(const std::string & str) { + SHA224_Update(&ctx, str.c_str(), str.size()); +} + +std::string SHA224::hexdigest() { + unsigned char buf[SHA224_DIGEST_LENGTH]; + SHA224_Final(buf, &ctx); + return hexlify(std::string((char *) buf, SHA224_DIGEST_LENGTH)); +} + +std::size_t SHA224::blocksize() const { + return 512; +} + +std::size_t SHA224::digestsize() const { + return 224; +} + +} +} \ No newline at end of file diff --git a/src/Hashes/OpenSSL/SHA256.cpp b/src/Hashes/OpenSSL/SHA256.cpp new file mode 100644 index 0000000..cc6d66d --- /dev/null +++ b/src/Hashes/OpenSSL/SHA256.cpp @@ -0,0 +1,38 @@ +#include "Hashes/OpenSSL/SHA256.h" + +namespace OpenPGP { +namespace Hash { + +SHA256::SHA256() : + MerkleDamgard(), + ctx() +{ + SHA256_Init(&ctx); +} + +SHA256::SHA256(const std::string & str) : + SHA256() +{ + update(str); +} + +void SHA256::update(const std::string & str) { + SHA256_Update(&ctx, str.c_str(), str.size()); +} + +std::string SHA256::hexdigest() { + unsigned char buf[SHA256_DIGEST_LENGTH]; + SHA256_Final(buf, &ctx); + return hexlify(std::string((char *) buf, SHA256_DIGEST_LENGTH)); +} + +std::size_t SHA256::blocksize() const { + return 256; +} + +std::size_t SHA256::digestsize() const { + return 256; +} + +} +} \ No newline at end of file diff --git a/src/Hashes/OpenSSL/SHA384.cpp b/src/Hashes/OpenSSL/SHA384.cpp new file mode 100644 index 0000000..5c140da --- /dev/null +++ b/src/Hashes/OpenSSL/SHA384.cpp @@ -0,0 +1,38 @@ +#include "Hashes/OpenSSL/SHA384.h" + +namespace OpenPGP { +namespace Hash { + +SHA384::SHA384() : + MerkleDamgard(), + ctx() +{ + SHA384_Init(&ctx); +} + +SHA384::SHA384(const std::string & str) : + SHA384() +{ + update(str); +} + +void SHA384::update(const std::string & str) { + SHA384_Update(&ctx, str.c_str(), str.size()); +} + +std::string SHA384::hexdigest() { + unsigned char buf[SHA384_DIGEST_LENGTH]; + SHA384_Final(buf, &ctx); + return hexlify(std::string((char *) buf, SHA384_DIGEST_LENGTH)); +} + +std::size_t SHA384::blocksize() const { + return 1024; +} + +std::size_t SHA384::digestsize() const { + return 384; +} + +} +} \ No newline at end of file diff --git a/src/Hashes/OpenSSL/SHA512.cpp b/src/Hashes/OpenSSL/SHA512.cpp new file mode 100644 index 0000000..fde9b85 --- /dev/null +++ b/src/Hashes/OpenSSL/SHA512.cpp @@ -0,0 +1,38 @@ +#include "Hashes/OpenSSL/SHA512.h" + +namespace OpenPGP { +namespace Hash { + +SHA512::SHA512() : + MerkleDamgard(), + ctx() +{ + SHA512_Init(&ctx); +} + +SHA512::SHA512(const std::string & str) : + SHA512() +{ + update(str); +} + +void SHA512::update(const std::string & str) { + SHA512_Update(&ctx, str.c_str(), str.size()); +} + +std::string SHA512::hexdigest() { + unsigned char buf[SHA512_DIGEST_LENGTH]; + SHA512_Final(buf, &ctx); + return hexlify(std::string((char *) buf, SHA512_DIGEST_LENGTH)); +} + +std::size_t SHA512::blocksize() const { + return 1024; +} + +std::size_t SHA512::digestsize() const { + return 512; +} + +} +} \ No newline at end of file diff --git a/src/Hashes/RIPEMD160.cpp b/src/Hashes/RIPEMD160.cpp index f6138de..25427b1 100644 --- a/src/Hashes/RIPEMD160.cpp +++ b/src/Hashes/RIPEMD160.cpp @@ -1,5 +1,8 @@ #include "Hashes/RIPEMD160.h" +namespace OpenPGP { +namespace Hash { + uint32_t RIPEMD160::F(const uint32_t & x, const uint32_t & y, const uint32_t & z, const uint8_t round) const { if (round < 16) { return x ^ y ^ z; @@ -89,4 +92,7 @@ std::size_t RIPEMD160::blocksize() const { std::size_t RIPEMD160::digestsize() const { return 160; +} + +} } \ No newline at end of file diff --git a/src/Hashes/SHA1.cpp b/src/Hashes/SHA1.cpp index daeeee0..23ac9c4 100644 --- a/src/Hashes/SHA1.cpp +++ b/src/Hashes/SHA1.cpp @@ -1,5 +1,8 @@ #include "Hashes/SHA1.h" +namespace OpenPGP { +namespace Hash { + void SHA1::calc(const std::string & data, context & state) const { for(unsigned int n = 0; n < (data.size() >> 6); n++) { std::string temp = data.substr(n << 6, 64); @@ -80,4 +83,7 @@ std::size_t SHA1::blocksize() const { std::size_t SHA1::digestsize() const { return 160; +} + +} } \ No newline at end of file diff --git a/src/Hashes/SHA224.cpp b/src/Hashes/SHA224.cpp index 8648dc9..bf0f5ed 100644 --- a/src/Hashes/SHA224.cpp +++ b/src/Hashes/SHA224.cpp @@ -1,5 +1,8 @@ #include "Hashes/SHA224.h" +namespace OpenPGP { +namespace Hash { + void SHA224::original_h() { ctx.h0 = 0xc1059ed8; ctx.h1 = 0x367cd507; @@ -33,4 +36,7 @@ std::size_t SHA224::blocksize() const { std::size_t SHA224::digestsize() const { return 224; +} + +} } \ No newline at end of file diff --git a/src/Hashes/SHA256.cpp b/src/Hashes/SHA256.cpp index b7ae81d..5c02f09 100644 --- a/src/Hashes/SHA256.cpp +++ b/src/Hashes/SHA256.cpp @@ -1,5 +1,8 @@ #include "Hashes/SHA256.h" +namespace OpenPGP { +namespace Hash { + uint32_t SHA256::S0(const uint32_t & value) const { return ROR(value, 2, 32) ^ ROR(value, 13, 32) ^ ROR(value, 22, 32); } @@ -92,4 +95,7 @@ std::size_t SHA256::blocksize() const { std::size_t SHA256::digestsize() const { return 256; +} + +} } \ No newline at end of file diff --git a/src/Hashes/SHA2_Functions.cpp b/src/Hashes/SHA2_Functions.cpp index 25279f6..2f699e5 100644 --- a/src/Hashes/SHA2_Functions.cpp +++ b/src/Hashes/SHA2_Functions.cpp @@ -1,5 +1,8 @@ #include "Hashes/SHA2_Functions.h" +namespace OpenPGP { +namespace Hash { + uint64_t Ch(const uint64_t & m, const uint64_t & n, const uint64_t & o) { return (m & n) ^ (~m & o); } @@ -8,3 +11,5 @@ uint64_t Maj(const uint64_t & m, const uint64_t & n, const uint64_t & o) { return (m & n) ^ (m & o) ^ (n & o); } +} +} \ No newline at end of file diff --git a/src/Hashes/SHA384.cpp b/src/Hashes/SHA384.cpp index 8d5b145..58140a8 100644 --- a/src/Hashes/SHA384.cpp +++ b/src/Hashes/SHA384.cpp @@ -1,5 +1,8 @@ #include "Hashes/SHA384.h" +namespace OpenPGP { +namespace Hash { + void SHA384::original_h() { ctx.h0 = 0xcbbb9d5dc1059ed8ULL; ctx.h1 = 0x629a292a367cd507ULL; @@ -33,4 +36,7 @@ std::size_t SHA384::blocksize() const { std::size_t SHA384::digestsize() const { return 384; +} + +} } \ No newline at end of file diff --git a/src/Hashes/SHA512.cpp b/src/Hashes/SHA512.cpp index 3205f69..272b31e 100644 --- a/src/Hashes/SHA512.cpp +++ b/src/Hashes/SHA512.cpp @@ -1,5 +1,8 @@ #include "Hashes/SHA512.h" +namespace OpenPGP { +namespace Hash { + uint64_t SHA512::S0(uint64_t & value) const { return ROR(value, 28, 64) ^ ROR(value, 34, 64) ^ ROR(value, 39, 64); } @@ -92,4 +95,7 @@ std::size_t SHA512::blocksize() const { std::size_t SHA512::digestsize() const { return 512; +} + +} } \ No newline at end of file diff --git a/src/Packets/Key.cpp b/src/Packets/Key.cpp index 73d9ffa..0f912e7 100644 --- a/src/Packets/Key.cpp +++ b/src/Packets/Key.cpp @@ -232,11 +232,11 @@ std::string Key::get_fingerprint() const { std::string m = write_MPI(i); data += m.substr(2, m.size() - 2); } - return MD5(data).digest(); + return Hash::MD5(data).digest(); } else if (version == 4) { const std::string packet = raw_common(); - return SHA1("\x99" + unhexlify(makehex(packet.size(), 4)) + packet).digest(); + return Hash::SHA1("\x99" + unhexlify(makehex(packet.size(), 4)) + packet).digest(); } else{ throw std::runtime_error("Error: Key packet version " + std::to_string(version) + " not defined."); diff --git a/src/Packets/Tag5.cpp b/src/Packets/Tag5.cpp index 610a706..a28d9f5 100644 --- a/src/Packets/Tag5.cpp +++ b/src/Packets/Tag5.cpp @@ -329,7 +329,7 @@ std::string Tag5::calculate_key(const std::string & passphrase) const { key = s2k -> run(passphrase, Sym::KEY_LENGTH.at(sym) >> 3); } else{ - key = MD5(passphrase).digest(); // simple MD5 for all other values + key = Hash::MD5(passphrase).digest(); // simple MD5 for all other values } return key; diff --git a/tests/Hashes/md5.cpp b/tests/Hashes/md5.cpp index 3a62d7d..8c0ffc9 100644 --- a/tests/Hashes/md5.cpp +++ b/tests/Hashes/md5.cpp @@ -1,6 +1,6 @@ #include -#include "Hashes/MD5.h" +#include "Hashes/Hashes.h" #include "testvectors/md5/md5testvectors.h" @@ -9,7 +9,7 @@ TEST(MD5, vectors) { ASSERT_EQ(MD5_BYTES.size(), MD5_HASHES.size()); for ( unsigned int i = 0; i < MD5_BYTES.size(); ++i ) { - auto md5 = MD5(unhexlify(MD5_BYTES[i])); + auto md5 = OpenPGP::Hash::MD5(unhexlify(MD5_BYTES[i])); EXPECT_EQ(md5.hexdigest(), MD5_HASHES[i]); } } diff --git a/tests/Hashes/ripemd160.cpp b/tests/Hashes/ripemd160.cpp index e09a952..94e9944 100644 --- a/tests/Hashes/ripemd160.cpp +++ b/tests/Hashes/ripemd160.cpp @@ -1,6 +1,6 @@ #include -#include "Hashes/RIPEMD160.h" +#include "Hashes/Hashes.h" #include "testvectors/ripemd/ripemd160testvectors.h" @@ -9,7 +9,7 @@ TEST(RIPEMD, testvectors) { ASSERT_EQ(RIPEMD160_MSG.size(), RIPEMD160_MSG_HEXDIGEST.size()); for ( unsigned int i = 0; i < RIPEMD160_MSG.size(); ++i ) { - auto ripemd160 = RIPEMD160(unhexlify(RIPEMD160_MSG[i])); + auto ripemd160 = OpenPGP::Hash::RIPEMD160(unhexlify(RIPEMD160_MSG[i])); EXPECT_EQ(ripemd160.hexdigest(), RIPEMD160_MSG_HEXDIGEST[i]); } } diff --git a/tests/Hashes/sha1.cpp b/tests/Hashes/sha1.cpp index 93a71fb..ecda9d2 100644 --- a/tests/Hashes/sha1.cpp +++ b/tests/Hashes/sha1.cpp @@ -1,6 +1,6 @@ #include -#include "Hashes/SHA1.h" +#include "Hashes/Hashes.h" #include "testvectors/sha/sha1shortmsg.h" @@ -9,7 +9,7 @@ TEST(SHA1, short_msg) { ASSERT_EQ(SHA1_SHORT_MSG.size(), SHA1_SHORT_MSG_HEXDIGEST.size()); for ( unsigned int i = 0; i < SHA1_SHORT_MSG.size(); ++i ) { - auto sha1 = SHA1(unhexlify(SHA1_SHORT_MSG[i])); + auto sha1 = OpenPGP::Hash::SHA1(unhexlify(SHA1_SHORT_MSG[i])); EXPECT_EQ(sha1.hexdigest(), SHA1_SHORT_MSG_HEXDIGEST[i]); } } diff --git a/tests/Hashes/sha224.cpp b/tests/Hashes/sha224.cpp index c57eeb3..5c69d0c 100644 --- a/tests/Hashes/sha224.cpp +++ b/tests/Hashes/sha224.cpp @@ -1,6 +1,6 @@ #include -#include "Hashes/SHA224.h" +#include "Hashes/Hashes.h" #include "testvectors/sha/sha224shortmsg.h" @@ -9,7 +9,7 @@ TEST(SHA224, short_msg) { ASSERT_EQ(SHA224_SHORT_MSG.size(), SHA224_SHORT_MSG_HEXDIGEST.size()); for ( unsigned int i = 0; i < SHA224_SHORT_MSG.size(); ++i ) { - auto sha224 = SHA224(unhexlify(SHA224_SHORT_MSG[i])); + auto sha224 = OpenPGP::Hash::SHA224(unhexlify(SHA224_SHORT_MSG[i])); EXPECT_EQ(sha224.hexdigest(), SHA224_SHORT_MSG_HEXDIGEST[i]); } } diff --git a/tests/Hashes/sha256.cpp b/tests/Hashes/sha256.cpp index bd3145d..deacc74 100644 --- a/tests/Hashes/sha256.cpp +++ b/tests/Hashes/sha256.cpp @@ -1,6 +1,6 @@ #include -#include "Hashes/SHA256.h" +#include "Hashes/Hashes.h" #include "testvectors/sha/sha256shortmsg.h" @@ -9,7 +9,7 @@ TEST(SHA256, short_msg) { ASSERT_EQ(SHA256_SHORT_MSG.size(), SHA256_SHORT_MSG_HEXDIGEST.size()); for ( unsigned int i = 0; i < SHA256_SHORT_MSG.size(); ++i ) { - auto sha256 = SHA256(unhexlify(SHA256_SHORT_MSG[i])); + auto sha256 = OpenPGP::Hash::SHA256(unhexlify(SHA256_SHORT_MSG[i])); EXPECT_EQ(sha256.hexdigest(), SHA256_SHORT_MSG_HEXDIGEST[i]); } } diff --git a/tests/Hashes/sha384.cpp b/tests/Hashes/sha384.cpp index cddea03..364e07b 100644 --- a/tests/Hashes/sha384.cpp +++ b/tests/Hashes/sha384.cpp @@ -1,6 +1,6 @@ #include -#include "Hashes/SHA384.h" +#include "Hashes/Hashes.h" #include "testvectors/sha/sha384shortmsg.h" @@ -9,7 +9,7 @@ TEST(SHA384, short_msg) { ASSERT_EQ(SHA384_SHORT_MSG.size(), SHA384_SHORT_MSG_HEXDIGEST.size()); for ( unsigned int i = 0; i < SHA384_SHORT_MSG.size(); ++i ) { - auto sha384 = SHA384(unhexlify(SHA384_SHORT_MSG[i])); + auto sha384 = OpenPGP::Hash::SHA384(unhexlify(SHA384_SHORT_MSG[i])); EXPECT_EQ(sha384.hexdigest(), SHA384_SHORT_MSG_HEXDIGEST[i]); } } diff --git a/tests/Hashes/sha512.cpp b/tests/Hashes/sha512.cpp index 0d45703..60a1c75 100644 --- a/tests/Hashes/sha512.cpp +++ b/tests/Hashes/sha512.cpp @@ -1,6 +1,6 @@ #include -#include "Hashes/SHA512.h" +#include "Hashes/Hashes.h" #include "testvectors/sha/sha512shortmsg.h" @@ -9,7 +9,7 @@ TEST(SHA512, short_msg) { ASSERT_EQ(SHA512_SHORT_MSG.size(), SHA512_SHORT_MSG_HEXDIGEST.size()); for ( unsigned int i = 0; i < SHA512_SHORT_MSG.size(); ++i ) { - auto sha512 = SHA512(unhexlify(SHA512_SHORT_MSG[i])); + auto sha512 = OpenPGP::Hash::SHA512(unhexlify(SHA512_SHORT_MSG[i])); EXPECT_EQ(sha512.hexdigest(), SHA512_SHORT_MSG_HEXDIGEST[i]); } } diff --git a/tests/PKA/dsa.cpp b/tests/PKA/dsa.cpp index 4a879bf..57b3089 100644 --- a/tests/PKA/dsa.cpp +++ b/tests/PKA/dsa.cpp @@ -22,7 +22,7 @@ TEST(DSA, dsa_siggen) { auto q = OpenPGP::hextompi(DSA_SIGGEN_Q); auto g = OpenPGP::hextompi(DSA_SIGGEN_G); for ( unsigned int i = 0; i < DSA_SIGGEN_MSG.size(); ++i ) { - auto digest = SHA1(unhexlify(DSA_SIGGEN_MSG[i])).digest(); + auto digest = OpenPGP::Hash::use(OpenPGP::Hash::ID::SHA1, unhexlify(DSA_SIGGEN_MSG[i])); auto x = OpenPGP::hextompi(DSA_SIGGEN_X[i]); auto y = OpenPGP::hextompi(DSA_SIGGEN_Y[i]); auto k = OpenPGP::hextompi(DSA_SIGGEN_K[i]); @@ -47,7 +47,7 @@ TEST(DSA, keygen) { std::make_pair(3072, 256), }; - static const std::string digest = SHA1(unhexlify(DSA_SIGGEN_MSG[0])).digest(); + static const std::string digest = OpenPGP::Hash::use(OpenPGP::Hash::ID::SHA1, unhexlify(DSA_SIGGEN_MSG[0])); for(std::pair const & ln : LN) { OpenPGP::PKA::Values pub = OpenPGP::PKA::DSA::new_public(ln.first, ln.second);