Zen and the Art of Microcode Hacking
AMD Zen CPUs verified microcode patch signatures using AES-CMAC as a hash function, and the CMAC key was a well-known NIST example key reused across many CPU generations. An attacker who knew that key could forge a public key that collided with AMD's expected hash and then sign arbitrary microcode patches, allowing custom microcode to be loaded onto the CPU.
bughunters.google.com
Published on bughunters.google.com
Today we are releasing the full details of EntrySign, the AMD Zen microcode signature validation vulnerability which we initially disclosed last month.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
EntrySign is the name given to a vulnerability in the microcode signature validation of AMD Zen CPUs. The research was first disclosed in a security advisory and this post publishes the full details. The target is not a web application: it is the microcode update mechanism of AMD Zen 1 through Zen 4 processors, and the asset involved is the microcode patch format and the routine that verifies it before loading it into on-die patch RAM.
Modern x86 CPUs are complex instruction set computer (CISC) cores that internally use a reduced instruction set computer (RISC) engine, called the microcode engine, to implement complicated instructions and architectural transitions. Intel and AMD each designed their own undocumented RISC-like microcode instruction sets. Because hardware bugs cannot be fixed by shipping new silicon cheaply, x86 vendors created a mechanism to update microcode at runtime: available from AMD K8 in 2003 and Intel P6 in 1995. The microcode instructions and data live in an on-die ROM with additional on-die patch SRAM. That patch RAM can be loaded with new instructions during a microcode update, typically at BIOS and OS boot, and cannot be accessed directly from the x86 core. The only documented way to change it is to load an authentic microcode update from AMD, verified by a cryptographic signature.

Fig. 1. The EntrySign vulnerability logo
In a Zen core, once the current instruction pointer is known, the CPU checks the micro-op cache; on a hit the micro-ops go straight to the queue for dispatch. Otherwise the instruction cache returns the bytes of the x86 macro instruction and the decoder decides whether it is a fastpath instruction, implemented with a hard-coded set of micro-ops, or a microcoded instruction, which requires the microcode engine to emit a variable length set of micro-ops. The patch RAM sits next to the microcode ROM. The emitted micro-ops are pushed into the micro-op queue and sent to the dispatcher.

Fig. 2. A simplified view of AMD’s Zen architecture highlighting where the microcode engine might be
Setup
The work required reverse engineering the microcode update process itself, since the format, the instruction semantics and the verification algorithm are undocumented. The key used by an old Zen 1 CPU turned out to be the example key from NIST SP 800-38B. and they released a tool suite, zentool, to examine, author, sign and load microcode patches. The proof-of-concept patch described later hooks the RDRAND instruction. Reproducing it involves cloning and building zentool, creating a donor patch, editing, resigning and loading it with sudo.
Finding it
AMD microcode patches are protected in two ways: encryption to prevent reverse engineering of the patches, and digital signatures to prevent loading of inauthentic patches. The verification and loading process has four steps.
I) Authorship. AMD generates a new microcode patch and delivers it as a binary blob to BIOS, OS and other partners. The blob contains:
- a header describing the patch header format, metadata about the target CPU, date and versioning information;
- a 2048-bit RSA PKCS #1 signature;
- a 2048-bit RSA public key modulus, with 0x10001 as the exponent;
- a 2048-bit Montgomery inverse of the public key, used to simplify RSA modular operations;
- a bit indicating whether the remainder of the patch is encrypted;
- an array of match registers and mask values selecting which microcode and instructions to patch;
- an array of micro-ops bundled in sets of four quads, each paired with a sequence word indicating where to execute next.

Fig. 3. An example of a microcode patch file
II) Authentication. AMD signs the patch with the RSA private key corresponding to the public key embedded in the patch. During verification the CPU hashes the RSA public key and checks it against the hash of AMD’s public key fused into the chip at manufacturing, so that only the original key pair can sign patches.
III) Delivery. AMD ships the patch to OEMs, OS platforms and other partners for validation and distribution.
IV) Verification and installation. The patch is loaded at runtime or at the next reboot:
- The BIOS or OS identifies the correct patch file from the CPU identifiers in the header and starts the update routine.
- The software writes the virtual address of the patch blob to MSR 0xc0010020, which tells the microcode to start executing the update routine.
- The microcode copies the patch to internal memory and verifies that the patch’s CPU identifiers match the hardware.
- The microcode checks whether the patch version is older than the installed one; if so, the patch is rejected, which prevents rollback attacks.
- The patch hashes the RSA public key using AES-CMAC and confirms the hash matches the value fused in by AMD.
- The patch hashes the patch contents: match registers, instruction masks and patch instructions.
- The RSA PKCS #1 signature is decrypted using the RSA public key and the supplied Montgomery modular inverse. The result is a padded AES-CMAC hash of the patch contents.
- If the signed hash matches the calculated hash, the patch contents are copied into internal patch RAM; otherwise the patch is rejected.
- The CPU microcode patch version in MSR 0x8b is updated.
The signature scheme is an almost standard RSASSA-PKCS1-v1_5, except that instead of one of the recommended hash functions it uses an alternative that is prone to collisions. Standard RSA signatures hash the message with something like SHA-256, pad the hash with a constant and a hash function identifier, and sign the padded value; the recipient encrypts the signature with the public key and compares the result with the padded hash it computes. Faking a signature therefore requires breaking RSA or breaking the hash. Storing a hash of the public key instead of the key itself is common where storage is scarce, such as mask ROM or fuses: AMD uses a 2048-bit RSA key but stores a 128-bit hash on-die. That is safe only as long as the hash function is secure.
A hash function takes an arbitrarily long input and produces a fixed output. In the example below, two 6-bit strings, split into blocks of 3 bits, each produce a different 3-bit output.

