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, KeyProviderOptions options);
153 std::uint64_t room_handle_{0};
154 KeyProviderOptions options_;
155 };
156
158 public:
159 FrameCryptor(std::uint64_t room_handle, std::string participant_identity,
160 int key_index, bool enabled);
161 ~FrameCryptor() = default;
162 FrameCryptor(const FrameCryptor &) = delete;
163 FrameCryptor &operator=(const FrameCryptor &) = delete;
164 FrameCryptor(FrameCryptor &&) noexcept = default;
165 FrameCryptor &operator=(FrameCryptor &&) noexcept = default;
166
167 const std::string &participantIdentity() const;
168 int keyIndex() const;
169 bool enabled() const;
170
172 void setEnabled(bool enabled);
173
175 void setKeyIndex(int key_index);
176
177 private:
178 std::uint64_t room_handle_{0};
179 bool enabled_{false};
180 std::string participant_identity_;
181 int key_index_{0};
182 };
183
184 ~E2EEManager() = default;
185 E2EEManager(const E2EEManager &) = delete;
186 E2EEManager &operator=(const E2EEManager &) = delete;
187 E2EEManager(E2EEManager &&) noexcept = delete;
188 E2EEManager &operator=(E2EEManager &&) noexcept = delete;
189
191 bool enabled() const;
192
198 void setEnabled(bool enabled);
199
203 const KeyProvider *keyProvider() const;
204
207
208protected:
210 explicit E2EEManager(std::uint64_t room_handle, const E2EEOptions &options);
211 friend class Room;
212
213private:
214 std::uint64_t room_handle_{0};
215 bool enabled_{false};
216 E2EEOptions options_;
217 KeyProvider key_provider_;
218};
219
220} // 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:97
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