Skip to content

Commit

Permalink
Sink CiphertextMessageType down to be FFI-only
Browse files Browse the repository at this point in the history
Regular Rust code doesn't need CiphertextMessageType because it can
already match on CiphertextMessage. On the other hand, FFI code could
really use named constants for the various message types.
  • Loading branch information
jrose-signal committed Oct 21, 2020
1 parent 8b7363a commit 0860733
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 147 deletions.
2 changes: 1 addition & 1 deletion rust/bridge/ffi/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ autogen_warning = "/* This file was automatically generated by cbindgen */"
prefix_with_name = true

[export]
include = ["SignalErrorCode", "FfiDirection"]
include = ["SignalErrorCode", "FfiDirection", "CiphertextMessageType"]
prefix = "Signal"
renaming_overrides_prefixing = true

Expand Down
19 changes: 18 additions & 1 deletion rust/bridge/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,14 +1284,31 @@ pub unsafe extern "C" fn signal_encrypt_message(

ffi_fn_destroy!(signal_ciphertext_message_destroy destroys CiphertextMessage);

#[derive(Debug, ToPrimitive)]
#[repr(C)]
pub enum CiphertextMessageType {
Whisper = 2,
PreKey = 3,
SenderKey = 4,
SenderKeyDistribution = 5,
}

#[no_mangle]
pub unsafe extern "C" fn signal_ciphertext_message_type(
typ: *mut u8,
msg: *const CiphertextMessage,
) -> *mut SignalFfiError {
run_ffi_safe(|| {
let msg = native_handle_cast::<CiphertextMessage>(msg)?;
*typ = msg.message_type().encoding();
let message_type = match msg {
CiphertextMessage::SignalMessage(_) => CiphertextMessageType::Whisper,
CiphertextMessage::PreKeySignalMessage(_) => CiphertextMessageType::PreKey,
CiphertextMessage::SenderKeyMessage(_) => CiphertextMessageType::SenderKey,
CiphertextMessage::SenderKeyDistributionMessage(_) => {
CiphertextMessageType::SenderKeyDistribution
}
};
*typ = num_traits::ToPrimitive::to_u8(&message_type).unwrap();
Ok(())
})
}
Expand Down
4 changes: 2 additions & 2 deletions rust/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ pub use {
identity_key::{IdentityKey, IdentityKeyPair},
kdf::HKDF,
protocol::{
CiphertextMessage, CiphertextMessageType, PreKeySignalMessage,
SenderKeyDistributionMessage, SenderKeyMessage, SignalMessage,
CiphertextMessage, PreKeySignalMessage, SenderKeyDistributionMessage, SenderKeyMessage,
SignalMessage,
},
ratchet::{
are_we_alice, initialize_alice_session, initialize_bob_session,
Expand Down
30 changes: 0 additions & 30 deletions rust/protocol/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,7 @@ pub enum CiphertextMessage {
SenderKeyDistributionMessage(SenderKeyDistributionMessage),
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum CiphertextMessageType {
Whisper,
PreKey,
SenderKey,
SenderKeyDistribution,
}

impl CiphertextMessageType {
pub fn encoding(&self) -> u8 {
match self {
CiphertextMessageType::Whisper => 2,
CiphertextMessageType::PreKey => 3,
CiphertextMessageType::SenderKey => 4,
CiphertextMessageType::SenderKeyDistribution => 5,
}
}
}

impl CiphertextMessage {
pub fn message_type(&self) -> CiphertextMessageType {
match self {
CiphertextMessage::SignalMessage(_) => CiphertextMessageType::Whisper,
CiphertextMessage::PreKeySignalMessage(_) => CiphertextMessageType::PreKey,
CiphertextMessage::SenderKeyMessage(_) => CiphertextMessageType::SenderKey,
CiphertextMessage::SenderKeyDistributionMessage(_) => {
CiphertextMessageType::SenderKeyDistribution
}
}
}

pub fn serialize(&self) -> &[u8] {
match self {
CiphertextMessage::SignalMessage(x) => x.serialized(),
Expand Down
Loading

0 comments on commit 0860733

Please sign in to comment.