test_olm.cpp 15.3 KB
Newer Older
Richard van der Hoff's avatar
Richard van der Hoff committed
1
#include "olm/olm.h"
2
3
4
5
6
#include "unittest.hh"

#include <cstddef>
#include <cstdint>
#include <cstring>
7
#include <vector>
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

struct MockRandom {
    MockRandom(std::uint8_t tag, std::uint8_t offset = 0)
        : tag(tag), current(offset) {}
    void operator()(
        std::uint8_t * bytes, std::size_t length
    ) {
        while (length > 32) {
            bytes[0] = tag;
            std::memset(bytes + 1, current, 31);
            length -= 32;
            bytes += 32;
            current += 1;
        }
        if (length) {
            bytes[0] = tag;
            std::memset(bytes + 1, current, length - 1);
            current += 1;
        }
    }
    std::uint8_t tag;
    std::uint8_t current;
};

int main() {

{ /** Pickle account test */

TestCase test_case("Pickle account test");
MockRandom mock_random('P');


40
41
42
43
44
45
std::vector<std::uint8_t> account_buffer(::olm_account_size());
::OlmAccount *account = ::olm_account(account_buffer.data());
std::vector<std::uint8_t> random(::olm_create_account_random_length(account));
mock_random(random.data(), random.size());
::olm_create_account(account, random.data(), random.size());
std::vector<std::uint8_t> ot_random(::olm_account_generate_one_time_keys_random_length(
46
    account, 42
47
48
49
                                        ));
mock_random(ot_random.data(), ot_random.size());
::olm_account_generate_one_time_keys(account, 42, ot_random.data(), ot_random.size());
50

51
std::size_t pickle_length = ::olm_pickle_account_length(account);
52
53
std::vector<std::uint8_t> pickle1(pickle_length);
std::size_t res = ::olm_pickle_account(account, "secret_key", 10, pickle1.data(), pickle_length);
54
55
assert_equals(pickle_length, res);

56
std::vector<std::uint8_t> pickle2(pickle1);
57

58
59
std::vector<std::uint8_t> account_buffer2(::olm_account_size());
::OlmAccount *account2 = ::olm_account(account_buffer2.data());
60
assert_not_equals(std::size_t(-1), ::olm_unpickle_account(
61
    account2, "secret_key", 10, pickle2.data(), pickle_length
62
));
63
assert_equals(pickle_length, ::olm_pickle_account_length(account2));
64
res = ::olm_pickle_account(account2, "secret_key", 10, pickle2.data(), pickle_length);
65
assert_equals(pickle_length, res);
66

67
assert_equals(pickle1.data(), pickle2.data(), pickle_length);
68
69
}

70
71
72
73
74
75
76
77
78
79
80
81

{
    TestCase test_case("Old account unpickle test");

    // this uses the old pickle format, which did not use enough space
    // for the Ed25519 key. We should reject it.
    std::uint8_t pickle[] =
        "x3h9er86ygvq56pM1yesdAxZou4ResPQC9Rszk/fhEL9JY/umtZ2N/foL/SUgVXS"
        "v0IxHHZTafYjDdzJU9xr8dQeBoOTGfV9E/lCqDGBnIlu7SZndqjEKXtzGyQr4sP4"
        "K/A/8TOu9iK2hDFszy6xETiousHnHgh2ZGbRUh4pQx+YMm8ZdNZeRnwFGLnrWyf9"
        "O5TmXua1FcU";

82
83
    std::vector<std::uint8_t> account_buffer(::olm_account_size());
    ::OlmAccount *account = ::olm_account(account_buffer.data());
84
85
86
87
88
89
90
91
92
93
94
95
96
    assert_equals(
        std::size_t(-1),
        ::olm_unpickle_account(
            account, "", 0, pickle, sizeof(pickle)-1
        )
    );
    assert_equals(
        std::string("BAD_LEGACY_ACCOUNT_PICKLE"),
        std::string(::olm_account_last_error(account))
    );
}


97
98
99
100
101
{ /** Pickle session test */

TestCase test_case("Pickle session test");
MockRandom mock_random('P');

102
103
104
105
106
std::vector<std::uint8_t> account_buffer(::olm_account_size());
::OlmAccount *account = ::olm_account(account_buffer.data());
std::vector<std::uint8_t> random(::olm_create_account_random_length(account));
mock_random(random.data(), random.size());
::olm_create_account(account, random.data(), random.size());
107

108
109
std::vector<std::uint8_t> session_buffer(::olm_session_size());
::OlmSession *session = ::olm_session(session_buffer.data());
110
111
112
113
std::uint8_t identity_key[32];
std::uint8_t one_time_key[32];
mock_random(identity_key, sizeof(identity_key));
mock_random(one_time_key, sizeof(one_time_key));
114
115
std::vector<std::uint8_t> random2(::olm_create_outbound_session_random_length(session));
mock_random(random2.data(), random2.size());
116
117
118
119
120

::olm_create_outbound_session(
    session, account,
    identity_key, sizeof(identity_key),
    one_time_key, sizeof(one_time_key),
121
    random2.data(), random2.size()
122
123
124
125
);


std::size_t pickle_length = ::olm_pickle_session_length(session);
126
127
std::vector<std::uint8_t> pickle1(pickle_length);
std::size_t res = ::olm_pickle_session(session, "secret_key", 10, pickle1.data(), pickle_length);
128
129
assert_equals(pickle_length, res);

130
std::vector<std::uint8_t> pickle2(pickle1);
131

132
133
std::vector<std::uint8_t> session_buffer2(::olm_session_size());
::OlmSession *session2 = ::olm_session(session_buffer2.data());
134
assert_not_equals(std::size_t(-1), ::olm_unpickle_session(
135
    session2, "secret_key", 10, pickle2.data(), pickle_length
136
137
));
assert_equals(pickle_length, ::olm_pickle_session_length(session2));
138
res = ::olm_pickle_session(session2, "secret_key", 10, pickle2.data(), pickle_length);
139
assert_equals(pickle_length, res);
140

141
assert_equals(pickle1.data(), pickle2.data(), pickle_length);
142
143
}

144
145
146
147
148
149
{ /** Loopback test */

TestCase test_case("Loopback test");
MockRandom mock_random_a('A', 0x00);
MockRandom mock_random_b('B', 0x80);

150
151
152
153
154
155
156
157
158
159
160
161
std::vector<std::uint8_t> a_account_buffer(::olm_account_size());
::OlmAccount *a_account = ::olm_account(a_account_buffer.data());
std::vector<std::uint8_t> a_random(::olm_create_account_random_length(a_account));
mock_random_a(a_random.data(), a_random.size());
::olm_create_account(a_account, a_random.data(), a_random.size());

std::vector<std::uint8_t> b_account_buffer(::olm_account_size());
::OlmAccount *b_account = ::olm_account(b_account_buffer.data());
std::vector<std::uint8_t> b_random(::olm_create_account_random_length(b_account));
mock_random_b(b_random.data(), b_random.size());
::olm_create_account(b_account, b_random.data(), b_random.size());
std::vector<std::uint8_t> o_random(::olm_account_generate_one_time_keys_random_length(
162
        b_account, 42
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
));
mock_random_b(o_random.data(), o_random.size());
::olm_account_generate_one_time_keys(b_account, 42, o_random.data(), o_random.size());

std::vector<std::uint8_t> a_id_keys(::olm_account_identity_keys_length(a_account));
::olm_account_identity_keys(a_account, a_id_keys.data(), a_id_keys.size());

std::vector<std::uint8_t> b_id_keys(::olm_account_identity_keys_length(b_account));
std::vector<std::uint8_t> b_ot_keys(::olm_account_one_time_keys_length(b_account));
::olm_account_identity_keys(b_account, b_id_keys.data(), b_id_keys.size());
::olm_account_one_time_keys(b_account, b_ot_keys.data(), b_ot_keys.size());

std::vector<std::uint8_t> a_session_buffer(::olm_session_size());
::OlmSession *a_session = ::olm_session(a_session_buffer.data());
std::vector<std::uint8_t> a_rand(::olm_create_outbound_session_random_length(a_session));
mock_random_a(a_rand.data(), a_rand.size());
179
180
assert_not_equals(std::size_t(-1), ::olm_create_outbound_session(
    a_session, a_account,
181
182
183
    b_id_keys.data() + 15, 43, // B's curve25519 identity key
    b_ot_keys.data() + 25, 43, // B's curve25519 one time key
    a_rand.data(), a_rand.size()
184
185
186
));

std::uint8_t plaintext[] = "Hello, World";
187
188
189
std::vector<std::uint8_t> message_1(::olm_encrypt_message_length(a_session, 12));
std::vector<std::uint8_t> a_message_random(::olm_encrypt_random_length(a_session));
mock_random_a(a_message_random.data(), a_message_random.size());
190
191
192
193
assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session));
assert_not_equals(std::size_t(-1), ::olm_encrypt(
    a_session,
    plaintext, 12,
194
195
    a_message_random.data(), a_message_random.size(),
    message_1.data(), message_1.size()
196
197
198
));


