extern crate alloc;
use alloc::boxed::Box;
use alloc::vec::Vec;
use crate::authentication::{IdentityHandle, SharedSecretHandle};
use crate::error::SecurityResult;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct CryptoHandle(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ReceiverMac {
pub key_id: u32,
pub mac: [u8; 16],
}
impl ReceiverMac {
pub const WIRE_SIZE: usize = 4 + 16;
}
pub trait CryptographicPlugin: Send + Sync {
fn endpoint_payload_token(&self, _handle: CryptoHandle) -> Option<alloc::vec::Vec<u8>> {
None
}
fn register_local_participant(
&mut self,
identity: IdentityHandle,
properties: &[(&str, &str)],
) -> SecurityResult<CryptoHandle>;
fn register_matched_remote_participant(
&mut self,
local: CryptoHandle,
remote_identity: IdentityHandle,
shared_secret: SharedSecretHandle,
) -> SecurityResult<CryptoHandle>;
fn register_local_endpoint(
&mut self,
participant: CryptoHandle,
is_writer: bool,
properties: &[(&str, &str)],
) -> SecurityResult<CryptoHandle>;
fn create_local_participant_crypto_tokens(
&mut self,
local: CryptoHandle,
remote: CryptoHandle,
) -> SecurityResult<Vec<u8>>;
fn set_remote_participant_crypto_tokens(
&mut self,
local: CryptoHandle,
remote: CryptoHandle,
tokens: &[u8],
) -> SecurityResult<()>;
fn encrypt_submessage(
&self,
local: CryptoHandle,
remote_list: &[CryptoHandle],
plaintext: &[u8],
aad_extension: &[u8],
) -> SecurityResult<Vec<u8>>;
fn decrypt_submessage(
&self,
local: CryptoHandle,
remote: CryptoHandle,
ciphertext: &[u8],
aad_extension: &[u8],
) -> SecurityResult<Vec<u8>>;
fn encrypt_submessage_multi(
&self,
local: CryptoHandle,
receivers: &[(CryptoHandle, u32)],
plaintext: &[u8],
aad_extension: &[u8],
) -> SecurityResult<(Vec<u8>, Vec<ReceiverMac>)> {
let handles: Vec<CryptoHandle> = receivers.iter().map(|(h, _)| *h).collect();
let ciphertext = self.encrypt_submessage(local, &handles, plaintext, aad_extension)?;
Ok((ciphertext, Vec::new()))
}
#[allow(clippy::too_many_arguments)]
fn decrypt_submessage_with_receiver_mac(
&self,
local: CryptoHandle,
remote: CryptoHandle,
own_key_id: u32,
own_mac_key_handle: CryptoHandle,
ciphertext: &[u8],
macs: &[ReceiverMac],
aad_extension: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (own_key_id, own_mac_key_handle);
if macs.is_empty() {
return self.decrypt_submessage(local, remote, ciphertext, aad_extension);
}
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement receiver-specific mac verification",
))
}
fn encode_kx_submessage(
&self,
handle: CryptoHandle,
plaintext: &[u8],
aad_extension: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, plaintext, aad_extension);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement key-exchange-channel protection",
))
}
fn decode_kx_submessage(
&self,
handle: CryptoHandle,
ciphertext: &[u8],
aad_extension: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, ciphertext, aad_extension);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement key-exchange-channel protection",
))
}
fn encode_kx_datawriter_submessage(
&self,
handle: CryptoHandle,
plaintext: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, plaintext);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement cyclone-format kx submessage protection",
))
}
fn decode_kx_datawriter_submessage(
&self,
handle: CryptoHandle,
wire: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, wire);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement cyclone-format kx submessage protection",
))
}
fn encode_data_datawriter_submessage(
&self,
handle: CryptoHandle,
plaintext: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, plaintext);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement cyclone-format data submessage protection",
))
}
fn decode_data_datawriter_submessage(
&self,
handle: CryptoHandle,
wire: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, wire);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement cyclone-format data submessage protection",
))
}
fn decode_data_by_key_id(&self, wire: &[u8]) -> SecurityResult<Vec<u8>> {
let _ = wire;
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement key-id-based data submessage decode",
))
}
fn encode_rtps_message_cyclone(
&self,
local: CryptoHandle,
message: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (local, message);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement cyclone SRTPS encode",
))
}
fn decode_rtps_message_cyclone(&self, message: &[u8]) -> SecurityResult<Vec<u8>> {
let _ = message;
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement cyclone SRTPS decode",
))
}
fn encode_serialized_payload(
&self,
handle: CryptoHandle,
payload: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, payload);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement encode_serialized_payload",
))
}
fn decode_serialized_payload(&self, encoded: &[u8]) -> SecurityResult<Vec<u8>> {
let _ = encoded;
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement decode_serialized_payload",
))
}
fn decode_serialized_payload_with(
&self,
handle: CryptoHandle,
encoded: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, encoded);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement decode_serialized_payload_with",
))
}
fn decode_serialized_payload_kx(
&self,
handle: CryptoHandle,
encoded: &[u8],
) -> SecurityResult<Vec<u8>> {
let _ = (handle, encoded);
Err(crate::error::SecurityError::new(
crate::error::SecurityErrorKind::NotImplemented,
"plugin does not implement decode_serialized_payload_kx",
))
}
fn plugin_class_id(&self) -> &str;
}
pub type CryptoPluginBox = Box<dyn CryptographicPlugin>;