Exemple #1
0
    def no_ec_multiply(self, v, compressed = False):
            k = CKey()
            k.generate(unhexlify(v['unencrypted_hex']))
            k.set_compressed(compressed)

            # Test get_secret()
            self.assertEqual(unhexlify(v['unencrypted_hex']), k.get_secret())
            self.assertEqual(v['unencrypted_wif'], k.get_secret(form=CKeyForm.BASE58))

            # Test encryption
            b = Bip38(k, v['passphrase'])
            self.assertEqual(v['encrypted'], b.encrypt_no_ec_multiply())

            # Test decryption
            self.assertEqual(unhexlify(v['unencrypted_hex']), Bip38.decrypt(v['encrypted'], v['passphrase']))
    def no_ec_multiply(self, v, compressed=False):
        k = CKey()
        k.generate(unhexlify(v['unencrypted_hex']))
        k.set_compressed(compressed)

        # Test get_secret()
        self.assertEqual(unhexlify(v['unencrypted_hex']), k.get_secret())
        self.assertEqual(v['unencrypted_wif'],
                         k.get_secret(form=CKeyForm.BASE58))

        # Test encryption
        b = Bip38(k, v['passphrase'])
        self.assertEqual(v['encrypted'], b.encrypt_no_ec_multiply())

        # Test decryption
        self.assertEqual(unhexlify(v['unencrypted_hex']),
                         Bip38.decrypt(v['encrypted'], v['passphrase']))
Exemple #3
0
    def test_no_compression_ec_multiply_lot_sequence_numbers(self):
        vec = [ {'passphrase': 'MOLON LABE',
                 'passphrase_code': 'passphraseaB8feaLQDENqCgr4gKZpmf4VoaT6qdjJNJiv7fsKvjqavcJxvuR1hy25aTu5sX',
                 'encrypted': '6PgNBNNzDkKdhkT6uJntUXwwzQV8Rr2tZcbkDcuC9DZRsS6AtHts4Ypo1j',
                 'salt': '\x4f\xca\x5a\x97',
                 'seedb': '\x87\xa1\x3b\x07\x85\x8f\xa7\x53\xcd\x3a\xb3\xf1\xc5\xea\xfb\x5f\x12\x57\x9b\x6c\x33\xc9\xa5\x3f',
                 'bitcoin_address': '1Jscj8ALrYu2y9TD8NrpvDBugPedmbj4Yh',
                 'unencrypted_wif': '5JLdxTtcTHcfYcmJsNVy1v2PMDx432JPoYcBTVVRHpPaxUrdtf8',
                 'unencrypted_hex': '44EA95AFBF138356A05EA32110DFD627232D0F2991AD221187BE356F19FA8190',
                 'confirmation_code': 'cfrm38V8aXBn7JWA1ESmFMUn6erxeBGZGAxJPY4e36S9QWkzZKtaVqLNMgnifETYw7BPwWC9aPD',
                 'lot': 263183,
                 'sequence': 1,
                },
                {'passphrase': 'ΜΟΛΩΝ ΛΑΒΕ',
                 'passphrase_code': 'passphrased3z9rQJHSyBkNBwTRPkUGNVEVrUAcfAXDyRU1V28ie6hNFbqDwbFBvsTK7yWVK',
                 'encrypted': '6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5mar2ngH',
                 'salt': '\xc4\x0e\xa7\x6f',
                 'seedb': '\x03\xb0\x6a\x1e\xa7\xf9\x21\x9a\xe3\x64\x56\x0d\x7b\x98\x5a\xb1\xfa\x27\x02\x5a\xaa\x7e\x42\x7a',
                 'bitcoin_address': '1Lurmih3KruL4xDB5FmHof38yawNtP9oGf',
                 'unencrypted_wif': '5KMKKuUmAkiNbA3DazMQiLfDq47qs8MAEThm4yL8R2PhV1ov33D',
                 'unencrypted_hex': 'CA2759AA4ADB0F96C414F36ABEB8DB59342985BE9FA50FAAC228C8E7D90E3006',
                 'confirmation_code': 'cfrm38V8G4qq2ywYEFfWLD5Cc6msj9UwsG2Mj4Z6QdGJAFQpdatZLavkgRd1i4iBMdRngDqDs51',
                 'lot': 806938,
                 'sequence': 1,
                } ]

        for v in vec:
            k = CKey()
            k.generate(unhexlify(v['unencrypted_hex']))
            k.set_compressed(False)

            # Test get_secret()
            self.assertEqual(unhexlify(v['unencrypted_hex']), k.get_secret())
            self.assertEqual(v['unencrypted_wif'], k.get_secret(form=CKeyForm.BASE58))

            # Test get_intermediate
            b = Bip38(k, v['passphrase'], ec_multiply = True, ls_numbers = True)
            self.assertEqual(v['passphrase_code'], b.get_intermediate(salt = v['salt'], lot=v['lot'], sequence=v['sequence']))

            # Test encryption
            self.assertEqual(v['encrypted'], b.encrypt_ec_multiply(v['passphrase_code'], seedb=v['seedb']))

            # Test decryption
            self.assertEqual(unhexlify(v['unencrypted_hex']), Bip38.decrypt(v['encrypted'], v['passphrase']))
