Secure Boot key generation is the process of creating a set of cryptographic keys—specifically the Platform Key (PK), Key Exchange Keys (KEK), and Signature Database (db/dbx)—to establish a hardware-rooted chain of trust. This achieves a state where the UEFI firmware will only execute bootloaders and kernels that have been digitally signed by a trusted authority, preventing rootkits and unauthorized operating systems from loading during the boot process. To perform this, you need a Linux environment with the efitools and openssl packages installed, and a motherboard that supports "User Mode" or "Custom Mode" for Secure Boot.
What You'll Need
- A computer with UEFI firmware that supports custom key enrollment.
- A live USB or installed version of a Linux distribution (such as Ubuntu, Fedora, or Debian).
- The
opensslpackage for generating the actual RSA keys. - The
efitoolspackage for converting raw keys into the EFI Signature Database (.esl) and signed update (.auth) formats. - A secure backup location (USB drive or encrypted vault) to store the private keys.
Step-by-Step
- Prepare the Environment: Open a terminal and install the necessary toolsets. On Debian-based systems, use
sudo apt install openssl efitools. Ensure you are working in a dedicated directory to avoid mixing up the various key files generated during the process. - Generate the Platform Key (PK): The PK is the master key that controls access to the KEK. Generate the RSA private key first using OpenSSL:
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=My Platform Key/" -keyout PK.key -out PK.crt -nodes -days 3650. This creates a private key (PK.key) and a public certificate (PK.crt). - Generate the Key Exchange Key (KEK): The KEK is used to sign updates to the signature database. Generate it similarly:
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=My Key Exchange Key/" -keyout KEK.key -out KEK.crt -nodes -days 3650. This separates the authority to update the system (KEK) from the authority to own the platform (PK). - Generate the Signature Database Key (db): The db key is what actually signs the bootloaders (like GRUB or the Linux kernel). Generate it using:
openssl req -new -x509 -newkey rsa:2048 -subj "/CN=My Signature Database/" -keyout db.key -out db.crt -nodes -days 3650. - Convert Certificates to EFI Format: UEFI firmware cannot read .crt files directly; they must be converted to EFI Signature Lists (.esl). Use the
cert-to-efi-sig-listcommand from the efitools package:cert-to-efi-sig-list -g PK.crt PK.esl,cert-to-efi-sig-list -g KEK.crt KEK.esl, andcert-to-efi-sig-list -g db.crt db.esl. - Sign the EFI Signature Lists: To import these lists into the firmware, they must be signed by the key above them in the hierarchy. Sign the db list with the KEK:
sign-efi-sig-list -k KEK.key -c KEK.crt db.esl db.auth. Then, sign the KEK list with the PK:sign-efi-sig-list -k PK.key -c PK.crt KEK.esl KEK.auth. Finally, sign the PK list with itself (or leave it as is depending on firmware requirements):sign-efi-sig-list -k PK.key -c PK.crt PK.esl PK.auth. - Enter UEFI Setup: Reboot your computer and enter the BIOS/UEFI settings. Look for the Secure Boot section. Note that labels may differ slightly by version, device, or region. You must change the Secure Boot mode from "Standard" to "Custom" or "User Mode". If the option is greyed out, you may need to "Clear Secure Boot Keys" or "Delete all Secure Boot variables" first to put the firmware into Setup Mode.
- Enroll the Keys: Use the firmware's built-in key management tool to upload the .auth files. Enroll the db.auth first, then the KEK.auth, and finally the PK.auth. Once the PK is enrolled, the firmware will automatically transition from Setup Mode to User Mode, locking the configuration.
- Sign the Bootloader: Now that the hardware trusts your db key, you must sign your actual boot files (e.g.,
vmlinuzorgrubx64.efi) usingsbsign:sbsign --key db.key --cert db.crt --output bootx64.efi bootx64.efi. Replace the original bootloader with this signed version.
Common Mistakes
One of the most frequent errors is failing to back up the private keys (.key files). If you enroll your keys and then lose the private keys, you can no longer sign new kernel updates or bootloaders. This effectively locks you out of your own system if a kernel update occurs, as the new, unsigned kernel will be rejected by the firmware. Another common mistake is enrolling the keys in the wrong order. If you enroll the Platform Key (PK) first, the firmware may immediately exit Setup Mode, preventing you from enrolling the KEK or db keys. Always follow the bottom-up approach: db, then KEK, then PK.
Users also frequently forget to sign the kernel itself. Signing the bootloader (like GRUB) is only the first step; if the bootloader then attempts to load an unsigned Linux kernel, the system will halt with a "Secure Boot Violation" error. Every single piece of code that executes before the OS takes full control must be signed by a key present in the db database.
Tips
To simplify long-term maintenance, consider using a "shim" bootloader. A shim is a small, pre-signed binary (often signed by Microsoft) that can be configured to trust your own custom keys. This allows you to maintain your own keys without having to manually sign every single single update to the bootloader, as the shim handles the hand-off to the kernel using your internal keys.
When generating keys, use a strong passphrase for your private keys if you are storing them on a disk. While the examples above use the -nodes flag for automation, in a production environment, you should encrypt your private keys. Additionally, keep a "Golden Image" of your keys on an offline USB drive stored in a physical safe. If your motherboard dies and you move to a new one, having these keys allows you to maintain the same trust chain across different hardware without re-signing every single binary on your disk.
Troubleshooting
If the system fails to boot and displays a "Secure Boot Violation" or "Invalid Signature" message, the first step is to enter the UEFI settings and temporarily disable Secure Boot. This will allow you to boot into your OS and verify the signatures of your binaries using the sbverify tool. Check that the file you are booting is exactly the one you signed; sometimes updates overwrite the signed .efi file with an unsigned version.
If the UEFI firmware refuses to accept the .auth files, ensure that the certificates were generated using RSA 2048. Some older UEFI implementations do not support Elliptic Curve (ECC) keys or RSA 4096. If the "Custom Mode" option is missing entirely, check if a BIOS password is set; some manufacturers hide advanced security settings until an administrator password is created. If you have completely locked yourself out and cannot disable Secure Boot, you may need to use the motherboard's physical "Clear CMOS" jumper to reset the firmware to factory defaults, which usually reverts Secure Boot to the default factory keys.
Frequently Asked Questions
Does generating my own keys remove the Microsoft keys?
Yes, typically. When you enter "Custom Mode" and enroll your own PK, KEK, and db, you replace the factory-installed Microsoft keys. This means Windows or other Microsoft-signed binaries will no longer boot unless you manually import the Microsoft certificates into your own db list.
Can I use 4096-bit keys for better security?
While 4096-bit keys are cryptographically stronger, many UEFI firmware implementations have strict limits on the size of the key variables they can store in NVRAM. 2048-bit RSA keys are the industry standard for UEFI and offer the best compatibility across different motherboard brands.
What happens if I lose my PK.key file?
If you lose the PK.key, you cannot update the KEK or the PK itself via software. To regain control, you would need to clear the Secure Boot keys via the UEFI BIOS menu (if accessible) or perform a CMOS reset to return the system to Setup Mode.
Is this process different for laptops versus desktops?
The underlying logic of PK, KEK, and db is the same for all UEFI systems. However, laptop manufacturers often restrict access to the "Custom Mode" or "Key Management" menus more strictly than desktop motherboard vendors. Some laptops may require a specific manufacturer-provided tool to enroll custom keys.
Do I need to re-sign the kernel after every update?
Yes. Every time the kernel is updated (e.g., via apt upgrade or dnf update), a new kernel binary is created. Since this new binary is unsigned, you must sign it with your db.key before rebooting, or the system will refuse to boot the new version.