]> Cypherpunks.ru repositories - nncp.git/blob - doc/pkt.texi
9c8c32e262da4a90ce1578b469eeec9c46f79f6f
[nncp.git] / doc / pkt.texi
1 @node Packet
2 @unnumbered Packet format
3
4 All packets are
5 @url{https://tools.ietf.org/html/rfc4506, XDR}-encoded structures.
6
7 @menu
8 * Plain packet: Plain.
9 * Encrypted packet: Encrypted.
10 @end menu
11
12 @node Plain
13 @section Plain packet
14
15 Plain packet contains either the whole file, or file request (freq), or
16 transition packet or email message. It is called "plain", because it
17 contains plaintext, but plain packets would never be stored on your hard
18 drive.
19
20 @verbatim
21             HEADER
22 +--------------------------------------+--...---+
23 | MAGIC | TYPE | NICE | PATHLEN | PATH | PAYLOAD|
24 +--------------------------------------+--...---+
25 @end verbatim
26
27 @multitable @columnfractions 0.2 0.3 0.5
28 @headitem @tab XDR type @tab Value
29 @item Magic number @tab
30     8-byte, fixed length opaque data @tab
31     @verb{|N N C P P 0x00 0x00 0x01|}
32 @item Payload type @tab
33     unsigned integer @tab
34     0 (file), 1 (freq), 2 (mail), 3 (transition)
35 @item Niceness @tab
36     unsigned integer @tab
37     1-255, preferred packet @ref{Niceness, niceness} level
38 @item Path length @tab
39     unsigned integer @tab
40     actual length of @emph{path} field's payload
41 @item Path @tab
42     255 byte, fixed length opaque data @tab
43     @itemize
44     @item UTF-8 encoded destination path for file transfer
45     @item UTF-8 encoded source path for file request
46     @item UTF-8 encoded, space separated, email recipients list
47     @item Node's id the transition packet must be relayed on
48     @end itemize
49 @end multitable
50
51 Path has fixed size because of hiding its actual length -- it is
52 valuable metadata. Payload is appended to the header -- it is not stored
53 as XDR field, because most XDR libraries will store all that data in the
54 memory.
55
56 Depending on the packet's type, payload could store:
57
58 @itemize
59 @item File contents
60 @item Destination path for freq
61 @item @url{http://zlib.net/, zlib} compressed email
62 @item Whole encrypted packet we need to relay on
63 @end itemize
64
65 Also depending on packet's type, niceness level means:
66
67 @itemize
68 @item Preferable niceness level for files sent by freq
69 @item @env{NNCP_NICE} variable's value passed during
70     @ref{CfgSendmail} invocation.
71 @end itemize
72
73 @node Encrypted
74 @section Encrypted packet
75
76 Encrypted packets are the only files found in spools, in exchangeable
77 storages and that are synchronized between TCP daemons.
78
79 Each encrypted packet has the following header:
80
81 @verbatim
82   +------------ HEADER --------------------+   +-------- ENCRYPTED --------+
83  /                                          \ /                             \
84 +--------------------------------------------+------------+----...-----------+------+
85 | MAGIC | NICE | SENDER | RCPT | EPUB | SIGN | SIZE | MAC | CIPHERTEXT | MAC | JUNK |
86 +-------------------------------------/------\------------+----...-----------+------+
87                                      /        \
88                       +-------------------------------------+
89                       | MAGIC | NICE | SENDER | RCPT | EPUB |
90                       +-------------------------------------+
91 @end verbatim
92
93 @multitable @columnfractions 0.2 0.3 0.5
94 @headitem @tab XDR type @tab Value
95 @item Magic number @tab
96     8-byte, fixed length opaque data @tab
97     @verb{|N N C P E 0x00 0x00 0x03|}
98 @item Niceness @tab
99     unsigned integer @tab
100     1-255, packet @ref{Niceness, niceness} level
101 @item Sender @tab
102     32-byte, fixed length opaque data @tab
103     Sender node's id
104 @item Recipient @tab
105     32-byte, fixed length opaque data @tab
106     Recipient node's id
107 @item Exchange public key @tab
108     32-byte, fixed length opaque data @tab
109     Ephemeral curve25519 public key
110 @item Signature @tab
111     64-byte, fixed length opaque data @tab
112     ed25519 signature for that packet's header
113 @end multitable
114
115 Signature is calculated over all previous fields.
116
117 All following encryption is done using @url{https://cr.yp.to/chacha.html,
118 ChaCha20} algorithm. Data is splitted on 128 KiB blocks. Each block is
119 encrypted with increasing nonce counter. @url{https://blake2.net/,
120 BLAKE2b-256} MAC is appended to the ciphertext.
121
122 After the headers comes an encrypted payload size and MAC of that size.
123
124 @multitable @columnfractions 0.2 0.3 0.5
125 @headitem @tab XDR type @tab Value
126 @item Size @tab
127     unsigned hyper integer @tab
128     Payload size.
129 @end multitable
130
131 Next comes the actual encrypted payload with corresponding MAC.
132
133 Each node has static @strong{exchange} and @strong{signature} keypairs.
134 When node A want to send encrypted packet to node B, it:
135
136 @enumerate
137 @item generates ephemeral @url{http://cr.yp.to/ecdh.html, curve25519} keypair
138 @item prepares structure for signing
139 @item signs that structure using private
140     @url{http://ed25519.cr.yp.to/, ed25519} signature key
141 @item takes remote node's exchange public key and performs
142     Diffie-Hellman computation on this remote static public key and
143     private ephemeral one
144 @item derive the keys:
145     @enumerate
146     @item initialize @url{https://blake2.net/, BLAKE2Xb} XOF with
147     derived ephemeral key and 224-byte output length
148     @item feed @verb{|N N C P E 0x00 0x00 0x03|} magic number to XOF
149     @item read 32-bytes of "size" encryption key (for ChaCha20)
150     @item read 64-bytes of "size" authentication key (for BLAKE2b-MAC)
151     @item read 32-bytes of payload encryption key
152     @item read 64-bytes of payload authentication key
153     @item optionally read 32-bytes pad generation key (for ChaCha20)
154     @end enumerate
155 @item encrypts size, appends its ciphertext to the header
156 @item appends MAC tag over that ciphertext
157 @item encrypts and appends payload ciphertext
158 @item appends MAC tag over that payload ciphertext
159 @item possibly appends any kind of "junk" noise data to hide real
160     payload's size from the adversary
161 @end enumerate