Exemple #4
0
    def _decrypt_ec_multiply(k_buffer, passphrase):
        if passphrase is None or passphrase == '':
            raise ValueError("Passphrase must not be empty.")

        prefix = k_buffer[0:1]
        flagbyte = k_buffer[1:2]
        ls_numbers = False
        if ''.join(chr(ord(c)&ord(k)) for c,k in izip(flagbyte, '\x04')) == '\x04':
            ls_numbers = True
            ls, = unpack('>I', k_buffer[10:14])
            lot = ls / 4096
            sequence = ls - (lot*4096)
            salt = k_buffer[6:10]
        else:
            salt = k_buffer[6:14]
        
        address_hash = k_buffer[2:6]
        ownerentropy = k_buffer[6:14]

        prefactor = scrypt.hash(passphrase, salt, N=16384, r=8, p=8, buflen=32)
        if ls_numbers is True:
            passfactor = SHA256.new(SHA256.new(prefactor+ownerentropy).digest()).digest()
        else:
            passfactor = prefactor

        passpoint = Bip38._compute_passpoint(passfactor)
        derived = scrypt.hash(passpoint, address_hash + ownerentropy, N=1024, r=1, p=1, buflen=64)
        derived_half1 = derived[:32]
        derived_half2 = derived[32:64]
        cipher = AES.new(derived_half2)

        decrypted_half2 = Bip38.xor_zip(cipher.decrypt(k_buffer[22:22+16]), derived_half1[16:32])
        ep12 = decrypted_half2[0:8]
        decrypted_half1 = Bip38.xor_zip(cipher.decrypt(k_buffer[14:14+8] + ep12), derived_half1[:16])
        seedb = decrypted_half1[0:16] + decrypted_half2[8:16]
        factorb = SHA256.new(SHA256.new(seedb).digest()).digest()

        r = ssl.BN_new()
        pf = ssl.BN_bin2bn(passfactor, 32, ssl.BN_new())
        fb = ssl.BN_bin2bn(factorb, 32, ssl.BN_new())
        NID_secp256k1 = 714
        k = ssl.EC_KEY_new_by_curve_name(NID_secp256k1)
        group = ssl.EC_KEY_get0_group(k)
        ctx = ssl.BN_CTX_new()
        ssl.BN_CTX_start(ctx)
        order = ssl.BN_CTX_get(ctx)
        if ssl.EC_GROUP_get_order(group, order, ctx) == 0:
            raise Exception('Error in EC_GROUP_get_order()')
        ssl.BN_mod_mul(r, pf, fb, order, ctx)
        ssl.BN_CTX_free(ctx) 
        pkey = CKey()
        final =  ctypes.create_string_buffer(32)
        ssl.BN_bn2bin(r, final)
        pkey.generate(secret=final)
        return pkey.get_secret()
