1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-17 05:03:07 +00:00
trezor-firmware/src/apps/wallet/cipher_key_value.py

44 lines
1.2 KiB
Python
Raw Normal View History

2016-12-08 15:18:12 +00:00
from trezor import ui
2016-11-18 13:59:36 +00:00
2016-12-12 14:19:51 +00:00
def cipher_key_value(msg, seckey: bytes) -> bytes:
2016-11-18 13:59:36 +00:00
from trezor.crypto.hashlib import sha512
from trezor.crypto import hmac
from trezor.crypto.aes import AES_CBC_Encrypt, AES_CBC_Decrypt
data = msg.key
data += 'E1' if msg.ask_on_encrypt else 'E0'
data += 'D1' if msg.ask_on_decrypt else 'D0'
data = hmac.new(seckey, data, sha512).digest()
key = data[:32]
if msg.iv and len(msg.iv) == 16:
iv = msg.iv
else:
iv = data[32:48]
if msg.encrypt:
aes = AES_CBC_Encrypt(key=key, iv=iv)
else:
aes = AES_CBC_Decrypt(key=key, iv=iv)
2016-12-12 14:19:51 +00:00
return aes.update(msg.value)
2017-08-15 13:09:09 +00:00
async def layout_cipher_key_value(ctx, msg):
2016-12-12 14:19:51 +00:00
from trezor.messages.CipheredKeyValue import CipheredKeyValue
from ..common import seed
if len(msg.value) % 16 > 0:
raise ValueError('Value length must be a multiple of 16')
ui.display.clear()
ui.display.text(10, 30, 'CipherKeyValue',
2017-09-26 10:05:05 +00:00
ui.BOLD, ui.LIGHT_GREEN, ui.BG)
ui.display.text(10, 60, msg.key, ui.MONO, ui.FG, ui.BG)
2016-12-12 14:19:51 +00:00
node = await seed.derive_node(ctx, msg.address_n)
2016-12-12 14:19:51 +00:00
value = cipher_key_value(msg, node.private_key())
2016-11-18 13:59:36 +00:00
return CipheredKeyValue(value=value)