Fig. 4. A hash function takes arbitrarily long input and produces fixed-length output
A secure hash function ensures that, given an output, it is not feasible to reverse the operation and calculate the input, which is called a preimage. Given the output 101, it should be impossible to know that 010111 hashes to it without trying inputs until one matches by chance. In the toy example the hash is only 3 bits, so a few tries (up to eight in the worst case) find a preimage; a real hash has an output of at least 128 bits, which makes this too expensive.
Exploitation
Step 1: recognise that CMAC is not a hash
The root cause is that the Zen verification algorithm uses CMAC as a hash function, but CMAC is a message authentication code and does not provide the same guarantees as a cryptographic hash. To compute the 3-bit CMAC of the 6-bit message 000111, the message is split into blocks, here 000 and 111.

Fig. 5. An example of how the CMAC algorithm works
CMAC uses an encryption algorithm, in this case AES, and chains the output with the next block using XOR. The diagram omits a final step for simplicity. Any modification to the input, for example changing 000111 into 010111, produces an unpredictable change in the output, from 110 to 101.

Fig. 6. If the input changes, the CMAC algorithm generates an unpredictable change to the output
The weakness is that anyone who has the encryption key can observe the intermediate values of the encryption and calculate a way to correct the difference so that the final output stays the same even when the inputs are completely different. Given the prefix 010, changing the second block from 111 to 101 makes the last operation output the same value as for the input 000111. Both inputs hash to 110, and the compensating block is calculated with a single XOR: the input to the last encryption operation must be 100.

Fig. 7. Manipulating CMAC to receive the desired output value
Secure hash functions have no secret key, so knowledge of the intermediate state cannot be used to generate a collision. CMAC was not designed as a hash function and is weak against an adversary who holds the key. Every AMD Zen CPU must hold the same AES-CMAC key to hash the AMD public key and the patch contents, so the key only needs to leak from a single CPU to compromise all CPUs using it. That opens the door to hardware attacks such as reading the key from ROM with a scanning electron microscope, side-channel attacks such as Correlation Power Analysis during validation, or other software and hardware attacks that reveal the key. The researchers state that it is a safe assumption that such a key will not remain secret forever.
Step 2: obtain the key and forge a public key
The key from an old Zen 1 CPU turned out to be the example key from NIST SP 800-38B, Appendix D.1 (2b7e1516 28aed2a6 abf71588 09cf4f3c), and it was reused until at least Zen 4. With this key both uses of AES-CMAC can be broken: the RSA public key and the patch contents. The researchers forged new public keys that generate the same hash as the authentic AMD key, calculated collisions for signatures, and generated a microcode patch sharing the signature of another legitimately signed message.
A collision requires injecting a specific compensating block that looks like random data and must be aligned to 16 bytes. On microcode that can cause crashes depending on where the block lands, so the researchers instead generated preimages of the public key CMAC. With a forged RSA public key they could sign arbitrary microcode patches without working around compensating blocks.
Step 3: find a factorable modulus
To generate preimages of the public key CMAC, the researchers generated candidate RSA public keys that collided with the expected CMAC value and checked whether they could be factored. After a few attempts they found a number with prime factors that were easy to find, the equivalent of p and q in RSA, from which the private key can be generated:
N = [redacted] * 0x61
[redacted]
The sixteen byte value [redacted] is there to compensate and generate the collision. A 2048-bit number was easy to factor because, while an RSA public key is hard to factor, a random odd integer of the same size is very likely to be the product of a few small primes with only one large prime. Factoring can then be done by dividing by small primes to check for a zero residue and testing whether the leftover is likely prime. RSA is usually computed with two large distinct primes, but the mathematics holds for a product of multiple primes.
Step 4: satisfy the Montgomery check
With the forged key the researchers could sign arbitrary patches without injecting compensating blocks on the microcode, as long as the CPU accepted the public key, which consists of the generated modulus and the public exponent 65537, hardcoded for efficiency. One more value was needed. The implementation uses Montgomery modular multiplication to simplify signature verification, so the verification scheme checks one more value before trusting the public key.
Montgomery multiplication requires a value R such that operations modulo R are easy, R is coprime with the modulus N, and R is larger than N. AMD chose R as 2**2048 because modular operations modulo a power of two are easy and the modulus is 2048 bits, and because N is a product of two primes it is guaranteed to be coprime with a power of two. Montgomery reduction also needs the constant N' (or N_), defined as:

Fig. 8. Definition of the constant N’ (or N_)
Putting it together:
R = 2**2048
N_inv = gmpy2. invert (N, R)
N_ = (N_inv * (R - 1)) % R
bin((N_ * N) % R) = 0b111...111
The verification routine checks that the bottom 2048 bits of N * N_ are all ones, equivalent to -1, and returns an error otherwise. With this value, multiplications can be done with Montgomery modular multiplication, likely also important because of space constraints in the microcode ROM.
Step 5: understand the microcode engine
Before writing arbitrary microcode, the researchers examined how AMD microcode is implemented and executed. Prior work is limited: the 1998 book The anatomy of a high-performance microprocessor covers AMD’s K6 RISC86 architecture, and Koppe et al. published Reverse Engineering x86 Processor Microcode in 2017 documenting work against AMD K8 and K10 CPUs from the mid 2000s. To the researchers’ knowledge, the current AMD microcode design stems from the NexGen x86 CPUs of the mid 1990s, which used an internal RISC-like architecture called RISC86. AMD acquired NexGen in 1996 and launched the AMD K6 using that technology. Many details have changed, but the fundamental design of Zen microcode shares similarities with the original NexGen design.