Exemple #5
0
    def test_no_compression_ec_multiply_no_lot_sequence_numbers(self):
        vec = [ {'passphrase': 'TestingOneTwoThree',
                 'passphrase_code': 'passphrasepxFy57B9v8HtUsszJYKReoNDV6VHjUSGt8EVJmux9n1J3Ltf1gRxyDGXqnf9qm',
                 'encrypted': '6PfQu77ygVyJLZjfvMLyhLMQbYnu5uguoJJ4kMCLqWwPEdfpwANVS76gTX',
                 'salt': '\xa5\x0d\xba\x67\x72\xcb\x93\x83',
                 'seedb': '\x99\x24\x1d\x58\x24\x5c\x88\x38\x96\xf8\x08\x43\xd2\x84\x66\x72\xd7\x31\x2e\x61\x95\xca\x1a\x6c',
                 'bitboin_address': '1PE6TQi6HTVNz5DLwB1LcpMBALubfuN2z2',
                 'unencrypted_wif': '5K4caxezwjGCGfnoPTZ8tMcJBLB7Jvyjv4xxeacadhq8nLisLR2',
                 'unencrypted_hex': 'A43A940577F4E97F5C4D39EB14FF083A98187C64EA7C99EF7CE460833959A519',
                },
                {'passphrase': 'Satoshi',
                 'passphrase_code': 'passphraseoRDGAXTWzbp72eVbtUDdn1rwpgPUGjNZEc6CGBo8i5EC1FPW8wcnLdq4ThKzAS',
                 'encrypted': '6PfLGnQs6VZnrNpmVKfjotbnQuaJK4KZoPFrAjx1JMJUa1Ft8gnf5WxfKd',
                 'salt': '\x67\x01\x0a\x95\x73\x41\x89\x06',
                 'seedb': '\x49\x11\x1e\x30\x1d\x94\xea\xb3\x39\xff\x9f\x68\x22\xee\x99\xd9\xf4\x96\x06\xdb\x3b\x47\xa4\x97',
                 'bitcoin_address': '1CqzrtZC6mXSAhoxtFwVjz8LtwLJjDYU3V',
                 'unencrypted_wif': '5KJ51SgxWaAYR13zd9ReMhJpwrcX47xTJh2D3fGPG9CM8vkv5sH',
                 'unencrypted_hex': 'C2C8036DF268F498099350718C4A3EF3984D2BE84618C2650F5171DCC5EB660A',
                } ]
 
        for v in vec:
            k = CKey()
            k.generate(unhexlify(v['unencrypted_hex']))
            k.set_compressed(False)

            # Test get_secret()
            self.assertEqual(unhexlify(v['unencrypted_hex']), k.get_secret())
            self.assertEqual(v['unencrypted_wif'], k.get_secret(form=CKeyForm.BASE58))

            # Test get_intermediate
            b = Bip38(k, v['passphrase'], ec_multiply = True)
            self.assertEqual(v['passphrase_code'], b.get_intermediate(salt = v['salt']))

            # Test encryption
            self.assertEqual(v['encrypted'], Bip38.encrypt_ec_multiply(v['passphrase_code'], seedb=v['seedb']))

            # Test decryption
            self.assertEqual(unhexlify(v['unencrypted_hex']), Bip38.decrypt(v['encrypted'], v['passphrase']))
Exemple #6
0
def generate_address():
# def get_addr(k,version = 0):
    k = CKey()
    version = 0
    k.generate()
    k.set_compressed(True)
    pubkey = k.get_pubkey()
    secret = k.get_secret()
    hash160 = rhash(pubkey)
    addr = base58_check_encode(hash160, version)
    # payload = secret
    # if k.compressed:
    #    payload = secret + chr(1)
    pkey = base58_check_encode(payload, 128+version)
    return addr, pkey
Exemple #7
0
def MyHash160(s):
    h = hashlib.new('ripemd160')
    h.update(hashlib.sha256(s).digest())
    return h.digest()


