Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 74D1FC4338F for ; Wed, 18 Aug 2021 22:49:16 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id C547D61075 for ; Wed, 18 Aug 2021 22:49:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C547D61075 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 78D7182DA7; Thu, 19 Aug 2021 00:49:13 +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="jzxIT/9v"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5F07982DD5; Thu, 19 Aug 2021 00:49:11 +0200 (CEST) Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) (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 EE6ED82DA3 for ; Thu, 19 Aug 2021 00:49:07 +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=mr.nuke.me@gmail.com Received: by mail-ot1-x32d.google.com with SMTP id 61-20020a9d0d430000b02903eabfc221a9so6252632oti.0 for ; Wed, 18 Aug 2021 15:49:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=bX81Iv3tpeJrNEAh4yFnpJcKOGyLOTRxGucoNax87ZE=; b=jzxIT/9vmb8hRhCUm+HewIu4kNBVCFBcC6BUDvMINei6tj27VdVfW9P90g3ARGNQgw pdJ7v3NzgsKfGQD7OfVRwd+NOFasAFRQUZE56d70nf9FIPC3d+9eBVW+RAma3gEDpk6S 098LUYOTyY38PcisFrNKG+O28dYx9yxK/rThvR/X5/nyav9d8H7d3Fw3+EwtEUx/06gs XVFUDqpXRJSWfm34pKBy4WHr4lV8xyjLwe/MH5x8pLYMZjrwIzTb7UrBwk+KrCzmIARF xJzqlhgSxkmEiHRz4laTZ6mGTBb3LanaJV16+IY0P6MtK9JupjNzdJLOs0hADhR076ma H30A== 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:mime-version :content-transfer-encoding; bh=bX81Iv3tpeJrNEAh4yFnpJcKOGyLOTRxGucoNax87ZE=; b=XuD3UWneqMOJx9E/GABLnldhnR9AS/g83Dy4IOKuQWO8sbgcLgieVUtNl7uGeGop96 mm/KaCnZ16ipIumSFQ6gl8XVyvrp3ghmKzXnl4UgbgRT+KB01FfrZJbToqptgkuKe3UH Eul1GEQf2sYfhbd9gLW1Ex91KGYt8osK5dnSlS3FAQYNcJS22pdXeY0eQH+FntT4B+o0 TXJlUqAckEIgujl4Nlm3s4mZNVnnsY8bSnX8EN0dnxyHwXfKEWizxmvxDmB4eVmvPOhT +H0zr3B/UH/aZEVcm023usPI/4YQUw50fNxGrRDJsELskGz8/Ioc55pYAoqoPrmUC+yN o8Tg== X-Gm-Message-State: AOAM532LHQ4vyCDE0G4K1HEcoUiU4F4x66HH6q1a8W5eS9EBzBvYaTTr 513o9vl/QfBmT+Uqlu+PhyPhjSPFEzE= X-Google-Smtp-Source: ABdhPJzp4IjNwwcD2m6ssadlchgdtcTz7LV22/9VpvnNw66+8ZWUFT9mm/pb026hRqrTYWSOULxF/w== X-Received: by 2002:a9d:6b06:: with SMTP id g6mr8865072otp.86.1629326946013; Wed, 18 Aug 2021 15:49:06 -0700 (PDT) Received: from nuclearis3.lan (c-98-195-139-126.hsd1.tx.comcast.net. [98.195.139.126]) by smtp.gmail.com with ESMTPSA id 21sm231783ooy.5.2021.08.18.15.49.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Aug 2021 15:49:05 -0700 (PDT) From: Alexandru Gagniuc To: u-boot@lists.denx.de Cc: sjg@chromium.org, Alexandru Gagniuc Subject: [PATCH] image: rsa: Move padding_algos to linker lists Date: Wed, 18 Aug 2021 17:49:02 -0500 Message-Id: <20210818224902.2286717-1-mr.nuke.me@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean We are not guaranteed to have the padding_pkcs_15_verify symbol since commit 92c960bc1d ("lib: rsa: Remove #ifdefs from rsa.h"), and commit 61416fe9df ("Kconfig: FIT_SIGNATURE should not select RSA_VERIFY") The padding_algos only make sense with RSA verification, which can now be disabled in lieu of ECDSA. In fact this will lead to build failures because of the missing symbol mentioned earlier. To resolve this, move the padding_algos to a linker list, with declarations moved to rsa_verify.c. This is consistent with commit 6909edb4ce ("image: rsa: Move verification algorithm to a linker list") One could argue that the added #ifdef USE_HOSTCC is ugly, and should be hidden within the U_BOOT_PADDING_ALGO() macro. However, this would be inconsistent with the "cryptos" list. This logic for was not previously explored: Without knowledge of the U_BOOT_PADDING_ALGO() macro, its use is similar to something being declared. However, should #ifndef USE_HOSTCC be part of the macro, it would not be obvious that it behaves differently on host code and target code. Having the #ifndef outside the macro makes this obvious. Also, the #ifdef is not always necessary. For example ecda-verify makes use of U_BOOT_CRYPTO_ALGO() without any accompanying #ifdefs. The fundamental issue is a lack of separation of host and target code in rsa_verify. Therefore, the declaration of a padding algo with the external #ifdef is more readable and consistent. Signed-off-by: Alexandru Gagniuc --- common/image-sig.c | 23 ++++++----------------- include/image.h | 4 ++++ lib/rsa/rsa-verify.c | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/common/image-sig.c b/common/image-sig.c index fb0035524e..fa9407bb30 100644 --- a/common/image-sig.c +++ b/common/image-sig.c @@ -51,19 +51,6 @@ struct checksum_algo checksum_algos[] = { }; -struct padding_algo padding_algos[] = { - { - .name = "pkcs-1.5", - .verify = padding_pkcs_15_verify, - }, -#ifdef CONFIG_FIT_RSASSA_PSS - { - .name = "pss", - .verify = padding_pss_verify, - } -#endif /* CONFIG_FIT_RSASSA_PSS */ -}; - struct checksum_algo *image_get_checksum_algo(const char *full_name) { int i; @@ -129,14 +116,16 @@ struct crypto_algo *image_get_crypto_algo(const char *full_name) struct padding_algo *image_get_padding_algo(const char *name) { - int i; + struct padding_algo *padding, *end; if (!name) return NULL; - for (i = 0; i < ARRAY_SIZE(padding_algos); i++) { - if (!strcmp(padding_algos[i].name, name)) - return &padding_algos[i]; + padding = ll_entry_start(struct padding_algo, paddings); + end = ll_entry_end(struct padding_algo, paddings); + for (; padding < end; padding++) { + if (!strcmp(padding->name, name)) + return padding; } return NULL; diff --git a/include/image.h b/include/image.h index e20f0b69d5..ab0a6c2ef0 100644 --- a/include/image.h +++ b/include/image.h @@ -1334,6 +1334,10 @@ struct padding_algo { const uint8_t *hash, int hash_len); }; +/* Declare a new U-Boot padding algorithm handler */ +#define U_BOOT_PADDING_ALGO(__name) \ +ll_entry_declare(struct padding_algo, __name, paddings) + /** * image_get_checksum_algo() - Look up a checksum algorithm * diff --git a/lib/rsa/rsa-verify.c b/lib/rsa/rsa-verify.c index 3840764e42..ad6d33d043 100644 --- a/lib/rsa/rsa-verify.c +++ b/lib/rsa/rsa-verify.c @@ -95,6 +95,13 @@ int padding_pkcs_15_verify(struct image_sign_info *info, return 0; } +#ifndef USE_HOSTCC +U_BOOT_PADDING_ALGO(pkcs_15) = { + .name = "pkcs-1.5", + .verify = padding_pkcs_15_verify, +}; +#endif + #ifdef CONFIG_FIT_RSASSA_PSS static void u32_i2osp(uint32_t val, uint8_t *buf) { @@ -296,6 +303,14 @@ out: return ret; } + +#ifndef USE_HOSTCC +U_BOOT_PADDING_ALGO(pss) = { + .name = "pss", + .verify = padding_pss_verify, +}; +#endif + #endif #if CONFIG_IS_ENABLED(FIT_SIGNATURE) || CONFIG_IS_ENABLED(RSA_VERIFY_WITH_PKEY) -- 2.31.1