Fig. 9. Microcode engine design
When the frontend decodes the next x86 instruction it decides whether the instruction is microcoded or takes a fast path by directly emitting micro-ops. Many simple instructions decode into a small number of micro-ops passed to the backend and cached in the micro-op cache, so the decode step can be skipped next time. For more complex instructions and system operations, control passes to the microcode engine, which emits a series of micro-ops. Circuitry determines the entry point in the microcode ROM for the current x86 instruction, for example RDRAND => 0x0543, and the engine begins there. The ROM holds fixed length 64-bit microcode instructions (micro-ops) bundled into sets of four quads and terminated with a sequence word, a 32-bit command word that tells the engine what to do next. The engine uses branch delay slots for these sequence words, so a specified branch does not occur until after execution of the next quad.
The specification, format and semantics of microcode instructions and sequence words are undocumented. The researchers spent a lot of time reverse engineering them, both before and after gaining code execution, and much work remains; some bits may never be fully understood. The disassembly and assembly in zentool are therefore not always accurate.
As an example, the disassembly of the microcode instructions related to the Retbleed patch is shown below. This is one of the few patches where AMD published details of what it does:
AMD "Zen 2" CPUs support a configuration bit in MSR C001_10E3 (DE_CFG2) which changes the behaviour of the decode block when the processor attempts to predict a branch on a non-branch instruction (BTC-NOBR case). When bit 1 (SuppressBPOnNonBr) is set the branch prediction information on non-branch instructions is ignored and no speculation at the predicted target will be observed. Setting this bit mitigates the risk of potential information disclosure as a result of speculation in the BTC-NOBR case.
Some systems with recent microcode updates installed may already have this MSR bit set to 1.

Fig. 10. A disassembled view of the microcode patch released by AMD to mitigate the Retbleed vulnerability
For this patch the researchers believe it executes on completion of the microcode update routine itself and so does not hook a specific instruction or ROM address. The first micro-op reads memory from offset 0x264 in segment 5, an internal microcode memory region, into temporary register t8. The second does a bitwise OR of t8 with 0x0002. The third writes t8 back to segment 5 at offset 0x264. The last is a no-op. The sequence word is not fully understood. Together the sequence appears to mimic AMD’s bulletin by setting the SuppressBPOnNonBr bit in the DE_CFG2 MSR, implemented in microcode memory, when loaded.
A more complex sequence in the same patch is shown below. ROM address 0x111A is hooked, and the researchers do not know what was originally implemented there. The first basic block appears to perform a bitwise AND on a temporary register and conditionally branch on the result. Without access to the original ROM it is difficult to know what the register is expected to contain. The conditional branch jumps to another patch quad at 0x1FCD; otherwise the remaining two instructions execute before falling through to the same second quad. The other conditional branch jumps to ROM address 0x041E, with a follow-on connection to 0x042A based on the original sequence word (0x12042A) and the delay slot branch mechanics.