# Generate public and private keys
key = Key()
# key.set_privkey(0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725)
# key.set_pubkey(bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"))
key.generate()
key.set_compressed(True)
private_key = key.get_privkey()
public_key = key.get_pubkey()
secret = key.get_secret()
private_key_hex = private_key.encode('hex')
public_key_hex = public_key.encode('hex')
secret_hex = binascii.hexlify(secret)
print "Private key: ", private_key_hex
print "Public key: ", public_key_hex
print "secret    : ", secret_hex

public_key = bytearray.fromhex(public_key_hex)
# public_key = bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6")

# Perform SHA-256 and RIPEMD-160 hashing on public key
hash160_address = MyHash160(public_key)

# add version byte: 0x00 for Main Network
extended_address = '\x00' + hash160_address
Exemple #8
0
    return hashlib.sha256(hashlib.sha256(s).digest()).digest()

def MyHash160(s):
    h = hashlib.new('ripemd160')
    h.update(hashlib.sha256(s).digest())
    return h.digest()
    
# Generate public and private keys
key = Key()
# key.set_privkey(0x18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725)
# key.set_pubkey(bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6"))
key.generate()
key.set_compressed(True)
private_key = key.get_privkey()
public_key = key.get_pubkey()
secret = key.get_secret()
private_key_hex = private_key.encode('hex')
public_key_hex = public_key.encode('hex')
secret_hex = binascii.hexlify(secret)
print "Private key: ", private_key_hex
print "Public key: ", public_key_hex
print "secret    : ", secret_hex

public_key = bytearray.fromhex(public_key_hex)
# public_key = bytearray.fromhex("0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B23522CD470243453A299FA9E77237716103ABC11A1DF38855ED6F2EE187E9C582BA6")

# Perform SHA-256 and RIPEMD-160 hashing on public key
hash160_address = MyHash160(public_key)

