vboot_test.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. #
  3. # Copyright (c) 2013, Google Inc.
  4. #
  5. # Simple Verified Boot Test Script
  6. #
  7. # SPDX-License-Identifier: GPL-2.0+
  8. set -e
  9. # Run U-Boot and report the result
  10. # Args:
  11. # $1: Test message
  12. run_uboot() {
  13. echo -n "Test Verified Boot Run: $1: "
  14. ${uboot} -d sandbox-u-boot.dtb >${tmp} -c '
  15. sb load hostfs - 100 test.fit;
  16. fdt addr 100;
  17. bootm 100;
  18. reset'
  19. if ! grep -q "$2" ${tmp}; then
  20. echo
  21. echo "Verified boot key check failed, output follows:"
  22. cat ${tmp}
  23. false
  24. else
  25. echo "OK"
  26. fi
  27. }
  28. echo "Simple Verified Boot Test"
  29. echo "========================="
  30. echo
  31. echo "Please see doc/uImage.FIT/verified-boot.txt for more information"
  32. echo
  33. err=0
  34. tmp=/tmp/vboot_test.$$
  35. dir=$(dirname $0)
  36. if [ -z ${O} ]; then
  37. O=.
  38. fi
  39. O=$(readlink -f ${O})
  40. dtc="-I dts -O dtb -p 2000"
  41. uboot="${O}/u-boot"
  42. mkimage="${O}/tools/mkimage"
  43. fit_check_sign="${O}/tools/fit_check_sign"
  44. keys="${dir}/dev-keys"
  45. echo ${mkimage} -D "${dtc}"
  46. echo "Build keys"
  47. mkdir -p ${keys}
  48. PUBLIC_EXPONENT=${1}
  49. if [ -z "${PUBLIC_EXPONENT}" ]; then
  50. PUBLIC_EXPONENT=65537
  51. fi
  52. # Create an RSA key pair
  53. openssl genpkey -algorithm RSA -out ${keys}/dev.key \
  54. -pkeyopt rsa_keygen_bits:2048 \
  55. -pkeyopt rsa_keygen_pubexp:${PUBLIC_EXPONENT} 2>/dev/null
  56. # Create a certificate containing the public key
  57. openssl req -batch -new -x509 -key ${keys}/dev.key -out ${keys}/dev.crt
  58. pushd ${dir} >/dev/null
  59. function do_test {
  60. echo do $sha test
  61. # Compile our device tree files for kernel and U-Boot
  62. dtc -p 0x1000 sandbox-kernel.dts -O dtb -o sandbox-kernel.dtb
  63. dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb
  64. # Create a number kernel image with zeroes
  65. head -c 5000 /dev/zero >test-kernel.bin
  66. # Build the FIT, but don't sign anything yet
  67. echo Build FIT with signed images
  68. ${mkimage} -D "${dtc}" -f sign-images-$sha.its test.fit >${tmp}
  69. run_uboot "unsigned signatures:" "dev-"
  70. # Sign images with our dev keys
  71. echo Sign images
  72. ${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \
  73. -r test.fit >${tmp}
  74. run_uboot "signed images" "dev+"
  75. # Create a fresh .dtb without the public keys
  76. dtc -p 0x1000 sandbox-u-boot.dts -O dtb -o sandbox-u-boot.dtb
  77. echo Build FIT with signed configuration
  78. ${mkimage} -D "${dtc}" -f sign-configs-$sha.its test.fit >${tmp}
  79. run_uboot "unsigned config" $sha"+ OK"
  80. # Sign images with our dev keys
  81. echo Sign images
  82. ${mkimage} -D "${dtc}" -F -k dev-keys -K sandbox-u-boot.dtb \
  83. -r test.fit >${tmp}
  84. run_uboot "signed config" "dev+"
  85. echo check signed config on the host
  86. if ! ${fit_check_sign} -f test.fit -k sandbox-u-boot.dtb >${tmp}; then
  87. echo
  88. echo "Verified boot key check on host failed, output follows:"
  89. cat ${tmp}
  90. false
  91. else
  92. if ! grep -q "dev+" ${tmp}; then
  93. echo
  94. echo "Verified boot key check failed, output follows:"
  95. cat ${tmp}
  96. false
  97. else
  98. echo "OK"
  99. fi
  100. fi
  101. run_uboot "signed config" "dev+"
  102. # Increment the first byte of the signature, which should cause failure
  103. sig=$(fdtget -t bx test.fit /configurations/conf@1/signature@1 value)
  104. newbyte=$(printf %x $((0x${sig:0:2} + 1)))
  105. sig="${newbyte} ${sig:2}"
  106. fdtput -t bx test.fit /configurations/conf@1/signature@1 value ${sig}
  107. run_uboot "signed config with bad hash" "Bad Data Hash"
  108. }
  109. sha=sha1
  110. do_test
  111. sha=sha256
  112. do_test
  113. popd >/dev/null
  114. echo
  115. if ${ok}; then
  116. echo "Test passed"
  117. else
  118. echo "Test failed"
  119. fi