def decrypt_message(bip32, address_n, nonce_pub, payload, msg_hmac): priv_node = bip32.get_private_node(address_n) priv_key = tools.EcKey(priv_node.private_key) shared_secret_point = tools.public_key_to_point(nonce_pub) * priv_key.privkey.secret_multiplier shared_secret = tools.point_to_public_key(shared_secret_point, True) keying_bytes = PBKDF2(shared_secret, "Bitcoin Secure Message" + nonce_pub, iterations=2048, macmodule=hmac, digestmodule=sha256).read(80) aes_key = keying_bytes[:32] hmac_key = keying_bytes[32:64] aes_iv = keying_bytes[64:] msg_hmac_new = hmac.HMAC(key=hmac_key, msg=payload, digestmod=sha256).digest()[:8] if msg_hmac_new != msg_hmac: raise Exception('Message_HMAC does not match') decrypter = pyaes.Decrypter(pyaes.AESModeOfOperationCFB(key=aes_key, iv=aes_iv, segment_size=16)) payload = decrypter.feed(payload) + decrypter.feed() if not ord(payload[0]) in [0x00, 0x01, 0x80, 0x81]: raise Exception('AES decryption failed') signing = (ord(payload[0]) & 0x01) > 0 display_only = (ord(payload[0]) & 0x80) > 0 if signing: message = tools.deser_length_string(payload[1:-(21+65)]) address_bin = payload[-(21+65):-65] signature = payload[-65:] address = tools.hash_160_to_bc_address(address_bin[1:], ord(address_bin[0])) verify_message(address, signature, message) else: message = tools.deser_length_string(payload[1:]) address = None return (message, address, display_only)
def _get_address(self, coin, address_n, multisig): if multisig: # check if we own the pubkey pubkey = BIP32(self.storage.get_node()).get_public_node(address_n).public_key try: pubkeys = [ public_ckd(n.node, list(n.address_n)).public_key for n in multisig.pubkeys ] sig_index = list(pubkeys).index(pubkey) except ValueError: return proto.Failure(code=proto_types.Failure_Other, message="Pubkey not found in multisig script") # convert script to P2SH address script = transaction.compile_script_multisig(multisig) h160 = tools.hash_160(script) address = tools.hash_160_to_bc_address(h160, coin.address_type_p2sh) else: address = BIP32(self.storage.get_node()).get_address(coin, address_n) self.layout.show_receiving_address(address) self.custom_message = True # Yes button will redraw screen return proto.Address(address=address)