public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: AKASHI Takahiro <takahiro.akashi@linaro.org>
To: u-boot@lists.denx.de
Subject: [PATCH 05/13] efi_loader: signature: make efi_hash_regions more generic
Date: Tue, 2 Jun 2020 14:05:32 +0900	[thread overview]
Message-ID: <20200602050532.GB20446@laputa> (raw)
In-Reply-To: <546be087-0ee3-8e0c-8e68-172456223c6e@gmx.de>

Heinrich,

On Sat, May 30, 2020 at 08:58:02AM +0200, Heinrich Schuchardt wrote:
> On 5/29/20 8:41 AM, AKASHI Takahiro wrote:
> > There are a couple of occurrences of hash calculations in which a new
> > efi_hash_regions will be commonly used.
> >
> > Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
> > ---
> >  lib/efi_loader/efi_signature.c | 44 +++++++++++++---------------------
> >  1 file changed, 16 insertions(+), 28 deletions(-)
> >
> > diff --git a/lib/efi_loader/efi_signature.c b/lib/efi_loader/efi_signature.c
> > index 35f678de057e..00e442783059 100644
> > --- a/lib/efi_loader/efi_signature.c
> > +++ b/lib/efi_loader/efi_signature.c
> > @@ -30,6 +30,7 @@ const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
> >  /**
> >   * efi_hash_regions - calculate a hash value
> >   * @regs:	List of regions
> > + * @count:	Number of regions
> >   * @hash:	Pointer to a pointer to buffer holding a hash value
> >   * @size:	Size of buffer to be returned
> >   *
> > @@ -37,18 +38,20 @@ const efi_guid_t efi_guid_cert_type_pkcs7 = EFI_CERT_TYPE_PKCS7_GUID;
> >   *
> >   * Return:	true on success, false on error
> >   */
> > -static bool efi_hash_regions(struct efi_image_regions *regs, void **hash,
> > -			     size_t *size)
> > +static bool efi_hash_regions(struct image_region *regs, int count,
> > +			     void **hash, size_t *size)
> 
> What is this size parameter good for if we know all signatures are
> SHA256? Should it be eliminiated?

It would make more sense when we support algorithms other than sha256.
The caller, then, could look like:

  efi_hash_regions("sha256", regs, count, hash, size);
  if (!memcmp(data, hash, size))
    ...

That way, callers in different use cases won't have to use an immediate
value for actual hash size.
For example, an certificate entry in the revocation list can be
digested with, according to UEFI specification,
  EFI_CERT_X509_SHA256_GUID
  EFI_CERT_X509_SHA384_GUID
  EFI_CERT_X509_SHA512_GUID

Meanwhile, signatures can be different type of message digests, as well.
  EFI_CERT_SHA1_GUID
  EFI_CERT_SHA224_GUID
  EFI_CERT_SHA256_GUID
  EFI_CERT_SHA384_GUID
  EFI_CERT_SHA512_GUID

> Otherwise
> Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

Thanks,
-Takahiro Akashi


