Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AE579ECAAA1 for ; Tue, 6 Sep 2022 14:28:05 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3705D848DC; Tue, 6 Sep 2022 16:28:03 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="EJq983zW"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 68BF4849E0; Tue, 6 Sep 2022 15:27:28 +0200 (CEST) Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 7F782849CE for ; Tue, 6 Sep 2022 15:27:25 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=fr0st61te@gmail.com Received: by mail-lj1-x22e.google.com with SMTP id z20so12316434ljq.3 for ; Tue, 06 Sep 2022 06:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date; bh=Ev59jYcCQ9+yUOnpRHd573+wn4QygzSXZ/6s76PhGss=; b=EJq983zW5coo7NIgXQT3v4ELVso+4aCJj6E+KPmXia1ebPtdofXFsuQbvhHucvebyM 27kKrBQ9eCn6nSekW7wJpPhS0MjLsFcZvqDoyQNhgv5sLyR3kiuoWBhrCQFpbacyeywi kZC1vMBwYk0J/VerG5f3yzY3DEgne0kBTkhxJYp1i0swF1ICaX7ox+KQINJ5n/V5jrz9 kiDahh11PMoxzy53sk0ZoNZsf8uENp+ssBx5mRO2yXSxL4BGWIMaYmhHRoZC3yLDDPGA 7iGMn0kl4AgIufVvpfpji6MMuIKoFme6lgHrVpjqEkx/EzUrqT2U/Q3+ddovD2gyKf2D 4U8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date; bh=Ev59jYcCQ9+yUOnpRHd573+wn4QygzSXZ/6s76PhGss=; b=W2g4U/JbUBzUQVCOTLnWYDcFFVDzts6tmbEIqYHLlkq16mhSNUIUwuyHKd/DHtQF1Y oWOgVcIkAvTexM4pRMihGgdbrAyW70Qzyr0e5sLC6sIlAaVnZjpZQYoFDkcIh1AGEYWr EMxXjG5E/4ac4EIVIfvetOczauui22T2zcStEgpnfq30i70WKTFuDEbIAVJ9FGolKNyA DHNuOuFqLSjtHzOPEjZ6KarIYM1huF00UFeS6D/CCRDmJXE6UmzP/dLkMO0f1yFjSQqs uSdVeG9HE6pOX3Sh+LYFyWtISRI4S7qzXktV+0X60XzJkfSvWsCgYKtIRpGb6qxAgMs7 h7aQ== X-Gm-Message-State: ACgBeo0O2BXCaJHw46XQM+NnIwtdOv+O45sxdLJZlDduHfYF7du/Tnqm +Ota17mSV/rkKLcWMpuHP5I= X-Google-Smtp-Source: AA6agR4HnUg5oFJAl2h8J2TvVeDN3gKMPWAcdh8nWv9AIkgyOlVoGoxSVHRFpidCpEGUI0NF9Xiitg== X-Received: by 2002:a2e:b019:0:b0:26a:ab07:c950 with SMTP id y25-20020a2eb019000000b0026aab07c950mr819173ljk.381.1662470844629; Tue, 06 Sep 2022 06:27:24 -0700 (PDT) Received: from [100.119.237.241] (95-31-189-34.broadband.corbina.ru. [95.31.189.34]) by smtp.gmail.com with ESMTPSA id b22-20020a056512071600b004945c1c5cccsm1735892lfs.268.2022.09.06.06.27.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 06:27:23 -0700 (PDT) Message-ID: Subject: Re: [PATCH 1/3] binman: add sign option for binman From: Ivan Mikhaylov To: Alper Nebi Yasak Cc: U-Boot Mailing List , Ivan Mikhaylov , Simon Glass , Jan Kiszka Date: Tue, 06 Sep 2022 16:27:21 +0000 In-Reply-To: References: <20220321214319.33254-1-fr0st61te@gmail.com> <20220321214319.33254-2-fr0st61te@gmail.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4 MIME-Version: 1.0 X-Mailman-Approved-At: Tue, 06 Sep 2022 16:28:01 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On Thu, 2022-04-07 at 01:22 +0300, Alper Nebi Yasak wrote: > On 06/04/2022 23:28, Ivan Mikhaylov wrote: > > On Tue, 2022-04-05 at 21:54 +0300, Alper Nebi Yasak wrote: > > > On 22/03/2022 00:43, Ivan Mikhaylov wrote: > > > > Introduce proof of concept for binman's new option which > > > > provides > > > > sign > > > > and replace sections in binary images. > > > >=20 > > > > Usage as example: > > > >=20 > > > > from: > > > > mkimage -G privateky -r -o sha256,rsa4096 -F fit > > > > binman replace -i flash.bin -f fit.fit fit > > > >=20 > > > > to: > > > > binman sign -i flash.bin -k privatekey -a sha256,rsa4096 -f > > > > fit.fit > > > > fit > > >=20 > > > This shouldn't need the fit.fit input. It can be extracted from > > > the > > > image as you said in the cover letter. But I think instead of > > > doing > > > "extract -> sign with mkimage -> replace", signing should be > > > implemented > > > in the entry types as a new method. This way we can support other > > > entry-specific ways to sign things (see vblock for an example). > >=20 > > I have both of these things already: > > 1.extract->sign->replace > > =C2=A0 binman sign -i flash.bin -k privatekey -a sha256,rsa4096 fit > > 2.sign->replace > > =C2=A0 binman sign -i flash.bin -k privatekey -a sha256,rsa4096 -f > > fit.fit > > fit > >=20 > > Just waited for review to see in which direction should I move. > >=20 > > I've the case when I need only FIT image sign and replace after > > that. I > > think they both fit in 'sign' option. Okay about new method, so we > > talking about just one call like 'SignEntries' without mkimage and > > other steps with tools? >=20 > See below... >=20 > > >=20 > > > > Signed-off-by: Ivan Mikhaylov > > > > --- > > > > =C2=A0tools/binman/cmdline.py | 13 +++++++++++++ > > > > =C2=A0tools/binman/control.py | 26 +++++++++++++++++++++++++- > > > > =C2=A02 files changed, 38 insertions(+), 1 deletion(-) > > > >=20 > > > > diff --git a/tools/binman/cmdline.py b/tools/binman/cmdline.py > > > > index 0626b850f4..1a25f95ff1 100644 > > > > --- a/tools/binman/cmdline.py > > > > +++ b/tools/binman/cmdline.py > > > > @@ -160,6 +160,19 @@ controlled by a description in the board > > > > device tree.''' > > > > =C2=A0=C2=A0=C2=A0=C2=A0 replace_parser.add_argument('paths', type= =3Dstr, nargs=3D'*', > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'Paths within fi= le to > > > > replace > > > > (wildcard)') > > > > =C2=A0 > > > > +=C2=A0=C2=A0=C2=A0 sign_parser =3D subparsers.add_parser('sign', > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'Sign entries > > > > in > > > > image') > > > > +=C2=A0=C2=A0=C2=A0 sign_parser.add_argument('-a', '--algo', type= =3Dstr, > > > > required=3DTrue, > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'Hash algorithm e.g. > > > > sha256,rsa4096') > > > > +=C2=A0=C2=A0=C2=A0 sign_parser.add_argument('-f', '--file', type= =3Dstr, > > > > required=3DTrue, > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'Input filename to sig= n') > > > > +=C2=A0=C2=A0=C2=A0 sign_parser.add_argument('-i', '--image', type= =3Dstr, > > > > required=3DTrue, > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'Image filename to > > > > update') > > > > +=C2=A0=C2=A0=C2=A0 sign_parser.add_argument('-k', '--key', type=3D= str, > > > > required=3DTrue, > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'Private key file for > > > > signing') > > > > +=C2=A0=C2=A0=C2=A0 sign_parser.add_argument('paths', type=3Dstr, n= args=3D'*', > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'Paths within file to > > > > sign > > > > (wildcard)') > > > > + > > >=20 > > > If we want to support signing entry types other than FIT, I guess > > > we > > > need to base things on "EntryArg"s to make the arguments more > > > dynamic, > > > like named-by-arg blobs do. > >=20 > > Should we care about such possibility in terms of these patches? >=20 > There's already vblock and pre-load entry types where 'sign' is a > valid > thing to do. If we add a 'binman sign', it's reasonable to expect it > to > work on those as well. But these 'algo' and 'key' arguments don't > directly apply to vblock, its signing interface is a bit weird and > needs > different arguments. >=20 > Personally, I'm not sure if I'd use 'binman sign' as I like to > clean-build things, so the argument mismatch is not that big of a > concern for me. >=20 > > >=20 > > > > =C2=A0=C2=A0=C2=A0=C2=A0 test_parser =3D subparsers.add_parser('tes= t', help=3D'Run > > > > tests') > > > > =C2=A0=C2=A0=C2=A0=C2=A0 test_parser.add_argument('-P', '--processe= s', type=3Dint, > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 help=3D'set number= of processes to use for running > > > > tests') > > > > diff --git a/tools/binman/control.py b/tools/binman/control.py > > > > index a179f78129..7595ea7776 100644 > > > > --- a/tools/binman/control.py > > > > +++ b/tools/binman/control.py > > > > @@ -19,6 +19,7 @@ from binman import cbfs_util > > > > =C2=A0from binman import elf > > > > =C2=A0from patman import command > > > > =C2=A0from patman import tout > > > > +from patman import tools > > > > =C2=A0 > > > > =C2=A0# List of images we plan to create > > > > =C2=A0# Make this global so that it can be referenced from tests > > > > @@ -434,6 +435,26 @@ def ReplaceEntries(image_fname, > > > > input_fname, > > > > indir, entry_paths, > > > > =C2=A0=C2=A0=C2=A0=C2=A0 AfterReplace(image, allow_resize=3Dallow_r= esize, > > > > write_map=3Dwrite_map) > > > > =C2=A0=C2=A0=C2=A0=C2=A0 return image > > > > =C2=A0 > > > > +def MkimageSign(privatekey_fname, algo, input_fname): > > > > +=C2=A0=C2=A0=C2=A0 tools.Run('mkimage', '-G', privatekey_fname, '-= r', '-o', > > > > algo, > > > > '-F', input_fname) > > > > + > > > > +def SignEntries(image_fname, input_fname, privatekey_fname, > > > > algo, > > > > entry_paths): > > > > +=C2=A0=C2=A0=C2=A0 """Sign and replace the data from one or more e= ntries from > > > > input files > > > > + > > > > +=C2=A0=C2=A0=C2=A0 Args: > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 image_fname: Image file= name to process > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 input_fname: Single inp= ut filename to use if replacing > > > > one > > > > file, None > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= otherwise > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 algo: Hashing algorithm > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 privatekey_fname: Priva= te key filename > > > > + > > > > +=C2=A0=C2=A0=C2=A0 Returns: > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 List of EntryInfo recor= ds that were signed and > > > > replaced > > > > +=C2=A0=C2=A0=C2=A0 """ > > > > + > > > > +=C2=A0=C2=A0=C2=A0 MkimageSign(privatekey_fname, algo, input_fname= ) > > > > + > > > > +=C2=A0=C2=A0=C2=A0 return ReplaceEntries(image_fname, input_fname,= None, > > > > entry_paths) > > >=20 > > > I wrote a bit above, but what I mean is the mkimage call would go > > > into a > > > new method in etype/fit.py, and SignEntries() would be something > > > like > > > ReplaceEntries() but end up calling that method instead of > > > WriteData(). > >=20 > > Yeap, I agree, as I said above about 'how it should be'. Do you > > think > > it should be like additional implementation for mkimage in > > etype/fit.py > > which should be used in SignEntries inside? Something like: > > def SignEntries(params): > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0fit =3D SignFIT(params) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0return ReplaceEntries(p= arams with fit) >=20 > Don't call ReplaceEntries(). Try to copy what it does, but don't call > entry.WriteData() either. Replacing FIT sections is broken on master > for > now, it tries to rebuild itself to the original specification. I'm > planning to fix it, but the way I'm thinking of is demoting the entry > to > 'blob-ext' when replaced. It wouldn't be nice if that happened also > when > we're signing an entry. >=20 > I think SignEntries would be more like (untested): >=20 > def SignEntries(image_fname, entry_paths, ...): > =C2=A0=C2=A0=C2=A0 image_fname =3D os.path.abspath(image_fname) > =C2=A0=C2=A0=C2=A0 image =3D Image.FromFile(image_fname) > =C2=A0=C2=A0=C2=A0 state.PrepareFromLoadedData(image) > =C2=A0=C2=A0=C2=A0 image.LoadData() >=20 > =C2=A0=C2=A0=C2=A0 for entry_path in entry_paths: > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 entry =3D image.FindEntryPath(= entry_path) > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 entry.UpdateSignatures(...)=C2= =A0 # TODO >=20 > =C2=A0=C2=A0=C2=A0 ProcessImage(image, update_fdt=3DTrue, write_map=3DFal= se, > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 get_contents=3DFalse, allow_resize=3DFalse) >=20 > Then you'd implement UpdateSignatures in fit.py to configure the > entry > to use the given params. I'm not sure how exactly, I thought you'd > call > mkimage there, but it might be necessary to edit the underlying > binman > control dtb to change the signature nodes. Alper, this way works but until 74d3b231(binman: Create FIT subentries in the FIT section, not its parent) commit. self.data or self.GetData() is None for entry which need to be signed after this commit. Any ideas how to get the data of fit entry? Thanks. >=20 > >=20 > > Anyways, waiting for Simon's review. > >=20 > > Thanks.