blob: fb474414221542be36b1e9e3ffd82bafffabc78e [file] [log] [blame]
Googler25e92cf2023-12-13 10:05:01 +00001# SPDX-License-Identifier: GPL-2.0+
2# Copyright (c) 2016, Google Inc.
3#
4# U-Boot Verified Boot Test
5
6"""
7This tests verified boot in the following ways:
8
9For image verification:
10- Create FIT (unsigned) with mkimage
11- Check that verification shows that no keys are verified
12- Sign image
13- Check that verification shows that a key is now verified
14
15For configuration verification:
16- Corrupt signature and check for failure
17- Create FIT (with unsigned configuration) with mkimage
18- Check that image verification works
19- Sign the FIT and mark the key as 'required' for verification
20- Check that image verification works
21- Corrupt the signature
22- Check that image verification no-longer works
23
24Tests run with both SHA1 and SHA256 hashing.
25"""
26
27
28import shutil
29import pytest
30import sys
31import struct
32import u_boot_utils as util
33import vboot_evil
34
35# Only run the full suite on a few combinations, since it doesn't add any more
36# test coverage.
37TESTDATA = [
38 ['sha1', '', None, False, True],
39 ['sha1', '', '-E -p 0x10000', False, False],
40 ['sha1', '-pss', None, False, False],
41 ['sha1', '-pss', '-E -p 0x10000', False, False],
42 ['sha256', '', None, False, False],
43 ['sha256', '', '-E -p 0x10000', False, False],
44 ['sha256', '-pss', None, False, False],
45 ['sha256', '-pss', '-E -p 0x10000', False, False],
46 ['sha256', '-pss', None, True, False],
47 ['sha256', '-pss', '-E -p 0x10000', True, True],
48]
49
50@pytest.mark.boardspec('sandbox')
51@pytest.mark.buildconfigspec('fit_signature')
52@pytest.mark.requiredtool('dtc')
53@pytest.mark.requiredtool('fdtget')
54@pytest.mark.requiredtool('fdtput')
55@pytest.mark.requiredtool('openssl')
56def test_vboot(u_boot_console):
57 """Test verified boot signing with mkimage and verification with 'bootm'.
58
59 This works using sandbox only as it needs to update the device tree used
60 by U-Boot to hold public keys from the signing process.
61
62 The SHA1 and SHA256 tests are combined into a single test since the
63 key-generation process is quite slow and we want to avoid doing it twice.
64 """
65 def dtc(dts):
66 """Run the device tree compiler to compile a .dts file
67
68 The output file will be the same as the input file but with a .dtb
69 extension.
70
71 Args:
72 dts: Device tree file to compile.
73 """
74 dtb = dts.replace('.dts', '.dtb')
75 util.run_and_log(cons, 'dtc %s %s%s -O dtb '
76 '-o %s%s' % (dtc_args, datadir, dts, tmpdir, dtb))
77
78 def run_bootm(sha_algo, test_type, expect_string, boots, fit=None):
79 """Run a 'bootm' command U-Boot.
80
81 This always starts a fresh U-Boot instance since the device tree may
82 contain a new public key.
83
84 Args:
85 test_type: A string identifying the test type.
86 expect_string: A string which is expected in the output.
87 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
88 use.
89 boots: A boolean that is True if Linux should boot and False if
90 we are expected to not boot
91 """
92 cons.restart_uboot()
93 with cons.log.section('Verified boot %s %s' % (sha_algo, test_type)):
94 output = cons.run_command_list(
95 ['host load hostfs - 100 %stest.fit' % tmpdir,
96 'fdt addr 100',
97 'bootm 100'])
98 assert(expect_string in ''.join(output))
99 if boots:
100 assert('sandbox: continuing, as we cannot run' in ''.join(output))
101
102 def make_fit(its):
103 """Make a new FIT from the .its source file.
104
105 This runs 'mkimage -f' to create a new FIT.
106
107 Args:
108 its: Filename containing .its source.
109 """
110 util.run_and_log(cons, [mkimage, '-D', dtc_args, '-f',
111 '%s%s' % (datadir, its), fit])
112
113 def sign_fit(sha_algo):
114 """Sign the FIT
115
116 Signs the FIT and writes the signature into it. It also writes the
117 public key into the dtb.
118
119 Args:
120 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
121 use.
122 """
123 cons.log.action('%s: Sign images' % sha_algo)
124 util.run_and_log(cons, [mkimage, '-F', '-k', tmpdir, '-K', dtb,
125 '-r', fit])
126
127 def replace_fit_totalsize(size):
128 """Replace FIT header's totalsize with something greater.
129
130 The totalsize must be less than or equal to FIT_SIGNATURE_MAX_SIZE.
131 If the size is greater, the signature verification should return false.
132
133 Args:
134 size: The new totalsize of the header
135
136 Returns:
137 prev_size: The previous totalsize read from the header
138 """
139 total_size = 0
140 with open(fit, 'r+b') as handle:
141 handle.seek(4)
142 total_size = handle.read(4)
143 handle.seek(4)
144 handle.write(struct.pack(">I", size))
145 return struct.unpack(">I", total_size)[0]
146
147 def test_with_algo(sha_algo, padding):
148 """Test verified boot with the given hash algorithm.
149
150 This is the main part of the test code. The same procedure is followed
151 for both hashing algorithms.
152
153 Args:
154 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
155 use.
156 """
157 # Compile our device tree files for kernel and U-Boot. These are
158 # regenerated here since mkimage will modify them (by adding a
159 # public key) below.
160 dtc('sandbox-kernel.dts')
161 dtc('sandbox-u-boot.dts')
162
163 # Build the FIT, but don't sign anything yet
164 cons.log.action('%s: Test FIT with signed images' % sha_algo)
165 make_fit('sign-images-%s%s.its' % (sha_algo , padding))
166 run_bootm(sha_algo, 'unsigned images', 'dev-', True)
167
168 # Sign images with our dev keys
169 sign_fit(sha_algo)
170 run_bootm(sha_algo, 'signed images', 'dev+', True)
171
172 # Create a fresh .dtb without the public keys
173 dtc('sandbox-u-boot.dts')
174
175 cons.log.action('%s: Test FIT with signed configuration' % sha_algo)
176 make_fit('sign-configs-%s%s.its' % (sha_algo , padding))
177 run_bootm(sha_algo, 'unsigned config', '%s+ OK' % sha_algo, True)
178
179 # Sign images with our dev keys
180 sign_fit(sha_algo)
181 run_bootm(sha_algo, 'signed config', 'dev+', True)
182
183 cons.log.action('%s: Check signed config on the host' % sha_algo)
184
185 util.run_and_log(cons, [fit_check_sign, '-f', fit, '-k', tmpdir,
186 '-k', dtb])
187
188 # Replace header bytes
189 bcfg = u_boot_console.config.buildconfig
190 max_size = int(bcfg.get('config_fit_signature_max_size', 0x10000000), 0)
191 existing_size = replace_fit_totalsize(max_size + 1)
192 run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
193 cons.log.action('%s: Check overflowed FIT header totalsize' % sha_algo)
194
195 # Replace with existing header bytes
196 replace_fit_totalsize(existing_size)
197 run_bootm(sha_algo, 'signed config', 'dev+', True)
198 cons.log.action('%s: Check default FIT header totalsize' % sha_algo)
199
200 # Increment the first byte of the signature, which should cause failure
201 sig = util.run_and_log(cons, 'fdtget -t bx %s %s value' %
202 (fit, sig_node))
203 byte_list = sig.split()
204 byte = int(byte_list[0], 16)
205 byte_list[0] = '%x' % (byte + 1)
206 sig = ' '.join(byte_list)
207 util.run_and_log(cons, 'fdtput -t bx %s %s value %s' %
208 (fit, sig_node, sig))
209
210 run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash', False)
211
212 cons.log.action('%s: Check bad config on the host' % sha_algo)
213 util.run_and_log_expect_exception(cons, [fit_check_sign, '-f', fit,
214 '-k', dtb], 1, 'Failed to verify required signature')
215
216 cons = u_boot_console
217 tmpdir = cons.config.result_dir + '/'
218 tmp = tmpdir + 'vboot.tmp'
219 datadir = cons.config.source_dir + '/test/py/tests/vboot/'
220 fit = '%stest.fit' % tmpdir
221 mkimage = cons.config.build_dir + '/tools/mkimage'
222 fit_check_sign = cons.config.build_dir + '/tools/fit_check_sign'
223 dtc_args = '-I dts -O dtb -i %s' % tmpdir
224 dtb = '%ssandbox-u-boot.dtb' % tmpdir
225 sig_node = '/configurations/conf-1/signature'
226
227 # Create an RSA key pair
228 public_exponent = 65537
229 util.run_and_log(cons, 'openssl genpkey -algorithm RSA -out %sdev.key '
230 '-pkeyopt rsa_keygen_bits:2048 '
231 '-pkeyopt rsa_keygen_pubexp:%d' %
232 (tmpdir, public_exponent))
233
234 # Create a certificate containing the public key
235 util.run_and_log(cons, 'openssl req -batch -new -x509 -key %sdev.key -out '
236 '%sdev.crt' % (tmpdir, tmpdir))
237
238 # Create a number kernel image with zeroes
239 with open('%stest-kernel.bin' % tmpdir, 'w') as fd:
240 fd.write(5000 * chr(0))
241
242 try:
243 # We need to use our own device tree file. Remember to restore it
244 # afterwards.
245 old_dtb = cons.config.dtb
246 cons.config.dtb = dtb
247 test_with_algo('sha1','')
248 test_with_algo('sha1','-pss')
249 test_with_algo('sha256','')
250 test_with_algo('sha256','-pss')
251 finally:
252 # Go back to the original U-Boot with the correct dtb.
253 cons.config.dtb = old_dtb
254 cons.restart_uboot()