zenith-net 0.1.0

Zenith 网络地址与传输层抽象:L2-L4 协议解析、TCP/UDP/QUIC 状态机、来源准入引擎、单队列 Worker 数据面循环
//! Test initial key derivation via rustls to match aioquic reference

use rustls::quic::Version;
use rustls::Side;

fn get_quic_keys(client_dcid: &[u8]) -> rustls::quic::Keys {
    // 工程红线:rustls + ring 为唯一密码学后端(aws-lc-rs 已以 default-features=false 排除)
    let suite = rustls::crypto::ring::cipher_suite::TLS13_AES_128_GCM_SHA256;
    suite
        .tls13()
        .unwrap()
        .quic_suite()
        .unwrap()
        .keys(client_dcid, Side::Server, Version::V1)
}

#[test]
fn test_rustls_initial_keys_match_aioquic() {
    let client_dcid = [0x0e, 0x95, 0x59, 0x96, 0x7f, 0xd8, 0xf3, 0xf3];
    let keys = get_quic_keys(&client_dcid);

    let remote_packet = keys.remote.packet.as_ref();
    let remote_header = keys.remote.header.as_ref();

    // rustls 的 Keys 不实现 Debug 也不暴露原始密钥字节,
    // 通过加密/解密往返验证密钥正确性
    println!("remote packet tag_len: {}", remote_packet.tag_len());
    println!("remote header sample_len: {}", remote_header.sample_len());

    // Since rustls doesn't expose raw key bytes, let's try encrypt/decrypt
    // roundtrip with a known packet to verify
    let plaintext = b"hello world test";
    let aad = b"some header aad test";
    let pn: u64 = 42;

    let mut buf = plaintext.to_vec();
    let tag = remote_packet
        .encrypt_in_place(pn, aad, &mut buf)
        .expect("encrypt");
    buf.extend_from_slice(tag.as_ref());

    // Now decrypt
    let decrypted = remote_packet
        .decrypt_in_place(pn, aad, &mut buf)
        .expect("decrypt");

    assert_eq!(decrypted, plaintext.as_slice());
    println!("Roundtrip encrypt/decrypt OK!");
}

#[test]
fn test_aead_with_real_packet_parameters() {
    let client_dcid = [0x0e, 0x95, 0x59, 0x96, 0x7f, 0xd8, 0xf3, 0xf3];
    let keys = get_quic_keys(&client_dcid);

    let aad = [0xc7, 0x00, 0x00, 0x00, 0x01, 0x08, 0x0e, 0x95, 0x59, 0x96, 0x7f, 0xd8, 0xf3, 0xf3,
        0x08, 0x48, 0x21, 0xbe, 0x84, 0x26, 0x0a, 0xd0, 0x0b, 0x00, 0x41, 0xf2, 0x78, 0xaa, 0xdd];
    let pn: u64 = 7908061;

    // Real ciphertext (495 bytes of ciphertext + 16 bytes tag)
    // Let's encrypt a known plaintext first to verify our keys work
    let plaintext = b"test plaintext content for verification";
    let mut buf = plaintext.to_vec();

    let tag = keys.remote.packet.encrypt_in_place(pn, &aad, &mut buf).expect("encrypt");
    buf.extend_from_slice(tag.as_ref());

    // Now try to decrypt
    let decrypted = keys.remote.packet.decrypt_in_place(pn, &aad, &mut buf).expect("decrypt");
    assert_eq!(decrypted, plaintext.as_slice());
    println!("Encrypt/decrypt roundtrip with pn={} OK", pn);

    // Test that wrong pn fails
    let mut buf2 = plaintext.to_vec();
    let tag2 = keys.remote.packet.encrypt_in_place(pn, &aad, &mut buf2).expect("encrypt");
    buf2.extend_from_slice(tag2.as_ref());
    let bad_pn = pn + 1;
    let result = keys.remote.packet.decrypt_in_place(bad_pn, &aad, &mut buf2);
    assert!(result.is_err(), "Wrong pn should fail");
    println!("Wrong pn correctly fails");

    // Test that wrong aad fails
    let mut buf3 = plaintext.to_vec();
    let tag3 = keys.remote.packet.encrypt_in_place(pn, &aad, &mut buf3).expect("encrypt");
    buf3.extend_from_slice(tag3.as_ref());
    let bad_aad = [0u8; 29];
    let result = keys.remote.packet.decrypt_in_place(pn, &bad_aad, &mut buf3);
    assert!(result.is_err(), "Wrong AAD should fail");
    println!("Wrong AAD correctly fails");
}