199
200
201
std::vector<std::uint8_t> tmp_message_1(message_1);
std::vector<std::uint8_t> b_session_buffer(::olm_account_size());
::OlmSession *b_session = ::olm_session(b_session_buffer.data());
202
::olm_create_inbound_session(
203
    b_session, b_account, tmp_message_1.data(), message_1.size()
204
205
);

206
// Check that the inbound session matches the message it was created from.
207
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
208
209
assert_equals(std::size_t(1), ::olm_matches_inbound_session(
    b_session,
210
    tmp_message_1.data(), message_1.size()
211
212
213
214
));

// Check that the inbound session matches the key this message is supposed
// to be from.
215
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
216
217
assert_equals(std::size_t(1), ::olm_matches_inbound_session_from(
    b_session,
218
219
    a_id_keys.data() + 15, 43, // A's curve125519 identity key.
    tmp_message_1.data(), message_1.size()
220
221
222
));

// Check that the inbound session isn't from a different user.
223
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
224
225
assert_equals(std::size_t(0), ::olm_matches_inbound_session_from(
    b_session,
226
227
    b_id_keys.data() + 15, 43, // B's curve25519 identity key.
    tmp_message_1.data(), message_1.size()
228
229
230
));

// Check that we can decrypt the message.
231
232
233
234
235
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
std::vector<std::uint8_t> plaintext_1(::olm_decrypt_max_plaintext_length(
    b_session, 0, tmp_message_1.data(), message_1.size()
));
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
236
237
assert_equals(std::size_t(12), ::olm_decrypt(
    b_session, 0,
238
239
    tmp_message_1.data(), message_1.size(),
    plaintext_1.data(), plaintext_1.size()
240
241
));

