Cryptographic Hardware Implementations
Low-Level Authentication & Crypto Hardware · Security Fundamentals track
7 min read
In this module
Crypto in Silicon
Software crypto works, but it carries two stubborn problems: speed and side channels. Hardware crypto instructions answer both. Bake the primitive directly into a CPU execution unit and you get orders of magnitude more throughput, with whole categories of side-channel attack ruled out by construction.
AES-NI: AES in Hardware
Intel's AES-NI (AES New Instructions) provides hardware acceleration for the AES block cipher. The key instructions are:
- AESENC: Perform one round of AES encryption
- AESENCLAST: Perform the final round of AES encryption
- AESDEC / AESDECLAST: Corresponding decryption rounds
- AESKEYGENASSIST: Assist in expanding the cipher key into round keys
AES-128 takes ten rounds. With AES-NI, each round shrinks down to a single instruction working on 128-bit XMM registers:
; AES-128 encryption with AES-NImovdqu xmm0, [plaintext] ; Load 128-bit plaintext blockpxor xmm0, xmm1 ; XOR with round key 0 (whitening)aesenc xmm0, xmm2 ; Round 1aesenc xmm0, xmm3 ; Round 2; ... rounds 3-9 ...aesenclast xmm0, xmm11 ; Final round (round 10)movdqu [ciphertext], xmm0 ; Store resultThe security payoff is the real story: every AES-NI op runs in fixed time on dedicated circuits. No table lookups, no cache accesses tied to key material, no branches that bend with the data. The whole cache-timing attack family — the one researchers used to lift AES keys out of software implementations — simply has nothing to grip onto.
AES-NI hits roughly one cycle per byte on modern Intel chips — over 10x faster than optimised software AES. Full-disk encryption, TLS, VPNs all run with overhead that barely shows up on a profile. Before AES-NI shipped, encryption could chew 20-30% of CPU time on network-heavy workloads.
SHA Extensions
Similar to AES-NI, Intel and ARM provide hardware instructions for SHA hash functions:
- Intel SHA Extensions:
SHA1MSG1,SHA1MSG2,SHA1NEXTE,SHA1RNDS4for SHA-1, andSHA256MSG1,SHA256MSG2,SHA256RNDS2for SHA-256 - ARM SHA Extensions:
SHA1C,SHA1H,SHA1M,SHA1P,SHA1SU0,SHA1SU1and corresponding SHA-256 instructions
These instructions speed up hashing wherever it's used: digital signatures, MACs, key derivation, integrity checks. Like AES-NI, they run in constant time.
Hardware Random Number Generation
All of cryptography rides on unpredictable random numbers — keys, nonces, IVs, protocol randomness. Hardware RNGs pull entropy from physical sources software has no way to model.
Intel RDRAND hands out numbers from a hardware RNG that passes NIST SP 800-90A compliance testing. Internally, it's a DRNG seeded from an on-chip entropy source riding thermal noise.
Intel RDSEED exposes the raw entropy source itself, meant for seeding software PRNGs rather than for direct use. RDSEED can fail (carry flag clear) if entropy is momentarily exhausted, while RDRAND keeps a larger buffer and rarely runs dry.
; Generate a 64-bit random numberrdrand rax ; Random number in RAX, CF=1 if validjnc .retry ; Retry if entropy depleted (rare)That fragment hides where .retry goes and when to stop. Intel's DRNG guide recommends retrying RDRAND up to 10 times and treating ten failures in a row as a broken generator. A complete loop looks like this:
get_random: mov ecx, 10 ; attempts left.retry: rdrand rax ; CF=1: RAX holds a random value jc .done ; CF=0: nothing ready, and RAX has been zeroed dec ecx jnz .retry jmp rng_failure ; ten misses in a row: give up loudly.done: retHere's one run where the first attempt comes back empty. The random value is made up for illustration:
| Step | Instruction | CF | RAX | ECX | What happens |
|---|---|---|---|---|---|
| 1 | mov ecx, 10 | – | – | 10 | Ten attempts allowed |
| 2 | rdrand rax | 0 | 0x0000000000000000 | 10 | No value ready; RAX is cleared |
| 3 | jc .done | 0 | 0 | 10 | Not taken |
| 4 | dec ecx / jnz .retry | 0 | 0 | 9 | Back for another try |
| 5 | rdrand rax | 1 | 0x8c41f2d07a39e655 | 9 | Valid value |
| 6 | jc .done | 1 | 0x8c41... | 9 | Taken; return the value |
Step 2 is the trap. Code that skips the jc check carries on with RAX = 0 and quietly uses an all-zero "random" key. The flag isn't a complete safety net either: in 2019 some AMD Ryzen 3000 systems got all ones back from RDRAND, with CF=1, on every call until a firmware fix arrived, which is one more reason to mix hardware output with other entropy.
ARM RNDR (ARMv8.5-A) gives you the same shape on ARM, reading from a hardware random number generator.
The catch with hardware RNGs is trust. You have to take the manufacturer's word for the implementation, and the RDRAND debate hammered that point hard — a backdoored RDRAND could quietly hand out predictable output. Best practice is to blend hardware entropy with software entropy sources (interrupt timing jitter, for instance), so compromising either one alone gets an attacker nowhere.
Constant-Time Programming
Hardware crypto instructions only cover the inner loop. Everything wrapped around them still has to be written carefully — any operation whose runtime bends with secret data is a leak. Constant-time programming is the discipline of ruling out that bend, everywhere it might hide.
Avoid secret-dependent branches:
// INSECURE: branch depends on secretif (secret_byte & 0x01) { result = table_a[index];} else { result = table_b[index];}
// SECURE: constant-time conditional selectuint8_t mask = -(secret_byte & 0x01); // 0xFF if bit set, 0x00 if notresult = (table_a[index] & mask) | (table_b[index] & ~mask);Avoid secret-dependent memory access:
// INSECURE: table index depends on secret (cache timing leak)result = lookup_table[secret_byte];
// SECURE: access every table entry, select the right oneresult = 0;for (int i = 0; i < 256; i++) { uint8_t mask = constant_time_eq(i, secret_byte); result |= lookup_table[i] & mask;}Avoid secret-dependent loop counts:
// INSECURE: loop count depends on secret key lengthfor (int i = 0; i < key_length; i++) { ... }
// SECURE: always iterate maximum length, mask operationsfor (int i = 0; i < MAX_KEY_LENGTH; i++) { uint8_t active = constant_time_lt(i, key_length); // Perform operation only if active, but always execute the loop body}Cache-Timing Resistant Code
Cache timing attacks lean on a basic fact: hitting a cache line is faster than hitting main memory. If a crypto operation reaches for different cache lines depending on the secret key, anyone who shares that cache — a noisy neighbour VM in cloud, for instance — can watch the access pattern and reconstruct the key.
Hardware crypto instructions sidestep the whole problem because they don't make data-dependent memory accesses. Plenty of crypto outside AES and SHA still lives in software, though. There, techniques like bit-slicing — coding the algorithm with bitwise ops on registers and zero memory lookups — give you cache-timing resistance, at the cost of code that's a beast to read.
C has no concept of "constant-time". A compiler is free to take your branchless conditional select and helpfully turn it into a conditional branch because the latter is faster. Projects like libsodium and BoringSSL pile on compiler barriers, volatile accesses, and hand-rolled assembly to keep the compiler from quietly undoing their constant-time guarantees.