From fc5614eff23507e823fe1513ce392cd84d4aeee0 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 11 Feb 2020 15:42:41 +0100 Subject: [PATCH] adding crypto/hash.h and move openssl specific code from checksum.cpp to crypto/openssl/hash.cpp --- CMakeLists.txt | 5 ++++- src/checksum.cpp | 23 +++++++++++----------- src/crypto/hash.h | 38 +++++++++++++++++++++++++++++++++++++ src/crypto/openssl/hash.cpp | 38 +++++++++++++++++++++++++++++++++++++ src/crypto/types.h | 5 +++++ 5 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 src/crypto/hash.h create mode 100644 src/crypto/openssl/hash.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fa88c06..2396237 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -45,7 +45,10 @@ endif() # Libraries find_package(OpenSSL 1.1 REQUIRED) -set (PROGRAM_SOURCE ${PROGRAM_SOURCE} src/crypto/openssl/ec.cpp) +set (PROGRAM_SOURCE ${PROGRAM_SOURCE} + src/crypto/openssl/ec.cpp + src/crypto/openssl/hash.cpp +) if (USE_THREADS) find_package(Threads) diff --git a/src/checksum.cpp b/src/checksum.cpp index aed8deb..9f46b0e 100644 --- a/src/checksum.cpp +++ b/src/checksum.cpp @@ -21,32 +21,31 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include -#include #include +#include "crypto/hash.h" #include "checksum.h" namespace eoskeygen { -inline void sha256d(const unsigned char *data, std::size_t len, unsigned char *out) { - SHA256(data, len, out); - SHA256(out, 32, out); +inline void sha256d(const unsigned char *data, std::size_t len, sha256_t *out) { + sha256(data, len, out); + sha256(out->data, 32, out); } -#define checksum_impl(name, func) \ - checksum_t checksum_##name(const unsigned char *data, std::size_t len) { \ +#define checksum_impl(func, type) \ + checksum_t checksum_##func(const unsigned char *data, std::size_t len) { \ \ checksum_t crc; \ - unsigned char hash[32]; \ + type hash; \ \ - func(data, len, hash); \ + func(data, len, &hash); \ \ - std::memcpy(crc.data(), hash, crc.size()); \ + std::memcpy(crc.data(), &hash, crc.size()); \ return crc; \ } -checksum_impl(sha256d, sha256d) -checksum_impl(ripemd160, RIPEMD160) +checksum_impl(sha256d, sha256_t) +checksum_impl(ripemd160, ripemd160_t) } // namespace eosio-keygen diff --git a/src/crypto/hash.h b/src/crypto/hash.h new file mode 100644 index 0000000..10b0373 --- /dev/null +++ b/src/crypto/hash.h @@ -0,0 +1,38 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * 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 EOSIOKEYGEN_CRYPTO_HASH_H +#define EOSIOKEYGEN_CRYPTO_HASH_H + +#include +#include "types.h" + +namespace eoskeygen { + +sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out); + +ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out); + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_CRYPTO_HASH_H */ diff --git a/src/crypto/openssl/hash.cpp b/src/crypto/openssl/hash.cpp new file mode 100644 index 0000000..08cf360 --- /dev/null +++ b/src/crypto/openssl/hash.cpp @@ -0,0 +1,38 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * 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. + */ +#include +#include +#include "../hash.h" + +namespace eoskeygen { + +sha256_t* sha256(const unsigned char *data, std::size_t len, sha256_t* out) { + return (sha256_t *) SHA256(data, len, out->data); +} + +ripemd160_t* ripemd160(const unsigned char *data, std::size_t len, ripemd160_t* out) { + return (ripemd160_t *) RIPEMD160(data, len, out->data); +} + +} // namespace eoskeygen diff --git a/src/crypto/types.h b/src/crypto/types.h index f1a505d..23e2585 100644 --- a/src/crypto/types.h +++ b/src/crypto/types.h @@ -45,6 +45,11 @@ struct ec_keypair { ec_pubkey_t pub; }; +// Hashes. + +typedef struct { unsigned char data[20]; } ripemd160_t; +typedef struct { unsigned char data[32]; } sha256_t; + } // namespace eoskeygen #endif /* EOSIOKEYGEN_CRYPTO_TYPES_H */