LiveKit C++ Client SDK v1.1.0
Real-time audio/video/data SDK for C++
Loading...
Searching...
No Matches
e2ee.h
1/*
2 * Copyright 2025 LiveKit
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an “AS IS” BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cstdint>
20#include <memory>
21#include <optional>
22#include <string>
23#include <vector>
24
25#include "livekit/visibility.h"
26
27namespace livekit {
28
31enum class EncryptionType {
32 NONE = 0,
33 GCM = 1,
34 CUSTOM = 2,
35};
36
39 PBKDF2 = 0,
40 HKDF = 1,
41};
42
44inline constexpr const char* kDefaultRatchetSalt = "LKFrameEncryptionKey";
45inline constexpr int kDefaultRatchetWindowSize = 16;
46inline constexpr int kDefaultFailureTolerance = -1;
47inline constexpr int kDefaultKeyRingSize = 16;
48inline constexpr KeyDerivationFunction kDefaultKeyDerivationFunction = KeyDerivationFunction::PBKDF2;
49
65 std::optional<std::vector<std::uint8_t>> shared_key;
66
70 std::vector<std::uint8_t> ratchet_salt = std::vector<std::uint8_t>(
71 kDefaultRatchetSalt, kDefaultRatchetSalt + std::char_traits<char>::length(kDefaultRatchetSalt));
72
74 int ratchet_window_size = kDefaultRatchetWindowSize;
75
77 int failure_tolerance = kDefaultFailureTolerance;
78
80 int key_ring_size = kDefaultKeyRingSize;
81
83 KeyDerivationFunction key_derivation_function = kDefaultKeyDerivationFunction;
84};
85
98 KeyProviderOptions key_provider_options{};
99 EncryptionType encryption_type = EncryptionType::GCM; // default & recommended
100};
101
117class LIVEKIT_API E2EEManager {
118public:
126 class LIVEKIT_API KeyProvider {
127 public:
128 ~KeyProvider() = default;
129
130 KeyProvider(const KeyProvider&) = delete;
131 KeyProvider& operator=(const KeyProvider&) = delete;
132 KeyProvider(KeyProvider&&) noexcept = default;
133 KeyProvider& operator=(KeyProvider&&) noexcept = default;
134
137
139 void setSharedKey(const std::vector<std::uint8_t>& key, int key_index = 0);
140
142 std::vector<std::uint8_t> exportSharedKey(int key_index = 0) const;
143
145 std::vector<std::uint8_t> ratchetSharedKey(int key_index = 0);
146
148 void setKey(const std::string& participant_identity, const std::vector<std::uint8_t>& key, int key_index = 0);
149
151 std::vector<std::uint8_t> exportKey(const std::string& participant_identity, int key_index = 0) const;
152
154 std::vector<std::uint8_t> ratchetKey(const std::string& participant_identity, int key_index = 0);
155
156 private:
157 friend class E2EEManager;
158 KeyProvider(std::uint64_t room_handle, KeyProviderOptions options);
159 std::uint64_t room_handle_{0};
160 KeyProviderOptions options_;
161 };
162
164 class LIVEKIT_API FrameCryptor {
165 public:
166 FrameCryptor(std::uint64_t room_handle, std::string participant_identity, int key_index, bool enabled);
167 ~FrameCryptor() = default;
168 FrameCryptor(const FrameCryptor&) = delete;
169 FrameCryptor& operator=(const FrameCryptor&) = delete;
170 FrameCryptor(FrameCryptor&&) noexcept = default;
171 FrameCryptor& operator=(FrameCryptor&&) noexcept = default;
172
173 const std::string& participantIdentity() const;
174 int keyIndex() const;
175 bool enabled() const;
176
178 void setEnabled(bool enabled);
179
181 void setKeyIndex(int key_index);
182
183 private:
184 std::uint64_t room_handle_{0};
185 bool enabled_{false};
186 std::string participant_identity_;
187 int key_index_{0};
188 };
189
190 ~E2EEManager() = default;
191 E2EEManager(const E2EEManager&) = delete;
192 E2EEManager& operator=(const E2EEManager&) = delete;
193 E2EEManager(E2EEManager&&) noexcept = delete;
194 E2EEManager& operator=(E2EEManager&&) noexcept = delete;
195
197 bool enabled() const;
198
204 void setEnabled(bool enabled);
205
215 std::weak_ptr<KeyProvider> keyProvider();
216 std::weak_ptr<const KeyProvider> keyProvider() const;
217
219 std::vector<E2EEManager::FrameCryptor> frameCryptors() const;
220
221protected:
223 explicit E2EEManager(std::uint64_t room_handle, const E2EEOptions& options);
224 friend class Room;
225
226private:
227 std::uint64_t room_handle_{0};
228 bool enabled_{false};
229 E2EEOptions options_;
232 std::shared_ptr<KeyProvider> key_provider_;
233};
234
235} // namespace livekit
Frame-level cryptor controls for one participant.
Definition e2ee.h:164
void setKeyIndex(int key_index)
Sets the active key index for this participant cryptor.
void setEnabled(bool enabled)
Enables or disables frame encryption/decryption for this participant.
Manages encryption keys used by the E2EE pipeline.
Definition e2ee.h:126
std::vector< std::uint8_t > exportSharedKey(int key_index=0) const
Exports the shared key for a given key slot.
void setKey(const std::string &participant_identity, const std::vector< std::uint8_t > &key, int key_index=0)
Sets a key for a specific participant identity.
void setSharedKey(const std::vector< std::uint8_t > &key, int key_index=0)
Sets the shared key for the given key slot.
const KeyProviderOptions & options() const
Returns the options used to initialize this KeyProvider.
std::vector< std::uint8_t > exportKey(const std::string &participant_identity, int key_index=0) const
Exports a participant-specific key.
std::vector< std::uint8_t > ratchetSharedKey(int key_index=0)
Ratchets the shared key at key_index and returns the newly derived key.
std::vector< std::uint8_t > ratchetKey(const std::string &participant_identity, int key_index=0)
Ratchets a participant-specific key and returns the new key.
E2EE manager for a connected room.
Definition e2ee.h:117
Represents a LiveKit room session.
Definition room.h:98
Public API for the LiveKit C++ Client SDK.
Definition audio_frame.h:25
constexpr const char * kDefaultRatchetSalt
Defaults (match Rust KeyProviderOptions::default()).
Definition e2ee.h:44
KeyDerivationFunction
Key derivation algorithm used by the key provider.
Definition e2ee.h:38
EncryptionType
Encryption algorithm type used by the underlying stack.
Definition e2ee.h:31
End-to-end encryption (E2EE) configuration for a room.
Definition e2ee.h:97
Options for configuring the key provider used by E2EE.
Definition e2ee.h:57
std::optional< std::vector< std::uint8_t > > shared_key
Shared static key for "shared-key E2EE" (optional).
Definition e2ee.h:65
std::vector< std::uint8_t > ratchet_salt
Salt used when deriving ratcheted keys.
Definition e2ee.h:70
int key_ring_size
Number of key slots retained by the key provider.
Definition e2ee.h:80
KeyDerivationFunction key_derivation_function
Algorithm used when deriving ratcheted keys.
Definition e2ee.h:83
int ratchet_window_size
Controls how many previous keys are retained during ratcheting.
Definition e2ee.h:74
int failure_tolerance
Number of tolerated ratchet failures before reporting encryption errors.
Definition e2ee.h:77