Page MenuHomePhorge

D353.1787343790.diff
No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None

D353.1787343790.diff

diff --git a/src/crypto/base64.hpp b/src/crypto/base64.hpp
--- a/src/crypto/base64.hpp
+++ b/src/crypto/base64.hpp
@@ -6,6 +6,7 @@
#pragma once
#include <libkazv-config.hpp>
+#include <string>
namespace Kazv
{
diff --git a/src/crypto/crypto.hpp b/src/crypto/crypto.hpp
--- a/src/crypto/crypto.hpp
+++ b/src/crypto/crypto.hpp
@@ -144,7 +144,8 @@
* "body": "<body>"
* }
* }
- * ```
+ * ``` if the encryption is successful.
+ * Otherwise, return an empty json object.
*/
nlohmann::json encryptOlmWithRandom(
RandomData random, nlohmann::json eventJson, std::string theirCurve25519IdentityKey);
diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp
--- a/src/crypto/crypto.cpp
+++ b/src/crypto/crypto.cpp
@@ -285,7 +285,7 @@
auto str = j.dump();
auto signature = checkVodozemacError([&]() {
- return m_d->account.value()->sign(str);
+ return m_d->account.value()->sign(rust::Slice<const std::uint8_t>(reinterpret_cast<const std::uint8_t *>(str.data()), str.size()));
});
if (!signature.has_value()) {
@@ -458,7 +458,7 @@
auto res = checkVodozemacError([&]() {
auto key = vodozemac::types::ed25519_key_from_base64(ed25519Key);
auto sig = vodozemac::types::ed25519_signature_from_base64(signature);
- key->verify(message, *sig);
+ key->verify(rust::Slice<const std::uint8_t>(reinterpret_cast<const std::uint8_t *>(message.data()), message.size()), *sig);
// It throws if the signature cannot be verified
return true;
});
diff --git a/src/crypto/session.hpp b/src/crypto/session.hpp
--- a/src/crypto/session.hpp
+++ b/src/crypto/session.hpp
@@ -76,7 +76,8 @@
* of at least size `encryptRandomSize()`.
* @param plainText The plain text to encrypt.
*
- * @return A pair containing the type and encrypted message string.
+ * @return A pair containing the type and encrypted message string if
+ * the encryption is successful. Otherwise, return {-1, ""}.
*/
std::pair<int /* type */, std::string /* message */> encryptWithRandom(
RandomData random, std::string plainText);
diff --git a/src/crypto/session.cpp b/src/crypto/session.cpp
--- a/src/crypto/session.cpp
+++ b/src/crypto/session.cpp
@@ -10,10 +10,31 @@
#include "crypto-p.hpp"
#include "session-p.hpp"
-
+#include "base64.hpp"
namespace Kazv
{
+ static rust::Vec<std::uint8_t> decodeBase64ToVec(std::string encoded)
+ {
+ auto decodedMsg = decodeBase64(encoded);
+ auto decodedMsgVec = rust::Vec<std::uint8_t>();
+ decodedMsgVec.reserve(decodedMsg.size());
+ std::transform(
+ decodedMsg.begin(), decodedMsg.end(),
+ std::back_inserter(decodedMsgVec),
+ [](char v) {
+ return static_cast<std::uint8_t>(v);
+ }
+ );
+ return decodedMsgVec;
+ }
+
+ static const vodozemac::olm::SessionConfig &sessionConfigV1()
+ {
+ static rust::Box<vodozemac::olm::SessionConfig> v1 = vodozemac::olm::new_session_config_version_1();
+ return *v1;
+ }
+
SessionPrivate::SessionPrivate()
: session(std::nullopt)
{
@@ -33,6 +54,7 @@
auto identityKey = vodozemac::types::curve_key_from_base64(rust::Str(theirIdentityKey));
auto oneTimeKey = vodozemac::types::curve_key_from_base64(rust::Str(theirOneTimeKey));
return cryptoD.account.value()->create_outbound_session(
+ sessionConfigV1(),
*identityKey,
*oneTimeKey
);
@@ -51,9 +73,10 @@
auto identityKey = vodozemac::types::curve_key_from_base64(rust::Str(theirIdentityKey));
auto msg = vodozemac::olm::olm_message_from_parts(vodozemac::olm::OlmMessageParts{
0, // pre-key message
- message,
+ decodeBase64ToVec(message),
});
return cryptoD.account.value()->create_inbound_session(
+ sessionConfigV1(),
*identityKey,
*msg
);
@@ -180,7 +203,7 @@
auto res = checkVodozemacError([&]() {
auto msg = vodozemac::olm::olm_message_from_parts(vodozemac::olm::OlmMessageParts{
0, // pre-key
- message,
+ decodeBase64ToVec(message),
});
return m_d->session.value()->session_matches(*msg);
});
@@ -199,7 +222,7 @@
auto res = checkVodozemacError([&]() {
auto msg = vodozemac::olm::olm_message_from_parts(vodozemac::olm::OlmMessageParts{
static_cast<std::size_t>(type),
- message,
+ decodeBase64ToVec(message),
});
return m_d->session.value()->decrypt(*msg);
});
@@ -229,8 +252,9 @@
}
auto [type, msg] = res.value()->to_parts();
+ auto base64Msg = encodeBase64(std::string(msg.begin(), msg.end()));
- return { type, static_cast<std::string>(msg) };
+ return { type, std::move(base64Msg) };
}
void to_json(nlohmann::json &j, const Session &s)

File Metadata

Mime Type
text/plain
Expires
Fri, Aug 21, 1:23 PM (3 h, 52 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1732601
Default Alt Text
D353.1787343790.diff (5 KB)

Event Timeline