ffi-tox 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/ProjectTox-Core/AUTHORS +0 -0
  3. data/ProjectTox-Core/ChangeLog +0 -0
  4. data/ProjectTox-Core/INSTALL +370 -0
  5. data/ProjectTox-Core/INSTALL.md +455 -56
  6. data/ProjectTox-Core/Makefile.am +35 -0
  7. data/ProjectTox-Core/NEWS +0 -0
  8. data/ProjectTox-Core/README +43 -0
  9. data/ProjectTox-Core/README.md +34 -44
  10. data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
  11. data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
  12. data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
  13. data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
  14. data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
  15. data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
  16. data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
  17. data/ProjectTox-Core/auto_tests/helpers.h +15 -0
  18. data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
  19. data/ProjectTox-Core/auto_tests/network_test.c +171 -0
  20. data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
  21. data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
  22. data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
  23. data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
  24. data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
  25. data/ProjectTox-Core/autogen.sh +6 -0
  26. data/ProjectTox-Core/build/Makefile.am +14 -0
  27. data/ProjectTox-Core/configure.ac +694 -0
  28. data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
  29. data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
  30. data/ProjectTox-Core/dist-build/android-build.sh +59 -0
  31. data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
  32. data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
  33. data/ProjectTox-Core/docs/Group-Chats.md +71 -0
  34. data/ProjectTox-Core/docs/Hardening.txt +60 -0
  35. data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
  36. data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
  37. data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
  38. data/ProjectTox-Core/docs/TODO +62 -0
  39. data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
  40. data/ProjectTox-Core/docs/av_api.md +194 -0
  41. data/ProjectTox-Core/libtoxav.pc.in +11 -0
  42. data/ProjectTox-Core/libtoxcore.pc.in +11 -0
  43. data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
  44. data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
  45. data/ProjectTox-Core/m4/pkg.m4 +199 -0
  46. data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
  47. data/ProjectTox-Core/other/DHTnodes +3 -0
  48. data/ProjectTox-Core/other/Makefile.inc +20 -0
  49. data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
  50. data/ProjectTox-Core/other/tox.png +0 -0
  51. data/ProjectTox-Core/testing/DHT_test.c +170 -98
  52. data/ProjectTox-Core/testing/Makefile.inc +112 -0
  53. data/ProjectTox-Core/testing/Messenger_test.c +133 -69
  54. data/ProjectTox-Core/testing/dns3_test.c +115 -0
  55. data/ProjectTox-Core/testing/misc_tools.c +59 -13
  56. data/ProjectTox-Core/testing/nTox.c +1127 -264
  57. data/ProjectTox-Core/testing/nTox.h +10 -19
  58. data/ProjectTox-Core/testing/tox_shell.c +159 -0
  59. data/ProjectTox-Core/testing/tox_sync.c +299 -0
  60. data/ProjectTox-Core/tools/README +11 -0
  61. data/ProjectTox-Core/tools/astylerc +11 -0
  62. data/ProjectTox-Core/tools/pre-commit +17 -0
  63. data/ProjectTox-Core/toxav/Makefile.inc +36 -0
  64. data/ProjectTox-Core/toxav/codec.c +357 -0
  65. data/ProjectTox-Core/toxav/codec.h +116 -0
  66. data/ProjectTox-Core/toxav/msi.c +1949 -0
  67. data/ProjectTox-Core/toxav/msi.h +267 -0
  68. data/ProjectTox-Core/toxav/rtp.c +600 -0
  69. data/ProjectTox-Core/toxav/rtp.h +196 -0
  70. data/ProjectTox-Core/toxav/toxav.c +1148 -0
  71. data/ProjectTox-Core/toxav/toxav.h +389 -0
  72. data/ProjectTox-Core/toxcore/DHT.c +2521 -0
  73. data/ProjectTox-Core/toxcore/DHT.h +412 -0
  74. data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
  75. data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
  76. data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
  77. data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
  78. data/ProjectTox-Core/toxcore/Messenger.h +818 -0
  79. data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
  80. data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
  81. data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
  82. data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
  83. data/ProjectTox-Core/toxcore/assoc.c +1033 -0
  84. data/ProjectTox-Core/toxcore/assoc.h +104 -0
  85. data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
  86. data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
  87. data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
  88. data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
  89. data/ProjectTox-Core/toxcore/group_chats.c +837 -0
  90. data/ProjectTox-Core/toxcore/group_chats.h +199 -0
  91. data/ProjectTox-Core/toxcore/list.c +256 -0
  92. data/ProjectTox-Core/toxcore/list.h +85 -0
  93. data/ProjectTox-Core/toxcore/logger.c +153 -0
  94. data/ProjectTox-Core/toxcore/logger.h +84 -0
  95. data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
  96. data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
  97. data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
  98. data/ProjectTox-Core/toxcore/network.c +979 -0
  99. data/ProjectTox-Core/toxcore/network.h +367 -0
  100. data/ProjectTox-Core/toxcore/onion.c +540 -0
  101. data/ProjectTox-Core/toxcore/onion.h +150 -0
  102. data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
  103. data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
  104. data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
  105. data/ProjectTox-Core/toxcore/onion_client.h +253 -0
  106. data/ProjectTox-Core/toxcore/ping.c +346 -0
  107. data/ProjectTox-Core/toxcore/ping.h +47 -0
  108. data/ProjectTox-Core/toxcore/ping_array.c +162 -0
  109. data/ProjectTox-Core/toxcore/ping_array.h +75 -0
  110. data/ProjectTox-Core/toxcore/tox.c +940 -0
  111. data/ProjectTox-Core/toxcore/tox.h +734 -0
  112. data/ProjectTox-Core/toxcore/util.c +193 -0
  113. data/ProjectTox-Core/toxcore/util.h +63 -0
  114. data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
  115. data/ProjectTox-Core/toxdns/toxdns.c +238 -0
  116. data/ProjectTox-Core/toxdns/toxdns.h +88 -0
  117. data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
  118. data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
  119. data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
  120. data/interfaces/libtox.i +2 -6
  121. data/lib/ffi-tox/libtox.rb +406 -28
  122. metadata +124 -46
  123. data/ProjectTox-Core/CMakeLists.txt +0 -50
  124. data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
  125. data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
  126. data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
  127. data/ProjectTox-Core/core/CMakeLists.txt +0 -19
  128. data/ProjectTox-Core/core/DHT.c +0 -1104
  129. data/ProjectTox-Core/core/DHT.h +0 -111
  130. data/ProjectTox-Core/core/LAN_discovery.c +0 -79
  131. data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
  132. data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
  133. data/ProjectTox-Core/core/Messenger.c +0 -596
  134. data/ProjectTox-Core/core/Messenger.h +0 -165
  135. data/ProjectTox-Core/core/friend_requests.c +0 -131
  136. data/ProjectTox-Core/core/friend_requests.h +0 -51
  137. data/ProjectTox-Core/core/net_crypto.c +0 -575
  138. data/ProjectTox-Core/core/net_crypto.h +0 -134
  139. data/ProjectTox-Core/core/network.c +0 -205
  140. data/ProjectTox-Core/core/network.h +0 -134
  141. data/ProjectTox-Core/docs/commands.md +0 -25
  142. data/ProjectTox-Core/docs/start_guide.de.md +0 -40
  143. data/ProjectTox-Core/docs/start_guide.md +0 -38
  144. data/ProjectTox-Core/other/CMakeLists.txt +0 -9
  145. data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
  146. data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
  147. data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
  148. data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
  149. data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
  150. data/ProjectTox-Core/testing/misc_tools.h +0 -29
  151. data/ProjectTox-Core/testing/nTox_win32.c +0 -387
  152. data/ProjectTox-Core/testing/nTox_win32.h +0 -40
  153. data/ProjectTox-Core/testing/rect.py +0 -45