> >  {
> > -	*size = 0;
> > -	*hash = calloc(1, SHA256_SUM_LEN);
> >  	if (!*hash) {
> > -		debug("Out of memory\n");
> > -		return false;
> > +		*hash = calloc(1, SHA256_SUM_LEN);
> > +		if (!*hash) {
> > +			debug("Out of memory\n");
> > +			return false;
> > +		}
> >  	}
> > -	*size = SHA256_SUM_LEN;
> > +	if (size)
> > +		*size = SHA256_SUM_LEN;
> >
> > -	hash_calculate("sha256", regs->reg, regs->num, *hash);
> > +	hash_calculate("sha256", regs, count, *hash);
> >  #ifdef DEBUG
> >  	debug("hash calculated:\n");
> >  	print_hex_dump("    ", DUMP_PREFIX_OFFSET, 16, 1,
> > @@ -73,26 +76,10 @@ static bool efi_hash_msg_content(struct pkcs7_message *msg, void **hash,
> >  {
> >  	struct image_region regtmp;
> >
> > -	*size = 0;
> > -	*hash = calloc(1, SHA256_SUM_LEN);
> > -	if (!*hash) {
> > -		debug("Out of memory\n");
> > -		free(msg);
> > -		return false;
> > -	}
> > -	*size = SHA256_SUM_LEN;
> > -
> >  	regtmp.data = msg->data;
> >  	regtmp.size = msg->data_len;
> >
> > -	hash_calculate("sha256", &regtmp, 1, *hash);
> > -#ifdef DEBUG
> > -	debug("hash calculated based on contentInfo:\n");
> > -	print_hex_dump("    ", DUMP_PREFIX_OFFSET, 16, 1,
> > -		       *hash, SHA256_SUM_LEN, false);
> > -#endif
> > -
> > -	return true;
> > +	return efi_hash_regions(&regtmp, 1, hash, size);
> >  }
> >
> >  /**
> > @@ -168,9 +155,10 @@ static bool efi_signature_verify(struct efi_image_regions *regs,
> >  			       false);
> >  #endif
> >  		/* against contentInfo first */
> > +		hash = NULL;
> >  		if ((msg->data && efi_hash_msg_content(msg, &hash, &size)) ||
> >  				/* for signed image */
> > -		    efi_hash_regions(regs, &hash, &size)) {
> > +		    efi_hash_regions(regs->reg, regs->num, &hash, &size)) {
> >  				/* for authenticated variable */
> >  			if (ps_info->msgdigest_len != size ||
> >  			    memcmp(hash, ps_info->msgdigest, size)) {
> > @@ -238,7 +226,7 @@ bool efi_signature_verify_with_list(struct efi_image_regions *regs,
> >  	      regs, signed_info, siglist, valid_cert);
> >
> >  	if (!signed_info) {
> > -		void *hash;
> > +		void *hash = NULL;
> >  		size_t size;
> >
> >  		debug("%s: unsigned image\n", __func__);
> > @@ -252,7 +240,7 @@ bool efi_signature_verify_with_list(struct efi_image_regions *regs,
> >  			goto out;
> >  		}
> >
> > -		if (!efi_hash_regions(regs, &hash, &size)) {
> > +		if (!efi_hash_regions(regs->reg, regs->num, &hash, &size)) {
> >  			debug("Digesting unsigned image failed\n");
> >  			goto out;
> >  		}
> >
> 

  reply	other threads:[~2020-06-02  5:05 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-29  6:41 [PATCH 00/13] efi_loader: rework/improve UEFI secure boot code AKASHI Takahiro
2020-05-29  6:41 ` [PATCH 01/13] efi_loader: signature: move efi_guid_cert_type_pkcs7 to efi_signature.c AKASHI Takahiro
2020-05-29 10:27   ` Heinrich Schuchardt
2020-05-29  6:41 ` [PATCH 02/13] efi_loader: image_loader: add a check against certificate type of authenticode AKASHI Takahiro
2020-05-29 10:37   ` Heinrich Schuchardt
2020-06-02  2:22     ` AKASHI Takahiro
2020-05-29  6:41 ` [PATCH 03/13] efi_loader: image_loader: retrieve authenticode only if it exists AKASHI Takahiro
2020-05-30  6:02   ` Heinrich Schuchardt
2020-05-29  6:41 ` [PATCH 04/13] efi_loader: signature: fix a size check against revocation list AKASHI Takahiro
2020-05-30  6:42   ` Heinrich Schuchardt
2020-05-29  6:41 ` [PATCH 05/13] efi_loader: signature: make efi_hash_regions more generic AKASHI Takahiro
2020-05-30  6:58   ` Heinrich Schuchardt
2020-06-02  5:05     ` AKASHI Takahiro [this message]
2020-05-29  6:41 ` [PATCH 06/13] efi_loader: image_loader: verification for all signatures should pass AKASHI Takahiro
2020-05-30  7:01   ` Heinrich Schuchardt
2020-06-02  5:22     ` AKASHI Takahiro
2020-05-29  6:41 ` [PATCH 07/13] efi_loader: image_loader: add digest-based verification for signed image AKASHI Takahiro
2020-05-30  7:09   ` Heinrich Schuchardt
2020-06-02  5:31     ` AKASHI Takahiro
2020-05-29  6:41 ` [PATCH 08/13] test/py: efi_secboot: remove all "re.search" AKASHI Takahiro
2020-05-30  7:04   ` Heinrich Schuchardt
2020-06-02  5:58     ` AKASHI Takahiro
2020-06-02  8:27       ` Heinrich Schuchardt
2020-07-02 16:21   ` Heinrich Schuchardt
2020-05-29  6:41 ` [PATCH 09/13] test/py: efi_secboot: fix test case 1g of test_authvar AKASHI Takahiro
2020-07-02 16:28   ` Heinrich Schuchardt
2020-05-29  6:41 ` [PATCH 10/13] test/py: efi_secboot: split "signed image" test case-1 into two cases AKASHI Takahiro
2020-05-29  6:41 ` [PATCH 11/13] test/py: efi_secboot: add a test against certificate revocation AKASHI Takahiro
2020-05-29  6:41 ` [PATCH 12/13] test/py: efi_secboot: add a test for multiple signatures AKASHI Takahiro
2020-05-29  6:41 ` [PATCH 13/13] test/py: efi_secboot: add a test for verifying with digest of signed image AKASHI Takahiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200602050532.GB20446@laputa \
    --to=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox