libkazv
crypto.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of libkazv.
3  * SPDX-FileCopyrightText: 2020-2024 tusooa <tusooa@kazv.moe>
4  * SPDX-License-Identifier: AGPL-3.0-or-later
5  */
6 
7 #pragma once
8 #include <libkazv-config.hpp>
9 
10 #include <memory>
11 
12 #include <nlohmann/json.hpp>
13 
14 #include <immer/map.hpp>
15 #include <immer/flex_vector.hpp>
16 
17 #include <maybe.hpp>
18 
19 #include "crypto-util.hpp"
20 #include "time-util.hpp"
21 
22 namespace Kazv
23 {
24  class Session;
25 
27  {
31  int messages{};
32  };
33 
34  struct CryptoPrivate;
35  class Crypto
36  {
37  public:
41  explicit Crypto();
42 
46  static std::size_t constructRandomSize();
47 
54 
55  Crypto(const Crypto &that);
56  Crypto(Crypto &&that);
57  Crypto &operator=(const Crypto &that);
58  Crypto &operator=(Crypto &&that);
59  ~Crypto();
60 
61  bool operator==(const Crypto &that) const;
62 
66  bool valid() const;
67 
68  std::string ed25519IdentityKey() const;
69  std::string curve25519IdentityKey() const;
70 
71  std::string sign(nlohmann::json j);
72 
73  void setUploadedOneTimeKeysCount(immer::map<std::string /* algorithm */, int> uploadedOneTimeKeysCount);
74 
75  int uploadedOneTimeKeysCount(std::string algorithm) const;
76 
77  std::size_t maxNumberOfOneTimeKeys() const;
78 
88  static std::size_t genOneTimeKeysRandomSize(int num);
89 
97  void genOneTimeKeysWithRandom(RandomData random, int num);
98 
110 
111  int numUnpublishedOneTimeKeys() const;
112 
114 
118 
123  std::size_t encryptOlmRandomSize(std::string theirCurve25519IdentityKey) const;
124 
129  static std::size_t encryptOlmMaxRandomSize();
130 
150  RandomData random, nlohmann::json eventJson, std::string theirCurve25519IdentityKey);
151 
155 
156  bool createInboundGroupSession(KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key);
157 
159 
160  std::string outboundGroupSessionInitialKey(std::string roomId);
161 
162  std::string outboundGroupSessionCurrentKey(std::string roomId);
163 
165  bool verify(nlohmann::json object, std::string userId, std::string deviceId, std::string ed25519Key);
166 
168 
173  static std::size_t rotateMegOlmSessionRandomSize();
174 
185  std::string rotateMegOlmSessionWithRandom(RandomData random, Timestamp timeMs, std::string roomId);
186 
208  std::optional<std::string> rotateMegOlmSessionWithRandomIfNeeded(
209  RandomData random,
210  Timestamp timeMs,
211  std::string roomId,
213 
214  using UserIdToDeviceIdMap = immer::map<std::string, immer::flex_vector<std::string>>;
216  immer::map<std::string, immer::map<std::string /* deviceId */,
217  std::string /* curve25519IdentityKey */>> keyMap) const;
218 
222  static std::size_t createOutboundSessionRandomSize();
223 
233  RandomData random,
234  std::string theirIdentityKey,
235  std::string theirOneTimeKey);
236 
237  template<class Archive>
238  void save(Archive & ar, const unsigned int /* version */) const {
239  ar << toJson().dump();
240  }
241 
242  template<class Archive>
243  void load(Archive &ar, const unsigned int /* version */) {
244  std::string j;
245  ar >> j;
246  loadJson(nlohmann::json::parse(std::move(j)));
247  }
248 
249  BOOST_SERIALIZATION_SPLIT_MEMBER()
250 
251  nlohmann::json toJson() const;
252  void loadJson(const nlohmann::json &j);
253 
254  private:
255 
256  friend class Session;
257  friend class SessionPrivate;
258  std::unique_ptr<CryptoPrivate> m_d;
259  };
260 }
261 
262 BOOST_CLASS_VERSION(Kazv::Crypto, 0)
maybe.hpp
Kazv::Crypto::outboundGroupSessionCurrentKey
std::string outboundGroupSessionCurrentKey(std::string roomId)
Definition: crypto.cpp:566
Kazv::Crypto::devicesMissingOutboundSessionKey
UserIdToDeviceIdMap devicesMissingOutboundSessionKey(immer::map< std::string, immer::map< std::string, std::string >> keyMap) const
Definition: crypto.cpp:572
Kazv::Crypto::rotateMegOlmSessionWithRandomIfNeeded
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.
Definition: crypto.cpp:552
Kazv::Session
Definition: session.hpp:27
Kazv::Crypto::~Crypto
~Crypto()
Kazv::RandomData
std::string RandomData
Definition: crypto-util.hpp:34
Kazv::Crypto::uploadedOneTimeKeysCount
int uploadedOneTimeKeysCount(std::string algorithm) const
Definition: crypto.cpp:384
Kazv::Crypto::valid
bool valid() const
Definition: crypto.cpp:269
crypto-util.hpp
Kazv::Crypto::createOutboundSessionRandomSize
static std::size_t createOutboundSessionRandomSize()
Definition: crypto.cpp:597
Kazv::Crypto::getInboundGroupSessionEd25519KeyFromEvent
MaybeString getInboundGroupSessionEd25519KeyFromEvent(const nlohmann::json &eventJson) const
Definition: crypto.cpp:446
nlohmann
Definition: location.hpp:26
Kazv::Crypto::curve25519IdentityKey
std::string curve25519IdentityKey() const
Definition: crypto.cpp:302
Kazv::Crypto::hasInboundGroupSession
bool hasInboundGroupSession(KeyOfGroupSession k) const
Definition: crypto.cpp:406
Kazv::Crypto::genOneTimeKeysRandomSize
static std::size_t genOneTimeKeysRandomSize(int num)
Get the size of random data needed to generate num one-time keys.
Definition: crypto.cpp:335
Kazv::MegOlmSessionRotateDesc::ms
Timestamp ms
The time limit of the megolm session, in milliseconds.
Definition: crypto.hpp:29
Kazv::Maybe
Definition: maybe.hpp:24
Kazv
Definition: location.hpp:10
Kazv::Crypto::encryptOlmMaxRandomSize
static std::size_t encryptOlmMaxRandomSize()
Definition: crypto.cpp:485
Kazv::Timestamp
std::int_fast64_t Timestamp
Definition: event.hpp:18
Kazv::MegOlmSessionRotateDesc
Definition: crypto.hpp:26
Kazv::Crypto::genOneTimeKeysWithRandom
void genOneTimeKeysWithRandom(RandomData random, int num)
Generate num one-time keys with user-provided random data.
Definition: crypto.cpp:347
Kazv::Crypto::rotateMegOlmSessionRandomSize
static std::size_t rotateMegOlmSessionRandomSize()
Definition: crypto.cpp:539
Kazv::SessionPrivate
Definition: session-p.hpp:14
Kazv::KeyOfGroupSession
Definition: crypto-util.hpp:23
Kazv::Crypto::operator=
Crypto & operator=(const Crypto &that)
Definition: crypto.cpp:252
Kazv::Crypto::outboundGroupSessionInitialKey
std::string outboundGroupSessionInitialKey(std::string roomId)
Definition: crypto.cpp:560
Kazv::Crypto::verify
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.
Definition: crypto.cpp:421
Kazv::Crypto::markOneTimeKeysAsPublished
void markOneTimeKeysAsPublished()
Definition: crypto.cpp:371
Kazv::Crypto::ed25519IdentityKey
std::string ed25519IdentityKey() const
Definition: crypto.cpp:297
Kazv::Crypto::Crypto
Crypto()
Construct an invalid Crypto.
Definition: crypto.cpp:230
Kazv::Crypto::encryptOlmRandomSize
std::size_t encryptOlmRandomSize(std::string theirCurve25519IdentityKey) const
Definition: crypto.cpp:463
Kazv::Crypto::constructRandomSize
static std::size_t constructRandomSize()
Definition: crypto.cpp:219
Kazv::json
nlohmann::json json
Definition: jsonwrap.hpp:20
Kazv::Crypto::maxNumberOfOneTimeKeys
std::size_t maxNumberOfOneTimeKeys() const
Definition: crypto.cpp:330
Kazv::CryptoPrivate
Definition: crypto-p.hpp:24
Kazv::Crypto::sign
std::string sign(nlohmann::json j)
Definition: crypto.cpp:307
Kazv::Crypto::createInboundGroupSession
bool createInboundGroupSession(KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key)
Definition: crypto.cpp:401
Kazv::Crypto::UserIdToDeviceIdMap
immer::map< std::string, immer::flex_vector< std::string > > UserIdToDeviceIdMap
Definition: crypto.hpp:214
Kazv::Crypto::save
void save(Archive &ar, const unsigned int) const
Definition: crypto.hpp:238
Kazv::Crypto
Definition: crypto.hpp:35
Kazv::MegOlmSessionRotateDesc::messages
int messages
The message limit of the megolm session.
Definition: crypto.hpp:31
Kazv::Crypto::encryptOlmWithRandom
nlohmann::json encryptOlmWithRandom(RandomData random, nlohmann::json eventJson, std::string theirCurve25519IdentityKey)
Encrypt eventJson with olm, for the recipient identified with theirCurve25519IdentityKey.
Definition: crypto.cpp:491
Kazv::Crypto::operator==
bool operator==(const Crypto &that) const
Definition: crypto.cpp:264
std
Definition: clientutil.hpp:216
Kazv::Crypto::loadJson
void loadJson(const nlohmann::json &j)
Definition: crypto.cpp:637
Kazv::Crypto::encryptMegOlm
nlohmann::json encryptMegOlm(nlohmann::json eventJson)
returns the content template with everything but deviceId eventJson should contain type,...
Definition: crypto.cpp:511
Kazv::Crypto::setUploadedOneTimeKeysCount
void setUploadedOneTimeKeysCount(immer::map< std::string, int > uploadedOneTimeKeysCount)
Definition: crypto.cpp:325
Kazv::Crypto::rotateMegOlmSessionWithRandom
std::string rotateMegOlmSessionWithRandom(RandomData random, Timestamp timeMs, std::string roomId)
Rotate the megolm session using user-provided random data.
Definition: crypto.cpp:544
Kazv::Crypto::load
void load(Archive &ar, const unsigned int)
Definition: crypto.hpp:243
Kazv::Crypto::unpublishedOneTimeKeys
nlohmann::json unpublishedOneTimeKeys() const
According to olm.h, this returns an object like.
Definition: crypto.cpp:363
Kazv::Crypto::createOutboundSessionWithRandom
void createOutboundSessionWithRandom(RandomData random, std::string theirIdentityKey, std::string theirOneTimeKey)
Create an outbound session using user-provided random data.
Definition: crypto.cpp:602
libkazv-config.hpp
Kazv::Crypto::toJson
nlohmann::json toJson() const
Definition: crypto.cpp:621
Kazv::Crypto::numUnpublishedOneTimeKeys
int numUnpublishedOneTimeKeys() const
Definition: crypto.cpp:379
Kazv::RandomTag
The tag to indicate that a constructor should use user-provided random data.
Definition: crypto-util.hpp:32
Kazv::Crypto::decrypt
MaybeString decrypt(nlohmann::json eventJson)
Returns decrypted message if we can decrypt it otherwise returns the error.
Definition: crypto.cpp:389
time-util.hpp