don't use variable length or zero-length arrays in test files

as some compilers don't handle that
This commit is contained in:
Hubert Chathi 2019-04-22 10:12:42 -04:00
parent 157c0fa67e
commit ea13edcae0
9 changed files with 346 additions and 342 deletions

View file

@ -14,7 +14,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = olm::encode_base64_length(input_length);
assert_equals(std::size_t(15), output_length);
std::uint8_t output[output_length];
std::uint8_t output[15];
olm::encode_base64(input, input_length, output);
assert_equals(expected_output, output, output_length);
}
@ -29,7 +29,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = ::_olm_encode_base64_length(input_length);
assert_equals(std::size_t(15), output_length);
std::uint8_t output[output_length];
std::uint8_t output[15];
output_length = ::_olm_encode_base64(input, input_length, output);
assert_equals(std::size_t(15), output_length);
assert_equals(expected_output, output, output_length);
@ -45,7 +45,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = olm::decode_base64_length(input_length);
assert_equals(std::size_t(11), output_length);
std::uint8_t output[output_length];
std::uint8_t output[11];
olm::decode_base64(input, input_length, output);
assert_equals(expected_output, output, output_length);
}
@ -60,7 +60,7 @@ std::size_t input_length = sizeof(input) - 1;
std::size_t output_length = ::_olm_decode_base64_length(input_length);
assert_equals(std::size_t(11), output_length);
std::uint8_t output[output_length];
std::uint8_t output[11];
output_length = ::_olm_decode_base64(input, input_length, output);
assert_equals(std::size_t(11), output_length);
assert_equals(expected_output, output, output_length);

View file