# add version byte: 0x00 for Main Network
extended_address = '\x00' + hash160_address
Exemple #9
0
    def _decrypt_ec_multiply(k_buffer, passphrase):
        if passphrase is None or passphrase == '':
            raise ValueError("Passphrase must not be empty.")

        prefix = k_buffer[0:1]
        flagbyte = k_buffer[1:2]
        ls_numbers = False
        if ''.join(chr(ord(c) & ord(k))
                   for c, k in izip(flagbyte, '\x04')) == '\x04':
            ls_numbers = True
            ls, = unpack('>I', k_buffer[10:14])
            lot = ls / 4096
            sequence = ls - (lot * 4096)
            salt = k_buffer[6:10]
        else:
            salt = k_buffer[6:14]

        address_hash = k_buffer[2:6]
        ownerentropy = k_buffer[6:14]

        prefactor = scrypt.hash(passphrase, salt, N=16384, r=8, p=8, buflen=32)
        if ls_numbers is True:
            passfactor = SHA256.new(
                SHA256.new(prefactor + ownerentropy).digest()).digest()
        else:
            passfactor = prefactor

        passpoint = Bip38._compute_passpoint(passfactor)
        derived = scrypt.hash(passpoint,
                              address_hash + ownerentropy,
                              N=1024,
                              r=1,
                              p=1,
                              buflen=64)
        derived_half1 = derived[:32]
        derived_half2 = derived[32:64]
        cipher = AES.new(derived_half2)

        decrypted_half2 = Bip38.xor_zip(cipher.decrypt(k_buffer[22:22 + 16]),
                                        derived_half1[16:32])
        ep12 = decrypted_half2[0:8]
        decrypted_half1 = Bip38.xor_zip(
            cipher.decrypt(k_buffer[14:14 + 8] + ep12), derived_half1[:16])
        seedb = decrypted_half1[0:16] + decrypted_half2[8:16]
        factorb = SHA256.new(SHA256.new(seedb).digest()).digest()

        r = ssl.BN_new()
        pf = ssl.BN_bin2bn(passfactor, 32, ssl.BN_new())
        fb = ssl.BN_bin2bn(factorb, 32, ssl.BN_new())
        NID_secp256k1 = 714
        k = ssl.EC_KEY_new_by_curve_name(NID_secp256k1)
        group = ssl.EC_KEY_get0_group(k)
        ctx = ssl.BN_CTX_new()
        ssl.BN_CTX_start(ctx)
        order = ssl.BN_CTX_get(ctx)
        if ssl.EC_GROUP_get_order(group, order, ctx) == 0:
            raise Exception('Error in EC_GROUP_get_order()')
        ssl.BN_mod_mul(r, pf, fb, order, ctx)
        ssl.BN_CTX_free(ctx)
        pkey = CKey()
        final = ctypes.create_string_buffer(32)
        ssl.BN_bn2bin(r, final)
        pkey.generate(secret=final)
        return pkey.get_secret()
    def test_no_compression_ec_multiply_no_lot_sequence_numbers(self):
        vec = [{
            'passphrase':
            'TestingOneTwoThree',
            'passphrase_code':
            'passphrasepxFy57B9v8HtUsszJYKReoNDV6VHjUSGt8EVJmux9n1J3Ltf1gRxyDGXqnf9qm',
            'encrypted':
            '6PfQu77ygVyJLZjfvMLyhLMQbYnu5uguoJJ4kMCLqWwPEdfpwANVS76gTX',
            'salt':
            '\xa5\x0d\xba\x67\x72\xcb\x93\x83',
            'seedb':
            '\x99\x24\x1d\x58\x24\x5c\x88\x38\x96\xf8\x08\x43\xd2\x84\x66\x72\xd7\x31\x2e\x61\x95\xca\x1a\x6c',
            'bitboin_address':
            '1PE6TQi6HTVNz5DLwB1LcpMBALubfuN2z2',
            'unencrypted_wif':
            '5K4caxezwjGCGfnoPTZ8tMcJBLB7Jvyjv4xxeacadhq8nLisLR2',
            'unencrypted_hex':
            'A43A940577F4E97F5C4D39EB14FF083A98187C64EA7C99EF7CE460833959A519',
        }, {
            'passphrase':
            'Satoshi',
            'passphrase_code':
            'passphraseoRDGAXTWzbp72eVbtUDdn1rwpgPUGjNZEc6CGBo8i5EC1FPW8wcnLdq4ThKzAS',
            'encrypted':
            '6PfLGnQs6VZnrNpmVKfjotbnQuaJK4KZoPFrAjx1JMJUa1Ft8gnf5WxfKd',
            'salt':
            '\x67\x01\x0a\x95\x73\x41\x89\x06',
            'seedb':
            '\x49\x11\x1e\x30\x1d\x94\xea\xb3\x39\xff\x9f\x68\x22\xee\x99\xd9\xf4\x96\x06\xdb\x3b\x47\xa4\x97',
            'bitcoin_address':
            '1CqzrtZC6mXSAhoxtFwVjz8LtwLJjDYU3V',
            'unencrypted_wif':
            '5KJ51SgxWaAYR13zd9ReMhJpwrcX47xTJh2D3fGPG9CM8vkv5sH',
            'unencrypted_hex':
            'C2C8036DF268F498099350718C4A3EF3984D2BE84618C2650F5171DCC5EB660A',
        }]

        for v in vec:
            k = CKey()
            k.generate(unhexlify(v['unencrypted_hex']))
            k.set_compressed(False)

            # Test get_secret()
            self.assertEqual(unhexlify(v['unencrypted_hex']), k.get_secret())
            self.assertEqual(v['unencrypted_wif'],
                             k.get_secret(form=CKeyForm.BASE58))

            # Test get_intermediate
            b = Bip38(k, v['passphrase'], ec_multiply=True)
            self.assertEqual(v['passphrase_code'],
                             b.get_intermediate(salt=v['salt']))

            # Test encryption
            self.assertEqual(
                v['encrypted'],
                Bip38.encrypt_ec_multiply(v['passphrase_code'],
                                          seedb=v['seedb']))

            # Test decryption
            self.assertEqual(unhexlify(v['unencrypted_hex']),
                             Bip38.decrypt(v['encrypted'], v['passphrase']))
    def test_no_compression_ec_multiply_lot_sequence_numbers(self):
        vec = [{
            'passphrase': 'MOLON LABE',
            'passphrase_code':
            'passphraseaB8feaLQDENqCgr4gKZpmf4VoaT6qdjJNJiv7fsKvjqavcJxvuR1hy25aTu5sX',
            'encrypted':
            '6PgNBNNzDkKdhkT6uJntUXwwzQV8Rr2tZcbkDcuC9DZRsS6AtHts4Ypo1j',
            'salt': '\x4f\xca\x5a\x97',
            'seedb':
            '\x87\xa1\x3b\x07\x85\x8f\xa7\x53\xcd\x3a\xb3\xf1\xc5\xea\xfb\x5f\x12\x57\x9b\x6c\x33\xc9\xa5\x3f',
            'bitcoin_address': '1Jscj8ALrYu2y9TD8NrpvDBugPedmbj4Yh',
            'unencrypted_wif':
            '5JLdxTtcTHcfYcmJsNVy1v2PMDx432JPoYcBTVVRHpPaxUrdtf8',
            'unencrypted_hex':
            '44EA95AFBF138356A05EA32110DFD627232D0F2991AD221187BE356F19FA8190',
            'confirmation_code':
            'cfrm38V8aXBn7JWA1ESmFMUn6erxeBGZGAxJPY4e36S9QWkzZKtaVqLNMgnifETYw7BPwWC9aPD',
            'lot': 263183,
            'sequence': 1,
        }, {
            'passphrase': 'ΜΟΛΩΝ ΛΑΒΕ',
            'passphrase_code':
            'passphrased3z9rQJHSyBkNBwTRPkUGNVEVrUAcfAXDyRU1V28ie6hNFbqDwbFBvsTK7yWVK',
            'encrypted':
            '6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5mar2ngH',
            'salt': '\xc4\x0e\xa7\x6f',
            'seedb':
            '\x03\xb0\x6a\x1e\xa7\xf9\x21\x9a\xe3\x64\x56\x0d\x7b\x98\x5a\xb1\xfa\x27\x02\x5a\xaa\x7e\x42\x7a',
            'bitcoin_address': '1Lurmih3KruL4xDB5FmHof38yawNtP9oGf',
            'unencrypted_wif':
            '5KMKKuUmAkiNbA3DazMQiLfDq47qs8MAEThm4yL8R2PhV1ov33D',
            'unencrypted_hex':
            'CA2759AA4ADB0F96C414F36ABEB8DB59342985BE9FA50FAAC228C8E7D90E3006',
            'confirmation_code':
            'cfrm38V8G4qq2ywYEFfWLD5Cc6msj9UwsG2Mj4Z6QdGJAFQpdatZLavkgRd1i4iBMdRngDqDs51',
            'lot': 806938,
            'sequence': 1,
        }]

        for v in vec:
            k = CKey()
            k.generate(unhexlify(v['unencrypted_hex']))
            k.set_compressed(False)

            # Test get_secret()
            self.assertEqual(unhexlify(v['unencrypted_hex']), k.get_secret())
            self.assertEqual(v['unencrypted_wif'],
                             k.get_secret(form=CKeyForm.BASE58))

            # Test get_intermediate
            b = Bip38(k, v['passphrase'], ec_multiply=True, ls_numbers=True)
            self.assertEqual(
                v['passphrase_code'],
                b.get_intermediate(salt=v['salt'],
                                   lot=v['lot'],
                                   sequence=v['sequence']))

            # Test encryption
            self.assertEqual(
                v['encrypted'],
                b.encrypt_ec_multiply(v['passphrase_code'], seedb=v['seedb']))

            # Test decryption
            self.assertEqual(unhexlify(v['unencrypted_hex']),
                             Bip38.decrypt(v['encrypted'], v['passphrase']))