Coverage Report

Created: 2025-10-08 19:34

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