@ -146,7 +146,10 @@ assert_equals(input, actual, length);
TestCase test_case("SHA 256 Test Case 1");
std::uint8_t input[0] = {};
// we want to take the hash of the empty string, but MSVC doesn't like
// allocating 0 bytes, so allocate one item, but pass a length of zero to
// sha256
std::uint8_t input[1] = {0};
std::uint8_t expected[32] = {
0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
@ -157,7 +160,7 @@ std::uint8_t expected[32] = {
std::uint8_t actual[32];
_olm_crypto_sha256(input, sizeof(input), actual);
_olm_crypto_sha256(input, 0, actual);
assert_equals(expected, actual, 32);
@ -167,7 +170,10 @@ assert_equals(expected, actual, 32);
TestCase test_case("HMAC Test Case 1");
std::uint8_t input[0] = {};
// we want to take the hash of the empty string, but MSVC doesn't like
// allocating 0 bytes, so allocate one item, but pass a length of zero to
// hmac_sha256
std::uint8_t input[1] = {0};
std::uint8_t expected[32] = {
0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec,
@ -178,7 +184,7 @@ std::uint8_t expected[32] = {
std::uint8_t actual[32];
_olm_crypto_hmac_sha256(input, sizeof(input), input, sizeof(input), actual);
_olm_crypto_hmac_sha256(input, 0, input, 0, actual);
assert_equals(expected, actual, 32);

View file

@ -16,6 +16,7 @@
#include "olm/outbound_group_session.h"
#include "unittest.hh"
#include <vector>
int main() {
@ -23,33 +24,32 @@ int main() {
TestCase test_case("Pickle outbound group session");
size_t size = olm_outbound_group_session_size();
uint8_t memory[size];
OlmOutboundGroupSession *session = olm_outbound_group_session(memory);
std::vector<uint8_t> memory(size);
OlmOutboundGroupSession *session = olm_outbound_group_session(memory.data());
size_t pickle_length = olm_pickle_outbound_group_session_length(session);
uint8_t pickle1[pickle_length];
std::vector<uint8_t> pickle1(pickle_length);
size_t res = olm_pickle_outbound_group_session(
session, "secret_key", 10, pickle1, pickle_length
session, "secret_key", 10, pickle1.data(), pickle_length
);
assert_equals(pickle_length, res);
uint8_t pickle2[pickle_length];
memcpy(pickle2, pickle1, pickle_length);
std::vector<uint8_t> pickle2(pickle1);
uint8_t buffer2[size];
OlmOutboundGroupSession *session2 = olm_outbound_group_session(buffer2);
std::vector<uint8_t> buffer2(size);
OlmOutboundGroupSession *session2 = olm_outbound_group_session(buffer2.data());
res = olm_unpickle_outbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length
session2, "secret_key", 10, pickle2.data(), pickle_length
);
assert_not_equals((size_t)-1, res);
assert_equals(pickle_length,
olm_pickle_outbound_group_session_length(session2));
res = olm_pickle_outbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length
session2, "secret_key", 10, pickle2.data(), pickle_length
);
assert_equals(pickle_length, res);
assert_equals(pickle1, pickle2, pickle_length);
assert_equals(pickle1.data(), pickle2.data(), pickle_length);
}
@ -57,32 +57,31 @@ int main() {
TestCase test_case("Pickle inbound group session");
size_t size = olm_inbound_group_session_size();
uint8_t memory[size];
OlmInboundGroupSession *session = olm_inbound_group_session(memory);
std::vector<uint8_t> memory(size);
OlmInboundGroupSession *session = olm_inbound_group_session(memory.data());
size_t pickle_length = olm_pickle_inbound_group_session_length(session);
uint8_t pickle1[pickle_length];
std::vector<uint8_t> pickle1(pickle_length);
size_t res = olm_pickle_inbound_group_session(
session, "secret_key", 10, pickle1, pickle_length
session, "secret_key", 10, pickle1.data(), pickle_length
);
assert_equals(pickle_length, res);
uint8_t pickle2[pickle_length];
memcpy(pickle2, pickle1, pickle_length);
std::vector<uint8_t> pickle2(pickle1);
uint8_t buffer2[size];
OlmInboundGroupSession *session2 = olm_inbound_group_session(buffer2);
std::vector<uint8_t> buffer2(size);
OlmInboundGroupSession *session2 = olm_inbound_group_session(buffer2.data());
res = olm_unpickle_inbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length
session2, "secret_key", 10, pickle2.data(), pickle_length
);
assert_not_equals((size_t)-1, res);
assert_equals(pickle_length,
olm_pickle_inbound_group_session_length(session2));
res = olm_pickle_inbound_group_session(
session2, "secret_key", 10, pickle2, pickle_length
session2, "secret_key", 10, pickle2.data(), pickle_length
);
assert_equals(pickle1, pickle2, pickle_length);
assert_equals(pickle1.data(), pickle2.data(), pickle_length);
}
{
@ -99,8 +98,8 @@ int main() {
/* build the outbound session */
size_t size = olm_outbound_group_session_size();
uint8_t memory[size];
OlmOutboundGroupSession *session = olm_outbound_group_session(memory);
std::vector<uint8_t> memory(size);
OlmOutboundGroupSession *session = olm_outbound_group_session(memory.data());
assert_equals((size_t)160,
olm_init_outbound_group_session_random_length(session));
@ -111,8 +110,8 @@ int main() {
assert_equals(0U, olm_outbound_group_session_message_index(session));
size_t session_key_len = olm_outbound_group_session_key_length(session);
uint8_t session_key[session_key_len];
olm_outbound_group_session_key(session, session_key, session_key_len);
std::vector<uint8_t> session_key(session_key_len);
olm_outbound_group_session_key(session, session_key.data(), session_key_len);
/* encode the message */
uint8_t plaintext[] = "Message";
@ -121,57 +120,56 @@ int main() {
size_t msglen = olm_group_encrypt_message_length(
session, plaintext_length);
uint8_t msg[msglen];
std::vector<uint8_t> msg(msglen);
res = olm_group_encrypt(session, plaintext, plaintext_length,
msg, msglen);
msg.data(), msglen);
assert_equals(msglen, res);
assert_equals(1U, olm_outbound_group_session_message_index(session));
/* build the inbound session */
size = olm_inbound_group_session_size();
uint8_t inbound_session_memory[size];
std::vector<uint8_t> inbound_session_memory(size);
OlmInboundGroupSession *inbound_session =
olm_inbound_group_session(inbound_session_memory);
olm_inbound_group_session(inbound_session_memory.data());
assert_equals(0, olm_inbound_group_session_is_verified(inbound_session));
res = olm_init_inbound_group_session(
inbound_session, session_key, session_key_len);
inbound_session, session_key.data(), session_key_len);
assert_equals((size_t)0, res);
assert_equals(1, olm_inbound_group_session_is_verified(inbound_session));
/* Check the session ids */
size_t out_session_id_len = olm_outbound_group_session_id_length(session);
uint8_t out_session_id[out_session_id_len];
std::vector<uint8_t> out_session_id(out_session_id_len);
assert_equals(out_session_id_len, olm_outbound_group_session_id(
session, out_session_id, out_session_id_len
session, out_session_id.data(), out_session_id_len
));
size_t in_session_id_len = olm_inbound_group_session_id_length(
inbound_session
);
uint8_t in_session_id[in_session_id_len];
std::vector<uint8_t> in_session_id(in_session_id_len);
assert_equals(in_session_id_len, olm_inbound_group_session_id(
inbound_session, in_session_id, in_session_id_len
inbound_session, in_session_id.data(), in_session_id_len
));
assert_equals(in_session_id_len, out_session_id_len);
assert_equals(out_session_id, in_session_id, in_session_id_len);
assert_equals(out_session_id.data(), in_session_id.data(), in_session_id_len);
/* decode the message */
/* olm_group_decrypt_max_plaintext_length destroys the input so we have to
copy it. */
uint8_t msgcopy[msglen];
memcpy(msgcopy, msg, msglen);
size = olm_group_decrypt_max_plaintext_length(inbound_session, msgcopy, msglen);
uint8_t plaintext_buf[size];
std::vector<uint8_t> msgcopy(msg);
size = olm_group_decrypt_max_plaintext_length(inbound_session, msgcopy.data(), msglen);
std::vector<uint8_t> plaintext_buf(size);
uint32_t message_index;
res = olm_group_decrypt(inbound_session, msg, msglen,
plaintext_buf, size, &message_index);
res = olm_group_decrypt(inbound_session, msg.data(), msglen,
plaintext_buf.data(), size, &message_index);
assert_equals(plaintext_length, res);
assert_equals(plaintext, plaintext_buf, res);
assert_equals(plaintext, plaintext_buf.data(), res);
assert_equals(message_index, uint32_t(0));
}
@ -192,9 +190,9 @@ int main() {
/* init first inbound group session, and decrypt */
std::size_t size = olm_inbound_group_session_size();
uint8_t session_memory1[size];
std::vector<uint8_t> session_memory1(size);
OlmInboundGroupSession *session1 =
olm_inbound_group_session(session_memory1);
olm_inbound_group_session(session_memory1.data());
assert_equals(0, olm_inbound_group_session_is_verified(session1));
std::size_t res = olm_init_inbound_group_session(
@ -205,24 +203,24 @@ int main() {
/* olm_group_decrypt_max_plaintext_length destroys the input so we have to
copy it. */
uint8_t msgcopy[msglen];
memcpy(msgcopy, message, msglen);
size = olm_group_decrypt_max_plaintext_length(session1, msgcopy, msglen);
uint8_t plaintext_buf[size];
std::vector<uint8_t> msgcopy(msglen);
memcpy(msgcopy.data(), message, msglen);
size = olm_group_decrypt_max_plaintext_length(session1, msgcopy.data(), msglen);
std::vector<uint8_t> plaintext_buf(size);
uint32_t message_index;
memcpy(msgcopy, message, msglen);
memcpy(msgcopy.data(), message, msglen);
res = olm_group_decrypt(
session1, msgcopy, msglen, plaintext_buf, size, &message_index
session1, msgcopy.data(), msglen, plaintext_buf.data(), size, &message_index
);
assert_equals((std::size_t)7, res);
assert_equals((const uint8_t *)"Message", plaintext_buf, res);
assert_equals((const uint8_t *)"Message", plaintext_buf.data(), res);
assert_equals(uint32_t(0), message_index);
/* export the keys */
size = olm_export_inbound_group_session_length(session1);
uint8_t export_memory[size];
std::vector<uint8_t> export_memory(size);
res = olm_export_inbound_group_session(
session1, export_memory, size, 0
session1, export_memory.data(), size, 0
);
assert_equals(size, res);
@ -231,25 +229,25 @@ int main() {
/* import the keys into another inbound group session */
size = olm_inbound_group_session_size();
uint8_t session_memory2[size];
std::vector<uint8_t> session_memory2(size);
OlmInboundGroupSession *session2 =
olm_inbound_group_session(session_memory2);
olm_inbound_group_session(session_memory2.data());
res = olm_import_inbound_group_session(
session2, export_memory, sizeof(export_memory)
session2, export_memory.data(), export_memory.size()
);
assert_equals((size_t)0, res);
assert_equals(0, olm_inbound_group_session_is_verified(session2));
/* decrypt the message with the new session */
memcpy(msgcopy, message, msglen);
size = olm_group_decrypt_max_plaintext_length(session2, msgcopy, msglen);
uint8_t plaintext_buf2[size];
memcpy(msgcopy, message, msglen);
memcpy(msgcopy.data(), message, msglen);
size = olm_group_decrypt_max_plaintext_length(session2, msgcopy.data(), msglen);
std::vector<uint8_t> plaintext_buf2(size);
memcpy(msgcopy.data(), message, msglen);
res = olm_group_decrypt(
session2, msgcopy, msglen, plaintext_buf2, size, &message_index
session2, msgcopy.data(), msglen, plaintext_buf2.data(), size, &message_index
);
assert_equals((std::size_t)7, res);
assert_equals((const uint8_t *)"Message", plaintext_buf2, res);
assert_equals((const uint8_t *)"Message", plaintext_buf2.data(), res);
assert_equals(uint32_t(0), message_index);
assert_equals(1, olm_inbound_group_session_is_verified(session2));
}
@ -274,9 +272,9 @@ int main() {
/* build the inbound session */
size_t size = olm_inbound_group_session_size();
uint8_t inbound_session_memory[size];
std::vector<uint8_t> inbound_session_memory(size);
OlmInboundGroupSession *inbound_session =
olm_inbound_group_session(inbound_session_memory);
olm_inbound_group_session(inbound_session_memory.data());
size_t res = olm_init_inbound_group_session(
inbound_session, session_key, sizeof(session_key)-1
@ -287,36 +285,36 @@ int main() {
/* olm_group_decrypt_max_plaintext_length destroys the input so we have to
copy it. */
uint8_t msgcopy[msglen];
memcpy(msgcopy, message, msglen);
std::vector<uint8_t> msgcopy(msglen);
memcpy(msgcopy.data(), message, msglen);
size = olm_group_decrypt_max_plaintext_length(
inbound_session, msgcopy, msglen
inbound_session, msgcopy.data(), msglen
);
memcpy(msgcopy, message, msglen);
uint8_t plaintext_buf[size];
memcpy(msgcopy.data(), message, msglen);
std::vector<uint8_t> plaintext_buf(size);
uint32_t message_index;
res = olm_group_decrypt(
inbound_session, msgcopy, msglen, plaintext_buf, size, &message_index
inbound_session, msgcopy.data(), msglen, plaintext_buf.data(), size, &message_index
);
assert_equals(message_index, uint32_t(0));
assert_equals(plaintext_length, res);
assert_equals(plaintext, plaintext_buf, res);
assert_equals(plaintext, plaintext_buf.data(), res);
/* now twiddle the signature */
message[msglen-1] = 'E';
memcpy(msgcopy, message, msglen);
memcpy(msgcopy.data(), message, msglen);
assert_equals(
size,
olm_group_decrypt_max_plaintext_length(
inbound_session, msgcopy, msglen
inbound_session, msgcopy.data(), msglen
)
);
memcpy(msgcopy, message, msglen);
memcpy(msgcopy.data(), message, msglen);
res = olm_group_decrypt(
inbound_session, msgcopy, msglen,
plaintext_buf, size, &message_index
inbound_session, msgcopy.data(), msglen,
plaintext_buf.data(), size, &message_index
);
assert_equals((size_t)-1, res);
assert_equals(

View file

@ -49,7 +49,7 @@ TestCase test_case("Message encode test");
std::size_t length = olm::encode_message_length(1, 10, 10, 8);
assert_equals(std::size_t(35), length);
std::uint8_t output[length];
std::uint8_t output[35];
olm::MessageWriter writer;
olm::encode_message(writer, 3, 1, 10, 10, output);

View file

@ -4,6 +4,7 @@
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <vector>
struct MockRandom {
MockRandom(std::uint8_t tag, std::uint8_t offset = 0)
@ -36,35 +37,34 @@ TestCase test_case("Pickle account test");
MockRandom mock_random('P');
std::uint8_t account_buffer[::olm_account_size()];
::OlmAccount *account = ::olm_account(account_buffer);
std::uint8_t random[::olm_create_account_random_length(account)];
mock_random(random, sizeof(random));
::olm_create_account(account, random, sizeof(random));
std::uint8_t ot_random[::olm_account_generate_one_time_keys_random_length(
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(
account, 42
)];
mock_random(ot_random, sizeof(ot_random));
::olm_account_generate_one_time_keys(account, 42, ot_random, sizeof(ot_random));
));
mock_random(ot_random.data(), ot_random.size());
::olm_account_generate_one_time_keys(account, 42, ot_random.data(), ot_random.size());
std::size_t pickle_length = ::olm_pickle_account_length(account);
std::uint8_t pickle1[pickle_length];
std::size_t res = ::olm_pickle_account(account, "secret_key", 10, pickle1, pickle_length);
std::vector<std::uint8_t> pickle1(pickle_length);
std::size_t res = ::olm_pickle_account(account, "secret_key", 10, pickle1.data(), pickle_length);
assert_equals(pickle_length, res);
std::uint8_t pickle2[pickle_length];
std::memcpy(pickle2, pickle1, pickle_length);
std::vector<std::uint8_t> pickle2(pickle1);
std::uint8_t account_buffer2[::olm_account_size()];
::OlmAccount *account2 = ::olm_account(account_buffer2);
std::vector<std::uint8_t> account_buffer2(::olm_account_size());
::OlmAccount *account2 = ::olm_account(account_buffer2.data());
assert_not_equals(std::size_t(-1), ::olm_unpickle_account(
account2, "secret_key", 10, pickle2, pickle_length
account2, "secret_key", 10, pickle2.data(), pickle_length
));
assert_equals(pickle_length, ::olm_pickle_account_length(account2));
res = ::olm_pickle_account(account2, "secret_key", 10, pickle2, pickle_length);
res = ::olm_pickle_account(account2, "secret_key", 10, pickle2.data(), pickle_length);
assert_equals(pickle_length, res);
assert_equals(pickle1, pickle2, pickle_length);
assert_equals(pickle1.data(), pickle2.data(), pickle_length);
}
@ -79,8 +79,8 @@ assert_equals(pickle1, pickle2, pickle_length);
"K/A/8TOu9iK2hDFszy6xETiousHnHgh2ZGbRUh4pQx+YMm8ZdNZeRnwFGLnrWyf9"
"O5TmXua1FcU";
std::uint8_t account_buffer[::olm_account_size()];
::OlmAccount *account = ::olm_account(account_buffer);
std::vector<std::uint8_t> account_buffer(::olm_account_size());
::OlmAccount *account = ::olm_account(account_buffer.data());
assert_equals(
std::size_t(-1),
::olm_unpickle_account(
@ -99,47 +99,46 @@ assert_equals(pickle1, pickle2, pickle_length);
TestCase test_case("Pickle session test");
MockRandom mock_random('P');
std::uint8_t account_buffer[::olm_account_size()];
::OlmAccount *account = ::olm_account(account_buffer);
std::uint8_t random[::olm_create_account_random_length(account)];
mock_random(random, sizeof(random));
::olm_create_account(account, random, sizeof(random));
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::uint8_t session_buffer[::olm_session_size()];
::OlmSession *session = ::olm_session(session_buffer);
std::vector<std::uint8_t> session_buffer(::olm_session_size());
::OlmSession *session = ::olm_session(session_buffer.data());
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));
std::uint8_t random2[::olm_create_outbound_session_random_length(session)];
mock_random(random2, sizeof(random2));
std::vector<std::uint8_t> random2(::olm_create_outbound_session_random_length(session));
mock_random(random2.data(), random2.size());
::olm_create_outbound_session(
session, account,
identity_key, sizeof(identity_key),
one_time_key, sizeof(one_time_key),
random2, sizeof(random2)
random2.data(), random2.size()
);
std::size_t pickle_length = ::olm_pickle_session_length(session);
std::uint8_t pickle1[pickle_length];
std::size_t res = ::olm_pickle_session(session, "secret_key", 10, pickle1, pickle_length);
std::vector<std::uint8_t> pickle1(pickle_length);
std::size_t res = ::olm_pickle_session(session, "secret_key", 10, pickle1.data(), pickle_length);
assert_equals(pickle_length, res);
std::uint8_t pickle2[pickle_length];
std::memcpy(pickle2, pickle1, pickle_length);
std::vector<std::uint8_t> pickle2(pickle1);
std::uint8_t session_buffer2[::olm_session_size()];
::OlmSession *session2 = ::olm_session(session_buffer2);
std::vector<std::uint8_t> session_buffer2(::olm_session_size());
::OlmSession *session2 = ::olm_session(session_buffer2.data());
assert_not_equals(std::size_t(-1), ::olm_unpickle_session(
session2, "secret_key", 10, pickle2, pickle_length
session2, "secret_key", 10, pickle2.data(), pickle_length
));
assert_equals(pickle_length, ::olm_pickle_session_length(session2));
res = ::olm_pickle_session(session2, "secret_key", 10, pickle2, pickle_length);
res = ::olm_pickle_session(session2, "secret_key", 10, pickle2.data(), pickle_length);
assert_equals(pickle_length, res);
assert_equals(pickle1, pickle2, pickle_length);
assert_equals(pickle1.data(), pickle2.data(), pickle_length);
}
{ /** Loopback test */
@ -148,145 +147,143 @@ TestCase test_case("Loopback test");
MockRandom mock_random_a('A', 0x00);
MockRandom mock_random_b('B', 0x80);
std::uint8_t a_account_buffer[::olm_account_size()];
::OlmAccount *a_account = ::olm_account(a_account_buffer);
std::uint8_t a_random[::olm_create_account_random_length(a_account)];
mock_random_a(a_random, sizeof(a_random));
::olm_create_account(a_account, a_random, sizeof(a_random));
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::uint8_t b_account_buffer[::olm_account_size()];
::OlmAccount *b_account = ::olm_account(b_account_buffer);
std::uint8_t b_random[::olm_create_account_random_length(b_account)];
mock_random_b(b_random, sizeof(b_random));
::olm_create_account(b_account, b_random, sizeof(b_random));
std::uint8_t o_random[::olm_account_generate_one_time_keys_random_length(
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(
b_account, 42
)];
mock_random_b(o_random, sizeof(o_random));
::olm_account_generate_one_time_keys(b_account, 42, o_random, sizeof(o_random));
));
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::uint8_t a_id_keys[::olm_account_identity_keys_length(a_account)];
::olm_account_identity_keys(a_account, a_id_keys, sizeof(a_id_keys));
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::uint8_t b_id_keys[::olm_account_identity_keys_length(b_account)];
std::uint8_t b_ot_keys[::olm_account_one_time_keys_length(b_account)];
::olm_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys));
::olm_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys));
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::uint8_t a_session_buffer[::olm_session_size()];
::OlmSession *a_session = ::olm_session(a_session_buffer);
std::uint8_t a_rand[::olm_create_outbound_session_random_length(a_session)];
mock_random_a(a_rand, sizeof(a_rand));
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());
assert_not_equals(std::size_t(-1), ::olm_create_outbound_session(
a_session, a_account,
b_id_keys + 15, 43, // B's curve25519 identity key
b_ot_keys + 25, 43, // B's curve25519 one time key
a_rand, sizeof(a_rand)
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()
));
std::uint8_t plaintext[] = "Hello, World";
std::uint8_t message_1[::olm_encrypt_message_length(a_session, 12)];
std::uint8_t a_message_random[::olm_encrypt_random_length(a_session)];
mock_random_a(a_message_random, sizeof(a_message_random));
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());
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,
a_message_random, sizeof(a_message_random),
message_1, sizeof(message_1)
a_message_random.data(), a_message_random.size(),
message_1.data(), message_1.size()
));
std::uint8_t tmp_message_1[sizeof(message_1)];
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
std::uint8_t b_session_buffer[::olm_account_size()];
::OlmSession *b_session = ::olm_session(b_session_buffer);
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());
::olm_create_inbound_session(
b_session, b_account, tmp_message_1, sizeof(message_1)
b_session, b_account, tmp_message_1.data(), message_1.size()
);
// Check that the inbound session matches the message it was created from.
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
assert_equals(std::size_t(1), ::olm_matches_inbound_session(
b_session,
tmp_message_1, sizeof(message_1)
tmp_message_1.data(), message_1.size()
));
// Check that the inbound session matches the key this message is supposed
// to be from.
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
assert_equals(std::size_t(1), ::olm_matches_inbound_session_from(
b_session,
a_id_keys + 15, 43, // A's curve125519 identity key.
tmp_message_1, sizeof(message_1)
a_id_keys.data() + 15, 43, // A's curve125519 identity key.
tmp_message_1.data(), message_1.size()
));
// Check that the inbound session isn't from a different user.
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
std::memcpy(tmp_message_1.data(), message_1.data(), message_1.size());
assert_equals(std::size_t(0), ::olm_matches_inbound_session_from(
b_session,
b_id_keys + 15, 43, // B's curve25519 identity key.
tmp_message_1, sizeof(message_1)
b_id_keys.data() + 15, 43, // B's curve25519 identity key.
tmp_message_1.data(), message_1.size()
));
// Check that we can decrypt the message.
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
std::uint8_t plaintext_1[::olm_decrypt_max_plaintext_length(
b_session, 0, tmp_message_1, sizeof(message_1)
)];
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
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());
assert_equals(std::size_t(12), ::olm_decrypt(
b_session, 0,
tmp_message_1, sizeof(message_1),
plaintext_1, sizeof(plaintext_1)
tmp_message_1.data(), message_1.size(),
plaintext_1.data(), plaintext_1.size()
));
assert_equals(plaintext, plaintext_1, 12);
assert_equals(plaintext, plaintext_1.data(), 12);
std::uint8_t message_2[::olm_encrypt_message_length(b_session, 12)];
std::uint8_t b_message_random[::olm_encrypt_random_length(b_session)];
mock_random_b(b_message_random, sizeof(b_message_random));
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());
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,
b_message_random, sizeof(b_message_random),
message_2, sizeof(message_2)
b_message_random.data(), b_message_random.size(),
message_2.data(), message_2.size()
));
std::uint8_t tmp_message_2[sizeof(message_2)];
std::memcpy(tmp_message_2, message_2, sizeof(message_2));
std::uint8_t plaintext_2[::olm_decrypt_max_plaintext_length(
a_session, 1, tmp_message_2, sizeof(message_2)
)];
std::memcpy(tmp_message_2, message_2, sizeof(message_2));
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());
assert_equals(std::size_t(12), ::olm_decrypt(
a_session, 1,
tmp_message_2, sizeof(message_2),
plaintext_2, sizeof(plaintext_2)
tmp_message_2.data(), message_2.size(),
plaintext_2.data(), plaintext_2.size()
));
assert_equals(plaintext, plaintext_2, 12);
assert_equals(plaintext, plaintext_2.data(), 12);
std::memcpy(tmp_message_2, message_2, sizeof(message_2));
std::memcpy(tmp_message_2.data(), message_2.data(), message_2.size());
assert_equals(std::size_t(-1), ::olm_decrypt(
a_session, 1,
tmp_message_2, sizeof(message_2),
plaintext_2, sizeof(plaintext_2)
tmp_message_2.data(), message_2.size(),
plaintext_2.data(), plaintext_2.size()
));
std::uint8_t a_session_id[::olm_session_id_length(a_session)];
std::vector<std::uint8_t> a_session_id(::olm_session_id_length(a_session));
assert_not_equals(std::size_t(-1), ::olm_session_id(
a_session, a_session_id, sizeof(a_session_id)
a_session, a_session_id.data(), a_session_id.size()
));
std::uint8_t b_session_id[::olm_session_id_length(b_session)];
std::vector<std::uint8_t> b_session_id(::olm_session_id_length(b_session));
assert_not_equals(std::size_t(-1), ::olm_session_id(
b_session, b_session_id, sizeof(b_session_id)
b_session, b_session_id.data(), b_session_id.size()
));
assert_equals(sizeof(a_session_id), sizeof(b_session_id));
assert_equals(a_session_id, b_session_id, sizeof(b_session_id));
assert_equals(a_session_id.size(), b_session_id.size());
assert_equals(a_session_id.data(), b_session_id.data(), b_session_id.size());
}
@ -296,107 +293,104 @@ TestCase test_case("More messages test");
MockRandom mock_random_a('A', 0x00);
MockRandom mock_random_b('B', 0x80);
std::uint8_t a_account_buffer[::olm_account_size()];
::OlmAccount *a_account = ::olm_account(a_account_buffer);
std::uint8_t a_random[::olm_create_account_random_length(a_account)];
mock_random_a(a_random, sizeof(a_random));
::olm_create_account(a_account, a_random, sizeof(a_random));
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::uint8_t b_account_buffer[::olm_account_size()];
::OlmAccount *b_account = ::olm_account(b_account_buffer);
std::uint8_t b_random[::olm_create_account_random_length(b_account)];
mock_random_b(b_random, sizeof(b_random));
::olm_create_account(b_account, b_random, sizeof(b_random));
std::uint8_t o_random[::olm_account_generate_one_time_keys_random_length(
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(
b_account, 42
)];
mock_random_b(o_random, sizeof(o_random));
::olm_account_generate_one_time_keys(b_account, 42, o_random, sizeof(o_random));
));
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::uint8_t b_id_keys[::olm_account_identity_keys_length(b_account)];
std::uint8_t b_ot_keys[::olm_account_one_time_keys_length(b_account)];
::olm_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys));
::olm_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys));
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::uint8_t a_session_buffer[::olm_session_size()];
::OlmSession *a_session = ::olm_session(a_session_buffer);
std::uint8_t a_rand[::olm_create_outbound_session_random_length(a_session)];
mock_random_a(a_rand, sizeof(a_rand));
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());
assert_not_equals(std::size_t(-1), ::olm_create_outbound_session(
a_session, a_account,
b_id_keys + 15, 43,
b_ot_keys + 25, 43,
a_rand, sizeof(a_rand)
b_id_keys.data() + 15, 43,
b_ot_keys.data() + 25, 43,
a_rand.data(), a_rand.size()
));
std::uint8_t plaintext[] = "Hello, World";
std::uint8_t message_1[::olm_encrypt_message_length(a_session, 12)];
std::uint8_t a_message_random[::olm_encrypt_random_length(a_session)];
mock_random_a(a_message_random, sizeof(a_message_random));
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());
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,
a_message_random, sizeof(a_message_random),
message_1, sizeof(message_1)
a_message_random.data(), a_message_random.size(),
message_1.data(), message_1.size()
));
std::uint8_t tmp_message_1[sizeof(message_1)];
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
std::uint8_t b_session_buffer[::olm_account_size()];
::OlmSession *b_session = ::olm_session(b_session_buffer);
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());
::olm_create_inbound_session(
b_session, b_account, tmp_message_1, sizeof(message_1)
b_session, b_account, tmp_message_1.data(), message_1.size()
);
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
std::uint8_t plaintext_1[::olm_decrypt_max_plaintext_length(
b_session, 0, tmp_message_1, sizeof(message_1)
)];
std::memcpy(tmp_message_1, message_1, sizeof(message_1));
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());
assert_equals(std::size_t(12), ::olm_decrypt(
b_session, 0,
tmp_message_1, sizeof(message_1),
plaintext_1, sizeof(plaintext_1)
tmp_message_1.data(), message_1.size(),
plaintext_1.data(), plaintext_1.size()
));
for (unsigned i = 0; i < 8; ++i) {
{
std::uint8_t msg_a[::olm_encrypt_message_length(a_session, 12)];
std::uint8_t rnd_a[::olm_encrypt_random_length(a_session)];
mock_random_a(rnd_a, sizeof(rnd_a));
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());
std::size_t type_a = ::olm_encrypt_message_type(a_session);
assert_not_equals(std::size_t(-1), ::olm_encrypt(
a_session, plaintext, 12, rnd_a, sizeof(rnd_a), msg_a, sizeof(msg_a)
a_session, plaintext, 12, rnd_a.data(), rnd_a.size(), msg_a.data(), msg_a.size()
));
std::uint8_t tmp_a[sizeof(msg_a)];
std::memcpy(tmp_a, msg_a, sizeof(msg_a));
std::uint8_t out_a[::olm_decrypt_max_plaintext_length(
b_session, type_a, tmp_a, sizeof(tmp_a)
)];
std::memcpy(tmp_a, msg_a, sizeof(msg_a));
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));
assert_equals(std::size_t(12), ::olm_decrypt(
b_session, type_a, msg_a, sizeof(msg_a), out_a, sizeof(out_a)
b_session, type_a, msg_a.data(), msg_a.size(), out_a.data(), out_a.size()
));
}
{
std::uint8_t msg_b[::olm_encrypt_message_length(b_session, 12)];
std::uint8_t rnd_b[::olm_encrypt_random_length(b_session)];
mock_random_b(rnd_b, sizeof(rnd_b));
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());
std::size_t type_b = ::olm_encrypt_message_type(b_session);
assert_not_equals(std::size_t(-1), ::olm_encrypt(
b_session, plaintext, 12, rnd_b, sizeof(rnd_b), msg_b, sizeof(msg_b)
b_session, plaintext, 12, rnd_b.data(), rnd_b.size(), msg_b.data(), msg_b.size()
));
std::uint8_t tmp_b[sizeof(msg_b)];
std::memcpy(tmp_b, msg_b, sizeof(msg_b));
std::uint8_t out_b[::olm_decrypt_max_plaintext_length(
a_session, type_b, tmp_b, sizeof(tmp_b)
)];
std::memcpy(tmp_b, msg_b, sizeof(msg_b));
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());
assert_equals(std::size_t(12), ::olm_decrypt(
a_session, type_b, msg_b, sizeof(msg_b), out_b, sizeof(out_b)
a_session, type_b, msg_b.data(), msg_b.size(), out_b.data(), out_b.size()
));
}
}

