k3_gen_x509_cert.sh 6.2 KB

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