/work/auto_tests/encryptsave_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stdint.h> |
2 | | #include <stdlib.h> |
3 | | #include <string.h> |
4 | | #include <sys/types.h> |
5 | | |
6 | | #include <sodium.h> |
7 | | |
8 | | #include "../testing/misc_tools.h" |
9 | | #include "../toxcore/ccompat.h" |
10 | | #include "../toxcore/crypto_core.h" |
11 | | #include "../toxcore/tox.h" |
12 | | #include "../toxencryptsave/toxencryptsave.h" |
13 | | #include "auto_test_support.h" |
14 | | #include "check_compat.h" |
15 | | |
16 | | 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}; |
17 | | 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}; |
18 | | static const char *pw = "hunter2"; |
19 | | static unsigned int pwlen = 7; |
20 | | |
21 | | 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}; |
22 | | // same as above, except standard opslimit instead of extra ops limit for test_known_kdf, and hash pw before kdf for compat |
23 | | |
24 | | /* cause I'm shameless */ |
25 | | static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata) |
26 | 50 | { |
27 | 50 | if (*((uint32_t *)userdata) != 974536) { |
28 | 0 | return; |
29 | 0 | } |
30 | | |
31 | 50 | if (length == 7 && memcmp("Gentoo", data, 7) == 0) { |
32 | 50 | tox_friend_add_norequest(m, public_key, nullptr); |
33 | 50 | } |
34 | 50 | } |
35 | | |
36 | | static void test_known_kdf(void) |
37 | 1 | { |
38 | 1 | unsigned char out[CRYPTO_SHARED_KEY_SIZE]; |
39 | 1 | int16_t res = crypto_pwhash_scryptsalsa208sha256(out, |
40 | 1 | CRYPTO_SHARED_KEY_SIZE, |
41 | 1 | pw, |
42 | 1 | pwlen, |
43 | 1 | test_salt, |
44 | 1 | crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE * 8, |
45 | 1 | crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE); |
46 | 1 | ck_assert_msg(res != -1, "crypto function failed"); |
47 | 1 | ck_assert_msg(memcmp(out, known_key, CRYPTO_SHARED_KEY_SIZE) == 0, "derived key is wrong"); |
48 | 1 | } |
49 | | |
50 | | static void test_save_friend(void) |
51 | 1 | { |
52 | 1 | Tox *tox1 = tox_new_log(nullptr, nullptr, nullptr); |
53 | 1 | Tox *tox2 = tox_new_log(nullptr, nullptr, nullptr); |
54 | 1 | ck_assert_msg(tox1 || tox2, "Failed to create 2 tox instances"); |
55 | 1 | tox_callback_friend_request(tox2, accept_friend_request); |
56 | 1 | uint8_t address[TOX_ADDRESS_SIZE]; |
57 | 1 | tox_self_get_address(tox2, address); |
58 | 1 | uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)"Gentoo", 7, nullptr); |
59 | 1 | ck_assert_msg(test != UINT32_MAX, "Failed to add friend"); |
60 | | |
61 | 1 | size_t size = tox_get_savedata_size(tox1); |
62 | 1 | uint8_t *data = (uint8_t *)malloc(size); |
63 | 1 | ck_assert(data != nullptr); |
64 | 1 | tox_get_savedata(tox1, data); |
65 | 1 | size_t size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; |
66 | 1 | uint8_t *enc_data = (uint8_t *)malloc(size2); |
67 | 1 | ck_assert(enc_data != nullptr); |
68 | 1 | Tox_Err_Encryption error1; |
69 | 1 | bool ret = tox_pass_encrypt(data, size, (const uint8_t *)"correcthorsebatterystaple", 25, enc_data, &error1); |
70 | 1 | ck_assert_msg(ret, "failed to encrypted save: %d", error1); |
71 | 1 | ck_assert_msg(tox_is_data_encrypted(enc_data), "magic number missing"); |
72 | | |
73 | 1 | struct Tox_Options *options = tox_options_new(nullptr); |
74 | 1 | ck_assert(options != nullptr); |
75 | 1 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); |
76 | 1 | tox_options_set_savedata_data(options, enc_data, size2); |
77 | | |
78 | 1 | Tox_Err_New err2; |
79 | 1 | Tox *tox3 = tox_new_log(options, &err2, nullptr); |
80 | 1 | ck_assert_msg(err2 == TOX_ERR_NEW_LOAD_ENCRYPTED, "wrong error! %d. should fail with %d", err2, |
81 | 1 | TOX_ERR_NEW_LOAD_ENCRYPTED); |
82 | 1 | ck_assert_msg(tox3 == nullptr, "tox_new with error should return NULL"); |
83 | 1 | uint8_t *dec_data = (uint8_t *)malloc(size); |
84 | 1 | ck_assert(dec_data != nullptr); |
85 | 1 | Tox_Err_Decryption err3; |
86 | 1 | ret = tox_pass_decrypt(enc_data, size2, (const uint8_t *)"correcthorsebatterystaple", 25, dec_data, &err3); |
87 | 1 | ck_assert_msg(ret, "failed to decrypt save: %d", err3); |
88 | 1 | tox_options_set_savedata_data(options, dec_data, size); |
89 | 1 | tox3 = tox_new_log(options, &err2, nullptr); |
90 | 1 | ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to load from decrypted data: %d", err2); |
91 | 1 | uint8_t address2[TOX_PUBLIC_KEY_SIZE]; |
92 | 1 | ret = tox_friend_get_public_key(tox3, 0, address2, nullptr); |
93 | 1 | ck_assert_msg(ret, "no friends!"); |
94 | 1 | ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match!"); |
95 | | |
96 | 1 | size = tox_get_savedata_size(tox3); |
97 | 1 | uint8_t *data2 = (uint8_t *)malloc(size); |
98 | 1 | ck_assert(data2 != nullptr); |
99 | 1 | tox_get_savedata(tox3, data2); |
100 | 1 | Tox_Err_Key_Derivation keyerr; |
101 | 1 | Tox_Pass_Key *key = tox_pass_key_derive((const uint8_t *)"123qweasdzxc", 12, &keyerr); |
102 | 1 | ck_assert_msg(key != nullptr, "pass key allocation failure"); |
103 | 1 | memcpy((uint8_t *)key, test_salt, TOX_PASS_SALT_LENGTH); |
104 | 1 | memcpy((uint8_t *)key + TOX_PASS_SALT_LENGTH, known_key2, TOX_PASS_KEY_LENGTH); |
105 | 1 | size2 = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; |
106 | 1 | uint8_t *encdata2 = (uint8_t *)malloc(size2); |
107 | 1 | ck_assert(encdata2 != nullptr); |
108 | 1 | ret = tox_pass_key_encrypt(key, data2, size, encdata2, &error1); |
109 | 1 | ck_assert_msg(ret, "failed to key encrypt %d", error1); |
110 | 1 | ck_assert_msg(tox_is_data_encrypted(encdata2), "magic number the second missing"); |
111 | | |
112 | 1 | uint8_t *out1 = (uint8_t *)malloc(size); |
113 | 1 | ck_assert(out1 != nullptr); |
114 | 1 | uint8_t *out2 = (uint8_t *)malloc(size); |
115 | 1 | ck_assert(out2 != nullptr); |
116 | 1 | ret = tox_pass_decrypt(encdata2, size2, (const uint8_t *)pw, pwlen, out1, &err3); |
117 | 1 | ck_assert_msg(ret, "failed to pw decrypt %d", err3); |
118 | 1 | ret = tox_pass_key_decrypt(key, encdata2, size2, out2, &err3); |
119 | 1 | ck_assert_msg(ret, "failed to key decrypt %d", err3); |
120 | 1 | ck_assert_msg(memcmp(out1, out2, size) == 0, "differing output data"); |
121 | | |
122 | | // and now with the code in use (I only bothered with manually to debug this, and it seems a waste |
123 | | // to remove the manual check now that it's there) |
124 | 1 | tox_options_set_savedata_data(options, out1, size); |
125 | 1 | Tox *tox4 = tox_new_log(options, &err2, nullptr); |
126 | 1 | ck_assert_msg(err2 == TOX_ERR_NEW_OK, "failed to new the third"); |
127 | 1 | uint8_t address5[TOX_PUBLIC_KEY_SIZE]; |
128 | 1 | ret = tox_friend_get_public_key(tox4, 0, address5, nullptr); |
129 | 1 | ck_assert_msg(ret, "no friends! the third"); |
130 | 1 | ck_assert_msg(memcmp(address, address2, TOX_PUBLIC_KEY_SIZE) == 0, "addresses don't match! the third"); |
131 | | |
132 | 1 | tox_pass_key_free(key); |
133 | 1 | tox_options_free(options); |
134 | | |
135 | 1 | tox_kill(tox1); |
136 | 1 | tox_kill(tox2); |
137 | 1 | tox_kill(tox3); |
138 | 1 | tox_kill(tox4); |
139 | | |
140 | 1 | free(out2); |
141 | 1 | free(out1); |
142 | 1 | free(encdata2); |
143 | 1 | free(data2); |
144 | 1 | free(dec_data); |
145 | 1 | free(enc_data); |
146 | 1 | free(data); |
147 | 1 | } |
148 | | |
149 | | static void test_keys(void) |
150 | 1 | { |
151 | 1 | Tox_Err_Encryption encerr; |
152 | 1 | Tox_Err_Decryption decerr; |
153 | 1 | Tox_Err_Key_Derivation keyerr; |
154 | 1 | const uint8_t *key_char = (const uint8_t *)"123qweasdzxc"; |
155 | 1 | Tox_Pass_Key *key = tox_pass_key_derive(key_char, 12, &keyerr); |
156 | 1 | ck_assert_msg(key != nullptr, "generic failure 1: %d", keyerr); |
157 | 1 | const uint8_t *string = (const uint8_t *)"No Patrick, mayonnaise is not an instrument."; // 44 |
158 | | |
159 | 1 | uint8_t encrypted[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; |
160 | 1 | bool ret = tox_pass_key_encrypt(key, string, 44, encrypted, &encerr); |
161 | 1 | ck_assert_msg(ret, "generic failure 2: %d", encerr); |
162 | | |
163 | | // Testing how tox handles encryption of large messages. |
164 | 1 | int size_large = 30 * 1024 * 1024; |
165 | 1 | int ciphertext_length2a = size_large + TOX_PASS_ENCRYPTION_EXTRA_LENGTH; |
166 | 1 | int plaintext_length2a = size_large; |
167 | 1 | uint8_t *encrypted2a = (uint8_t *)malloc(ciphertext_length2a); |
168 | 1 | ck_assert(encrypted2a != nullptr); |
169 | 1 | uint8_t *in_plaintext2a = (uint8_t *)malloc(plaintext_length2a); |
170 | 1 | ck_assert(in_plaintext2a != nullptr); |
171 | 1 | const Random *rng = os_random(); |
172 | 1 | ck_assert(rng != nullptr); |
173 | 1 | random_bytes(rng, in_plaintext2a, plaintext_length2a); |
174 | 1 | ret = tox_pass_encrypt(in_plaintext2a, plaintext_length2a, key_char, 12, encrypted2a, &encerr); |
175 | 1 | ck_assert_msg(ret, "tox_pass_encrypt failure 2a: %d", encerr); |
176 | | |
177 | | // Decryption of same message. |
178 | 1 | uint8_t *out_plaintext2a = (uint8_t *)malloc(plaintext_length2a); |
179 | 1 | ck_assert(out_plaintext2a != nullptr); |
180 | 1 | ret = tox_pass_decrypt(encrypted2a, ciphertext_length2a, key_char, 12, out_plaintext2a, &decerr); |
181 | 1 | ck_assert_msg(ret, "tox_pass_decrypt failure 2a: %d", decerr); |
182 | 1 | ck_assert_msg(memcmp(in_plaintext2a, out_plaintext2a, plaintext_length2a) == 0, "Large message decryption failed"); |
183 | 1 | free(encrypted2a); |
184 | 1 | free(in_plaintext2a); |
185 | 1 | free(out_plaintext2a); |
186 | | |
187 | | |
188 | 1 | uint8_t encrypted2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; |
189 | 1 | ret = tox_pass_encrypt(string, 44, key_char, 12, encrypted2, &encerr); |
190 | 1 | ck_assert_msg(ret, "generic failure 3: %d", encerr); |
191 | | |
192 | 1 | uint8_t out1[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; |
193 | 1 | uint8_t out2[44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; |
194 | | |
195 | 1 | ret = tox_pass_key_decrypt(key, encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, out1, &decerr); |
196 | 1 | ck_assert_msg(ret, "generic failure 4: %d", decerr); |
197 | 1 | ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 1 failed"); |
198 | | |
199 | 1 | ret = tox_pass_decrypt(encrypted2, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)"123qweasdzxc", 12, out2, |
200 | 1 | &decerr); |
201 | 1 | ck_assert_msg(ret, "generic failure 5: %d", decerr); |
202 | 1 | ck_assert_msg(memcmp(out2, string, 44) == 0, "decryption 2 failed"); |
203 | | |
204 | 1 | ret = tox_pass_decrypt(encrypted2, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, nullptr, 0, out2, &decerr); |
205 | 1 | ck_assert_msg(!ret, "Decrypt succeeded with wrong pass"); |
206 | 1 | ck_assert_msg(decerr != TOX_ERR_DECRYPTION_FAILED, "Bad error code %d", decerr); |
207 | | |
208 | | // test that pass_decrypt can decrypt things from pass_key_encrypt |
209 | 1 | ret = tox_pass_decrypt(encrypted, 44 + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, (const uint8_t *)"123qweasdzxc", 12, out1, |
210 | 1 | &decerr); |
211 | 1 | ck_assert_msg(ret, "generic failure 6: %d", decerr); |
212 | 1 | ck_assert_msg(memcmp(out1, string, 44) == 0, "decryption 3 failed"); |
213 | | |
214 | 1 | uint8_t salt[TOX_PASS_SALT_LENGTH]; |
215 | 1 | Tox_Err_Get_Salt salt_err; |
216 | 1 | ck_assert_msg(tox_get_salt(encrypted, salt, &salt_err), "couldn't get salt"); |
217 | 1 | ck_assert_msg(salt_err == TOX_ERR_GET_SALT_OK, "get_salt returned an error"); |
218 | 1 | Tox_Pass_Key *key2 = tox_pass_key_derive_with_salt((const uint8_t *)"123qweasdzxc", 12, salt, &keyerr); |
219 | 1 | ck_assert_msg(key2 != nullptr, "generic failure 7: %d", keyerr); |
220 | 1 | ck_assert_msg(0 == memcmp(key, key2, TOX_PASS_KEY_LENGTH + TOX_PASS_SALT_LENGTH), "salt comparison failed"); |
221 | 1 | tox_pass_key_free(key2); |
222 | 1 | tox_pass_key_free(key); |
223 | 1 | } |
224 | | |
225 | | int main(void) |
226 | 721 | { |
227 | 721 | setvbuf(stdout, nullptr, _IONBF, 0); |
228 | 721 | test_known_kdf(); |
229 | 721 | test_save_friend(); |
230 | 721 | test_keys(); |
231 | | |
232 | 721 | return 0; |
233 | 721 | } |