- 纯标准库实现 Kademlia DHT(UDP + asyncio),报文 Ed25519 签名认证 - 超过 1200 字节的载荷自动分片与重组,上限 1 MiB - 公共聊天室:成员公告 + DHT 日志回放,迟到入群者也能补到历史 - 一对一私聊:X25519 + HKDF + AEAD 端到端加密,支持离线信箱 - Tkinter 图形界面,asyncio 运行在后台线程,事件经队列回传 UI - 局域网组播自动发现 + 手动引导地址(-b)两种互联方式 - run.cmd / start.bat 启动脚本,自动寻找 py -3 或 python - 48 个单元测试覆盖密码学原语、DHT 路由/协议与聊天层 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
179 lines
6.8 KiB
Python
179 lines
6.8 KiB
Python
"""Test vectors for :mod:`dhtchat.crypto`.
|
|
|
|
Ed25519 vectors come from RFC 8032 section 7.1, X25519 vectors from
|
|
RFC 7748 sections 5.2 and 6.1.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import unittest
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from dhtchat import crypto # noqa: E402
|
|
|
|
|
|
class Ed25519Tests(unittest.TestCase):
|
|
VECTORS = [
|
|
(
|
|
"9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60",
|
|
"d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a",
|
|
"",
|
|
"e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821"
|
|
"590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b",
|
|
),
|
|
(
|
|
"4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb",
|
|
"3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c",
|
|
"72",
|
|
"92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da085ac1e4"
|
|
"3e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00",
|
|
),
|
|
(
|
|
"c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7",
|
|
"fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025",
|
|
"af82",
|
|
"6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac18ff9b53"
|
|
"8d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a",
|
|
),
|
|
]
|
|
|
|
def test_public_key_derivation(self):
|
|
for seed_hex, public_hex, _, _ in self.VECTORS:
|
|
seed = bytes.fromhex(seed_hex)
|
|
self.assertEqual(
|
|
crypto.ed25519_public_key(seed).hex(), public_hex
|
|
)
|
|
|
|
def test_signature_vectors(self):
|
|
for seed_hex, public_hex, message_hex, signature_hex in self.VECTORS:
|
|
seed = bytes.fromhex(seed_hex)
|
|
message = bytes.fromhex(message_hex)
|
|
signature = crypto.ed25519_sign(seed, message)
|
|
self.assertEqual(
|
|
crypto.ed25519_public_key(seed).hex(), public_hex
|
|
)
|
|
self.assertEqual(signature.hex(), signature_hex)
|
|
self.assertTrue(
|
|
crypto.ed25519_verify(bytes.fromhex(public_hex), message, signature)
|
|
)
|
|
|
|
def test_roundtrip_and_tamper(self):
|
|
seed = crypto.generate_private_key()
|
|
public = crypto.ed25519_public_key(seed)
|
|
message = b"hello dht chat"
|
|
signature = crypto.ed25519_sign(seed, message)
|
|
self.assertTrue(crypto.ed25519_verify(public, message, signature))
|
|
self.assertFalse(crypto.ed25519_verify(public, message + b"!", signature))
|
|
|
|
broken = bytearray(signature)
|
|
broken[10] ^= 0x01
|
|
self.assertFalse(crypto.ed25519_verify(public, message, bytes(broken)))
|
|
|
|
other_seed = crypto.generate_private_key()
|
|
self.assertFalse(
|
|
crypto.ed25519_verify(
|
|
crypto.ed25519_public_key(other_seed), message, signature
|
|
)
|
|
)
|
|
|
|
def test_rejects_malformed_inputs(self):
|
|
seed = crypto.generate_private_key()
|
|
public = crypto.ed25519_public_key(seed)
|
|
signature = crypto.ed25519_sign(seed, b"x")
|
|
self.assertFalse(crypto.ed25519_verify(public, b"x", signature[:-1]))
|
|
self.assertFalse(crypto.ed25519_verify(public[:-1], b"x", signature))
|
|
self.assertFalse(crypto.ed25519_verify(public, b"x", b"\x00" * 64))
|
|
self.assertFalse(crypto.ed25519_verify(b"\x00" * 32, b"x", signature))
|
|
|
|
|
|
class X25519Tests(unittest.TestCase):
|
|
def test_rfc7748_scalar_mult(self):
|
|
scalar = bytes.fromhex(
|
|
"a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4"
|
|
)
|
|
point = bytes.fromhex(
|
|
"e6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c"
|
|
)
|
|
expected = bytes.fromhex(
|
|
"c3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552"
|
|
)
|
|
self.assertEqual(crypto.x25519(scalar, point), expected)
|
|
|
|
def test_rfc7748_diffie_hellman(self):
|
|
alice_private = bytes.fromhex(
|
|
"77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a"
|
|
)
|
|
alice_public = bytes.fromhex(
|
|
"8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a"
|
|
)
|
|
bob_private = bytes.fromhex(
|
|
"5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb"
|
|
)
|
|
bob_public = bytes.fromhex(
|
|
"de9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f"
|
|
)
|
|
shared = bytes.fromhex(
|
|
"4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742"
|
|
)
|
|
self.assertEqual(crypto.x25519(alice_private, bob_public), shared)
|
|
self.assertEqual(crypto.x25519(bob_private, alice_public), shared)
|
|
|
|
def test_ed25519_conversion_agrees_with_ecdh(self):
|
|
seed_a = crypto.generate_private_key()
|
|
seed_b = crypto.generate_private_key()
|
|
pub_a = crypto.ed25519_public_key(seed_a)
|
|
pub_b = crypto.ed25519_public_key(seed_b)
|
|
|
|
secret_ab = crypto.shared_secret(seed_a, pub_b)
|
|
secret_ba = crypto.shared_secret(seed_b, pub_a)
|
|
self.assertEqual(secret_ab, secret_ba)
|
|
self.assertEqual(len(secret_ab), crypto.SHARED_SECRET_LEN)
|
|
|
|
|
|
class AeadTests(unittest.TestCase):
|
|
def test_roundtrip(self):
|
|
key = crypto.shared_secret(
|
|
crypto.generate_private_key(), crypto.ed25519_public_key(crypto.generate_private_key())
|
|
)
|
|
for payload in (b"", b"a", b"x" * 1000):
|
|
blob = crypto.aead_encrypt(key, payload, b"aad")
|
|
self.assertEqual(crypto.aead_decrypt(key, blob, b"aad"), payload)
|
|
if len(payload) > 16:
|
|
self.assertNotIn(payload, blob)
|
|
|
|
def test_authentication(self):
|
|
key = b"\x11" * 32
|
|
other = b"\x22" * 32
|
|
blob = bytearray(crypto.aead_encrypt(key, b"secret", b"aad"))
|
|
with self.assertRaises(ValueError):
|
|
crypto.aead_decrypt(other, bytes(blob), b"aad")
|
|
with self.assertRaises(ValueError):
|
|
crypto.aead_decrypt(key, bytes(blob), b"other-aad")
|
|
|
|
blob[-1] ^= 0x01
|
|
with self.assertRaises(ValueError):
|
|
crypto.aead_decrypt(key, bytes(blob), b"aad")
|
|
|
|
with self.assertRaises(ValueError):
|
|
crypto.aead_decrypt(key, b"short", b"aad")
|
|
|
|
def test_hkdf_matches_rfc5869_case1(self):
|
|
ikm = bytes.fromhex("0b" * 22)
|
|
salt = bytes.fromhex("000102030405060708090a0b0c")
|
|
info = bytes.fromhex("f0f1f2f3f4f5f6f7f8f9")
|
|
expected = bytes.fromhex(
|
|
"3cb25f25faacd57a90434f64d0362f2a"
|
|
"2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
|
|
"34007208d5b887185865"
|
|
)
|
|
self.assertEqual(
|
|
crypto.hkdf_sha256(ikm, salt, info, 42), expected
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|