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);
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 
165  bool createInboundGroupSession(KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key);
166 
168 
169  std::string outboundGroupSessionInitialKey(std::string roomId);
170 
171  std::string outboundGroupSessionCurrentKey(std::string roomId);
172 
174  bool verify(nlohmann::json object, std::string userId, std::string deviceId, std::string ed25519Key);
175 
177 
182  static std::size_t rotateMegOlmSessionRandomSize();
183 
194  std::string rotateMegOlmSessionWithRandom(RandomData random, Timestamp timeMs, std::string roomId);
195 
217  std::optional<std::string> rotateMegOlmSessionWithRandomIfNeeded(
218  RandomData random,
219  Timestamp timeMs,
220  std::string roomId,
222 
223  using UserIdToDeviceIdMap = immer::map<std::string, immer::flex_vector<std::string>>;
225  immer::map<std::string, immer::map<std::string /* deviceId */,
226  std::string /* curve25519IdentityKey */>> keyMap) const;
227 
231  static std::size_t createOutboundSessionRandomSize();
232 
242  RandomData random,
243  std::string theirIdentityKey,
244  std::string theirOneTimeKey);
245 
246  template<class Archive>
247  void save(Archive & ar, const unsigned int /* version */) const {
248  ar << toJson().dump();
249  }
250 
251  template<class Archive>
252  void load(Archive &ar, const unsigned int /* version */) {
253  std::string j;
254  ar >> j;
255  loadJson(nlohmann::json::parse(std::move(j)));
256  }
257 
258  BOOST_SERIALIZATION_SPLIT_MEMBER()
259 
260  nlohmann::json toJson() const;
261  void loadJson(const nlohmann::json &j);
262 
263  private:
264 
265  friend class Session;
266  friend struct SessionPrivate;
267  std::unique_ptr<CryptoPrivate> m_d;
268  };
269 }
270 
271 BOOST_CLASS_VERSION(Kazv::Crypto, 0)
Definition: crypto.hpp:36
void load(Archive &ar, const unsigned int)
Definition: crypto.hpp:252
nlohmann::json encryptMegOlm(nlohmann::json eventJson)
returns the content template with everything but deviceId eventJson should contain type,...
Definition: crypto.cpp:491
bool createInboundGroupSession(KeyOfGroupSession k, std::string sessionKey, std::string ed25519Key)
Create or update an inbound group session.
Definition: crypto.cpp:366
static std::size_t encryptOlmMaxRandomSize()
Definition: crypto.cpp:466
std::string curve25519IdentityKey() const
Definition: crypto.cpp:275
static std::size_t rotateMegOlmSessionRandomSize()
Definition: crypto.cpp:519
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:400
Crypto & operator=(const Crypto &that)
Definition: crypto.cpp:236
std::string outboundGroupSessionInitialKey(std::string roomId)
Definition: crypto.cpp:540
nlohmann::json toJson() const
Definition: crypto.cpp:601
static std::size_t createOutboundSessionRandomSize()
Definition: crypto.cpp:577
static std::size_t genOneTimeKeysRandomSize(int num)
Get the size of random data needed to generate num one-time keys.
Definition: crypto.cpp:308
int uploadedOneTimeKeysCount(std::string algorithm) const
Definition: crypto.cpp:349
bool hasInboundGroupSession(KeyOfGroupSession k) const
Definition: crypto.cpp:371
void genOneTimeKeysWithRandom(RandomData random, int num)
Generate num one-time keys with user-provided random data.
Definition: crypto.cpp:313
std::string ed25519IdentityKey() const
Definition: crypto.cpp:270
MaybeString getInboundGroupSessionEd25519KeyFromEvent(const nlohmann::json &eventJson) const
Definition: crypto.cpp:427
void setUploadedOneTimeKeysCount(immer::map< std::string, int > uploadedOneTimeKeysCount)
Definition: crypto.cpp:298
void createOutboundSessionWithRandom(RandomData random, std::string theirIdentityKey, std::string theirOneTimeKey)
Create an outbound session using user-provided random data.
Definition: crypto.cpp:582
void markOneTimeKeysAsPublished()
Definition: crypto.cpp:338
void loadJson(const nlohmann::json &j)
Definition: crypto.cpp:618
nlohmann::json unpublishedOneTimeKeys() const
According to olm.h, this returns an object like.
Definition: crypto.cpp:322
std::size_t maxNumberOfOneTimeKeys() const
Definition: crypto.cpp:303
immer::map< std::string, immer::flex_vector< std::string > > UserIdToDeviceIdMap
Definition: crypto.hpp:223
UserIdToDeviceIdMap devicesMissingOutboundSessionKey(immer::map< std::string, immer::map< std::string, std::string >> keyMap) const
Definition: crypto.cpp:552
bool valid() const
Definition: crypto.cpp:253
std::string sign(nlohmann::json j)
Definition: crypto.cpp:280
std::string outboundGroupSessionCurrentKey(std::string roomId)
Definition: crypto.cpp:546
std::string rotateMegOlmSessionWithRandom(RandomData random, Timestamp timeMs, std::string roomId)
Rotate the megolm session using user-provided random data.
Definition: crypto.cpp:524
MaybeString decrypt(nlohmann::json eventJson)
Returns decrypted message if we can decrypt it otherwise returns the error.
Definition: crypto.cpp:354
int numUnpublishedOneTimeKeys() const
Definition: crypto.cpp:344
nlohmann::json encryptOlmWithRandom(RandomData random, nlohmann::json eventJson, std::string theirCurve25519IdentityKey)
Encrypt eventJson with olm, for the recipient identified with theirCurve25519IdentityKey.
Definition: crypto.cpp:471
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:532
Crypto()
Construct an invalid Crypto.
Definition: crypto.cpp:214
void save(Archive &ar, const unsigned int) const
Definition: crypto.hpp:247
std::size_t encryptOlmRandomSize(std::string theirCurve25519IdentityKey) const
Definition: crypto.cpp:444
bool operator==(const Crypto &that) const
Definition: crypto.cpp:248
static std::size_t constructRandomSize()
Definition: crypto.cpp:209
Definition: maybe.hpp:25
Definition: session.hpp:27
Definition: location.hpp:10
std::string RandomData
Definition: crypto-util.hpp:34
nlohmann::json json
Definition: jsonwrap.hpp:20
std::int_fast64_t Timestamp
Definition: event.hpp:18
Definition: location.hpp:27
Definition: clientutil.hpp:217
Definition: crypto-p.hpp:26
Definition: crypto-util.hpp:24
Definition: crypto.hpp:27
int messages
The message limit of the megolm session.
Definition: crypto.hpp:31
Timestamp ms
The time limit of the megolm session, in milliseconds.
Definition: crypto.hpp:29
The tag to indicate that a constructor should use user-provided random data.
Definition: crypto-util.hpp:32
Definition: session-p.hpp:16