~/

← ~/blog

Cryptography Race Rust vs C

2026-11-30 · #rust , #aes-128 , #sha-256 , #c , #benchmark

Rust has been gaining traction as a system programming language, ranking #10 in terms of popularity as of Oct 2026 ( https://www.tiobe.com/tiobe-index/rust/ ). One of it’s main selling point is memory safety guarantee during compile time. This is significant considering that 60-70% of iOS vulnerabilities are due to memory safety ( https://langui.sh/2019/07/23/apple-memory-safety/ ), similar metrics are reported by Microsoft ( https://www.microsoft.com/en-us/msrc/blog/2019/07/we-need-a-safer-systems-programming-language ) and Google ( https://security.googleblog.com/2019/05/queue-hardening-enhancements.html ).

Out-of-bounds access bugs ( a type of spatial memory safety bug ) are happily compiled into binary by C/C++ compilers. Ever famous CrowdStrike Global IT Outage from 2024 was due to same when they deployed a piece of software that supplied 21 input fields where the code expected 20 ( https://www.cybersecuritydive.com/news/crowdstrike-mismatch-falcon-sensor-outage/723569/ ). That simple looking out-of-bound read was indeed the reason why GPs could’t see their patients, air traffic came to a halt, government services and banks went offline.

Memory bugs are also one of the most commonly exploited ways to hack into a system. Heartbleed, the ever famous OpenSSL cryptographic library bug was a buffer over-read (https://en.wikipedia.org/wiki/Heartbleed).

Importance of the memory safety guarantees are execerbated by the speed in which we started generating code with the help of LLMs. Guardrails are crucial when using LLMs to generate code, and rustc is the one of the best guardrails ( deterministic too ) there can be. Rustc says give me a piece of code and I will make sure it is free of memory safety bugs. It guarantees the binary is free from the type of bug that is so widespread ( 70% of critical bugs ) and causes significant security vulnerabities.

With the above in mind, below is a C vs Rust speed comparison for two ubiquitous cryptography algorithms, SHA-256 and AES-128, on ARMv8-A. Each algorithm is implemented twice in each language: a plain scalar version built from bit-wise ops, and an accelerated version using the ARMv8 Crypto Extensions (SHA256H/H2/SU0/SU1 for SHA-256, AESE/AESMC for AES). C is compiled with Apple Clang 21 at -O2. Rust is compiled with --release, opt-level 3, LTO and a single codegen unit, with the sha2 and aes target features turned on. Both harnesses do 1000 warmup iterations followed by 10,000 timed iterations over a 4 KiB buffer, and each figure is the median of 3 runs.

AlgorithmVariantCRust
SHA-256Scalar~122 MB/s~119 MB/s
SHA-256ARMv8 Crypto Extensions~1262 MB/s~1260 MB/s
AES-128Scalar~227 MB/s~256 MB/s
AES-128ARMv8 Crypto Extensions~7400 MB/s~8780 MB/s

Finding 1 - Neither language wins outright. Rust beats C on scalar AES-128 with about 12% higher throughput (~59 vs ~67 ns per block), whereas C is marginally (2-3%) faster on scalar SHA-256 (32.0 vs 32.9 µs per 4 KiB hash). At small input sizes SHA-256 is a statistical dead heat. I reckon with enough sweat ( or AI credits these days ) one could optimize the C code to match Rust for AES-128. The bigger point is that choosing Rust costs nothing in speed here, and you get the memory safety guarantees on top.

Finding 2 - Hardware acceleration matters far more than the language. The ARMv8 Crypto Extensions deliver an order-of-magnitude speedup in both algorithms: about 10x for SHA-256 and 33-34x for AES-128 over scalar. That gap dwarfs the C vs Rust gap. On accelerated SHA-256 the two languages are tied (1262 vs 1260 MB/s), which makes sense since both compile down to the same SHA256H/H2/SU0/SU1 sequence. On accelerated AES-128, Rust pulls ahead by about 19% (~1.75 vs ~2.1 ns per block). The instructions are the same in both binaries. The difference comes from scheduling: rustc fully unrolls the 10 rounds, keeps the round keys in registers and folds the extra XORs. If your crypto code isn’t using the hardware instructions, fix that before you worry about which language it is written in.

Code, test vectors and full results: https://github.com/golarkx/cryptography-standoff


more posts in ~/blog