示例#1
0
    def _get_entropy(self, size):
        random.seed()
        m = proto.Entropy()
        d = ''
        while len(d) < size:
            d += tools.get_local_entropy()

        m.entropy = d[:size]
        self.set_main_state()
        return m
示例#2
0
    def step1(self, display_random, strength, passphrase_protection, pin_protection, language, label):
        '''This starts resetting workflow by generating internal random
        and asking user to confirm device reset.'''
        
        if self.storage.is_initialized():
            return proto.Failure(message="Device is initialized already.")

        self.set_main_state()
        
        print "Starting device reset..."
        internal_entropy = tools.get_local_entropy()
        print "Trezor-generated entropy:", binascii.hexlify(internal_entropy)
        
        msg = []
        if display_random:
            msg += ["_cLocal entropy is", ]
            ent = binascii.hexlify(internal_entropy)
            while ent:
                msg += ["_c%s" % ent[:16], ]
                ent = ent[16:]

        def entropy_request():
            '''This is called after user confirmation of the action.
            Internal random is already generated, lets respond to computer with EntropyRequest
            and wait for EntropyAck'''
            if language not in self.storage.get_languages():
                raise Exception("Unsupported language")

            self.internal_entropy = internal_entropy
            self.external_entropy = None
            self.strength = strength
            self.passphrase_protection = passphrase_protection
            self.pin_protection = pin_protection
            self.label = label            
            self.language = language

            return proto.EntropyRequest()
        
        self.layout.show_question(msg, 'Setup device?', 'Next }', '{ Cancel')
        return self.yesno.request(proto_types.ButtonRequest_ResetDevice, entropy_request)
示例#3
0
def encrypt_message(pubkey, message, display_only, bip32, coin, address_n):

    if len(address_n) > 0:
        priv_node = bip32.get_private_node(address_n)
        priv_key = tools.EcKey(priv_node.private_key)
        signing = True
    else:
        signing = False

    if signing:
        if display_only:
            payload = chr(0x80 + 1)
        else:
            payload = chr(1)
        address, signature = sign_message(bip32, coin, address_n, message)
        address_bin = tools.bc_address_decode(address)[:21]
        payload += tools.ser_length(len(message)) + message + address_bin + signature
    else:
        if display_only:
            payload = chr(0x80)
        else:
            payload = chr(0)
        payload += tools.ser_length(len(message)) + message

    nonce = tools.get_local_entropy()
    nonce_key = tools.EcKey(nonce)
    nonce_pub = binascii.unhexlify(nonce_key.get_public_key(True))
    dest_pub = tools.public_key_to_point(pubkey)
    shared_secret_point = dest_pub * nonce_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:]
    encrypter = pyaes.Encrypter(pyaes.AESModeOfOperationCFB(key=aes_key, iv=aes_iv, segment_size=16))
    payload = encrypter.feed(payload) + encrypter.feed()
    msg_hmac = hmac.HMAC(key=hmac_key, msg=payload, digestmod=sha256).digest()[:8]
    return (nonce_pub, payload, msg_hmac)