Skip to content

Commit 8189aa9

Browse files
committed
Implement using HKDF as the KDF.
1 parent 330375c commit 8189aa9

8 files changed

Lines changed: 74 additions & 5 deletions

File tree

.cargo/config.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@ rustflags = ["-C", "link-args=-ObjC"]
1414
rustflags = ["-C", "link-args=-ObjC"]
1515

1616
[target.aarch64-apple-ios-sim]
17-
rustflags = ["-C", "link-args=-ObjC"]
17+
rustflags = ["-C", "link-args=-ObjC"]
18+
19+
[env]
20+
# DO not merge! WIP change to for custom webrtc-sys build
21+
LK_CUSTOM_WEBRTC = { value = "webrtc-sys/libwebrtc/linux-x64-release", relative = true }

libwebrtc/src/native/frame_cryptor.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,29 @@ use crate::{
2525

2626
pub type OnStateChange = Box<dyn FnMut(String, EncryptionState) + Send + Sync>;
2727

28+
#[derive(Copy, Clone, Debug)]
29+
#[non_exhaustive]
30+
pub enum KeyDerivationAlgorithm {
31+
PBKDF2,
32+
HKDF,
33+
}
34+
impl Into<sys_fc::ffi::KeyDerivationAlgorithm> for KeyDerivationAlgorithm {
35+
fn into(self) -> sys_fc::ffi::KeyDerivationAlgorithm {
36+
match self {
37+
KeyDerivationAlgorithm::PBKDF2 => sys_fc::ffi::KeyDerivationAlgorithm::PBKDF2,
38+
KeyDerivationAlgorithm::HKDF => sys_fc::ffi::KeyDerivationAlgorithm::HKDF,
39+
}
40+
}
41+
}
42+
2843
#[derive(Debug, Clone)]
2944
pub struct KeyProviderOptions {
3045
pub shared_key: bool,
3146
pub ratchet_window_size: i32,
3247
pub ratchet_salt: Vec<u8>,
3348
pub failure_tolerance: i32,
3449
pub key_ring_size: i32,
50+
pub key_derivation_algorithm: KeyDerivationAlgorithm,
3551
}
3652

3753
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -274,6 +290,7 @@ impl From<KeyProviderOptions> for sys_fc::ffi::KeyProviderOptions {
274290
ratchet_salt: value.ratchet_salt,
275291
failure_tolerance: value.failure_tolerance,
276292
key_ring_size: value.key_ring_size,
293+
key_derivation_algorithm: value.key_derivation_algorithm.into(),
277294
}
278295
}
279296
}

livekit-ffi/protocol/e2ee.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ message KeyProviderOptions {
3838
required int32 ratchet_window_size = 2;
3939
required bytes ratchet_salt = 3;
4040
required int32 failure_tolerance = 4; // -1 = no tolerance
41+
required int32 key_ring_size = 5;
42+
required KeyDerivationFunction key_derivation_function = 6;
43+
}
44+
45+
enum KeyDerivationFunction {
46+
PBKDF2 = 0;
47+
HKDF = 1;
4148
}
4249

4350
message E2eeOptions {

livekit-ffi/src/conversion/room.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use livekit::{
2121
options::{AudioEncoding, TrackPublishOptions, VideoEncoding},
2222
prelude::*,
2323
webrtc::{
24-
native::frame_cryptor::EncryptionState,
24+
native::frame_cryptor::{EncryptionState, KeyDerivationAlgorithm},
2525
prelude::{ContinualGatheringPolicy, IceServer, IceTransportsType, RtcConfiguration},
2626
},
2727
RoomInfo,
@@ -133,10 +133,22 @@ impl From<proto::DisconnectReason> for DisconnectReason {
133133

134134
impl From<proto::KeyProviderOptions> for KeyProviderOptions {
135135
fn from(value: proto::KeyProviderOptions) -> Self {
136+
let key_derivation_algorithm = value.key_derivation_function().into();
136137
Self {
137138
ratchet_window_size: value.ratchet_window_size,
138139
ratchet_salt: value.ratchet_salt,
139140
failure_tolerance: value.failure_tolerance,
141+
key_ring_size: value.key_ring_size,
142+
key_derivation_algorithm,
143+
}
144+
}
145+
}
146+
147+
impl From<proto::KeyDerivationFunction> for KeyDerivationAlgorithm {
148+
fn from(value: proto::KeyDerivationFunction) -> Self {
149+
match value {
150+
proto::KeyDerivationFunction::Pbkdf2 => KeyDerivationAlgorithm::PBKDF2,
151+
proto::KeyDerivationFunction::Hkdf => KeyDerivationAlgorithm::HKDF,
140152
}
141153
}
142154
}

livekit/src/room/e2ee/key_provider.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use libwebrtc::native::frame_cryptor as fc;
15+
use libwebrtc::native::frame_cryptor::{self as fc, KeyDerivationAlgorithm};
1616
use std::sync::{
1717
atomic::{AtomicI32, Ordering},
1818
Arc,
@@ -31,6 +31,7 @@ pub struct KeyProviderOptions {
3131
pub ratchet_salt: Vec<u8>,
3232
pub failure_tolerance: i32,
3333
pub key_ring_size: i32,
34+
pub key_derivation_algorithm: KeyDerivationAlgorithm,
3435
}
3536

3637
impl Default for KeyProviderOptions {
@@ -40,6 +41,7 @@ impl Default for KeyProviderOptions {
4041
ratchet_salt: DEFAULT_RATCHET_SALT.to_owned().into_bytes(),
4142
failure_tolerance: DEFAULT_FAILURE_TOLERANCE,
4243
key_ring_size: DEFAULT_KEY_RING_SIZE,
44+
key_derivation_algorithm: KeyDerivationAlgorithm::PBKDF2,
4345
}
4446
}
4547
}
@@ -60,6 +62,7 @@ impl KeyProvider {
6062
ratchet_salt: options.ratchet_salt,
6163
failure_tolerance: options.failure_tolerance,
6264
key_ring_size: options.key_ring_size,
65+
key_derivation_algorithm: options.key_derivation_algorithm,
6366
}),
6467
latest_key_index: Arc::new(AtomicI32::new(0)),
6568
}
@@ -72,6 +75,7 @@ impl KeyProvider {
7275
ratchet_salt: options.ratchet_salt,
7376
failure_tolerance: options.failure_tolerance,
7477
key_ring_size: options.key_ring_size,
78+
key_derivation_algorithm: options.key_derivation_algorithm,
7579
});
7680
handle.set_shared_key(0, shared_key);
7781
Self { handle, latest_key_index: Arc::new(AtomicI32::new(0)) }

