blob: 7a7f9c379de5df49127150f62c4a2a15ba3c06dd [file] [log] [blame]
Simon Glassd977ecd2016-07-03 09:40:46 -06001# SPDX-License-Identifier: GPL-2.0+
Tom Rini10e47792018-05-06 17:58:06 -04002# Copyright (c) 2016, Google Inc.
Simon Glassd977ecd2016-07-03 09:40:46 -06003#
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
Simon Glassd5deca02016-07-31 17:35:04 -060018- Check that image verification works
Simon Glassd977ecd2016-07-03 09:40:46 -060019- 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
Philippe Reynes5d472d32022-03-28 22:57:06 +020024For pre-load header verification:
25- Create FIT image with a pre-load header
26- Check that signature verification succeeds
27- Corrupt the FIT image
28- Check that signature verification fails
29- Launch an FIT image without a pre-load header
30- Check that image verification fails
31
Simon Glassd977ecd2016-07-03 09:40:46 -060032Tests run with both SHA1 and SHA256 hashing.
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000033
34This also tests fdt_add_pubkey utility in the simple way:
35- Create DTB and FIT files
36- Add keys with fdt_add_pubkey to DTB
37- Sign FIT image
38- Check with fit_check_sign that keys properly added to DTB file
Simon Glassd977ecd2016-07-03 09:40:46 -060039"""
40
Simon Glasse9eeca82021-09-19 15:14:48 -060041import os
Simon Glass5e942f72021-02-15 17:08:08 -070042import shutil
Teddy Reede6a47832018-06-09 11:38:05 -040043import struct
Simon Glass861b5042020-03-18 11:44:05 -060044import pytest
Simon Glassdb0e4532025-02-09 09:07:16 -070045import utils
Simon Glassc35df8f2020-03-18 11:43:59 -060046import vboot_forge
Simon Glass5e942f72021-02-15 17:08:08 -070047import vboot_evil
Simon Glassd977ecd2016-07-03 09:40:46 -060048
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000049# Common helper functions
Simon Glass32701112025-02-09 09:07:17 -070050def dtc(dts, ubman, dtc_args, datadir, tmpdir, dtb):
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000051 """Run the device tree compiler to compile a .dts file
52
53 The output file will be the same as the input file but with a .dtb
54 extension.
55
56 Args:
57 dts: Device tree file to compile.
Simon Glass32701112025-02-09 09:07:17 -070058 ubman: U-Boot console.
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000059 dtc_args: DTC arguments.
60 datadir: Path to data directory.
61 tmpdir: Path to temp directory.
62 dtb: Resulting DTB file.
63 """
64 dtb = dts.replace('.dts', '.dtb')
Simon Glass32701112025-02-09 09:07:17 -070065 utils.run_and_log(ubman, 'dtc %s %s%s -O dtb '
Simon Glassdb0e4532025-02-09 09:07:16 -070066 '-o %s%s' % (dtc_args, datadir, dts, tmpdir, dtb))
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000067
Simon Glass32701112025-02-09 09:07:17 -070068def make_fit(its, ubman, mkimage, dtc_args, datadir, fit):
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000069 """Make a new FIT from the .its source file.
70
71 This runs 'mkimage -f' to create a new FIT.
72
73 Args:
74 its: Filename containing .its source.
Simon Glass32701112025-02-09 09:07:17 -070075 ubman: U-Boot console.
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000076 mkimage: Path to mkimage utility.
77 dtc_args: DTC arguments.
78 datadir: Path to data directory.
79 fit: Resulting FIT file.
80 """
Simon Glass32701112025-02-09 09:07:17 -070081 utils.run_and_log(ubman, [mkimage, '-D', dtc_args, '-f',
Simon Glassdb0e4532025-02-09 09:07:16 -070082 '%s%s' % (datadir, its), fit])
Roman Kopytin84e2b2b2023-03-20 03:28:13 +000083
Simon Glass5e942f72021-02-15 17:08:08 -070084# Only run the full suite on a few combinations, since it doesn't add any more
85# test coverage.
Simon Glass3539c172022-08-06 17:51:52 -060086TESTDATA_IN = [
Philippe Reynes5d472d32022-03-28 22:57:06 +020087 ['sha1-basic', 'sha1', '', None, False, True, False, False],
88 ['sha1-pad', 'sha1', '', '-E -p 0x10000', False, False, False, False],
89 ['sha1-pss', 'sha1', '-pss', None, False, False, False, False],
90 ['sha1-pss-pad', 'sha1', '-pss', '-E -p 0x10000', False, False, False, False],
91 ['sha256-basic', 'sha256', '', None, False, False, False, False],
92 ['sha256-pad', 'sha256', '', '-E -p 0x10000', False, False, False, False],
93 ['sha256-pss', 'sha256', '-pss', None, False, False, False, False],
94 ['sha256-pss-pad', 'sha256', '-pss', '-E -p 0x10000', False, False, False, False],
95 ['sha256-pss-required', 'sha256', '-pss', None, True, False, False, False],
96 ['sha256-pss-pad-required', 'sha256', '-pss', '-E -p 0x10000', True, True, False, False],
97 ['sha384-basic', 'sha384', '', None, False, False, False, False],
98 ['sha384-pad', 'sha384', '', '-E -p 0x10000', False, False, False, False],
99 ['algo-arg', 'algo-arg', '', '-o sha256,rsa2048', False, False, True, False],
100 ['sha256-global-sign', 'sha256', '', '', False, False, False, True],
101 ['sha256-global-sign-pss', 'sha256', '-pss', '', False, False, False, True],
Simon Glassa0ba39d2020-03-18 11:44:00 -0600102]
103
Simon Glass3539c172022-08-06 17:51:52 -0600104# Mark all but the first test as slow, so they are not run with '-k not slow'
105TESTDATA = [TESTDATA_IN[0]]
106TESTDATA += [pytest.param(*v, marks=pytest.mark.slow) for v in TESTDATA_IN[1:]]
107
Michal Simek6e035ab2016-07-18 08:49:08 +0200108@pytest.mark.boardspec('sandbox')
Simon Glassd977ecd2016-07-03 09:40:46 -0600109@pytest.mark.buildconfigspec('fit_signature')
Stephen Warren2079db32017-09-18 11:11:49 -0600110@pytest.mark.requiredtool('dtc')
111@pytest.mark.requiredtool('fdtget')
112@pytest.mark.requiredtool('fdtput')
113@pytest.mark.requiredtool('openssl')
Philippe Reynes5d472d32022-03-28 22:57:06 +0200114@pytest.mark.parametrize("name,sha_algo,padding,sign_options,required,full_test,algo_arg,global_sign",
Simon Glass5e942f72021-02-15 17:08:08 -0700115 TESTDATA)
Simon Glassddba5202025-02-09 09:07:14 -0700116def test_vboot(ubman, name, sha_algo, padding, sign_options, required,
Philippe Reynes5d472d32022-03-28 22:57:06 +0200117 full_test, algo_arg, global_sign):
Simon Glassd977ecd2016-07-03 09:40:46 -0600118 """Test verified boot signing with mkimage and verification with 'bootm'.
119
120 This works using sandbox only as it needs to update the device tree used
121 by U-Boot to hold public keys from the signing process.
122
123 The SHA1 and SHA256 tests are combined into a single test since the
124 key-generation process is quite slow and we want to avoid doing it twice.
125 """
Philippe Reynes5d472d32022-03-28 22:57:06 +0200126 def dtc_options(dts, options):
127 """Run the device tree compiler to compile a .dts file
128
129 The output file will be the same as the input file but with a .dtb
130 extension.
131
132 Args:
133 dts: Device tree file to compile.
134 options: Options provided to the compiler.
135 """
136 dtb = dts.replace('.dts', '.dtb')
Simon Glass32701112025-02-09 09:07:17 -0700137 utils.run_and_log(ubman, 'dtc %s %s%s -O dtb -o %s%s %s' %
Simon Glassdb0e4532025-02-09 09:07:16 -0700138 (dtc_args, datadir, dts, tmpdir, dtb, options))
Philippe Reynes5d472d32022-03-28 22:57:06 +0200139
140 def run_binman(dtb):
141 """Run binman to build an image
142
143 Args:
144 dtb: Device tree file used as input file.
145 """
146 pythonpath = os.environ.get('PYTHONPATH', '')
147 os.environ['PYTHONPATH'] = pythonpath + ':' + '%s/../scripts/dtc/pylibfdt' % tmpdir
Simon Glass32701112025-02-09 09:07:17 -0700148 utils.run_and_log(ubman, [binman, 'build', '-d', "%s/%s" % (tmpdir,dtb),
Simon Glassdb0e4532025-02-09 09:07:16 -0700149 '-a', "pre-load-key-path=%s" % tmpdir, '-O',
150 tmpdir, '-I', tmpdir])
Philippe Reynes5d472d32022-03-28 22:57:06 +0200151 os.environ['PYTHONPATH'] = pythonpath
152
Simon Glass5e942f72021-02-15 17:08:08 -0700153 def run_bootm(sha_algo, test_type, expect_string, boots, fit=None):
Simon Glassd977ecd2016-07-03 09:40:46 -0600154 """Run a 'bootm' command U-Boot.
155
156 This always starts a fresh U-Boot instance since the device tree may
157 contain a new public key.
158
159 Args:
Simon Glassf223c732016-07-31 17:35:06 -0600160 test_type: A string identifying the test type.
161 expect_string: A string which is expected in the output.
162 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
163 use.
Tom Rinib65ce462016-09-18 09:46:58 -0400164 boots: A boolean that is True if Linux should boot and False if
165 we are expected to not boot
Simon Glass5e942f72021-02-15 17:08:08 -0700166 fit: FIT filename to load and verify
Simon Glassd977ecd2016-07-03 09:40:46 -0600167 """
Simon Glass5e942f72021-02-15 17:08:08 -0700168 if not fit:
169 fit = '%stest.fit' % tmpdir
Simon Glass32701112025-02-09 09:07:17 -0700170 ubman.restart_uboot()
171 with ubman.log.section('Verified boot %s %s' % (sha_algo, test_type)):
172 output = ubman.run_command_list(
Simon Glass5e942f72021-02-15 17:08:08 -0700173 ['host load hostfs - 100 %s' % fit,
Simon Glass861b5042020-03-18 11:44:05 -0600174 'fdt addr 100',
175 'bootm 100'])
176 assert expect_string in ''.join(output)
Tom Rinib65ce462016-09-18 09:46:58 -0400177 if boots:
Simon Glass861b5042020-03-18 11:44:05 -0600178 assert 'sandbox: continuing, as we cannot run' in ''.join(output)
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200179 else:
Simon Glass724c03b2020-03-18 11:44:04 -0600180 assert('sandbox: continuing, as we cannot run'
181 not in ''.join(output))
Simon Glassd977ecd2016-07-03 09:40:46 -0600182
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200183 def sign_fit(sha_algo, options):
Simon Glassd977ecd2016-07-03 09:40:46 -0600184 """Sign the FIT
185
186 Signs the FIT and writes the signature into it. It also writes the
187 public key into the dtb.
Simon Glassf223c732016-07-31 17:35:06 -0600188
189 Args:
190 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
191 use.
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200192 options: Options to provide to mkimage.
Simon Glassd977ecd2016-07-03 09:40:46 -0600193 """
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200194 args = [mkimage, '-F', '-k', tmpdir, '-K', dtb, '-r', fit]
195 if options:
196 args += options.split(' ')
Simon Glass32701112025-02-09 09:07:17 -0700197 ubman.log.action('%s: Sign images' % sha_algo)
198 utils.run_and_log(ubman, args)
Simon Glassd977ecd2016-07-03 09:40:46 -0600199
Philippe Reynes5d472d32022-03-28 22:57:06 +0200200 def sign_fit_dtb(sha_algo, options, dtb):
201 """Sign the FIT
202
203 Signs the FIT and writes the signature into it. It also writes the
204 public key into the dtb.
205
206 Args:
207 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
208 use.
209 options: Options to provide to mkimage.
210 """
211 args = [mkimage, '-F', '-k', tmpdir, '-K', dtb, '-r', fit]
212 if options:
213 args += options.split(' ')
Simon Glass32701112025-02-09 09:07:17 -0700214 ubman.log.action('%s: Sign images' % sha_algo)
215 utils.run_and_log(ubman, args)
Philippe Reynes5d472d32022-03-28 22:57:06 +0200216
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700217 def sign_fit_norequire(sha_algo, options):
218 """Sign the FIT
219
220 Signs the FIT and writes the signature into it. It also writes the
221 public key into the dtb. It does not mark key as 'required' in dtb.
222
223 Args:
224 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
225 use.
226 options: Options to provide to mkimage.
227 """
228 args = [mkimage, '-F', '-k', tmpdir, '-K', dtb, fit]
229 if options:
230 args += options.split(' ')
Simon Glass32701112025-02-09 09:07:17 -0700231 ubman.log.action('%s: Sign images' % sha_algo)
232 utils.run_and_log(ubman, args)
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700233
Teddy Reede6a47832018-06-09 11:38:05 -0400234 def replace_fit_totalsize(size):
235 """Replace FIT header's totalsize with something greater.
236
237 The totalsize must be less than or equal to FIT_SIGNATURE_MAX_SIZE.
238 If the size is greater, the signature verification should return false.
239
240 Args:
241 size: The new totalsize of the header
242
243 Returns:
244 prev_size: The previous totalsize read from the header
245 """
246 total_size = 0
247 with open(fit, 'r+b') as handle:
248 handle.seek(4)
249 total_size = handle.read(4)
250 handle.seek(4)
251 handle.write(struct.pack(">I", size))
252 return struct.unpack(">I", total_size)[0]
253
Philippe Reynes5d472d32022-03-28 22:57:06 +0200254 def corrupt_file(fit, offset, value):
255 """Corrupt a file
256
257 To corrupt a file, a value is written at the specified offset
258
259 Args:
260 fit: The file to corrupt
261 offset: Offset to write
262 value: Value written
263 """
264 with open(fit, 'r+b') as handle:
265 handle.seek(offset)
266 handle.write(struct.pack(">I", value))
267
Simon Glassb4a2f6a2020-03-18 11:44:07 -0600268 def create_rsa_pair(name):
269 """Generate a new RSA key paid and certificate
270
271 Args:
272 name: Name of of the key (e.g. 'dev')
273 """
274 public_exponent = 65537
Jamin Lin5975ad72022-01-19 16:23:21 +0800275
276 if sha_algo == "sha384":
277 rsa_keygen_bits = 3072
278 else:
279 rsa_keygen_bits = 2048
280
Simon Glass32701112025-02-09 09:07:17 -0700281 utils.run_and_log(ubman, 'openssl genpkey -algorithm RSA -out %s%s.key '
Jamin Lin5975ad72022-01-19 16:23:21 +0800282 '-pkeyopt rsa_keygen_bits:%d '
Simon Glassb4a2f6a2020-03-18 11:44:07 -0600283 '-pkeyopt rsa_keygen_pubexp:%d' %
Jamin Lin5975ad72022-01-19 16:23:21 +0800284 (tmpdir, name, rsa_keygen_bits, public_exponent))
Simon Glassb4a2f6a2020-03-18 11:44:07 -0600285
286 # Create a certificate containing the public key
Simon Glass32701112025-02-09 09:07:17 -0700287 utils.run_and_log(ubman, 'openssl req -batch -new -x509 -key %s%s.key '
Simon Glassdb0e4532025-02-09 09:07:16 -0700288 '-out %s%s.crt' % (tmpdir, name, tmpdir, name))
Simon Glassb4a2f6a2020-03-18 11:44:07 -0600289
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200290 def test_with_algo(sha_algo, padding, sign_options):
Simon Glassd5deca02016-07-31 17:35:04 -0600291 """Test verified boot with the given hash algorithm.
Simon Glassd977ecd2016-07-03 09:40:46 -0600292
293 This is the main part of the test code. The same procedure is followed
294 for both hashing algorithms.
295
296 Args:
Simon Glassf223c732016-07-31 17:35:06 -0600297 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
298 use.
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200299 padding: Either '' or '-pss', to select the padding to use for the
300 rsa signature algorithm.
301 sign_options: Options to mkimage when signing a fit image.
Simon Glassd977ecd2016-07-03 09:40:46 -0600302 """
Simon Glassdc3ab7e2016-07-31 17:35:02 -0600303 # Compile our device tree files for kernel and U-Boot. These are
304 # regenerated here since mkimage will modify them (by adding a
305 # public key) below.
Simon Glass32701112025-02-09 09:07:17 -0700306 dtc('sandbox-kernel.dts', ubman, dtc_args, datadir, tmpdir, dtb)
307 dtc('sandbox-u-boot.dts', ubman, dtc_args, datadir, tmpdir, dtb)
Simon Glassd977ecd2016-07-03 09:40:46 -0600308
309 # Build the FIT, but don't sign anything yet
Simon Glass32701112025-02-09 09:07:17 -0700310 ubman.log.action('%s: Test FIT with signed images' % sha_algo)
311 make_fit('sign-images-%s%s.its' % (sha_algo, padding), ubman, mkimage, dtc_args, datadir, fit)
Jan Kiszka30f64652022-02-03 21:43:50 +0100312 run_bootm(sha_algo, 'unsigned images', ' - OK' if algo_arg else 'dev-', True)
Simon Glassd977ecd2016-07-03 09:40:46 -0600313
314 # Sign images with our dev keys
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200315 sign_fit(sha_algo, sign_options)
Tom Rinib65ce462016-09-18 09:46:58 -0400316 run_bootm(sha_algo, 'signed images', 'dev+', True)
Simon Glassd977ecd2016-07-03 09:40:46 -0600317
318 # Create a fresh .dtb without the public keys
Simon Glass32701112025-02-09 09:07:17 -0700319 dtc('sandbox-u-boot.dts', ubman, dtc_args, datadir, tmpdir, dtb)
Simon Glassd977ecd2016-07-03 09:40:46 -0600320
Simon Glass32701112025-02-09 09:07:17 -0700321 ubman.log.action('%s: Test FIT with signed configuration' % sha_algo)
322 make_fit('sign-configs-%s%s.its' % (sha_algo, padding), ubman, mkimage, dtc_args, datadir, fit)
Jan Kiszka30f64652022-02-03 21:43:50 +0100323 run_bootm(sha_algo, 'unsigned config', '%s+ OK' % ('sha256' if algo_arg else sha_algo), True)
Simon Glassd977ecd2016-07-03 09:40:46 -0600324
325 # Sign images with our dev keys
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200326 sign_fit(sha_algo, sign_options)
Tom Rinib65ce462016-09-18 09:46:58 -0400327 run_bootm(sha_algo, 'signed config', 'dev+', True)
Simon Glassd977ecd2016-07-03 09:40:46 -0600328
Simon Glass32701112025-02-09 09:07:17 -0700329 ubman.log.action('%s: Check signed config on the host' % sha_algo)
Simon Glassd977ecd2016-07-03 09:40:46 -0600330
Simon Glass32701112025-02-09 09:07:17 -0700331 utils.run_and_log(ubman, [fit_check_sign, '-f', fit, '-k', dtb])
Simon Glassd977ecd2016-07-03 09:40:46 -0600332
Simon Glass5e942f72021-02-15 17:08:08 -0700333 if full_test:
Simon Glassb823daa2021-02-15 17:08:12 -0700334 # Make sure that U-Boot checks that the config is in the list of
335 # hashed nodes. If it isn't, a security bypass is possible.
Simon Glass5e942f72021-02-15 17:08:08 -0700336 ffit = '%stest.forged.fit' % tmpdir
337 shutil.copyfile(fit, ffit)
338 with open(ffit, 'rb') as fd:
339 root, strblock = vboot_forge.read_fdt(fd)
340 root, strblock = vboot_forge.manipulate(root, strblock)
341 with open(ffit, 'w+b') as fd:
342 vboot_forge.write_fdt(root, strblock, fd)
Simon Glassdb0e4532025-02-09 09:07:16 -0700343 utils.run_and_log_expect_exception(
Simon Glass32701112025-02-09 09:07:17 -0700344 ubman, [fit_check_sign, '-f', ffit, '-k', dtb],
Simon Glass5e942f72021-02-15 17:08:08 -0700345 1, 'Failed to verify required signature')
346
347 run_bootm(sha_algo, 'forged config', 'Bad Data Hash', False, ffit)
Simon Glassc35df8f2020-03-18 11:43:59 -0600348
Simon Glass5e942f72021-02-15 17:08:08 -0700349 # Try adding an evil root node. This should be detected.
350 efit = '%stest.evilf.fit' % tmpdir
351 shutil.copyfile(fit, efit)
352 vboot_evil.add_evil_node(fit, efit, evil_kernel, 'fakeroot')
353
Simon Glassdb0e4532025-02-09 09:07:16 -0700354 utils.run_and_log_expect_exception(
Simon Glass32701112025-02-09 09:07:17 -0700355 ubman, [fit_check_sign, '-f', efit, '-k', dtb],
Simon Glass5e942f72021-02-15 17:08:08 -0700356 1, 'Failed to verify required signature')
Simon Glass19d2c022021-02-15 17:08:11 -0700357 run_bootm(sha_algo, 'evil fakeroot', 'Bad FIT kernel image format',
358 False, efit)
Simon Glass5e942f72021-02-15 17:08:08 -0700359
360 # Try adding an @ to the kernel node name. This should be detected.
361 efit = '%stest.evilk.fit' % tmpdir
362 shutil.copyfile(fit, efit)
363 vboot_evil.add_evil_node(fit, efit, evil_kernel, 'kernel@')
364
Simon Glassb823daa2021-02-15 17:08:12 -0700365 msg = 'Signature checking prevents use of unit addresses (@) in nodes'
Simon Glassdb0e4532025-02-09 09:07:16 -0700366 utils.run_and_log_expect_exception(
Simon Glass32701112025-02-09 09:07:17 -0700367 ubman, [fit_check_sign, '-f', efit, '-k', dtb],
Simon Glassb823daa2021-02-15 17:08:12 -0700368 1, msg)
369 run_bootm(sha_algo, 'evil kernel@', msg, False, efit)
Simon Glassc35df8f2020-03-18 11:43:59 -0600370
371 # Create a new properly signed fit and replace header bytes
Simon Glass32701112025-02-09 09:07:17 -0700372 make_fit('sign-configs-%s%s.its' % (sha_algo, padding), ubman, mkimage, dtc_args, datadir, fit)
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200373 sign_fit(sha_algo, sign_options)
Simon Glassddba5202025-02-09 09:07:14 -0700374 bcfg = ubman.config.buildconfig
Teddy Reede6a47832018-06-09 11:38:05 -0400375 max_size = int(bcfg.get('config_fit_signature_max_size', 0x10000000), 0)
376 existing_size = replace_fit_totalsize(max_size + 1)
Simon Glass724c03b2020-03-18 11:44:04 -0600377 run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash',
378 False)
Simon Glass32701112025-02-09 09:07:17 -0700379 ubman.log.action('%s: Check overflowed FIT header totalsize' % sha_algo)
Teddy Reede6a47832018-06-09 11:38:05 -0400380
381 # Replace with existing header bytes
382 replace_fit_totalsize(existing_size)
383 run_bootm(sha_algo, 'signed config', 'dev+', True)
Simon Glass32701112025-02-09 09:07:17 -0700384 ubman.log.action('%s: Check default FIT header totalsize' % sha_algo)
Teddy Reede6a47832018-06-09 11:38:05 -0400385
Simon Glassd977ecd2016-07-03 09:40:46 -0600386 # Increment the first byte of the signature, which should cause failure
Simon Glass32701112025-02-09 09:07:17 -0700387 sig = utils.run_and_log(ubman, 'fdtget -t bx %s %s value' %
Simon Glassdb0e4532025-02-09 09:07:16 -0700388 (fit, sig_node))
Simon Glassd977ecd2016-07-03 09:40:46 -0600389 byte_list = sig.split()
390 byte = int(byte_list[0], 16)
Simon Glassdc3ab7e2016-07-31 17:35:02 -0600391 byte_list[0] = '%x' % (byte + 1)
Simon Glassd977ecd2016-07-03 09:40:46 -0600392 sig = ' '.join(byte_list)
Simon Glass32701112025-02-09 09:07:17 -0700393 utils.run_and_log(ubman, 'fdtput -t bx %s %s value %s' %
Simon Glassdb0e4532025-02-09 09:07:16 -0700394 (fit, sig_node, sig))
Simon Glassd977ecd2016-07-03 09:40:46 -0600395
Simon Glass724c03b2020-03-18 11:44:04 -0600396 run_bootm(sha_algo, 'Signed config with bad hash', 'Bad Data Hash',
397 False)
Simon Glassd977ecd2016-07-03 09:40:46 -0600398
Simon Glass32701112025-02-09 09:07:17 -0700399 ubman.log.action('%s: Check bad config on the host' % sha_algo)
Simon Glassdb0e4532025-02-09 09:07:16 -0700400 utils.run_and_log_expect_exception(
Simon Glass32701112025-02-09 09:07:17 -0700401 ubman, [fit_check_sign, '-f', fit, '-k', dtb],
Simon Glass861b5042020-03-18 11:44:05 -0600402 1, 'Failed to verify required signature')
Simon Glassd977ecd2016-07-03 09:40:46 -0600403
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200404 def test_required_key(sha_algo, padding, sign_options):
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200405 """Test verified boot with the given hash algorithm.
406
Simon Glass724c03b2020-03-18 11:44:04 -0600407 This function tests if U-Boot rejects an image when a required key isn't
408 used to sign a FIT.
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200409
410 Args:
Simon Glass724c03b2020-03-18 11:44:04 -0600411 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to use
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200412 padding: Either '' or '-pss', to select the padding to use for the
413 rsa signature algorithm.
414 sign_options: Options to mkimage when signing a fit image.
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200415 """
416 # Compile our device tree files for kernel and U-Boot. These are
417 # regenerated here since mkimage will modify them (by adding a
418 # public key) below.
Simon Glass32701112025-02-09 09:07:17 -0700419 dtc('sandbox-kernel.dts', ubman, dtc_args, datadir, tmpdir, dtb)
420 dtc('sandbox-u-boot.dts', ubman, dtc_args, datadir, tmpdir, dtb)
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200421
Simon Glass32701112025-02-09 09:07:17 -0700422 ubman.log.action('%s: Test FIT with configs images' % sha_algo)
Simon Glass724c03b2020-03-18 11:44:04 -0600423
424 # Build the FIT with prod key (keys required) and sign it. This puts the
425 # signature into sandbox-u-boot.dtb, marked 'required'
Simon Glass32701112025-02-09 09:07:17 -0700426 make_fit('sign-configs-%s%s-prod.its' % (sha_algo, padding), ubman, mkimage, dtc_args, datadir, fit)
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200427 sign_fit(sha_algo, sign_options)
Simon Glass724c03b2020-03-18 11:44:04 -0600428
429 # Build the FIT with dev key (keys NOT required). This adds the
430 # signature into sandbox-u-boot.dtb, NOT marked 'required'.
Simon Glass32701112025-02-09 09:07:17 -0700431 make_fit('sign-configs-%s%s.its' % (sha_algo, padding), ubman, mkimage, dtc_args, datadir, fit)
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700432 sign_fit_norequire(sha_algo, sign_options)
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200433
Simon Glass724c03b2020-03-18 11:44:04 -0600434 # So now sandbox-u-boot.dtb two signatures, for the prod and dev keys.
435 # Only the prod key is set as 'required'. But FIT we just built has
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700436 # a dev signature only (sign_fit_norequire() overwrites the FIT).
Simon Glass724c03b2020-03-18 11:44:04 -0600437 # Try to boot the FIT with dev key. This FIT should not be accepted by
438 # U-Boot because the prod key is required.
439 run_bootm(sha_algo, 'required key', '', False)
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200440
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700441 # Build the FIT with dev key (keys required) and sign it. This puts the
442 # signature into sandbox-u-boot.dtb, marked 'required'.
Simon Glass32701112025-02-09 09:07:17 -0700443 make_fit('sign-configs-%s%s.its' % (sha_algo, padding), ubman, mkimage, dtc_args, datadir, fit)
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700444 sign_fit(sha_algo, sign_options)
445
446 # Set the required-mode policy to "any".
447 # So now sandbox-u-boot.dtb two signatures, for the prod and dev keys.
448 # Both the dev and prod key are set as 'required'. But FIT we just built has
449 # a dev signature only (sign_fit() overwrites the FIT).
450 # Try to boot the FIT with dev key. This FIT should be accepted by
451 # U-Boot because the dev key is required and policy is "any" required key.
Simon Glass32701112025-02-09 09:07:17 -0700452 utils.run_and_log(ubman, 'fdtput -t s %s /signature required-mode any' %
Simon Glassdb0e4532025-02-09 09:07:16 -0700453 dtb)
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700454 run_bootm(sha_algo, 'multi required key', 'dev+', True)
455
456 # Set the required-mode policy to "all".
457 # So now sandbox-u-boot.dtb two signatures, for the prod and dev keys.
458 # Both the dev and prod key are set as 'required'. But FIT we just built has
459 # a dev signature only (sign_fit() overwrites the FIT).
460 # Try to boot the FIT with dev key. This FIT should not be accepted by
461 # U-Boot because the prod key is required and policy is "all" required key
Simon Glass32701112025-02-09 09:07:17 -0700462 utils.run_and_log(ubman, 'fdtput -t s %s /signature required-mode all' %
Simon Glassdb0e4532025-02-09 09:07:16 -0700463 dtb)
Thirupathaiah Annapureddy7e703f72020-08-16 23:01:10 -0700464 run_bootm(sha_algo, 'multi required key', '', False)
465
Philippe Reynes5d472d32022-03-28 22:57:06 +0200466 def test_global_sign(sha_algo, padding, sign_options):
467 """Test global image signature with the given hash algorithm and padding.
468
469 Args:
470 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to use
471 padding: Either '' or '-pss', to select the padding to use for the
472 rsa signature algorithm.
473 """
474
475 dtb = '%ssandbox-u-boot-global%s.dtb' % (tmpdir, padding)
Simon Glass32701112025-02-09 09:07:17 -0700476 ubman.config.dtb = dtb
Philippe Reynes5d472d32022-03-28 22:57:06 +0200477
478 # Compile our device tree files for kernel and U-Boot. These are
479 # regenerated here since mkimage will modify them (by adding a
480 # public key) below.
Simon Glass32701112025-02-09 09:07:17 -0700481 dtc('sandbox-kernel.dts', ubman, dtc_args, datadir, tmpdir, dtb)
Philippe Reynes5d472d32022-03-28 22:57:06 +0200482 dtc_options('sandbox-u-boot-global%s.dts' % padding, '-p 1024')
483
484 # Build the FIT with dev key (keys NOT required). This adds the
485 # signature into sandbox-u-boot.dtb, NOT marked 'required'.
Simon Glass32701112025-02-09 09:07:17 -0700486 make_fit('simple-images.its', ubman, mkimage, dtc_args, datadir, fit)
Philippe Reynes5d472d32022-03-28 22:57:06 +0200487 sign_fit_dtb(sha_algo, '', dtb)
488
489 # Build the dtb for binman that define the pre-load header
490 # with the global sigature.
Simon Glass32701112025-02-09 09:07:17 -0700491 dtc('sandbox-binman%s.dts' % padding, ubman, dtc_args, datadir, tmpdir, dtb)
Philippe Reynes5d472d32022-03-28 22:57:06 +0200492
493 # Run binman to create the final image with the not signed fit
494 # and the pre-load header that contains the global signature.
495 run_binman('sandbox-binman%s.dtb' % padding)
496
497 # Check that the signature is correctly verified by u-boot
498 run_bootm(sha_algo, 'global image signature',
499 'signature check has succeed', True, "%ssandbox.img" % tmpdir)
500
501 # Corrupt the image (just one byte after the pre-load header)
502 corrupt_file("%ssandbox.img" % tmpdir, 4096, 255);
503
504 # Check that the signature verification fails
505 run_bootm(sha_algo, 'global image signature',
506 'signature check has failed', False, "%ssandbox.img" % tmpdir)
507
508 # Check that the boot fails if the global signature is not provided
509 run_bootm(sha_algo, 'global image signature', 'signature is mandatory', False)
510
Simon Glass32701112025-02-09 09:07:17 -0700511 tmpdir = os.path.join(ubman.config.result_dir, name) + '/'
Simon Glasse9eeca82021-09-19 15:14:48 -0600512 if not os.path.exists(tmpdir):
513 os.mkdir(tmpdir)
Simon Glass32701112025-02-09 09:07:17 -0700514 datadir = ubman.config.source_dir + '/test/py/tests/vboot/'
Simon Glassd977ecd2016-07-03 09:40:46 -0600515 fit = '%stest.fit' % tmpdir
Simon Glass32701112025-02-09 09:07:17 -0700516 mkimage = ubman.config.build_dir + '/tools/mkimage'
517 binman = ubman.config.source_dir + '/tools/binman/binman'
518 fit_check_sign = ubman.config.build_dir + '/tools/fit_check_sign'
Simon Glassd977ecd2016-07-03 09:40:46 -0600519 dtc_args = '-I dts -O dtb -i %s' % tmpdir
520 dtb = '%ssandbox-u-boot.dtb' % tmpdir
Philippe Reynesa28e9222018-11-14 13:51:05 +0100521 sig_node = '/configurations/conf-1/signature'
Simon Glassd977ecd2016-07-03 09:40:46 -0600522
Simon Glassb4a2f6a2020-03-18 11:44:07 -0600523 create_rsa_pair('dev')
524 create_rsa_pair('prod')
Philippe Reynes1d5ef522019-09-18 16:04:53 +0200525
Simon Glassd977ecd2016-07-03 09:40:46 -0600526 # Create a number kernel image with zeroes
Simon Glass5e942f72021-02-15 17:08:08 -0700527 with open('%stest-kernel.bin' % tmpdir, 'wb') as fd:
528 fd.write(500 * b'\0')
529
530 # Create a second kernel image with ones
531 evil_kernel = '%stest-kernel1.bin' % tmpdir
532 with open(evil_kernel, 'wb') as fd:
533 fd.write(500 * b'\x01')
Simon Glassd977ecd2016-07-03 09:40:46 -0600534
Heinrich Schuchardt25da81a2023-12-11 19:07:33 +0100535 # We need to use our own device tree file. Remember to restore it
536 # afterwards.
Simon Glass32701112025-02-09 09:07:17 -0700537 old_dtb = ubman.config.dtb
Simon Glassd977ecd2016-07-03 09:40:46 -0600538 try:
Simon Glass32701112025-02-09 09:07:17 -0700539 ubman.config.dtb = dtb
Philippe Reynes5d472d32022-03-28 22:57:06 +0200540 if global_sign:
541 test_global_sign(sha_algo, padding, sign_options)
542 elif required:
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200543 test_required_key(sha_algo, padding, sign_options)
Simon Glassa0ba39d2020-03-18 11:44:00 -0600544 else:
Philippe Reynes2fbd17c2020-04-29 15:26:16 +0200545 test_with_algo(sha_algo, padding, sign_options)
Simon Glassd977ecd2016-07-03 09:40:46 -0600546 finally:
Simon Glass37c2ce12016-07-31 17:35:08 -0600547 # Go back to the original U-Boot with the correct dtb.
Simon Glass32701112025-02-09 09:07:17 -0700548 ubman.config.dtb = old_dtb
549 ubman.restart_uboot()
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000550
551
552TESTDATA_IN = [
553 ['sha1-basic', 'sha1', '', None, False],
554 ['sha1-pad', 'sha1', '', '-E -p 0x10000', False],
555 ['sha1-pss', 'sha1', '-pss', None, False],
556 ['sha1-pss-pad', 'sha1', '-pss', '-E -p 0x10000', False],
557 ['sha256-basic', 'sha256', '', None, False],
558 ['sha256-pad', 'sha256', '', '-E -p 0x10000', False],
559 ['sha256-pss', 'sha256', '-pss', None, False],
560 ['sha256-pss-pad', 'sha256', '-pss', '-E -p 0x10000', False],
561 ['sha256-pss-required', 'sha256', '-pss', None, False],
562 ['sha256-pss-pad-required', 'sha256', '-pss', '-E -p 0x10000', False],
563 ['sha384-basic', 'sha384', '', None, False],
564 ['sha384-pad', 'sha384', '', '-E -p 0x10000', False],
565 ['algo-arg', 'algo-arg', '', '-o sha256,rsa2048', True],
566 ['sha256-global-sign', 'sha256', '', '', False],
567 ['sha256-global-sign-pss', 'sha256', '-pss', '', False],
568]
569
570# Mark all but the first test as slow, so they are not run with '-k not slow'
571TESTDATA = [TESTDATA_IN[0]]
572TESTDATA += [pytest.param(*v, marks=pytest.mark.slow) for v in TESTDATA_IN[1:]]
573
574@pytest.mark.boardspec('sandbox')
575@pytest.mark.buildconfigspec('fit_signature')
576@pytest.mark.requiredtool('dtc')
577@pytest.mark.requiredtool('openssl')
578@pytest.mark.parametrize("name,sha_algo,padding,sign_options,algo_arg", TESTDATA)
Simon Glassddba5202025-02-09 09:07:14 -0700579def test_fdt_add_pubkey(ubman, name, sha_algo, padding, sign_options, algo_arg):
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000580 """Test fdt_add_pubkey utility with bunch of different algo options."""
581
582 def sign_fit(sha_algo, options):
583 """Sign the FIT
584
585 Signs the FIT and writes the signature into it.
586
587 Args:
588 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to
589 use.
590 options: Options to provide to mkimage.
591 """
592 args = [mkimage, '-F', '-k', tmpdir, fit]
593 if options:
594 args += options.split(' ')
Simon Glass32701112025-02-09 09:07:17 -0700595 ubman.log.action('%s: Sign images' % sha_algo)
596 utils.run_and_log(ubman, args)
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000597
598 def test_add_pubkey(sha_algo, padding, sign_options):
599 """Test fdt_add_pubkey utility with given hash algorithm and padding.
600
601 This function tests if fdt_add_pubkey utility may add public keys into dtb.
602
603 Args:
604 sha_algo: Either 'sha1' or 'sha256', to select the algorithm to use
605 padding: Either '' or '-pss', to select the padding to use for the
606 rsa signature algorithm.
607 sign_options: Options to mkimage when signing a fit image.
608 """
609
610 # Create a fresh .dtb without the public keys
Simon Glass32701112025-02-09 09:07:17 -0700611 dtc('sandbox-u-boot.dts', ubman, dtc_args, datadir, tmpdir, dtb)
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000612
Simon Glass32701112025-02-09 09:07:17 -0700613 ubman.log.action('%s: Test fdt_add_pubkey with signed configuration' % sha_algo)
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000614 # Then add the dev key via the fdt_add_pubkey tool
Simon Glass32701112025-02-09 09:07:17 -0700615 utils.run_and_log(ubman,
Simon Glassdb0e4532025-02-09 09:07:16 -0700616 [fdt_add_pubkey, '-a', '%s,%s' %
617 ('sha256' if algo_arg else sha_algo,
618 'rsa3072' if sha_algo == 'sha384' else 'rsa2048'),
619 '-k', tmpdir, '-n', 'dev', '-r', 'conf', dtb])
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000620
Simon Glass32701112025-02-09 09:07:17 -0700621 make_fit('sign-configs-%s%s.its' % (sha_algo, padding), ubman, mkimage, dtc_args, datadir, fit)
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000622
623 # Sign images with our dev keys
624 sign_fit(sha_algo, sign_options)
625
626 # Check with fit_check_sign that FIT is signed with key
Simon Glass32701112025-02-09 09:07:17 -0700627 utils.run_and_log(ubman, [fit_check_sign, '-f', fit, '-k', dtb])
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000628
Simon Glass32701112025-02-09 09:07:17 -0700629 tmpdir = os.path.join(ubman.config.result_dir, name) + '/'
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000630 if not os.path.exists(tmpdir):
631 os.mkdir(tmpdir)
Simon Glass32701112025-02-09 09:07:17 -0700632 datadir = ubman.config.source_dir + '/test/py/tests/vboot/'
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000633 fit = '%stest.fit' % tmpdir
Simon Glass32701112025-02-09 09:07:17 -0700634 mkimage = ubman.config.build_dir + '/tools/mkimage'
635 binman = ubman.config.source_dir + '/tools/binman/binman'
636 fit_check_sign = ubman.config.build_dir + '/tools/fit_check_sign'
637 fdt_add_pubkey = ubman.config.build_dir + '/tools/fdt_add_pubkey'
Roman Kopytin84e2b2b2023-03-20 03:28:13 +0000638 dtc_args = '-I dts -O dtb -i %s' % tmpdir
639 dtb = '%ssandbox-u-boot.dtb' % tmpdir
640
641 # keys created in test_vboot test
642
643 test_add_pubkey(sha_algo, padding, sign_options)