This commit is contained in:
irungentoo 2014-10-17 16:06:00 -04:00
commit 0fb3062f08
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
4 changed files with 137 additions and 0 deletions

View File

@ -25,6 +25,9 @@ unsigned char known_key[crypto_box_BEFORENMBYTES] = {0x29, 0x36, 0x1c, 0x9e, 0x6
char* pw = "hunter2"; char* pw = "hunter2";
unsigned int pwlen = 7; unsigned int pwlen = 7;
unsigned char known_key2[crypto_box_BEFORENMBYTES] = {0x7a, 0xfa, 0x95, 0x45, 0x36, 0x8a, 0xa2, 0x5c, 0x40, 0xfd, 0xc0, 0xe2, 0x35, 0x8, 0x7, 0x88, 0xfa, 0xf9, 0x37, 0x86, 0xeb, 0xff, 0x50, 0x4f, 0x3, 0xe2, 0xf6, 0xd9, 0xef, 0x9, 0x17, 0x1};
// same as above, except standard opslimit instead of extra ops limit for test_known_kdf, and hash pw before kdf for compat
/* cause I'm shameless */ /* cause I'm shameless */
void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
{ {
@ -61,11 +64,13 @@ START_TEST(test_save_friend)
tox_get_address(tox2, address); tox_get_address(tox2, address);
int test = tox_add_friend(tox1, address, (uint8_t *)"Gentoo", 7); int test = tox_add_friend(tox1, address, (uint8_t *)"Gentoo", 7);
ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); ck_assert_msg(test == 0, "Failed to add friend error code: %i", test);
uint32_t size = tox_encrypted_size(tox1); uint32_t size = tox_encrypted_size(tox1);
uint8_t data[size]; uint8_t data[size];
test = tox_encrypted_save(tox1, data, "correcthorsebatterystaple", 25); test = tox_encrypted_save(tox1, data, "correcthorsebatterystaple", 25);
ck_assert_msg(test == 0, "failed to encrypted save"); ck_assert_msg(test == 0, "failed to encrypted save");
ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing"); ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing");
Tox *tox3 = tox_new(0); Tox *tox3 = tox_new(0);
test = tox_encrypted_load(tox3, data, size, "correcthorsebatterystaple", 25); test = tox_encrypted_load(tox3, data, size, "correcthorsebatterystaple", 25);
ck_assert_msg(test == 0, "failed to encrypted load"); ck_assert_msg(test == 0, "failed to encrypted load");
@ -73,6 +78,73 @@ START_TEST(test_save_friend)
test = tox_get_client_id(tox3, 0, address2); test = tox_get_client_id(tox3, 0, address2);
ck_assert_msg(test == 0, "no friends!"); ck_assert_msg(test == 0, "no friends!");
ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match!"); ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match!");
size = tox_encrypted_size(tox3);
uint8_t data2[size];
uint8_t key[32 + crypto_box_BEFORENMBYTES];
memcpy(key, salt, 32); memcpy(key+32, known_key2, crypto_box_BEFORENMBYTES);
test = tox_encrypted_key_save(tox3, data2, key);
ck_assert_msg(test == 0, "failed to encrypted save the second");
ck_assert_msg(tox_is_save_encrypted(data2) == 1, "magic number the second missing");
// first test tox_encrypted_key_load
Tox* tox4 = tox_new(0);
test = tox_encrypted_key_load(tox4, data2, size, key);
ck_assert_msg(test == 0, "failed to encrypted load the second");
uint8_t address4[TOX_CLIENT_ID_SIZE];
test = tox_get_client_id(tox4, 0, address4);
ck_assert_msg(test == 0, "no friends! the second");
ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match! the second");
// now test compaitibilty with tox_encrypted_load, first manually...
uint8_t out1[size], out2[size];
printf("Trying to decrypt from pw:\n");
uint32_t sz1 = tox_pass_decrypt(data2+TOX_ENC_SAVE_MAGIC_LENGTH, size-TOX_ENC_SAVE_MAGIC_LENGTH, pw, pwlen, out1);
uint32_t sz2 = tox_pass_key_decrypt(data2+TOX_ENC_SAVE_MAGIC_LENGTH, size-TOX_ENC_SAVE_MAGIC_LENGTH, key, out2);
ck_assert_msg(sz1 == sz2, "differing output sizes");
ck_assert_msg(memcmp(out1, out2, sz1) == 0, "differing output data");
// and now with the code in use (I only bothered with manually to debug this, and it seems a waste
// to remove the manual check now that it's there)
Tox* tox5 = tox_new(0);
test = tox_encrypted_load(tox5, data2, size, pw, pwlen);
ck_assert_msg(test == 0, "failed to encrypted load the third");
uint8_t address5[TOX_CLIENT_ID_SIZE];
test = tox_get_client_id(tox4, 0, address5);
ck_assert_msg(test == 0, "no friends! the third");
ck_assert_msg(memcmp(address, address2, TOX_CLIENT_ID_SIZE) == 0, "addresses don't match! the third");
}
END_TEST
START_TEST(test_keys)
{
uint8_t key[tox_pass_key_length()];
tox_derive_key_from_pass("123qweasdzxc", 12, key);
uint8_t* string = "No Patrick, mayonnaise is not an instrument."; // 44
uint8_t encrypted[44+tox_pass_encryption_extra_length()];
int sz = tox_pass_key_encrypt(string, 44, key, encrypted);
uint8_t encrypted2[44+tox_pass_encryption_extra_length()];
int sz2 = tox_pass_encrypt(string, 44, "123qweasdzxc", 12, encrypted2);
ck_assert_msg(sz == sz2, "an encryption failed");
uint8_t out1[44+tox_pass_encryption_extra_length()];
uint8_t out2[44+tox_pass_encryption_extra_length()];
sz = tox_pass_key_decrypt(encrypted, 44+tox_pass_encryption_extra_length(), key, out1);
ck_assert_msg(sz == 44, "sz isn't right");
ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 1 failed");
sz2 = tox_pass_decrypt(encrypted2, 44+tox_pass_encryption_extra_length(), "123qweasdzxc", 12, out2);
ck_assert_msg(sz2 == 44, "sz2 isn't right");
ck_assert_msg(memcmp(out2, string, 44) == 0, "decryption 2 failed");
// test that pass_decrypt can decrypt things from pass_key_encrypt
sz = tox_pass_decrypt(encrypted, 44+tox_pass_encryption_extra_length(), "123qweasdzxc", 12, out1);
ck_assert_msg(sz == 44, "sz isn't right");
ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 3 failed");
} }
END_TEST END_TEST
@ -82,6 +154,7 @@ Suite * encryptsave_suite(void)
DEFTESTCASE_SLOW(known_kdf, 60); /* is 5-10 seconds on my computer, but is directly dependent on CPU */ DEFTESTCASE_SLOW(known_kdf, 60); /* is 5-10 seconds on my computer, but is directly dependent on CPU */
DEFTESTCASE(save_friend); DEFTESTCASE(save_friend);
DEFTESTCASE(keys);
return s; return s;
} }

