Implement signing FIT images during image build
cover Implement signing FIT images during image build
From: Alexander Kochetkov <[email protected]>
Hello!
I've done verified boot on Radxa Rock 3A. I've embedded public key in U-Boot SPL and signed FIT image configuration. All the work was done during U-Boot image build. For some use cases building and signing images in one go will be much simple, than building unsigned images and signing later. For example SPL-image for rk3568 called idbloader.img consist of TPL, U-boot SPL and U-boot SPL DTB with public key. So in order to assemble signed idbloader.img lately we have to keep all the intermediate files used during build.
To embed public key, I've replaced u-boot-spl node with blob-ext and generated u-boot-spl-with-pubkey-dtb blob using u-boot-spl-pubkey-dtb entry.
To sign FIT image I've used newly implemented fit property 'fit,sign'.
I haven't sign FIT image nodes, because I had realized that signing configuration is safe and sufficient for verified boot. But I doubt. So I've left that signing scheme in the test.
What do you think, is using signed configuration and signed images at the same time is much safer or doesn't provide any benefits?
Now I thinking about implementing configuration option, something like FIT_SIGNATURE_KEYDIR. The value of the option will be passed to binman using -I.
Alsi I want to embed another public key in the configuration DTB, so it will be used to verify kernel FIT. But I couldn't figure out how to do it using binman.
&binman {
u-boot-spl-with-pubkey-dtb {
filename = "u-boot-spl-with-pubkey-dtb.bin";
u-boot-spl-nodtb {
};
u-boot-spl-pubkey-dtb {
algo = "sha256,rsa2048";
required = "conf";
key-name-hint = "uboot-spl";
};
};
simple-bin {
...
mkimage {
...#ifdef CONFIG_ROCKCHIP_EXTERNAL_TPL
rockchip-tpl {
};
#elif defined(CONFIG_TPL)
u-boot-tpl {
};
#endif
blob-ext {
filename = "u-boot-spl-with-pubkey-dtb.bin";
};
};
fit: fit {
...
fit,sign;
...
configurations {
default = "@config-DEFAULT-SEQ";
@config-SEQ {
...
#ifdef CONFIG_SPL_FIT_SIGNATURE
signature {
algo = "sha256,rsa2048";
key-name-hint = "uboot-spl";
sign-images = "firmware", "loadables", "fdt";
};
#endif
};
};
};
};
}Alexander Kochetkov (3):
binman: fix passing loadables to mkimage on first run
image-host: fix 'unknown error' error message
binman: implement signing FIT images during image build
tools/binman/btool/mkimage.py | 5 +- tools/binman/entries.rst | 7 ++ tools/binman/etype/fit.py | 57 +++++++++++++- tools/binman/ftest.py | 95 ++++++++++++++++++++++++ tools/binman/test/326_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/326_rsa2048.key | 28 +++++++ tools/binman/test/327_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/328_fit_signature.dts | 61 +++++++++++++++ tools/image-host.c | 2 +- 9 files changed, 446 insertions(+), 5 deletions(-) create mode 100644 tools/binman/test/326_fit_signature.dts create mode 100644 tools/binman/test/326_rsa2048.key create mode 100644 tools/binman/test/327_fit_signature.dts create mode 100644 tools/binman/test/328_fit_signature.dts
Signature · 1 line
2.17.1
PATCH 1/3 binman: fix passing loadables to mkimage on first run
From: Alexander Kochetkov <[email protected]>
FIT use mkimage from BuildSectionData() to build FIT entry contents. BuildSectionData() get called several times during building FIT image.
Currently when fit inserts loadables, it use self._loadables property that contain loadables computed during previuos BuildSectionData() invocation. So for the first run it use empty list and pass no loadables to mkimage.
That makes problem for adding signature to FIT image because mkimage fails to add signature and aborts building FIT if no loadables provided.
The patch fixes described behaviour in a way that BuildSectionData() uses recently calculated loadables value, not previosly calculated.
--- tools/binman/etype/fit.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/binman/etype/fit.py +3 -3
@@ -542,8 +542,8 @@ class Entry_fit(Entry_section): """ val = fdt_util.GetStringList(node, 'fit,firmware') if val is None: - return None, self._loadables - valid_entries = list(self._loadables) + return None, loadables + valid_entries = list(loadables) for name, entry in self.GetEntries().items(): missing = [] entry.CheckMissing(missing)
@@ -558,7 +558,7 @@ class Entry_fit(Entry_section): firmware = name elif name not in result: result.append(name) - for name in self._loadables: + for name in loadables: if name != firmware and name not in result: result.append(name) return firmware, result
Signature · 1 line
2.17.1
Re: [PATCH 1/3] binman: fix passing loadables to mkimage on first run
Quoted 13 lines · On Mon, 16 Sept 2024 at 02:25, <[email protected]> wrote:
From: Alexander Kochetkov <[email protected]>
FIT use mkimage from
BuildSectionData()to build FIT entry contents.BuildSectionData()get called several times during building FIT image.Currently when fit inserts loadables, it use self._loadables property that contain loadables computed during previuos
BuildSectionData()invocation. So for the first run it use empty list and pass no loadables to mkimage.That makes problem for adding signature to FIT image because mkimage fails to add signature and aborts building FIT if no loadables provided.
Quoted 7 lines
The patch fixes described behaviour in a way that
BuildSectionData()uses recently calculated loadables value, not previosly calculated.Signed-off-by Alexander Kochetkov <[email protected]>--- tools/binman/etype/fit.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 28 lines
tools/binman/etype/fit.py +3
-4@@ -542,8 +542,8 @@ class Entry_fit(Entry_section): """ val = fdt_util.GetStringList(node, 'fit,firmware') if val is None: - return None, self._loadables - valid_entries = list(self._loadables) + return None, loadables + valid_entries = list(loadables) for name, entry in self.GetEntries().items(): missing = [] entry.CheckMissing(missing)@@ -558,7 +558,7 @@ class Entry_fit(Entry_section): firmware = name elif name not in result: result.append(name) - for name in self._loadables: + for name in loadables: if name != firmware and name not in result: result.append(name) return firmware, result --2.17.1
Simon Glass
Reviewed-byRe: [PATCH 1/3] binman: fix passing loadables to mkimage on first run
Quoted 13 lines · On Mon, 16 Sept 2024 at 02:25, <[email protected]> wrote:
From: Alexander Kochetkov <[email protected]>
FIT use mkimage from
BuildSectionData()to build FIT entry contents.BuildSectionData()get called several times during building FIT image.Currently when fit inserts loadables, it use self._loadables property that contain loadables computed during previuos
BuildSectionData()invocation. So for the first run it use empty list and pass no loadables to mkimage.That makes problem for adding signature to FIT image because mkimage fails to add signature and aborts building FIT if no loadables provided.
Quoted 7 lines
The patch fixes described behaviour in a way that
BuildSectionData()uses recently calculated loadables value, not previosly calculated.Signed-off-by Alexander Kochetkov <[email protected]>--- tools/binman/etype/fit.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 1 line
Applied to u-boot-dm, thanks!
PATCH 2/3 image-host: fix 'unknown error' error message
In reply to [email protected]From: Alexander Kochetkov <[email protected]>
Fix error message like this:
Can't add verification data for node 'fdt-1' (<unknown error>)
We get unknown error because we decode error as fdt error but actually it is system error.
--- tools/image-host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/image-host.c +1 -1
@@ -1333,7 +1333,7 @@ int fit_add_verification_data(const char *keydir, const char *keyfile,if (ret) {fprintf (stderr,"Can't add verification data for node '%s' (%s)\n" ,fdt_get_name (fit, noffset,NULL ), -fdt_strerror (ret)); +strerror (-ret));return ret; } }
Signature · 1 line
2.17.1
Re: [PATCH 2/3] image-host: fix 'unknown error' error message
Quoted 6 lines · On Mon, 16 Sept 2024 at 02:25, <[email protected]> wrote:
From: Alexander Kochetkov <[email protected]>
Fix error message like this:
Can't add verification data for node 'fdt-1' (<unknown error>)
Quoted 7 lines
We get unknown error because we decode error as fdt error but actually it is system error.
Signed-off-by Alexander Kochetkov <[email protected]>--- tools/image-host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 17 lines
tools/image-host.c +1
-2@@ -1333,7 +1333,7 @@ int fit_add_verification_data(const char *keydir, const char *keyfile,if (ret) {fprintf (stderr,"Can't add verification data for node '%s' (%s)\n" ,fdt_get_name (fit, noffset,NULL ), -fdt_strerror (ret)); +strerror (-ret));return ret; } } --2.17.1
Simon Glass
Reviewed-byRe: [PATCH 2/3] image-host: fix 'unknown error' error message
Quoted 6 lines · On Mon, 16 Sept 2024 at 02:25, <[email protected]> wrote:
From: Alexander Kochetkov <[email protected]>
Fix error message like this:
Can't add verification data for node 'fdt-1' (<unknown error>)
Quoted 7 lines
We get unknown error because we decode error as fdt error but actually it is system error.
Signed-off-by Alexander Kochetkov <[email protected]>--- tools/image-host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 1 line
Applied to u-boot-dm, thanks!
PATCH 3/3 binman: implement signing FIT images during image build
In reply to [email protected]From: Alexander Kochetkov <[email protected]>
The patch implement new property 'fit,sign' that can be declared at the top-level 'fit' node. If that option is declared, fit tryies to detect private keys directory among binman include directories. That directory than passed to mkimage using '-k' flag and that enable signing of FIT.
--- tools/binman/btool/mkimage.py | 5 +- tools/binman/entries.rst | 7 ++ tools/binman/etype/fit.py | 51 +++++++++++++ tools/binman/ftest.py | 95 ++++++++++++++++++++++++ tools/binman/test/326_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/326_rsa2048.key | 28 +++++++ tools/binman/test/327_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/328_fit_signature.dts | 61 +++++++++++++++ 8 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 tools/binman/test/326_fit_signature.dts create mode 100644 tools/binman/test/326_rsa2048.key create mode 100644 tools/binman/test/327_fit_signature.dts create mode 100644 tools/binman/test/328_fit_signature.dts
tools/binman/btool/mkimage.py +4 -1
@@ -22,7 +22,7 @@ class Bintoolmkimage(bintool.Bintool): # pylint: disable=R0913 def run(self, reset_timestamp=False, output_fname=None, external=False, - pad=None, align=None): + pad=None, align=None, priv_keys_dir=None): """Run mkimage Args:
@@ -34,6 +34,7 @@ class Bintoolmkimage(bintool.Bintool): other things to be easily added later, if required, such as signatures align: Bytes to use for alignment of the FIT and its external data + priv_keys_dir: Path to directory containing private keys version: True to get the mkimage version """ args = []
@@ -45,6 +46,8 @@ class Bintoolmkimage(bintool.Bintool): args += ['-B', f'{align:x}'] if reset_timestamp: args.append('-t') + if priv_keys_dir: + args += ['-k', f'{priv_keys_dir}'] if output_fname: args += ['-F', output_fname] return self.run_cmd(*args)
tools/binman/entries.rst +7 -0
@@ -815,6 +815,13 @@ The top-level 'fit' node supports the following special properties: fit,fdt-list-val = "dtb1", "dtb2"; + fit,sign + Enable signing FIT images via mkimage as described in + verified-boot.rst. If the property is found, the private keys path is + detected among binman include directories and passed to mkimage via + -k flag. All the keys required for signing FIT must be available at + time of signing and must be located in single include directory. + Substitutions ~~~~~~~~~~~~~
tools/binman/etype/fit.py +51 -0
81 lines not on this page · raw message
tools/binman/ftest.py +95 -0
101 lines not on this page · raw message
tools/binman/test/326_fit_signature.dts new file +98 -0
98 lines not on this page · raw message
tools/binman/test/326_rsa2048.key new file +28 -0
@@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDVUiT2JAF8Ajcx +3XTB5qdGxuPMVFcXKJH+4L66oSt4YUBGi1bClo80U2azu08BTzk2Jzv6hez/mvzL +hBvL3WnPwMl5vdOxb1kvUQyKLSw2bkM8VB0X1jGsKsKjzArg/aI8RknfiaSc5jua +2lqwUFwv2RMF8jvIMN/1GnTLdECeMFVgVFSFkzIocISAHGPoGUOxTf8xK7o0x4RX +NzB+95RtIqTQ5Az/KPVCOcQR5ETrUBXHF1I0rYjJjHHO4dUxxfDqFabt60EzQ/R2 +oZu58C4y0TrRI98g4hVPBYapildWjaNQm1Exa4ZaSDVl01OXsFW9Dm80PqfW4tTH +Cm4nuCq5AgMBAAECggEBAIoG5b2SHJfFwzrzpQmVmeTU6i6a3+MvMBAwEZkmkb8J +hhJfNFsiGjTsRgbDiuI5BbbBejCmmWvmN+3jZCzr7fwsLPEl36TufFF+atO5WOM7 +Qyv07QIwaOGSpXBgpSVhV6kSfdgy8p1G54hSAt4UkSGwnnt5ei8VWMP6Q1oltW3k +f9DQ/ar4UEVa4jlJU3xqchcUTiKBKSH6pMC/Fqlq8x5JTLmk1Yb6C2UNcgJYez1u +sHkdCA0FG3rFPrpFoQ1LUjMj1uEYNAxM3jOxE7Uvmk4yo9WpQDY7cRb2+Th9YY8a +IKQ2s81Yg2TmkGzr8f5nrZz3WbAmQhQgsKbwlo6snjUCgYEA7kBOt0JlU7bJTfOr +9s51g2VUfIH9lDS2Eh8MY+Bt6Y0Kdw/UK4HR8ZlN/nn0bHuHkc12K8lXEsQpgIEW +DaqHytZJHqFs2egzKu/IvQYZ2WXEMj47LZQxEDHO9gtjE+5qCW9yJGqxW9BJKPVD +F4spus4NqC+yD5OHM+6ESUtL/wMCgYEA5TZj6OHmECeh3efrwHqjDcjrqQbOTozU +KPCNCY3Pv4Cg4xas/L93TE2CY6HJTr6mwEMUM+M4Ujjj15VCmSDQ/YYcGau1jo+f +XdphOEENrPwoe9ATWIyBpT/wDrEz3L6JbE9dWMYY8vKYESt3qhVqDlbpmnYl8Jm+ +O3r5Cy2NlJMCgYEAyqzsCZuy5QcesnByvm8dqpxdxdkzJYu9wyakfKZj+gUgfO57 +OFOkjFk07yFB27MuPctCFredmfpDr+ygHRoPkG7AHw2Fss2EEaeP5bU18ilPQMqN +vxVMs5EblVVUgJUVoVcsC2yz2f4S7oPOAk5BPoehOIzydauznWrvIAas7I8CgYBr +CFHxLoNq6cbZQ3JACERZrIf2/vmZjoOHtoR1gKYRK7R1NmKDB7lihRMtCSBix/4/ +61Lkw+bJ5kzmn4lgzgUpTdWTWy5FquVlQxOA3EfRjlItNsXB5KKpksi7Y53vJ34u +eIUDbkW6NPQzmFOhtaw3k/gzq5Yd2v0M82iWAqiJRwKBgQCl2+e2cjISK31QhKTC +puhwQ0/YuC3zlwMXQgB3nPw8b9RlaDTMrRBCIUFIrrX11tHswGWpyVsxW2AvZ3Zm +jsWpwGkUdpRdXJBhSaisV/PA+x3kYhpibzEI8FrzhU69zNROCb8CTkN4WcdBdq6J +PUh/jRtKoE79qrlnIlNvFoz2gQ== +-----END PRIVATE KEY-----
tools/binman/test/327_fit_signature.dts new file +98 -0
98 lines not on this page · raw message
tools/binman/test/328_fit_signature.dts new file +61 -0
61 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [PATCH 3/3] binman: implement signing FIT images during image build
Quoted 10 lines · On Mon, 16 Sept 2024 at 02:25, <[email protected]> wrote:
From: Alexander Kochetkov <[email protected]>
The patch implement new property 'fit,sign' that can be declared at the top-level 'fit' node. If that option is declared, fit tryies to detect private keys directory among binman include directories. That directory than passed to mkimage using '-k' flag and that enable signing of FIT.
Signed-off-by Alexander Kochetkov <[email protected]>
Quoted 15 lines
--- tools/binman/btool/mkimage.py | 5 +- tools/binman/entries.rst | 7 ++ tools/binman/etype/fit.py | 51 +++++++++++++ tools/binman/ftest.py | 95 ++++++++++++++++++++++++ tools/binman/test/326_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/326_rsa2048.key | 28 +++++++ tools/binman/test/327_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/328_fit_signature.dts | 61 +++++++++++++++ 8 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 tools/binman/test/326_fit_signature.dts create mode 100644 tools/binman/test/326_rsa2048.key create mode 100644 tools/binman/test/327_fit_signature.dts create mode 100644 tools/binman/test/328_fit_signature.dts
Nice
Simon Glass
Reviewed-byRe: [PATCH 3/3] binman: implement signing FIT images during image build
Quoted 10 lines · On Mon, 16 Sept 2024 at 02:25, <[email protected]> wrote:
From: Alexander Kochetkov <[email protected]>
The patch implement new property 'fit,sign' that can be declared at the top-level 'fit' node. If that option is declared, fit tryies to detect private keys directory among binman include directories. That directory than passed to mkimage using '-k' flag and that enable signing of FIT.
Signed-off-by Alexander Kochetkov <[email protected]>
Quoted 15 lines
--- tools/binman/btool/mkimage.py | 5 +- tools/binman/entries.rst | 7 ++ tools/binman/etype/fit.py | 51 +++++++++++++ tools/binman/ftest.py | 95 ++++++++++++++++++++++++ tools/binman/test/326_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/326_rsa2048.key | 28 +++++++ tools/binman/test/327_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/328_fit_signature.dts | 61 +++++++++++++++ 8 files changed, 442 insertions(+), 1 deletion(-) create mode 100644 tools/binman/test/326_fit_signature.dts create mode 100644 tools/binman/test/326_rsa2048.key create mode 100644 tools/binman/test/327_fit_signature.dts create mode 100644 tools/binman/test/328_fit_signature.dts
Nice
Applied to u-boot-dm, thanks!
Hi,
Quoted 16 lines · On Mon, 16 Sept 2024 at 02:24, <[email protected]> wrote:
From: Alexander Kochetkov <[email protected]>
Hello!
I've done verified boot on Radxa Rock 3A. I've embedded public key in U-Boot SPL and signed FIT image configuration. All the work was done during U-Boot image build. For some use cases building and signing images in one go will be much simple, than building unsigned images and signing later. For example SPL-image for rk3568 called idbloader.img consist of TPL, U-boot SPL and U-boot SPL DTB with public key. So in order to assemble signed idbloader.img lately we have to keep all the intermediate files used during build.
To embed public key, I've replaced u-boot-spl node with blob-ext and generated u-boot-spl-with-pubkey-dtb blob using u-boot-spl-pubkey-dtb entry.
Quoted 8 lines
To sign FIT image I've used newly implemented fit property 'fit,sign'.
I haven't sign FIT image nodes, because I had realized that signing configuration is safe and sufficient for verified boot. But I doubt. So I've left that signing scheme in the test.
What do you think, is using signed configuration and signed images at the same time is much safer or doesn't provide any benefits?
So long as you have hashes on the images, then yes it is sufficient.
Quoted 4 lines
Now I thinking about implementing configuration option, something like
FIT_SIGNATURE_KEYDIR. The value of the option will be passed to binman using -I.
I suppose you saw entryargs - could that help?
Quoted 4 lines
Alsi I want to embed another public key in the configuration DTB, so it will be used to verify kernel FIT. But I couldn't figure out how to do it using binman.
I'm not sure what you are trying to do here. The kernel image (or really, its hash) would normally be verified using the same signature as for everything else in the configuration.
Quoted 75 lines
&binman { u-boot-spl-with-pubkey-dtb { filename = "u-boot-spl-with-pubkey-dtb.bin"; u-boot-spl-nodtb { }; u-boot-spl-pubkey-dtb { algo = "sha256,rsa2048"; required = "conf"; key-name-hint = "uboot-spl"; }; }; simple-bin { ... mkimage { ...#ifdef CONFIG_ROCKCHIP_EXTERNAL_TPL rockchip-tpl { }; #elif defined(CONFIG_TPL) u-boot-tpl { }; #endif blob-ext { filename = "u-boot-spl-with-pubkey-dtb.bin"; }; }; fit: fit { ... fit,sign; ... configurations { default = "@config-DEFAULT-SEQ"; @config-SEQ { ... #ifdef CONFIG_SPL_FIT_SIGNATURE signature { algo = "sha256,rsa2048"; key-name-hint = "uboot-spl"; sign-images = "firmware", "loadables", "fdt"; }; #endif }; }; }; }; }Alexander Kochetkov (3):
binman: fix passing loadables to mkimage on first run
image-host: fix 'unknown error' error message
binman: implement signing FIT images during image buildtools/binman/btool/mkimage.py | 5 +- tools/binman/entries.rst | 7 ++ tools/binman/etype/fit.py | 57 +++++++++++++- tools/binman/ftest.py | 95 ++++++++++++++++++++++++ tools/binman/test/326_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/326_rsa2048.key | 28 +++++++ tools/binman/test/327_fit_signature.dts | 98 +++++++++++++++++++++++++ tools/binman/test/328_fit_signature.dts | 61 +++++++++++++++ tools/image-host.c | 2 +- 9 files changed, 446 insertions(+), 5 deletions(-) create mode 100644 tools/binman/test/326_fit_signature.dts create mode 100644 tools/binman/test/326_rsa2048.key create mode 100644 tools/binman/test/327_fit_signature.dts create mode 100644 tools/binman/test/328_fit_signature.dts
Quoted 3 lines
--
2.17.1
Regards,
Simon