2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# Copyright (C) 2012-2018 SatoshiLabs and contributors
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
2018-06-21 14:28:34 +00:00
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
2018-06-21 14:28:34 +00:00
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
from binascii import hexlify, unhexlify
|
|
|
|
import pytest
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2018-05-11 12:53:51 +00:00
|
|
|
from .common import TrezorTest
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
|
2017-12-23 20:20:49 +00:00
|
|
|
class TestMsgCipherkeyvalue(TrezorTest):
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
def test_encrypt(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
|
|
|
|
|
|
|
# different ask values
|
2016-06-27 21:17:20 +00:00
|
|
|
res = self.client.encrypt_keyvalue([0, 1, 2], b"test", b"testing message!", ask_on_encrypt=True, ask_on_decrypt=True)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert hexlify(res) == b'676faf8f13272af601776bc31bc14e8f'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
2016-06-27 21:17:20 +00:00
|
|
|
res = self.client.encrypt_keyvalue([0, 1, 2], b"test", b"testing message!", ask_on_encrypt=True, ask_on_decrypt=False)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert hexlify(res) == b'5aa0fbcb9d7fa669880745479d80c622'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
2016-06-27 21:17:20 +00:00
|
|
|
res = self.client.encrypt_keyvalue([0, 1, 2], b"test", b"testing message!", ask_on_encrypt=False, ask_on_decrypt=True)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert hexlify(res) == b'958d4f63269b61044aaedc900c8d6208'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
2016-06-27 21:17:20 +00:00
|
|
|
res = self.client.encrypt_keyvalue([0, 1, 2], b"test", b"testing message!", ask_on_encrypt=False, ask_on_decrypt=False)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert hexlify(res) == b'e0cf0eb0425947000eb546cc3994bc6c'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
# different key
|
2016-06-27 21:17:20 +00:00
|
|
|
res = self.client.encrypt_keyvalue([0, 1, 2], b"test2", b"testing message!", ask_on_encrypt=True, ask_on_decrypt=True)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert hexlify(res) == b'de247a6aa6be77a134bb3f3f925f13af'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
# different message
|
2016-06-27 21:17:20 +00:00
|
|
|
res = self.client.encrypt_keyvalue([0, 1, 2], b"test", b"testing message! it is different", ask_on_encrypt=True, ask_on_decrypt=True)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert hexlify(res) == b'676faf8f13272af601776bc31bc14e8f3ae1c88536bf18f1b44f1e4c2c4a613d'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
# different path
|
2016-06-27 21:17:20 +00:00
|
|
|
res = self.client.encrypt_keyvalue([0, 1, 3], b"test", b"testing message!", ask_on_encrypt=True, ask_on_decrypt=True)
|
2017-12-23 20:20:49 +00:00
|
|
|
assert hexlify(res) == b'b4811a9d492f5355a5186ddbfccaae7b'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
def test_decrypt(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
|
|
|
|
|
|
|
# different ask values
|
2017-12-23 20:20:49 +00:00
|
|
|
res = self.client.decrypt_keyvalue([0, 1, 2], b"test", unhexlify("676faf8f13272af601776bc31bc14e8f"), ask_on_encrypt=True, ask_on_decrypt=True)
|
|
|
|
assert res == b'testing message!'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
2017-12-23 20:20:49 +00:00
|
|
|
res = self.client.decrypt_keyvalue([0, 1, 2], b"test", unhexlify("5aa0fbcb9d7fa669880745479d80c622"), ask_on_encrypt=True, ask_on_decrypt=False)
|
|
|
|
assert res == b'testing message!'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
2017-12-23 20:20:49 +00:00
|
|
|
res = self.client.decrypt_keyvalue([0, 1, 2], b"test", unhexlify("958d4f63269b61044aaedc900c8d6208"), ask_on_encrypt=False, ask_on_decrypt=True)
|
|
|
|
assert res == b'testing message!'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
2017-12-23 20:20:49 +00:00
|
|
|
res = self.client.decrypt_keyvalue([0, 1, 2], b"test", unhexlify("e0cf0eb0425947000eb546cc3994bc6c"), ask_on_encrypt=False, ask_on_decrypt=False)
|
|
|
|
assert res == b'testing message!'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
# different key
|
2017-12-23 20:20:49 +00:00
|
|
|
res = self.client.decrypt_keyvalue([0, 1, 2], b"test2", unhexlify("de247a6aa6be77a134bb3f3f925f13af"), ask_on_encrypt=True, ask_on_decrypt=True)
|
|
|
|
assert res == b'testing message!'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
# different message
|
2017-12-23 20:20:49 +00:00
|
|
|
res = self.client.decrypt_keyvalue([0, 1, 2], b"test", unhexlify("676faf8f13272af601776bc31bc14e8f3ae1c88536bf18f1b44f1e4c2c4a613d"), ask_on_encrypt=True, ask_on_decrypt=True)
|
|
|
|
assert res == b'testing message! it is different'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
# different path
|
2017-12-23 20:20:49 +00:00
|
|
|
res = self.client.decrypt_keyvalue([0, 1, 3], b"test", unhexlify("b4811a9d492f5355a5186ddbfccaae7b"), ask_on_encrypt=True, ask_on_decrypt=True)
|
|
|
|
assert res == b'testing message!'
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
def test_encrypt_badlen(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(Exception):
|
|
|
|
self.client.encrypt_keyvalue([0, 1, 2], b"test", b"testing")
|
2014-06-07 17:48:52 +00:00
|
|
|
|
|
|
|
def test_decrypt_badlen(self):
|
|
|
|
self.setup_mnemonic_nopin_nopassphrase()
|
2017-12-23 20:20:49 +00:00
|
|
|
with pytest.raises(Exception):
|
|
|
|
self.client.decrypt_keyvalue([0, 1, 2], b"test", b"testing")
|