View File

@ -18,6 +18,10 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(__MACH__)
#define MSG_NOSIGNAL 0
#endif
//IRC name and channel. //IRC name and channel.
#define IRC_NAME "Tox_syncbot" #define IRC_NAME "Tox_syncbot"
#define IRC_CHANNEL "#tox-real-ontopic" #define IRC_CHANNEL "#tox-real-ontopic"

View File

@ -181,6 +181,28 @@ int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint3
return tox_pass_encrypt(temp_data, temp_size, passphrase, pplength, data); return tox_pass_encrypt(temp_data, temp_size, passphrase, pplength, data);
} }
/* Save the messenger data encrypted with the given key from tox_derive_key.
* data must be at least tox_encrypted_size().
*
* returns 0 on success
* returns -1 on failure
*/
int tox_encrypted_key_save(const Tox *tox, uint8_t *data, uint8_t *key)
{
/* first get plain save data */
uint32_t temp_size = tox_size(tox);
uint8_t temp_data[temp_size];
tox_save(tox, temp_data);
/* the output data consists of, in order: magic number, enc_data */
/* first add the magic number */
memcpy(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH);
data += TOX_ENC_SAVE_MAGIC_LENGTH;
/* encrypt */
return tox_pass_key_encrypt(temp_data, temp_size, key, data);
}
/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by /* This is the inverse of tox_pass_key_encrypt, also using only keys produced by
* tox_derive_key_from_pass. * tox_derive_key_from_pass.
* *
@ -266,6 +288,29 @@ int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *
return tox_load(tox, temp_data, decrypt_length); return tox_load(tox, temp_data, decrypt_length);
} }
/* Load the messenger from encrypted data of size length, with key from tox_derive_key.
*
* returns 0 on success
* returns -1 on failure
*/
int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *key)
{
if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0)
return -1;
data += TOX_ENC_SAVE_MAGIC_LENGTH;
length -= TOX_ENC_SAVE_MAGIC_LENGTH;
uint32_t decrypt_length = length - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
uint8_t temp_data[decrypt_length];
if (tox_pass_key_decrypt(data, length, key, temp_data)
!= decrypt_length)
return -1;
return tox_load(tox, temp_data, decrypt_length);
}
/* Determines whether or not the given data is encrypted (by checking the magic number) /* Determines whether or not the given data is encrypted (by checking the magic number)
* *
* returns 1 if it is encrypted * returns 1 if it is encrypted

View File

@ -95,6 +95,14 @@ int tox_pass_encrypt(const uint8_t *data, uint32_t data_len, uint8_t *passphrase
*/ */
int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength); int tox_encrypted_save(const Tox *tox, uint8_t *data, uint8_t *passphrase, uint32_t pplength);
/* Save the messenger data encrypted with the given key from tox_derive_key.
* data must be at least tox_encrypted_size().
*
* returns 0 on success
* returns -1 on failure
*/
int tox_encrypted_key_save(const Tox *tox, uint8_t *data, uint8_t *key);
/* This is the inverse of tox_pass_key_encrypt, also using only keys produced by /* This is the inverse of tox_pass_key_encrypt, also using only keys produced by
* tox_derive_key_from_pass. * tox_derive_key_from_pass.
* *
@ -121,6 +129,13 @@ int tox_pass_decrypt(const uint8_t *data, uint32_t length, uint8_t *passphrase,
*/ */
int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength); int tox_encrypted_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *passphrase, uint32_t pplength);
/* Load the messenger from encrypted data of size length, with key from tox_derive_key.
*
* returns 0 on success
* returns -1 on failure
*/
int tox_encrypted_key_load(Tox *tox, const uint8_t *data, uint32_t length, uint8_t *key);
/* Determines whether or not the given data is encrypted (by checking the magic number) /* Determines whether or not the given data is encrypted (by checking the magic number)
* *
* returns 1 if it is encrypted * returns 1 if it is encrypted