libkazv
Kazv::Crypto Class Reference

#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)
 
Cryptooperator= (const Crypto &that)
 
Cryptooperator= (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)
 
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
 
class SessionPrivate
 

Member Typedef Documentation

◆ UserIdToDeviceIdMap

using Kazv::Crypto::UserIdToDeviceIdMap = immer::map<std::string, immer::flex_vector<std::string> >

Constructor & Destructor Documentation

◆ Crypto() [1/4]

Kazv::Crypto::Crypto ( )
explicit

Construct an invalid Crypto.

◆ Crypto() [2/4]

Kazv::Crypto::Crypto ( RandomTag  ,
RandomData  data 
)

Constructs a Crypto using user-provided random data.

Parameters
dataRandom data of size at least constructRandomSize().

◆ Crypto() [3/4]

Kazv::Crypto::Crypto ( const Crypto that)

◆ Crypto() [4/4]

Kazv::Crypto::Crypto ( Crypto &&  that)

◆ ~Crypto()

Kazv::Crypto::~Crypto ( )
default

Member Function Documentation

◆ constructRandomSize()

std::size_t Kazv::Crypto::constructRandomSize ( )
static
Returns
The size of random data needed to construct a Crypto.

◆ createInboundGroupSession()

bool Kazv::Crypto::createInboundGroupSession ( KeyOfGroupSession  k,
std::string  sessionKey,
std::string  ed25519Key 
)

◆ createOutboundSessionRandomSize()

std::size_t Kazv::Crypto::createOutboundSessionRandomSize ( )
static
Returns
The size of random data needed for createOutboundSessionWithRandom().

◆ createOutboundSessionWithRandom()

void Kazv::Crypto::createOutboundSessionWithRandom ( RandomData  random,
std::string  theirIdentityKey,
std::string  theirOneTimeKey 
)

Create an outbound session using user-provided random data.

Parameters
randomThe random data to use. It must be at least of size createOutboundSessionRandomSize().
theirIdeneityKeyThe identity key of the recipient.
theirOneTimeKeyThe one-time key of the recipient.

◆ curve25519IdentityKey()

std::string Kazv::Crypto::curve25519IdentityKey ( ) const

◆ decrypt()

MaybeString Kazv::Crypto::decrypt ( nlohmann::json  eventJson)

Returns decrypted message if we can decrypt it otherwise returns the error.

◆ devicesMissingOutboundSessionKey()

auto Kazv::Crypto::devicesMissingOutboundSessionKey ( immer::map< std::string, immer::map< std::string, std::string >>  keyMap) const

◆ ed25519IdentityKey()

std::string Kazv::Crypto::ed25519IdentityKey ( ) const

◆ encryptMegOlm()

nlohmann::json Kazv::Crypto::encryptMegOlm ( nlohmann::json  eventJson)

returns the content template with everything but deviceId eventJson should contain type, room_id and content

◆ encryptOlmMaxRandomSize()

std::size_t Kazv::Crypto::encryptOlmMaxRandomSize ( )
static
Returns
The maximum size of random data needed to encrypt a message for the session identified with theirCurve25519IdentityKey.

◆ encryptOlmRandomSize()

std::size_t Kazv::Crypto::encryptOlmRandomSize ( std::string  theirCurve25519IdentityKey) const
Returns
The size of random data needed to encrypt a message for the session identified with theirCurve25519IdentityKey.

◆ encryptOlmWithRandom()

nlohmann::json Kazv::Crypto::encryptOlmWithRandom ( RandomData  random,
nlohmann::json  eventJson,
std::string  theirCurve25519IdentityKey 
)

Encrypt eventJson with olm, for the recipient identified with theirCurve25519IdentityKey.

Parameters
randomThe random data to use for encryption. Must be of at least size encryptOlmRandomSize(theirCurve25519IdentityKey).
eventJsonThe event json to encrypt.
theirCurve25519IdentityKeyThe curve25519 identity key of the recipient.
Returns
A json object that looks like
{
"<their identity key>": {
"type": <number>,
"body": "<body>"
}
}

◆ genOneTimeKeysRandomSize()

std::size_t Kazv::Crypto::genOneTimeKeysRandomSize ( int  num)
static

Get the size of random data needed to generate num one-time keys.

Parameters
numThe number of one-time keys to generate.
Returns
The size of random data needed to generate num one-time keys.

