2018-06-21 14:28:34 +00:00
|
|
|
# This file is part of the Trezor project.
|
2017-01-03 18:40:05 +00:00
|
|
|
#
|
2019-05-29 16:44:09 +00:00
|
|
|
# Copyright (C) 2012-2019 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>.
|
2017-01-03 18:40:05 +00:00
|
|
|
|
2013-01-14 13:44:31 +00:00
|
|
|
|
2019-08-27 12:48:16 +00:00
|
|
|
# fmt: off
|
|
|
|
# 1 2 3 4 5 6 7 8 9 10 11 12
|
|
|
|
MNEMONIC12 = "alcohol woman abuse must during monitor noble actual mixed trade anger aisle"
|
|
|
|
MNEMONIC18 = "owner little vague addict embark decide pink prosper true fork panda embody mixture exchange choose canoe electric jewel"
|
|
|
|
MNEMONIC24 = "dignity pass list indicate nasty swamp pool script soccer toe leaf photo multiply desk host tomato cradle drill spread actor shine dismiss champion exotic"
|
|
|
|
MNEMONIC_ALLALLALL = " ".join(["all"] * 12)
|
|
|
|
# fmt: on
|
|
|
|
|
2016-11-28 15:01:45 +00:00
|
|
|
|
2018-03-02 17:25:39 +00:00
|
|
|
class TrezorTest:
|
2019-08-27 12:48:16 +00:00
|
|
|
mnemonic12 = MNEMONIC12
|
|
|
|
mnemonic18 = MNEMONIC18
|
|
|
|
mnemonic24 = MNEMONIC24
|
|
|
|
mnemonic_all = MNEMONIC_ALLALLALL
|
2018-08-10 12:04:58 +00:00
|
|
|
|
2018-08-13 16:21:24 +00:00
|
|
|
pin4 = "1234"
|
|
|
|
pin6 = "789456"
|
|
|
|
pin8 = "45678978"
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_entropy(strength, internal_entropy, external_entropy):
|
2018-08-13 16:21:24 +00:00
|
|
|
"""
|
2017-06-28 15:56:58 +00:00
|
|
|
strength - length of produced seed. One of 128, 192, 256
|
|
|
|
random - binary stream of random data from external HRNG
|
2018-08-13 16:21:24 +00:00
|
|
|
"""
|
2017-12-23 20:20:49 +00:00
|
|
|
import hashlib
|
|
|
|
|
2017-06-28 15:56:58 +00:00
|
|
|
if strength not in (128, 192, 256):
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Invalid strength")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if not internal_entropy:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Internal entropy is not provided")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if len(internal_entropy) < 32:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Internal entropy too short")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if not external_entropy:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("External entropy is not provided")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if len(external_entropy) < 32:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("External entropy too short")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
entropy = hashlib.sha256(internal_entropy + external_entropy).digest()
|
2018-08-13 16:21:24 +00:00
|
|
|
entropy_stripped = entropy[: strength // 8]
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
if len(entropy_stripped) * 8 != strength:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError("Entropy length mismatch")
|
2017-06-28 15:56:58 +00:00
|
|
|
|
|
|
|
return entropy_stripped
|