Skip to main content

livekit_datatrack/
e2ee.rs

1// Copyright 2025 LiveKit, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use bytes::Bytes;
16use core::fmt::Debug;
17use thiserror::Error;
18
19// TODO: If a core module for end-to-end encryption is created in the future
20// (livekit-e2ee), these traits should be moved to there.
21
22/// Encrypted payload and metadata required for decryption.
23pub struct EncryptedPayload {
24    pub payload: Bytes,
25    pub iv: [u8; 12],
26    pub key_index: u8,
27}
28
29/// An error indicating a payload could not be encrypted.
30#[derive(Debug, Error)]
31#[error("Encryption failed")]
32pub struct EncryptionError;
33
34/// An error indicating a payload could not be decrypted.
35#[derive(Debug, Error)]
36#[error("Decryption failed")]
37pub struct DecryptionError;
38
39/// Provider for encrypting payloads for E2EE.
40pub trait EncryptionProvider: Send + Sync + Debug {
41    /// Encrypts the given payload being sent by the local participant.
42    fn encrypt(&self, payload: Bytes) -> Result<EncryptedPayload, EncryptionError>;
43}
44
45/// Provider for decrypting payloads for E2EE.
46pub trait DecryptionProvider: Send + Sync + Debug {
47    /// Decrypts the given payload received from a remote participant.
48    ///
49    /// Sender identity is required in order for the proper key to be used
50    /// for decryption.
51    ///
52    fn decrypt(
53        &self,
54        payload: EncryptedPayload,
55        sender_identity: &str,
56    ) -> Result<Bytes, DecryptionError>;
57}