1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

apps.wallet: add CipherKeyValue

This commit is contained in:
Pavol Rusnak 2016-11-18 14:59:36 +01:00
parent 8618248504
commit 6e79da8df1
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
2 changed files with 50 additions and 1 deletions

View File

@ -3,7 +3,8 @@ from trezor.utils import unimport
from trezor.messages.wire_types import \
GetPublicKey, GetAddress, SignTx, EstimateTxSize, \
SignMessage, VerifyMessage, \
SignIdentity
SignIdentity, \
CipherKeyValue
@unimport
@ -51,6 +52,12 @@ def dispatch_SignIdentity(*args, **kwargs):
return layout_sign_identity(*args, **kwargs)
@unimport
def dispatch_CipherKeyValue(*args, **kwargs):
from .layout_cipherkeyvalue import layout_cipherkeyvalue
return layout_cipherkeyvalue(*args, **kwargs)
def boot():
register_type(GetPublicKey, protobuf_handler, dispatch_GetPublicKey)
register_type(GetAddress, protobuf_handler, dispatch_GetAddress)
@ -59,3 +66,4 @@ def boot():
register_type(SignMessage, protobuf_handler, dispatch_SignMessage)
register_type(VerifyMessage, protobuf_handler, dispatch_VerifyMessage)
register_type(SignIdentity, protobuf_handler, dispatch_SignIdentity)
register_type(CipherKeyValue, protobuf_handler, dispatch_CipherKeyValue)

View File

@ -0,0 +1,41 @@
from trezor import wire, ui
from trezor.utils import unimport
@unimport
async def layout_cipherkeyvalue(msg, session_id):
from trezor.messages.CipheredKeyValue import CipheredKeyValue
from ..common.seed import get_node
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)
node = await get_node(session_id, msg.address_n)
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)