]> git.xonotic.org Git - xonotic/darkplaces.git/blob - crypto.h
CREDITS: Add name
[xonotic/darkplaces.git] / crypto.h
1 #ifndef CRYPTO_H
2 #define CRYPTO_H
3
4 #include <stddef.h>
5 #include "qtypes.h"
6 struct lhnetaddress_s;
7
8 extern struct cvar_s crypto_developer;
9 extern struct cvar_s crypto_aeslevel;
10 #define ENCRYPTION_REQUIRED (crypto_aeslevel.integer >= 3)
11
12 extern int crypto_keyfp_recommended_length; // applies to LOCAL IDs, and to ALL keys
13
14 #define CRYPTO_HEADERSIZE 31
15 // AES case causes 16 to 31 bytes overhead
16 // SHA256 case causes 16 bytes overhead as we truncate to 128bit
17
18 #define FP64_SIZE 44
19 #define DHKEY_SIZE 16
20
21 typedef struct
22 {
23         unsigned char dhkey[DHKEY_SIZE]; // shared key, not NUL terminated
24         char client_idfp[FP64_SIZE+1];
25         char client_keyfp[FP64_SIZE+1];
26         qbool client_issigned;
27         char server_idfp[FP64_SIZE+1];
28         char server_keyfp[FP64_SIZE+1];
29         qbool server_issigned;
30         qbool authenticated;
31         qbool use_aes;
32         void *data;
33 }
34 crypto_t;
35
36 void Crypto_Init(void);
37 void Crypto_Init_Commands(void);
38 void Crypto_LoadKeys(void); // NOTE: when this is called, the SV_LockThreadMutex MUST be active
39 void Crypto_Shutdown(void);
40 qbool Crypto_Available(void);
41 void sha256(unsigned char *out, const unsigned char *in, int n); // may ONLY be called if Crypto_Available()
42 const void *Crypto_EncryptPacket(crypto_t *crypto, const void *data_src, size_t len_src, void *data_dst, size_t *len_dst, size_t len);
43 const void *Crypto_DecryptPacket(crypto_t *crypto, const void *data_src, size_t len_src, void *data_dst, size_t *len_dst, size_t len);
44 #define CRYPTO_NOMATCH 0        // process as usual (packet was not used)
45 #define CRYPTO_MATCH 1          // process as usual (packet was used)
46 #define CRYPTO_DISCARD 2        // discard this packet
47 #define CRYPTO_REPLACE 3        // make the buffer the current packet
48 int Crypto_ClientParsePacket(const char *data_in, size_t len_in, char *data_out, size_t *len_out, struct lhnetaddress_s *peeraddress);
49 int Crypto_ServerParsePacket(const char *data_in, size_t len_in, char *data_out, size_t *len_out, struct lhnetaddress_s *peeraddress);
50
51 // if len_out is nonzero, the packet is to be sent to the client
52
53 qbool Crypto_ServerAppendToChallenge(const char *data_in, size_t len_in, char *data_out, size_t *len_out, size_t maxlen);
54 crypto_t *Crypto_ServerGetInstance(struct lhnetaddress_s *peeraddress);
55 qbool Crypto_FinishInstance(crypto_t *out, crypto_t *in); // also clears allocated memory, and frees the instance received by ServerGetInstance
56 const char *Crypto_GetInfoResponseDataString(void);
57
58 // retrieves a host key for an address (can be exposed to menuqc, or used by the engine to look up stored keys e.g. for server bookmarking)
59 // pointers may be NULL
60 qbool Crypto_RetrieveHostKey(struct lhnetaddress_s *peeraddress, int *keyid, char *keyfp, size_t keyfplen, char *idfp, size_t idfplen, int *aeslevel, qbool *issigned);
61 int Crypto_RetrieveLocalKey(int keyid, char *keyfp, size_t keyfplen, char *idfp, size_t idfplen, qbool *issigned); // return value: -1 if more to come, +1 if valid, 0 if end of list
62
63 size_t Crypto_SignData(const void *data, size_t datasize, int keyid, void *signed_data, size_t signed_size);
64 size_t Crypto_SignDataDetached(const void *data, size_t datasize, int keyid, void *signed_data, size_t signed_size);
65
66 // netconn protocol:
67 //   non-crypto:
68 //     getchallenge                                            >
69 //                                                             < challenge
70 //     connect                                                 >
71 //                                                             < accept (or: reject)
72 //   crypto:
73 //     getchallenge                                            >
74 //                                                             < challenge SP <challenge> NUL vlen <size> d0pk <fingerprints I can auth to> NUL NUL <other fingerprints I accept>
75 //
76 //     IF serverfp:
77 //     d0pk\cnt\0\challenge\<challenge>\aeslevel\<level> NUL <serverfp> NUL <clientfp>
78 //                                                             >
79 //                                                               check if client would get accepted; if not, do "reject" now
80 //     require non-control packets to be encrypted               require non-control packets to be encrypted
81 //     do not send anything yet                                  do not send anything yet
82 //     RESET to serverfp                                         RESET to serverfp
83 //                                                               d0_blind_id_authenticate_with_private_id_start() = 1
84 //                                                             < d0pk\cnt\1\aes\<aesenabled> NUL *startdata*
85 //     d0_blind_id_authenticate_with_private_id_challenge() = 1
86 //     d0pk\cnt\2 NUL *challengedata*                          >
87 //                                                               d0_blind_id_authenticate_with_private_id_response() = 0
88 //                                                             < d0pk\cnt\3 NUL *responsedata*
89 //     d0_blind_id_authenticate_with_private_id_verify() = 1
90 //     store server's fingerprint NOW
91 //     d0_blind_id_sessionkey_public_id() = 1                    d0_blind_id_sessionkey_public_id() = 1
92 //
93 //     IF clientfp AND NOT serverfp:
94 //     RESET to clientfp                                         RESET to clientfp
95 //     d0_blind_id_authenticate_with_private_id_start() = 1
96 //     d0pk\cnt\0\challenge\<challenge>\aeslevel\<level> NUL NUL <clientfp> NUL *startdata*
97 //                                                             >
98 //                                                               check if client would get accepted; if not, do "reject" now
99 //     require non-control packets to be encrypted               require non-control packets to be encrypted
100 //                                                               d0_blind_id_authenticate_with_private_id_challenge() = 1
101 //                                                             < d0pk\cnt\5\aes\<aesenabled> NUL *challengedata*
102 //
103 //     IF clientfp AND serverfp:
104 //     RESET to clientfp                                         RESET to clientfp
105 //     d0_blind_id_authenticate_with_private_id_start() = 1
106 //     d0pk\cnt\4 NUL *startdata*                              >
107 //                                                               d0_blind_id_authenticate_with_private_id_challenge() = 1
108 //                                                             < d0pk\cnt\5 NUL *challengedata*
109 //
110 //     IF clientfp:
111 //     d0_blind_id_authenticate_with_private_id_response() = 0
112 //     d0pk\cnt\6 NUL *responsedata*                           >
113 //                                                               d0_blind_id_authenticate_with_private_id_verify() = 1
114 //                                                               store client's fingerprint NOW
115 //     d0_blind_id_sessionkey_public_id() = 1                    d0_blind_id_sessionkey_public_id() = 1
116 //     note: the ... is the "connect" message, except without the challenge. Reinterpret as regular connect message on server side
117 //
118 //     enforce encrypted transmission (key is XOR of the two DH keys)
119 //
120 //     IF clientfp:
121 //                                                             < challenge (mere sync message)
122 //
123 //     connect\...                                             >
124 //                                                             < accept (ALWAYS accept if connection is encrypted, ignore challenge as it had been checked before)
125 //
126 //     commence with ingame protocol
127
128 // in short:
129 //   server:
130 //     getchallenge NUL d0_blind_id: reply with challenge with added fingerprints
131 //     cnt=0: IF server will auth, cnt=1, ELSE cnt=5
132 //     cnt=2: cnt=3
133 //     cnt=4: cnt=5
134 //     cnt=6: send "challenge"
135 //   client:
136 //     challenge with added fingerprints: cnt=0; if client will auth but not server, append client auth start
137 //     cnt=1: cnt=2
138 //     cnt=3: IF client will auth, cnt=4, ELSE rewrite as "challenge"
139 //     cnt=5: cnt=6, server will continue by sending "challenge" (let's avoid sending two packets as response to one)
140 // other change:
141 //   accept empty "challenge", and challenge-less connect in case crypto protocol has executed and finished
142 //   statusResponse and infoResponse get an added d0_blind_id key that lists
143 //   the keys the server can auth with and to in key@ca SPACE key@ca notation
144 //   any d0pk\ message has an appended "id" parameter; messages with an unexpected "id" are ignored to prevent errors from multiple concurrent auth runs
145
146
147 // comparison to OTR:
148 // - encryption: yes
149 // - authentication: yes
150 // - deniability: no (attacker requires the temporary session key to prove you
151 //   have sent a specific message, the private key itself does not suffice), no
152 //   measures are taken to provide forgeability to even provide deniability
153 //   against an attacker who knows the temporary session key, as using CTR mode
154 //   for the encryption - which, together with deriving the MAC key from the
155 //   encryption key, and MACing the ciphertexts instead of the plaintexts,
156 //   would provide forgeability and thus deniability - requires longer
157 //   encrypted packets and deniability was not a goal of this, as we may e.g.
158 //   reserve the right to capture packet dumps + extra state info to prove a
159 //   client/server has sent specific packets to prove cheating)
160 // - perfect forward secrecy: yes (session key is derived via DH key exchange)
161
162 #endif