Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing the decrypting option of keyconv #64

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
12 changes: 12 additions & 0 deletions bulkwallet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# a simple bulk-wallet generator
#
# 3 arguments needed: address version, pattern, count
#
# some address versions:
# 0 Bitcoin
# 23 Primecoin
# 48 Litecoin

./vanitygen -kF compressed -X $1 $2 2>/dev/null | head -n `expr $3 \* 3` | egrep "Address|Privkey" | awk '{printf("%s ", $2); getline; printf("%s\n",$2)}'
81 changes: 54 additions & 27 deletions calc_addrs.cl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
* Steps:
* - Compute Px = Pxj * (1/Pz)^2
* - Compute Py = Pyj * (1/Pz)^3
* - Compute H = RIPEMD160(SHA256(0x04 | Px | Py))
* - Compute H = RIPEMD160(SHA256({0x02|0x03|0x04} | Px | Py?))
*
* Output:
* - Array of 20-byte address hash values
Expand All @@ -94,6 +94,13 @@
#define load_be32(v) bswap32(v)
#endif

/* Configuration -- maybe I shouldn't be passing this in preproc */
#ifdef COMPRESSED_ADDRESS
__constant bool compressed_address = 1;
#else
__constant bool compressed_address = 0;
#endif

/*
* Loop unrolling macros
*
Expand Down Expand Up @@ -1234,7 +1241,7 @@ hash_ec_point(uint *hash_out, __global bn_word *xy, __global bn_word *zip)
bn_mul_mont(&c, &c, &zzi); /* X / Z^2 */
bn_from_mont(&c, &c);

wh = 0x00000004; /* POINT_CONVERSION_UNCOMPRESSED */
wh = compressed_address ? 0x00000002 : 0x00000004; /* POINT_CONVERSION_[UN]COMPRESSED */

#define hash_ec_point_inner_3(i) \
wl = wh; \
Expand All @@ -1253,39 +1260,59 @@ hash_ec_point(uint *hash_out, __global bn_word *xy, __global bn_word *zip)
bn_mul_mont(&c, &c, &zzi); /* Y / Z^3 */
bn_from_mont(&c, &c);

#define hash_ec_point_inner_5(i) \
wl = wh; \
wh = c.d[(BN_NWORDS - 1) - i]; \
hash1[BN_NWORDS + i] = (wl << 24) | (wh >> 8);
if (!compressed_address) {
#define hash_ec_point_inner_5(i) \
wl = wh; \
wh = c.d[(BN_NWORDS - 1) - i]; \
hash1[BN_NWORDS + i] = (wl << 24) | (wh >> 8);

bn_unroll(hash_ec_point_inner_5);
bn_unroll(hash_ec_point_inner_5);
} else {
if (bn_is_odd(c)) {
hash1[0] |= 0x01000000; /* 0x03 for odd y */
}

/*
* Put in the last byte + SHA-2 padding.
*/
hash1[8] = wh << 24 | 0x800000;
hash1[9] = 0;
hash1[10] = 0;
hash1[11] = 0;
hash1[12] = 0;
hash1[13] = 0;
hash1[14] = 0;
hash1[15] = 33 * 8;
}

/*
* Hash the first 64 bytes of the buffer
*/
sha2_256_init(hash2);
sha2_256_block(hash2, hash1);

/*
* Hash the last byte of the buffer + SHA-2 padding
*/
hash1[0] = wh << 24 | 0x800000;
hash1[1] = 0;
hash1[2] = 0;
hash1[3] = 0;
hash1[4] = 0;
hash1[5] = 0;
hash1[6] = 0;
hash1[7] = 0;
hash1[8] = 0;
hash1[9] = 0;
hash1[10] = 0;
hash1[11] = 0;
hash1[12] = 0;
hash1[13] = 0;
hash1[14] = 0;
hash1[15] = 65 * 8;
sha2_256_block(hash2, hash1);
if (!compressed_address) {
/*
* Hash the last byte of the buffer + SHA-2 padding
*/
hash1[0] = wh << 24 | 0x800000;
hash1[1] = 0;
hash1[2] = 0;
hash1[3] = 0;
hash1[4] = 0;
hash1[5] = 0;
hash1[6] = 0;
hash1[7] = 0;
hash1[8] = 0;
hash1[9] = 0;
hash1[10] = 0;
hash1[11] = 0;
hash1[12] = 0;
hash1[13] = 0;
hash1[14] = 0;
hash1[15] = 65 * 8;
sha2_256_block(hash2, hash1);
}