Fig. 11. A more complex example of a sequence in a microcode patch released by AMD
Step 6: author, sign and load a patch with zentool
zentool is a collection of tools in a single project with the long-term goal of a binutils-like suite for AMD microcode. It supports limited disassembly, patch authoring with a limited amount of reverse engineered assembly, patch signing and patch loading. Details on decrypting and encrypting patches are planned for the future. The proof-of-concept patch hooks RDRAND so that it always returns 4, or sometimes 5 as one observer noted.
-
Clone the repository and build the tools. A donor microcode patch is needed: take an official patch for the current CPU and strip out all of the original patch contents.
Bash $ git clone https://github.com/google/security-research.git $ cd security-research/pocs/cpus/entrysign/zentool $ make all template.bin -
Replace the donor patch contents with the changes that replace RDRAND’s implementation.
Bash $ ./zentool -o rdrand.bin edit --match 0=@rdrand --seq 0=0x100002 --insn q0i0="mov.qs rax,rax,4" template.binThe arguments:
--o rdrand.binsaves the result into a new file.editselects the subcommand that modifies the patch contents.--match 0=@rdrandchanges match register 0 to point to the entry point of RDRAND in the microcode ROM. Match registers direct the microcode sequencer to execute instructions in patch RAM instead of ROM. Only a limited number of instruction-to-ROM-address associations have been identified; seematchscan.sh.--seq 0=0x100002sets the sequence word for the first two instruction quads to the value 7, instructing the sequencer to return to the next x86 instruction after these quads execute.--insn q0i0="mov.qs rax,rax,4"places the instruction at quad 0, slot 0. Themovstores 4 in RAX; theqsuffix means a 64-bit QWORD and thessuffix means RFLAGS should be set based on the operation.
The example is simplified to only set RAX and does not set the carry flag as a successful RDRAND execution would.
-
Sign the modified patch with the fake RSA key. CPUs without the EntrySign fix cannot tell it apart from an authentic AMD patch.
Bash $ ./zentool resign rdrand.bin -
Load the patch on a specific CPU using the standard WRMSR technique.
Bash $ sudo ./zentool load --cpu=2 rdrand.binA simple program that executes RDRAND shows that the instruction always returns 4.
C #include <stdbool.h> #include <stdio.h> int main(void) { unsigned long r; bool ok = false; asm volatile( "rdrand %0\n\t" "setc %1" : "=a"(r), "+r"(ok)); printf("ok? %u, val %#lx\n", ok, r); }The
tasksetprogram can force execution on the patched core, and theisolcpuskernel command line option prevents other jobs from running on it.Bash $ gcc rdrand.c -o rdrand $ taskset -c 0 ./rdrand ok? 1, val 0xda8cb8920d9e381c $ taskset -c 2 ./rdrand ok? 0, val 0x4The output shows ok? 1 with a random value on core 0 and ok? 0 with val 0x4 on core 2.
Root cause
The verification algorithm uses AES-CMAC as the hash function for both the RSA public key and the patch contents. CMAC is a keyed MAC, and an adversary who knows the key can compute compensating blocks that keep the final output unchanged while changing the input. Because every Zen CPU must hold the same key, a single leak compromises all CPUs using it, and the key turned out to be the public NIST SP 800-38B example key, reused from Zen 1 until at least Zen 4. The design also stores only a 128-bit hash of the public key on-die, which is safe only if the hash is secure. The RSA PKCS #1 signature, the fused key hash, the version check and the CPU identifier check did not stop the forgery. The Montgomery check on N * N_ was satisfied by computing the matching N_ for the forged modulus.
Impact
An attacker who can install a microcode patch can change the behaviour of instructions on the CPU. The researchers demonstrated this by making RDRAND return 4. The security impact is limited by two conditions: an attacker must first obtain host ring 0 access to attempt to install a patch, and patches do not persist through a power cycle. Threat models where an attacker is permitted to subvert microcode patches include confidential computing with SEV-SNP, DRTM with SKINIT, and supply chain modification. The vulnerability affects all unpatched Zen 1 through Zen 4 CPUs.
Fix
AMD’s fix modifies the microcode validation routine to use a custom secure hash function. It is paired with an AMD Secure Processor update that ensures the patch validation routine is updated before the x86 cores can attempt to install a tampered microcode patch. The researchers examined the patch and believe it remediates the signature verification vulnerability. Further details on how the update process was reverse engineered, how the validation algorithms were identified, how the CMAC key was extracted and some file format details are planned for upcoming months.
Variants to test
- Check whether a firmware or CPU vendor uses a MAC such as CMAC or HMAC with a shared key in place of a hash inside a signature scheme. A positive result is a verification path where knowledge of the key lets you produce two different inputs with the same stored digest.
- Look for published example keys, such as the NIST SP 800-38B test vector, reused in production firmware. A positive result is a device whose keyed hash matches a value computed with the published example key.
- Test whether a stored hash of a public key is checked with a weak or keyed function. A positive result is a forged public key that passes the on-die check.
- Test whether the Montgomery constant check can be satisfied for an arbitrary modulus by computing N_ as shown above. A positive result is a forged key that passes verification without an error.
- Test whether microcode or firmware patches can be loaded from an unprivileged context or survive a reboot. A positive result is a patch that persists or is installable below ring 0.
What happened, in plain words
Every modern AMD processor (the chip that does the actual computing in your computer) contains a tiny internal program called microcode. This program is stored inside the chip itself and tells the processor how to carry out complicated instructions. Sometimes the microcode has a bug, and the only practical way to fix it is to send the processor an update, a microcode patch, that replaces part of the internal program. Because a patch can change how the processor behaves, it must be protected: otherwise anyone could slip the processor a fake patch and make it do whatever they wanted.
AMD protects patches with a digital signature, a mathematical stamp that proves the patch really came from AMD. The vulnerability described here, called EntrySign, is a flaw in the way AMD processors check that stamp. The key used by an old Zen 1 processor turned out to be the example key from the NIST SP 800-38B publication. With that secret they could create their own patches, sign them so the processor accepted them as genuine AMD patches, and load them. They demonstrated this by changing the behaviour of a processor instruction called RDRAND so that it always returned the number 4.
The research was first disclosed in a security advisory, and this post publishes the full details. The affected products are AMD Zen 1 through Zen 4 processors that have not received AMD's fix. The asset involved is the microcode patch format and the routine that checks a patch before loading it into the chip's internal patch memory.

Fig. 1. The EntrySign vulnerability logo
A quick tour of the processor
A modern x86 processor is a complex instruction set computer (CISC) core. That means it understands a large, rich set of instructions, some of them quite complicated. Inside, though, it actually works with a much simpler set of small operations, in the style of a reduced instruction set computer (RISC). The part that translates the complicated instructions into these small operations is called the microcode engine. Intel and AMD each designed their own microcode instruction set, and neither company documents it publicly.
Why does this matter for security? Because hardware bugs cannot be fixed cheaply by manufacturing new chips. So x86 vendors created a way to update the microcode while the computer is running: AMD introduced it with the K8 in 2003, Intel with the P6 in 1995. The microcode instructions and data live in a read-only memory (ROM) built into the chip, plus a small writable memory called patch SRAM. During a microcode update, typically when the BIOS or the operating system boots, new instructions can be loaded into that patch memory. The processor core cannot read or write it directly. The only documented way to change it is to load an authentic microcode update from AMD, and the processor checks a cryptographic signature before accepting it.

