Class CdirMessage
java.lang.Object
net.vincent.communidirect.common.proto.CdirMessage
Codec for the CDIR v1 message protocol.
Wire format (all integers big-endian)
Offset Size Field
------ ---- -----------------------------------------------
0 4 Magic 0x43 0x44 0x49 0x52 ("CDIR")
4 1 Version 0x01
5 64 Signature Ed25519 sig over payload plaintext
69 32 Sender PubKey Raw 32-byte Ed25519 public key
101 256 Sealed Sess. Key XOR-wrapped 32-byte session key, zero-padded
357 4 Payload Length Unsigned 32-bit big-endian int
361 var XORed Payload Payload XOR'd with repeating session key
Session key wrapping
Because Ed25519 is a signing algorithm (not a KEM), the session key is wrapped using a key-derivation function over the recipient's public key:wrapKey = SHA-256(recipientRawPubKey32) // 32 bytes sealed = XOR(sessionKey32, wrapKey) // 32 bytes wire[0..31] = sealed; wire[32..255] = 0x00 // 256 bytes totalThe recipient unseals by computing
SHA-256(ownRawPubKey32) and
XOR-ing it against the first 32 bytes of the field.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intTotal size of the fixed message header in bytes: 361 (4 + 1 + 64 + 32 + 256 + 4).static final intFour ASCII bytes "CDIR" interpreted as a big-endian int.final byte[]Decrypted (plaintext) payload bytes.final StringHex-encoded SHA-256 digest of the sender's raw public key – used as a human-readable identity label in stored message headers.final byte[]32-byte raw Ed25519 public key of the sender.static final byteProtocol version byte: 0x01 for CDIR v1. -
Method Summary
Modifier and TypeMethodDescriptionstatic CdirMessagedecode(InputStream in, PrivateKey recipientPrivKey, byte[] recipientPubRaw) Reads one CDIR frame fromin, verifies its signature, decrypts the payload and returns a populatedCdirMessage.static voidencode(byte[] payload, PrivateKey senderPrivKey, byte[] senderPubKeyRaw, byte[] recipientPubRaw, OutputStream out) Encodes a plaintextpayloadinto a CDIR frame and writes it toout.
-
Field Details
-
MAGIC
public static final int MAGICFour ASCII bytes "CDIR" interpreted as a big-endian int.- See Also:
-
VERSION
public static final byte VERSIONProtocol version byte: 0x01 for CDIR v1.- See Also:
-
HEADER_SIZE
public static final int HEADER_SIZETotal size of the fixed message header in bytes: 361 (4 + 1 + 64 + 32 + 256 + 4).- See Also:
-
senderPubKeyRaw
public final byte[] senderPubKeyRaw32-byte raw Ed25519 public key of the sender. -
payload
public final byte[] payloadDecrypted (plaintext) payload bytes. -
senderPubKeyHash
Hex-encoded SHA-256 digest of the sender's raw public key – used as a human-readable identity label in stored message headers.
-
-
Method Details
-
encode
public static void encode(byte[] payload, PrivateKey senderPrivKey, byte[] senderPubKeyRaw, byte[] recipientPubRaw, OutputStream out) throws Exception Encodes a plaintextpayloadinto a CDIR frame and writes it toout.- Parameters:
payload- plaintext bytes to sendsenderPrivKey- sender's Ed25519 private key (for signing)senderPubKeyRaw- sender's 32-byte raw Ed25519 public keyrecipientPubRaw- recipient's 32-byte raw Ed25519 public key (used to wrap the session key)out- destination stream- Throws:
Exception- on any crypto or IO failure
-
decode
public static CdirMessage decode(InputStream in, PrivateKey recipientPrivKey, byte[] recipientPubRaw) throws Exception Reads one CDIR frame fromin, verifies its signature, decrypts the payload and returns a populatedCdirMessage.- Parameters:
in- source stream (must be positioned at the first magic byte)recipientPrivKey- server's Ed25519 private key (unused for Ed25519 KEM – kept for future asymmetric upgrade)recipientPubRaw- server's own 32-byte raw Ed25519 public key (used to unseal the session key)- Returns:
- decoded, signature-verified message
- Throws:
IOException- on read errorsSignatureException- if the Ed25519 signature does not verifyException- on any other crypto or protocol error
-