webrtc-sys/libwebrtc/.gclient

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
solutions = [
22
{
33
"name": 'src',
4-
"url": 'https://github.com/webrtc-sdk/webrtc.git@m137_release',
4+
"url": 'https://github.com/webrtc-sdk/webrtc.git@duan/hkdf-sha256-key-derivation',
55
"custom_deps": {},
66
"deps_file": "DEPS",
77
"managed": False,

webrtc-sys/src/frame_cryptor.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ webrtc::FrameCryptorTransformer::Algorithm AlgorithmToFrameCryptorAlgorithm(
4040
}
4141
}
4242

43+
webrtc::KeyDerivationAlgorithm
44+
KeyDerivationAlgorithmToFrameCryptorKeyDerivationAlgorithm(
45+
KeyDerivationAlgorithm algorithm) {
46+
switch (algorithm) {
47+
case KeyDerivationAlgorithm::PBKDF2:
48+
return webrtc::KeyDerivationAlgorithm::kPBKDF2;
49+
case KeyDerivationAlgorithm::HKDF:
50+
return webrtc::KeyDerivationAlgorithm::kHKDF;
51+
default:
52+
return webrtc::KeyDerivationAlgorithm::kPBKDF2;
53+
}
54+
}
55+
4356
KeyProvider::KeyProvider(KeyProviderOptions options) {
4457
webrtc::KeyProviderOptions rtc_options;
4558
rtc_options.shared_key = options.shared_key;
@@ -52,7 +65,9 @@ KeyProvider::KeyProvider(KeyProviderOptions options) {
5265
rtc_options.ratchet_window_size = options.ratchet_window_size;
5366
rtc_options.failure_tolerance = options.failure_tolerance;
5467
rtc_options.key_ring_size = options.key_ring_size;
55-
68+
rtc_options.key_derivation_algorithm =
69+
KeyDerivationAlgorithmToFrameCryptorKeyDerivationAlgorithm(
70+
options.key_derivation_algorithm);
5671
impl_ =
5772
new rtc::RefCountedObject<webrtc::DefaultKeyProviderImpl>(rtc_options);
5873
}

webrtc-sys/src/frame_cryptor.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ pub mod ffi {
2626
pub ratchet_salt: Vec<u8>,
2727
pub failure_tolerance: i32,
2828
pub key_ring_size: i32,
29+
pub key_derivation_algorithm: KeyDerivationAlgorithm,
30+
}
31+
32+
#[derive(Debug)]
33+
#[repr(i32)]
34+
pub enum KeyDerivationAlgorithm {
35+
PBKDF2 = 0,
36+
HKDF,
2937
}
3038

3139
#[derive(Debug)]
@@ -250,6 +258,8 @@ mod tests {
250258
ratchet_window_size: 16,
251259
ratchet_salt: vec![],
252260
failure_tolerance: -1,
261+
key_ring_size: 16,
262+
key_derivation_algorithm: ffi::KeyDerivationAlgorithm::HKDF,
253263
};
254264

255265
let key_provider = ffi::new_key_provider(options);

0 commit comments

Comments
 (0)