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