CoolrfulDARkness/moontlcp/handshake does not have a README file

    HandshakeError

    pub suberror HandshakeError {
    Incomplete
    InvalidMessageType(Byte)
    InvalidLength
    InvalidVersion(Byte, Byte)
    UnsupportedCipherSuite
    DecodeError(String)
    } derive(Eq,
    Debug
    )

    Alert

    pub(all) struct Alert {
    level : AlertLevel
    description : AlertDescription
    } derive(Eq,
    Debug
    )

    AlertDescription

    pub(all) enum AlertDescription {
    CloseNotify
    UnexpectedMessage
    BadRecordMac
    DecryptionFailed
    RecordOverflow
    HandshakeFailure
    BadCertificate
    UnsupportedCertificate
    CertificateExpired
    CertificateUnknown
    IllegalParameter
    UnknownCa
    DecodeError
    DecryptError
    ProtocolVersion
    InsufficientSecurity
    InternalError
    UnsupportedSite2Site
    } derive(Eq,
    Debug
    )

    AlertDescription::from_byte

    fn AlertDescription::from_byte(b : Byte) -> AlertDescription raise HandshakeError

    Decodes an alert description wire byte.

    AlertDescription::name

    fn AlertDescription::name(self : AlertDescription) -> String

    Returns the human-readable GM/T 0024-2023 name for an AlertDescription.

    AlertDescription::to_byte

    fn AlertDescription::to_byte(self : AlertDescription) -> Byte

    Encodes an AlertDescription to its RFC / GM/T 0024-2023 alert code.

    AlertLevel

    pub(all) enum AlertLevel {
    Warning
    Fatal
    } derive(Eq,
    Debug
    )

    AlertLevel::from_byte

    fn AlertLevel::from_byte(b : Byte) -> AlertLevel raise HandshakeError

    Decodes an alert level wire byte.

    AlertLevel::to_byte

    fn AlertLevel::to_byte(self : AlertLevel) -> Byte

    Encodes an AlertLevel to its wire byte (1 = Warning, 2 = Fatal).

    CertificateMsg

    pub(all) struct CertificateMsg {
    certificates : Array[Bytes]
    } derive(Eq,
    Debug
    )

    CertificateRequest

    pub(all) struct CertificateRequest {
    cert_types : Bytes
    ca_names : Array[Bytes]
    } derive(Eq,
    Debug
    )

    CertificateVerify

    pub(all) struct CertificateVerify {
    signature : Bytes
    } derive(Eq,
    Debug
    )

    CipherSuite

    pub(all) enum CipherSuite {
    EcdheSm4CbcSm3
    EccSm4CbcSm3
    EcdheSm4GcmSm3
    EccSm4GcmSm3
    } derive(Eq,
    Debug
    )

    The 4 standard GM/T 0024-2023 TLCP 1.1 cipher suites.

    CipherSuite::from_u16

    fn CipherSuite::from_u16(code : UInt) -> CipherSuite?

    Decodes a 16-bit wire code into a supported TLCP 1.1 cipher suite.

    CipherSuite::is_ecdhe

    fn CipherSuite::is_ecdhe(self : CipherSuite) -> Bool

    Returns true if the cipher suite uses ephemeral ECDHE key exchange (otherwise static ECC).

    CipherSuite::is_gcm

    fn CipherSuite::is_gcm(self : CipherSuite) -> Bool

    Returns true if the cipher suite uses SM4-GCM AEAD (otherwise SM4-CBC-HMAC-SM3).

    CipherSuite::name

    fn CipherSuite::name(self : CipherSuite) -> String

    Returns the standard GM/T 0024-2023 name of the cipher suite.

    CipherSuite::to_u16

    fn CipherSuite::to_u16(self : CipherSuite) -> UInt

    Returns the 16-bit GM/T 0024-2023 wire code for a cipher suite.

    ClientHello

    pub(all) struct ClientHello {
    version_major : Byte
    version_minor : Byte
    random : Bytes
    session_id : Bytes
    cipher_suites : Array[UInt]
    compression_methods : Bytes
    } derive(Eq,
    Debug
    )

    ClientKeyExchange

    pub(all) enum ClientKeyExchange {
    Ecc(Bytes)
    Ecdhe(Bytes)
    } derive(Eq,
    Debug
    )

    Finished

    pub(all) struct Finished {
    verify_data : Bytes
    } derive(Eq,
    Debug
    )

    HandshakeMsg

    pub(all) enum HandshakeMsg {
    ClientHello(ClientHello)
    ServerHello(ServerHello)
    Certificate(CertificateMsg)
    ServerKeyExchange(ServerKeyExchange)
    CertificateRequest(CertificateRequest)
    ServerHelloDone
    CertificateVerify(CertificateVerify)
    ClientKeyExchange(ClientKeyExchange)
    Finished(Finished)
    } derive(Eq,
    Debug
    )

    HandshakeSummary

    pub(all) struct HandshakeSummary {
    msg_type : Byte
    msg_type_name : String
    body_len : Int
    total_wire_len : Int
    protocol_version : UInt
    cipher_suites : Array[UInt]
    selected_suite : UInt?
    cert_count : Int
    session_id_len : Int
    } derive(Eq,
    Debug
    )

    Read-only structural summary of a single plaintext TLCP handshake message.

    KeyBlock

    pub(all) struct KeyBlock {
    client_mac_key : Bytes
    server_mac_key : Bytes
    client_write_key : Bytes
    server_write_key : Bytes
    client_write_iv : Bytes
    server_write_iv : Bytes
    } derive(Eq,
    Debug
    )

    Partitioned symmetric key material derived from master_secret.

    ServerHello

    pub(all) struct ServerHello {
    version_major : Byte
    version_minor : Byte
    random : Bytes
    session_id : Bytes
    cipher_suite : CipherSuite
    compression_method : Byte
    } derive(Eq,
    Debug
    )

    ServerKeyExchange

    pub(all) enum ServerKeyExchange {
    Ecc(Bytes)
    Ecdhe(Bytes, Bytes)
    } derive(Eq,
    Debug
    )

    Transcript

    pub struct Transcript {
    hasher :
    Sm3

    raw : Array[Byte]
    }

    Rolling SM3 handshake transcript accumulator that also retains the raw message byte stream (required for SM2 CertificateVerify signing).

    Transcript::hash

    fn Transcript::hash(self : Transcript) -> Bytes

    Returns the 32-byte SM3 digest of all messages fed so far without mutating state.

    Transcript::new

    fn Transcript::new() -> Transcript

    Creates an empty Transcript.

    Transcript::raw_bytes

    fn Transcript::raw_bytes(self : Transcript) -> Bytes

    Returns a copy of all raw handshake bytes accumulated so far.

    Transcript::update

    fn Transcript::update(self : Transcript, data : Bytes) -> Unit

    Appends encoded handshake message bytes to both the rolling SM3 hash and the raw transcript buffer.

    CURVE_TYPE_NAMED

    let CURVE_TYPE_NAMED : Byte

    HT_CERTIFICATE

    let HT_CERTIFICATE : Byte

    HT_CERTIFICATE_REQUEST

    let HT_CERTIFICATE_REQUEST : Byte

    HT_CERTIFICATE_VERIFY

    let HT_CERTIFICATE_VERIFY : Byte

    HT_CLIENT_HELLO

    let HT_CLIENT_HELLO : Byte

    HT_CLIENT_KEY_EXCHANGE

    let HT_CLIENT_KEY_EXCHANGE : Byte

    HT_FINISHED

    let HT_FINISHED : Byte

    HT_SERVER_HELLO

    let HT_SERVER_HELLO : Byte

    HT_SERVER_HELLO_DONE

    let HT_SERVER_HELLO_DONE : Byte

    HT_SERVER_KEY_EXCHANGE

    let HT_SERVER_KEY_EXCHANGE : Byte

    MAX_HANDSHAKE_MSG_LEN

    let MAX_HANDSHAKE_MSG_LEN : Int

    Maximum allowed handshake message body length (64 KiB) to prevent memory exhaustion / reassembly DoS attacks.

    NAMED_CURVE_SM2P256V1

    let NAMED_CURVE_SM2P256V1 : UInt

    RFC 8998 / GM/T 0024-2023 SM2 named curve identifier (sm2p256v1).

    build_ecc_ske_tbs

    fn build_ecc_ske_tbs(client_random : Bytes, server_random : Bytes, enc_cert_der : Bytes) -> Bytes

    Builds the GB/T 38636-2020 §6.4.5.4 ECC ServerKeyExchange TBS (to-be-signed) input: client_random || server_random || uint24(len(enc_cert)) || enc_cert.

    build_ecdhe_params

    fn build_ecdhe_params(curve_id : UInt, pub_uncompressed : Bytes) -> Bytes

    Builds ECDHE ServerECDHParams: CURVE_TYPE_NAMED (0x03) || be16(curve_id) || u8(len(pub)) || pub_uncompressed.

    build_ecdhe_ske_tbs

    fn build_ecdhe_ske_tbs(client_random : Bytes, server_random : Bytes, params : Bytes) -> Bytes

    Builds the ECDHE ServerKeyExchange TBS (to-be-signed) input: client_random || server_random || params.

    compute_verify_data

    fn compute_verify_data(master_secret : Bytes, is_client : Bool, transcript_hash : Bytes) -> Bytes

    Computes the 12-byte Finished.verify_data via P_SM3: PRF(master_secret, "client finished" | "server finished", transcript_hash)[0..11].

    decode_alert

    fn decode_alert(buf : BytesView) -> Alert raise HandshakeError

    Decodes a 2-byte TLCP Alert record payload.

    decode_handshake_frame

    fn decode_handshake_frame(buf : BytesView) -> (Byte, BytesView, Int) raise HandshakeError

    Decodes one handshake frame header + body slice from a possibly coalesced handshake stream, returning (msg_type, body_view, total_bytes_consumed). Rejects frames exceeding MAX_HANDSHAKE_MSG_LEN immediately with InvalidLength, and raises Incomplete if fewer than 4 + body_len bytes are available.

    decode_handshake_msg

    fn decode_handshake_msg(msg_type : Byte, body : BytesView, is_ecdhe : Bool) -> HandshakeMsg raise HandshakeError

    Decodes a handshake message body according to its msg_type and key-exchange mode (is_ecdhe).

    derive_key_block

    fn derive_key_block(master_secret : Bytes, server_random : Bytes, client_random : Bytes, suite : CipherSuite) -> KeyBlock

    Derives the TLCP 1.1 KeyBlock via P_SM3: PRF(master_secret, "key expansion", server_random || client_random).

    • CBC suites: 32/32/16/16/16/16 = 128 bytes.
    • GCM suites: 0/0/16/16/4/4 = 40 bytes.

    derive_master_secret

    fn derive_master_secret(pre_master_secret : Bytes, client_random : Bytes, server_random : Bytes) -> Bytes

    Derives the 48-byte TLCP 1.1 master_secret via P_SM3: PRF(pre_master_secret, "master secret", client_random || server_random)[0..47].

    encode_alert

    fn encode_alert(alert : Alert) -> Bytes

    Serializes a 2-byte TLCP Alert record payload.

    encode_handshake_msg

    fn encode_handshake_msg(msg : HandshakeMsg) -> Bytes

    Encodes a HandshakeMsg into its complete wire frame: msg_type (1B) || uint24_be(body.length()) || body.

    inspect_alert

    fn inspect_alert(payload : BytesView) -> (Byte, Byte, String) raise HandshakeError

    Statelessly inspects a 2-byte plaintext TLCP Alert payload, returning (level_byte, description_byte, human_readable_gmt0024_alert_name).

    inspect_handshake

    fn inspect_handshake(raw : BytesView) -> HandshakeSummary raise HandshakeError

    Statelessly inspects a single plaintext TLCP handshake frame at the start of raw.

    Validates the 4-byte handshake header (msg_type || uint24_be(body_len)), enforces body_len <= MAX_HANDSHAKE_MSG_LEN, and validates message-specific structural bounds for all 9 GM/T 0024-2023 handshake types without running a stateful session or mutating any state.

    parse_ecdhe_params

    fn parse_ecdhe_params(params : BytesView) -> (UInt, Bytes) raise HandshakeError

    Parses ECDHE ServerECDHParams into (curve_id, public_point_bytes). Accepts SM2 named curve IDs 0x0029 (RFC 8998), 0x0020, and 0x001D.