Fig. 2. A simplified view of AMD’s Zen architecture highlighting where the microcode engine might be
Here is how a Zen core handles an instruction. Once the processor knows the address of the next instruction (the instruction pointer), it first checks a small cache called the micro-op cache. If the small operations for that instruction are already there (a hit), they go straight into a queue to be dispatched to the part of the processor that executes them. If not, the instruction cache returns the bytes of the original x86 instruction, and the decoder decides what kind of instruction it is. A fastpath instruction is implemented with a fixed set of small operations. A microcoded instruction needs the microcode engine to produce a variable-length series of small operations. The patch memory sits right next to the microcode ROM. Either way, the small operations end up in the micro-op queue and are sent to the dispatcher.
Setup
To do this work the researchers had to reverse engineer the microcode update process itself, because the file format, the meaning of the microcode instructions and the verification algorithm are all undocumented. The key used by an old Zen 1 processor turned out to be the example key from NIST SP 800-38B. and they released a set of tools called zentool to examine, write, sign and load microcode patches. The demonstration patch described later changes the RDRAND instruction. Reproducing it involves cloning and building zentool, creating a donor patch, editing, resigning and loading it with sudo.
Finding it
AMD protects microcode patches in two ways: encryption, so that people cannot study the patches, and digital signatures, so that nobody can load a patch that did not come from AMD. The verification and loading process has four steps.
I) Authorship. AMD creates a new microcode patch and delivers it as a binary file (a blob) to BIOS makers, operating system vendors and other partners. The blob contains:
- a header describing the patch header format, metadata about the target processor, the date and version information;
- a 2048-bit RSA PKCS #1 signature;
- a 2048-bit RSA public key modulus, with 0x10001 as the exponent;
- a 2048-bit Montgomery inverse of the public key, used to simplify the RSA modular arithmetic;
- a bit indicating whether the rest of the patch is encrypted;
- an array of match registers and mask values that select which microcode and which instructions to patch;
- an array of small operations bundled in sets of four, called quads, each paired with a sequence word that says where to execute next.

Fig. 3. An example of a microcode patch file
II) Authentication. AMD signs the patch with the RSA private key that matches the public key embedded in the patch. During verification the processor hashes the RSA public key and compares it with the hash of AMD's public key that was burned into the chip when it was manufactured. This is meant to ensure that only the original key pair can sign patches.
III) Delivery. AMD ships the patch to hardware manufacturers, operating system platforms and other partners, who validate and distribute it.
IV) Verification and installation. The patch is loaded while the system is running or at the next reboot:
- The BIOS or operating system picks the correct patch file based on the processor identifiers in the header and starts the update routine.
- The software writes the memory address of the patch blob into a special processor register, MSR 0xc0010020. This tells the microcode to start running the update routine.
- The microcode copies the patch into internal memory and checks that the processor identifiers in the patch match the actual hardware.
- The microcode checks whether the patch version is older than the version already installed. If it is, the patch is rejected, which prevents an attacker from putting an old, vulnerable version back (a rollback attack).
- The patch hashes the RSA public key using AES-CMAC and confirms that the hash matches the value AMD burned into the chip.
- The patch hashes the patch contents: the match registers, the instruction masks and the patch instructions.
- The RSA PKCS #1 signature is decrypted using the RSA public key and the supplied Montgomery modular inverse. The result is a padded AES-CMAC hash of the patch contents.
- If the signed hash matches the calculated hash, the patch contents are copied into the internal patch memory. Otherwise the patch is rejected.
- The processor's microcode patch version, kept in MSR 0x8b, is updated.
The signature scheme is an almost standard one, called RSASSA-PKCS1-v1_5, except that instead of one of the recommended hash functions it uses an alternative that is prone to collisions. A collision means two different inputs that produce the same output.
In a standard RSA signature, the message is hashed with something like SHA-256, the hash is padded with a constant value and an identifier for the hash function, and the padded value is signed. The recipient uses the public key to decrypt the signature and compares the result with the padded hash it computes itself. Faking a signature therefore requires either breaking RSA or breaking the hash function.
Storing a hash of the public key instead of the key itself is common when storage is scarce, such as in mask ROM or in the one-time-programmable fuses on a chip. AMD uses a 2048-bit RSA key but stores only a 128-bit hash on the chip. That is safe only as long as the hash function is secure.
A hash function takes an input of any length and produces an output of fixed length. In the example below, two 6-bit strings, split into blocks of 3 bits, each produce a different 3-bit output.

Fig. 4. A hash function takes arbitrarily long input and produces fixed-length output
A secure hash function guarantees that, given an output, it is not feasible to work backwards and calculate the input. That property is called preimage resistance. Given the output 101, it should be impossible to know that 010111 hashes to it without trying inputs until one happens to match. In this toy example the hash is only 3 bits, so a few tries (at most eight in the worst case) will find a preimage. A real hash has an output of at least 128 bits, which makes this far too expensive.
Exploitation
Step 1: recognise that CMAC is not a hash
The root cause is that the Zen verification algorithm uses CMAC as if it were a hash function, but CMAC is a message authentication code and does not provide the same guarantees as a cryptographic hash. To compute the 3-bit CMAC of the 6-bit message 000111, the message is split into blocks, here 000 and 111.

Fig. 5. An example of how the CMAC algorithm works
CMAC uses an encryption algorithm, in this case AES, and chains the output with the next block using the XOR operation. (XOR is a simple bit operation: the result is 1 when the two bits differ and 0 when they are the same.) The diagram leaves out a final step for simplicity. Any change to the input, for example turning 000111 into 010111, produces an unpredictable change in the output, from 110 to 101.

Fig. 6. If the input changes, the CMAC algorithm generates an unpredictable change to the output
The weakness is that anyone who has the encryption key can watch the intermediate values of the encryption and calculate a way to correct the difference, so that the final output stays the same even when the inputs are completely different. Given the prefix 010, changing the second block from 111 to 101 makes the last operation output the same value as it would for the input 000111. Both inputs hash to 110, and the compensating block is calculated with a single XOR: the input to the last encryption operation must be 100.

