SimplifyC++ Article
Does Moving from C++ to Rust Make Cryptography More Secure?

Does Moving from C++ to Rust Make Cryptography More Secure?
Suppose a C++ program and a Rust program encrypt the same message with AES-256-GCM. Give both the same key, nonce, associated data, and authentication tag length, and two correct implementations will produce identical ciphertext and identical tags. Yet you may have good reason to trust one program more than the other. The algorithm defines how to encrypt the message. The program determines who can access the key, when it is safe to act on a decrypted message, and what happens if the server crashes halfway through an operation. [1]
Rust has a clear advantage in memory safety for new code. C++ has a strong case when a team has spent years testing its libraries and hardware integrations in production. Performance is a separate question, best answered on the target hardware with the intended workload. [2] [3] [4] [5]
Which Code Actually Performs the Encryption?
C++ developers can use Botan, a cryptography library written in C++ that supports encryption, digital signatures, key exchange, and hardware security modules. They can also call OpenSSL’s EVP interface. Fizz, Meta’s C++ implementation of TLS 1.3, illustrates how these layers fit together. Fizz implements the protocol and delegates many cryptographic operations to OpenSSL or libsodium. Rust offers a similar choice. RustCrypto provides implementations of cryptographic algorithms, while the rustls TLS library can delegate those operations to a provider such as aws-lc-rs, which uses AWS-LC underneath. [6] [7] [8] [9] [10] [11]
A C++ application and a Rust application can even call the same OpenSSL binary. With the same configuration and execution path, the processor executes the same cryptographic instructions inside the library. One application may still finish sooner. The other might copy the message twice, allocate a fresh buffer for every request, or initialize a cipher context for every message. Before attributing a performance difference to either language, ask which part of the program the benchmark timed. [12] [8] [4]
What Can the Language Catch Before the Program Runs?
Imagine a server that receives an encrypted message and passes a view of its receive buffer to a task scheduled to run later. What happens if the server releases the buffer before the task reads it?
In safe Rust, the compiler rejects a reference that could outlive the data it points to. Safe Rust also rules out data races on shared memory, provided that the libraries, their unsafe internals, and any foreign code they call uphold their safety contracts. That guarantee matters in the code that receives network traffic and handles keys. Every time a developer changes how a task borrows a buffer, the compiler checks the lifetime relationship again. [2] [13] [14]
In modern C++, the team can solve the same problem by transferring ownership of the buffer to the task. RAII ties the release of the buffer to the lifetime of its owner. A small change to the interface, however, changes the guarantee. A std::span tells the task where the bytes are and how many it can access. The span does not keep the buffer alive. Both languages can express the correct ownership model, but Rust enforces more of its conditions at compile time. [3] [2]
A Cryptographic Mistake Either Program Can Make
Consider a service that uses a counter to generate a unique nonce for each message encrypted with AES-GCM. The service restarts, the counter resets to zero, and the old key remains in use. Every memory access may be valid, yet the service can now repeat a nonce under the same key. As RFC 5116 explains, reusing a nonce to encrypt two different messages compromises their confidentiality and undermines the authenticity and integrity guarantees for that key. [1]
Rust’s ring library illustrates exactly how far a type system can help. Its Nonce type is consumed when used and deliberately does not implement Clone. This prevents a caller from using that same object twice. After a restart, however, the application can still construct a new object containing the old value. Preserving nonce uniqueness across the lifetime of a key, including crash recovery, is a responsibility of the system design. [15] [1]
A well-designed C++ interface can enforce another important rule by withholding decrypted bytes from the rest of the application until authentication succeeds. OpenSSL’s streaming decryption interface can produce bytes before the final tag check. If the application interprets those bytes as a command and executes it immediately, a subsequent authentication failure comes too late. The point at which an interface releases plaintext is a security decision. [8]
Side Channels and Constant-Time Execution
Two implementations can produce the correct result for every message while one leaks information about the key through execution time or memory access patterns. In cryptography, constant-time code avoids branches, memory addresses, and instruction timings that depend on secret values. The total time to process a request can still vary with public factors such as message length or system load. [16] [9]
RustCrypto documents a specific hardware assumption for the portable software path in its aes-gcm crate. Its constant-time guarantee depends on a processor whose multiplication time is independent of the operands. Botan likewise documents how it selects hardware AES instructions or alternative software implementations according to the processor’s capabilities. For an embedded engineer, the practical question is which code path will run on the target chip. Passing every functional test on a desktop establishes that the answers were correct there. Resistance to leakage on the device requires its own assessment. [9] [16]
What Do the Benchmarks Show?
On March 7, 2026, the rustls team published measurements from a bare-metal server running Debian 12 on an Intel Xeon E-2386G. The table shows two results from the same TLS 1.3 transfer benchmark using AES-256-GCM. Throughput is in megabytes per second, rounded to the nearest whole number. [5]
| Operation | BoringSSL | OpenSSL 3.6.1 | rustls 0.23.37 with aws-lc-rs |
|---|---|---|---|
| Sending | 7,565 | 5,844 | 7,421 |
| Receiving | 6,218 | 6,238 | 7,333 |
BoringSSL led on sends. Rustls led on receives. Changing the direction of transfer changed the ranking on the same processor with the same cipher. The rustls team measured complete TLS stacks with different protocol implementations and cryptographic providers. These figures tell us how those stacks performed under the published conditions. They cannot isolate the contribution of either application language. [5]
For an engineering decision, I would run two tests. First, I would link the C++ and Rust applications to the same library binary and measure both small-message latency and bulk throughput. I would account for key setup, tag generation, and tag verification consistently, and record allocations and buffer copies. Then I would test each complete service with the libraries, settings, and workload intended for production. The first test helps isolate application overhead. The second tells the team which deployable system serves its users better. [5] [17]
Which Would I Choose?
For a new network service that processes untrusted input, I would favor Rust, assuming good library and platform support. Over the service’s lifetime, developers will repeatedly change its message parsers, buffers, and concurrent tasks. Rust’s ownership and borrowing checks apply to every one of those revisions. [2] [13]
For a mature C++ product whose team has already tested its hardware security modules through an interface such as PKCS#11, I would keep the production-proven component as long as it meets the requirements. Rust can still be introduced where it delivers a specific benefit, such as a new parser at the boundary where untrusted input enters the system. [18] [14]
The same reasoning applies to post-quantum cryptography. The ML-KEM standard specifies a mechanism for establishing a shared secret, and both Botan and aws-lc-rs expose it. If the project requires FIPS 140-3 validation, check the cryptographic module’s certificate, its version, and its operating environment. The most useful question for the team is this: Where, precisely, could our system expose a secret, and which tools will help us keep it safe through years of changes? [19] [20] [21] [22]
References
- RFC 5116 — Authenticated Encryption and the Requirements for GCM
- The Rust Programming Language — References and Borrowing
- C++ Core Guidelines — Resource Management and Lifetimes
- Botan — Hardware Acceleration
- rustls Performance Report of March 7, 2026 — Methodology and Results
- Botan Cryptography Library
- Fizz — Meta’s C++ Implementation of TLS 1.3
- OpenSSL EVP — Encryption, Decryption, and Authentication Tag Verification
- RustCrypto — AES-GCM and Its Constant-Time Requirements
- rustls and Its Cryptographic Providers
- AWS Libcrypto for Rust User Guide
- OpenSSL Bindings for Rust
- The Rustonomicon — Data Races and Race Conditions
- The Rustonomicon — Working with Unsafe Rust
- ring — The Nonce Type
- Botan — Side Channels
- OpenSSL — Measuring Library Performance
- Botan — PKCS#11
- NIST FIPS 203 — Module-Lattice-Based Key-Encapsulation Mechanism Standard
- Botan — Public-Key Cryptography and Key Encapsulation
- aws-lc-rs — Key-Encapsulation Mechanisms
- NIST — Cryptographic Module Validation Program
What did you think?
Sign in to react or comment.
Comments
0No comments yet.