blob: 24cfc4e5fb5a9e4523d46a0ba89c63f009ef3bb2 [file] [log] [blame]
Lokesh Vutlaca711862019-05-02 15:35:50 +05301#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3#
4# Script to add K3 specific x509 cetificate to a binary.
5#
6
7# Variables
8OUTPUT=tiboot3.bin
9TEMP_X509=x509-temp.cert
10CERT=certificate.bin
11RAND_KEY=eckey.pem
12LOADADDR=0x41c00000
13BOOTCORE_OPTS=0
14BOOTCORE=16
Andrew F. Davisa2303f42020-05-27 09:47:55 -040015DEBUG_TYPE=0
Yogesh Siraswar00194272022-07-15 11:38:53 -050016SWRV=1
Lokesh Vutlaca711862019-05-02 15:35:50 +053017
18gen_degen_template() {
19cat << 'EOF' > degen-template.txt
20
21asn1=SEQUENCE:rsa_key
22
23[rsa_key]
24version=INTEGER:0
25modulus=INTEGER:0xDEGEN_MODULUS
26pubExp=INTEGER:1
27privExp=INTEGER:1
28p=INTEGER:0xDEGEN_P
29q=INTEGER:0xDEGEN_Q
30e1=INTEGER:1
31e2=INTEGER:1
32coeff=INTEGER:0xDEGEN_COEFF
33EOF
34}
35
36# Generate x509 Template
37gen_template() {
38cat << 'EOF' > x509-template.txt
39 [ req ]
40 distinguished_name = req_distinguished_name
41 x509_extensions = v3_ca
42 prompt = no
43 dirstring_type = nobmp
44
45 [ req_distinguished_name ]
46 C = US
47 ST = TX
48 L = Dallas
49 O = Texas Instruments Incorporated
50 OU = Processors
51 CN = TI support
52 emailAddress = support@ti.com
53
54 [ v3_ca ]
55 basicConstraints = CA:true
56 1.3.6.1.4.1.294.1.1 = ASN1:SEQUENCE:boot_seq
57 1.3.6.1.4.1.294.1.2 = ASN1:SEQUENCE:image_integrity
58 1.3.6.1.4.1.294.1.3 = ASN1:SEQUENCE:swrv
59# 1.3.6.1.4.1.294.1.4 = ASN1:SEQUENCE:encryption
60 1.3.6.1.4.1.294.1.8 = ASN1:SEQUENCE:debug
61
62 [ boot_seq ]
63 certType = INTEGER:TEST_CERT_TYPE
64 bootCore = INTEGER:TEST_BOOT_CORE
65 bootCoreOpts = INTEGER:TEST_BOOT_CORE_OPTS
66 destAddr = FORMAT:HEX,OCT:TEST_BOOT_ADDR
67 imageSize = INTEGER:TEST_IMAGE_LENGTH
68
69 [ image_integrity ]
70 shaType = OID:2.16.840.1.101.3.4.2.3
71 shaValue = FORMAT:HEX,OCT:TEST_IMAGE_SHA_VAL
72
73 [ swrv ]
Yogesh Siraswar00194272022-07-15 11:38:53 -050074 swrv = INTEGER:TEST_SWRV
Lokesh Vutlaca711862019-05-02 15:35:50 +053075
76# [ encryption ]
77# initalVector = FORMAT:HEX,OCT:TEST_IMAGE_ENC_IV
78# randomString = FORMAT:HEX,OCT:TEST_IMAGE_ENC_RS
79# iterationCnt = INTEGER:TEST_IMAGE_KEY_DERIVE_INDEX
80# salt = FORMAT:HEX,OCT:TEST_IMAGE_KEY_DERIVE_SALT
81
82 [ debug ]
83 debugUID = FORMAT:HEX,OCT:0000000000000000000000000000000000000000000000000000000000000000
Andrew F. Davis0428a0b2020-05-27 09:47:54 -040084 debugType = INTEGER:TEST_DEBUG_TYPE
Lokesh Vutlaca711862019-05-02 15:35:50 +053085 coreDbgEn = INTEGER:0
86 coreDbgSecEn = INTEGER:0
87EOF
88}
89
90parse_key() {
91 sed '/\ \ \ \ /s/://g' key.txt | awk '!/\ \ \ \ / {printf("\n%s\n", $0)}; /\ \ \ \ / {printf("%s", $0)}' | sed 's/\ \ \ \ //g' | awk "/$1:/{getline; print}"
92}
93
94gen_degen_key() {
95# Generate a 4096 bit RSA Key
96 openssl genrsa -out key.pem 1024 >>/dev/null 2>&1
97 openssl rsa -in key.pem -text -out key.txt >>/dev/null 2>&1
98 DEGEN_MODULUS=$( parse_key 'modulus' )
99 DEGEN_P=$( parse_key 'prime1' )
100 DEGEN_Q=$( parse_key 'prime2' )
101 DEGEN_COEFF=$( parse_key 'coefficient' )
102 gen_degen_template
103
104 sed -e "s/DEGEN_MODULUS/$DEGEN_MODULUS/"\
105 -e "s/DEGEN_P/$DEGEN_P/" \
106 -e "s/DEGEN_Q/$DEGEN_Q/" \
107 -e "s/DEGEN_COEFF/$DEGEN_COEFF/" \
108 degen-template.txt > degenerateKey.txt
109
110 openssl asn1parse -genconf degenerateKey.txt -out degenerateKey.der >>/dev/null 2>&1
111 openssl rsa -in degenerateKey.der -inform DER -outform PEM -out $RAND_KEY >>/dev/null 2>&1
112 KEY=$RAND_KEY
113 rm key.pem key.txt degen-template.txt degenerateKey.txt degenerateKey.der
114}
115
116declare -A options_help
117usage() {
118 if [ -n "$*" ]; then
119 echo "ERROR: $*"
120 fi
121 echo -n "Usage: $0 "
122 for option in "${!options_help[@]}"
123 do
124 arg=`echo ${options_help[$option]}|cut -d ':' -f1`
125 if [ -n "$arg" ]; then
126 arg=" $arg"
127 fi
128 echo -n "[-$option$arg] "
129 done
130 echo
131 echo -e "\nWhere:"
132 for option in "${!options_help[@]}"
133 do
134 arg=`echo ${options_help[$option]}|cut -d ':' -f1`
135 txt=`echo ${options_help[$option]}|cut -d ':' -f2`
136 tb="\t\t\t"
137 if [ -n "$arg" ]; then
138 arg=" $arg"
139 tb="\t"
140 fi
141 echo -e " -$option$arg:$tb$txt"
142 done
143 echo
144 echo "Examples of usage:-"
145 echo "# Example of signing the SYSFW binary with rsa degenerate key"
146 echo " $0 -c 0 -b ti-sci-firmware-am6x.bin -o sysfw.bin -l 0x40000"
147 echo "# Example of signing the SPL binary with rsa degenerate key"
148 echo " $0 -c 16 -b spl/u-boot-spl.bin -o tiboot3.bin -l 0x41c00000"
149}
150
151options_help[b]="bin_file:Bin file that needs to be signed"
152options_help[k]="key_file:file with key inside it. If not provided script generates a rsa degenerate key."
153options_help[o]="output_file:Name of the final output file. default to $OUTPUT"
154options_help[c]="core_id:target core id on which the image would be running. Default to $BOOTCORE"
155options_help[l]="loadaddr: Target load address of the binary in hex. Default to $LOADADDR"
Andrew F. Davisa2303f42020-05-27 09:47:55 -0400156options_help[d]="debug_type: Debug type, set to 4 to enable early JTAG. Default to $DEBUG_TYPE"
Yogesh Siraswar00194272022-07-15 11:38:53 -0500157options_help[r]="SWRV: Software Rev for X509 certificate"
Lokesh Vutlaca711862019-05-02 15:35:50 +0530158
Yogesh Siraswar00194272022-07-15 11:38:53 -0500159while getopts "b:k:o:c:l:d:h:r:" opt
Lokesh Vutlaca711862019-05-02 15:35:50 +0530160do
161 case $opt in
162 b)
163 BIN=$OPTARG
164 ;;
165 k)
166 KEY=$OPTARG
167 ;;
168 o)
169 OUTPUT=$OPTARG
170 ;;
171 l)
172 LOADADDR=$OPTARG
173 ;;
174 c)
175 BOOTCORE=$OPTARG
176 ;;
Andrew F. Davis0428a0b2020-05-27 09:47:54 -0400177 d)
178 DEBUG_TYPE=$OPTARG
179 ;;
Yogesh Siraswar00194272022-07-15 11:38:53 -0500180 r)
181 SWRV=$OPTARG
182 ;;
Lokesh Vutlaca711862019-05-02 15:35:50 +0530183 h)
184 usage
185 exit 0
186 ;;
187 \?)
188 usage "Invalid Option '-$OPTARG'"
189 exit 1
190 ;;
191 :)
192 usage "Option '-$OPTARG' Needs an argument."
193 exit 1
194 ;;
195 esac
196done
197
198if [ "$#" -eq 0 ]; then
199 usage "Arguments missing"
200 exit 1
201fi
202
203if [ -z "$BIN" ]; then
204 usage "Bin file missing in arguments"
205 exit 1
206fi
207
208# Generate rsa degenerate key if user doesn't provide a key
209if [ -z "$KEY" ]; then
210 gen_degen_key
211fi
212
213if [ $BOOTCORE == 0 ]; then # BOOTCORE M3, loaded by ROM
214 CERTTYPE=2
215elif [ $BOOTCORE == 16 ]; then # BOOTCORE R5, loaded by ROM
216 CERTTYPE=1
217else # Non BOOTCORE, loaded by SYSFW
218 BOOTCORE_OPTS_VER=$(printf "%01x" 1)
219 # Add input args option for SET and CLR flags.
220 BOOTCORE_OPTS_SETFLAG=$(printf "%08x" 0)
221 BOOTCORE_OPTS_CLRFLAG=$(printf "%08x" 0x100) # Clear FLAG_ARMV8_AARCH32
222 BOOTCORE_OPTS="0x$BOOTCORE_OPTS_VER$BOOTCORE_OPTS_SETFLAG$BOOTCORE_OPTS_CLRFLAG"
223 # Set the cert type to zero.
224 # We are not using public/private key store now
225 CERTTYPE=$(printf "0x%08x" 0)
226fi
227
228SHA_VAL=`openssl dgst -sha512 -hex $BIN | sed -e "s/^.*= //g"`
229BIN_SIZE=`cat $BIN | wc -c`
230ADDR=`printf "%08x" $LOADADDR`
231
232gen_cert() {
233 #echo "Certificate being generated :"
234 #echo " LOADADDR = 0x$ADDR"
235 #echo " IMAGE_SIZE = $BIN_SIZE"
236 #echo " CERT_TYPE = $CERTTYPE"
Andrew F. Davis0428a0b2020-05-27 09:47:54 -0400237 #echo " DEBUG_TYPE = $DEBUG_TYPE"
Yogesh Siraswar00194272022-07-15 11:38:53 -0500238 echo " SWRV = $SWRV"
Lokesh Vutlaca711862019-05-02 15:35:50 +0530239 sed -e "s/TEST_IMAGE_LENGTH/$BIN_SIZE/" \
240 -e "s/TEST_IMAGE_SHA_VAL/$SHA_VAL/" \
241 -e "s/TEST_CERT_TYPE/$CERTTYPE/" \
242 -e "s/TEST_BOOT_CORE_OPTS/$BOOTCORE_OPTS/" \
243 -e "s/TEST_BOOT_CORE/$BOOTCORE/" \
Andrew F. Davis0428a0b2020-05-27 09:47:54 -0400244 -e "s/TEST_BOOT_ADDR/$ADDR/" \
245 -e "s/TEST_DEBUG_TYPE/$DEBUG_TYPE/" \
Yogesh Siraswar00194272022-07-15 11:38:53 -0500246 -e "s/TEST_SWRV/$SWRV/" \
Andrew F. Davis0428a0b2020-05-27 09:47:54 -0400247 x509-template.txt > $TEMP_X509
Lokesh Vutlaca711862019-05-02 15:35:50 +0530248 openssl req -new -x509 -key $KEY -nodes -outform DER -out $CERT -config $TEMP_X509 -sha512
249}
250
251gen_template
252gen_cert
253cat $CERT $BIN > $OUTPUT
254
255# Remove all intermediate files
256rm $TEMP_X509 $CERT x509-template.txt
257if [ "$KEY" == "$RAND_KEY" ]; then
258 rm $RAND_KEY
259fi