@@ -0,0 +1,104 @@
1
+
2
+ #ifndef __ASSOC_H__
3
+ #define __ASSOC_H__
4
+
5
+ /* used by rendezvous */
6
+ #define ASSOC_AVAILABLE
7
+
8
+ /* For the legalese parts, see tox.h. */
9
+
10
+ /* enumerated lists are superior to magic numbers */
11
+ enum NODE_STATUS { BAD, SEENB_HEARDG, SEENG, USED };
12
+
13
+ /*
14
+ * Module to store currently unused ID <=> IP associations
15
+ * for a potential future use
16
+ */
17
+
18
+ typedef struct Assoc Assoc;
19
+
20
+ /*****************************************************************************/
21
+
22
+ /* custom distance handler, if it's not ID-distance based
23
+ * return values exactly like id_closest() */
24
+ typedef int (*Assoc_distance_relative_callback)(const Assoc *assoc, void *callback_data, const uint8_t *client_id,
25
+ const uint8_t *client_id1, const uint8_t *client_id2);
26
+
27
+ #define DISTANCE_INDEX_DISTANCE_BITS 44
28
+
29
+ /* absolute distance: can be same for different client_id_check values
30
+ * return value should have DISTANCE_INDEX_DISTANCE_BITS valid bits */
31
+ typedef uint64_t (*Assoc_distance_absolute_callback)(const Assoc *assoc, void *callback_data,
32
+ const uint8_t *client_id_ref, const uint8_t *client_id_check);
33
+
34
+ /*****************************************************************************/
35
+
36
+ /* Central entry point for new associations: add a new candidate to the cache
37
+ * returns 1 if entry is stored, 2 if existing entry was updated, 0 else */
38
+ uint8_t Assoc_add_entry(Assoc *assoc, const uint8_t *id, const IPPTs *ippts_send, const IP_Port *ipp_recv,
39
+ uint8_t used);
40
+
41
+ /*****************************************************************************/
42
+
43
+ typedef enum AssocCloseEntriesFlags {
44
+ ProtoIPv4 = 1,
45
+ ProtoIPv6 = 2,
46
+ LANOk = 4,
47
+ } AssocCloseEntriesFlags;
48
+
49
+ typedef struct Assoc_close_entries {
50
+ void *custom_data; /* given to distance functions */
51
+ uint8_t *wanted_id; /* the target client_id */
52
+ uint8_t flags; /* additional flags */
53
+
54
+ Assoc_distance_relative_callback distance_relative_func;
55
+ Assoc_distance_absolute_callback distance_absolute_func;
56
+
57
+ uint8_t count_good; /* that many should be "good" w.r.t. timeout */
58
+ uint8_t count; /* allocated number of close_indices */
59
+ Client_data **result;
60
+ } Assoc_close_entries;
61
+
62
+ /* find up to close_count nodes to put into close_nodes_used of ID_Nodes
63
+ * the distance functions can be NULL, then standard distance functions will be used
64
+ * the caller is responsible for allocating close_indices of sufficient size
65
+ *
66
+ * returns 0 on error
67
+ * returns the number of found nodes and the list of indices usable by Assoc_client()
68
+ * the caller is assumed to be registered from Assoc_register_callback()
69
+ * if they aren't, they should copy the Client_data and call Assoc_client_drop()
70
+ */
71
+ uint8_t Assoc_get_close_entries(Assoc *assoc, Assoc_close_entries *close_entries);
72
+
73
+ /*****************************************************************************/
74
+
75
+ /* create: default sizes (6, 5 => 320 entries) */
76
+ Assoc *new_Assoc_default(const uint8_t *public_id);
77
+
78
+ /* create: customized sizes
79
+ * total is (2^bits) * entries
80
+ * bits should be between 2 and 15 (else it's trimmed)
81
+ * entries will be reduced to the closest prime smaller or equal
82
+ *
83
+ * preferably bits should be large and entries small to ensure spread
84
+ * in the search space (e. g. 5, 5 is preferable to 2, 41) */
85
+ Assoc *new_Assoc(size_t bits, size_t entries, const uint8_t *public_id);
86
+
87
+ /* public_id changed (loaded), update which entry isn't stored */
88
+ void Assoc_self_client_id_changed(Assoc *assoc, const uint8_t *public_id);
89
+
90
+ /* every 45s send out a getnodes() for a "random" bucket */
91
+ #define ASSOC_BUCKET_REFRESH 45
92
+
93
+ /* refresh bucket's data from time to time
94
+ * this must be called only from DHT */
95
+ void do_Assoc(Assoc *assoc, DHT *dht);
96
+
97
+ /* destroy */
98
+ void kill_Assoc(Assoc *assoc);
99
+
100
+ #ifdef LOGGING
101
+ void Assoc_status(const Assoc *assoc);
102
+ #endif /* LOGGING */
103
+
104
+ #endif /* !__ASSOC_H__ */
@@ -0,0 +1,278 @@
1
+ /* net_crypto.c
2
+ *
3
+ * Functions for the core crypto.
4
+ *
5
+ * NOTE: This code has to be perfect. We don't mess around with encryption.
6
+ *
7
+ * Copyright (C) 2013 Tox project All Rights Reserved.
8
+ *
9
+ * This file is part of Tox.
10
+ *
11
+ * Tox is free software: you can redistribute it and/or modify
12
+ * it under the terms of the GNU General Public License as published by
13
+ * the Free Software Foundation, either version 3 of the License, or
14
+ * (at your option) any later version.
15
+ *
16
+ * Tox is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ * GNU General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU General Public License
22
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
23
+ *
24
+ */
25
+
26
+ #ifdef HAVE_CONFIG_H
27
+ #include "config.h"
28
+ #endif
29
+
30
+ #include "crypto_core.h"
31
+
32
+
33
+ /* Use this instead of memcmp; not vulnerable to timing attacks.
34
+ returns 0 if both mem locations of length are equal,
35
+ return -1 if they are not. */
36
+ int crypto_cmp(const uint8_t *mem1, const uint8_t *mem2, uint32_t length)
37
+ {
38
+ if (length == 16) {
39
+ return crypto_verify_16(mem1, mem2);
40
+ } else if (length == 32) {
41
+ return crypto_verify_32(mem1, mem2);
42
+ }
43
+
44
+ unsigned int i, check = 0;
45
+
46
+ for (i = 0; i < length; ++i) {
47
+ check |= mem1[i] ^ mem2[i];
48
+ }
49
+
50
+ return (1 & ((check - 1) >> 8)) - 1;
51
+ }
52
+
53
+ /* return a random number.
54
+ */
55
+ uint32_t random_int(void)
56
+ {
57
+ uint32_t randnum;
58
+ randombytes((uint8_t *)&randnum , sizeof(randnum));
59
+ return randnum;
60
+ }
61
+
62
+ uint64_t random_64b(void)
63
+ {
64
+ uint64_t randnum;
65
+ randombytes((uint8_t *)&randnum, sizeof(randnum));
66
+ return randnum;
67
+ }
68
+
69
+ /* Check if a Tox public key crypto_box_PUBLICKEYBYTES is valid or not.
70
+ * This should only be used for input validation.
71
+ *
72
+ * return 0 if it isn't.
73
+ * return 1 if it is.
74
+ */
75
+ int public_key_valid(const uint8_t *public_key)
76
+ {
77
+ if (public_key[31] >= 128) /* Last bit of key is always zero. */
78
+ return 0;
79
+
80
+ return 1;
81
+ }
82
+
83
+ /* Precomputes the shared key from their public_key and our secret_key.
84
+ * This way we can avoid an expensive elliptic curve scalar multiply for each
85
+ * encrypt/decrypt operation.
86
+ * enc_key has to be crypto_box_BEFORENMBYTES bytes long.
87
+ */
88
+ void encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, uint8_t *enc_key)
89
+ {
90
+ crypto_box_beforenm(enc_key, public_key, secret_key);
91
+ }
92
+
93
+ int encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *plain, uint32_t length,
94
+ uint8_t *encrypted)
95
+ {
96
+ if (length == 0)
97
+ return -1;
98
+
99
+ uint8_t temp_plain[length + crypto_box_ZEROBYTES];
100
+ uint8_t temp_encrypted[length + crypto_box_MACBYTES + crypto_box_BOXZEROBYTES];
101
+
102
+ memset(temp_plain, 0, crypto_box_ZEROBYTES);
103
+ memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); // Pad the message with 32 0 bytes.
104
+
105
+ if (crypto_box_afternm(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, secret_key) != 0)
106
+ return -1;
107
+
108
+ /* Unpad the encrypted message. */
109
+ memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES);
110
+ return length + crypto_box_MACBYTES;
111
+ }
112
+
113
+ int decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *encrypted, uint32_t length,
114
+ uint8_t *plain)
115
+ {
116
+ if (length <= crypto_box_BOXZEROBYTES)
117
+ return -1;
118
+
119
+ uint8_t temp_plain[length + crypto_box_ZEROBYTES];
120
+ uint8_t temp_encrypted[length + crypto_box_BOXZEROBYTES];
121
+
122
+ memset(temp_encrypted, 0, crypto_box_BOXZEROBYTES);
123
+ memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); // Pad the message with 16 0 bytes.
124
+
125
+ if (crypto_box_open_afternm(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, nonce, secret_key) != 0)
126
+ return -1;
127
+
128
+ memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES);
129
+ return length - crypto_box_MACBYTES;
130
+ }
131
+
132
+ int encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
133
+ const uint8_t *plain, uint32_t length, uint8_t *encrypted)
134
+ {
135
+ uint8_t k[crypto_box_BEFORENMBYTES];
136
+ encrypt_precompute(public_key, secret_key, k);
137
+ return encrypt_data_symmetric(k, nonce, plain, length, encrypted);
138
+ }
139
+
140
+ int decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
141
+ const uint8_t *encrypted, uint32_t length, uint8_t *plain)
142
+ {
143
+ uint8_t k[crypto_box_BEFORENMBYTES];
144
+ encrypt_precompute(public_key, secret_key, k);
145
+ return decrypt_data_symmetric(k, nonce, encrypted, length, plain);
146
+ }
147
+
148
+
149
+ /* Increment the given nonce by 1. */
150
+ void increment_nonce(uint8_t *nonce)
151
+ {
152
+ uint32_t i;
153
+
154
+ for (i = crypto_box_NONCEBYTES; i != 0; --i) {
155
+ ++nonce[i - 1];
156
+
157
+ if (nonce[i - 1] != 0)
158
+ break;
159
+ }
160
+ }
161
+ /* increment the given nonce by num */
162
+ void increment_nonce_number(uint8_t *nonce, uint32_t num)
163
+ {
164
+ uint32_t num1, num2;
165
+ memcpy(&num1, nonce + (crypto_box_NONCEBYTES - sizeof(num1)), sizeof(num1));
166
+ num1 = ntohl(num1);
167
+ num2 = num + num1;
168
+
169
+ if (num2 < num1) {
170
+ uint32_t i;
171
+
172
+ for (i = crypto_box_NONCEBYTES - sizeof(num1); i != 0; --i) {
173
+ ++nonce[i - 1];
174
+
175
+ if (nonce[i - 1] != 0)
176
+ break;
177
+ }
178
+ }
179
+
180
+ num2 = htonl(num2);
181
+ memcpy(nonce + (crypto_box_NONCEBYTES - sizeof(num2)), &num2, sizeof(num2));
182
+ }
183
+
184
+ /* Fill the given nonce with random bytes. */
185
+ void random_nonce(uint8_t *nonce)
186
+ {
187
+ randombytes(nonce, crypto_box_NONCEBYTES);
188
+ }
189
+
190
+ /* Fill a key crypto_box_KEYBYTES big with random bytes */
191
+ void new_symmetric_key(uint8_t *key)
192
+ {
193
+ randombytes(key, crypto_box_KEYBYTES);
194
+ }
195
+
196
+ static uint8_t base_nonce[crypto_box_NONCEBYTES];
197
+ static uint8_t nonce_set = 0;
198
+
199
+ /* Gives a nonce guaranteed to be different from previous ones.*/
200
+ void new_nonce(uint8_t *nonce)
201
+ {
202
+ if (nonce_set == 0) {
203
+ random_nonce(base_nonce);
204
+ nonce_set = 1;
205
+ }
206
+
207
+ increment_nonce(base_nonce);
208
+ memcpy(nonce, base_nonce, crypto_box_NONCEBYTES);
209
+ }
210
+
211
+ /* Create a request to peer.
212
+ * send_public_key and send_secret_key are the pub/secret keys of the sender.
213
+ * recv_public_key is public key of receiver.
214
+ * packet must be an array of MAX_CRYPTO_REQUEST_SIZE big.
215
+ * Data represents the data we send with the request with length being the length of the data.
216
+ * request_id is the id of the request (32 = friend request, 254 = ping request).
217
+ *
218
+ * return -1 on failure.
219
+ * return the length of the created packet on success.
220
+ */
221
+ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_key, uint8_t *packet,
222
+ const uint8_t *recv_public_key, const uint8_t *data, uint32_t length, uint8_t request_id)
223
+ {
224
+ if (MAX_CRYPTO_REQUEST_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 +
225
+ crypto_box_MACBYTES)
226
+ return -1;
227
+
228
+ uint8_t nonce[crypto_box_NONCEBYTES];
229
+ uint8_t temp[MAX_CRYPTO_REQUEST_SIZE];
230
+ memcpy(temp + 1, data, length);
231
+ temp[0] = request_id;
232
+ new_nonce(nonce);
233
+ int len = encrypt_data(recv_public_key, send_secret_key, nonce, temp, length + 1,
234
+ 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet);
235
+
236
+ if (len == -1)
237
+ return -1;
238
+
239
+ packet[0] = NET_PACKET_CRYPTO;
240
+ memcpy(packet + 1, recv_public_key, crypto_box_PUBLICKEYBYTES);
241
+ memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, send_public_key, crypto_box_PUBLICKEYBYTES);
242
+ memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES * 2, nonce, crypto_box_NONCEBYTES);
243
+
244
+ return len + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES;
245
+ }
246
+
247
+ /* Puts the senders public key in the request in public_key, the data from the request
248
+ * in data if a friend or ping request was sent to us and returns the length of the data.
249
+ * packet is the request packet and length is its length.
250
+ *
251
+ * return -1 if not valid request.
252
+ */
253
+ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
254
+ uint8_t *request_id, const uint8_t *packet, uint16_t length)
255
+ {
256
+ if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES &&
257
+ length <= MAX_CRYPTO_REQUEST_SIZE) {
258
+ if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
259
+ memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
260
+ uint8_t nonce[crypto_box_NONCEBYTES];
261
+ uint8_t temp[MAX_CRYPTO_REQUEST_SIZE];
262
+ memcpy(nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2, crypto_box_NONCEBYTES);
263
+ int len1 = decrypt_data(public_key, self_secret_key, nonce,
264
+ packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES,
265
+ length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), temp);
266
+
267
+ if (len1 == -1 || len1 == 0)
268
+ return -1;
269
+
270
+ request_id[0] = temp[0];
271
+ --len1;
272
+ memcpy(data, temp + 1, len1);
273
+ return len1;
274
+ }
275
+ }
276
+
277
+ return -1;
278
+ }
@@ -0,0 +1,151 @@
1
+ /* crypto_core.h
2
+ *
3
+ * Functions for the core crypto.
4
+ *
5
+ * Copyright (C) 2013 Tox project All Rights Reserved.
6
+ *
7
+ * This file is part of Tox.
8
+ *
9
+ * Tox is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * Tox is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+ #ifndef CORE_CRYPTO_H
24
+ #define CORE_CRYPTO_H
25
+
26
+ #include "network.h"
27
+
28
+ #ifndef VANILLA_NACL
29
+ /* We use libsodium by default. */
30
+ #include <sodium.h>
31
+ #else
32
+ #include <crypto_box.h>
33
+ #include <randombytes.h>
34
+ #include <crypto_hash_sha256.h>
35
+ #include <crypto_hash_sha512.h>
36
+ #include <crypto_verify_16.h>
37
+ #include <crypto_verify_32.h>
38
+ #define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
39
+ #endif
40
+
41
+ #define crypto_box_KEYBYTES (crypto_box_BEFORENMBYTES)
42
+
43
+ /* Use this instead of memcmp; not vulnerable to timing attacks.
44
+ returns 0 if both mem locations of length are equal,
45
+ return -1 if they are not. */
46
+ int crypto_cmp(const uint8_t *mem1, const uint8_t *mem2, uint32_t length);
47
+
48
+ /* return a random number.
49
+ *
50
+ * random_int for a 32bin int.
51
+ * random_64b for a 64bit int.
52
+ */
53
+ uint32_t random_int(void);
54
+ uint64_t random_64b(void);
55
+
56
+ /* Check if a Tox public key crypto_box_PUBLICKEYBYTES is valid or not.
57
+ * This should only be used for input validation.
58
+ *
59
+ * return 0 if it isn't.
60
+ * return 1 if it is.
61
+ */
62
+ int public_key_valid(const uint8_t *public_key);
63
+
64
+ /* Encrypts plain of length length to encrypted of length + 16 using the
65
+ * public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce.
66
+ *
67
+ * return -1 if there was a problem.
68
+ * return length of encrypted data if everything was fine.
69
+ */
70
+ int encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
71
+ const uint8_t *plain, uint32_t length, uint8_t *encrypted);
72
+
73
+
74
+ /* Decrypts encrypted of length length to plain of length length - 16 using the
75
+ * public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce.
76
+ *
77
+ * return -1 if there was a problem (decryption failed).
78
+ * return length of plain data if everything was fine.
79
+ */
80
+ int decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
81
+ const uint8_t *encrypted, uint32_t length, uint8_t *plain);
82
+
83
+ /* Fast encrypt/decrypt operations. Use if this is not a one-time communication.
84
+ encrypt_precompute does the shared-key generation once so it does not have
85
+ to be preformed on every encrypt/decrypt. */
86
+ void encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key, uint8_t *enc_key);
87
+
88
+ /* Encrypts plain of length length to encrypted of length + 16 using a
89
+ * secret key crypto_box_KEYBYTES big and a 24 byte nonce.
90
+ *
91
+ * return -1 if there was a problem.
92
+ * return length of encrypted data if everything was fine.
93
+ */
94
+ int encrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *plain, uint32_t length,
95
+ uint8_t *encrypted);
96
+
97
+ /* Decrypts encrypted of length length to plain of length length - 16 using a
98
+ * secret key crypto_box_KEYBYTES big and a 24 byte nonce.
99
+ *
100
+ * return -1 if there was a problem (decryption failed).
101
+ * return length of plain data if everything was fine.
102
+ */
103
+ int decrypt_data_symmetric(const uint8_t *secret_key, const uint8_t *nonce, const uint8_t *encrypted, uint32_t length,
104
+ uint8_t *plain);
105
+
106
+ /* Increment the given nonce by 1. */
107
+ void increment_nonce(uint8_t *nonce);
108
+
109
+ /* increment the given nonce by num */
110
+ void increment_nonce_number(uint8_t *nonce, uint32_t num);
111
+
112
+ /* Fill the given nonce with random bytes. */
113
+ void random_nonce(uint8_t *nonce);
114
+
115
+ /* Fill a key crypto_box_KEYBYTES big with random bytes */
116
+ void new_symmetric_key(uint8_t *key);
117
+
118
+ /*Gives a nonce guaranteed to be different from previous ones.*/
119
+ void new_nonce(uint8_t *nonce);
120
+
121
+ #define MAX_CRYPTO_REQUEST_SIZE 1024
122
+
123
+ #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */
124
+ #define CRYPTO_PACKET_HARDENING 48 /* Hardening crypto packet ID. */
125
+ #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */
126
+ #define CRYPTO_PACKET_GROUP_CHAT_GET_NODES 48 /* Group chat get Nodes packet */
127
+ #define CRYPTO_PACKET_GROUP_CHAT_SEND_NODES 49 /* Group chat send Nodes packet */
128
+ #define CRYPTO_PACKET_GROUP_CHAT_BROADCAST 50 /* Group chat broadcast packet */
129
+
130
+ /* Create a request to peer.
131
+ * send_public_key and send_secret_key are the pub/secret keys of the sender.
132
+ * recv_public_key is public key of receiver.
133
+ * packet must be an array of MAX_CRYPTO_REQUEST_SIZE big.
134
+ * Data represents the data we send with the request with length being the length of the data.
135
+ * request_id is the id of the request (32 = friend request, 254 = ping request).
136
+ *
137
+ * return -1 on failure.
138
+ * return the length of the created packet on success.
139
+ */
140
+ int create_request(const uint8_t *send_public_key, const uint8_t *send_secret_key, uint8_t *packet,
141
+ const uint8_t *recv_public_key, const uint8_t *data, uint32_t length, uint8_t request_id);
142
+
143
+ /* puts the senders public key in the request in public_key, the data from the request
144
+ in data if a friend or ping request was sent to us and returns the length of the data.
145
+ packet is the request packet and length is its length
146
+ return -1 if not valid request. */
147
+ int handle_request(const uint8_t *self_public_key, const uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
148
+ uint8_t *request_id, const uint8_t *packet, uint16_t length);
149
+
150
+
151
+ #endif