summaryrefslogtreecommitdiff
path: root/auto_tests/encryptsave_test.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-03-31 19:44:51 -0400
committerirungentoo <irungentoo@gmail.com>2015-03-31 19:44:51 -0400
commit99e0fde2977aac291552a678323bb16e62046fb0 (patch)
treea484c48c301538e1c43d54cf056ba0204338f60e /auto_tests/encryptsave_test.c
parent48249360dcde0a8c81ae62c2bace349fdb8274a1 (diff)
parente998aca8f75e46cd360210ac588ee77af0106c6d (diff)
Merge branch 'master' of https://github.com/dubslow/toxcore
Diffstat (limited to 'auto_tests/encryptsave_test.c')
-rw-r--r--auto_tests/encryptsave_test.c125
1 files changed, 67 insertions, 58 deletions
diff --git a/auto_tests/encryptsave_test.c b/auto_tests/encryptsave_test.c
index e2f41f43..13e06db4 100644
--- a/auto_tests/encryptsave_test.c
+++ b/auto_tests/encryptsave_test.c
@@ -55,7 +55,6 @@ END_TEST
55 55
56START_TEST(test_save_friend) 56START_TEST(test_save_friend)
57{ 57{
58 TOX_ERR_ENCRYPTED_NEW err = TOX_ERR_ENCRYPTED_NEW_OK;
59 Tox *tox1 = tox_new(0, 0, 0, 0); 58 Tox *tox1 = tox_new(0, 0, 0, 0);
60 Tox *tox2 = tox_new(0, 0, 0, 0); 59 Tox *tox2 = tox_new(0, 0, 0, 0);
61 ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); 60 ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances");
@@ -63,99 +62,109 @@ START_TEST(test_save_friend)
63 tox_callback_friend_request(tox2, accept_friend_request, &to_compare); 62 tox_callback_friend_request(tox2, accept_friend_request, &to_compare);
64 uint8_t address[TOX_ADDRESS_SIZE]; 63 uint8_t address[TOX_ADDRESS_SIZE];
65 tox_self_get_address(tox2, address); 64 tox_self_get_address(tox2, address);
66 int test = tox_friend_add(tox1, address, (uint8_t *)"Gentoo", 7, 0); 65 uint32_t test = tox_friend_add(tox1, address, (uint8_t *)"Gentoo", 7, 0);
67 ck_assert_msg(test == 0, "Failed to add friend error code: %i", test); 66 ck_assert_msg(test != UINT32_MAX, "Failed to add friend");
68 67
69 uint32_t size = tox_encrypted_size(tox1); 68 size_t size = tox_get_savedata_size(tox1);
70 uint8_t data[size]; 69 uint8_t data[size];
71 test = tox_encrypted_save(tox1, data, "correcthorsebatterystaple", 25); 70 tox_get_savedata(tox1, data);
72 ck_assert_msg(test == 0, "failed to encrypted save"); 71 size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
73 //ck_assert_msg(tox_is_save_encrypted(data) == 1, "magic number missing"); 72 uint8_t enc_data[size2];
74 73 TOX_ERR_ENCRYPTION error1;
75 Tox *tox3 = tox_encrypted_new(0, data, size, "correcthorsebatterystaple", 25, &err); 74 bool ret = tox_pass_encrypt(data, size, "correcthorsebatterystaple", 25, enc_data, &error1);
76 ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new"); 75 ck_assert_msg(ret, "failed to encrypted save: %u", error1);
76 ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing");
77
78 TOX_ERR_NEW err2;
79 Tox *tox3 = tox_new(0, enc_data, size2, &err2);
80 ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %u. should fail with %u", err2,
81 TOX_ERR_NEW_LOAD_ENCRYPTED);
82 uint8_t dec_data[size];
83 TOX_ERR_DECRYPTION err3;
84 ret = tox_pass_decrypt(enc_data, size2, "correcthorsebatterystaple", 25, dec_data, &err3);
85 ck_assert_msg(ret, "failed to decrypt save: %u", err3);
86 tox3 = tox_new(0, dec_data, size, &err2);
87 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %u", err2);
77 uint8_t address2[TOX_PUBLIC_KEY_SIZE]; 88 uint8_t address2[TOX_PUBLIC_KEY_SIZE];
78 test = tox_friend_get_public_key(tox3, 0, address2, 0); 89 ret = tox_friend_get_public_key(tox3, 0, address2, 0);
79 ck_assert_msg(test == 1, "no friends!"); 90 ck_assert_msg(ret, "no friends!");
80 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!"); 91 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!");
81 92
82 size = tox_encrypted_size(tox3); 93 size = tox_get_savedata_size(tox3);
83 uint8_t data2[size]; 94 uint8_t data2[size];
95 tox_get_savedata(tox3, data2);
84 uint8_t key[32 + crypto_box_BEFORENMBYTES]; 96 uint8_t key[32 + crypto_box_BEFORENMBYTES];
85 memcpy(key, salt, 32); 97 memcpy(key, salt, 32);
86 memcpy(key + 32, known_key2, crypto_box_BEFORENMBYTES); 98 memcpy(key + 32, known_key2, crypto_box_BEFORENMBYTES);
87 test = tox_encrypted_key_save(tox3, data2, key); 99 size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
88 ck_assert_msg(test == 0, "failed to encrypted save the second"); 100 uint8_t encdata2[size2];
89 //ck_assert_msg(tox_is_save_encrypted(data2) == 1, "magic number the second missing"); 101 ret = tox_pass_key_encrypt(data2, size, key, encdata2, &error1);
90 102 ck_assert_msg(ret, "failed to key encrypt %u", error1);
91 // first test tox_encrypted_key_new 103 ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing");
92 Tox *tox4 = tox_encrypted_key_new(0, data2, size, key, &err); 104
93 ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new the second");
94 uint8_t address4[TOX_PUBLIC_KEY_SIZE];
95 test = tox_friend_get_public_key(tox4, 0, address4, 0);
96 ck_assert_msg(test == 1, "no friends! the second");
97 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the second");
98
99 // now test compaitibilty with tox_encrypted_new, first manually...
100 uint8_t out1[size], out2[size]; 105 uint8_t out1[size], out2[size];
101 //printf("Trying to decrypt from pw:\n"); 106 ret = tox_pass_decrypt(encdata2, size2, pw, pwlen, out1, &err3);
102 uint32_t sz1 = tox_pass_decrypt(data2, size, pw, pwlen, out1); 107 ck_assert_msg(ret, "failed to pw decrypt %u", err3);
103 uint32_t sz2 = tox_pass_key_decrypt(data2, size, key, out2); 108 ret = tox_pass_key_decrypt(encdata2, size2, key, out2, &err3);
104 ck_assert_msg(sz1 == sz2, "differing output sizes"); 109 ck_assert_msg(ret, "failed to key decrypt %u", err3);
105 ck_assert_msg(memcmp(out1, out2, sz1) == 0, "differing output data"); 110 ck_assert_msg(memcmp(out1, out2, size) == 0, "differing output data");
106 111
107 // and now with the code in use (I only bothered with manually to debug this, and it seems a waste 112 // and now with the code in use (I only bothered with manually to debug this, and it seems a waste
108 // to remove the manual check now that it's there) 113 // to remove the manual check now that it's there)
109 Tox *tox5 = tox_encrypted_new(0, data2, size, pw, pwlen, &err); 114 Tox *tox4 = tox_new(0, out1, size, &err2);
110 ck_assert_msg(err == TOX_ERR_ENCRYPTED_NEW_OK, "failed to encrypted new the third"); 115 ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third");
111 uint8_t address5[TOX_PUBLIC_KEY_SIZE]; 116 uint8_t address5[TOX_PUBLIC_KEY_SIZE];
112 test = tox_friend_get_public_key(tox4, 0, address5, 0); 117 ret = tox_friend_get_public_key(tox4, 0, address5, 0);
113 ck_assert_msg(test == 1, "no friends! the third"); 118 ck_assert_msg(ret, "no friends! the third");
114 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third"); 119 ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third");
115 120
116 tox_kill(tox1); 121 tox_kill(tox1);
117 tox_kill(tox2); 122 tox_kill(tox2);
118 tox_kill(tox3); 123 tox_kill(tox3);
119 tox_kill(tox4); 124 tox_kill(tox4);
120 tox_kill(tox5);
121} 125}
122END_TEST 126END_TEST
123 127
124START_TEST(test_keys) 128START_TEST(test_keys)
125{ 129{
126 uint8_t key[tox_pass_key_length()]; 130 TOX_ERR_ENCRYPTION encerr;
127 tox_derive_key_from_pass("123qweasdzxc", 12, key); 131 TOX_ERR_DECRYPTION decerr;
132 TOX_ERR_KEY_DERIVATION keyerr;
133 uint8_t key[TOX_PASS_KEY_LENGTH];
134 bool ret = tox_derive_key_from_pass("123qweasdzxc", 12, key, &keyerr);
135 ck_assert_msg(ret, "generic failure 1: %u", keyerr);
128 uint8_t *string = "No Patrick, mayonnaise is not an instrument."; // 44 136 uint8_t *string = "No Patrick, mayonnaise is not an instrument."; // 44
129 137
130 uint8_t encrypted[44 + tox_pass_encryption_extra_length()]; 138 uint8_t encrypted[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
131 int sz = tox_pass_key_encrypt(string, 44, key, encrypted); 139 ret = tox_pass_key_encrypt(string, 44, key, encrypted, &encerr);
132 140 ck_assert_msg(ret, "generic failure 2: %u", encerr);
133 uint8_t encrypted2[44 + tox_pass_encryption_extra_length()];
134 int sz2 = tox_pass_encrypt(string, 44, "123qweasdzxc", 12, encrypted2);
135 141
136 ck_assert_msg(sz == sz2, "an encryption failed"); 142 uint8_t encrypted2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
143 ret = tox_pass_encrypt(string, 44, "123qweasdzxc", 12, encrypted2, &encerr);
144 ck_assert_msg(ret, "generic failure 3: %u", encerr);
137 145
138 uint8_t out1[44 + tox_pass_encryption_extra_length()]; 146 uint8_t out1[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
139 uint8_t out2[44 + tox_pass_encryption_extra_length()]; 147 uint8_t out2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH];
140 148
141 sz = tox_pass_key_decrypt(encrypted, 44 + tox_pass_encryption_extra_length(), key, out1); 149 ret = tox_pass_key_decrypt(encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, key, out1, &decerr);
142 ck_assert_msg(sz == 44, "sz isn't right"); 150 ck_assert_msg(ret, "generic failure 4: %u", decerr);
143 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 1 failed"); 151 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 1 failed");
144 152
145 sz2 = tox_pass_decrypt(encrypted2, 44 + tox_pass_encryption_extra_length(), "123qweasdzxc", 12, out2); 153 ret = tox_pass_decrypt(encrypted2, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, "123qweasdzxc", 12, out2, &decerr);
146 ck_assert_msg(sz2 == 44, "sz2 isn't right"); 154 ck_assert_msg(ret, "generic failure 5: %u", &decerr);
147 ck_assert_msg(memcmp(out2, string, 44) == 0, "decryption 2 failed"); 155 ck_assert_msg(memcmp(out2, string, 44) == 0, "decryption 2 failed");
148 156
149 // test that pass_decrypt can decrypt things from pass_key_encrypt 157 // test that pass_decrypt can decrypt things from pass_key_encrypt
150 sz = tox_pass_decrypt(encrypted, 44 + tox_pass_encryption_extra_length(), "123qweasdzxc", 12, out1); 158 ret = tox_pass_decrypt(encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, "123qweasdzxc", 12, out1, &decerr);
151 ck_assert_msg(sz == 44, "sz isn't right"); 159 ck_assert_msg(ret, "generic failure 6: %u", decerr);
152 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 3 failed"); 160 ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 3 failed");
153 161
154 uint8_t salt[tox_pass_salt_length()]; 162 uint8_t salt[TOX_PASS_SALT_LENGTH];
155 ck_assert_msg(0 == tox_get_salt(encrypted, salt), "couldn't get salt"); 163 ck_assert_msg(tox_get_salt(encrypted, salt), "couldn't get salt");
156 uint8_t key2[tox_pass_key_length()]; 164 uint8_t key2[TOX_PASS_KEY_LENGTH];
157 tox_derive_key_with_salt("123qweasdzxc", 12, salt, key2); 165 ret = tox_derive_key_with_salt("123qweasdzxc", 12, salt, key2, &keyerr);
158 ck_assert_msg(0 == memcmp(key, key2, tox_pass_key_length()), "salt comparison failed"); 166 ck_assert_msg(ret, "generic failure 7: %u", keyerr);
167 ck_assert_msg(0 == memcmp(key, key2, TOX_PASS_KEY_LENGTH), "salt comparison failed");
159} 168}
160END_TEST 169END_TEST
161 170
@@ -163,7 +172,7 @@ Suite *encryptsave_suite(void)
163{ 172{
164 Suite *s = suite_create("encryptsave"); 173 Suite *s = suite_create("encryptsave");
165 174
166 DEFTESTCASE_SLOW(known_kdf, 60); /* is 5-10 seconds on my computer, but is directly dependent on CPU */ 175 DEFTESTCASE_SLOW(known_kdf, 60);
167 DEFTESTCASE_SLOW(save_friend, 20); 176 DEFTESTCASE_SLOW(save_friend, 20);
168 DEFTESTCASE_SLOW(keys, 30); 177 DEFTESTCASE_SLOW(keys, 30);
169 178