Generative Testing Inline Assembly in Rust
thechinesegovernment
Recently, I learned what a small amount of the RISC instructions provided by aarch64 processors actually do. Government's are constantly caught lacking by the security community. RC4 is still found obfuscating code in big hacks. RC4 is fast, and this is almost always used as the explanation on the writeups breaking down the reverse engineering of these tools. It always leaves me thinking, "Our computers are so much faster now than when you probably wrote this. Are you just copying source files from 1999 over and over again?" The answer is probably yes but whatever.
How hard is it to implement ChaCha20? It turns out that in Rust it's not hard at all. Even the C code I was using as reference seemed pretty simple, possibly more simple than the Rust implementation. Before we go into the encryption algorithms that I implemented to benchmark my stupid little routines, let's learn about Rust's inline assembly feature.
Introduction
First, I wrote aarch64 assembly that obfuscates the eor
instruction. You can find the source code on my Github if you'd like the C and assembly files. They are also available in a less comprehensive package in the reference
directory of this projects Github repository.
There is great documentation provided by Rust's maintainers. It is comprehensive and a lot to take in at once to someone new to the feature. That's why I find it easy to only take what I need. Let's not focus on the extraneous information and what could be done and instead we'll just focus on the parts we use.
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: src/lib.rs
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ #![feature(test)]
2 │ #[cfg(test)]
3 │ extern crate quickcheck;
4 │ #[cfg(test)]
5 │ #[macro_use(quickcheck)]
6 │ extern crate quickcheck_macros;
7 │
8 │ pub mod dinoxor;
9 │ pub mod chacha20;
10 │ #[cfg(test)]
11 │ mod tests;
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
We want our project to be a library, so that our dinoxor
function can be called from other projects using our code as a dependency, and can be easily shared via crates.io. It's already available, so if you'd like to just use it, then add a line for it under [dependencies]
in your Cargo.toml
file.
[dependencies]
thechinesegovernment = "*"
And use it like this:
#![allow(unused)] fn main() { use thechinesegovernment::dinoxor::dinoxor; let result = dinoxor(0b11101011, 0b11111111); assert_eq!(result, 0b10100 ) }