2016-11-25 21:53:55 +00:00
|
|
|
# This file is part of the TREZOR project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2016 Marek Palatinus <slush@satoshilabs.com>
|
|
|
|
# Copyright (C) 2012-2016 Pavol Rusnak <stick@satoshilabs.com>
|
|
|
|
# Copyright (C) 2016 Jochen Hoenicke <hoenicke@gmail.com>
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2013-12-16 15:03:38 +00:00
|
|
|
import struct
|
|
|
|
import hmac
|
|
|
|
import hashlib
|
2016-06-27 21:17:20 +00:00
|
|
|
import sys
|
2013-12-16 15:03:38 +00:00
|
|
|
|
2014-01-14 13:29:18 +00:00
|
|
|
import ecdsa
|
|
|
|
from ecdsa.util import string_to_number, number_to_string
|
|
|
|
from ecdsa.curves import SECP256k1
|
|
|
|
from ecdsa.ellipticcurve import Point, INFINITY
|
2013-12-16 15:03:38 +00:00
|
|
|
|
2018-04-18 11:47:20 +00:00
|
|
|
from trezorlib import tools
|
|
|
|
from trezorlib import messages
|
2014-01-13 03:44:57 +00:00
|
|
|
|
2016-06-27 21:17:20 +00:00
|
|
|
|
2014-01-14 13:29:18 +00:00
|
|
|
def point_to_pubkey(point):
|
|
|
|
order = SECP256k1.order
|
|
|
|
x_str = number_to_string(point.x(), order)
|
|
|
|
y_str = number_to_string(point.y(), order)
|
|
|
|
vk = x_str + y_str
|
2018-02-27 15:30:32 +00:00
|
|
|
return struct.pack('B', (vk[63] & 1) + 2) + vk[0:32] # To compressed key
|
2014-01-14 13:29:18 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-01-14 13:29:18 +00:00
|
|
|
def sec_to_public_pair(pubkey):
|
|
|
|
"""Convert a public key in sec binary format to a public pair."""
|
|
|
|
x = string_to_number(pubkey[1:33])
|
|
|
|
sec0 = pubkey[:1]
|
|
|
|
if sec0 not in (b'\2', b'\3'):
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Compressed pubkey expected")
|
2014-01-14 13:29:18 +00:00
|
|
|
|
|
|
|
def public_pair_for_x(generator, x, is_even):
|
|
|
|
curve = generator.curve()
|
|
|
|
p = curve.p()
|
|
|
|
alpha = (pow(x, 3, p) + curve.a() * x + curve.b()) % p
|
2014-02-07 00:47:55 +00:00
|
|
|
beta = ecdsa.numbertheory.square_root_mod_prime(alpha, p)
|
2014-01-14 13:29:18 +00:00
|
|
|
if is_even == bool(beta & 1):
|
|
|
|
return (x, p - beta)
|
|
|
|
return (x, beta)
|
|
|
|
|
|
|
|
return public_pair_for_x(ecdsa.ecdsa.generator_secp256k1, x, is_even=(sec0 == b'\2'))
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2013-12-16 15:03:38 +00:00
|
|
|
def is_prime(n):
|
2018-04-18 13:00:59 +00:00
|
|
|
return bool(n & tools.HARDENED_FLAG)
|
2013-12-16 15:03:38 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2013-12-16 15:03:38 +00:00
|
|
|
def fingerprint(pubkey):
|
2014-01-14 13:29:18 +00:00
|
|
|
return string_to_number(tools.hash_160(pubkey)[:4])
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-01-14 13:29:18 +00:00
|
|
|
def get_address(public_node, address_type):
|
|
|
|
return tools.public_key_to_bc_address(public_node.public_key, address_type)
|
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-01-14 13:29:18 +00:00
|
|
|
def public_ckd(public_node, n):
|
|
|
|
if not isinstance(n, list):
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError('Parameter must be a list')
|
2014-01-14 13:29:18 +00:00
|
|
|
|
2018-04-18 11:47:20 +00:00
|
|
|
node = messages.HDNodeType()
|
2014-01-14 13:29:18 +00:00
|
|
|
node.CopyFrom(public_node)
|
|
|
|
|
|
|
|
for i in n:
|
|
|
|
node.CopyFrom(get_subnode(node, i))
|
|
|
|
|
|
|
|
return node
|
2013-12-16 15:03:38 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2013-12-16 15:03:38 +00:00
|
|
|
def get_subnode(node, i):
|
|
|
|
# Public Child key derivation (CKD) algorithm of BIP32
|
|
|
|
i_as_bytes = struct.pack(">L", i)
|
|
|
|
|
|
|
|
if is_prime(i):
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Prime derivation not supported")
|
2013-12-16 15:03:38 +00:00
|
|
|
|
|
|
|
# Public derivation
|
|
|
|
data = node.public_key + i_as_bytes
|
|
|
|
|
|
|
|
I64 = hmac.HMAC(key=node.chain_code, msg=data, digestmod=hashlib.sha512).digest()
|
|
|
|
I_left_as_exponent = string_to_number(I64[:32])
|
|
|
|
|
2018-04-18 11:47:20 +00:00
|
|
|
node_out = messages.HDNodeType()
|
2013-12-16 15:03:38 +00:00
|
|
|
node_out.depth = node.depth + 1
|
|
|
|
node_out.child_num = i
|
|
|
|
node_out.chain_code = I64[32:]
|
|
|
|
node_out.fingerprint = fingerprint(node.public_key)
|
|
|
|
|
2014-01-14 13:29:18 +00:00
|
|
|
# BIP32 magic converts old public key to new public point
|
|
|
|
x, y = sec_to_public_pair(node.public_key)
|
2017-06-23 19:31:42 +00:00
|
|
|
point = I_left_as_exponent * SECP256k1.generator + Point(SECP256k1.curve, x, y, SECP256k1.order)
|
2014-01-14 13:29:18 +00:00
|
|
|
|
|
|
|
if point == INFINITY:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Point cannot be INFINITY")
|
2014-01-14 13:29:18 +00:00
|
|
|
|
|
|
|
# Convert public point to compressed public key
|
|
|
|
node_out.public_key = point_to_pubkey(point)
|
2013-12-16 15:03:38 +00:00
|
|
|
|
|
|
|
return node_out
|
2014-02-21 17:56:51 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-02-21 21:15:48 +00:00
|
|
|
def serialize(node, version=0x0488B21E):
|
2016-06-27 21:17:20 +00:00
|
|
|
s = b''
|
2014-02-21 21:15:48 +00:00
|
|
|
s += struct.pack('>I', version)
|
2014-02-21 17:56:51 +00:00
|
|
|
s += struct.pack('>B', node.depth)
|
|
|
|
s += struct.pack('>I', node.fingerprint)
|
|
|
|
s += struct.pack('>I', node.child_num)
|
|
|
|
s += node.chain_code
|
|
|
|
if node.private_key:
|
2016-06-27 21:17:20 +00:00
|
|
|
s += b'\x00' + node.private_key
|
2016-05-26 15:20:44 +00:00
|
|
|
else:
|
2014-02-21 17:56:51 +00:00
|
|
|
s += node.public_key
|
2018-04-20 15:23:43 +00:00
|
|
|
s += tools.btc_hash(s)[:4]
|
2014-02-21 17:56:51 +00:00
|
|
|
return tools.b58encode(s)
|
2014-12-12 21:19:30 +00:00
|
|
|
|
2017-06-23 19:31:42 +00:00
|
|
|
|
2014-12-12 21:19:30 +00:00
|
|
|
def deserialize(xpub):
|
|
|
|
data = tools.b58decode(xpub, None)
|
|
|
|
|
2018-04-20 15:23:43 +00:00
|
|
|
if tools.btc_hash(data[:-4])[:4] != data[-4:]:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Checksum failed")
|
2014-12-12 21:19:30 +00:00
|
|
|
|
2018-04-18 11:47:20 +00:00
|
|
|
node = messages.HDNodeType()
|
2014-12-12 21:19:30 +00:00
|
|
|
node.depth = struct.unpack('>B', data[4:5])[0]
|
|
|
|
node.fingerprint = struct.unpack('>I', data[5:9])[0]
|
|
|
|
node.child_num = struct.unpack('>I', data[9:13])[0]
|
|
|
|
node.chain_code = data[13:45]
|
2016-01-12 23:17:38 +00:00
|
|
|
|
2014-12-12 21:19:30 +00:00
|
|
|
key = data[45:-4]
|
2018-02-27 15:30:32 +00:00
|
|
|
if key[0] == 0:
|
2014-12-12 21:19:30 +00:00
|
|
|
node.private_key = key[1:]
|
|
|
|
else:
|
|
|
|
node.public_key = key
|
|
|
|
|
|
|
|
return node
|