/*
* Hash the SHA-2 result with RIPEMD160
Expand Down
17 changes: 12 additions & 5 deletions keyconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ usage(const char *progname)
"-e Encrypt output key, prompt for password\n"
"-E <password> Encrypt output key with <password> (UNSAFE)\n"
"-c <key> Combine private key parts to make complete private key\n"
"-d Decrypt output key, prompt for password\n"
"-v Verbose output\n",
version, progname);
}
Expand All @@ -52,10 +53,11 @@ main(int argc, char **argv)
int pass_prompt = 0;
int verbose = 0;
int generate = 0;
int decrypt = 0;
int opt;
int res;

while ((opt = getopt(argc, argv, "8E:ec:vG")) != -1) {
while ((opt = getopt(argc, argv, "8E:ec:vGd")) != -1) {
switch (opt) {
case '8':
pkcs8 = 1;
Expand Down Expand Up @@ -86,6 +88,9 @@ main(int argc, char **argv)
case 'G':
generate = 1;
break;
case 'd':
decrypt = 1;
break;
default:
usage(argv[0]);
return 1;
Expand Down Expand Up @@ -123,13 +128,15 @@ main(int argc, char **argv)
key_in = argv[optind];
}

res = vg_decode_privkey_any(pkey, &privtype, key_in, NULL);
if (res < 0) {

if (decrypt) {
if (EVP_read_pw_string(pwbuf, sizeof(pwbuf),
"Enter import password:", 0) ||
!vg_decode_privkey_any(pkey, &privtype, key_in, pwbuf))
!vg_protect_decode_privkey(pkey, &privtype, key_in, pwbuf))
return 1;
}
res = 1;
} else
res = vg_decode_privkey_any(pkey, &privtype, key_in, NULL);

if (!res) {
fprintf(stderr, "ERROR: Unrecognized key format\n");
Expand Down
3 changes: 3 additions & 0 deletions oclengine.c
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,9 @@ vg_ocl_init(vg_context_t *vcp, vg_ocl_context_t *vocp, cl_device_id did,
if (vocp->voc_quirks & VG_OCL_AMD_BFI_INT)
end += snprintf(optbuf + end, sizeof(optbuf) - end,
"-DAMD_BFI_INT ");
if (vcp->vc_compressed)
end += snprintf(optbuf + end, sizeof(optbuf) - end,
"-DCOMPRESSED_ADDRESS");
if (vocp->voc_quirks & VG_OCL_NV_VERBOSE)
end += snprintf(optbuf + end, sizeof(optbuf) - end,
"-cl-nv-verbose ");
Expand Down
30 changes: 27 additions & 3 deletions oclvanitygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ usage(const char *name)
"-N Generate namecoin address\n"
"-T Generate bitcoin testnet address\n"
"-X <version> Generate address with the given version\n"
"-F <format> Generate address with the given format (pubkey, compressed)\n"
"-e Encrypt private keys, prompt for password\n"
"-E <password> Encrypt private keys with <password> (UNSAFE)\n"
"-p <platform> Select OpenCL platform\n"
Expand Down Expand Up @@ -119,11 +120,12 @@ main(int argc, char **argv)
int pattfpi[MAX_FILE];
int npattfp = 0;
int pattstdin = 0;
int compressed = 0;

int i;

while ((opt = getopt(argc, argv,
"vqik1NTX:eE:p:P:d:w:t:g:b:VSh?f:o:s:D:")) != -1) {
"vqik1NTX:F:eE:p:P:d:w:t:g:b:VSh?f:o:s:D:")) != -1) {
switch (opt) {
case 'v':
verbose = 2;
Expand Down Expand Up @@ -152,6 +154,16 @@ main(int argc, char **argv)
addrtype = atoi(optarg);
privtype = 128 + addrtype;
break;
case 'F':
if (!strcmp(optarg, "compressed"))
compressed = 1;
else
if (strcmp(optarg, "pubkey")) {
fprintf(stderr,
"Invalid format '%s'\n", optarg);
return 1;
}
break;
case 'e':
prompt_password = 1;
break;
Expand Down Expand Up @@ -302,6 +314,17 @@ main(int argc, char **argv)
"WARNING: case insensitive mode incompatible with "
"regular expressions\n");

if (!seedfile)
{
#if !defined(_WIN32)
struct stat st;
if (stat("/dev/random", &st) == 0)
{
seedfile = "/dev/random";
}
#endif
}

if (seedfile) {
opt = -1;
#if !defined(_WIN32)
Expand All @@ -313,10 +336,10 @@ main(int argc, char **argv)
#endif
opt = RAND_load_file(seedfile, opt);
if (!opt) {
fprintf(stderr, "Could not load RNG seed %s\n", optarg);
fprintf(stderr, "Could not load RNG seed '%s'\n", seedfile);
return 1;
}
if (verbose > 0) {
if (verbose > 1) {
fprintf(stderr,
"Read %d bytes from RNG seed file\n", opt);
}
Expand All @@ -330,6 +353,7 @@ main(int argc, char **argv)
caseinsensitive);
}

vcp->vc_compressed = compressed;
vcp->vc_verbose = verbose;
vcp->vc_result_file = result_file;
vcp->vc_remove_on_match = remove_on_match;
Expand Down
31 changes: 31 additions & 0 deletions paperwallet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# paper wallet generator
#
# takes a list of addresses and privkeys (as produced by bulkwallet.sh) on
# stdin, produces HTML output with QR codes
#
# depends on base64 and qrencode

cat <<EOF
<html>
<body>
<h1>Paper Wallet for
EOF
whoami
cat <<EOF
</h1><h2>Generated
EOF
date
cat <<EOF
</h2>
<table style="table-layout: fixed; word-wrap: break-word; width: 800px;">
EOF

sed "s/\(.*\) \(.*\)/echo -en \"<tr><td style=\\\\\"text-align: center; width: 150px;\\\\\"><img src=\\\\\"data:image\/png;base64,\"; qrencode -l L -o - \1 | base64 -w 0; echo \"\\\\\" \\\\><\/td><td style=\\\\\"width: 500px; font-family: monospace;\\\\\"><p style=\\\\\"text-align: left;\\\\\">\1<\/p><p style=\\\\\"text-align: right;\\\\\">\2<\/p><\/td><td style=\\\\\"text-align: center; width: 150px;\\\\\"><img src=\\\\\"data:image\/png;base64,\"; qrencode -l L -o - \2 | base64 -w 0; echo \"\\\\\" \\\\><\/td><\/tr>\"/" | bash

cat <<EOF
</table>
</body>
</html>
EOF
18 changes: 13 additions & 5 deletions pattern.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ vg_exec_context_calc_address(vg_exec_context_t *vxcp)
}
len = EC_POINT_point2oct(pgroup,
pubkey,
POINT_CONVERSION_UNCOMPRESSED,
vxcp->vxc_vc->vc_compressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED,
eckey_buf,
sizeof(eckey_buf),
vxcp->vxc_bnctx);
Expand Down Expand Up @@ -528,9 +528,14 @@ vg_output_match_console(vg_context_t *vcp, EC_KEY *pkey, const char *pattern)
}

assert(EC_KEY_check_key(pkey));
vg_encode_address(ppnt,
EC_KEY_get0_group(pkey),
vcp->vc_pubkeytype, addr_buf);
if (vcp->vc_compressed)
vg_encode_address_compressed(ppnt,
EC_KEY_get0_group(pkey),
vcp->vc_pubkeytype, addr_buf);
else
vg_encode_address(ppnt,
EC_KEY_get0_group(pkey),
vcp->vc_pubkeytype, addr_buf);
if (isscript)
vg_encode_script_address(ppnt,
EC_KEY_get0_group(pkey),
Expand All @@ -550,7 +555,10 @@ vg_output_match_console(vg_context_t *vcp, EC_KEY *pkey, const char *pattern)
}
}
if (!vcp->vc_key_protect_pass) {
vg_encode_privkey(pkey, vcp->vc_privtype, privkey_buf);
if (vcp->vc_compressed)
vg_encode_privkey_compressed(pkey, vcp->vc_privtype, privkey_buf);
else
vg_encode_privkey(pkey, vcp->vc_privtype, privkey_buf);
}

if (!vcp->vc_result_file || (vcp->vc_verbose > 0)) {
Expand Down
1 change: 1 addition & 0 deletions pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ enum vg_format {

/* Application-level context, incl. parameters and global pattern store */
struct _vg_context_s {
int vc_compressed;
int vc_addrtype;
int vc_privtype;
unsigned long vc_npatterns;
Expand Down
Loading