Secure boot in Android from Bootloader to the top layer
Assistant
User
which part the we can find public key for the secondary bootloader
Assistant
Great question!
Where is the public key for verifying the Secondary Bootloader stored?
User
what is include $(BUILD_PREBUILT)
Assistant
The line include $(BUILD_PREBUILT) is commonly found in Android (Android.mk files) within the Android build system (based on GNU Make).
Certainly! Here's an explanation of SHA-256 and X.509:
SHA-256
Stands for: Secure Hash Algorithm 256-bit
Type: Cryptographic hash function
Purpose: It takes an input (or 'message') and produces a fixed-size string of bytes—typically a 256-bit (32-byte) hash value, often rendered as a 64-character hexadecimal number.
Properties:
Deterministic: Same input always produces the same output.
Fast to compute the hash for any given input.
Pre-image resistance: Given a hash, it should be infeasible to find the input that produced it.
Collision resistance: It’s very hard to find two different inputs that produce the same hash.
Avalanche effect: Small changes in input cause large changes in the output hash.
Use cases:
Ensuring data integrity (e.g., verifying files or messages haven't been altered).
Digital signatures.
Password hashing (often combined with salts).
Blockchain mining.
SHA-256 is part of the SHA-2 family, designed by the NSA, and widely used today in security protocols like TLS, SSL, PGP, SSH, and cryptocurrencies like Bitcoin.
X.509
Type: A standard defining the format of public key certificates.
Purpose: X.509 certificates are used to associate a public key with an identity (person, organization, server) and to bind that identity to a digital certificate.
Content of an X.509 certificate:
Version
Serial number
Signature algorithm identifier
Issuer (the authority that issued the certificate)
Validity period (start and end dates)
Subject (the entity the certificate is issued to)
Subject’s public key information
Extensions (optional additional data)
Digital signature of the issuer
How it works in practice:
A Certificate Authority (CA) signs the certificate, attesting that the public key belongs to the subject.
Used widely in Secure Sockets Layer (SSL)/Transport Layer Security (TLS) to secure web traffic (HTTPS).
Used in many other cryptographic systems for authentication and establishing secure communication.
Structure: Typically encoded in ASN.1 and formatted in DER or PEM encoding.
Summary
SHA-256: A cryptographic hash function used to create a unique fingerprint of data.
X.509: A standard format for public key certificates enabling secure identity verification and key exchange.
If you want, I can provide examples or explain how they work together (e.g., an X.509 certificate’s signature might involve SHA-256). Let me know!
Certainly! Here's an overview of Secure Boot in Android, covering the process from the Bootloader all the way to the top (Android OS) layer. This mechanism ensures that every stage of the boot process is verified to prevent unauthorized or malicious code from running on the device.
What is Secure Boot?
Secure Boot is a security standard that ensures that a device boots only using software that is trusted by the device manufacturer. The underlying principle is chain of trust: every stage verifies the integrity and authenticity of the next stage before handing over control.
Secure Boot Process in Android
1. ROM (Read Only Memory) / Primary Bootloader (Immutable, Hardware-Based Root of Trust)
The process begins immediately after power-on, with the device’s immutable Primary Bootloader, typically stored in on-chip ROM (or mask ROM).
This bootstrap code is hardware-embedded and considered the Root of Trust.
The Primary Bootloader verifies the integrity and authenticity of the Secondary Bootloader before executing it, usually by checking digital signatures using a public key embedded safely in hardware.
This key verifies that the bootloader binary is signed by a trusted authority (e.g., device manufacturer).
2. Secondary Bootloader (e.g., Bootloader Stage 2, or Aboot in Android)
The Secondary Bootloader is responsible for initializing more hardware components, loading partitions, etc.
Secure Boot ensures that this bootloader is also signed and verifies the signature before executing it.
In Android devices, this stage may be called Aboot or Little Kernel (LK) depending on the vendor.
This stage verifies the integrity of the next component: the Boot Image.
3. Boot Image / Kernel and Ramdisk Verification
The bootloader loads the boot image, which contains:
The Linux Kernel
Ramdisk (init process and essential drivers)
The boot image is signed and verified before being loaded.
Verification involves cryptographic signature checking using Android Verified Boot keys. Android typically uses RSA or ECDSA signatures with SHA-256 hashes.
4. Verified Boot (Android Verified Boot - AVB)
Since Android 7.0+, devices implement Android Verified Boot (AVB).
AVB extends traditional secure boot with:
Verification of the entire system partition and additional partitions (vendor, product, etc.), not only the boot partition.
A rollback protection mechanism to prevent booting old, vulnerable images.
The verification process uses hash trees (Merkle trees) to efficiently verify the integrity of large partitions.
If verification passes, the device boots normally.
If verification fails:
The device might enter recovery mode, show a warning screen, or refuse to boot, depending on device policy and user consent state.
5. Init and Starting Android Framework
After the kernel boots, it starts the init process (in the ramdisk), which sets up user space.
The init process then starts the Zygote (Android’s runtime launcher) and subsequently the Android framework.
Before Android framework apps run, the keymaster and Gatekeeper TrustZone components may kick in for cryptographic operations and security checks.
6. Runtime Protection: dm-verity
Android uses dm-verity (device-mapper verity), a kernel feature, to ensure system partitions are not tampered with during runtime.
dm-verity provides on-the-fly verification of read-only partitions like system and vendor.
This protects against malicious modification of important files.
Summary of the Chain of Trust
Stage
What is Verified
How
Primary Bootloader (ROM)
Secondary Bootloader image
Signature verification with embedded keys
Secondary Bootloader (Aboot)
Boot Image (Kernel + Ramdisk)
Signature verification
Boot Image
System partition (via AVB/dm-verity)
Hash and signature verification, Merkle trees
System partitions at runtime
Integrity of system files
dm-verity runtime checks
Additional Notes
OEM Keys & Key Management: The root keys used for signature verification are burned into hardware or securely stored in replay-protected memory areas.
User Choice & Unlocking Bootloader: Unlocking the bootloader disables the secure boot enforcement on many devices, allowing custom ROMs but reducing security.
TEE (Trusted Execution Environment): Plays a role especially in key management and secure storage, but is beyond boot for runtime security.
If you want, I can provide a detailed flowchart or specific example with actual Android components like how Verified Boot works or how dm-verity is integrated. Let me know!
The public key used to verify the Secondary Bootloader (also called the Stage 2 bootloader) is embedded in the device's immutable Primary Bootloader or the read-only hardware storage that acts as the Root of Trust.
More specifically:
The Primary Bootloader (or ROM bootloader)—which is a small piece of code burned into on-chip mask ROM or e-fuse memory during manufacturing—contains the public key(s) used to verify the cryptographic signatures of the Secondary Bootloader.
This primary stage has no external update capability, so it is inherently trusted and cannot be tampered with.
When the device powers on, this Primary Bootloader:
Reads the Secondary Bootloader binary from flash memory.
Uses the embedded public key to verify the signature of the Secondary Bootloader.
If verification passes, it executes the Secondary Bootloader; if not, it stops the boot process.
Why is the public key stored here?
Security: Keys embedded in hardware or immutable memory cannot be altered or replaced by attackers.
Trust Anchor: This public key acts as the root of trust to establish the entire secure boot chain.
Signature scheme: Usually the Secondary Bootloader is signed (e.g., with RSA or ECDSA private key), and this public key corresponds to that private key.
Examples by OEM/Platform
Qualcomm-based devices: The primary bootloader (e.g., SBL - Secondary Boot Loader in Qualcomm terminology) stores root public keys in mask ROM.
Google Pixel: The boot ROM contains Google's root public keys for verifying bootloader images.
Many SoC vendors: embed the public key or a hash of the key in the boot ROM or secure fuse memory.
Summary
Component
Where is the Public Key for Secondary Bootloader Verification Stored?
Primary Bootloader (ROM)
Hardcoded in on-chip ROM (mask ROM) or one-time programmable memory (e-fuse)
Secondary Bootloader Image
Verified using this embedded public key before execution
If you want details on a specific SoC or Android device vendor, I can provide more vendor-specific insights!
makefiles
What does include $(BUILD_PREBUILT) mean?
$(BUILD_PREBUILT) is a Makefile variable defined by the Android build system.
It points to a standard makefile fragment that provides build rules and logic for incorporating prebuilt binaries into the build.
When you do:
include $(BUILD_PREBUILT)
you are importing and executing the rules to tell the build system:
"Here is a binary or file already built (prebuilt). Please package it properly without rebuilding it."
What is a prebuilt in Android build?
A prebuilt is a binary file (e.g., a .so library, an executable, a firmware blob, or other files) built outside of the current build tree, which you want to include (copy) into the final build outputs (like system.img, vendor.img, or an APK).
Instead of compiling source code, you simply tell the build system where the file is, and it adds it to the final build in the right place.
Typical usage in Android.mk for a prebuilt file
Here's a minimal example of using BUILD_PREBUILT in an Android.mk: