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;
58 size_t signed_certs_count;
59 uint8_t provider_publickey[crypto_sign_ed25519_PUBLICKEYBYTES];
60 uint8_t provider_secretkey[crypto_sign_ed25519_SECRETKEYBYTES];
62 size_t keypairs_count;
63 uint64_t nonce_ts_last;
64 unsigned char hash_key[crypto_shorthash_KEYBYTES];
66 struct slabhash *shared_secrets_cache;
67 /** lock on shared secret cache counters */
68 lock_basic_type shared_secrets_cache_lock;
69 /** number of misses from shared_secrets_cache */
70 size_t num_query_dnscrypt_secret_missed_cache;
73 struct dnscrypt_query_header {
74 uint8_t magic_query[DNSCRYPT_MAGIC_HEADER_LEN];
75 uint8_t publickey[crypto_box_PUBLICKEYBYTES];
76 uint8_t nonce[crypto_box_HALF_NONCEBYTES];
77 uint8_t mac[crypto_box_MACBYTES];
81 * Initialize DNSCrypt environment.
82 * Initialize sodium library and allocate the dnsc_env structure.
83 * \return an uninitialized struct dnsc_env.
85 struct dnsc_env * dnsc_create(void);
88 * Apply configuration.
89 * Read certificates and secret keys from configuration. Initialize hashkey and
90 * provider name as well as loading cert TXT records.
91 * In case of issue applying configuration, this function fatals.
92 * \param[in] env the struct dnsc_env to populate.
93 * \param[in] cfg the config_file struct with dnscrypt options.
94 * \return 0 on success.
96 int dnsc_apply_cfg(struct dnsc_env *env, struct config_file *cfg);
99 * Delete DNSCrypt environment
102 void dnsc_delete(struct dnsc_env *env);
105 * handle a crypted dnscrypt request.
106 * Determine wether or not a query is coming over the dnscrypt listener and
107 * attempt to uncurve it or detect if it is a certificate query.
108 * return 0 in case of failure.
110 int dnsc_handle_curved_request(struct dnsc_env* dnscenv,
111 struct comm_reply* repinfo);
113 * handle an unencrypted dnscrypt request.
114 * Determine wether or not a query is going over the dnscrypt channel and
115 * attempt to curve it unless it was not crypted like when it is a
117 * \return 0 in case of failure.
120 int dnsc_handle_uncurved_request(struct comm_reply *repinfo);
123 * Computes the size of the shared secret cache entry.
125 size_t dnsc_shared_secrets_sizefunc(void *k, void *d);
128 * Compares two shared secret cache keys.
130 int dnsc_shared_secrets_compfunc(void *m1, void *m2);
133 * Function to delete a shared secret cache key.
135 void dnsc_shared_secrets_delkeyfunc(void *k, void* arg);
138 * Function to delete a share secret cache value.
140 void dnsc_shared_secrets_deldatafunc(void* d, void* arg);
142 #endif /* USE_DNSCRYPT */