Telethon/client/tests/two_factor_auth_test.py

83 lines
4.8 KiB
Python
Raw Normal View History

2023-07-08 14:04:20 +03:00
from pytest import mark, raises
from telethon._impl.crypto.two_factor_auth import (
calculate_2fa,
check_p_prime_and_subgroup,
pad_to_256,
)
def test_calculations_1() -> None:
m1, g_a = calculate_2fa(
salt1=bytes((1,)),
salt2=bytes((2,)),
g=3,
p=pad_to_256(bytes((47,))),
g_b=bytes((5,)),
a=bytes((6,)),
password=bytes((7,)),
)
expected_m1 = b"\x9d\x83\xc4g\x00\xb8t\xe8\x07\xc4U\xe7\x11$\x1e\xde\x9e\xeabX;8G\xd7\xb7{z2\x13 6\xce"
expected_g_a = bytes(255) + b"\x18"
assert expected_m1 == m1
assert expected_g_a == g_a
def test_calculations_2() -> None:
(m1, g_a) = calculate_2fa(
salt1=b"_H<8\xbd\t\x86\xe7\xcd\xc9Z\xe18\xefOI\xb9Q\xc1\xf8\x1cq?\xec\xde\xf3\xafi,\xecKG\x16\xac\x9bw\n\x19^\xbe",
salt2=b"\xb6\x16\xfck\xbe\xdfQ\x11\x19\xc5\xed4b\x95'\xf1",
g=3,
p=b"\xc7\x1c\xae\xb9\xc6\xb1\xc9\x04\x8elR/p\xf1?s\x98\r@#\x8e>!\xc1I4\xd07V=\x93\x0fH\x19\x8a\n\xa7\xc1@X\"\x94\x93\xd2%0\xf4\xdb\xfa3on\n\xc9%\x13\x95C\xae\xd4L\xce|7 \xfdQ\xf6\x94XpZ\xc6\x8c\xd4\xfekk\x13\xab\xdc\x97FQ)i2\x84T\xf1\x8f\xaf\x8cY_d$w\xfe\x96\xbb*\x94\x1d[\xcd\x1dJ\xc8\xccI\x88\x07\x08\xfa\x9b7\x8e<O:\x90`\xbe\xe6|\xf9\xa4\xa4\xa6\x95\x81\x10Q\x90~\x16'S\xb5k\x0fkA\r\xbat\xd8\xa8K*\x14\xb3\x14N\x0e\xf1(GT\xfd\x17\xed\x95\rYe\xb4\xb9\xddFX-\xb1\x17\x8d\x16\x9ck\xc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0e\x87[\x13S\xa4\xac\xfe2\x92\xff\xb5j\x0fX\xb2\xa3\x90'\xc9\xbf\xdd\x91\xfdLS\x1d#\xc7}n\x8f}X>\xae\xea1m\xed\xde\xd6\x995)l\xe7\xea4\xe9\xben\xf2\xfb\xd8)\xea\xc4\xc9\xbddm\xc1V>G\xf7{\x91C\x1c\xa0\x02\xcfy\xfc\x14\x9d\x96\x82r\x83\\\x15\xca\x1ck,^\x03q*.\x1b]R\xf5\xe4\xfa\xa1\xc1l\xb1w\xfa\xfd\x96\xa6\xaa[K?Ld\x99\x15dKc\x85\\\xfb8V\x1f\xf1\x7f\xed\xfb\x8a"
assert expected_m1 == m1
assert expected_g_a == g_a
@mark.parametrize(("p", "g"), [(4, 0), (13, 0)])
def test_bad_g(p: int, g: int) -> None:
with raises(ValueError) as e:
check_p_prime_and_subgroup(p.to_bytes((p.bit_length() + 7) // 8), g)
assert e.match("bad g")
@mark.parametrize(
("p", "g"),
[
(11, 2),
(13, 3),
(13, 5),
(13, 6),
(13, 7),
],
)
def test_incorrect_pg(p: int, g: int) -> None:
assert not check_p_prime_and_subgroup(p.to_bytes((p.bit_length() + 7) // 8), g)
@mark.parametrize(
("p", "g"),
[
(23, 2),
(47, 3),
(11, 4),
(11, 5),
(179, 5),
(383, 6),
(479, 7),
(383, 7),
(503, 7),
],
)
def test_correct_pg(p: int, g: int) -> None:
assert check_p_prime_and_subgroup(p.to_bytes((p.bit_length() + 7) // 8), g)