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: 871752 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; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="D9CRM5ro"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3zfS2F0QWjz9sQm for ; Sun, 11 Feb 2018 22:56:35 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 15F1EC21F1D; Sun, 11 Feb 2018 11:56:32 +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-Google-Smtp-Source: AH8x2257Qfocj/JGDXNRE7kQsGUMea4Pc51bh9rTzW9eCLynBVfXT64BZmImqN/k1pNswZqy6Cg1uQ== 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);