2016-11-06 16:04:25 +00:00
|
|
|
from common import *
|
2016-11-06 11:38:33 +00:00
|
|
|
|
2023-06-28 10:58:54 +00:00
|
|
|
from trezor import config
|
2016-11-06 11:38:33 +00:00
|
|
|
from trezor.crypto import random
|
|
|
|
|
2016-11-06 16:04:25 +00:00
|
|
|
|
2017-11-10 14:18:43 +00:00
|
|
|
PINAPP = 0x00
|
|
|
|
PINKEY = 0x00
|
|
|
|
|
|
|
|
|
|
|
|
def random_entry():
|
|
|
|
while True:
|
2019-02-12 17:21:23 +00:00
|
|
|
appid, key = 1 + random.uniform(63), random.uniform(256)
|
2017-11-10 14:18:43 +00:00
|
|
|
if appid != PINAPP or key != PINKEY:
|
|
|
|
break
|
|
|
|
return appid, key
|
|
|
|
|
|
|
|
|
2016-11-06 11:38:33 +00:00
|
|
|
class TestConfig(unittest.TestCase):
|
2017-06-20 11:24:12 +00:00
|
|
|
def test_init(self):
|
|
|
|
config.init()
|
|
|
|
config.init()
|
|
|
|
config.init()
|
|
|
|
|
2016-11-08 20:34:52 +00:00
|
|
|
def test_wipe(self):
|
2017-06-20 11:24:12 +00:00
|
|
|
config.init()
|
2016-11-08 20:34:52 +00:00
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.unlock("", None), True)
|
|
|
|
config.set(1, 1, b"hello")
|
|
|
|
config.set(1, 2, b"world")
|
2017-12-14 18:01:41 +00:00
|
|
|
v0 = config.get(1, 1)
|
|
|
|
v1 = config.get(1, 2)
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(v0, b"hello")
|
|
|
|
self.assertEqual(v1, b"world")
|
2016-11-08 20:34:52 +00:00
|
|
|
config.wipe()
|
2017-12-14 18:01:41 +00:00
|
|
|
v0 = config.get(1, 1)
|
|
|
|
v1 = config.get(1, 2)
|
2018-12-28 15:54:25 +00:00
|
|
|
self.assertEqual(v0, None)
|
|
|
|
self.assertEqual(v1, None)
|
2016-11-08 20:34:52 +00:00
|
|
|
|
2017-11-10 14:18:43 +00:00
|
|
|
def test_lock(self):
|
|
|
|
for _ in range(128):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.unlock("", None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
appid, key = random_entry()
|
|
|
|
value = random.bytes(16)
|
|
|
|
config.set(appid, key, value)
|
|
|
|
config.init()
|
2018-12-28 15:54:25 +00:00
|
|
|
self.assertEqual(config.get(appid, key), None)
|
2017-11-10 14:18:43 +00:00
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
config.set(appid, key, bytes())
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
|
|
|
|
2018-01-13 14:21:40 +00:00
|
|
|
def test_public(self):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.unlock("", None), True)
|
2018-01-13 14:21:40 +00:00
|
|
|
|
|
|
|
appid, key = random_entry()
|
|
|
|
|
|
|
|
value32 = random.bytes(32)
|
|
|
|
config.set(appid, key, value32)
|
|
|
|
value16 = random.bytes(16)
|
|
|
|
config.set(appid, key, value16, True)
|
|
|
|
|
|
|
|
v1 = config.get(appid, key)
|
|
|
|
v2 = config.get(appid, key, True)
|
|
|
|
self.assertNotEqual(v1, v2)
|
|
|
|
self.assertEqual(v1, value32)
|
|
|
|
self.assertEqual(v2, value16)
|
|
|
|
|
|
|
|
config.init()
|
|
|
|
|
|
|
|
v1 = config.get(appid, key)
|
|
|
|
v2 = config.get(appid, key, True)
|
|
|
|
self.assertNotEqual(v1, v2)
|
2018-12-28 15:54:25 +00:00
|
|
|
self.assertEqual(v1, None)
|
2018-01-13 14:21:40 +00:00
|
|
|
self.assertEqual(v2, value16)
|
|
|
|
|
2017-11-10 14:18:43 +00:00
|
|
|
def test_change_pin(self):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertTrue(config.unlock("", None))
|
|
|
|
config.set(1, 1, b"value")
|
|
|
|
PINS = (
|
|
|
|
"123",
|
|
|
|
"123",
|
|
|
|
"Trezor T",
|
|
|
|
"3141592653589793238462643383279502884197",
|
|
|
|
"",
|
|
|
|
)
|
|
|
|
old_pin = ""
|
2020-08-13 21:29:50 +00:00
|
|
|
for new_pin in PINS:
|
|
|
|
self.assertTrue(config.unlock(old_pin, None))
|
|
|
|
|
|
|
|
# The APP namespace which is reserved for storage related values is inaccessible even
|
|
|
|
# when unlocked.
|
2021-03-10 13:43:21 +00:00
|
|
|
with self.assertRaises(ValueError):
|
2023-06-28 10:46:29 +00:00
|
|
|
config.set(PINAPP, PINKEY, b"value")
|
2020-08-13 21:29:50 +00:00
|
|
|
|
|
|
|
self.assertTrue(config.change_pin(old_pin, new_pin, None, None))
|
|
|
|
|
|
|
|
# Old PIN cannot be used to change the current PIN.
|
|
|
|
if old_pin != new_pin:
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertFalse(config.change_pin(old_pin, "666", None, None))
|
2020-08-13 21:29:50 +00:00
|
|
|
|
|
|
|
# Storage remains unlocked.
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.get(1, 1), b"value")
|
2020-08-13 21:29:50 +00:00
|
|
|
|
|
|
|
# The APP namespace which is reserved for storage related values is inaccessible even
|
|
|
|
# when unlocked.
|
2021-03-10 13:43:21 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
config.get(PINAPP, PINKEY)
|
2020-08-13 21:29:50 +00:00
|
|
|
|
|
|
|
# Old PIN cannot be used to unlock storage.
|
|
|
|
if old_pin != new_pin:
|
|
|
|
config.init()
|
|
|
|
self.assertFalse(config.unlock(old_pin, None))
|
|
|
|
self.assertEqual(config.get(1, 1), None)
|
|
|
|
with self.assertRaises(RuntimeError):
|
2023-06-28 10:46:29 +00:00
|
|
|
config.set(1, 1, b"new value")
|
2020-08-13 21:29:50 +00:00
|
|
|
|
|
|
|
# New PIN unlocks the storage.
|
|
|
|
self.assertTrue(config.unlock(new_pin, None))
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.get(1, 1), b"value")
|
2020-08-13 21:29:50 +00:00
|
|
|
|
|
|
|
# Lock the storage.
|
|
|
|
config.init()
|
|
|
|
old_pin = new_pin
|
2019-08-13 15:50:06 +00:00
|
|
|
|
|
|
|
def test_change_sd_salt(self):
|
|
|
|
salt1 = b"0123456789abcdef0123456789abcdef"
|
|
|
|
salt2 = b"0123456789ABCDEF0123456789ABCDEF"
|
|
|
|
|
|
|
|
# Enable PIN and SD salt.
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertTrue(config.unlock("", None))
|
|
|
|
config.set(1, 1, b"value")
|
|
|
|
self.assertFalse(config.change_pin("", "", salt1, None))
|
|
|
|
self.assertTrue(config.change_pin("", "000", None, salt1))
|
|
|
|
self.assertEqual(config.get(1, 1), b"value")
|
2019-08-13 15:50:06 +00:00
|
|
|
|
|
|
|
# Disable PIN and change SD salt.
|
|
|
|
config.init()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertFalse(config.unlock("000", None))
|
2019-08-13 15:50:06 +00:00
|
|
|
self.assertIsNone(config.get(1, 1))
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertTrue(config.unlock("000", salt1))
|
|
|
|
self.assertTrue(config.change_pin("000", "", salt1, salt2))
|
|
|
|
self.assertEqual(config.get(1, 1), b"value")
|
2019-08-13 15:50:06 +00:00
|
|
|
|
|
|
|
# Disable SD salt.
|
|
|
|
config.init()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertFalse(config.unlock("000", salt2))
|
2019-08-13 15:50:06 +00:00
|
|
|
self.assertIsNone(config.get(1, 1))
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertTrue(config.unlock("", salt2))
|
|
|
|
self.assertTrue(config.change_pin("", "", salt2, None))
|
|
|
|
self.assertEqual(config.get(1, 1), b"value")
|
2019-08-13 15:50:06 +00:00
|
|
|
|
|
|
|
# Check that PIN and SD salt are disabled.
|
|
|
|
config.init()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertTrue(config.unlock("", None))
|
|
|
|
self.assertEqual(config.get(1, 1), b"value")
|
2017-11-10 14:18:43 +00:00
|
|
|
|
2016-11-06 11:38:33 +00:00
|
|
|
def test_set_get(self):
|
2017-06-20 11:24:12 +00:00
|
|
|
config.init()
|
2016-11-06 11:38:33 +00:00
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.unlock("", None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
for _ in range(32):
|
|
|
|
appid, key = random_entry()
|
2016-11-23 13:45:58 +00:00
|
|
|
value = random.bytes(128)
|
|
|
|
config.set(appid, key, value)
|
|
|
|
value2 = config.get(appid, key)
|
|
|
|
self.assertEqual(value, value2)
|
2016-11-06 11:38:33 +00:00
|
|
|
|
2021-03-10 13:43:21 +00:00
|
|
|
# Test value deletion.
|
|
|
|
self.assertTrue(config.delete(appid, key))
|
|
|
|
self.assertIsNone(config.get(appid, key))
|
|
|
|
self.assertFalse(config.delete(appid, key))
|
|
|
|
|
|
|
|
# Test get/set for APP out ouf range.
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
2023-06-28 10:46:29 +00:00
|
|
|
config.set(0, 1, b"test")
|
2021-03-10 13:43:21 +00:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
config.get(0, 1)
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
2023-06-28 10:46:29 +00:00
|
|
|
config.set(192, 1, b"test")
|
2021-03-10 13:43:21 +00:00
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
config.get(192, 1)
|
|
|
|
|
2021-03-12 15:12:46 +00:00
|
|
|
def test_counter(self):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
|
|
|
|
|
|
|
# Test writable_locked when storage is locked.
|
|
|
|
|
|
|
|
config.lock()
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
config.set_counter(1, 2, 0, False)
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
config.next_counter(1, 2, False)
|
|
|
|
|
|
|
|
config.set_counter(1, 2, 100, True)
|
|
|
|
for i in range(100, 200):
|
|
|
|
self.assertEqual(config.next_counter(1, 2, True), i + 1)
|
|
|
|
|
|
|
|
# Test increment with storage unlocked.
|
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.unlock("", None), True)
|
2021-03-12 15:12:46 +00:00
|
|
|
|
|
|
|
for i in range(200, 300):
|
|
|
|
self.assertEqual(config.next_counter(1, 2, True), i + 1)
|
|
|
|
|
|
|
|
# Test counter deletion.
|
|
|
|
|
|
|
|
config.delete(1, 2, True, True)
|
|
|
|
self.assertEqual(config.next_counter(1, 2, True), 0)
|
|
|
|
|
|
|
|
# Test overflow protection.
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
config.set_counter(1, 2, 0x1_0000_0000, True)
|
|
|
|
|
|
|
|
start = 0xFFFF_FFFF - 100
|
|
|
|
config.set_counter(1, 2, start, True)
|
|
|
|
for i in range(start, 0xFFFF_FFFF):
|
|
|
|
self.assertEqual(config.next_counter(1, 2, True), i + 1)
|
|
|
|
|
|
|
|
with self.assertRaises(RuntimeError):
|
|
|
|
config.next_counter(1, 2, True)
|
|
|
|
|
2017-12-08 15:46:13 +00:00
|
|
|
def test_compact(self):
|
|
|
|
config.init()
|
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.unlock("", None), True)
|
2017-12-08 15:46:13 +00:00
|
|
|
appid, key = 1, 1
|
|
|
|
for _ in range(259):
|
|
|
|
value = random.bytes(259)
|
|
|
|
config.set(appid, key, value)
|
|
|
|
value2 = config.get(appid, key)
|
|
|
|
self.assertEqual(value, value2)
|
|
|
|
|
2016-11-06 11:38:33 +00:00
|
|
|
def test_get_default(self):
|
2017-06-20 11:24:12 +00:00
|
|
|
config.init()
|
2016-11-06 11:38:33 +00:00
|
|
|
config.wipe()
|
2023-06-28 10:46:29 +00:00
|
|
|
self.assertEqual(config.unlock("", None), True)
|
2017-11-10 14:18:43 +00:00
|
|
|
for _ in range(128):
|
|
|
|
appid, key = random_entry()
|
2016-11-23 13:45:58 +00:00
|
|
|
value = config.get(appid, key)
|
2018-12-28 15:54:25 +00:00
|
|
|
self.assertEqual(value, None)
|
2016-11-06 11:38:33 +00:00
|
|
|
|
2017-12-16 01:11:26 +00:00
|
|
|
|
2023-06-28 10:46:29 +00:00
|
|
|
if __name__ == "__main__":
|
2016-11-06 11:38:33 +00:00
|
|
|
unittest.main()
|