1 #ifndef UNBOUND_DNSCRYPT_H
2 #define UNBOUND_DNSCRYPT_H
6 * dnscrypt functions for encrypting DNS packets.
9 #include "dnscrypt/dnscrypt_config.h"
12 #define DNSCRYPT_MAGIC_HEADER_LEN 8U
13 #define DNSCRYPT_MAGIC_RESPONSE "r6fnvWj8"
15 #ifndef DNSCRYPT_MAX_PADDING
16 # define DNSCRYPT_MAX_PADDING 256U
18 #ifndef DNSCRYPT_BLOCK_SIZE
19 # define DNSCRYPT_BLOCK_SIZE 64U
21 #ifndef DNSCRYPT_MIN_PAD_LEN
22 # define DNSCRYPT_MIN_PAD_LEN 8U
25 #define crypto_box_HALF_NONCEBYTES (crypto_box_NONCEBYTES / 2U)
28 #include "dnscrypt/cert.h"
29 #include "util/locks.h"
31 #define DNSCRYPT_QUERY_HEADER_SIZE \
32 (DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_PUBLICKEYBYTES + crypto_box_HALF_NONCEBYTES + crypto_box_MACBYTES)
33 #define DNSCRYPT_RESPONSE_HEADER_SIZE \
34 (DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_NONCEBYTES + crypto_box_MACBYTES)
36 #define DNSCRYPT_REPLY_HEADER_SIZE \
37 (DNSCRYPT_MAGIC_HEADER_LEN + crypto_box_HALF_NONCEBYTES * 2 + crypto_box_MACBYTES)
44 typedef struct KeyPair_ {
45 uint8_t crypt_publickey[crypto_box_PUBLICKEYBYTES];
46 uint8_t crypt_secretkey[crypto_box_SECRETKEYBYTES];
49 typedef struct cert_ {
50 uint8_t magic_query[DNSCRYPT_MAGIC_HEADER_LEN];
51 uint8_t es_version[2];
56 struct SignedCert *signed_certs;
57 struct SignedCert **rotated_certs;
59 size_t signed_certs_count;
60 size_t rotated_certs_count;
61 uint8_t provider_publickey[crypto_sign_ed25519_PUBLICKEYBYTES];
62 uint8_t provider_secretkey[crypto_sign_ed25519_SECRETKEYBYTES];
64 size_t keypairs_count;
65 uint64_t nonce_ts_last;
66 unsigned char hash_key[crypto_shorthash_KEYBYTES];
70 struct slabhash *shared_secrets_cache;
71 /** lock on shared secret cache counters */
72 lock_basic_type shared_secrets_cache_lock;
73 /** number of misses from shared_secrets_cache */
74 size_t num_query_dnscrypt_secret_missed_cache;
76 /** slabhash keeping track of nonce/cient pk/server sk pairs. */
77 struct slabhash *nonces_cache;
78 /** lock on nonces_cache, used to avoid race condition in updating the hash */
79 lock_basic_type nonces_cache_lock;
80 /** number of replayed queries */
81 size_t num_query_dnscrypt_replay;
84 struct dnscrypt_query_header {
85 uint8_t magic_query[DNSCRYPT_MAGIC_HEADER_LEN];
86 uint8_t publickey[crypto_box_PUBLICKEYBYTES];
87 uint8_t nonce[crypto_box_HALF_NONCEBYTES];
88 uint8_t mac[crypto_box_MACBYTES];
92 * Initialize DNSCrypt environment.
93 * Initialize sodium library and allocate the dnsc_env structure.
94 * \return an uninitialized struct dnsc_env.
96 struct dnsc_env * dnsc_create(void);
99 * Apply configuration.
100 * Read certificates and secret keys from configuration. Initialize hashkey and
101 * provider name as well as loading cert TXT records.
102 * In case of issue applying configuration, this function fatals.
103 * \param[in] env the struct dnsc_env to populate.
104 * \param[in] cfg the config_file struct with dnscrypt options.
105 * \return 0 on success.
107 int dnsc_apply_cfg(struct dnsc_env *env, struct config_file *cfg);
110 * Delete DNSCrypt environment
113 void dnsc_delete(struct dnsc_env *env);
116 * handle a crypted dnscrypt request.
117 * Determine wether or not a query is coming over the dnscrypt listener and
118 * attempt to uncurve it or detect if it is a certificate query.
119 * return 0 in case of failure.
121 int dnsc_handle_curved_request(struct dnsc_env* dnscenv,
122 struct comm_reply* repinfo);
124 * handle an unencrypted dnscrypt request.
125 * Determine wether or not a query is going over the dnscrypt channel and
126 * attempt to curve it unless it was not crypted like when it is a
128 * \return 0 in case of failure.
131 int dnsc_handle_uncurved_request(struct comm_reply *repinfo);
134 * Computes the size of the shared secret cache entry.
136 size_t dnsc_shared_secrets_sizefunc(void *k, void *d);
139 * Compares two shared secret cache keys.
141 int dnsc_shared_secrets_compfunc(void *m1, void *m2);
144 * Function to delete a shared secret cache key.
146 void dnsc_shared_secrets_delkeyfunc(void *k, void* arg);
149 * Function to delete a share secret cache value.
151 void dnsc_shared_secrets_deldatafunc(void* d, void* arg);
154 * Computes the size of the nonce cache entry.
156 size_t dnsc_nonces_sizefunc(void *k, void *d);
159 * Compares two nonce cache keys.
161 int dnsc_nonces_compfunc(void *m1, void *m2);
164 * Function to delete a nonce cache key.
166 void dnsc_nonces_delkeyfunc(void *k, void* arg);
169 * Function to delete a nonce cache value.
171 void dnsc_nonces_deldatafunc(void* d, void* arg);
174 #endif /* USE_DNSCRYPT */