Transclusion: CL-SOM-iMX7: U-Boot: Building Secure Images: Sign
Contents
Code Signing Example
Code Signing Tool (CST)
Download CST
- Download NXP's Code Signing Tool.
Sign in (registration is required) to your NXP account prior to downloading the CST tool |
Extract the CST archive
cd /home/development/cl-som-imx7/u-boot gunzip -c /path/to/downloaded/cst-3.1.0.tgz | tar xzvf - mv release cst
Create a PKI Tree
cd /home/development/cl-som-imx7/u-boot/cst/keys ./hab4_pki_tree.sh +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ This script is a part of the Code signing tools for Freescale's High Assurance Boot. It generates a basic PKI tree. The PKI tree consists of one or more Super Root Keys (SRK), with each SRK having two subordinate keys: + a Command Sequence File (CSF) key + Image key. Additional keys can be added to the PKI tree but a separate script is available for this. This this script assumes openssl is installed on your system and is included in your search path. Finally, the private keys generated are password protectedwith the password provided by the file key_pass.txt. The format of the file is the password repeated twice: my_password my_password All private keys in the PKI tree are in PKCS #8 format will be protected by the same password. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Do you want to use an existing CA key (y/n)?: n Do you want to use Elliptic Curve Cryptography (y/n)?: n Enter key length in bits for PKI tree: 2048 Enter PKI tree duration (years): 10 How many Super Root Keys should be generated? 4 Do you want the SRK certificates to have the CA flag set? (y/n)?: y A default 'serial' file was created! A default file 'key_pass.txt' was created with password = test! ...
Generate SRK Table
cd ../crts/ ../linux64/bin/srktool -h 4 -t SRK_1_2_3_4_table.bin -e SRK_1_2_3_4_fuse.bin -d sha256 -c SRK1_sha256_2048_65537_v3_ca_crt.pem,SRK2_sha256_2048_65537_v3_ca_crt.pem,SRK3_sha256_2048_65537_v3_ca_crt.pem,SRK4_sha256_2048_65537_v3_ca_crt.pem
CSF Description Template
- Generate the example command sequence file template.
cd .. mkdir bin -p cd bin cat <<EOF >csf.txt #Illustrative Command Sequence File Description [Header] Version = 4.2 Hash Algorithm = sha256 Engine = ANY Engine Configuration = 0 Certificate Format = X509 Signature Format = CMS [Install SRK] File = "../crts/SRK_1_2_3_4_table.bin" # Index of the key location in the SRK table to be installed Source index = 0 [Install CSFK] # Key used to authenticate the CSF data File = "../crts/CSF1_1_sha256_2048_65537_v3_usr_crt.pem" [Authenticate CSF] [Install Key] # Key slot index used to authenticate the key to be installed Verification index = 0 # Target key slot in HAB key store where key will be installed Target Index = 2 # Key to install File= "../crts/IMG1_1_sha256_2048_65537_v3_usr_crt.pem" [Authenticate Data] # Key slot index used to authenticate the image data Verification index = 2 # Address Offset Length Data File Path Blocks = blk_vals file_name EOF
Signing U-Boot Firmware
CSF Description
- Generate the example command sequence files for the U-Boot and SPL.
awk 'BEGIN {file_name="\"/home/development/cl-som-imx7/u-boot/u-boot-cl-som-imx7-hab/SPL\""} /HAB Blocks:/ {blk_vals=$3 " " $4 " " $5} {sub(/blk_vals/, blk_vals); sub(/file_name/, file_name)} printline {print} ENDFILE {printline=1}' ../../u-boot-cl-som-imx7-hab/SPL.log ./csf.txt > ./csf-spl.txt awk 'BEGIN {file_name="\"/home/development/cl-som-imx7/u-boot/u-boot-cl-som-imx7-hab/u-boot-ivt.img\""} /HAB Blocks:/ {blk_vals=$3 " " $4 " " $5} {sub(/blk_vals/, blk_vals); sub(/file_name/, file_name)} printline {print} ENDFILE {printline=1}' ../../u-boot-cl-som-imx7-hab/u-boot-ivt.img.log ./csf.txt > ./csf-uboot.txt
CSF Binary Signature
- Generate the CSF binary signature for the U-Boot and SPL.
../linux64/bin/cst --o csf-spl.bin --i csf-spl.txt ../linux64/bin/cst --o csf-uboot.bin --i csf-uboot.txt
Attach CSF Signature
- Attach CSF Signature to the U-Boot and SPL Images.
cat /home/development/cl-som-imx7/u-boot/u-boot-cl-som-imx7-hab/SPL csf-spl.bin > spl-signed cat /home/development/cl-som-imx7/u-boot/u-boot-cl-som-imx7-hab/u-boot-ivt.img csf-uboot.bin > u-boot-signed
Generate Firmware Image
- Merge the SPL and U-Boot images into one firmware image.
dd if=/dev/zero count=640 bs=1K | tr '\000' '\377' > cl-som-imx7-firmware dd if=spl-signed of=cl-som-imx7-firmware bs=1K seek=1 conv=notrunc dd if=u-boot-signed of=cl-som-imx7-firmware bs=1K seek=64 conv=notrunc mv cl-som-imx7-firmware /tftproot/cl-som-imx7/test/
Signing Kernel Image
Image Parameters
- Calculate kernel image parameters.
Replace /path/to/zimage/zImage with the actual kernel image path.
zimage_path=/path/to/zimage/zImage read zimage_pad_size <<< $(ls -l $zimage_path | awk '{size=int(($5+0xfff)/0x1000)*0x1000; print size}') zimage_self_ptr=$(printf "0x%x\n" $(($zimage_pad_size+0x80800000))) zimage_csf_ptr=$(printf "0x%x\n" $(($zimage_self_ptr+0x20))) zImage_pad_ivt_size=$(printf "0x%x\n" $(($zimage_pad_size+0x20)))
Image Vector Table
- Generate the image vector table.
cat <<EOF >genIVT #! /usr/bin/perl -w use strict; open(my \$out, '>:raw', 'ivt.bin') or die "Unable to open: \$!"; print \$out pack("V", 0x412000D1); # Signature print \$out pack("V", 0x80800000); # Load Address print \$out pack("V", 0x0); # Reserved print \$out pack("V", 0x0); # DCD pointer print \$out pack("V", 0x0); # Boot Data print \$out pack("V", $zimage_self_ptr); # Self Pointer *ivt print \$out pack("V", $zimage_csf_ptr); # CSF Pointer *csf print \$out pack("V", 0x0); # Reserved close(\$out); EOF chmod +x genIVT ./genIVT
Image Padding
objcopy -I binary -O binary --pad-to $zimage_pad_size --gap-fill=0x00 $zimage_path zImage_pad
Adding IVT
- Append the image vector table at the end of the padded kernel image.
cat zImage_pad ivt.bin > zImage_pad_ivt
CSF Description
- Generate the example command sequence files for the kernel image.
awk -v blk_vals="0x80800000 0x000 $zImage_pad_ivt_size" ' {sub(/blk_vals/, blk_vals); sub(/file_name/, "\"zImage_pad_ivt\""); print}' ./csf.txt > ./csf-zimage.txt
CSF Binary Signature
- Generate the CSF binary signature for the kernel image.
../linux64/bin/cst --o csf-zImage --i csf-zimage.txt
Attach CSF Signature
- Attach CSF Signature to the kernel image.
cat zImage_pad_ivt csf-zImage > zImage_signed