Removed define that could become confusing.

This commit is contained in:
irungentoo 2013-10-24 13:34:04 -04:00
parent f3be560904
commit 8e0ab68d30
7 changed files with 53 additions and 44 deletions

View File

@ -95,8 +95,8 @@ START_TEST(test_known)
unsigned char m[131];
int clen, mlen;
ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char),
"cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
ck_assert_msg(sizeof(c) == sizeof(m) + crypto_box_MACBYTES * sizeof(unsigned char),
"cyphertext should be crypto_box_MACBYTES bytes longer than plaintext");
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
@ -121,8 +121,8 @@ START_TEST(test_fast_known)
encrypt_precompute(bobpk, alicesk, k);
ck_assert_msg(sizeof(c) == sizeof(m) + ENCRYPTION_PADDING * sizeof(unsigned char),
"cyphertext should be ENCRYPTION_PADDING bytes longer than plaintext");
ck_assert_msg(sizeof(c) == sizeof(m) + crypto_box_MACBYTES * sizeof(unsigned char),
"cyphertext should be crypto_box_MACBYTES bytes longer than plaintext");
ck_assert_msg(sizeof(test_c) == sizeof(c), "sanity check failed");
ck_assert_msg(sizeof(test_m) == sizeof(m), "sanity check failed");
@ -151,10 +151,10 @@ START_TEST(test_endtoend)
unsigned char n[crypto_box_NONCEBYTES];
unsigned char m[500];
unsigned char c1[sizeof(m) + ENCRYPTION_PADDING];
unsigned char c2[sizeof(m) + ENCRYPTION_PADDING];
unsigned char c3[sizeof(m) + ENCRYPTION_PADDING];
unsigned char c4[sizeof(m) + ENCRYPTION_PADDING];
unsigned char c1[sizeof(m) + crypto_box_MACBYTES];
unsigned char c2[sizeof(m) + crypto_box_MACBYTES];
unsigned char c3[sizeof(m) + crypto_box_MACBYTES];
unsigned char c4[sizeof(m) + crypto_box_MACBYTES];
unsigned char m1[sizeof(m)];
unsigned char m2[sizeof(m)];
unsigned char m3[sizeof(m)];
@ -190,7 +190,7 @@ START_TEST(test_endtoend)
c4len = encrypt_data_fast(k2, n, m, mlen, c4);
ck_assert_msg(c1len == c2len && c1len == c3len && c1len == c4len, "cyphertext lengths differ");
ck_assert_msg(c1len == mlen + (int)ENCRYPTION_PADDING, "wrong cyphertext length");
ck_assert_msg(c1len == mlen + (int)crypto_box_MACBYTES, "wrong cyphertext length");
ck_assert_msg(memcmp(c1, c2, c1len) == 0 && memcmp(c1, c3, c1len) == 0
&& memcmp(c1, c4, c1len) == 0, "crypertexts differ");
@ -215,12 +215,12 @@ START_TEST(test_large_data)
unsigned char n[crypto_box_NONCEBYTES];
unsigned char m1[MAX_DATA_SIZE - ENCRYPTION_PADDING];
unsigned char c1[sizeof(m1) + ENCRYPTION_PADDING];
unsigned char m1[MAX_DATA_SIZE - crypto_box_MACBYTES];
unsigned char c1[sizeof(m1) + crypto_box_MACBYTES];
unsigned char m1prime[sizeof(m1)];
unsigned char m2[MAX_DATA_SIZE];
unsigned char c2[sizeof(m2) + ENCRYPTION_PADDING];
unsigned char c2[sizeof(m2) + crypto_box_MACBYTES];
int c1len, c2len;
int m1plen;
@ -236,7 +236,7 @@ START_TEST(test_large_data)
c1len = encrypt_data_fast(k, n, m1, sizeof(m1), c1);
c2len = encrypt_data_fast(k, n, m2, sizeof(m2), c2);
ck_assert_msg(c1len == sizeof(m1) + ENCRYPTION_PADDING, "could not encrypt max size");
ck_assert_msg(c1len == sizeof(m1) + crypto_box_MACBYTES, "could not encrypt max size");
ck_assert_msg(c2len == -1, "incorrectly succeeded encrypting massive size");
m1plen = decrypt_data_fast(k, n, c1, c1len, m1prime);

