From patchwork Fri Sep 6 07:08:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: AKASHI Takahiro X-Patchwork-Id: 1158810 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="kHcznoF6"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 46PpXH5M91z9sN1 for ; Fri, 6 Sep 2019 17:07:11 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 72B47C21E2C; Fri, 6 Sep 2019 07:06:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 4BD3FC21E0F; Fri, 6 Sep 2019 07:06:13 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 873BFC21E02; Fri, 6 Sep 2019 07:06:00 +0000 (UTC) Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.195]) by lists.denx.de (Postfix) with ESMTPS id 5F165C21E44 for ; Fri, 6 Sep 2019 07:05:57 +0000 (UTC) Received: by mail-pf1-f195.google.com with SMTP id w22so3730352pfi.9 for ; Fri, 06 Sep 2019 00:05:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=2Ypo4JOWwwvXgZnHmnJ4eHGjvLcVMR4Sjs4txMK9FL8=; b=kHcznoF638gvlYuGymoTjTvAaXvGysosOU+fg+ZOzzDUiNGrxYfJNbBqsoJ1/uwSd6 JpUrL+3SG4Y3jVgwROiXoUCNQCjj/lI3siRZdcp8LHRWa6SJJLFfGT5QXnVtbvwa69gu unpU65/6dNXVDGapYV2ohl82y5BvwvbqoPnrinMdel2bFtY5rHnqaM37M5O0Rny0IBXb pB79Vq9yhA7qHeP/bHeF5ELa9eLIPgPB6mawU6XFgucgyAh8TIFNLt79yQsSkeVggxDc +pTL8CjVzxEl5Kb/+iSpOs/v/GXAeTDzeJyGDHC89F5RTJFTxQMwapVRtV7H3+dWmdmx oXBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=2Ypo4JOWwwvXgZnHmnJ4eHGjvLcVMR4Sjs4txMK9FL8=; b=FvLFcIYy7G5xF5TFUkd4M46cWGvYnvykA2VF1RJ+spUVo+UHvNCIHDa9WppRqWmuSz GOA052iugw45tfVi9OH4xsxaDp0oiD3XaBFD4MZLpKLFacxV8x/R49FPakOn4OiZT5pL SpFohSosBkOg6qFG77/CDLHBK9C1SaDAd92SEB5IV0eh4Ah48ECnG1Vwq65VRWwWirZ6 /bRYMiU7gHhLI7+t/ncymgp6+1QpJUfQLuQd5WziThlrZaXqi8G3KrSBmKn40sPjwr0r Hyq3B0dRuBkInCNbZTqI85Iu+GlWVDx4LcfrgmIFWZquwJGneykiOIy5JMeeMRk7ssv6 fjDw== X-Gm-Message-State: APjAAAXXc935GEGlBzhgXP2Hm7aRilcQahQw9dJ8X4fNjmNYKjvjZa/G mbHQoU+gMbS0ZccGviluzsp1VQ== X-Google-Smtp-Source: APXvYqz6+rfKTbUs84Vh5igxUJP7DLjvFEBCiG55Tv4Kd95d8Ig1xx7q0Ul50B4eSjklLrHI1f5//g== X-Received: by 2002:a62:8745:: with SMTP id i66mr8670054pfe.259.1567753555996; Fri, 06 Sep 2019 00:05:55 -0700 (PDT) Received: from linaro.org ([121.95.100.191]) by smtp.googlemail.com with ESMTPSA id z25sm4829385pfa.91.2019.09.06.00.05.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Sep 2019 00:05:55 -0700 (PDT) From: AKASHI Takahiro To: trini@konsulko.com, sjg@chromium.org, xypron.glpk@gmx.de, agraf@csgraf.de Date: Fri, 6 Sep 2019 16:08:08 +0900 Message-Id: <20190906070808.1198-4-takahiro.akashi@linaro.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190906070808.1198-1-takahiro.akashi@linaro.org> References: <20190906070808.1198-1-takahiro.akashi@linaro.org> MIME-Version: 1.0 Cc: u-boot@lists.denx.de Subject: [U-Boot] [RFC 3/3] lib: rsa: add rsa_verify_with_pkey() X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 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" This function, and hence rsa_verify(), will perform RSA verification with two essential parameters for a RSA public key in contract of rsa_verify_with_keynode(), which requires additional three parameters stored in FIT image. It will be used in implementing UEFI secure boot, i.e. image authentication and variable authentication. Signed-off-by: AKASHI Takahiro --- lib/rsa/Kconfig | 7 +++++ lib/rsa/Makefile | 3 ++- lib/rsa/rsa-verify.c | 63 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 64 insertions(+), 9 deletions(-) diff --git a/lib/rsa/Kconfig b/lib/rsa/Kconfig index 338c8124da59..3c1986a26f8c 100644 --- a/lib/rsa/Kconfig +++ b/lib/rsa/Kconfig @@ -25,6 +25,13 @@ config RSA_VERIFY help Add RSA signature verification support. +config RSA_VERIFY_WITH_PKEY + bool "Execute RSA verification without key parameters from FDT" + depends on RSA + help + This options enables RSA signature verification without + using public key parameters which is embedded control FDT. + config RSA_SOFTWARE_EXP bool "Enable driver for RSA Modular Exponentiation in software" depends on DM diff --git a/lib/rsa/Makefile b/lib/rsa/Makefile index d66eef74c514..fd4592fd6a8a 100644 --- a/lib/rsa/Makefile +++ b/lib/rsa/Makefile @@ -5,5 +5,6 @@ # (C) Copyright 2000-2007 # Wolfgang Denk, DENX Software Engineering, wd@denx.de. -obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o rsa-keyprop.o +obj-$(CONFIG_RSA_VERIFY) += rsa-verify.o rsa-checksum.o +obj-$(CONFIG_RSA_VERIFY_WITH_PKEY) += rsa-keyprop.o obj-$(CONFIG_RSA_SOFTWARE_EXP) += rsa-mod-exp.o diff --git a/lib/rsa/rsa-verify.c b/lib/rsa/rsa-verify.c index 287fcc4d234d..80eabff3e940 100644 --- a/lib/rsa/rsa-verify.c +++ b/lib/rsa/rsa-verify.c @@ -17,9 +17,14 @@ #include "mkimage.h" #include #endif +#include #include #include +#ifndef __UBOOT__ /* for host tools */ +#undef CONFIG_RSA_VERIFY_WITH_PKEY +#endif + /* Default public exponent for backward compatibility */ #define RSA_DEFAULT_PUBEXP 65537 @@ -342,6 +347,34 @@ static int rsa_verify_key(struct image_sign_info *info, return 0; } +#ifdef CONFIG_RSA_VERIFY_WITH_PKEY +/** + * rsa_verify_with_pkey() + * + */ +static int rsa_verify_with_pkey(struct image_sign_info *info, + const void *hash, uint8_t *sig, uint sig_len) +{ + struct key_prop *prop; + int ret; + + /* Public key is self-described to fill key_prop */ + prop = rsa_gen_key_prop(info->key, info->keylen); + if (!prop) { + debug("Generating necessary parameter for decoding failed\n"); + return -EACCES; + } + + ret = rsa_verify_key(info, prop, sig, sig_len, hash, + info->crypto->key_len); + + rsa_free_key_prop(prop); + + return ret; +} +#endif + +#if CONFIG_IS_ENABLED(FIT_SIGNATURE) /** * rsa_verify_with_keynode() - Verify a signature against some data using * information in node with prperties of RSA Key like modulus, exponent etc. @@ -395,18 +428,21 @@ static int rsa_verify_with_keynode(struct image_sign_info *info, return ret; } +#endif int rsa_verify(struct image_sign_info *info, const struct image_region region[], int region_count, uint8_t *sig, uint sig_len) { - const void *blob = info->fdt_blob; /* Reserve memory for maximum checksum-length */ uint8_t hash[info->crypto->key_len]; + int ret = -EACCES; +#if CONFIG_IS_ENABLED(FIT_SIGNATURE) + const void *blob = info->fdt_blob; int ndepth, noffset; int sig_node, node; char name[100]; - int ret; +#endif /* * Verify that the checksum-length does not exceed the @@ -419,12 +455,6 @@ int rsa_verify(struct image_sign_info *info, return -EINVAL; } - sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); - if (sig_node < 0) { - debug("%s: No signature node found\n", __func__); - return -ENOENT; - } - /* Calculate checksum with checksum-algorithm */ ret = info->checksum->calculate(info->checksum->name, region, region_count, hash); @@ -433,6 +463,22 @@ int rsa_verify(struct image_sign_info *info, return -EINVAL; } +#ifdef CONFIG_RSA_VERIFY_WITH_PKEY + if (!info->fdt_blob) { + /* don't rely on fdt properties */ + ret = rsa_verify_with_pkey(info, hash, sig, sig_len); + + return ret; + } +#endif + +#if CONFIG_IS_ENABLED(FIT_SIGNATURE) + sig_node = fdt_subnode_offset(blob, 0, FIT_SIG_NODENAME); + if (sig_node < 0) { + debug("%s: No signature node found\n", __func__); + return -ENOENT; + } + /* See if we must use a particular key */ if (info->required_keynode != -1) { ret = rsa_verify_with_keynode(info, hash, sig, sig_len, @@ -459,6 +505,7 @@ int rsa_verify(struct image_sign_info *info, break; } } +#endif return ret; }