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 
158  std::string outboundGroupSessionInitialKey(std::string roomId);
159 
160  std::string outboundGroupSessionCurrentKey(std::string roomId);
161 
163  bool verify(nlohmann::json object, std::string userId, std::string deviceId, std::string ed25519Key);
164 
166 
171  static std::size_t rotateMegOlmSessionRandomSize();
172 
183  std::string rotateMegOlmSessionWithRandom(RandomData random, Timestamp timeMs, std::string roomId);
184 
206  std::optional<std::string> rotateMegOlmSessionWithRandomIfNeeded(
207  RandomData random,
208  Timestamp timeMs,
209  std::string roomId,
211 
212  using UserIdToDeviceIdMap = immer::map<std::string, immer::flex_vector<std::string>>;
214  immer::map<std::string, immer::map<std::string /* deviceId */,
215  std::string /* curve25519IdentityKey */>> keyMap) const;
216 
220  static std::size_t createOutboundSessionRandomSize();
221 
231  RandomData random,
232  std::string theirIdentityKey,
233  std::string theirOneTimeKey);
234 
235  template<class Archive>
236  void save(Archive & ar, const unsigned int /* version */) const {
237  ar << toJson().dump();
238  }
239 
240  template<class Archive>
241  void load(Archive &ar, const unsigned int /* version */) {
242  std::string j;
243  ar >> j;
244  loadJson(nlohmann::json::parse(std::move(j)));
245  }
246 
247  BOOST_SERIALIZATION_SPLIT_MEMBER()
248 
249  nlohmann::json toJson() const;
250  void loadJson(const nlohmann::json &j);
251 
252  private:
253 
254  friend class Session;
255  friend class SessionPrivate;
256  std::unique_ptr<CryptoPrivate> m_d;
257  };
258 }
259 
260 BOOST_CLASS_VERSION(Kazv::Crypto, 0)
maybe.hpp
Kazv::Crypto::outboundGroupSessionCurrentKey
std::string outboundGroupSessionCurrentKey(std::string roomId)
Definition: crypto.cpp:562
Kazv::Crypto::devicesMissingOutboundSessionKey
UserIdToDeviceIdMap devicesMissingOutboundSessionKey(immer::map< std::string, immer::map< std::string, std::string >> keyMap) const
Definition: crypto.cpp:568
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:548
Kazv::Session
Definition: session.hpp:27
Kazv::Crypto::~Crypto
~Crypto()
Kazv::RandomData
std::string RandomData
Definition: crypto-util.hpp:35
Kazv::Crypto::uploadedOneTimeKeysCount
int uploadedOneTimeKeysCount(std::string algorithm) const
Definition: crypto.cpp:386
Kazv::Crypto::valid
bool valid() const
Definition: crypto.cpp:271
crypto-util.hpp
Kazv::Crypto::createOutboundSessionRandomSize
static std::size_t createOutboundSessionRandomSize()
Definition: crypto.cpp:593
Kazv::Crypto::getInboundGroupSessionEd25519KeyFromEvent
MaybeString getInboundGroupSessionEd25519KeyFromEvent(const nlohmann::json &eventJson) const
Definition: crypto.cpp:443
nlohmann
Definition: location.hpp:26
Kazv::Crypto::curve25519IdentityKey
std::string curve25519IdentityKey() const
Definition: crypto.cpp:304
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:337
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:483
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:349
Kazv::Crypto::rotateMegOlmSessionRandomSize
static std::size_t rotateMegOlmSessionRandomSize()
Definition: crypto.cpp:535
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:254
Kazv::Crypto::outboundGroupSessionInitialKey
std::string outboundGroupSessionInitialKey(std::string roomId)
Definition: crypto.cpp:556
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:418
Kazv::Crypto::markOneTimeKeysAsPublished
void markOneTimeKeysAsPublished()
Definition: crypto.cpp:373
Kazv::Crypto::ed25519IdentityKey
std::string ed25519IdentityKey() const
Definition: crypto.cpp:299
Kazv::Crypto::Crypto
Crypto()
Construct an invalid Crypto.
Definition: crypto.cpp:232
Kazv::Crypto::encryptOlmRandomSize
std::size_t encryptOlmRandomSize(std::string theirCurve25519IdentityKey) const
Definition: crypto.cpp:461
Kazv::Crypto::constructRandomSize
static std::size_t constructRandomSize()
Definition: crypto.cpp:221
Kazv::json
nlohmann::json json
Definition: jsonwrap.hpp:20
Kazv::Crypto::maxNumberOfOneTimeKeys
std::size_t maxNumberOfOneTimeKeys() const
Definition: crypto.cpp:332
Kazv::CryptoPrivate
Definition: crypto-p.hpp:24
Kazv::Crypto::sign
std::string sign(nlohmann::json j)
Definition: crypto.cpp:309
Kazv::Crypto::createInboundGroupSession
bool createInboundGroupSession(KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key)
Definition: crypto.cpp:403
Kazv::Crypto::UserIdToDeviceIdMap
immer::map< std::string, immer::flex_vector< std::string > > UserIdToDeviceIdMap
Definition: crypto.hpp:212
Kazv::Crypto::save
void save(Archive &ar, const unsigned int) const
Definition: crypto.hpp:236
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:489
Kazv::Crypto::operator==
bool operator==(const Crypto &that) const
Definition: crypto.cpp:266
std
Definition: clientutil.hpp:216
Kazv::Crypto::loadJson
void loadJson(const nlohmann::json &j)
Definition: crypto.cpp:633
Kazv::Crypto::encryptMegOlm
nlohmann::json encryptMegOlm(nlohmann::json eventJson)
returns the content template with everything but deviceId eventJson should contain type,...
Definition: crypto.cpp:509
Kazv::Crypto::setUploadedOneTimeKeysCount
void setUploadedOneTimeKeysCount(immer::map< std::string, int > uploadedOneTimeKeysCount)
Definition: crypto.cpp:327
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:540
Kazv::Crypto::load
void load(Archive &ar, const unsigned int)
Definition: crypto.hpp:241
Kazv::Crypto::unpublishedOneTimeKeys
nlohmann::json unpublishedOneTimeKeys() const
According to olm.h, this returns an object like.
Definition: crypto.cpp:365
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:598
libkazv-config.hpp
Kazv::Crypto::toJson
nlohmann::json toJson() const
Definition: crypto.cpp:617
Kazv::Crypto::numUnpublishedOneTimeKeys
int numUnpublishedOneTimeKeys() const
Definition: crypto.cpp:381
Kazv::RandomTag
The tag to indicate that a constructor should use user-provided random data.
Definition: crypto-util.hpp:33
Kazv::Crypto::decrypt
MaybeString decrypt(nlohmann::json eventJson)
Returns decrypted message if we can decrypt it otherwise returns the error.
Definition: crypto.cpp:391
time-util.hpp