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

app.lisk: restore sign and verify functions with correct message digest (with prefix)

This commit is contained in:
Aleksey Popov 2018-06-06 23:53:15 +03:00
parent faccce5bd1
commit caffef2e0a
No known key found for this signature in database
GPG Key ID: 4ABD4AD9EEFF0131
8 changed files with 136 additions and 1 deletions

View File

@ -1,6 +1,6 @@
from trezor.wire import register, protobuf_workflow
from trezor.messages.wire_types import \
LiskGetAddress, LiskSignTx, LiskGetPublicKey
LiskGetAddress, LiskSignTx, LiskGetPublicKey, LiskSignMessage, LiskVerifyMessage
def dispatch_LiskGetAddress(*args, **kwargs):
@ -18,7 +18,19 @@ def dispatch_LiskSignTx(*args, **kwargs):
return lisk_sign_tx(*args, **kwargs)
def dispatch_LiskSignMessage(*args, **kwargs):
from .sign_message import lisk_sign_message
return lisk_sign_message(*args, **kwargs)
def dispatch_LiskVerifyMessage(*args, **kwargs):
from .verify_message import lisk_verify_message
return lisk_verify_message(*args, **kwargs)
def boot():
register(LiskGetPublicKey, protobuf_workflow, dispatch_LiskGetPublicKey)
register(LiskGetAddress, protobuf_workflow, dispatch_LiskGetAddress)
register(LiskSignMessage, protobuf_workflow, dispatch_LiskSignMessage)
register(LiskVerifyMessage, protobuf_workflow, dispatch_LiskVerifyMessage)
register(LiskSignTx, protobuf_workflow, dispatch_LiskSignTx)

View File

@ -0,0 +1,35 @@
from .helpers import LISK_CURVE, get_address_from_public_key
from apps.wallet.sign_message import require_confirm_sign_message
from trezor.crypto.hashlib import sha256
from trezor.utils import HashWriter
from apps.wallet.sign_tx.signing import write_varint
def message_digest(message):
h = HashWriter(sha256)
signed_message_header = 'Lisk Signed Message:\n'
write_varint(h, len(signed_message_header))
h.extend(signed_message_header)
write_varint(h, len(message))
h.extend(message)
return sha256(h.get_digest()).digest()
async def lisk_sign_message(ctx, msg):
from trezor.messages.LiskMessageSignature import LiskMessageSignature
from trezor.crypto.curve import ed25519
from ..common import seed
message = msg.message
await require_confirm_sign_message(ctx, message)
address_n = msg.address_n or ()
node = await seed.derive_node(ctx, address_n, LISK_CURVE)
seckey = node.private_key()
pubkey = node.public_key()
pubkey = pubkey[1:] # skip ed25519 pubkey marker
signature = ed25519.sign(seckey, message_digest(message))
return LiskMessageSignature(public_key=pubkey, signature=signature)

View File

@ -0,0 +1,20 @@
async def lisk_verify_message(ctx, msg):
from trezor.crypto.curve import ed25519
from .helpers import get_address_from_public_key
from .sign_message import message_digest
from trezor import wire
from trezor.messages.Success import Success
from trezor.messages.FailureType import ProcessError
from apps.wallet.verify_message import require_confirm_verify_message
verify = ed25519.verify(msg.public_key, msg.signature, message_digest(msg.message))
if not verify:
raise wire.ProcessError('Invalid signature')
address = get_address_from_public_key(msg.public_key)
await require_confirm_verify_message(ctx, address, msg.message)
return Success(message='Message verified')

View File

@ -0,0 +1,18 @@
# Automatically generated by pb2py
import protobuf as p
class LiskMessageSignature(p.MessageType):
MESSAGE_WIRE_TYPE = 119
FIELDS = {
1: ('public_key', p.BytesType, 0),
2: ('signature', p.BytesType, 0),
}
def __init__(
self,
public_key: bytes = None,
signature: bytes = None
) -> None:
self.public_key = public_key
self.signature = signature

View File

@ -0,0 +1,23 @@
# Automatically generated by pb2py
import protobuf as p
if __debug__:
try:
from typing import List
except ImportError:
List = None
class LiskSignMessage(p.MessageType):
MESSAGE_WIRE_TYPE = 118
FIELDS = {
1: ('address_n', p.UVarintType, p.FLAG_REPEATED),
2: ('message', p.BytesType, 0),
}
def __init__(
self,
address_n: List[int] = None,
message: bytes = None
) -> None:
self.address_n = address_n if address_n is not None else []
self.message = message

View File

@ -0,0 +1,21 @@
# Automatically generated by pb2py
import protobuf as p
class LiskVerifyMessage(p.MessageType):
MESSAGE_WIRE_TYPE = 120
FIELDS = {
1: ('signature', p.BytesType, 0),
2: ('public_key', p.BytesType, 0),
3: ('message', p.BytesType, 0),
}
def __init__(
self,
signature: bytes = None,
public_key: bytes = None,
message: bytes = None
) -> None:
self.signature = signature
self.public_key = public_key
self.message = message

View File

@ -88,6 +88,9 @@ LiskGetAddress = 114
LiskAddress = 115
LiskSignTx = 116
LiskSignedTx = 117
LiskSignMessage = 118
LiskMessageSignature = 119
LiskVerifyMessage = 120
LiskGetPublicKey = 121
LiskPublicKey = 122
StellarGetPublicKey = 200

View File

@ -88,6 +88,9 @@ LiskGetAddress = 114
LiskAddress = 115
LiskSignTx = 116
LiskSignedTx = 117
LiskSignMessage = 118
LiskMessageSignature = 119
LiskVerifyMessage = 120
LiskGetPublicKey = 121
LiskPublicKey = 122
StellarGetPublicKey = 200