Fig. 7. Manipulating CMAC to receive the desired output value
Secure hash functions have no secret key, so knowing the intermediate state cannot be used to generate a collision. CMAC was not designed as a hash function, and it is weak against an adversary who holds the key. Every AMD Zen processor must hold the same AES-CMAC key in order to hash the AMD public key and the patch contents, so the key only needs to leak from a single processor to compromise every processor that uses it. That opens the door to hardware attacks, such as reading the key out of the ROM with a scanning electron microscope, to side-channel attacks, such as Correlation Power Analysis during validation, or to other software and hardware attacks that reveal the key. The researchers state that it is a safe assumption that such a key will not remain secret forever.
Step 2: obtain the key and forge a public key
The key taken from an old Zen 1 processor turned out to be the example key from the NIST SP 800-38B publication, Appendix D.1 (2b7e1516 28aed2a6 abf71588 09cf4f3c), and it was reused until at least Zen 4. With this key both uses of AES-CMAC can be broken: the one on the RSA public key and the one on the patch contents. The researchers forged new public keys that produce the same hash as the authentic AMD key, calculated collisions for signatures, and generated a microcode patch that shares the signature of another message that was legitimately signed.
A collision requires injecting a specific compensating block that looks like random data and must be aligned to 16 bytes. On microcode that can cause crashes depending on where the block lands, so the researchers instead generated preimages of the public key CMAC. A preimage is an input that produces a chosen output. With a forged RSA public key they could sign arbitrary microcode patches without having to work around compensating blocks.
Step 3: find a factorable modulus
To generate preimages of the public key CMAC, the researchers generated candidate RSA public keys that collided with the expected CMAC value and checked whether they could be factored. Factoring means writing a number as a product of smaller numbers. After a few attempts they found a number whose prime factors were easy to find, the equivalent of p and q in RSA, from which the private key can be generated:
The sixteen byte value [redacted] is there to compensate and generate the collision. That value is there to compensate and generate the collision.
A 2048-bit number was easy to factor because, while an RSA public key is hard to factor, a random odd integer of the same size is very likely to be the product of a few small primes with only one large prime. Factoring can then be done by dividing the number by small primes to check for a zero remainder and testing whether what is left is likely to be prime. RSA is usually computed with two large distinct primes, but the mathematics holds for a product of several primes.
Step 4: satisfy the Montgomery check
With the forged key the researchers could sign arbitrary patches without injecting compensating blocks into the microcode, as long as the processor accepted the public key. That key consists of the generated modulus and the public exponent 65537, which is hardcoded for efficiency. One more value was needed. The implementation uses Montgomery modular multiplication to simplify signature verification, so the verification scheme checks one more value before trusting the public key.
Montgomery multiplication requires a value R such that arithmetic modulo R is easy, R is coprime with the modulus N (they share no common factor other than 1), and R is larger than N. AMD chose R as 2**2048 because arithmetic modulo a power of two is easy and the modulus is 2048 bits, and because N is a product of two primes it is guaranteed to be coprime with a power of two. Montgomery reduction also needs the constant N' (or N_), defined as:

Fig. 8. Definition of the constant N’ (or N_)
Putting it together:
R = 2**2048
N_inv = gmpy2. invert (N, R)
N_ = (N_inv * (R - 1)) % R
bin((N_ * N) % R) = 0b111...111
Line by line: the first line sets R to 2 raised to the power 2048. The verification routine checks that the bottom 2048 bits of N * N_ are all ones, which is equivalent to -1, and returns an error otherwise. With this value, multiplications can be done with Montgomery modular multiplication, which is likely also important because of space constraints in the microcode ROM.
Step 5: understand the microcode engine
Before writing arbitrary microcode, the researchers examined how AMD microcode is implemented and executed. Prior work is limited: the 1998 book The anatomy of a high-performance microprocessor covers AMD's K6 RISC86 architecture, and Koppe et al. published Reverse Engineering x86 Processor Microcode in 2017 documenting work against AMD K8 and K10 processors from the mid 2000s. To the researchers' knowledge, the current AMD microcode design stems from the NexGen x86 processors of the mid 1990s, which used an internal RISC-like architecture called RISC86. AMD acquired NexGen in 1996 and launched the AMD K6 using that technology. Many details have changed, but the fundamental design of Zen microcode shares similarities with the original NexGen design.

Fig. 9. Microcode engine design
When the frontend decodes the next x86 instruction it decides whether the instruction is microcoded or takes a fast path by directly emitting small operations. Many simple instructions decode into a small number of these operations, which are passed to the backend and cached in the micro-op cache, so the decode step can be skipped next time. For more complex instructions and system operations, control passes to the microcode engine, which emits a series of small operations. Circuitry in the processor determines the entry point in the microcode ROM for the current x86 instruction, for example RDRAND => 0x0543, and the engine begins there. The ROM holds fixed-length 64-bit microcode instructions (micro-ops) bundled into sets of four quads and terminated with a sequence word, a 32-bit command word that tells the engine what to do next. The engine uses branch delay slots for these sequence words, so a specified branch does not happen until after the next quad has executed.
The specification, format and meaning of microcode instructions and sequence words are undocumented. The researchers spent a lot of time reverse engineering them, both before and after gaining code execution, and much work remains; some bits may never be fully understood. The disassembly and assembly in zentool are therefore not always accurate.
As an example, the disassembly of the microcode instructions related to the Retbleed patch is shown below. This is one of the few patches where AMD published details of what it does:
AMD "Zen 2" CPUs support a configuration bit in MSR C001_10E3 (DE_CFG2) which changes the behaviour of the decode block when the processor attempts to predict a branch on a non-branch instruction (BTC-NOBR case). When bit 1 (SuppressBPOnNonBr) is set the branch prediction information on non-branch instructions is ignored and no speculation at the predicted target will be observed. Setting this bit mitigates the risk of potential information disclosure as a result of speculation in the BTC-NOBR case.
Some systems with recent microcode updates installed may already have this MSR bit set to 1.