View File

@ -61,7 +61,7 @@ int main(int argc, char *argv[])
unsigned char n[crypto_box_NONCEBYTES];
unsigned char m[500];
unsigned char c[sizeof(m) + ENCRYPTION_PADDING];
unsigned char c[sizeof(m) + crypto_box_MACBYTES];
unsigned char k[crypto_box_BEFORENMBYTES];

View File

@ -131,6 +131,7 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id
uint64_t temp_time = unix_time();
for (i = 0; i < length; i++)
/* Dead nodes are considered dead (not in the list)*/
if (!is_timeout(temp_time, list[i].assoc4.timestamp, KILL_NODE_TIMEOUT) ||
!is_timeout(temp_time, list[i].assoc6.timestamp, KILL_NODE_TIMEOUT))
@ -289,6 +290,7 @@ static void get_close_nodes_inner(DHT *dht, uint8_t *client_id, Node_format *nod
continue;
IPPTsPng *ipptp = NULL;
if (sa_family == AF_INET)
ipptp = &client->assoc4;
else
@ -640,9 +642,9 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli
if (ping_id == 0)
return -1;
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES];
uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE];
uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING];
uint8_t encrypt[sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES];
uint8_t nonce[crypto_box_NONCEBYTES];
new_nonce(nonce);
@ -656,7 +658,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli
sizeof(ping_id) + CLIENT_ID_SIZE,
encrypt );
if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING)
if (len != sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES)
return -1;
data[0] = NET_PACKET_GET_NODES;
@ -667,6 +669,8 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli
return sendpacket(dht->c->lossless_udp->net, ip_port, data, sizeof(data));
}
#define NODES_ENCRYPTED_MESSAGE_LENGTH (sizeof(Node_format) + crypto_secretbox_MACBYTES)
/* Send a send nodes response. */
/* because of BINARY compatibility, the Node_format MUST BE Node4_format,
* IPv6 nodes are sent in a different message */
@ -678,7 +682,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
size_t Node4_format_size = sizeof(Node4_format);
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
+ Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];
+ Node4_format_size * MAX_SENT_NODES + crypto_box_MACBYTES];
Node_format nodes_list[MAX_SENT_NODES];
int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET, LAN_ip(ip_port.ip) == 0);
@ -687,7 +691,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
return 0;
uint8_t plain[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES];
uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];
uint8_t encrypt[sizeof(ping_id) + Node4_format_size * MAX_SENT_NODES + crypto_box_MACBYTES];
uint8_t nonce[crypto_box_NONCEBYTES];
new_nonce(nonce);
@ -728,7 +732,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl
if (len == -1)
return -1;
if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING)
if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size + crypto_box_MACBYTES)
return -1;
data[0] = NET_PACKET_SEND_NODES;
@ -748,7 +752,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_
size_t Node_format_size = sizeof(Node_format);
uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id)
+ Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];
+ Node_format_size * MAX_SENT_NODES + crypto_box_MACBYTES];
Node_format nodes_list[MAX_SENT_NODES];
int num_nodes = get_close_nodes(dht, client_id, nodes_list, AF_INET6, LAN_ip(ip_port.ip) == 0);
@ -757,7 +761,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_
return 0;
uint8_t plain[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES];
uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + ENCRYPTION_PADDING];
uint8_t encrypt[sizeof(ping_id) + Node_format_size * MAX_SENT_NODES + crypto_box_MACBYTES];
uint8_t nonce[crypto_box_NONCEBYTES];
new_nonce(nonce);
@ -774,7 +778,7 @@ static int sendnodes_ipv6(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_
if (len == -1)
return -1;
if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING)
if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size + crypto_box_MACBYTES)
return -1;
data[0] = NET_PACKET_SEND_NODES_IPV6;
@ -791,7 +795,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32
uint64_t ping_id;
if (length != ( 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES
+ sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING ))
+ sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES ))
return 1;
/* Check if packet is from ourself. */
@ -804,7 +808,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32
dht->c->self_secret_key,
packet + 1 + CLIENT_ID_SIZE,
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING,
sizeof(ping_id) + CLIENT_ID_SIZE + crypto_box_MACBYTES,
plain );
if (len != sizeof(ping_id) + CLIENT_ID_SIZE)
@ -826,7 +830,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
DHT *dht = object;
uint64_t ping_id;
uint32_t cid_size = 1 + CLIENT_ID_SIZE;
cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING;
cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + crypto_box_MACBYTES;
size_t Node4_format_size = sizeof(Node4_format);
@ -843,7 +847,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
dht->c->self_secret_key,
packet + 1 + CLIENT_ID_SIZE,
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + num_nodes * Node4_format_size + ENCRYPTION_PADDING, plain );
sizeof(ping_id) + num_nodes * Node4_format_size + crypto_box_MACBYTES, plain );
if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node4_format_size)
return 1;
@ -888,7 +892,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet,
DHT *dht = object;
uint64_t ping_id;
uint32_t cid_size = 1 + CLIENT_ID_SIZE;
cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING;
cid_size += crypto_box_NONCEBYTES + sizeof(ping_id) + crypto_box_MACBYTES;
size_t Node_format_size = sizeof(Node_format);
@ -905,7 +909,7 @@ static int handle_sendnodes_ipv6(void *object, IP_Port source, uint8_t *packet,
dht->c->self_secret_key,
packet + 1 + CLIENT_ID_SIZE,
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + num_nodes * Node_format_size + ENCRYPTION_PADDING, plain );
sizeof(ping_id) + num_nodes * Node_format_size + crypto_box_MACBYTES, plain );
if ((unsigned int)len != sizeof(ping_id) + num_nodes * Node_format_size)
return 1;
@ -1193,14 +1197,16 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
connected = 0;
/* If ip is not zero and node is good. */
if (ip_isset(&client->assoc4.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) {
if (ip_isset(&client->assoc4.ret_ip_port.ip)
&& !is_timeout(temp_time, client->assoc4.ret_timestamp, BAD_NODE_TIMEOUT)) {
ipv4s[num_ipv4s] = client->assoc4.ret_ip_port;
++num_ipv4s;
connected = 1;
}
if (ip_isset(&client->assoc6.ret_ip_port.ip) && !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) {
if (ip_isset(&client->assoc6.ret_ip_port.ip)
&& !is_timeout(temp_time, client->assoc6.ret_timestamp, BAD_NODE_TIMEOUT)) {
ipv6s[num_ipv6s] = client->assoc6.ret_ip_port;
++num_ipv6s;
@ -1213,12 +1219,15 @@ static int friend_iplist(DHT *dht, IP_Port *ip_portlist, uint16_t friend_num)
#ifdef FRIEND_IPLIST_PAD
memcpy(ip_portlist, ipv6s, num_ipv6s * sizeof(IP_Port));
if (num_ipv6s == MAX_FRIEND_CLIENTS)
return MAX_FRIEND_CLIENTS;
int num_ipv4s_used = MAX_FRIEND_CLIENTS - num_ipv6s;
if (num_ipv4s_used > num_ipv4s)
num_ipv4s_used = num_ipv4s;
memcpy(&ip_portlist[num_ipv6s], ipv4s, num_ipv4s_used * sizeof(IP_Port));
return num_ipv6s + num_ipv4s_used;
@ -1825,6 +1834,7 @@ static int dht_load_state_callback(void *outer, uint8_t *data, uint32_t length,
break;
#ifdef DEBUG
default:
fprintf(stderr, "Load state (DHT): contains unrecognized part (len %u, type %u)\n",
length, type);

View File

@ -247,7 +247,7 @@ int write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uin
int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *packet, uint8_t *recv_public_key,
uint8_t *data, uint32_t length, uint8_t request_id)
{
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING)
if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES)
return -1;
uint8_t nonce[crypto_box_NONCEBYTES];
@ -278,7 +278,7 @@ int create_request(uint8_t *send_public_key, uint8_t *send_secret_key, uint8_t *
int handle_request(uint8_t *self_public_key, uint8_t *self_secret_key, uint8_t *public_key, uint8_t *data,
uint8_t *request_id, uint8_t *packet, uint16_t length)
{
if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING &&
if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES &&
length <= MAX_DATA_SIZE) {
if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {
memcpy(public_key, packet + 1 + crypto_box_PUBLICKEYBYTES, crypto_box_PUBLICKEYBYTES);
@ -313,8 +313,8 @@ static int cryptopacket_handle(void *object, IP_Port source, uint8_t *packet, ui
DHT *dht = object;
if (packet[0] == NET_PACKET_CRYPTO) {
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + crypto_box_MACBYTES ||
length > MAX_DATA_SIZE + crypto_box_MACBYTES)
return 1;
if (memcmp(packet + 1, dht->c->self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { // Check if request is for us.

View File

@ -77,8 +77,6 @@ typedef struct {
#include "DHT.h"
#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
/* return zero if the buffer contains only zeros. */
uint8_t crypto_iszero(uint8_t *buffer, uint32_t blen);

View File

@ -70,6 +70,7 @@ typedef int sock_t;
#include <crypto_box.h>
#include <randombytes.h>
#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
#define crypto_secretbox_MACBYTES (crypto_secretbox_ZEROBYTES - crypto_secretbox_BOXZEROBYTES)
#endif
#ifndef IPV6_ADD_MEMBERSHIP

View File

@ -132,7 +132,7 @@ static bool is_pinging(PING *ping, IP_Port ipp, uint64_t ping_id) // O(n) TOD
return false;
}
#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + ENCRYPTION_PADDING)
#define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + crypto_box_MACBYTES)
int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id)
{
@ -157,7 +157,7 @@ int send_ping_request(PING *ping, IP_Port ipp, uint8_t *client_id)
(uint8_t *) &ping_id, sizeof(ping_id),
pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES);
if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
if (rc != sizeof(ping_id) + crypto_box_MACBYTES)
return 1;
return sendpacket(ping->c->lossless_udp->net, ipp, pk, sizeof(pk));
@ -182,7 +182,7 @@ static int send_ping_response(PING *ping, IP_Port ipp, uint8_t *client_id, uint6
(uint8_t *) &ping_id, sizeof(ping_id),
pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES);
if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
if (rc != sizeof(ping_id) + crypto_box_MACBYTES)
return 1;
return sendpacket(ping->c->lossless_udp->net, ipp, pk, sizeof(pk));
@ -207,7 +207,7 @@ static int handle_ping_request(void *_dht, IP_Port source, uint8_t *packet, uint
ping->c->self_secret_key,
packet + 1 + CLIENT_ID_SIZE,
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + ENCRYPTION_PADDING,
sizeof(ping_id) + crypto_box_MACBYTES,
(uint8_t *) &ping_id);
if (rc != sizeof(ping_id))
@ -239,7 +239,7 @@ static int handle_ping_response(void *_dht, IP_Port source, uint8_t *packet, uin
ping->c->self_secret_key,
packet + 1 + CLIENT_ID_SIZE,
packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + ENCRYPTION_PADDING,
sizeof(ping_id) + crypto_box_MACBYTES,
(uint8_t *) &ping_id);
if (rc != sizeof(ping_id))