2016-12-08 15:18:12 +00:00
|
|
|
from trezor import ui
|
2016-11-18 13:59:36 +00:00
|
|
|
from trezor.utils import unimport
|
|
|
|
|
|
|
|
|
|
|
|
@unimport
|
2016-12-08 15:18:12 +00:00
|
|
|
async def layout_cipher_key_value(session_id, msg):
|
2016-11-18 13:59:36 +00:00
|
|
|
from trezor.messages.CipheredKeyValue import CipheredKeyValue
|
2016-12-08 15:18:12 +00:00
|
|
|
from ..common import seed
|
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
|
|
|
|
|
|
|
|
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',
|
|
|
|
ui.BOLD, ui.LIGHT_GREEN, ui.BLACK)
|
|
|
|
ui.display.text(10, 60, msg.key, ui.MONO, ui.WHITE, ui.BLACK)
|
|
|
|
|
2016-12-08 15:18:12 +00:00
|
|
|
node = await seed.get_node(session_id, msg.address_n)
|
2016-11-18 13:59:36 +00:00
|
|
|
seckey = node.private_key()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
value = aes.update(msg.value)
|
|
|
|
|
|
|
|
return CipheredKeyValue(value=value)
|