libkazv
Loading...
Searching...
No Matches
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
22namespace Kazv
23{
24 class Session;
25
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
177 std::size_t importInboundGroupSessions(const nlohmann::json &keys);
178
180
181 std::string outboundGroupSessionInitialKey(std::string roomId);
182
183 std::string outboundGroupSessionCurrentKey(std::string roomId);
184
186 bool verify(nlohmann::json object, std::string userId, std::string deviceId, std::string ed25519Key);
187
189
194 static std::size_t rotateMegOlmSessionRandomSize();
195
206 std::string rotateMegOlmSessionWithRandom(RandomData random, Timestamp timeMs, std::string roomId);
207
229 std::optional<std::string> rotateMegOlmSessionWithRandomIfNeeded(
230 RandomData random,
231 Timestamp timeMs,
232 std::string roomId,
234
235 using UserIdToDeviceIdMap = immer::map<std::string, immer::flex_vector<std::string>>;
237 immer::map<std::string, immer::map<std::string /* deviceId */,
238 std::string /* curve25519IdentityKey */>> keyMap) const;
239
243 static std::size_t createOutboundSessionRandomSize();
244
254 RandomData random,
255 std::string theirIdentityKey,
256 std::string theirOneTimeKey);
257
258 template<class Archive>
259 void save(Archive & ar, const unsigned int /* version */) const {
260 ar << toJson().dump();
261 }
262
263 template<class Archive>
264 void load(Archive &ar, const unsigned int /* version */) {
265 std::string j;
266 ar >> j;
267 loadJson(nlohmann::json::parse(std::move(j)));
268 }
269
270 BOOST_SERIALIZATION_SPLIT_MEMBER()
271
272 nlohmann::json toJson() const;
273 void loadJson(const nlohmann::json &j);
274
275 private:
276
277 friend class Session;
278 friend struct SessionPrivate;
279 std::unique_ptr<CryptoPrivate> m_d;
280 };
281}
282
283BOOST_CLASS_VERSION(Kazv::Crypto, 0)
Definition crypto.hpp:36
void load(Archive &ar, const unsigned int)
Definition crypto.hpp:264
nlohmann::json encryptMegOlm(nlohmann::json eventJson)
returns the content template with everything but deviceId eventJson should contain type,...
Definition crypto.cpp:529
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:504
std::string curve25519IdentityKey() const
Definition crypto.cpp:275
static std::size_t rotateMegOlmSessionRandomSize()
Definition crypto.cpp:557
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:438
Crypto & operator=(const Crypto &that)
Definition crypto.cpp:236
std::string outboundGroupSessionInitialKey(std::string roomId)
Definition crypto.cpp:578
nlohmann::json toJson() const
Definition crypto.cpp:639
static std::size_t createOutboundSessionRandomSize()
Definition crypto.cpp:615
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:409
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:465
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:620
UserIdToDeviceIdMap devicesMissingOutboundSessionKey(immer::map< std::string, immer::map< std::string, std::string > > keyMap) const
Definition crypto.cpp:590
void markOneTimeKeysAsPublished()
Definition crypto.cpp:338
void loadJson(const nlohmann::json &j)
Definition crypto.cpp:656
nlohmann::json unpublishedOneTimeKeys() const
According to olm.h, this returns an object like.
Definition crypto.cpp:322
std::size_t importInboundGroupSessions(const nlohmann::json &keys)
Import group sessions from json.
Definition crypto.cpp:371
std::size_t maxNumberOfOneTimeKeys() const
Definition crypto.cpp:303
immer::map< std::string, immer::flex_vector< std::string > > UserIdToDeviceIdMap
Definition crypto.hpp:235
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:584
std::string rotateMegOlmSessionWithRandom(RandomData random, Timestamp timeMs, std::string roomId)
Rotate the megolm session using user-provided random data.
Definition crypto.cpp:562
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:509
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:570
Crypto()
Construct an invalid Crypto.
Definition crypto.cpp:214
void save(Archive &ar, const unsigned int) const
Definition crypto.hpp:259
std::size_t encryptOlmRandomSize(std::string theirCurve25519IdentityKey) const
Definition crypto.cpp:482
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:35
nlohmann::json json
Definition jsonwrap.hpp:20
std::int_fast64_t Timestamp
Definition event.hpp:18
Definition location.hpp:27
Definition clientutil.hpp:140
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:33
Definition session-p.hpp:16