From f4630f4d071ade164b7c644fde6f72807170c98c Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Sat, 17 Apr 2021 17:07:40 +0700 Subject: [PATCH] Treat AES encrypt and decrypt differently --- src/bc/de.rs | 5 +++-- src/bc/ser.rs | 4 ++-- src/bc/xml_crypto.rs | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/bc/de.rs b/src/bc/de.rs index 47d6219..1c616fb 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -141,7 +141,8 @@ fn bc_modern_msg<'a, 'b>( let processed_ext_buf = match context.get_encrypted() { EncryptionProtocol::Unencrypted => ext_buf, encryption_protocol => { - decrypted = xml_crypto::crypt(header.channel_id as u32, ext_buf, &encryption_protocol); + decrypted = + xml_crypto::decrypt(header.channel_id as u32, ext_buf, &encryption_protocol); &decrypted } }; @@ -167,7 +168,7 @@ fn bc_modern_msg<'a, 'b>( // Extract remainder of message as binary, if it exists let encryption_protocol = context.get_encrypted(); let processed_payload_buf = - xml_crypto::crypt(header.channel_id as u32, payload_buf, &encryption_protocol); + xml_crypto::decrypt(header.channel_id as u32, payload_buf, &encryption_protocol); if let Ok(xml) = BcXml::try_parse(processed_payload_buf.as_slice()) { payload = Some(BcPayloads::BcXml(xml)); } else { diff --git a/src/bc/ser.rs b/src/bc/ser.rs index e448d5d..aa68eb5 100644 --- a/src/bc/ser.rs +++ b/src/bc/ser.rs @@ -82,7 +82,7 @@ fn bc_ext( encryption_protocol: &EncryptionProtocol, ) -> impl SerializeFn { let xml_bytes = xml.serialize(vec![]).unwrap(); - let enc_bytes = xml_crypto::crypt(enc_offset, &xml_bytes, encryption_protocol); + let enc_bytes = xml_crypto::encrypt(enc_offset, &xml_bytes, encryption_protocol); slice(enc_bytes) } @@ -94,7 +94,7 @@ fn bc_payload( let payload_bytes = match payload { BcPayloads::BcXml(x) => { let xml_bytes = x.serialize(vec![]).unwrap(); - xml_crypto::crypt(enc_offset, &xml_bytes, encryption_protocol) + xml_crypto::encrypt(enc_offset, &xml_bytes, encryption_protocol) } BcPayloads::Binary(x) => x.to_owned(), }; diff --git a/src/bc/xml_crypto.rs b/src/bc/xml_crypto.rs index de03ac5..66bfa1d 100644 --- a/src/bc/xml_crypto.rs +++ b/src/bc/xml_crypto.rs @@ -4,7 +4,7 @@ use openssl::symm::*; const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF]; const IV: &[u8] = b"0123456789abcdef"; -pub fn crypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) -> Vec { +pub fn decrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) -> Vec { match encryption_protocol { EncryptionProtocol::Unencrypted => buf.to_vec(), EncryptionProtocol::BCEncrypt => { @@ -15,14 +15,38 @@ pub fn crypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) .collect() } EncryptionProtocol::Aes(key) => { - // New protocol here + // AES decryption if let Some(aeskey) = key { let t = Cipher::aes_128_cfb128(); - decrypt(t, aeskey, Some(IV), &buf).unwrap() + openssl::symm::decrypt(t, aeskey, Some(IV), &buf).unwrap() } else { // Not yet ready to decrypt (still in login phase) // Use BCEncrypt - crypt(offset, buf, &EncryptionProtocol::BCEncrypt) + decrypt(offset, buf, &EncryptionProtocol::BCEncrypt) + } + } + } +} + +pub fn encrypt(offset: u32, buf: &[u8], encryption_protocol: &EncryptionProtocol) -> Vec { + match encryption_protocol { + EncryptionProtocol::Unencrypted => { + // Encrypt is the same as decrypt + decrypt(offset, buf, encryption_protocol) + } + EncryptionProtocol::BCEncrypt => { + // Encrypt is the same as decrypt + decrypt(offset, buf, encryption_protocol) + } + EncryptionProtocol::Aes(key) => { + // AES encryption + if let Some(aeskey) = key { + let t = Cipher::aes_128_cfb128(); + openssl::symm::encrypt(t, aeskey, Some(IV), &buf).unwrap() + } else { + // Not yet ready to decrypt (still in login phase) + // Use BCEncrypt + encrypt(offset, buf, &EncryptionProtocol::BCEncrypt) } } } @@ -33,7 +57,7 @@ fn test_xml_crypto() { let sample = include_bytes!("samples/xml_crypto_sample1.bin"); let should_be = include_bytes!("samples/xml_crypto_sample1_plaintext.bin"); - let decrypted = crypt(0, &sample[..]); + let decrypted = decrypt(0, &sample[..]); assert_eq!(decrypted, &should_be[..]); } @@ -41,7 +65,7 @@ fn test_xml_crypto() { fn test_xml_crypto_roundtrip() { let zeros: [u8; 256] = [0; 256]; - let decrypted = crypt(0, &zeros[..]); - let encrypted = crypt(0, &decrypted[..]); + let decrypted = encrypt(0, &zeros[..]); + let encrypted = decrypt(0, &decrypted[..]); assert_eq!(encrypted, &zeros[..]); }