2019-04-12 09:41:32 +00:00
|
|
|
from common import *
|
2019-05-03 16:28:14 +00:00
|
|
|
from trezor.crypto import slip39, random
|
2019-04-12 09:41:32 +00:00
|
|
|
from slip39_vectors import vectors
|
|
|
|
|
2019-05-03 16:28:14 +00:00
|
|
|
def combinations(iterable, r):
|
|
|
|
# Taken from https://docs.python.org/3.7/library/itertools.html#itertools.combinations
|
|
|
|
pool = tuple(iterable)
|
|
|
|
n = len(pool)
|
|
|
|
if r > n:
|
|
|
|
return
|
|
|
|
indices = list(range(r))
|
|
|
|
yield tuple(pool[i] for i in indices)
|
|
|
|
while True:
|
|
|
|
for i in reversed(range(r)):
|
|
|
|
if indices[i] != i + n - r:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
indices[i] += 1
|
|
|
|
for j in range(i+1, r):
|
|
|
|
indices[j] = indices[j-1] + 1
|
|
|
|
yield tuple(pool[i] for i in indices)
|
2019-04-12 09:41:32 +00:00
|
|
|
|
|
|
|
class TestCryptoSlip39(unittest.TestCase):
|
2019-04-12 19:17:47 +00:00
|
|
|
MS = b"ABCDEFGHIJKLMNOP"
|
|
|
|
|
|
|
|
def test_basic_sharing_random(self):
|
2019-06-23 10:18:59 +00:00
|
|
|
ms = random.bytes(32)
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(ms, identifier, 1, [(3, 5)])
|
2019-06-23 10:18:59 +00:00
|
|
|
mnemonics = mnemonics[0]
|
2019-05-03 16:28:14 +00:00
|
|
|
self.assertEqual(slip39.combine_mnemonics(mnemonics[:3]), slip39.combine_mnemonics(mnemonics[2:]))
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_basic_sharing_fixed(self):
|
2019-06-25 12:36:30 +00:00
|
|
|
generated_identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(self.MS, generated_identifier, 1, [(3, 5)])
|
2019-06-23 10:18:59 +00:00
|
|
|
mnemonics = mnemonics[0]
|
2019-05-03 16:28:14 +00:00
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics(mnemonics[:3])
|
2019-04-15 16:13:29 +00:00
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b""), self.MS)
|
2019-06-25 12:36:30 +00:00
|
|
|
self.assertEqual(generated_identifier, identifier)
|
2019-04-15 16:13:29 +00:00
|
|
|
self.assertEqual(slip39.combine_mnemonics(mnemonics[1:4])[2], ems)
|
2019-04-12 19:17:47 +00:00
|
|
|
with self.assertRaises(slip39.MnemonicError):
|
2019-04-14 18:49:14 +00:00
|
|
|
slip39.combine_mnemonics(mnemonics[1:3])
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_passphrase(self):
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(self.MS, identifier, 1, [(3, 5)], b"TREZOR")
|
2019-06-23 10:18:59 +00:00
|
|
|
mnemonics = mnemonics[0]
|
2019-04-15 16:13:29 +00:00
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics(mnemonics[1:4])
|
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b"TREZOR"), self.MS)
|
|
|
|
self.assertNotEqual(slip39.decrypt(identifier, exponent, ems, b""), self.MS)
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_iteration_exponent(self):
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(self.MS, identifier, 1, [(3, 5)], b"TREZOR", 1)
|
2019-06-23 10:18:59 +00:00
|
|
|
mnemonics = mnemonics[0]
|
2019-04-15 16:13:29 +00:00
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics(mnemonics[1:4])
|
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b"TREZOR"), self.MS)
|
|
|
|
self.assertNotEqual(slip39.decrypt(identifier, exponent, ems, b""), self.MS)
|
2019-04-12 19:17:47 +00:00
|
|
|
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(self.MS, identifier, 1, [(3, 5)], b"TREZOR", 2)
|
2019-06-23 10:18:59 +00:00
|
|
|
mnemonics = mnemonics[0]
|
2019-04-15 16:13:29 +00:00
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics(mnemonics[1:4])
|
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b"TREZOR"), self.MS)
|
|
|
|
self.assertNotEqual(slip39.decrypt(identifier, exponent, ems, b""), self.MS)
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_group_sharing(self):
|
2019-05-03 16:28:14 +00:00
|
|
|
group_threshold = 2
|
|
|
|
group_sizes = (5, 3, 5, 1)
|
|
|
|
member_thresholds = (3, 2, 2, 1)
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(
|
|
|
|
self.MS, identifier, group_threshold, list(zip(member_thresholds, group_sizes))
|
2019-05-03 16:28:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Test all valid combinations of mnemonics.
|
|
|
|
for groups in combinations(zip(mnemonics, member_thresholds), group_threshold):
|
|
|
|
for group1_subset in combinations(groups[0][0], groups[0][1]):
|
|
|
|
for group2_subset in combinations(groups[1][0], groups[1][1]):
|
|
|
|
mnemonic_subset = list(group1_subset + group2_subset)
|
|
|
|
random.shuffle(mnemonic_subset)
|
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics(mnemonic_subset)
|
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b""), self.MS)
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Minimal sets of mnemonics.
|
2019-05-03 16:28:14 +00:00
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics([mnemonics[2][0], mnemonics[2][2], mnemonics[3][0]])
|
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b""), self.MS)
|
2019-04-15 16:13:29 +00:00
|
|
|
self.assertEqual(slip39.combine_mnemonics([mnemonics[2][3], mnemonics[3][0], mnemonics[2][4]])[2], ems)
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
# One complete group and one incomplete group out of two groups required.
|
|
|
|
with self.assertRaises(slip39.MnemonicError):
|
2019-04-14 18:49:14 +00:00
|
|
|
slip39.combine_mnemonics(mnemonics[0][2:] + [mnemonics[1][0]])
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
# One group of two required.
|
|
|
|
with self.assertRaises(slip39.MnemonicError):
|
2019-04-14 18:49:14 +00:00
|
|
|
slip39.combine_mnemonics(mnemonics[0][1:4])
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
|
2019-06-11 09:26:29 +00:00
|
|
|
def test_group_sharing_threshold_1(self):
|
|
|
|
group_threshold = 1
|
|
|
|
group_sizes = (5, 3, 5, 1)
|
|
|
|
member_thresholds = (3, 2, 2, 1)
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(
|
|
|
|
self.MS, identifier, group_threshold, list(zip(member_thresholds, group_sizes))
|
2019-06-11 09:26:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Test all valid combinations of mnemonics.
|
|
|
|
for group, threshold in zip(mnemonics, member_thresholds):
|
|
|
|
for group_subset in combinations(group, threshold):
|
|
|
|
mnemonic_subset = list(group_subset)
|
|
|
|
random.shuffle(mnemonic_subset)
|
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics(mnemonic_subset)
|
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b""), self.MS)
|
|
|
|
|
|
|
|
|
|
|
|
def test_all_groups_exist(self):
|
|
|
|
for group_threshold in (1, 2, 5):
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
|
|
|
mnemonics = slip39.generate_mnemonics_from_data(
|
|
|
|
self.MS, identifier, group_threshold, [(3, 5), (1, 1), (2, 3), (2, 5), (3, 5)]
|
2019-06-11 09:26:29 +00:00
|
|
|
)
|
|
|
|
self.assertEqual(len(mnemonics), 5)
|
|
|
|
self.assertEqual(len(sum(mnemonics, [])), 19)
|
|
|
|
|
2019-06-18 16:34:29 +00:00
|
|
|
|
2019-05-02 13:00:04 +00:00
|
|
|
def test_invalid_sharing(self):
|
2019-06-25 12:36:30 +00:00
|
|
|
identifier = slip39.generate_random_identifier()
|
2019-05-02 13:00:04 +00:00
|
|
|
# Short master secret.
|
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 12:36:30 +00:00
|
|
|
slip39.generate_mnemonics_from_data(self.MS[:14], identifier, 1, [(2, 3)])
|
2019-05-02 13:00:04 +00:00
|
|
|
|
|
|
|
# Odd length master secret.
|
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 12:36:30 +00:00
|
|
|
slip39.generate_mnemonics_from_data(self.MS + b"X", identifier,1, [(2, 3)])
|
2019-05-02 13:00:04 +00:00
|
|
|
|
|
|
|
# Group threshold exceeds number of groups.
|
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 12:36:30 +00:00
|
|
|
slip39.generate_mnemonics_from_data(self.MS, identifier, 3, [(3, 5), (2, 5)])
|
2019-05-02 13:00:04 +00:00
|
|
|
|
2019-05-03 14:09:40 +00:00
|
|
|
# Invalid group threshold.
|
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 12:36:30 +00:00
|
|
|
slip39.generate_mnemonics_from_data(self.MS, identifier, 0, [(3, 5), (2, 5)])
|
2019-05-03 14:09:40 +00:00
|
|
|
|
|
|
|
# Member threshold exceeds number of members.
|
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 12:36:30 +00:00
|
|
|
slip39.generate_mnemonics_from_data(self.MS, identifier, 2, [(3, 2), (2, 5)])
|
2019-05-03 14:09:40 +00:00
|
|
|
|
|
|
|
# Invalid member threshold.
|
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 12:36:30 +00:00
|
|
|
slip39.generate_mnemonics_from_data(self.MS, identifier, 2, [(0, 2), (2, 5)])
|
2019-05-03 14:09:40 +00:00
|
|
|
|
2019-05-02 13:00:04 +00:00
|
|
|
# Group with multiple members and threshold 1.
|
|
|
|
with self.assertRaises(ValueError):
|
2019-06-25 12:36:30 +00:00
|
|
|
slip39.generate_mnemonics_from_data(self.MS, identifier, 2, [(3, 5), (1, 3), (2, 5)])
|
2019-05-02 13:00:04 +00:00
|
|
|
|
|
|
|
|
2019-04-12 19:17:47 +00:00
|
|
|
def test_vectors(self):
|
2019-04-12 09:41:32 +00:00
|
|
|
for mnemonics, secret in vectors:
|
|
|
|
if secret:
|
2019-04-15 16:13:29 +00:00
|
|
|
identifier, exponent, ems = slip39.combine_mnemonics(mnemonics)
|
|
|
|
self.assertEqual(slip39.decrypt(identifier, exponent, ems, b"TREZOR"), unhexlify(secret))
|
2019-04-12 09:41:32 +00:00
|
|
|
else:
|
|
|
|
with self.assertRaises(slip39.MnemonicError):
|
2019-04-15 16:13:29 +00:00
|
|
|
slip39.combine_mnemonics(mnemonics)
|
2019-04-12 19:17:47 +00:00
|
|
|
|
|
|
|
|
2019-06-18 16:34:29 +00:00
|
|
|
def test_error_location(self):
|
2019-06-18 21:50:03 +00:00
|
|
|
mnemonics = [
|
|
|
|
"duckling enlarge academic academic agency result length solution fridge kidney coal piece deal husband erode duke ajar critical decision keyboard",
|
|
|
|
"theory painting academic academic armed sweater year military elder discuss acne wildlife boring employer fused large satoshi bundle carbon diagnose anatomy hamster leaves tracks paces beyond phantom capital marvel lips brave detect luck",
|
|
|
|
]
|
2019-06-18 16:34:29 +00:00
|
|
|
for mnemonic in mnemonics:
|
|
|
|
data = tuple(slip39.mnemonic_to_indices(mnemonic))
|
|
|
|
self.assertEqual(slip39.rs1024_error_index(data), None)
|
|
|
|
for i in range(len(data)):
|
2019-06-18 21:50:03 +00:00
|
|
|
for _ in range(50):
|
|
|
|
error_data = error_data = data[:i] + (data[i] ^ (random.uniform(1023) + 1), ) + data[i + 1:]
|
|
|
|
self.assertEqual(slip39.rs1024_error_index(error_data), i)
|
2019-06-18 16:34:29 +00:00
|
|
|
|
|
|
|
|
2019-04-12 09:41:32 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|