LiveKit C++ SDK
Real-time audio/video 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
25namespace livekit {
26
27/* Encryption algorithm type used by the underlying stack.
28 * Keep this aligned with your proto enum values. */
29enum class EncryptionType {
30 NONE = 0,
31 GCM = 1,
32 CUSTOM = 2,
33};
34
35/* Defaults (match other SDKs / Python defaults). */
36inline constexpr const char *kDefaultRatchetSalt = "LKFrameEncryptionKey";
37inline constexpr int kDefaultRatchetWindowSize = 16;
38inline constexpr int kDefaultFailureTolerance = -1;
39
58 std::optional<std::vector<std::uint8_t>> shared_key;
59
63 std::vector<std::uint8_t> ratchet_salt = std::vector<std::uint8_t>(
64 kDefaultRatchetSalt, kDefaultRatchetSalt + std::char_traits<char>::length(
65 kDefaultRatchetSalt));
66
68 int ratchet_window_size = kDefaultRatchetWindowSize;
69
71 int failure_tolerance = kDefaultFailureTolerance;
72};
73
88 KeyProviderOptions key_provider_options{};
89 EncryptionType encryption_type = EncryptionType::GCM; // default & recommended
90};
91
110public:
118 public:
119 ~KeyProvider() = default;
120
121 KeyProvider(const KeyProvider &) = delete;
122 KeyProvider &operator=(const KeyProvider &) = delete;
123 KeyProvider(KeyProvider &&) noexcept = default;
124 KeyProvider &operator=(KeyProvider &&) noexcept = default;
125
128
130 void setSharedKey(const std::vector<std::uint8_t> &key, int key_index = 0);
131
133 std::vector<std::uint8_t> exportSharedKey(int key_index = 0) const;
134
136 std::vector<std::uint8_t> ratchetSharedKey(int key_index = 0);
137
139 void setKey(const std::string &participant_identity,
140 const std::vector<std::uint8_t> &key, int key_index = 0);
141
143 std::vector<std::uint8_t> exportKey(const std::string &participant_identity,
144 int key_index = 0) const;
145
147 std::vector<std::uint8_t>
148 ratchetKey(const std::string &participant_identity, int key_index = 0);
149
150 private:
151 friend class E2EEManager;
152 KeyProvider(std::uint64_t room_handle,
154 std::uint64_t room_handle_{0};
155 KeyProviderOptions options_;
156 };
157
159 public:
160 FrameCryptor(std::uint64_t room_handle, std::string participant_identity,
161 int key_index, bool enabled);
162 ~FrameCryptor() = default;
163 FrameCryptor(const FrameCryptor &) = delete;
164 FrameCryptor &operator=(const FrameCryptor &) = delete;
165 FrameCryptor(FrameCryptor &&) noexcept = default;
166 FrameCryptor &operator=(FrameCryptor &&) noexcept = default;
167
168 const std::string &participantIdentity() const;
169 int keyIndex() const;
170 bool enabled() const;
171
173 void setEnabled(bool enabled);
174
176 void setKeyIndex(int key_index);
177
178 private:
179 std::uint64_t room_handle_{0};
180 bool enabled_{false};
181 std::string participant_identity_;
182 int key_index_{0};
183 };
184
185 ~E2EEManager() = default;
186 E2EEManager(const E2EEManager &) = delete;
187 E2EEManager &operator=(const E2EEManager &) = delete;
188 E2EEManager(E2EEManager &&) noexcept = delete;
189 E2EEManager &operator=(E2EEManager &&) noexcept = delete;
190
192 bool enabled() const;
193
199 void setEnabled(bool enabled);
200
204 const KeyProvider *keyProvider() const;
205
208
209protected:
211 explicit E2EEManager(std::uint64_t room_handle, const E2EEOptions &options);
212 friend class Room;
213
214private:
215 std::uint64_t room_handle_{0};
216 bool enabled_{false};
217 E2EEOptions options_;
218 KeyProvider key_provider_;
219};
220
221} // namespace livekit
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.
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.
Definition e2ee.h:109
bool enabled() const
Returns whether E2EE is currently enabled for this room at runtime.
std::vector< E2EEManager::FrameCryptor > frameCryptors() const
Retrieves the current list of frame cryptors from the underlying runtime.
void setEnabled(bool enabled)
KeyProvider * keyProvider()
Definition room.h:89
Definition e2ee.h:87
Definition e2ee.h:50
std::optional< std::vector< std::uint8_t > > shared_key
Definition e2ee.h:58
std::vector< std::uint8_t > ratchet_salt
Definition e2ee.h:63
int ratchet_window_size
Controls how many previous keys are retained during ratcheting.
Definition e2ee.h:68
int failure_tolerance
Number of tolerated ratchet failures before reporting encryption errors.
Definition e2ee.h:71