242
assert_equals(plaintext, plaintext_1.data(), 12);
243

244
245
246
std::vector<std::uint8_t> message_2(::olm_encrypt_message_length(b_session, 12));
std::vector<std::uint8_t> b_message_random(::olm_encrypt_random_length(b_session));
mock_random_b(b_message_random.data(), b_message_random.size());
247
248
249
250
assert_equals(std::size_t(1), ::olm_encrypt_message_type(b_session));
assert_not_equals(std::size_t(-1), ::olm_encrypt(
    b_session,
    plaintext, 12,
251
252
    b_message_random.data(), b_message_random.size(),
    message_2.data(), message_2.size()
253
254
));

255
256
257
258
259
std::vector<std::uint8_t> tmp_message_2(message_2);
std::vector<std::uint8_t> plaintext_2(::olm_decrypt_max_plaintext_length(
    a_session, 1, tmp_message_2.data(), message_2.size()
));
std::memcpy(tmp_message_2.data(), message_2.data(), message_2.size());
260
261
assert_equals(std::size_t(12), ::olm_decrypt(
    a_session, 1,
262
263
    tmp_message_2.data(), message_2.size(),
    plaintext_2.data(), plaintext_2.size()
264
265
));

266
assert_equals(plaintext, plaintext_2.data(), 12);
267

268
std::memcpy(tmp_message_2.data(), message_2.data(), message_2.size());
269
270
assert_equals(std::size_t(-1), ::olm_decrypt(
    a_session, 1,
271
272
    tmp_message_2.data(), message_2.size(),
    plaintext_2.data(), plaintext_2.size()
273
274
));

275
std::vector<std::uint8_t> a_session_id(::olm_session_id_length(a_session));
276
assert_not_equals(std::size_t(-1), ::olm_session_id(
277
    a_session, a_session_id.data(), a_session_id.size()
278
279
));

280
std::vector<std::uint8_t> b_session_id(::olm_session_id_length(b_session));
281
assert_not_equals(std::size_t(-1), ::olm_session_id(
282
    b_session, b_session_id.data(), b_session_id.size()
283
284
));

285
286
assert_equals(a_session_id.size(), b_session_id.size());
assert_equals(a_session_id.data(), b_session_id.data(), b_session_id.size());
287

288
289
290
291
292
293
294
295
}

