Try AES on binary payloads

This commit is contained in:
QuantumEntangledAndy committed 2023-08-06 17:31:08 +07:00
1 parent 971c1cddf8
commit b90d6d7eed
5 files changed
+43 -23

No files matched your search

+7 -2
View File
@@ -29,7 +29,9 @@ impl Encoder<Bc> for BcCodex {
// let context = self.context.read().unwrap();
let buf: Vec<u8> = Default::default();
let enc_protocol: EncryptionProtocol = match self.context.get_encrypted() {
EncryptionProtocol::Aes(_) if item.meta.msg_id == 1 => {
EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_)
if item.meta.msg_id == 1 =>
{
// During login the encyption protocol cannot go higher than BCEncrypt
// even if we support AES. (BUt it can go lower i.e. None)
EncryptionProtocol::BCEncrypt
@@ -110,7 +112,10 @@ impl Decoder for BcCodex {
match encryption_protocol_byte {
0x00 => self.context.set_encrypted(EncryptionProtocol::Unencrypted),
0x01 => self.context.set_encrypted(EncryptionProtocol::BCEncrypt),
0x02 | 0x12 => self.context.set_encrypted(EncryptionProtocol::Aes(
0x02 => self.context.set_encrypted(EncryptionProtocol::Aes(
self.context.credentials.make_aeskey(nonce),
)),
0x12 => self.context.set_encrypted(EncryptionProtocol::FullAes(
self.context.credentials.make_aeskey(nonce),
)),
_ => {
+23 -19
View File
@@ -149,24 +149,23 @@ fn bc_modern_msg<'a>(
msg_id: 1,
response_code,
..
} if (response_code & 0xff) == 0x00 => EncryptionProtocol::Unencrypted,
BcHeader {
msg_id: 1,
response_code,
..
} if (response_code & 0xff) == 0x01 => EncryptionProtocol::BCEncrypt,
BcHeader {
msg_id: 1,
response_code,
..
} if (response_code & 0xff) == 0x02 || (response_code & 0xff) == 0x12 => {
EncryptionProtocol::BCEncrypt
} // This is AES but the first packet with the NONCE is BCEcrypt, since the NONCE in this packet is required to build the AES key
BcHeader { msg_id: 1, .. }
if matches!(context.get_encrypted(), EncryptionProtocol::Aes(_)) =>
{
// Maximum encryption level is BCEncrypt during login... Not sure why Reolink did it like that
EncryptionProtocol::BCEncrypt
} if (response_code >> 8) & 0xff == 0xdd => {
// 0xdd means we are setting the encryption method
// Durig login, the max encryption is BcEncrypt since
// the nonce has not been exchanged yet
match response_code & 0xff {
0x00 => EncryptionProtocol::Unencrypted,
_ => EncryptionProtocol::BCEncrypt,
}
}
BcHeader { msg_id: 1, .. } => {
match *context.get_encrypted() {
EncryptionProtocol::Aes(_) | EncryptionProtocol::FullAes(_) => {
// During login max is BcEncrypt
EncryptionProtocol::BCEncrypt
}
n => n,
}
}
_ => *context.get_encrypted(),
};
@@ -174,7 +173,12 @@ fn bc_modern_msg<'a>(
let processed_payload_buf =
xml_crypto::decrypt(header.channel_id as u32, payload_buf, &encryption_protocol);
if context.in_bin_mode.contains(&(header.msg_num)) || in_binary {
payload = Some(BcPayloads::Binary(payload_buf.to_vec()));
payload = match context.get_encrypted() {
EncryptionProtocol::FullAes(_) => {
Some(BcPayloads::Binary(processed_payload_buf.to_vec()))
}
_ => Some(BcPayloads::Binary(payload_buf.to_vec())),
};
} else {
let xml = BcXml::try_parse(processed_payload_buf.as_slice()).map_err(|_| {
error!("header.msg_id: {}", header.msg_id);
+3
View File
@@ -200,6 +200,9 @@ pub enum EncryptionProtocol {
/// Latest cameras/firmwares use Aes with the key derived from
/// the camera's password and the negotiated NONCE
Aes([u8; 16]),
/// Same as Aes but the media stream is also encrypted and not just
/// the control commands
FullAes([u8; 16]),
}
#[derive(Debug)]
+8
View File
@@ -259,6 +259,12 @@ pub struct Extension {
/// The rfID used in the PIR
#[yaserde(rename = "rfId")]
pub rf_id: Option<u8>,
/// Encrypted binary has this to verify successful decryption
#[yaserde(rename = "checkPos")]
pub check_pos: Option<u32>,
/// Encrypted binary has this to verify successful decryption
#[yaserde(rename = "checkValue")]
pub check_value: Option<u32>,
}
impl Default for Extension {
@@ -270,6 +276,8 @@ impl Default for Extension {
token: None,
channel_id: None,
rf_id: None,
check_pos: None,
check_value: None,
}
}
}
+2 -2
View File
@@ -21,7 +21,7 @@ pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol
.map(|(key, i)| *i ^ key ^ (offset as u8))
.collect()
}
EncryptionProtocol::Aes(aeskey) => {
EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => {
// AES decryption
let mut decrypted = buf.to_vec();
@@ -41,7 +41,7 @@ pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol
// Encrypt is the same as decrypt
decrypt(offset, buf, encryption_protocol)
}
EncryptionProtocol::Aes(aeskey) => {
EncryptionProtocol::Aes(aeskey) | EncryptionProtocol::FullAes(aeskey) => {
// AES encryption
let mut encrypted = buf.to_vec();
Aes128CfbEnc::new(aeskey.into(), IV.into()).encrypt(&mut encrypted);