aboutsummaryrefslogtreecommitdiff
path: root/azalea-auth/src/encryption.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-04-24 22:46:41 -0500
committermat <github@matdoes.dev>2022-04-24 22:46:41 -0500
commitf4dd3a9293367fa8f3d839a184e8055b22595204 (patch)
tree3beafa3c8035c69a33b7d0f12779236bf786cf36 /azalea-auth/src/encryption.rs
parent4c00bd886578c70f6aeb35400d9d03b355df3155 (diff)
downloadazalea-drasl-f4dd3a9293367fa8f3d839a184e8055b22595204.tar.xz
ENCRYPTION WORKS!!!!!!!!!!!
Diffstat (limited to 'azalea-auth/src/encryption.rs')
-rw-r--r--azalea-auth/src/encryption.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/azalea-auth/src/encryption.rs b/azalea-auth/src/encryption.rs
index 72739620..6f2e43a8 100644
--- a/azalea-auth/src/encryption.rs
+++ b/azalea-auth/src/encryption.rs
@@ -1,3 +1,8 @@
+use aes::{
+ cipher::{AsyncStreamCipher, NewCipher},
+ Aes128,
+};
+use cfb8::Cfb8;
use rand::{rngs::OsRng, RngCore};
use sha1::{Digest, Sha1};
@@ -24,7 +29,9 @@ fn hex_digest(digest: &[u8]) -> String {
num_bigint::BigInt::from_signed_bytes_be(digest).to_str_radix(16)
}
+#[derive(Debug)]
pub struct EncryptResult {
+ pub secret_key: [u8; 16],
pub encrypted_public_key: Vec<u8>,
pub encrypted_nonce: Vec<u8>,
}
@@ -43,11 +50,26 @@ pub fn encrypt(public_key: &[u8], nonce: &[u8]) -> Result<EncryptResult, String>
let encrypted_nonce: Vec<u8> = rsa_public_encrypt_pkcs1::encrypt(&public_key, &nonce)?;
Ok(EncryptResult {
+ secret_key,
encrypted_public_key,
encrypted_nonce,
})
}
+// TODO: update the aes and cfb8 crates
+pub type Aes128Cfb = Cfb8<Aes128>;
+
+pub fn create_cipher(key: &[u8]) -> Aes128Cfb {
+ Aes128Cfb::new_from_slices(&key, &key).unwrap()
+}
+
+pub fn encrypt_packet(cipher: &mut Aes128Cfb, packet: &mut [u8]) {
+ cipher.encrypt(packet);
+}
+pub fn decrypt_packet(cipher: &mut Aes128Cfb, packet: &mut [u8]) {
+ cipher.decrypt(packet);
+}
+
#[cfg(test)]
mod tests {
use super::*;