libkazv
|
#include <crypto.hpp>
Public Types | |
using | UserIdToDeviceIdMap = immer::map< std::string, immer::flex_vector< std::string > > |
Public Member Functions | |
Crypto () | |
Construct an invalid Crypto. More... | |
Crypto (RandomTag, RandomData data) | |
Constructs a Crypto using user-provided random data. More... | |
Crypto (const Crypto &that) | |
Crypto (Crypto &&that) | |
Crypto & | operator= (const Crypto &that) |
Crypto & | operator= (Crypto &&that) |
~Crypto () | |
bool | operator== (const Crypto &that) const |
bool | valid () const |
std::string | ed25519IdentityKey () const |
std::string | curve25519IdentityKey () const |
std::string | sign (nlohmann::json j) |
void | setUploadedOneTimeKeysCount (immer::map< std::string, int > uploadedOneTimeKeysCount) |
int | uploadedOneTimeKeysCount (std::string algorithm) const |
std::size_t | maxNumberOfOneTimeKeys () const |
void | genOneTimeKeysWithRandom (RandomData random, int num) |
Generate num one-time keys with user-provided random data. More... | |
nlohmann::json | unpublishedOneTimeKeys () const |
According to olm.h, this returns an object like. More... | |
int | numUnpublishedOneTimeKeys () const |
void | markOneTimeKeysAsPublished () |
MaybeString | decrypt (nlohmann::json eventJson) |
Returns decrypted message if we can decrypt it otherwise returns the error. More... | |
std::size_t | encryptOlmRandomSize (std::string theirCurve25519IdentityKey) const |
nlohmann::json | encryptOlmWithRandom (RandomData random, nlohmann::json eventJson, std::string theirCurve25519IdentityKey) |
Encrypt eventJson with olm, for the recipient identified with theirCurve25519IdentityKey . More... | |
nlohmann::json | encryptMegOlm (nlohmann::json eventJson) |
returns the content template with everything but deviceId eventJson should contain type, room_id and content More... | |
bool | createInboundGroupSession (KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key) |
Create or update an inbound group session. More... | |
bool | hasInboundGroupSession (KeyOfGroupSession k) const |
std::string | outboundGroupSessionInitialKey (std::string roomId) |
std::string | outboundGroupSessionCurrentKey (std::string roomId) |
bool | verify (nlohmann::json object, std::string userId, std::string deviceId, std::string ed25519Key) |
Check whether the signature of userId/deviceId is valid in object. More... | |
MaybeString | getInboundGroupSessionEd25519KeyFromEvent (const nlohmann::json &eventJson) const |
std::string | rotateMegOlmSessionWithRandom (RandomData random, Timestamp timeMs, std::string roomId) |
Rotate the megolm session using user-provided random data. More... | |
std::optional< std::string > | rotateMegOlmSessionWithRandomIfNeeded (RandomData random, Timestamp timeMs, std::string roomId, MegOlmSessionRotateDesc desc) |
Rotate the megolm session using user-provided random data, if we need to rotate it. More... | |
UserIdToDeviceIdMap | devicesMissingOutboundSessionKey (immer::map< std::string, immer::map< std::string, std::string >> keyMap) const |
void | createOutboundSessionWithRandom (RandomData random, std::string theirIdentityKey, std::string theirOneTimeKey) |
Create an outbound session using user-provided random data. More... | |
template<class Archive > | |
void | save (Archive &ar, const unsigned int) const |
template<class Archive > | |
void | load (Archive &ar, const unsigned int) |
nlohmann::json | toJson () const |
void | loadJson (const nlohmann::json &j) |
Static Public Member Functions | |
static std::size_t | constructRandomSize () |
static std::size_t | genOneTimeKeysRandomSize (int num) |
Get the size of random data needed to generate num one-time keys. More... | |
static std::size_t | encryptOlmMaxRandomSize () |
static std::size_t | rotateMegOlmSessionRandomSize () |
static std::size_t | createOutboundSessionRandomSize () |
Friends | |
class | Session |
struct | SessionPrivate |
using Kazv::Crypto::UserIdToDeviceIdMap = immer::map<std::string, immer::flex_vector<std::string> > |
|
explicit |
Construct an invalid Crypto.
Kazv::Crypto::Crypto | ( | RandomTag | , |
RandomData | data | ||
) |
Constructs a Crypto using user-provided random data.
data | Random data of size at least constructRandomSize() . |
Kazv::Crypto::Crypto | ( | const Crypto & | that | ) |
Kazv::Crypto::Crypto | ( | Crypto && | that | ) |
|
default |
|
static |
bool Kazv::Crypto::createInboundGroupSession | ( | KeyOfGroupSession | k, |
std::string | sessionKey, | ||
std::string | ed25519Key | ||
) |
Create or update an inbound group session.
k | The key of the group session. |
sessionKey | The session key. |
ed25519Key | The ed25519 identity key of the sender of the m.room_key event. |
|
static |
createOutboundSessionWithRandom()
. void Kazv::Crypto::createOutboundSessionWithRandom | ( | RandomData | random, |
std::string | theirIdentityKey, | ||
std::string | theirOneTimeKey | ||
) |
Create an outbound session using user-provided random data.
random | The random data to use. It must be at least of size createOutboundSessionRandomSize() . |
theirIdeneityKey | The identity key of the recipient. |
theirOneTimeKey | The one-time key of the recipient. |
std::string Kazv::Crypto::curve25519IdentityKey | ( | ) | const |
MaybeString Kazv::Crypto::decrypt | ( | nlohmann::json | eventJson | ) |
Returns decrypted message if we can decrypt it otherwise returns the error.
auto Kazv::Crypto::devicesMissingOutboundSessionKey | ( | immer::map< std::string, immer::map< std::string, std::string >> | keyMap | ) | const |
std::string Kazv::Crypto::ed25519IdentityKey | ( | ) | const |
nlohmann::json Kazv::Crypto::encryptMegOlm | ( | nlohmann::json | eventJson | ) |
returns the content template with everything but deviceId eventJson should contain type, room_id and content
|
static |
theirCurve25519IdentityKey
. std::size_t Kazv::Crypto::encryptOlmRandomSize | ( | std::string | theirCurve25519IdentityKey | ) | const |
theirCurve25519IdentityKey
. nlohmann::json Kazv::Crypto::encryptOlmWithRandom | ( | RandomData | random, |
nlohmann::json | eventJson, | ||
std::string | theirCurve25519IdentityKey | ||
) |
Encrypt eventJson
with olm, for the recipient identified with theirCurve25519IdentityKey
.
random | The random data to use for encryption. Must be of at least size encryptOlmRandomSize(theirCurve25519IdentityKey) . |
eventJson | The event json to encrypt. |
theirCurve25519IdentityKey | The curve25519 identity key of the recipient. |
|
static |
Get the size of random data needed to generate num
one-time keys.
num | The number of one-time keys to generate. |
num
one-time keys. void Kazv::Crypto::genOneTimeKeysWithRandom | ( | RandomData | random, |
int | num | ||
) |
Generate num
one-time keys with user-provided random data.
random | The random data. Must be of at least size genOneTimeKeysRandomSize(num) . |
num | The number of one-time keys to generate. |
MaybeString Kazv::Crypto::getInboundGroupSessionEd25519KeyFromEvent | ( | const nlohmann::json & | eventJson | ) | const |
bool Kazv::Crypto::hasInboundGroupSession | ( | KeyOfGroupSession | k | ) | const |
|
inline |
void Kazv::Crypto::loadJson | ( | const nlohmann::json & | j | ) |
void Kazv::Crypto::markOneTimeKeysAsPublished | ( | ) |
std::size_t Kazv::Crypto::maxNumberOfOneTimeKeys | ( | ) | const |
int Kazv::Crypto::numUnpublishedOneTimeKeys | ( | ) | const |
bool Kazv::Crypto::operator== | ( | const Crypto & | that | ) | const |
std::string Kazv::Crypto::outboundGroupSessionCurrentKey | ( | std::string | roomId | ) |
std::string Kazv::Crypto::outboundGroupSessionInitialKey | ( | std::string | roomId | ) |
|
static |
rotateMegOlmSessionWithRandom()
and rotateMegOlmSessionWithRandomIfNeeded()
. std::string Kazv::Crypto::rotateMegOlmSessionWithRandom | ( | RandomData | random, |
Timestamp | timeMs, | ||
std::string | roomId | ||
) |
Rotate the megolm session using user-provided random data.
random | The random data. Must be of at least size rotateMegOlmSessionRandomSize() . |
timeMs | The creation time of the new megolm session. |
roomId | The room id of the megolm session to rotate. |
std::optional< std::string > Kazv::Crypto::rotateMegOlmSessionWithRandomIfNeeded | ( | RandomData | random, |
Timestamp | timeMs, | ||
std::string | roomId, | ||
MegOlmSessionRotateDesc | desc | ||
) |
Rotate the megolm session using user-provided random data, if we need to rotate it.
The session will be rotated if and only if
timeMs
and the creation time of the megolm session has reached the time limit in desc
, OR;desc
.random | The random data. Must be of at least size rotateMegOlmSessionRandomSize() . |
timeMs | The timestamp to judge whether the session has reached its time limit. If the megolm session is rotated, this will also be the creation time of the new megolm session. |
roomId | The room id of the megolm session to rotate. |
desc | The rotation specification of this room. |
std::nullopt
otherwise.
|
inline |
void Kazv::Crypto::setUploadedOneTimeKeysCount | ( | immer::map< std::string, int > | uploadedOneTimeKeysCount | ) |
std::string Kazv::Crypto::sign | ( | nlohmann::json | j | ) |
nlohmann::json Kazv::Crypto::toJson | ( | ) | const |
nlohmann::json Kazv::Crypto::unpublishedOneTimeKeys | ( | ) | const |
According to olm.h, this returns an object like.
{ curve25519: { "AAAAAA": "wo76WcYtb0Vk/pBOdmduiGJ0wIEjW4IBMbbQn7aSnTo", "AAAAAB": "LRvjo46L1X2vx69sS9QNFD29HWulxrmW11Up5AfAjgU" } }
int Kazv::Crypto::uploadedOneTimeKeysCount | ( | std::string | algorithm | ) | const |
bool Kazv::Crypto::valid | ( | ) | const |
bool Kazv::Crypto::verify | ( | nlohmann::json | object, |
std::string | userId, | ||
std::string | deviceId, | ||
std::string | ed25519Key | ||
) |
Check whether the signature of userId/deviceId is valid in object.
|
friend |
|
friend |