1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-keygen synced 2026-06-18 04:00:03 +02:00

Merge pull request #19 from eosswedenorg/openssl-refactor

Closes #17
This commit is contained in:
Henrik Hautakoski 2020-02-11 16:39:16 +01:00 committed by GitHub
commit 84f01dd707
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 230 additions and 68 deletions

View file

@ -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,10 @@ endif()
# Libraries
find_package(OpenSSL 1.1 REQUIRED)
set (PROGRAM_SOURCE ${PROGRAM_SOURCE}
src/crypto/openssl/ec.cpp
src/crypto/openssl/hash.cpp
)
if (USE_THREADS)
find_package(Threads)

View file

@ -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

View file

@ -21,11 +21,13 @@
* 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 <string>
#include "ec/types.h"
#include "crypto/types.h"
namespace eoskeygen {
std::string wif_priv_encode(ec_privkey_t priv);
@ -33,4 +35,6 @@ std::string wif_pub_encode(ec_pubkey_t pub);
void wif_print_key(const struct ec_keypair *key);
#endif /* WIF_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_WIF_H */

View file

@ -28,6 +28,8 @@
#include <cassert>
#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<unsigned char>& vch) {
return base58_encode(vch.data(), vch.data() + vch.size());
}
} // namespace eoskeygen

View file

@ -21,14 +21,18 @@
* 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 <string>
#include <vector>
namespace eoskeygen {
std::string base58_encode(const std::string& str);
std::string base58_encode(const std::vector<unsigned char>& vch);
std::string base58_encode(const unsigned char* pbegin, const unsigned char* pend);
#endif /* BASE58_H */
} //namespace eoskeygen
#endif /* EOSIOKEYGEN_BASE58_H */

View file

@ -22,13 +22,15 @@
* SOFTWARE.
*/
#include <chrono>
#include "ec/generate.h"
#include "crypto/ec.h"
#include "benchmark.h"
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<steady_clock> start;
@ -48,3 +50,5 @@ void benchmark(size_t num_keys, struct benchmark_result* res) {
res->sec = duration<float>(steady_clock::now() - start).count();
res->kps = static_cast<float>(num_keys) / res->sec;
}
} // namespace eoskeygen

View file

@ -21,11 +21,13 @@
* 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 <ctime>
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);
#endif /* BENCHMARK_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_BENCHMARK_H */

View file

@ -21,28 +21,31 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <openssl/sha.h>
#include <openssl/ripemd.h>
#include <cstring>
#include "crypto/hash.h"
#include "checksum.h"
inline void sha256d(const unsigned char *data, std::size_t len, unsigned char *out) {
SHA256(data, len, out);
SHA256(out, 32, out);
namespace eoskeygen {
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

View file

@ -21,12 +21,14 @@
* 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 <cstddef>
#include <array>
namespace eoskeygen {
#define CHECKSUM_SIZE 4
typedef std::array<unsigned char, CHECKSUM_SIZE> 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);
#endif /* CHECKSUM_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_CHECKSUM_H */

View file

@ -21,11 +21,13 @@
* 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 <ostream>
namespace eoskeygen {
namespace console {
// enum for all supported colors.
@ -80,4 +82,6 @@ namespace console {
} // namespace console
#endif /* CONSOLE_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_CONSOLE_H */

View file

@ -24,6 +24,8 @@
#include <iostream>
#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

View file

@ -25,6 +25,8 @@
#include <iostream>
#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

View file

@ -21,15 +21,19 @@
* 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_CRYPTO_EC_H
#define EOSIOKEYGEN_CRYPTO_EC_H
#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);
#endif /* EC_GENERATE_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_CRYPTO_EC_H */

38
src/crypto/hash.h Normal file
View file

@ -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 <cstddef>
#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 */

View file

@ -24,9 +24,11 @@
#include <openssl/ec.h>
#include <openssl/bn.h>
#include <openssl/hmac.h>
#include "generate.h"
#include "../ec.h"
static int ec_generate_pair(unsigned char *priv, unsigned char *pub) {
namespace eoskeygen {
int ec_generate_key(struct ec_keypair *pair) {
int ret = -1;
EC_KEY *k;
@ -50,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:
@ -65,7 +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

View file

@ -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 <openssl/sha.h>
#include <openssl/ripemd.h>
#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

View file

@ -21,11 +21,13 @@
* 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_CRYPTO_TYPES_H
#define EOSIOKEYGEN_CRYPTO_TYPES_H
#include <array>
namespace eoskeygen {
#define EC_PRIVKEY_SIZE 32
/*
@ -43,4 +45,11 @@ struct ec_keypair {
ec_pubkey_t pub;
};
#endif /* EC_TYPES_H */
// Hashes.
typedef struct { unsigned char data[20]; } ripemd160_t;
typedef struct { unsigned char data[32]; } sha256_t;
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_CRYPTO_TYPES_H */

View file

@ -22,10 +22,12 @@
* SOFTWARE.
*/
#include <string>
#include "ec/generate.h"
#include "crypto/ec.h"
#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

View file

@ -21,11 +21,13 @@
* 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"
namespace eoskeygen {
class KeySearch
{
public :
@ -67,4 +69,6 @@ protected :
#endif /* HAVE_THREADS */
};
#endif /* KEY_SEARCH_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_KEY_SEARCH_H */

View file

@ -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

View file

@ -21,11 +21,13 @@
* 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"
#include "crypto/types.h"
namespace eoskeygen {
struct key_result {
size_t pos; // position where the word was found.
@ -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 <result>), false otherwise.
bool key_contains_word(const struct ec_keypair* key, const strlist_t& word_list, struct key_result *result);
#endif /* KEY_SEARCH_HELPERS_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_KEY_SEARCH_HELPERS_H */

View file

@ -25,10 +25,12 @@
#include <thread>
#include <mutex>
#include <vector>
#include "ec/generate.h"
#include "crypto/ec.h"
#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

View file

@ -28,7 +28,7 @@
#include <cstring>
#include "string.h"
#include "WIF.h"
#include "ec/generate.h"
#include "crypto/ec.h"
#include "key_search.h"
#include "benchmark.h"
@ -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]);

View file

@ -26,6 +26,8 @@
#include <algorithm>
#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

View file

@ -21,12 +21,14 @@
* 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 <vector>
#include <string>
namespace eoskeygen {
typedef std::vector<std::string> 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);
#endif /* STRING_H */
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_STRING_H */