]> Cypherpunks.ru repositories - nncp.git/blobdiff - doc/pkt.texi
Remove double spaces
[nncp.git] / doc / pkt.texi
index 87484a107a82e99be1f86d7321dec20a44472acb..4549505961533aed1290bffe37d79f85824abb60 100644 (file)
@@ -2,8 +2,7 @@
 @unnumbered Packet format
 
 All packets are
-@url{https://en.wikipedia.org/wiki/External_Data_Representation,
-XDR}-encoded structures.
+@url{https://tools.ietf.org/html/rfc4506, XDR}-encoded structures.
 
 @menu
 * Plain packet: Plain.
@@ -18,8 +17,6 @@ transition packet or email message. It is called "plain", because it
 contains plaintext, but plain packets would never be stored on your hard
 drive.
 
-Each packet has the following header:
-
 @verbatim
             HEADER
 +-------------------------------+--...---+
@@ -28,35 +25,32 @@ Each packet has the following header:
 @end verbatim
 
 @multitable @columnfractions 0.2 0.3 0.5
-@headitem  @tab XDR type @tab Value
-
+@headitem @tab XDR type @tab Value
 @item Magic number @tab
-  8-byte, fixed length opaque data @tab
-  @code{NNCPP0x10x00x00}
-
+    8-byte, fixed length opaque data @tab
+    @verb{|N N C P P 0x00 0x00 0x01|}
 @item Payload type @tab
-  unsigned integer @tab
-  0 (file), 1 (freq), 2 (mail), 3 (transition)
-
+    unsigned integer @tab
+    0 (file), 1 (freq), 2 (mail), 3 (transition)
 @item Path length @tab
-  unsigned integer @tab
-  actual length of following field's payload
-
+    unsigned integer @tab
+    actual length of @emph{path} field's payload
 @item Path @tab
-  255 byte, fixed length opaque data @tab
-  @itemize
-  @item UTF-8 encoded destination path for file transfer
-  @item UTF-8 encoded source path for file request
-  @item UTF-8 encoded, space separated, email recipients list
-  @item Node id the transition packet must be relayed on
-  @end itemize
-
+    255 byte, fixed length opaque data @tab
+    @itemize
+    @item UTF-8 encoded destination path for file transfer
+    @item UTF-8 encoded source path for file request
+    @item UTF-8 encoded, space separated, email recipients list
+    @item Node's id the transition packet must be relayed on
+    @end itemize
 @end multitable
 
 Path has fixed size because of hiding its actual length -- it is
-valuable metadata.
+valuable metadata. Payload is appended to the header -- it is not stored
+as XDR field, because most XDR libraries will store all that data in the
+memory.
 
-Actual payload comes after that header to the very end:
+Depending on the packet's type, payload could store:
 
 @itemize
 @item File contents
@@ -74,44 +68,34 @@ storages and that are synchronized between TCP daemons.
 Each encrypted packet has the following header:
 
 @verbatim
-                    HEADER
-+--------------------------------------------+-------...--------+
-| MAGIC | NICE | SENDER | EPUB | SIGN | SIZE | CIPHERTEXT | MAC |
-+------------------------------/------\------+-------...--------+
+  +------------ HEADER -------------+   +-------- ENCRYPTED --------+
+ /                                   \ /                             \
++-------------------------------------+------------+----...-----------+------+
+| MAGIC | NICE | SENDER | EPUB | SIGN | SIZE | MAC | CIPHERTEXT | MAC | JUNK |
++------------------------------/------\------------+----...-----------+------+
                               /        \
-             +--------------------------------------------+
-             | MAGIC | NICE | RCPT | SENDER | EPUB | SIZE |
-             +--------------------------------------------+
+               +-------------------------------------+
+               | MAGIC | NICE | RCPT | SENDER | EPUB |
+               +-------------------------------------+
 @end verbatim
 
 @multitable @columnfractions 0.2 0.3 0.5
-@headitem  @tab XDR type @tab Value
-
+@headitem @tab XDR type @tab Value
 @item Magic number @tab
-  8-byte, fixed length opaque data @tab
-  @code{NNCPE0x10x00x00}
-
+    8-byte, fixed length opaque data @tab
+    @verb{|N N C P E 0x00 0x00 0x01|}
 @item Niceness @tab
-  unsigned integer @tab
-  1-255, packet niceness level, its priority.
-  Lower value means higher precedence
-
+    unsigned integer @tab
+    1-255, packet @ref{Niceness, niceness} level
 @item Sender @tab
-  32-byte, fixed length opaque data @tab
-  Sender node's id
-
+    32-byte, fixed length opaque data @tab
+    Sender node's id
 @item Exchange public key @tab
-  32-byte, fixed length opaque data @tab
-  Ephemeral curve25519 public key
-
+    32-byte, fixed length opaque data @tab
+    Ephemeral curve25519 public key
 @item Signature @tab
-  64-byte, fixed length opaque data @tab
-  ed25519 signature for that encrypted packet
-
-@item Size @tab
-  unsigned hyper integer @tab
-  Encrypted payload size
-
+    64-byte, fixed length opaque data @tab
+    ed25519 signature for that packet's header
 @end multitable
 
 Signature is calculated over the following structure:
@@ -122,35 +106,52 @@ Signature is calculated over the following structure:
 @item Recipient (32-byte recipient node's id)
 @item Sender
 @item Exchange public key
-@item Size
 @end itemize
 
-Actual encrypted payload comes after that header. Payload is encrypted
-using @url{https://www.schneier.com/academic/twofish/, Twofish}
-algorithm with 256-bit key in
+All following encryption is done using
+@url{https://www.schneier.com/academic/twofish/, Twofish} algorithm with
+256-bit key in
 @url{https://en.wikipedia.org/wiki/Counter_mode#Counter_.28CTR.29, CTR}
 mode of operation with zero initialization vector (because each
-encrypted packet has ephemeral exchange key). Ciphertext's length is
-equal to plaintext. @url{https://blake2.net/, BLAKE2b-256} MAC is
-appended to the ciphertext.
+encrypted packet has ephemeral exchange key). @url{https://blake2.net/,
+BLAKE2b-256} MAC is appended to the ciphertext.
+
+After the headers comes an encrypted payload size and MAC of that size.
+
+@multitable @columnfractions 0.2 0.3 0.5
+@headitem @tab XDR type @tab Value
+@item Size @tab
+    unsigned hyper integer @tab
+    Payload size.
+@end multitable
+
+Next comes the actual encrypted payload with corresponding MAC.
 
 Each node has static @strong{exchange} and @strong{signature} keypairs.
 When node A want to send encrypted packet to node B, it:
 
 @enumerate
 @item generates ephemeral @url{http://cr.yp.to/ecdh.html, curve25519} keypair
-@item prepares structure for signing (underlying payload size must be
-already known)
-@item signs that structure using private @url{http://ed25519.cr.yp.to/,
-ed25519} signature key
+@item prepares structure for signing
+@item signs that structure using private
+    @url{http://ed25519.cr.yp.to/, ed25519} signature key
 @item takes remote node's exchange public key and performs
-Diffie-Hellman computation on this remote static public key and private
-ephemeral one
-@item derived ephemeral key used as an input to
-@url{https://en.wikipedia.org/wiki/HKDF, HKDF}-BLAKE2b-256 key
-derivation function
-@item two 256-bit keys are derived from it for using with Twofish and
-BLAKE2b-MAC functions
-@item Twofish encryption and BLAKE2b-MACing is performed over the
-plaintext. Ciphertext and MAC tag are appended to the header
+    Diffie-Hellman computation on this remote static public key and
+    private ephemeral one
+@item derived ephemeral key is used as an input to
+    @url{https://en.wikipedia.org/wiki/HKDF, HKDF}-BLAKE2b-256 KDF
+@item derives four session keys using
+    @url{https://en.wikipedia.org/wiki/HKDF, HKDF}-BLAKE2b-256 KDF:
+    @enumerate
+    @item "Size" encryption (for Twofish) key
+    @item "Size" authentication (for BLAKE2b-MAC) key
+    @item Payload encryption key
+    @item Payload authentication key
+    @end enumerate
+@item encrypts size, appends its ciphertext to the header
+@item appends MAC tag over that ciphertext
+@item encrypts and appends payload ciphertext
+@item appends MAC tag over that payload ciphertext
+@item possibly appends any kind of "junk" noise data to hide real
+    payload's size from the adversary
 @end enumerate