View file

@ -1,6 +1,8 @@
#include "olm/olm.h"
#include "unittest.hh"
#include <vector>
struct test_case {
const char *msghex;
const char *expected_error;
@ -37,14 +39,14 @@ void decode_hex(
}
void decrypt_case(int message_type, const test_case * test_case) {
std::uint8_t session_memory[olm_session_size()];
::OlmSession * session = ::olm_session(session_memory);
std::vector<std::uint8_t> session_memory(olm_session_size());
::OlmSession * session = ::olm_session(session_memory.data());
std::uint8_t pickled[strlen(session_data)];
::memcpy(pickled, session_data, sizeof(pickled));
std::vector<std::uint8_t> pickled(strlen(session_data));
::memcpy(pickled.data(), session_data, pickled.size());
assert_not_equals(
::olm_error(),
::olm_unpickle_session(session, "", 0, pickled, sizeof(pickled))
::olm_unpickle_session(session, "", 0, pickled.data(), pickled.size())
);
std::size_t message_length = strlen(test_case->msghex) / 2;
@ -67,12 +69,12 @@ void decrypt_case(int message_type, const test_case * test_case) {
assert_not_equals(::olm_error(), max_length);
uint8_t plaintext[max_length];
std::vector<uint8_t> plaintext(max_length);
decode_hex(test_case->msghex, message, message_length);
olm_decrypt(
session, message_type,
message, message_length,
plaintext, max_length
plaintext.data(), max_length
);
free(message);
}

View file

@ -1,13 +1,15 @@
#include "olm/olm.h"
#include "unittest.hh"
#include <vector>
int main() {
{
TestCase("Olm sha256 test");
std::uint8_t utility_buffer[::olm_utility_size()];
::OlmUtility * utility = ::olm_utility(utility_buffer);
std::vector<std::uint8_t> utility_buffer(::olm_utility_size());
::OlmUtility * utility = ::olm_utility(utility_buffer.data());
assert_equals(std::size_t(43), ::olm_sha256_length(utility));
std::uint8_t output[43];

View file

@ -5,6 +5,7 @@
#include "unittest.hh"
#include <iostream>
#include <vector>
int main() {
@ -13,8 +14,8 @@ int main() {
TestCase test_case("Public Key Encryption/Decryption Test Case 1");
std::uint8_t decryption_buffer[olm_pk_decryption_size()];
OlmPkDecryption *decryption = olm_pk_decryption(decryption_buffer);
std::vector<std::uint8_t> decryption_buffer(olm_pk_decryption_size());
OlmPkDecryption *decryption = olm_pk_decryption(decryption_buffer.data());
std::uint8_t alice_private[32] = {
0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D,
@ -34,25 +35,25 @@ std::uint8_t bob_private[32] = {
const std::uint8_t *bob_public = (std::uint8_t *) "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08";
std::uint8_t pubkey[::olm_pk_key_length()];
std::vector<std::uint8_t> pubkey(::olm_pk_key_length());
olm_pk_key_from_private(
decryption,
pubkey, sizeof(pubkey),
pubkey.data(), pubkey.size(),
alice_private, sizeof(alice_private)
);
assert_equals(alice_public, pubkey, olm_pk_key_length());
assert_equals(alice_public, pubkey.data(), olm_pk_key_length());
uint8_t *alice_private_back_out = (uint8_t *)malloc(olm_pk_private_key_length());
olm_pk_get_private_key(decryption, alice_private_back_out, olm_pk_private_key_length());
assert_equals(alice_private, alice_private_back_out, olm_pk_private_key_length());
free(alice_private_back_out);
std::uint8_t encryption_buffer[olm_pk_encryption_size()];
OlmPkEncryption *encryption = olm_pk_encryption(encryption_buffer);
std::vector<std::uint8_t> encryption_buffer(olm_pk_encryption_size());
OlmPkEncryption *encryption = olm_pk_encryption(encryption_buffer.data());
olm_pk_encryption_set_recipient_key(encryption, pubkey, sizeof(pubkey));
olm_pk_encryption_set_recipient_key(encryption, pubkey.data(), pubkey.size());
const size_t plaintext_length = 14;
const std::uint8_t *plaintext = (std::uint8_t *) "This is a test";
@ -60,27 +61,27 @@ const std::uint8_t *plaintext = (std::uint8_t *) "This is a test";
size_t ciphertext_length = olm_pk_ciphertext_length(encryption, plaintext_length);
std::uint8_t *ciphertext_buffer = (std::uint8_t *) malloc(ciphertext_length);
std::uint8_t output_buffer[olm_pk_mac_length(encryption)];
std::uint8_t ephemeral_key[olm_pk_key_length()];
std::vector<std::uint8_t> output_buffer(olm_pk_mac_length(encryption));
std::vector<std::uint8_t> ephemeral_key(olm_pk_key_length());
olm_pk_encrypt(
encryption,
plaintext, plaintext_length,
ciphertext_buffer, ciphertext_length,
output_buffer, sizeof(output_buffer),
ephemeral_key, sizeof(ephemeral_key),
output_buffer.data(), output_buffer.size(),
ephemeral_key.data(), ephemeral_key.size(),
bob_private, sizeof(bob_private)
);
assert_equals(bob_public, ephemeral_key, olm_pk_key_length());
assert_equals(bob_public, ephemeral_key.data(), olm_pk_key_length());
size_t max_plaintext_length = olm_pk_max_plaintext_length(decryption, ciphertext_length);
std::uint8_t *plaintext_buffer = (std::uint8_t *) malloc(max_plaintext_length);
olm_pk_decrypt(
decryption,
ephemeral_key, sizeof(ephemeral_key),
output_buffer, sizeof(output_buffer),
ephemeral_key.data(), ephemeral_key.size(),
output_buffer.data(), output_buffer.size(),
ciphertext_buffer, ciphertext_length,
plaintext_buffer, max_plaintext_length
);
@ -96,8 +97,8 @@ free(plaintext_buffer);
TestCase test_case("Public Key Decryption pickling");
std::uint8_t decryption_buffer[olm_pk_decryption_size()];
OlmPkDecryption *decryption = olm_pk_decryption(decryption_buffer);
std::vector<std::uint8_t> decryption_buffer(olm_pk_decryption_size());
OlmPkDecryption *decryption = olm_pk_decryption(decryption_buffer.data());
std::uint8_t alice_private[32] = {
0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D,
@ -108,37 +109,37 @@ std::uint8_t alice_private[32] = {
const std::uint8_t *alice_public = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmoK";
std::uint8_t pubkey[olm_pk_key_length()];
std::vector<std::uint8_t> pubkey(olm_pk_key_length());
olm_pk_key_from_private(
decryption,
pubkey, sizeof(pubkey),
pubkey.data(), pubkey.size(),
alice_private, sizeof(alice_private)
);
const uint8_t *PICKLE_KEY=(uint8_t *)"secret_key";
std::uint8_t pickle_buffer[olm_pickle_pk_decryption_length(decryption)];
std::vector<std::uint8_t> pickle_buffer(olm_pickle_pk_decryption_length(decryption));
const uint8_t *expected_pickle = (uint8_t *) "qx37WTQrjZLz5tId/uBX9B3/okqAbV1ofl9UnHKno1eipByCpXleAAlAZoJgYnCDOQZDQWzo3luTSfkF9pU1mOILCbbouubs6TVeDyPfgGD9i86J8irHjA";
olm_pickle_pk_decryption(
decryption,
PICKLE_KEY, strlen((char *)PICKLE_KEY),
pickle_buffer, sizeof(pickle_buffer)
pickle_buffer.data(), pickle_buffer.size()
);
assert_equals(expected_pickle, pickle_buffer, olm_pickle_pk_decryption_length(decryption));
assert_equals(expected_pickle, pickle_buffer.data(), olm_pickle_pk_decryption_length(decryption));
olm_clear_pk_decryption(decryption);
memset(pubkey, 0, olm_pk_key_length());
memset(pubkey.data(), 0, olm_pk_key_length());
olm_unpickle_pk_decryption(
decryption,
PICKLE_KEY, strlen((char *)PICKLE_KEY),
pickle_buffer, sizeof(pickle_buffer),
pubkey, sizeof(pubkey)
pickle_buffer.data(), pickle_buffer.size(),
pubkey.data(), pubkey.size()
);
assert_equals(alice_public, pubkey, olm_pk_key_length());
assert_equals(alice_public, pubkey.data(), olm_pk_key_length());
char *ciphertext = strdup("ntk49j/KozVFtSqJXhCejg");
const char *mac = "zpzU6BkZcNI";
@ -168,8 +169,8 @@ free(plaintext_buffer);
TestCase test_case("Public Key Signing");
std::uint8_t signing_buffer[olm_pk_signing_size()];
OlmPkSigning *signing = olm_pk_signing(signing_buffer);
std::vector<std::uint8_t> signing_buffer(olm_pk_signing_size());
OlmPkSigning *signing = olm_pk_signing(signing_buffer.data());
std::uint8_t seed[32] = {
0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D,
@ -180,11 +181,11 @@ std::uint8_t seed[32] = {
//const std::uint8_t *pub_key = (std::uint8_t *) "hSDwCYkwp1R0i33ctD73Wg2/Og0mOBr066SpjqqbTmoK";
char pubkey[olm_pk_signing_public_key_length() + 1];
std::vector<char> pubkey(olm_pk_signing_public_key_length() + 1);
olm_pk_signing_key_from_seed(
signing,
pubkey, sizeof(pubkey),
pubkey.data(), pubkey.size() - 1,
seed, sizeof(seed)
);
@ -205,7 +206,7 @@ size_t result;
result = ::olm_ed25519_verify(
utility,
pubkey, olm_pk_signing_public_key_length(),
pubkey.data(), olm_pk_signing_public_key_length(),
message, strlen(message),
sig_buffer, olm_pk_signature_length()
);
@ -216,7 +217,7 @@ sig_buffer[5] = 'm';
result = ::olm_ed25519_verify(
utility,
pubkey, olm_pk_signing_public_key_length(),
pubkey.data(), olm_pk_signing_public_key_length(),
message, strlen(message),
sig_buffer, olm_pk_signature_length()
);

View file

@ -16,6 +16,7 @@
#include "olm/cipher.h"
#include "unittest.hh"
#include <vector>
int main() {
@ -57,23 +58,23 @@ std::size_t encrypt_length, decrypt_length;
random_length = alice.encrypt_random_length();
assert_equals(std::size_t(0), random_length);
std::uint8_t message[message_length];
std::vector<std::uint8_t> message(message_length);
encrypt_length = alice.encrypt(
plaintext, plaintext_length,
NULL, 0,
message, message_length
message.data(), message_length
);
assert_equals(message_length, encrypt_length);
output_length = bob.decrypt_max_plaintext_length(message, message_length);
std::uint8_t output[output_length];
output_length = bob.decrypt_max_plaintext_length(message.data(), message_length);
std::vector<std::uint8_t> output(output_length);
decrypt_length = bob.decrypt(
message, message_length,
output, output_length
message.data(), message_length,
output.data(), output_length
);
assert_equals(plaintext_length, decrypt_length);
assert_equals(plaintext, output, decrypt_length);
assert_equals(plaintext, output.data(), decrypt_length);
}
@ -83,24 +84,24 @@ std::size_t encrypt_length, decrypt_length;
random_length = bob.encrypt_random_length();
assert_equals(std::size_t(32), random_length);
std::uint8_t message[message_length];
std::vector<std::uint8_t> message(message_length);
std::uint8_t random[] = "This is a random 32 byte string.";
encrypt_length = bob.encrypt(
plaintext, plaintext_length,
random, 32,
message, message_length
message.data(), message_length
);
assert_equals(message_length, encrypt_length);
output_length = alice.decrypt_max_plaintext_length(message, message_length);
std::uint8_t output[output_length];
output_length = alice.decrypt_max_plaintext_length(message.data(), message_length);
std::vector<std::uint8_t> output(output_length);
decrypt_length = alice.decrypt(
message, message_length,
output, output_length
message.data(), message_length,
output.data(), output_length
);
assert_equals(plaintext_length, decrypt_length);
assert_equals(plaintext, output, decrypt_length);
assert_equals(plaintext, output.data(), decrypt_length);
}
} /* Send/receive message test case */
@ -130,12 +131,12 @@ std::size_t encrypt_length, decrypt_length;
random_length = alice.encrypt_random_length();
assert_equals(std::size_t(0), random_length);
std::uint8_t message_1[message_1_length];
std::vector<std::uint8_t> message_1(message_1_length);
std::uint8_t random[] = "This is a random 32 byte string.";
encrypt_length = alice.encrypt(
plaintext_1, plaintext_1_length,
random, 32,
message_1, message_1_length
message_1.data(), message_1_length
);
assert_equals(message_1_length, encrypt_length);
@ -143,36 +144,36 @@ std::size_t encrypt_length, decrypt_length;
random_length = alice.encrypt_random_length();
assert_equals(std::size_t(0), random_length);
std::uint8_t message_2[message_2_length];
std::vector<std::uint8_t> message_2(message_2_length);
encrypt_length = alice.encrypt(
plaintext_2, plaintext_2_length,
NULL, 0,
message_2, message_2_length
message_2.data(), message_2_length
);
assert_equals(message_2_length, encrypt_length);
output_length = bob.decrypt_max_plaintext_length(
message_2, message_2_length
message_2.data(), message_2_length
);
std::uint8_t output_1[output_length];
std::vector<std::uint8_t> output_1(output_length);
decrypt_length = bob.decrypt(
message_2, message_2_length,
output_1, output_length
message_2.data(), message_2_length,
output_1.data(), output_length
);
assert_equals(plaintext_2_length, decrypt_length);
assert_equals(plaintext_2, output_1, decrypt_length);
assert_equals(plaintext_2, output_1.data(), decrypt_length);
output_length = bob.decrypt_max_plaintext_length(
message_1, message_1_length
message_1.data(), message_1_length
);
std::uint8_t output_2[output_length];
std::vector<std::uint8_t> output_2(output_length);
decrypt_length = bob.decrypt(
message_1, message_1_length,
output_2, output_length
message_1.data(), message_1_length,
output_2.data(), output_length
);
assert_equals(plaintext_1_length, decrypt_length);
assert_equals(plaintext_1, output_2, decrypt_length);
assert_equals(plaintext_1, output_2.data(), decrypt_length);
}
} /* Out of order test case */
@ -193,24 +194,24 @@ std::uint8_t random[] = "This is a random 32 byte string";
for (unsigned i = 0; i < 8; ++i) {
{
std::uint8_t msg[alice.encrypt_output_length(sizeof(plaintext))];
std::vector<std::uint8_t> msg(alice.encrypt_output_length(sizeof(plaintext)));
alice.encrypt(
plaintext, 15, random, 32, msg, sizeof(msg)
plaintext, 15, random, 32, msg.data(), msg.size()
);
std::uint8_t output[bob.decrypt_max_plaintext_length(msg, sizeof(msg))];
std::vector<std::uint8_t> output(bob.decrypt_max_plaintext_length(msg.data(), msg.size()));
assert_equals(
std::size_t(15), bob.decrypt(msg, sizeof(msg), output, sizeof(output))
std::size_t(15), bob.decrypt(msg.data(), msg.size(), output.data(), output.size())
);
}
random[31]++;
{
std::uint8_t msg[bob.encrypt_output_length(sizeof(plaintext))];
std::vector<std::uint8_t> msg(bob.encrypt_output_length(sizeof(plaintext)));
bob.encrypt(
plaintext, 15, random, 32, msg, sizeof(msg)
plaintext, 15, random, 32, msg.data(), msg.size()
);
std::uint8_t output[alice.decrypt_max_plaintext_length(msg, sizeof(msg))];
std::vector<std::uint8_t> output(alice.decrypt_max_plaintext_length(msg.data(), msg.size()));
assert_equals(
std::size_t(15), alice.decrypt(msg, sizeof(msg), output, sizeof(output))
std::size_t(15), alice.decrypt(msg.data(), msg.size(), output.data(), output.size())
);
}
random[31]++;