2 DNSCrypt version 2 protocol specification
3 =========================================
8 The DNSCrypt protocol can use the UDP and TCP transport protocols.
9 DNSCrypt Clients and resolvers should support the protocol over UDP
10 and must support it over TCP.
12 The default port for this protocol should be 443, both for TCP and UDP.
14 Both the client and the resolver initially generate a short-term key
15 pair for each supported encryption system.
17 From a client perspective, a DNSCrypt session begins with the client
18 sending a non-authenticated DNS query to a DNSCrypt-enabled resolver.
19 This DNS query encodes the certificate versions supported by the
20 client, as well as a public identifier of the provider requested by
23 The resolver responds with a public set of signed certificates, that
24 must be verified by the client using a previously distributed public
25 key, known as the provider public key.
27 Each certificate includes a validity period, a serial number, a
28 version that defines a key exchange mechanism, an authenticated
29 encryption algorithm and its parameters, as well as a short-term
30 public key, known as the resolver public key.
32 A resolver can support multiple algorithms and advertise multiple
33 resolver public keys simultaneously. The client picks the one with the
34 highest serial number among the currently valid ones that match a
35 supported protocol version.
37 Each certificate includes a magic number that the client must prefix its
38 queries with, in order for the resolver to know what certificate was
39 chosen by the client to construct a given query.
41 The encryption algorithm, resolver public key and client magic number
42 from the chosen certificate are then used by the client to send
43 encrypted queries. These queries include the client public key.
45 Using this client public key, and knowing which certificate was chosen
46 by the client as well as the relevant secret key, the resolver
47 verifies and decrypts the query, and encrypts the response using the
50 2. Common definitions for client queries
51 ----------------------------------------
53 <dnscrypt-query> ::= <client-magic> <client-pk> <client-nonce> <encrypted-query>
55 <client-magic> ::= a 8 byte identifier for the resolver certificate
58 <client-pk> ::= the client's public key, whose length depends on the
59 encryption algorithm defined in the chosen certificate.
61 <client-sk> ::= the client's secret key.
63 <resolver-pk> ::= the resolver's public key.
65 <client-nonce> ::= a unique query identifier for a given
66 (<client-sk>, <resolver-pk>) tuple. The same query sent twice for the same
67 (<client-sk>, <resolver-pk>) tuple must use two distinct <client-nonce>
68 values. The length of <client-nonce> depends on the chosen encryption
71 <encrypted-query> ::= AE(<shared-key> <client-nonce> <client-nonce-pad>,
72 <client-query> <client-query-pad>)
74 AE ::= the authenticated encryption algorithm.
76 <shared-key> ::= the shared key derived from <resolver-pk> and
77 <client-sk>, using the key exchange algorithm defined in the chosen
80 <client-query> ::= the unencrypted client query. The query is not
81 modified; in particular, the query flags are not altered and the query
82 length must be kept in queries prepared to be sent over TCP.
84 <client-nonce-pad> ::= <client-nonce> length is half the nonce length
85 required by the encryption algorithm. In client queries, the other half,
86 <client-nonce-pad> is filled with NUL bytes.
88 <client-query-pad> ::= variable-length padding.
90 3. Padding for client queries over UDP
91 --------------------------------------
93 Prior to encryption, queries are padded using the ISO/IEC 7816-4
94 format. The padding starts with a byte valued 0x80 followed by a
95 variable number of NUL bytes.
97 <client-query> <client-query-pad> must be at least <min-query-len>
98 bytes. If the length of the client query is less than <min-query-len>,
99 the padding length must be adjusted in order to satisfy this
102 <min-query-len> is a variable length, initially set to 256 bytes, and
103 must be a multiple of 64 bytes.
105 4. Client queries over UDP
106 --------------------------
108 Client queries sent using UDP must be padded as described in section 3.
110 A UDP packet can contain a single query, whose entire content is
111 the <dnscrypt-query> construction documented in section 2.
113 UDP packets using the DNSCrypt protocol can be fragmented into
114 multiple IP packets and can use a single source port.
116 After having received a query, the resolver can either ignore the query
117 or reply with a DNSCrypt-encapsulated response.
119 The client must verify and decrypt the response using the resolver's
120 public key, the shared secret and the received nonce. If the response
121 cannot be verified, the response must be discarded.
123 If the response has the TC flag set, the client must:
124 1) send the query again using TCP
125 2) set the new minimum query length as:
127 <min-query-len> ::= min(<min-query-len> + 64, <max-query-len>)
129 <min-query-len> must be capped so that the full length of a DNSCrypt
130 packet doesn't exceed the maximum size required by the transport layer.
132 The client may decrease <min-query-len>, but the length must remain a multiple
135 5. Padding for client queries over TCP
136 --------------------------------------
138 Prior to encryption, queries are padded using the ISO/IEC 7816-4
139 format. The padding starts with a byte valued 0x80 followed by a
140 variable number of NUL bytes.
142 The length of <client-query-pad> is randomly chosen between 0 and 256
143 bytes, but the total length of <client-query> <client-query-pad> must be
144 a multiple of 64 bytes.
146 For example, an originally unpadded 56-bytes DNS query can be padded as:
148 <56-bytes-query> 0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00
150 <56-bytes-query> 0x80 (0x00 * 71)
152 <56-bytes-query> 0x80 (0x00 * 135)
154 <56-bytes-query> 0x80 (0x00 * 199)
156 6. Client queries over TCP
157 --------------------------
159 Client queries over TCP only differ from queries sent over UDP by the
160 padding length computation.
162 Unlike UDP queries, a query sent over TCP can be shorter than the
165 After having received a response from the resolver, the client and the
166 resolver must close the TCP connection. Multiple transactions over the
167 same TCP connections are not allowed by this revision of the protocol.
169 7. Common definitions for resolver responses
170 --------------------------------------------
172 <dnscrypt-response> ::= <resolver-magic> <nonce> <encrypted-response>
174 <resolver-magic> ::= 0x72 0x36 0x66 0x6e 0x76 0x57 0x6a 0x38
176 <nonce> ::= <client-nonce> <resolver-nonce>
178 <client-nonce> ::= the nonce sent by the client in the related query.
180 <client-pk> ::= the client's secret key.
182 <resolver-sk> ::= the resolver's public key.
184 <resolver-nonce> ::= a unique response identifier for a given
185 (<client-pk>, <resolver-sk>) tuple. The length of <resolver-nonce>
186 depends on the chosen encryption algorithm.
188 <encrypted-response> ::= AE(<shared-key>, <nonce>,
189 <resolver-response> <resolver-response-pad>)
191 AE ::= the authenticated encryption algorithm.
193 <shared-key> ::= the shared key derived from <resolver-sk> and
194 <client-pk>, using the key exchange algorithm defined in the chosen
197 <resolver-response> ::= the unencrypted resolver response. The
198 response is not modified; in particular, the query flags are not
199 altered and the response length must be kept in responses prepared to
202 <resolver-response-pad> ::= variable-length padding.
204 8. Padding for resolver responses
205 ---------------------------------
207 Prior to encryption, responses are padded using the ISO/IEC 7816-4
208 format. The padding starts with a byte valued 0x80 followed by a
209 variable number of NUL bytes.
211 The total length of <resolver-response> <resolver-response-pad> must
212 be a multiple of 64 bytes.
214 The length of <resolver-response-pad> must be between 0 and 256 bytes,
215 and must be constant for a given (<resolver-sk>, <client-nonce>) tuple.
217 A pseudo random function can be used to satisfy this requirement.
219 9. Resolver responses over UDP
220 ------------------------------
222 The resolver must verify and decrypt client queries. Queries that
223 cannot be verified must be ignored.
225 Any client-supplied nonce must be accepted. However, a resolver can
226 ignore or refuse queries encrypted using untrusted public keys.
228 Responses must be padded using the algorithm described in section 8
229 and encrypted as described in section 7.
231 If the full client query length is shorter than 256 bytes, or shorter
232 than the full response length, the resolver may truncate the response
233 and set the TC flag prior to encrypting it. The response length should
234 always be equal to or shorter than the initial client query length.
236 10. Resolver responses over TCP
237 -------------------------------
239 The resolver must verify and decrypt client queries. Queries that
240 cannot be verified must be ignored.
242 Any client-supplied nonce must be accepted. However, a resolver can
243 ignore or refuse queries encrypted using untrusted public keys.
245 Responses must be padded using the algorithm described in section 8
246 and encrypted as described in section 7.
248 Responses must be send unmodified even if their length exceeds the
249 length of the client query.
251 11. Authenticated encryption and key exchange algorithm
252 -------------------------------------------------------
254 The X25519-XSalsa20Poly1305 construction, and the way to use it
255 described in this section, must be referenced in certificates as
256 version 1 of the public-key authenticated encryption system.
258 In the current version, the construction, originally implemented in
259 the NaCl cryptographic library and exposed under the name
260 "crypto_box", uses the Curve25119 elliptic curve in Montgomery form
261 and the hsalsa20 hash function for key exchange, the XSalsa20 stream
262 cipher, and Poly1305 for message authentication.
264 The public and secret keys are 32 bytes long in storage. The MAC is 16 bytes
265 long, and is prepended to the ciphertext.
267 When using X25519-XSalsa20Poly1305, this construction requires a 24 bytes
268 nonce, that must not be reused for a given shared secret.
270 With a 24 bytes nonce, a question sent by a DNSCrypt client must be
271 encrypted using the shared secret, and a nonce constructed as follows:
272 12 bytes chosen by the client followed by 12 NUL (0) bytes.
274 A response to this question must be encrypted using the shared secret,
275 and a nonce constructed as follows: the bytes originally chosen by
276 the client, followed by bytes chosen by the resolver.
278 The resolver's half of the nonce should be randomly chosen.
280 The client's half of the nonce can include a timestamp in addition to a
281 counter or to random bytes, so that when a response is received, the
282 client can use this timestamp to immediately discard responses to
283 queries that have been sent too long ago, or dated in the future.
288 The client begins a DNSCrypt session by sending a regular unencrypted
289 TXT DNS query to the resolver IP address, on the DNSCrypt port, first
290 over UDP, then, in case of failure, timeout or truncation, over TCP.
292 Resolvers are not required to serve certificates both on UDP and TCP.
294 The name in the question must follow this scheme:
296 <provider name> ::= <protocol-major-version> . dnscrypt-cert . <zone>
298 A major protocol version has only one certificate format.
300 A DNSCrypt client implementing the second version of the protocol must
301 send a query with the TXT type and a name of the form:
303 2.dnscrypt-cert.example.com
305 The zone must be a valid DNS name, but may not be registered in the DNS
308 A single provider name can be shared by multiple resolvers operated by
309 the same entity, and a resolver can respond to multiple provider
310 names, especially to support multiple protocol versions simultaneously.
312 In order to use a DNSCrypt-enabled resolver, a client must know the
313 following information:
315 - The resolver IP address and port
317 - The provider public key
319 The provider public key is a long-term key whose sole purpose is to
320 verify the certificates. It is never used to encrypt or verify DNS queries.
321 A unique provider public key can be used to sign multiple certificates.
323 For example, an organization operating multiple resolvers can use
324 a unique provider name and provider public key across all resolvers,
325 and just provide a list of IP addresses and ports. Each resolver may
326 have its unique set of certificates that can be signed with the
329 Certificates should be signed on dedicated hardware and not on the
330 resolvers. Resolvers must serve the certificates, provided that they
331 have already been signed.
333 A successful response to certificate request contains one or more TXT
334 records, each record containing a certificate encoded as follows:
336 <cert> ::= <cert-magic> <es-version> <protocol-minor-version> <signature>
337 <resolver-pk> <client-magic> <serial> <ts-start> <ts-end>
340 <cert-magic> ::= 0x44 0x4e 0x53 0x43
342 <es-version> ::= the cryptographic construction to use with this
344 For X25519-XSalsa20Poly1305, <es-version> must be 0x00 0x01.
345 For X25519-Chacha20Poly1305, <es-version> must be 0x00 0x02.
347 <protocol-minor-version> ::= 0x00 0x00
349 <signature> ::= a 64-byte signature of (<resolver-pk> <client-magic>
350 <serial> <ts-start> <ts-end> <extensions>) using the Ed25519 algorithm and the
351 provider secret key. Ed25519 must be used in this version of the
354 <resolver-pk> ::= the resolver short-term public key, which is 32 bytes when
357 <client-magic> ::= the first 8 bytes of a client query that was built
358 using the information from this certificate. It may be a truncated
359 public key. Two valid certificates cannot share the same <client-magic>.
361 <serial> ::= a 4 byte serial number in big-endian format. If more than
362 one certificates are valid, the client must prefer the certificate
363 with a higher serial number.
365 <ts-start> ::= the date the certificate is valid from, as a 4-byte
366 unsigned Unix timestamp.
368 <ts-end> ::= the date the certificate is valid until (inclusive), as a
369 4-byte unsigned Unix timestamp.
371 <extensions> ::= empty in the current protocol version, but may
372 contain additional data in future revisions, including minor versions.
373 The computation and the verification of the signature must include the
374 extensions. An implementation not supporting these extensions must
377 Certificates made of these information, without extensions, are 116 bytes
378 long. With the addition of the cert-magic, es-version and
379 protocol-minor-version, the record is 124 bytes long.
381 After having received a set of certificates, the client checks their
382 validity based on the current date, filters out the ones designed for
383 encryption systems that are not supported by the client, and chooses
384 the certificate with the higher serial number.
386 DNSCrypt queries sent by the client must use the <client-magic>
387 header of the chosen certificate, as well as the specified encryption
388 system and public key.
390 The client must check for new certificates every hour, and switch to a
391 new certificate if the current certificate is not present or not valid
394 The client shouldn't switch to a new certificate if the current one is
395 still valid and still present in the set of returned certificates,
396 even if the new set includes certificates with a higher serial number
397 than the current one.
399 13. Operational perspective
400 ---------------------------
402 Special attention should be paid to the uniqueness of the generated
405 Client public keys can be used by resolvers to authenticate clients,
406 link queries to customer accounts, and unlock business-specific
407 features such as redirecting specific domain names to a sinkhole.
409 Resolvers accessible from any client IP address can also opt for only
410 responding to a set of whitelisted public keys.
412 Resolvers accepting queries from any client must accept any client
413 public key. In particular, an anonymous client can generate a new key pair
414 for every session, or even for every query. This mitigates the ability
415 for a resolver to group queries by client public keys, and discover
416 the set of IP addresses a user might have been operating.
418 Resolvers that do not keep query logs should rotate the short-term key pair
419 every 24 hours, and must throw away the previous secret key.
421 During a key rotation, and provided that the old key hasn't been
422 compromised, a resolver shoud accept both the old and the new key for at
423 least 4 hours, and public them as different certificates.
425 Provider public keys may be published as a DNSSEC-signed TXT records,
426 in the same zone as the provider name.
428 For example, a query for the TXT type on the name "2.pubkey.example.com"
429 may return a signed record containing a hexadecimal-encoded provider
430 public key for the provider name "2.dnscrypt-cert.example.com".
432 While authenticated and unauthenticated queries can share the same
433 resolver TCP and/or UDP port, this should be avoided. Client magic
434 numbers do not completely prevent collisions with legitimate unauthenticated
435 DNS queries. In addition, DNSCrypt offers some mitigation against
436 abusing resolvers to conduct DDoS attacks. Accepting unauthenticated
437 queries on the same port would defeat this mechanism.
439 As this version of the protocol may require many short-lived TCP
440 sessions, the TCP Fast Open mechanism should be used where available.
445 Known open source implementations of the DNSCrypt version 2 protocol are:
447 - dnscrypt-wrapper - server-side implementation in C
448 - powerdns dnsdist - a DNS loadbalancer that provides server-side DNSCrypt
449 - dnscrypt-proxy - client-side implementation in C
450 - pcap_dnsproxy - client-side implementation in C++
451 - dnscrypt-python - client-side implementation in Python
453 Known closed source implementations of the protocol :
455 - OpenNIC Fusl servers - server-side implementation as a Linux kernel module