CoolrfulDARkness/moontlcp/record does not have a README file

    RecordError

    pub suberror RecordError {
    Incomplete
    InvalidContentType(Byte)
    UnsupportedVersion(Byte, Byte)
    RecordOverflow(Int)
    BadRecordMac
    SequenceOverflow
    CryptoError(String)
    } derive(Eq,
    Debug
    )

    CipherState

    pub enum CipherState {
    Null
    Sm4CbcHmacSm3(
    Sm4Key
    , Bytes)
    Sm4Gcm(
    Sm4Key
    , Bytes)
    }

    Active symmetric protection state for one direction of a TLCP record layer.

    CipherState::new_cbc

    fn CipherState::new_cbc(enc_key : Bytes, mac_key : Bytes) -> CipherState raise RecordError

    Creates an SM4-CBC-HMAC-SM3 cipher state (16-byte SM4 key, 32-byte HMAC-SM3 key).

    CipherState::new_gcm

    fn CipherState::new_gcm(enc_key : Bytes, implicit_iv : Bytes) -> CipherState raise RecordError

    Creates an SM4-GCM AEAD cipher state (16-byte SM4 key, 4-byte implicit IV salt).

    CipherState::null

    fn CipherState::null() -> CipherState

    Creates an unprotected (null) cipher state used prior to ChangeCipherSpec.

    ContentType

    pub(all) enum ContentType {
    ChangeCipherSpec
    Alert
    Handshake
    ApplicationData
    } derive(Eq,
    Debug
    )

    ContentType::from_byte

    fn ContentType::from_byte(b : Byte) -> ContentType raise RecordError

    Decodes a wire byte into a TLCP ContentType, raising InvalidContentType for unknown record types.

    ContentType::name

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

    Returns the human-readable GM/T 0024-2023 name for a TLCP record ContentType.

    ContentType::to_byte

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

    Encodes a TLCP ContentType to its wire byte value (20..23).

    RawRecord

    pub(all) struct RawRecord {
    content_type : ContentType
    version_major : Byte
    version_minor : Byte
    payload : Bytes
    } derive(Eq,
    Debug
    )

    RecordLayer

    pub struct RecordLayer {
    cipher : CipherState
    seq : UInt64
    }

    Stateful one-directional TLCP 1.1 record layer tracking cipher state and 64-bit record sequence number.

    RecordLayer::new

    Creates a new RecordLayer initialized with CipherState::null() and seq = 0.

    RecordLayer::open

    fn RecordLayer::open(self : RecordLayer, rec : RawRecord) -> Bytes raise RecordError

    Decrypts and authenticates a received RawRecord, returning the plaintext.

    • Enforces TLCP 1.1 (0x0101) version and length limits.
    • For SM4-CBC-HMAC-SM3: performs constant-time TLS padding verification, constant-time MAC extraction, and Lucky13 block-count equalization; all padding and MAC failures uniformly raise BadRecordMac.
    • For SM4-GCM: verifies 12-byte nonce, 13-byte AAD, and 16-byte tag; any authentication failure raises BadRecordMac.
    • Increments seq strictly after verification succeeds.

    RecordLayer::seal

    fn RecordLayer::seal(self : RecordLayer, content_type : ContentType, plaintext : Bytes, explicit_iv : Bytes) -> Bytes raise RecordError

    Protects a plaintext fragment and encodes a complete TLCP 1.1 wire record.

    • For Null: explicit_iv is unused.
    • For SM4-CBC-HMAC-SM3: explicit_iv must be 16 bytes.
    • For SM4-GCM: explicit_iv must be 8 bytes (appended to 4-byte implicit IV).

    Increments seq only after encryption and encoding succeed.

    RecordLayer::seq_num

    fn RecordLayer::seq_num(self : RecordLayer) -> UInt64

    Returns the current 64-bit record sequence number.

    RecordLayer::set_cipher

    fn RecordLayer::set_cipher(self : RecordLayer, cipher : CipherState) -> Unit

    Switches the active cipher state and resets the sequence number to zero per GM/T 0024-2023 §6.2.1.

    RecordView

    pub(all) struct RecordView {
    content_type : Byte
    content_type_name : String
    version_major : Byte
    version_minor : Byte
    is_tlcp_1_1 : Bool
    payload_len : Int
    total_wire_len : Int
    payload_offset : Int
    } derive(Eq,
    Debug
    )

    Read-only metadata view of a single TLCP/TLS wire record header and payload bounds.

    MAX_CIPHERTEXT_LEN

    let MAX_CIPHERTEXT_LEN : Int

    MAX_INSPECT_RECORDS

    let MAX_INSPECT_RECORDS : Int

    Maximum number of concatenated records walked by inspect_record_stream in a single call, bounding worst-case work against tiny-record stream floods.

    MAX_PLAINTEXT_LEN

    let MAX_PLAINTEXT_LEN : Int

    RECORD_HEADER_LEN

    let RECORD_HEADER_LEN : Int

    TLCP_VERSION_MAJOR

    let TLCP_VERSION_MAJOR : Byte

    TLCP_VERSION_MINOR

    let TLCP_VERSION_MINOR : Byte

    TLCP_VERSION_U16

    let TLCP_VERSION_U16 : UInt

    decode_record

    fn decode_record(buf : BytesView) -> (RawRecord, Int) raise RecordError

    Parses a single TLCP 1.1 (0x0101) record from buf, returning (record, bytes_consumed). Rejects truncated buffers with Incomplete, non-0x0101 versions with UnsupportedVersion, invalid types with InvalidContentType, and payloads exceeding MAX_CIPHERTEXT_LEN with RecordOverflow.

    encode_record

    fn encode_record(rec : RawRecord) -> Bytes raise RecordError

    Serializes a RawRecord into wire format: [content_type(1B), version_major(1B), version_minor(1B), length(2B BE), payload].

    fragment_plaintext

    fn fragment_plaintext(content_type : ContentType, data : Bytes) -> Array[RawRecord]

    Splits plaintext data into TLCP 1.1 RawRecord fragments of at most MAX_PLAINTEXT_LEN (16384) bytes each.

    inspect_record

    fn inspect_record(raw : BytesView) -> RecordView raise RecordError

    Statelessly inspects a single TLCP/TLS wire record at the start of raw.

    Validates the 5-byte record header, checks that content_type is in 20..23, sets is_tlcp_1_1 for protocol version 0x0101 (without rejecting other versions so dissectors can flag downgrade packets), enforces payload_len <= MAX_CIPHERTEXT_LEN, and checks raw.length() >= 5 + payload_len.

    inspect_record_stream

    fn inspect_record_stream(raw : BytesView) -> Array[RecordView] raise RecordError

    Statelessly walks concatenated TLCP/TLS wire records from a TCP stream slice.

    Each returned RecordView records its absolute payload_offset within raw so callers can slice payloads via raw[v.payload_offset : v.payload_offset + v.payload_len]. Returns [] on an empty slice, raises Incomplete on a truncated trailing record, and raises RecordOverflow if more than MAX_INSPECT_RECORDS records appear in one buffer.