2014-09-12 10:28:50 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-06-05 04:58:28 +08:00
|
|
|
#include "check_compat.h"
|
|
|
|
|
2016-09-01 07:33:20 +08:00
|
|
|
#include <stdint.h>
|
2014-09-12 10:28:50 +08:00
|
|
|
#include <stdlib.h>
|
2016-09-01 07:33:20 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
2014-09-12 10:28:50 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "helpers.h"
|
|
|
|
|
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
|
2017-01-21 05:16:55 +08:00
|
|
|
#include "../toxcore/ccompat.h"
|
2014-09-12 10:28:50 +08:00
|
|
|
#include "../toxcore/crypto_core.h"
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "../toxencryptsave/toxencryptsave.h"
|
2014-09-12 10:28:50 +08:00
|
|
|
#ifdef VANILLA_NACL
|
|
|
|
#include "../toxencryptsave/crypto_pwhash_scryptsalsa208sha256/crypto_pwhash_scryptsalsa208sha256.h"
|
2016-12-19 10:47:42 +08:00
|
|
|
#else
|
|
|
|
#include <sodium.h>
|
2014-09-12 10:28:50 +08:00
|
|
|
#endif
|
|
|
|
|
2016-12-10 19:21:22 +08:00
|
|
|
static unsigned char test_salt[TOX_PASS_SALT_LENGTH] = {0xB1, 0xC2, 0x09, 0xEE, 0x50, 0x6C, 0xF0, 0x20, 0xC4, 0xD6, 0xEB, 0xC0, 0x44, 0x51, 0x3B, 0x60, 0x4B, 0x39, 0x4A, 0xCF, 0x09, 0x53, 0x4F, 0xEA, 0x08, 0x41, 0xFA, 0xCA, 0x66, 0xD2, 0x68, 0x7F};
|
|
|
|
static unsigned char known_key[TOX_PASS_KEY_LENGTH] = {0x29, 0x36, 0x1c, 0x9e, 0x65, 0xbb, 0x46, 0x8b, 0xde, 0xa1, 0xac, 0xf, 0xd5, 0x11, 0x81, 0xc8, 0x29, 0x28, 0x17, 0x23, 0xa6, 0xc3, 0x6b, 0x77, 0x2e, 0xd7, 0xd3, 0x10, 0xeb, 0xd2, 0xf7, 0xc8};
|
2016-09-05 23:10:48 +08:00
|
|
|
static const char *pw = "hunter2";
|
|
|
|
static unsigned int pwlen = 7;
|
2014-09-12 10:28:50 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
static unsigned char known_key2[CRYPTO_SHARED_KEY_SIZE] = {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};
|
2014-10-17 19:02:15 +08:00
|
|
|
// same as above, except standard opslimit instead of extra ops limit for test_known_kdf, and hash pw before kdf for compat
|
|
|
|
|
2014-09-12 10:28:50 +08:00
|
|
|
/* cause I'm shameless */
|
2016-09-05 23:10:48 +08:00
|
|
|
static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
|
2014-09-12 10:28:50 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (*((uint32_t *)userdata) != 974536) {
|
2014-09-12 10:28:50 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-09-12 10:28:50 +08:00
|
|
|
|
|
|
|
if (length == 7 && memcmp("Gentoo", data, 7) == 0) {
|
2018-01-29 05:30:39 +08:00
|
|
|
tox_friend_add_norequest(m, public_key, nullptr);
|
2014-09-12 10:28:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
START_TEST(test_known_kdf)
|
|
|
|
{
|
2016-12-19 10:47:42 +08:00
|
|
|
unsigned char out[CRYPTO_SHARED_KEY_SIZE];
|
2016-08-10 20:52:40 +08:00
|
|
|
int res = crypto_pwhash_scryptsalsa208sha256(out,
|
2016-12-19 10:47:42 +08:00
|
|
|
CRYPTO_SHARED_KEY_SIZE,
|
2016-08-10 20:52:40 +08:00
|
|
|
pw,
|
|
|
|
pwlen,
|
2016-09-30 22:15:50 +08:00
|
|
|
test_salt,
|
2016-08-10 20:52:40 +08:00
|
|
|
crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 8,
|
|
|
|
crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE);
|
|
|
|
ck_assert_msg(res != -1, "crypto function failed");
|
2016-12-19 10:47:42 +08:00
|
|
|
ck_assert_msg(memcmp(out, known_key, CRYPTO_SHARED_KEY_SIZE) == 0, "derived key is wrong");
|
2014-09-12 10:28:50 +08:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
|
|
|
START_TEST(test_save_friend)
|
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
Tox *tox1 = tox_new_log(nullptr, nullptr, nullptr);
|
|
|
|
Tox *tox2 = tox_new_log(nullptr, nullptr, nullptr);
|
2014-09-12 10:28:50 +08:00
|
|
|
ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
|
2016-08-23 05:44:58 +08:00
|
|
|
tox_callback_friend_request(tox2, accept_friend_request);
|
2015-03-02 09:31:55 +08:00
|
|
|
uint8_t address[TOX_ADDRESS_SIZE];
|
|
|
|
tox_self_get_address(tox2, address);
|
2018-01-29 05:30:39 +08:00
|
|
|
uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)"Gentoo", 7, nullptr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(test != UINT32_MAX, "Failed to add friend");
|
2014-10-17 19:02:15 +08:00
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
size_t size = tox_get_savedata_size(tox1);
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, data, size);
|
2015-04-01 07:15:59 +08:00
|
|
|
tox_get_savedata(tox1, data);
|
2015-04-01 07:44:51 +08:00
|
|
|
size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, enc_data, size2);
|
2015-04-01 07:15:59 +08:00
|
|
|
TOX_ERR_ENCRYPTION error1;
|
2016-08-18 07:37:45 +08:00
|
|
|
bool ret = tox_pass_encrypt(data, size, (const uint8_t *)"correcthorsebatterystaple", 25, enc_data, &error1);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "failed to encrypted save: %u", error1);
|
|
|
|
ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing");
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
struct Tox_Options *options = tox_options_new(nullptr);
|
2016-12-16 01:35:54 +08:00
|
|
|
tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE);
|
|
|
|
tox_options_set_savedata_data(options, enc_data, size2);
|
2015-05-23 06:23:56 +08:00
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
TOX_ERR_NEW err2;
|
2018-01-29 05:30:39 +08:00
|
|
|
Tox *tox3 = tox_new_log(options, &err2, nullptr);
|
2015-04-01 07:44:51 +08:00
|
|
|
ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2,
|
|
|
|
TOX_ERR_NEW_LOAD_ENCRYPTED);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(tox3 == nullptr, "tox_new with error should return NULL");
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, dec_data, size);
|
2015-04-01 07:15:59 +08:00
|
|
|
TOX_ERR_DECRYPTION err3;
|
2016-08-18 07:37:45 +08:00
|
|
|
ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "failed to decrypt save: %u", err3);
|
2016-12-16 01:35:54 +08:00
|
|
|
tox_options_set_savedata_data(options, dec_data, size);
|
2018-01-29 05:30:39 +08:00
|
|
|
tox3 = tox_new_log(options, &err2, nullptr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2);
|
2015-03-02 09:31:55 +08:00
|
|
|
uint8_t address2[TOX_PUBLIC_KEY_SIZE];
|
2018-01-29 05:30:39 +08:00
|
|
|
ret = tox_friend_get_public_key(tox3, 0, address2, nullptr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "no friends!");
|
2015-03-02 09:31:55 +08:00
|
|
|
ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!");
|
2014-10-17 19:02:15 +08:00
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
size = tox_get_savedata_size(tox3);
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, data2, size);
|
2015-04-01 07:15:59 +08:00
|
|
|
tox_get_savedata(tox3, data2);
|
2016-12-16 11:00:55 +08:00
|
|
|
TOX_ERR_KEY_DERIVATION keyerr;
|
|
|
|
Tox_Pass_Key *key = tox_pass_key_derive((const uint8_t *)"123qweasdzxc", 12, &keyerr);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(key != nullptr, "pass key allocation failure");
|
2016-12-10 19:21:22 +08:00
|
|
|
memcpy((uint8_t *)key, test_salt, TOX_PASS_SALT_LENGTH);
|
|
|
|
memcpy((uint8_t *)key + TOX_PASS_SALT_LENGTH, known_key2, TOX_PASS_KEY_LENGTH);
|
2015-04-01 07:15:59 +08:00
|
|
|
size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, encdata2, size2);
|
2016-12-10 19:21:22 +08:00
|
|
|
ret = tox_pass_key_encrypt(key, data2, size, encdata2, &error1);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "failed to key encrypt %u", error1);
|
|
|
|
ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing");
|
|
|
|
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, out1, size);
|
|
|
|
VLA(uint8_t, out2, size);
|
2016-08-18 07:37:45 +08:00
|
|
|
ret = tox_pass_decrypt(encdata2, size2, (const uint8_t *)pw, pwlen, out1, &err3);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "failed to pw decrypt %u", err3);
|
2016-12-10 19:21:22 +08:00
|
|
|
ret = tox_pass_key_decrypt(key, encdata2, size2, out2, &err3);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "failed to key decrypt %u", err3);
|
|
|
|
ck_assert_msg(memcmp(out1, out2, size) == 0, "differing output data");
|
2014-10-17 19:02:15 +08:00
|
|
|
|
|
|
|
// 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)
|
2016-12-16 01:35:54 +08:00
|
|
|
tox_options_set_savedata_data(options, out1, size);
|
2018-01-29 05:30:39 +08:00
|
|
|
Tox *tox4 = tox_new_log(options, &err2, nullptr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third");
|
2015-03-02 09:31:55 +08:00
|
|
|
uint8_t address5[TOX_PUBLIC_KEY_SIZE];
|
2018-01-29 05:30:39 +08:00
|
|
|
ret = tox_friend_get_public_key(tox4, 0, address5, nullptr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "no friends! the third");
|
2015-03-02 09:31:55 +08:00
|
|
|
ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third");
|
2015-02-09 06:34:49 +08:00
|
|
|
|
2016-12-10 19:21:22 +08:00
|
|
|
tox_pass_key_free(key);
|
2016-12-16 01:35:54 +08:00
|
|
|
tox_options_free(options);
|
2016-12-10 19:21:22 +08:00
|
|
|
|
2015-02-09 06:34:49 +08:00
|
|
|
tox_kill(tox1);
|
|
|
|
tox_kill(tox2);
|
|
|
|
tox_kill(tox3);
|
|
|
|
tox_kill(tox4);
|
2014-09-12 10:28:50 +08:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2014-10-17 22:19:27 +08:00
|
|
|
START_TEST(test_keys)
|
|
|
|
{
|
2015-04-01 07:15:59 +08:00
|
|
|
TOX_ERR_ENCRYPTION encerr;
|
|
|
|
TOX_ERR_DECRYPTION decerr;
|
|
|
|
TOX_ERR_KEY_DERIVATION keyerr;
|
2016-12-16 11:00:55 +08:00
|
|
|
Tox_Pass_Key *key = tox_pass_key_derive((const uint8_t *)"123qweasdzxc", 12, &keyerr);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(key != nullptr, "generic failure 1: %u", keyerr);
|
2016-08-18 07:37:45 +08:00
|
|
|
const uint8_t *string = (const uint8_t *)"No Patrick, mayonnaise is not an instrument."; // 44
|
2014-10-17 22:19:27 +08:00
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
uint8_t encrypted[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
|
2016-12-16 11:00:55 +08:00
|
|
|
bool ret = tox_pass_key_encrypt(key, string, 44, encrypted, &encerr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "generic failure 2: %u", encerr);
|
2014-10-17 22:19:27 +08:00
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
uint8_t encrypted2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
|
2016-08-18 07:37:45 +08:00
|
|
|
ret = tox_pass_encrypt(string, 44, (const uint8_t *)"123qweasdzxc", 12, encrypted2, &encerr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "generic failure 3: %u", encerr);
|
2014-10-17 22:19:27 +08:00
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
uint8_t out1[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
|
|
|
|
uint8_t out2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
|
2014-10-17 22:19:27 +08:00
|
|
|
|
2016-12-10 19:21:22 +08:00
|
|
|
ret = tox_pass_key_decrypt(key, encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, out1, &decerr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "generic failure 4: %u", decerr);
|
2014-10-17 22:19:27 +08:00
|
|
|
ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 1 failed");
|
|
|
|
|
2016-08-18 07:37:45 +08:00
|
|
|
ret = tox_pass_decrypt(encrypted2, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)"123qweasdzxc", 12, out2,
|
|
|
|
&decerr);
|
2015-04-01 08:30:09 +08:00
|
|
|
ck_assert_msg(ret, "generic failure 5: %u", decerr);
|
2014-10-17 22:19:27 +08:00
|
|
|
ck_assert_msg(memcmp(out2, string, 44) == 0, "decryption 2 failed");
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
ret = tox_pass_decrypt(encrypted2, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, nullptr, 0, out2, &decerr);
|
2015-05-28 07:54:04 +08:00
|
|
|
ck_assert_msg(!ret, "Decrypt succeeded with wrong pass");
|
|
|
|
ck_assert_msg(decerr != TOX_ERR_DECRYPTION_FAILED, "Bad error code %u", decerr);
|
|
|
|
|
2014-10-17 22:19:27 +08:00
|
|
|
// test that pass_decrypt can decrypt things from pass_key_encrypt
|
2016-08-18 07:37:45 +08:00
|
|
|
ret = tox_pass_decrypt(encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)"123qweasdzxc", 12, out1,
|
|
|
|
&decerr);
|
2015-04-01 07:15:59 +08:00
|
|
|
ck_assert_msg(ret, "generic failure 6: %u", decerr);
|
2014-10-17 22:19:27 +08:00
|
|
|
ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 3 failed");
|
2014-10-23 17:19:18 +08:00
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
uint8_t salt[TOX_PASS_SALT_LENGTH];
|
2016-12-10 19:21:22 +08:00
|
|
|
TOX_ERR_GET_SALT salt_err;
|
|
|
|
ck_assert_msg(tox_get_salt(encrypted, salt, &salt_err), "couldn't get salt");
|
|
|
|
ck_assert_msg(salt_err == TOX_ERR_GET_SALT_OK, "get_salt returned an error");
|
2016-12-16 11:00:55 +08:00
|
|
|
Tox_Pass_Key *key2 = tox_pass_key_derive_with_salt((const uint8_t *)"123qweasdzxc", 12, salt, &keyerr);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(key2 != nullptr, "generic failure 7: %u", keyerr);
|
2016-12-10 19:21:22 +08:00
|
|
|
ck_assert_msg(0 == memcmp(key, key2, TOX_PASS_KEY_LENGTH + TOX_PASS_SALT_LENGTH), "salt comparison failed");
|
|
|
|
tox_pass_key_free(key2);
|
|
|
|
tox_pass_key_free(key);
|
2014-10-17 22:19:27 +08:00
|
|
|
}
|
|
|
|
END_TEST
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static Suite *encryptsave_suite(void)
|
2014-09-12 10:28:50 +08:00
|
|
|
{
|
|
|
|
Suite *s = suite_create("encryptsave");
|
|
|
|
|
2015-04-01 07:15:59 +08:00
|
|
|
DEFTESTCASE_SLOW(known_kdf, 60);
|
2015-02-09 06:34:49 +08:00
|
|
|
DEFTESTCASE_SLOW(save_friend, 20);
|
|
|
|
DEFTESTCASE_SLOW(keys, 30);
|
2014-09-12 10:28:50 +08:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2014-09-12 10:28:50 +08:00
|
|
|
{
|
2018-02-19 01:50:50 +08:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2018-01-29 05:30:39 +08:00
|
|
|
srand((unsigned int) time(nullptr));
|
2014-09-12 10:28:50 +08:00
|
|
|
|
2014-11-26 04:31:46 +08:00
|
|
|
Suite *encryptsave = encryptsave_suite();
|
2014-09-12 10:28:50 +08:00
|
|
|
SRunner *test_runner = srunner_create(encryptsave);
|
|
|
|
|
|
|
|
int number_failed = 0;
|
|
|
|
srunner_run_all(test_runner, CK_NORMAL);
|
|
|
|
number_failed = srunner_ntests_failed(test_runner);
|
|
|
|
|
|
|
|
srunner_free(test_runner);
|
|
|
|
|
|
|
|
return number_failed;
|
|
|
|
}
|