1 #include "olm/olm.h"
2 #include "unittest.hh"
3 
4 #include <cstddef>
5 #include <cstdint>
6 #include <cstring>
7 
8 struct MockRandom {
MockRandomMockRandom9     MockRandom(std::uint8_t tag, std::uint8_t offset = 0)
10         : tag(tag), current(offset) {}
operator ()MockRandom11     void operator()(
12         void * buf, std::size_t length
13     ) {
14         std::uint8_t * bytes = (std::uint8_t *) buf;
15         while (length > 32) {
16             bytes[0] = tag;
17             std::memset(bytes + 1, current, 31);
18             length -= 32;
19             bytes += 32;
20             current += 1;
21         }
22         if (length) {
23             bytes[0] = tag;
24             std::memset(bytes + 1, current, length - 1);
25             current += 1;
26         }
27     }
28     std::uint8_t tag;
29     std::uint8_t current;
30 };
31 
check_malloc(std::size_t size)32 std::uint8_t * check_malloc(std::size_t size) {
33     if (size == std::size_t(-1)) {
34         assert_not_equals(std::size_t(-1), size);
35     }
36     return (std::uint8_t *)::malloc(size);
37 }
38 
main()39 int main() {
40 
41 { /** More messages test */
42 
43 TestCase test_case("More messages test");
44 MockRandom mock_random_a('A', 0x00);
45 MockRandom mock_random_b('B', 0x80);
46 
47 void * a_account_buffer = check_malloc(::olm_account_size());
48 ::OlmAccount *a_account = ::olm_account(a_account_buffer);
49 
50 std::size_t a_random_size = ::olm_create_account_random_length(a_account);
51 void * a_random = check_malloc(a_random_size);
52 mock_random_a(a_random, a_random_size);
53 ::olm_create_account(a_account, a_random, a_random_size);
54 free(a_random);
55 
56 void * b_account_buffer = check_malloc(::olm_account_size());
57 ::OlmAccount *b_account = ::olm_account(b_account_buffer);
58 
59 std::size_t b_random_size = ::olm_create_account_random_length(b_account);
60 void * b_random = check_malloc(b_random_size);
61 mock_random_b(b_random, b_random_size);
62 ::olm_create_account(b_account, b_random, b_random_size);
63 free(b_random);
64 
65 std::size_t o_random_size = ::olm_account_generate_one_time_keys_random_length(
66     b_account, 42
67 );
68 void * o_random = check_malloc(o_random_size);
69 mock_random_b(o_random, o_random_size);
70 ::olm_account_generate_one_time_keys(b_account, 42, o_random, o_random_size);
71 free(o_random);
72 
73 
74 std::size_t b_id_keys_size = ::olm_account_identity_keys_length(b_account);
75 std::size_t b_ot_keys_size = ::olm_account_one_time_keys_length(b_account);
76 std::uint8_t * b_id_keys = (std::uint8_t *) check_malloc(b_id_keys_size);
77 std::uint8_t * b_ot_keys = (std::uint8_t *) check_malloc(b_ot_keys_size);
78 ::olm_account_identity_keys(b_account, b_id_keys, b_id_keys_size);
79 ::olm_account_one_time_keys(b_account, b_ot_keys, b_ot_keys_size);
80 
81 void * a_session_buffer = check_malloc(::olm_session_size());
82 ::OlmSession *a_session = ::olm_session(a_session_buffer);
83 
84 std::size_t a_rand_size = ::olm_create_outbound_session_random_length(a_session);
85 void * a_rand = check_malloc(a_rand_size);
86 mock_random_a(a_rand, a_rand_size);
87 assert_not_equals(std::size_t(-1), ::olm_create_outbound_session(
88     a_session, a_account,
89     b_id_keys + 15, 43,
90     b_ot_keys + 25, 43,
91     a_rand, a_rand_size
92 ));
93 free(b_id_keys);
94 free(b_ot_keys);
95 free(a_rand);
96 
97 void * plaintext = malloc(12);
98 ::memcpy(plaintext, "Hello, World", 12);
99 
100 std::size_t message_1_size = ::olm_encrypt_message_length(a_session, 12);
101 void * message_1 = check_malloc(message_1_size);
102 std::size_t a_message_random_size = ::olm_encrypt_random_length(a_session);
103 void * a_message_random = check_malloc(a_message_random_size);
104 mock_random_a(a_message_random, a_message_random_size);
105 assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session));
106 assert_not_equals(std::size_t(-1), ::olm_encrypt(
107     a_session,
108     plaintext, 12,
109     a_message_random, a_message_random_size,
110     message_1, message_1_size
111 ));
112 free(a_message_random);
113 
114 void * tmp_message_1 = check_malloc(message_1_size);
115 std::memcpy(tmp_message_1, message_1, message_1_size);
116 
117 void * b_session_buffer = check_malloc(olm_account_size());
118 ::OlmSession *b_session = ::olm_session(b_session_buffer);
119 ::olm_create_inbound_session(
120     b_session, b_account, tmp_message_1, message_1_size
121 );
122 
123 std::memcpy(tmp_message_1, message_1, message_1_size);
124 
125 std::size_t plaintext_1_size = ::olm_decrypt_max_plaintext_length(
126     b_session, 0, tmp_message_1, message_1_size
127 );
128 void * plaintext_1 = check_malloc(plaintext_1_size);
129 std::memcpy(tmp_message_1, message_1, message_1_size);
130 assert_equals(std::size_t(12), ::olm_decrypt(
131     b_session, 0,
132     tmp_message_1, message_1_size,
133     plaintext_1, plaintext_1_size
134 ));
135 free(tmp_message_1);
136 free(plaintext_1);
137 free(message_1);
138 
139 assert_not_equals(
140     std::size_t(-1), ::olm_remove_one_time_keys(b_account, b_session)
141 );
142 
143 for (unsigned i = 0; i < 8; ++i) {
144     {
145     std::size_t msg_a_size = ::olm_encrypt_message_length(a_session, 12);
146     std::size_t rnd_a_size = ::olm_encrypt_random_length(a_session);
147     void * msg_a = check_malloc(msg_a_size);
148     void * rnd_a = check_malloc(rnd_a_size);
149     mock_random_a(rnd_a, rnd_a_size);
150     std::size_t type_a = ::olm_encrypt_message_type(a_session);
151     assert_not_equals(std::size_t(-1), ::olm_encrypt(
152         a_session, plaintext, 12, rnd_a, rnd_a_size, msg_a, msg_a_size
153     ));
154     free(rnd_a);
155 
156     void * tmp_a = check_malloc(msg_a_size);
157     std::memcpy(tmp_a, msg_a, msg_a_size);
158     std::size_t out_a_size = ::olm_decrypt_max_plaintext_length(
159         b_session, type_a, tmp_a, msg_a_size
160     );
161     void * out_a = check_malloc(out_a_size);
162     std::memcpy(tmp_a, msg_a, msg_a_size);
163     assert_equals(std::size_t(12), ::olm_decrypt(
164         b_session, type_a, tmp_a, msg_a_size, out_a, out_a_size
165     ));
166     free(tmp_a);
167     free(msg_a);
168     free(out_a);
169     }
170     {
171     std::size_t msg_b_size = ::olm_encrypt_message_length(b_session, 12);
172     std::size_t rnd_b_size = ::olm_encrypt_random_length(b_session);
173     void * msg_b = check_malloc(msg_b_size);
174     void * rnd_b = check_malloc(rnd_b_size);
175     mock_random_b(rnd_b, rnd_b_size);
176     std::size_t type_b = ::olm_encrypt_message_type(b_session);
177     assert_not_equals(std::size_t(-1), ::olm_encrypt(
178             b_session, plaintext, 12, rnd_b, rnd_b_size, msg_b, msg_b_size
179     ));
180     free(rnd_b);
181 
182     void * tmp_b = check_malloc(msg_b_size);
183     std::memcpy(tmp_b, msg_b, msg_b_size);
184     std::size_t out_b_size = ::olm_decrypt_max_plaintext_length(
185             a_session, type_b, tmp_b, msg_b_size
186     );
187     void * out_b = check_malloc(out_b_size);
188     std::memcpy(tmp_b, msg_b, msg_b_size);
189     assert_equals(std::size_t(12), ::olm_decrypt(
190             a_session, type_b, msg_b, msg_b_size, out_b, out_b_size
191     ));
192     free(tmp_b);
193     free(msg_b);
194     free(out_b);
195     }
196 }
197 ::olm_clear_account(a_account);
198 ::olm_clear_account(b_account);
199 ::olm_clear_session(a_session);
200 ::olm_clear_session(b_session);
201 
202 free(a_account_buffer);
203 free(b_account_buffer);
204 free(a_session_buffer);
205 free(b_session_buffer);
206 free(plaintext);
207 
208 }
209 
210 }
211