From 0e9d23086f177b796f111d7e27d6b8816b45fdfb Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Feb 2020 16:58:08 +0100 Subject: [PATCH 1/5] Adding "eoskeygen" namespace. --- src/WIF.cpp | 4 ++++ src/WIF.h | 4 ++++ src/base58.cpp | 4 ++++ src/base58.h | 4 ++++ src/benchmark.cpp | 4 ++++ src/benchmark.h | 4 ++++ src/checksum.cpp | 4 ++++ src/checksum.h | 4 ++++ src/console.h | 4 ++++ src/console_ansi.cpp | 4 ++++ src/console_win32.cpp | 4 ++++ src/ec/generate.h | 4 ++++ src/ec/openssl.cpp | 4 ++++ src/ec/types.h | 4 ++++ src/key_search.cpp | 4 ++++ src/key_search.h | 4 ++++ src/key_search_helpers.cpp | 4 ++++ src/key_search_helpers.h | 4 ++++ src/key_search_mt.cpp | 4 ++++ src/main.cpp | 22 +++++++++++----------- src/string.cpp | 4 ++++ src/string.h | 4 ++++ 22 files changed, 95 insertions(+), 11 deletions(-) diff --git a/src/WIF.cpp b/src/WIF.cpp index 5577d8e..d31aff4 100644 --- a/src/WIF.cpp +++ b/src/WIF.cpp @@ -27,6 +27,8 @@ #include "checksum.h" #include "WIF.h" +namespace eoskeygen { + #define PRIV_KEY_PREFIX 0x80 /* 0x80 for "Bitcoin mainnet". Always used by EOS. */ std::string wif_priv_encode(ec_privkey_t priv) { @@ -60,3 +62,5 @@ void wif_print_key(const struct ec_keypair *key) { std::cout << "Public: " << wif_pub_encode(key->pub) << std::endl; std::cout << "Private: " << wif_priv_encode(key->secret) << std::endl; } + +} // namespace eoskeygen diff --git a/src/WIF.h b/src/WIF.h index 48eb6a8..55d6993 100644 --- a/src/WIF.h +++ b/src/WIF.h @@ -27,10 +27,14 @@ #include #include "ec/types.h" +namespace eoskeygen { + std::string wif_priv_encode(ec_privkey_t priv); std::string wif_pub_encode(ec_pubkey_t pub); void wif_print_key(const struct ec_keypair *key); +} // namespace eoskeygen + #endif /* WIF_H */ diff --git a/src/base58.cpp b/src/base58.cpp index cd3eccd..ad00911 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -28,6 +28,8 @@ #include #include "base58.h" +namespace eoskeygen { + static const char charmap[59] = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; std::string base58_encode(const unsigned char* pbegin, const unsigned char* pend) { @@ -80,3 +82,5 @@ std::string base58_encode(const std::vector& vch) { return base58_encode(vch.data(), vch.data() + vch.size()); } + +} // namespace eoskeygen diff --git a/src/base58.h b/src/base58.h index 82039b5..59e269d 100644 --- a/src/base58.h +++ b/src/base58.h @@ -27,8 +27,12 @@ #include #include +namespace eoskeygen { + std::string base58_encode(const std::string& str); std::string base58_encode(const std::vector& vch); std::string base58_encode(const unsigned char* pbegin, const unsigned char* pend); +} //namespace eoskeygen + #endif /* BASE58_H */ diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 228b045..42b6b20 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -29,6 +29,8 @@ using std::chrono::steady_clock; using std::chrono::duration; using std::chrono::time_point; +namespace eoskeygen { + void benchmark(size_t num_keys, struct benchmark_result* res) { time_point start; @@ -48,3 +50,5 @@ void benchmark(size_t num_keys, struct benchmark_result* res) { res->sec = duration(steady_clock::now() - start).count(); res->kps = static_cast(num_keys) / res->sec; } + +} // namespace eoskeygen diff --git a/src/benchmark.h b/src/benchmark.h index f001808..e21a178 100644 --- a/src/benchmark.h +++ b/src/benchmark.h @@ -26,6 +26,8 @@ #include +namespace eoskeygen { + struct benchmark_result { float sec; // elapsed seconds. float kps; // keys per second. @@ -33,4 +35,6 @@ struct benchmark_result { void benchmark(size_t num_keys, struct benchmark_result* res); +} // namespace eoskeygen + #endif /* BENCHMARK_H */ diff --git a/src/checksum.cpp b/src/checksum.cpp index 53c7e33..aed8deb 100644 --- a/src/checksum.cpp +++ b/src/checksum.cpp @@ -26,6 +26,8 @@ #include #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); @@ -46,3 +48,5 @@ inline void sha256d(const unsigned char *data, std::size_t len, unsigned char *o checksum_impl(sha256d, sha256d) checksum_impl(ripemd160, RIPEMD160) + +} // namespace eosio-keygen diff --git a/src/checksum.h b/src/checksum.h index 1ac08a2..49b27f4 100644 --- a/src/checksum.h +++ b/src/checksum.h @@ -27,6 +27,8 @@ #include #include +namespace eoskeygen { + #define CHECKSUM_SIZE 4 typedef std::array checksum_t; @@ -35,4 +37,6 @@ checksum_t checksum_sha256d(const unsigned char *data, std::size_t len); checksum_t checksum_ripemd160(const unsigned char *data, std::size_t len); +} // namespace eoskeygen + #endif /* CHECKSUM_H */ diff --git a/src/console.h b/src/console.h index c81a5b7..49932a7 100644 --- a/src/console.h +++ b/src/console.h @@ -26,6 +26,8 @@ #include +namespace eoskeygen { + namespace console { // enum for all supported colors. @@ -80,4 +82,6 @@ namespace console { } // namespace console +} // namespace eoskeygen + #endif /* CONSOLE_H */ diff --git a/src/console_ansi.cpp b/src/console_ansi.cpp index 3aab56b..272615a 100644 --- a/src/console_ansi.cpp +++ b/src/console_ansi.cpp @@ -24,6 +24,8 @@ #include #include "console.h" +namespace eoskeygen { + namespace console { std::ostream& reset(std::ostream& os) { @@ -69,3 +71,5 @@ std::ostream& operator<<(std::ostream& os, const fg& obj) { } } // namespace console + +} // namespace eoskeygen diff --git a/src/console_win32.cpp b/src/console_win32.cpp index 5f7e87d..a6c2800 100644 --- a/src/console_win32.cpp +++ b/src/console_win32.cpp @@ -25,6 +25,8 @@ #include #include "console.h" +namespace eoskeygen { + // WinAPI colors #define FG_BLACK 0 #define FG_BLUE FOREGROUND_BLUE @@ -88,3 +90,5 @@ std::ostream& operator<<(std::ostream& os, const fg& obj) { } } // namespace console + +} // namespace eoskeygen diff --git a/src/ec/generate.h b/src/ec/generate.h index ccde10c..c3f686c 100644 --- a/src/ec/generate.h +++ b/src/ec/generate.h @@ -26,10 +26,14 @@ #include "types.h" +namespace eoskeygen { + /** * Generates a keypair using the secp256k1 curve. * public key is in compressed format. */ int ec_generate_key(struct ec_keypair *pair); +} // namespace eoskeygen + #endif /* EC_GENERATE_H */ diff --git a/src/ec/openssl.cpp b/src/ec/openssl.cpp index 40e0cf3..563af41 100644 --- a/src/ec/openssl.cpp +++ b/src/ec/openssl.cpp @@ -26,6 +26,8 @@ #include #include "generate.h" +namespace eoskeygen { + static int ec_generate_pair(unsigned char *priv, unsigned char *pub) { int ret = -1; @@ -69,3 +71,5 @@ int ec_generate_key(struct ec_keypair *pair) { return ec_generate_pair(pair->secret.data(), pair->pub.data()); } + +} // namespace eoskeygen diff --git a/src/ec/types.h b/src/ec/types.h index 19a2312..a59cf7a 100644 --- a/src/ec/types.h +++ b/src/ec/types.h @@ -26,6 +26,8 @@ #include +namespace eoskeygen { + #define EC_PRIVKEY_SIZE 32 /* @@ -43,4 +45,6 @@ struct ec_keypair { ec_pubkey_t pub; }; +} // namespace eoskeygen + #endif /* EC_TYPES_H */ diff --git a/src/key_search.cpp b/src/key_search.cpp index 9e1a259..a6a190c 100644 --- a/src/key_search.cpp +++ b/src/key_search.cpp @@ -26,6 +26,8 @@ #include "key_search_helpers.h" #include "key_search.h" +namespace eoskeygen { + void KeySearch::addWord(const std::string& str) { std::string tmp = str; @@ -78,3 +80,5 @@ void KeySearch::find(size_t num_results) { _search_linear(num_results); } + +} // namespace eoskeygen diff --git a/src/key_search.h b/src/key_search.h index 6a6e5b3..c1210ea 100644 --- a/src/key_search.h +++ b/src/key_search.h @@ -26,6 +26,8 @@ #include "string.h" +namespace eoskeygen { + class KeySearch { public : @@ -67,4 +69,6 @@ protected : #endif /* HAVE_THREADS */ }; +} // namespace eoskeygen + #endif /* KEY_SEARCH_H */ diff --git a/src/key_search_helpers.cpp b/src/key_search_helpers.cpp index 32a7a21..b6aad5b 100644 --- a/src/key_search_helpers.cpp +++ b/src/key_search_helpers.cpp @@ -26,6 +26,8 @@ #include "console.h" #include "key_search_helpers.h" +namespace eoskeygen { + void key_search_result(const struct ec_keypair* key, const struct key_result* result) { std::string pub = wif_pub_encode(key->pub); @@ -59,3 +61,5 @@ bool key_contains_word(const struct ec_keypair* key, const strlist_t& word_list, } return false; } + +} // namespace eoskeygen diff --git a/src/key_search_helpers.h b/src/key_search_helpers.h index 1b80105..c688421 100644 --- a/src/key_search_helpers.h +++ b/src/key_search_helpers.h @@ -27,6 +27,8 @@ #include "string.h" #include "ec/types.h" +namespace eoskeygen { + struct key_result { size_t pos; // position where the word was found. size_t len; // the length of the word. @@ -38,4 +40,6 @@ void key_search_result(const struct ec_keypair* key, const struct key_result* re // returns true if a word was found (stored in ), false otherwise. bool key_contains_word(const struct ec_keypair* key, const strlist_t& word_list, struct key_result *result); +} // namespace eoskeygen + #endif /* KEY_SEARCH_HELPERS_H */ diff --git a/src/key_search_mt.cpp b/src/key_search_mt.cpp index 85f3e2a..2d01382 100644 --- a/src/key_search_mt.cpp +++ b/src/key_search_mt.cpp @@ -29,6 +29,8 @@ #include "key_search_helpers.h" #include "key_search.h" +namespace eoskeygen { + // Max keys to search for, std::size_t g_max; @@ -95,3 +97,5 @@ void KeySearch::_search_mt(size_t n) t[i].join(); } } + +} // namespace eoskeygen diff --git a/src/main.cpp b/src/main.cpp index 002c05d..2eed33c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,13 +39,13 @@ bool option_l33t = false; int option_num_threads = std::thread::hardware_concurrency(); #endif /* HAVE_THREADS */ -void cmd_search(const strlist_t& words, int count) { +void cmd_search(const eoskeygen::strlist_t& words, int count) { - KeySearch ks; + eoskeygen::KeySearch ks; if (option_l33t) { for(std::size_t i = 0; i < words.size(); i++) { - ks.addList(l33twords(words[i])); + ks.addList(eoskeygen::l33twords(words[i])); } } else { ks.addList(words); @@ -56,7 +56,7 @@ void cmd_search(const strlist_t& words, int count) { #endif /* HAVE_THREADS */ std::cout << "Searching for " << count - << " keys containing: " << strjoin(ks.getList(), ",") + << " keys containing: " << eoskeygen::strjoin(ks.getList(), ",") #ifdef HAVE_THREADS << ", Using: " << option_num_threads << " threads" #endif /* HAVE_THREADS */ @@ -106,12 +106,12 @@ void usage(const char *name) { void cmd_benchmark(size_t num_keys) { - struct benchmark_result res; + struct eoskeygen::benchmark_result res; std::cout << "Benchmark: Generating " << num_keys << " keys" << std::endl; - benchmark(num_keys, &res); + eoskeygen::benchmark(num_keys, &res); std::cout << "Result: Took " << res.sec << " seconds, " << res.kps << " keys per second." << std::endl; @@ -125,9 +125,9 @@ int main(int argc, char **argv) { // No args, just print a key. if (argc <= 1) { - struct ec_keypair pair; - ec_generate_key(&pair); - wif_print_key(&pair); + struct eoskeygen::ec_keypair pair; + eoskeygen::ec_generate_key(&pair); + eoskeygen::wif_print_key(&pair); return 0; } @@ -139,7 +139,7 @@ int main(int argc, char **argv) { if (!strcmp(argv[p], "search")) { int count = 10; - strlist_t words; + eoskeygen::strlist_t words; while(p++ < argc - 1) { if (!strcmp(argv[p], "--l33t")) { @@ -167,7 +167,7 @@ int main(int argc, char **argv) { } // wordlist and count else if (words.size() < 1) { - words = strsplitwords(std::string(argv[p])); + words = eoskeygen::strsplitwords(std::string(argv[p])); if (p + 1 < argc) { count = atoi(argv[++p]); diff --git a/src/string.cpp b/src/string.cpp index bee3330..477311c 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -26,6 +26,8 @@ #include #include "string.h" +namespace eoskeygen { + strlist_t strsplitwords(const std::string& str, const std::string& delim) { strlist_t words = strsplit(str, delim); @@ -156,3 +158,5 @@ strlist_t l33twords(std::string str) { _l33t(list, str, 0); return list; } + +} // namespace eoskeygen diff --git a/src/string.h b/src/string.h index b3f6300..a0b07fc 100644 --- a/src/string.h +++ b/src/string.h @@ -27,6 +27,8 @@ #include #include +namespace eoskeygen { + typedef std::vector strlist_t; strlist_t strsplitwords(const std::string& str, const std::string& delim = ","); @@ -46,4 +48,6 @@ strlist_t& base58_strip(strlist_t& list); strlist_t l33twords(std::string str); +} // namespace eoskeygen + #endif /* STRING_H */ From 04641354ffb963082480d05c0763a096bf02af43 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 10 Feb 2020 17:01:31 +0100 Subject: [PATCH 2/5] Adding "EOSIOKEYGEN" prefix to all header guards. --- src/WIF.h | 6 +++--- src/base58.h | 6 +++--- src/benchmark.h | 6 +++--- src/checksum.h | 6 +++--- src/console.h | 6 +++--- src/ec/generate.h | 6 +++--- src/ec/types.h | 6 +++--- src/key_search.h | 6 +++--- src/key_search_helpers.h | 6 +++--- src/string.h | 6 +++--- 10 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/WIF.h b/src/WIF.h index 55d6993..9ee077c 100644 --- a/src/WIF.h +++ b/src/WIF.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef WIF_H -#define WIF_H +#ifndef EOSIOKEYGEN_WIF_H +#define EOSIOKEYGEN_WIF_H #include #include "ec/types.h" @@ -37,4 +37,4 @@ void wif_print_key(const struct ec_keypair *key); } // namespace eoskeygen -#endif /* WIF_H */ +#endif /* EOSIOKEYGEN_WIF_H */ diff --git a/src/base58.h b/src/base58.h index 59e269d..16b7798 100644 --- a/src/base58.h +++ b/src/base58.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef BASE58_H -#define BASE58_H +#ifndef EOSIOKEYGEN_BASE58_H +#define EOSIOKEYGEN_BASE58_H #include #include @@ -35,4 +35,4 @@ std::string base58_encode(const unsigned char* pbegin, const unsigned char* pend } //namespace eoskeygen -#endif /* BASE58_H */ +#endif /* EOSIOKEYGEN_BASE58_H */ diff --git a/src/benchmark.h b/src/benchmark.h index e21a178..8b1f57b 100644 --- a/src/benchmark.h +++ b/src/benchmark.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef BENCHMARK_H -#define BENCHMARK_H +#ifndef EOSIOKEYGEN_BENCHMARK_H +#define EOSIOKEYGEN_BENCHMARK_H #include @@ -37,4 +37,4 @@ void benchmark(size_t num_keys, struct benchmark_result* res); } // namespace eoskeygen -#endif /* BENCHMARK_H */ +#endif /* EOSIOKEYGEN_BENCHMARK_H */ diff --git a/src/checksum.h b/src/checksum.h index 49b27f4..b48f783 100644 --- a/src/checksum.h +++ b/src/checksum.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef CHECKSUM_H -#define CHECKSUM_H +#ifndef EOSIOKEYGEN_CHECKSUM_H +#define EOSIOKEYGEN_CHECKSUM_H #include #include @@ -39,4 +39,4 @@ checksum_t checksum_ripemd160(const unsigned char *data, std::size_t len); } // namespace eoskeygen -#endif /* CHECKSUM_H */ +#endif /* EOSIOKEYGEN_CHECKSUM_H */ diff --git a/src/console.h b/src/console.h index 49932a7..bc20e0e 100644 --- a/src/console.h +++ b/src/console.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef CONSOLE_H -#define CONSOLE_H +#ifndef EOSIOKEYGEN_CONSOLE_H +#define EOSIOKEYGEN_CONSOLE_H #include @@ -84,4 +84,4 @@ namespace console { } // namespace eoskeygen -#endif /* CONSOLE_H */ +#endif /* EOSIOKEYGEN_CONSOLE_H */ diff --git a/src/ec/generate.h b/src/ec/generate.h index c3f686c..e617fa3 100644 --- a/src/ec/generate.h +++ b/src/ec/generate.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef EC_GENERATE_H -#define EC_GENERATE_H +#ifndef EOSIOKEYGEN_EC_GENERATE_H +#define EOSIOKEYGEN_EC_GENERATE_H #include "types.h" @@ -36,4 +36,4 @@ int ec_generate_key(struct ec_keypair *pair); } // namespace eoskeygen -#endif /* EC_GENERATE_H */ +#endif /* EOSIOKEYGEN_EC_GENERATE_H */ diff --git a/src/ec/types.h b/src/ec/types.h index a59cf7a..f48b063 100644 --- a/src/ec/types.h +++ b/src/ec/types.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef EC_TYPES_H -#define EC_TYPES_H +#ifndef EOSIOKEYGEN_EC_TYPES_H +#define EOSIOKEYGEN_EC_TYPES_H #include @@ -47,4 +47,4 @@ struct ec_keypair { } // namespace eoskeygen -#endif /* EC_TYPES_H */ +#endif /* EOSIOKEYGEN_EC_TYPES_H */ diff --git a/src/key_search.h b/src/key_search.h index c1210ea..78de4d6 100644 --- a/src/key_search.h +++ b/src/key_search.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef KEY_SEARCH_H -#define KEY_SEARCH_H +#ifndef EOSIOKEYGEN_KEY_SEARCH_H +#define EOSIOKEYGEN_KEY_SEARCH_H #include "string.h" @@ -71,4 +71,4 @@ protected : } // namespace eoskeygen -#endif /* KEY_SEARCH_H */ +#endif /* EOSIOKEYGEN_KEY_SEARCH_H */ diff --git a/src/key_search_helpers.h b/src/key_search_helpers.h index c688421..5e8ff39 100644 --- a/src/key_search_helpers.h +++ b/src/key_search_helpers.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef KEY_SEARCH_HELPSER_H -#define KEY_SEARCH_HELPERS_H +#ifndef EOSIOKEYGEN_KEY_SEARCH_HELPSER_H +#define EOSIOKEYGEN_KEY_SEARCH_HELPERS_H #include "string.h" #include "ec/types.h" @@ -42,4 +42,4 @@ bool key_contains_word(const struct ec_keypair* key, const strlist_t& word_list, } // namespace eoskeygen -#endif /* KEY_SEARCH_HELPERS_H */ +#endif /* EOSIOKEYGEN_KEY_SEARCH_HELPERS_H */ diff --git a/src/string.h b/src/string.h index a0b07fc..52313a0 100644 --- a/src/string.h +++ b/src/string.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef STRING_H -#define STRING_H +#ifndef EOSIOKEYGEN_STRING_H +#define EOSIOKEYGEN_STRING_H #include #include @@ -50,4 +50,4 @@ strlist_t l33twords(std::string str); } // namespace eoskeygen -#endif /* STRING_H */ +#endif /* EOSIOKEYGEN_STRING_H */ From bc0369f30149cb6750dbaf35af27104f6b8b6ac7 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 11 Feb 2020 14:48:10 +0100 Subject: [PATCH 3/5] Move "ec" directory to "crypto" and move openssl implementation into crypto/openssl directory. --- CMakeLists.txt | 2 +- src/WIF.h | 2 +- src/benchmark.cpp | 2 +- src/{ec/generate.h => crypto/ec.h} | 6 +++--- src/{ec/openssl.cpp => crypto/openssl/ec.cpp} | 2 +- src/{ec => crypto}/types.h | 6 +++--- src/key_search.cpp | 2 +- src/key_search_helpers.h | 2 +- src/key_search_mt.cpp | 2 +- src/main.cpp | 2 +- 10 files changed, 14 insertions(+), 14 deletions(-) rename src/{ec/generate.h => crypto/ec.h} (92%) rename src/{ec/openssl.cpp => crypto/openssl/ec.cpp} (98%) rename src/{ec => crypto}/types.h (93%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4906a70..fa88c06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,7 +18,6 @@ set (PROGRAM_EXE ${CMAKE_PROJECT_NAME}) set (PROGRAM_SOURCE src/string.cpp - src/ec/openssl.cpp src/base58.cpp src/checksum.cpp src/WIF.cpp @@ -46,6 +45,7 @@ endif() # Libraries find_package(OpenSSL 1.1 REQUIRED) +set (PROGRAM_SOURCE ${PROGRAM_SOURCE} src/crypto/openssl/ec.cpp) if (USE_THREADS) find_package(Threads) diff --git a/src/WIF.h b/src/WIF.h index 9ee077c..a8bba10 100644 --- a/src/WIF.h +++ b/src/WIF.h @@ -25,7 +25,7 @@ #define EOSIOKEYGEN_WIF_H #include -#include "ec/types.h" +#include "crypto/types.h" namespace eoskeygen { diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 42b6b20..bc8e69f 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -22,7 +22,7 @@ * SOFTWARE. */ #include -#include "ec/generate.h" +#include "crypto/ec.h" #include "benchmark.h" using std::chrono::steady_clock; diff --git a/src/ec/generate.h b/src/crypto/ec.h similarity index 92% rename from src/ec/generate.h rename to src/crypto/ec.h index e617fa3..e8558dd 100644 --- a/src/ec/generate.h +++ b/src/crypto/ec.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef EOSIOKEYGEN_EC_GENERATE_H -#define EOSIOKEYGEN_EC_GENERATE_H +#ifndef EOSIOKEYGEN_CRYPTO_EC_H +#define EOSIOKEYGEN_CRYPTO_EC_H #include "types.h" @@ -36,4 +36,4 @@ int ec_generate_key(struct ec_keypair *pair); } // namespace eoskeygen -#endif /* EOSIOKEYGEN_EC_GENERATE_H */ +#endif /* EOSIOKEYGEN_CRYPTO_EC_H */ diff --git a/src/ec/openssl.cpp b/src/crypto/openssl/ec.cpp similarity index 98% rename from src/ec/openssl.cpp rename to src/crypto/openssl/ec.cpp index 563af41..da83738 100644 --- a/src/ec/openssl.cpp +++ b/src/crypto/openssl/ec.cpp @@ -24,7 +24,7 @@ #include #include #include -#include "generate.h" +#include "../ec.h" namespace eoskeygen { diff --git a/src/ec/types.h b/src/crypto/types.h similarity index 93% rename from src/ec/types.h rename to src/crypto/types.h index f48b063..f1a505d 100644 --- a/src/ec/types.h +++ b/src/crypto/types.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef EOSIOKEYGEN_EC_TYPES_H -#define EOSIOKEYGEN_EC_TYPES_H +#ifndef EOSIOKEYGEN_CRYPTO_TYPES_H +#define EOSIOKEYGEN_CRYPTO_TYPES_H #include @@ -47,4 +47,4 @@ struct ec_keypair { } // namespace eoskeygen -#endif /* EOSIOKEYGEN_EC_TYPES_H */ +#endif /* EOSIOKEYGEN_CRYPTO_TYPES_H */ diff --git a/src/key_search.cpp b/src/key_search.cpp index a6a190c..12a3a11 100644 --- a/src/key_search.cpp +++ b/src/key_search.cpp @@ -22,7 +22,7 @@ * SOFTWARE. */ #include -#include "ec/generate.h" +#include "crypto/ec.h" #include "key_search_helpers.h" #include "key_search.h" diff --git a/src/key_search_helpers.h b/src/key_search_helpers.h index 5e8ff39..f3a6bd5 100644 --- a/src/key_search_helpers.h +++ b/src/key_search_helpers.h @@ -25,7 +25,7 @@ #define EOSIOKEYGEN_KEY_SEARCH_HELPERS_H #include "string.h" -#include "ec/types.h" +#include "crypto/types.h" namespace eoskeygen { diff --git a/src/key_search_mt.cpp b/src/key_search_mt.cpp index 2d01382..cc775c1 100644 --- a/src/key_search_mt.cpp +++ b/src/key_search_mt.cpp @@ -25,7 +25,7 @@ #include #include #include -#include "ec/generate.h" +#include "crypto/ec.h" #include "key_search_helpers.h" #include "key_search.h" diff --git a/src/main.cpp b/src/main.cpp index 2eed33c..3e16602 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,7 +28,7 @@ #include #include "string.h" #include "WIF.h" -#include "ec/generate.h" +#include "crypto/ec.h" #include "key_search.h" #include "benchmark.h" From 7a4cc43ec950b854941c02dc57851f0a3957382e Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 11 Feb 2020 14:49:56 +0100 Subject: [PATCH 4/5] src/crypto/openssl/ec.cpp: remove static helper function. --- src/crypto/openssl/ec.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/crypto/openssl/ec.cpp b/src/crypto/openssl/ec.cpp index da83738..2d7fbac 100644 --- a/src/crypto/openssl/ec.cpp +++ b/src/crypto/openssl/ec.cpp @@ -28,7 +28,7 @@ namespace eoskeygen { -static int ec_generate_pair(unsigned char *priv, unsigned char *pub) { +int ec_generate_key(struct ec_keypair *pair) { int ret = -1; EC_KEY *k; @@ -52,12 +52,12 @@ static int ec_generate_pair(unsigned char *priv, unsigned char *pub) { } // Copy private key to binary format. - EC_KEY_priv2oct(k, priv, EC_PRIVKEY_SIZE); + EC_KEY_priv2oct(k, pair->secret.data(), EC_PRIVKEY_SIZE); // Copy public key key EC_POINT_point2oct(EC_KEY_get0_group(k), EC_KEY_get0_public_key(k), POINT_CONVERSION_COMPRESSED, - pub, EC_PUBKEY_SIZE, ctx); + pair->pub.data(), EC_PUBKEY_SIZE, ctx); ret = 0; fail2: @@ -67,9 +67,4 @@ fail1: return ret; } -int ec_generate_key(struct ec_keypair *pair) { - - return ec_generate_pair(pair->secret.data(), pair->pub.data()); -} - } // namespace eoskeygen From fc5614eff23507e823fe1513ce392cd84d4aeee0 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Tue, 11 Feb 2020 15:42:41 +0100 Subject: [PATCH 5/5] 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 */