diff --git a/src/crypto.cpp b/src/crypto.cpp index 351d9d034e0aff5cadadc1eac1822c958abe3da4..8ed346e2d0189e4294f9b6866a97f12478817325 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -9,11 +9,11 @@ using namespace mtx::client::crypto; constexpr std::size_t SIGNATURE_SIZE = 64; -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> mtx::client::crypto::create_buffer(std::size_t nbytes) { - auto buf = std::make_unique<uint8_t[]>(nbytes); - randombytes_buf(buf.get(), nbytes); + auto buf = std::make_unique<BinaryBuf>(nbytes); + randombytes_buf(buf->data(), buf->size()); return buf; } @@ -26,7 +26,7 @@ mtx::client::crypto::olm_new_account() const auto nbytes = olm_account->new_account_random_length(); auto buf = create_buffer(nbytes); - int result = olm_account->new_account(buf.get(), nbytes); + int result = olm_account->new_account(buf->data(), buf->size()); if (result == -1) throw olm_exception("olm_new_account", olm_account->last_error); @@ -40,12 +40,12 @@ mtx::client::crypto::identity_keys(std::shared_ptr<olm::Account> account) const auto nbytes = account->get_identity_json_length(); auto buf = create_buffer(nbytes); - int result = account->get_identity_json(buf.get(), nbytes); + int result = account->get_identity_json(buf->data(), buf->size()); if (result == -1) throw olm_exception("identity_keys", account->last_error); - std::string data(buf.get(), buf.get() + nbytes); + std::string data(buf->begin(), buf->end()); IdentityKeys keys = json::parse(data); return keys; @@ -66,7 +66,7 @@ mtx::client::crypto::sign_identity_keys(std::shared_ptr<olm::Account> account, {"ed25519:" + device_id, keys.ed25519}, }}}; - return encode_base64(sign_message(account, body.dump()).get(), SIGNATURE_SIZE); + return encode_base64(sign_message(account, body.dump())->data(), SIGNATURE_SIZE); } std::size_t @@ -76,7 +76,7 @@ mtx::client::crypto::generate_one_time_keys(std::shared_ptr<olm::Account> accoun const auto nbytes = account->generate_one_time_keys_random_length(number_of_keys); auto buf = create_buffer(nbytes); - return account->generate_one_time_keys(number_of_keys, buf.get(), nbytes); + return account->generate_one_time_keys(number_of_keys, buf->data(), buf->size()); } json @@ -85,12 +85,12 @@ mtx::client::crypto::one_time_keys(std::shared_ptr<olm::Account> account) const auto nbytes = account->get_one_time_keys_json_length(); auto buf = create_buffer(nbytes); - int result = account->get_one_time_keys_json(buf.get(), nbytes); + int result = account->get_one_time_keys_json(buf->data(), buf->size()); if (result == -1) throw olm_exception("one_time_keys", account->last_error); - std::string data(buf.get(), buf.get() + nbytes); + std::string data(buf->begin(), buf->end()); return json::parse(data); } @@ -104,7 +104,7 @@ mtx::client::crypto::sign_one_time_key(std::shared_ptr<olm::Account> account, auto signature_buf = sign_message(account, j.dump()); - return encode_base64(signature_buf.get(), SIGNATURE_SIZE); + return encode_base64(signature_buf->data(), signature_buf->size()); } std::map<std::string, json> @@ -125,16 +125,15 @@ mtx::client::crypto::sign_one_time_keys(std::shared_ptr<olm::Account> account, return signed_one_time_keys; } -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> mtx::client::crypto::sign_message(std::shared_ptr<olm::Account> account, const std::string &msg) { // Message buffer - auto buf = str_to_buffer(msg); - std::size_t nbytes = msg.size(); + auto buf = str_to_buffer(msg); // Signature buffer auto signature_buf = create_buffer(SIGNATURE_SIZE); - account->sign(buf.get(), nbytes, signature_buf.get(), SIGNATURE_SIZE); + account->sign(buf->data(), buf->size(), signature_buf->data(), signature_buf->size()); return signature_buf; } @@ -149,19 +148,19 @@ mtx::client::crypto::signed_one_time_key_json(const mtx::identifiers::User &user {"signatures", {{user_id.to_string(), {{"ed25519:" + device_id, signature}}}}}}; } -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> mtx::client::crypto::str_to_buffer(const std::string &data) { auto str_pointer = reinterpret_cast<const uint8_t *>(&data[0]); const auto nbytes = data.size(); auto buf = create_buffer(nbytes); - memcpy(buf.get(), str_pointer, nbytes); + memcpy(buf->data(), str_pointer, buf->size()); return buf; } -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> mtx::client::crypto::decode_base64(const std::string &data) { const auto nbytes = data.size(); @@ -173,7 +172,7 @@ mtx::client::crypto::decode_base64(const std::string &data) auto output_buf = create_buffer(output_nbytes); auto input_buf = str_to_buffer(data); - olm::decode_base64(input_buf.get(), nbytes, output_buf.get()); + olm::decode_base64(input_buf->data(), nbytes, output_buf->data()); return output_buf; } @@ -187,12 +186,12 @@ mtx::client::crypto::encode_base64(const uint8_t *data, std::size_t len) throw std::runtime_error("invalid base64 input length"); auto output_buf = create_buffer(output_nbytes); - olm::encode_base64(data, len, output_buf.get()); + olm::encode_base64(data, len, output_buf->data()); - return std::string(output_buf.get(), output_buf.get() + output_nbytes); + return std::string(output_buf->begin(), output_buf->end()); } -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> mtx::client::crypto::json_to_buffer(const nlohmann::json &obj) { return str_to_buffer(obj.dump()); diff --git a/src/crypto.hpp b/src/crypto.hpp index ff6dbf0a6270b4104b16ef17df184159c733c225..ca81d12937e16a35f40f298d8a202e2b40e6d249 100644 --- a/src/crypto.hpp +++ b/src/crypto.hpp @@ -8,13 +8,16 @@ #include <olm/account.hh> #include <olm/error.h> -static constexpr const char *ED25519 = "ed25519"; -static constexpr const char *CURVE25519 = "curve25519"; - namespace mtx { namespace client { namespace crypto { +static constexpr const char *ED25519 = "ed25519"; +static constexpr const char *CURVE25519 = "curve25519"; + +//! Data representation used to interact with libolm. +using BinaryBuf = std::vector<uint8_t>; + struct IdentityKeys { std::string curve25519; @@ -90,7 +93,7 @@ nlohmann::json one_time_keys(std::shared_ptr<olm::Account> user); //! Create a uint8_t buffer which is initialized with random bytes. -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> create_buffer(std::size_t nbytes); //! Sign the given one time keys and encode it to base64. @@ -105,7 +108,7 @@ sign_identity_keys(std::shared_ptr<olm::Account> account, const std::string &device_id); //! Sign the given message. -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> sign_message(std::shared_ptr<olm::Account> account, const std::string &msg); //! Generate the json structure for the signed one time key. @@ -126,15 +129,15 @@ std::string encode_base64(const uint8_t *data, std::size_t len); //! Decode the given base64 string -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> decode_base64(const std::string &data); //! Convert the given string to an uint8_t buffer. -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> str_to_buffer(const std::string &data); //! Convert the given json struct to an uint8_t buffer. -std::unique_ptr<uint8_t[]> +std::unique_ptr<BinaryBuf> json_to_buffer(const nlohmann::json &obj); } // namespace crypto diff --git a/tests/utils.cpp b/tests/utils.cpp index 5e19bae70a4033f0f30e210d1fb53d35c68ad370..ac6774345e64fe86a2ae97d86254104396ec0f12 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -15,13 +15,7 @@ TEST(Utilities, JsonToBuffer) auto msg = json({{"key", "text"}}); auto buf = json_to_buffer(msg); - auto strjson = msg.dump(); - auto len = msg.dump().size(); - - for (uint8_t i = 0; i < len; i++) { - if (strjson[i] != buf[i]) - FAIL(); - } + EXPECT_EQ(std::string(buf->begin(), buf->end()), msg.dump()); } TEST(Utilities, VerifySignedOneTimeKey) @@ -39,9 +33,9 @@ TEST(Utilities, VerifySignedOneTimeKey) olm::Utility utillity; auto res = utillity.ed25519_verify(alice->identity_keys.ed25519_key.public_key, - str_to_buffer(msg).get(), + str_to_buffer(msg)->data(), msg.size(), - sig_buf.get(), + sig_buf->data(), SIGNATURE_SIZE); EXPECT_EQ(utillity.last_error, 0); @@ -67,9 +61,9 @@ TEST(Utilities, VerifySignedIdentityKeys) olm::Utility utillity; auto res = utillity.ed25519_verify(alice->identity_keys.ed25519_key.public_key, - str_to_buffer(msg).get(), + str_to_buffer(msg)->data(), msg.size(), - sig_buf.get(), + sig_buf->data(), SIGNATURE_SIZE); EXPECT_EQ(utillity.last_error, 0);