{ /** More messages test */

TestCase test_case("More messages test");
MockRandom mock_random_a('A', 0x00);
MockRandom mock_random_b('B', 0x80);

296
297
298
299
300
301
302
303
304
305
306
307
std::vector<std::uint8_t> a_account_buffer(::olm_account_size());
::OlmAccount *a_account = ::olm_account(a_account_buffer.data());
std::vector<std::uint8_t> a_random(::olm_create_account_random_length(a_account));
mock_random_a(a_random.data(), a_random.size());
::olm_create_account(a_account, a_random.data(), a_random.size());

std::vector<std::uint8_t> b_account_buffer(::olm_account_size());
::OlmAccount *b_account = ::olm_account(b_account_buffer.data());
std::vector<std::uint8_t> b_random(::olm_create_account_random_length(b_account));
mock_random_b(b_random.data(), b_random.size());
::olm_create_account(b_account, b_random.data(), b_random.size());
std::vector<std::uint8_t> o_random(::olm_account_generate_one_time_keys_random_length(
308
        b_account, 42
309
310
311
312
313
314
315
316
317
318
319
320
321
));
mock_random_b(o_random.data(), o_random.size());
::olm_account_generate_one_time_keys(b_account, 42, o_random.data(), o_random.size());

std::vector<std::uint8_t> b_id_keys(::olm_account_identity_keys_length(b_account));
std::vector<std::uint8_t> b_ot_keys(::olm_account_one_time_keys_length(b_account));
::olm_account_identity_keys(b_account, b_id_keys.data(), b_id_keys.size());
::olm_account_one_time_keys(b_account, b_ot_keys.data(), b_ot_keys.size());

std::vector<std::uint8_t> a_session_buffer(::olm_session_size());
::OlmSession *a_session = ::olm_session(a_session_buffer.data());
std::vector<std::uint8_t> a_rand(::olm_create_outbound_session_random_length(a_session));
mock_random_a(a_rand.data(), a_rand.size());
322
323
assert_not_equals(std::size_t(-1), ::olm_create_outbound_session(
    a_session, a_account,
324
325
326
    b_id_keys.data() + 15, 43,
    b_ot_keys.data() + 25, 43,
    a_rand.data(), a_rand.size()
327
328
329
));

std::uint8_t plaintext[] = "Hello, World";
330
331
332
std::vector<std::uint8_t> message_1(::olm_encrypt_message_length(a_session, 12));
std::vector<std::uint8_t> a_message_random(::olm_encrypt_random_length(a_session));
mock_random_a(a_message_random.data(), a_message_random.size());
333
334
335
336
assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session));
assert_not_equals(std::size_t(-1), ::olm_encrypt(
    a_session,
    plaintext, 12,
337
338
    a_message_random.data(), a_message_random.size(),
    message_1.data(), message_1.size()
339
340
));

341
342
343
std::vector<std::uint8_t> tmp_message_1(message_1);
std::vector<std::uint8_t> b_session_buffer(::olm_account_size());
::OlmSession *b_session = ::olm_session(b_session_buffer.data());
344
::olm_create_inbound_session(
345
    b_session, b_account, tmp_message_1.data(), message_1.size()
346
347
);

348
349
350
351
352
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
std::vector<std::uint8_t> plaintext_1(::olm_decrypt_max_plaintext_length(
    b_session, 0, tmp_message_1.data(), message_1.size()
));
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
353
354
assert_equals(std::size_t(12), ::olm_decrypt(
    b_session, 0,
355
356
    tmp_message_1.data(), message_1.size(),
    plaintext_1.data(), plaintext_1.size()
357
358
359
360
));

for (unsigned i = 0; i < 8; ++i) {
    {
361
362
363
    std::vector<std::uint8_t> msg_a(::olm_encrypt_message_length(a_session, 12));
    std::vector<std::uint8_t> rnd_a(::olm_encrypt_random_length(a_session));
    mock_random_a(rnd_a.data(), rnd_a.size());
364
365
    std::size_t type_a = ::olm_encrypt_message_type(a_session);
    assert_not_equals(std::size_t(-1), ::olm_encrypt(
366
        a_session, plaintext, 12, rnd_a.data(), rnd_a.size(), msg_a.data(), msg_a.size()
367
368
    ));

369
370
371
372
373
    std::vector<std::uint8_t> tmp_a(msg_a);
    std::vector<std::uint8_t> out_a(::olm_decrypt_max_plaintext_length(
        b_session, type_a, tmp_a.data(), tmp_a.size()
    ));
    std::memcpy(tmp_a.data(), msg_a.data(), sizeof(msg_a));
374
    assert_equals(std::size_t(12), ::olm_decrypt(
375
        b_session, type_a, msg_a.data(), msg_a.size(), out_a.data(), out_a.size()
376
377
378
    ));
    }
    {
379
380
381
    std::vector<std::uint8_t> msg_b(::olm_encrypt_message_length(b_session, 12));
    std::vector<std::uint8_t> rnd_b(::olm_encrypt_random_length(b_session));
    mock_random_b(rnd_b.data(), rnd_b.size());
382
383
    std::size_t type_b = ::olm_encrypt_message_type(b_session);
    assert_not_equals(std::size_t(-1), ::olm_encrypt(
384
        b_session, plaintext, 12, rnd_b.data(), rnd_b.size(), msg_b.data(), msg_b.size()
385
386
    ));

387
388
389
390
391
    std::vector<std::uint8_t> tmp_b(msg_b);
    std::vector<std::uint8_t> out_b(::olm_decrypt_max_plaintext_length(
        a_session, type_b, tmp_b.data(), tmp_b.size()
    ));
    std::memcpy(tmp_b.data(), msg_b.data(), msg_b.size());
392
    assert_equals(std::size_t(12), ::olm_decrypt(
393
        a_session, type_b, msg_b.data(), msg_b.size(), out_b.data(), out_b.size()
394
395
396
397
398
399
    ));
    }
}
}

}