Fig. 10. A disassembled view of the microcode patch released by AMD to mitigate the Retbleed vulnerability
For this patch the researchers believe it runs when the microcode update routine itself finishes, and so does not hook a specific instruction or ROM address. The first micro-op reads memory from offset 0x264 in segment 5, an internal microcode memory region, into a temporary register called t8. The second does a bitwise OR of t8 with 0x0002. The third writes t8 back to segment 5 at offset 0x264. The last is a no-op, an instruction that does nothing. The sequence word is not fully understood. Together the sequence appears to match AMD's bulletin by setting the SuppressBPOnNonBr bit in the DE_CFG2 MSR, which is implemented in microcode memory, when the patch is loaded.
A more complex sequence in the same patch is shown below. ROM address 0x111A is hooked, and the researchers do not know what was originally implemented there. The first basic block appears to perform a bitwise AND on a temporary register and conditionally branch on the result. Without access to the original ROM it is difficult to know what the register is expected to contain. The conditional branch jumps to another patch quad at 0x1FCD; otherwise the remaining two instructions execute before falling through to the same second quad. The other conditional branch jumps to ROM address 0x041E, with a follow-on connection to 0x042A based on the original sequence word (0x12042A) and the delay slot branch mechanics.

Fig. 11. A more complex example of a sequence in a microcode patch released by AMD
Step 6: author, sign and load a patch with zentool
zentool is a collection of tools in a single project, with the long-term goal of a binutils-like suite for AMD microcode. (binutils is the standard collection of tools programmers use to work with ordinary machine code.) It supports limited disassembly, patch authoring with a limited amount of reverse engineered assembly, patch signing and patch loading. Details on decrypting and encrypting patches are planned for the future. The demonstration patch hooks RDRAND so that it always returns 4, or sometimes 5 as one observer noted.
-
Clone the repository and build the tools. A donor microcode patch is needed: take an official patch for the current processor and strip out all of the original patch contents.
Bash $ git clone https://github.com/google/security-research.git $ cd security-research/pocs/cpus/entrysign/zentool $ make all template.bin -
Replace the donor patch contents with the changes that replace RDRAND's implementation.
Bash $ ./zentool -o rdrand.bin edit --match 0=@rdrand --seq 0=0x100002 --insn q0i0="mov.qs rax,rax,4" template.binThe arguments:
--o rdrand.binsaves the result into a new file.editselects the subcommand that modifies the patch contents.--match 0=@rdrandchanges match register 0 to point to the entry point of RDRAND in the microcode ROM. Match registers tell the microcode sequencer when to run instructions from patch memory instead of ROM. Only a limited number of instruction-to-ROM-address associations have been identified; seematchscan.sh.--seq 0=0x100002sets the sequence word for the first two instruction quads to the value 7, telling the sequencer to return to the next x86 instruction after these quads have executed.--insn q0i0="mov.qs rax,rax,4"places the instruction at quad 0, slot 0. Themovstores 4 in the RAX register; theqsuffix means a 64-bit QWORD and thessuffix means RFLAGS should be set based on the operation.
The example is simplified to only set RAX and does not set the carry flag as a successful RDRAND execution would.
-
Sign the modified patch with the fake RSA key. Processors without the EntrySign fix cannot tell it apart from an authentic AMD patch.
Bash $ ./zentool resign rdrand.bin -
Load the patch on a specific processor using the standard WRMSR technique. WRMSR is the processor instruction that writes to a special register (an MSR).
Bash $ sudo ./zentool load --cpu=2 rdrand.binA simple program that executes RDRAND shows that the instruction always returns 4.
C #include <stdbool.h> #include <stdio.h> int main(void) { unsigned long r; bool ok = false; asm volatile( "rdrand %0\n\t" "setc %1" : "=a"(r), "+r"(ok)); printf("ok? %u, val %#lx\n", ok, r); }The
tasksetprogram can force execution on the patched core, and theisolcpuskernel command line option prevents other jobs from running on it.Bash $ gcc rdrand.c -o rdrand $ taskset -c 0 ./rdrand ok? 1, val 0xda8cb8920d9e381c $ taskset -c 2 ./rdrand ok? 0, val 0x4The output shows ok? 1 with a random value on core 0 and ok? 0 with val 0x4 on core 2.
Root cause
The verification algorithm uses AES-CMAC as the hash function for both the RSA public key and the patch contents. CMAC is a keyed MAC, and an adversary who knows the key can compute compensating blocks that keep the final output unchanged while changing the input. Because every Zen processor must hold the same key, a single leak compromises every processor that uses it, and the key turned out to be the public NIST SP 800-38B example key, reused from Zen 1 until at least Zen 4. The design also stores only a 128-bit hash of the public key on the chip, which is safe only if the hash is secure. The RSA PKCS #1 signature, the burned-in key hash, the version check and the processor identifier check did not stop the forgery. The Montgomery check on N * N_ was satisfied by computing the matching N_ for the forged modulus.
Impact
An attacker who can install a microcode patch can change the behaviour of instructions on the processor. The researchers demonstrated this by making RDRAND return 4. The security impact is limited by two conditions: an attacker must first obtain host ring 0 access (the highest privilege level on the machine) to attempt to install a patch, and patches do not persist through a power cycle. Threat models where an attacker is permitted to subvert microcode patches include confidential computing with SEV-SNP, DRTM with SKINIT, and supply chain modification. The vulnerability affects all unpatched Zen 1 through Zen 4 processors.
Fix
AMD's fix modifies the microcode validation routine to use a custom secure hash function. It is paired with an AMD Secure Processor update that ensures the patch validation routine is updated before the x86 cores can attempt to install a tampered microcode patch. The researchers examined the patch and believe it remediates the signature verification vulnerability. Further details on how the update process was reverse engineered, how the validation algorithms were identified, how the CMAC key was extracted and some file format details are planned for upcoming months.
Variants to test
- Check whether a firmware or processor vendor uses a MAC such as CMAC or HMAC with a shared key in place of a hash inside a signature scheme. A positive result is a verification path where knowledge of the key lets you produce two different inputs with the same stored digest.
- Look for published example keys, such as the NIST SP 800-38B test vector, reused in production firmware. A positive result is a device whose keyed hash matches a value computed with the published example key.
- Test whether a stored hash of a public key is checked with a weak or keyed function. A positive result is a forged public key that passes the on-chip check.
- Test whether the Montgomery constant check can be satisfied for an arbitrary modulus by computing N_ as shown above. A positive result is a forged key that passes verification without an error.
- Test whether microcode or firmware patches can be loaded from an unprivileged context or survive a reboot. A positive result is a patch that persists or is installable below ring 0.
Glossary
- AES: Advanced Encryption Standard, a widely used symmetric encryption algorithm.
- AES-CMAC: a message authentication code built from AES; it uses a secret key to produce a short value that proves a message is authentic.
- BIOS: the firmware that starts a computer before the operating system loads.
- Blob: a binary file treated as a single opaque chunk of data.
- Branch delay slot: a design where the instruction after a branch still executes before the branch takes effect.
- CISC: Complex Instruction Set Computer, a processor design with a large set of rich instructions.
- CMAC: Cipher-based Message Authentication Code, a keyed function that produces an authentication tag for a message.
- Collision: two different inputs that produce the same output from a function.
- Coprime: two numbers that share no common factor other than 1.
- Correlation Power Analysis: a side-channel attack that studies a device's power consumption to recover secret keys.
- Decoder: the part of a processor that turns instruction bytes into internal operations.
- Digital signature: a mathematical stamp that proves a message came from the holder of a private key and was not altered.
- Disassembly: turning machine code back into readable instructions.
- Donor patch: an official patch with its contents stripped out, used as a starting point for a custom patch.
- DRTM / SKINIT: Dynamic Root of Trust for Measurement, a way to start a trusted environment; SKINIT is the AMD instruction for it.
- Fastpath instruction: an instruction implemented with a fixed set of internal operations.
- Fuses: one-time-programmable bits on a chip, often used to store permanent values such as key hashes.
- Hash function: a function that turns any input into a fixed-length output, ideally in a way that cannot be reversed.
- HMAC: a keyed message authentication code built from a hash function.
- Instruction cache: a small fast memory holding recently used instruction bytes.
- Instruction pointer: the address of the next instruction to execute.
- Isolcpus: a Linux kernel option that keeps other jobs off specified processor cores.
- MAC: Message Authentication Code, a keyed function that proves a message is authentic.
- Mask ROM: read-only memory whose contents are fixed when the chip is manufactured.
- Match register: a register that tells the microcode sequencer when to use patch memory instead of ROM.
- Micro-op: a small internal operation that a processor actually executes.
- Micro-op cache: a small fast memory holding recently used micro-ops.
- Microcode: the internal program a processor runs to carry out instructions.
- Microcode engine: the part of a processor that emits micro-ops for complex instructions.
- Microcode patch: an update that replaces part of a processor's microcode.
- Modular arithmetic: arithmetic where numbers wrap around at a fixed value, the modulus.
- Modulus: the number at which modular arithmetic wraps around; in RSA, the product of two primes.
- Montgomery modular multiplication: a technique for doing modular multiplication efficiently.
- MSR: Model Specific Register, a special processor register accessed with the RDMSR and WRMSR instructions.
- NexGen / RISC86: a 1990s processor company and its internal RISC-like architecture, later acquired by AMD.
- No-op: an instruction that does nothing.
- Patch SRAM: small writable memory on a chip where microcode patches are installed.
- Preimage: an input that produces a given output from a function.
- Prime factor: a prime number that divides a number exactly.
- Private key / public key: the two halves of an asymmetric key pair; the private key signs, the public key verifies.
- QWORD: a 64-bit value.
- Quad: a set of four micro-ops in the microcode ROM.
- RAX: a general-purpose 64-bit register on x86 processors.
- RDRAND: an x86 instruction that returns a hardware random number.
- Retbleed: a processor vulnerability that AMD mitigated with a microcode patch.
- RFLAGS: the register holding processor status flags such as the carry flag.
- Ring 0: the highest privilege level on x86, where the operating system kernel runs.
- RISC: Reduced Instruction Set Computer, a processor design with a small set of simple instructions.
- Rollback attack: reinstalling an older, vulnerable version of software or firmware.
- RSA: a public-key cryptosystem used for encryption and signatures.
- RSASSA-PKCS1-v1_5: a standard scheme for making RSA signatures.
- Scanning electron microscope: an instrument that can image very small structures, used here to read a chip's ROM.
- SEV-SNP: an AMD confidential computing feature that protects virtual machines.
- Sequence word: a 32-bit command word in microcode that says where to execute next.
- Side-channel attack: an attack that uses physical information such as power use or timing to recover secrets.
- Signature verification: checking that a digital signature is valid for a message and a public key.
- Taskset: a Linux program that pins a process to specific processor cores.
- WRMSR: the x86 instruction that writes to a model specific register.
- XOR: a bit operation whose result is 1 when the two bits differ and 0 when they are the same.
- zentool: the researchers' suite of tools for examining, writing, signing and loading AMD microcode patches.