From patchwork Sun Feb 11 11:56:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jun Nie X-Patchwork-Id: 127908 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp2281600ljc; Sun, 11 Feb 2018 03:56:33 -0800 (PST) X-Google-Smtp-Source: AH8x227fq8KKt0V6nVGHx6mQJL1zi3an+gkMzbVPKpDMO7FWSNlhRs9J2Ty3qR/HL6TaqkyVLtgF X-Received: by 10.80.214.215 with SMTP id l23mr11957029edj.44.1518350193554; Sun, 11 Feb 2018 03:56:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518350193; cv=none; d=google.com; s=arc-20160816; b=fHt2UaCZFqROfIwPV0FbBUcpL5OX3TmzZmIP3KTPmaDjpB+AY+lflHbGYEusTKGqLA hBAeQldWDblEQitDd5eEVqnpajWph/vlQrFaFCpeBN0TOIyeCq+nxH76ULJ0QRRYcDyL 7EYVSz+YcZ0FilPQOIzEzeAzmFT8Vusc+EXZPEaKgsqtOsihUsXdMVBiDnKrJnKY7+oN 32P7e6rkAPrq9rVT2o5QBsaxKhR9x9oc4a56HJxebiwZvtlLu8LC7mmjK8DukbCaK02J Uhnnh5IQWHWQK0l6mb/a5j/KgIg3H2znyAo8VrPLPlDFM8vUoQeYC/0fbHeFOf6H6SbW O/wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:message-id:date:to:from :dkim-signature:arc-authentication-results; bh=9MJ7CF2sWtAYm1BsE9BBMRI9Dl1UgG/njyuyl3nfIqQ=; b=BjJ4kJVm3cQ8k2pcBdcpwVXjteExVpzrA8FzcG+iGufrTEFdzoyHwh/o5zJWgASxYG igr722FJnv5BVUNTEwpyAxpGddvdVATfpGvDk0o5d0+mkttuiqGlcqHu6WeWJXubs6Vz CsLveU8LAoSxkDhMceJIDaz3jmqQ7sZE2vFM4LEUENWlWQKxN27hHa53DQmHRz3kjknh ez9WePhDu8kT2Y9txJ4jc9wqG07vtnbwYDKxFjOMKiToNo0HHdYaQ0sPUxvhHDrFHESo bigJ4rj5F+gpi1hootfrY0i8h3m1oNhTvCqHZaLmslKwo1yjUgJaV8z5Lv7Wyp2WuMW1 VKlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=D9CRM5ro; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id n52si1667217edd.202.2018.02.11.03.56.32; Sun, 11 Feb 2018 03:56:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org header.s=google header.b=D9CRM5ro; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: by lists.denx.de (Postfix, from userid 105) id B86A4C21EE6; Sun, 11 Feb 2018 11:56:31 +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 C780CC21DA3; Sun, 11 Feb 2018 11:56:29 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 25087C21DA3; Sun, 11 Feb 2018 11:56:28 +0000 (UTC) Received: from mail-pg0-f66.google.com (mail-pg0-f66.google.com [74.125.83.66]) by lists.denx.de (Postfix) with ESMTPS id 806BDC21C51 for ; Sun, 11 Feb 2018 11:56:27 +0000 (UTC) Received: by mail-pg0-f66.google.com with SMTP id j9so4751765pgv.3 for ; Sun, 11 Feb 2018 03:56:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=uGdDEnbcvR/SS/qTTlGi+hc6A5x10zTbiYV7AWPcZh0=; b=D9CRM5roKyOeWsY1MvBBJOytTAVhNZ53gwENgfc0F8WxA/fbui7KMUylLOH5v9WoqB 7UCxU+4zc80KZ9Z34i4ajyGQ+DvMbrflsJD/wtZqE5lIzx08jz6GsxytdKxY2zg1iTjw kIVuS2BcXyLE5EGTNbZ/2OTurllpXCVFNtpiE= 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; bh=uGdDEnbcvR/SS/qTTlGi+hc6A5x10zTbiYV7AWPcZh0=; b=EY+xONP2xMGcgcj11G3z6epBp0uej5fE60zEDj1WVuzTZ7iJpqzNo+Lh34QvPMZ+Gj yuc58MDyMZX5zdVb9TeIboaW5AgARlC5q+xEJLb7trOuDXBPx2xtFVlwjcNuPGn+VqGK k33uQgu+GHCaXPJzllkDIZoX8IToXu46u3VtpkqvnaY63MDBOmmQQdt7Se+IBHb2B6l9 VU1wmq3evbfD3iLkFUxb5eZT0b9mD8mZxCjTYGMWOmVsVs0gvRV+d85AhQdGrLhmcPhQ D9Y6aNWax2vp4jkPPa9+eqpZflVwm5pBPGJfh1zFpxJN9UehY8pjLCiW43QiJT+qgNCc KClQ== X-Gm-Message-State: APf1xPDjWMIQy+Rgc5rWkL1Av/RDfxRwax+dMzZWq/XZCG98fyWTwvjB 72bSS1PJXer2R3cnEeW8Zi40+w== X-Received: by 10.99.125.79 with SMTP id m15mr1312667pgn.428.1518350185819; Sun, 11 Feb 2018 03:56:25 -0800 (PST) Received: from localhost.localdomain (61-216-91-114.HINET-IP.hinet.net. [61.216.91.114]) by smtp.gmail.com with ESMTPSA id n27sm21134787pfb.63.2018.02.11.03.56.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 11 Feb 2018 03:56:24 -0800 (PST) From: Jun Nie To: teddy.reed@gmail.com, sumit.garg@nxp.com, andre.przywara@arm.com, siarhei.siamashka@gmail.com Date: Sun, 11 Feb 2018 19:56:17 +0800 Message-Id: <1518350177-21229-1-git-send-email-jun.nie@linaro.org> X-Mailer: git-send-email 1.9.1 Cc: u-boot@lists.denx.de Subject: [U-Boot] [PATCH] SPL: Add signature verification when loading image 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: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Add signature verification when loading FIT image in SPL. Signed-off-by: Jun Nie --- common/image-fit.c | 56 +++++++++++++++++++++++++++++++--------------------- common/spl/spl_fit.c | 12 +++++++++++ include/image.h | 2 ++ 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/common/image-fit.c b/common/image-fit.c index f6e956a..94d9d4b 100644 --- a/common/image-fit.c +++ b/common/image-fit.c @@ -1068,34 +1068,14 @@ static int fit_image_check_hash(const void *fit, int noffset, const void *data, return 0; } -/** - * fit_image_verify - verify data integrity - * @fit: pointer to the FIT format image header - * @image_noffset: component image node offset - * - * fit_image_verify() goes over component image hash nodes, - * re-calculates each data hash and compares with the value stored in hash - * node. - * - * returns: - * 1, if all hashes are valid - * 0, otherwise (or on error) - */ -int fit_image_verify(const void *fit, int image_noffset) +int fit_image_verify_with_data(const void *fit, int image_noffset, + const void *data, size_t size) { - const void *data; - size_t size; int noffset = 0; char *err_msg = ""; int verify_all = 1; int ret; - /* Get image data and data length */ - if (fit_image_get_data(fit, image_noffset, &data, &size)) { - err_msg = "Can't get image data/size"; - goto error; - } - /* Verify all required signatures */ if (IMAGE_ENABLE_VERIFY && fit_image_verify_required_sigs(fit, image_noffset, data, size, @@ -1153,6 +1133,38 @@ error: } /** + * fit_image_verify - verify data integrity + * @fit: pointer to the FIT format image header + * @image_noffset: component image node offset + * + * fit_image_verify() goes over component image hash nodes, + * re-calculates each data hash and compares with the value stored in hash + * node. + * + * returns: + * 1, if all hashes are valid + * 0, otherwise (or on error) + */ +int fit_image_verify(const void *fit, int image_noffset) +{ + const void *data; + size_t size; + int noffset = 0; + char *err_msg = ""; + + /* Get image data and data length */ + if (fit_image_get_data(fit, image_noffset, &data, &size)) { + err_msg = "Can't get image data/size"; + printf("error!\n%s for '%s' hash node in '%s' image node\n", + err_msg, fit_get_name(fit, noffset, NULL), + fit_get_name(fit, image_noffset, NULL)); + return 0; + } + + return fit_image_verify_with_data(fit, image_noffset, data, size); +} + +/** * fit_all_image_verify - verify data integrity for all images * @fit: pointer to the FIT format image header * diff --git a/common/spl/spl_fit.c b/common/spl/spl_fit.c index cc07fbc..8d382eb 100644 --- a/common/spl/spl_fit.c +++ b/common/spl/spl_fit.c @@ -174,6 +174,9 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector, uint8_t image_comp = -1, type = -1; const void *data; bool external_data = false; +#ifdef CONFIG_SPL_FIT_SIGNATURE + int ret; +#endif if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { if (fit_image_get_comp(fit, node, &image_comp)) @@ -252,7 +255,16 @@ static int spl_load_fit_image(struct spl_load_info *info, ulong sector, image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); } +#ifdef CONFIG_SPL_FIT_SIGNATURE + printf("## Checking hash(es) for Image %s ...\n", + fit_get_name(fit, node, NULL)); + ret = fit_image_verify_with_data(fit, node, + (const void *)load_addr, length); + printf("\n"); + return !ret; +#else return 0; +#endif } static int spl_fit_append_fdt(struct spl_image_info *spl_image, diff --git a/include/image.h b/include/image.h index 325b014..77c11f8 100644 --- a/include/image.h +++ b/include/image.h @@ -1013,6 +1013,8 @@ int fit_add_verification_data(const char *keydir, void *keydest, void *fit, const char *comment, int require_keys, const char *engine_id); +int fit_image_verify_with_data(const void *fit, int image_noffset, + const void *data, size_t size); int fit_image_verify(const void *fit, int noffset); int fit_config_verify(const void *fit, int conf_noffset); int fit_all_image_verify(const void *fit);