◆ genOneTimeKeysWithRandom()

void Kazv::Crypto::genOneTimeKeysWithRandom ( RandomData  random,
int  num 
)

Generate num one-time keys with user-provided random data.

Parameters
randomThe random data. Must be of at least size genOneTimeKeysRandomSize(num).
numThe number of one-time keys to generate.

◆ getInboundGroupSessionEd25519KeyFromEvent()

MaybeString Kazv::Crypto::getInboundGroupSessionEd25519KeyFromEvent ( const nlohmann::json eventJson) const

◆ load()

template<class Archive >
void Kazv::Crypto::load ( Archive &  ar,
const unsigned int   
)
inline

◆ loadJson()

void Kazv::Crypto::loadJson ( const nlohmann::json j)

◆ markOneTimeKeysAsPublished()

void Kazv::Crypto::markOneTimeKeysAsPublished ( )

◆ maxNumberOfOneTimeKeys()

std::size_t Kazv::Crypto::maxNumberOfOneTimeKeys ( ) const

◆ numUnpublishedOneTimeKeys()

int Kazv::Crypto::numUnpublishedOneTimeKeys ( ) const

◆ operator=() [1/2]

Crypto & Kazv::Crypto::operator= ( const Crypto that)

◆ operator=() [2/2]

Crypto & Kazv::Crypto::operator= ( Crypto &&  that)

◆ operator==()

bool Kazv::Crypto::operator== ( const Crypto that) const

◆ outboundGroupSessionCurrentKey()

std::string Kazv::Crypto::outboundGroupSessionCurrentKey ( std::string  roomId)

◆ outboundGroupSessionInitialKey()

std::string Kazv::Crypto::outboundGroupSessionInitialKey ( std::string  roomId)

◆ rotateMegOlmSessionRandomSize()

std::size_t Kazv::Crypto::rotateMegOlmSessionRandomSize ( )
static
Returns
The size of random data needed for rotateMegOlmSessionWithRandom() and rotateMegOlmSessionWithRandomIfNeeded().

◆ rotateMegOlmSessionWithRandom()

std::string Kazv::Crypto::rotateMegOlmSessionWithRandom ( RandomData  random,
Timestamp  timeMs,
std::string  roomId 
)

Rotate the megolm session using user-provided random data.

Parameters
randomThe random data. Must be of at least size rotateMegOlmSessionRandomSize().
timeMsThe creation time of the new megolm session.
roomIdThe room id of the megolm session to rotate.
Returns
The new session key.

◆ rotateMegOlmSessionWithRandomIfNeeded()

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

  • The difference between timeMs and the creation time of the megolm session has reached the time limit in desc, OR;
  • The number of messages this megolm session has encrypted has reached the message limit in desc.
Parameters
randomThe random data. Must be of at least size rotateMegOlmSessionRandomSize().
timeMsThe 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.
roomIdThe room id of the megolm session to rotate.
descThe rotation specification of this room.
Returns
The session key if the session is rotated, std::nullopt otherwise.

◆ save()

template<class Archive >
void Kazv::Crypto::save ( Archive &  ar,
const unsigned int   
) const
inline

◆ setUploadedOneTimeKeysCount()

void Kazv::Crypto::setUploadedOneTimeKeysCount ( immer::map< std::string, int >  uploadedOneTimeKeysCount)

◆ sign()

std::string Kazv::Crypto::sign ( nlohmann::json  j)

◆ toJson()

nlohmann::json Kazv::Crypto::toJson ( ) const

◆ unpublishedOneTimeKeys()

nlohmann::json Kazv::Crypto::unpublishedOneTimeKeys ( ) const

According to olm.h, this returns an object like.

{ curve25519: { "AAAAAA": "wo76WcYtb0Vk/pBOdmduiGJ0wIEjW4IBMbbQn7aSnTo", "AAAAAB": "LRvjo46L1X2vx69sS9QNFD29HWulxrmW11Up5AfAjgU" } }

◆ uploadedOneTimeKeysCount()

int Kazv::Crypto::uploadedOneTimeKeysCount ( std::string  algorithm) const

◆ valid()

bool Kazv::Crypto::valid ( ) const
Returns
whether this Crypto is valid.

◆ verify()

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.

Friends And Related Function Documentation

◆ Session

friend class Session
friend

◆ SessionPrivate

friend class SessionPrivate
friend

The documentation for this class was generated from the following files: