From patchwork Fri Sep 3 00:54:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Gagniuc X-Patchwork-Id: 1524014 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; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20210112 header.b=MlALHGh5; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4H0zqJ1d3Vz9sSn for ; Fri, 3 Sep 2021 10:55:24 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 5C38F83524; Fri, 3 Sep 2021 02:54:57 +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="MlALHGh5"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C37D282ECD; Fri, 3 Sep 2021 02:54:39 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ot1-x332.google.com (mail-ot1-x332.google.com [IPv6:2607:f8b0:4864:20::332]) (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 C0CE983514 for ; Fri, 3 Sep 2021 02:54:31 +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-x332.google.com with SMTP id m7-20020a9d4c87000000b0051875f56b95so4799783otf.6 for ; Thu, 02 Sep 2021 17:54:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Yek3sblzaw9lOHbL0D6H3sDvUA4RpWt2Dru2uzKzEmo=; b=MlALHGh5oDQ5W6SQYMuhx9vigECWtMVSClHJqYWwmQaC7UaLoDSlpplR/G6Oa2rmE6 ZE+U2AT1saKz7OC5XzfRKNDE4euPdcFppamTOZf+LWz1gz15DiVVFybh9I8SPxppZqfV LT5PnhmH8nWTmCIbtZdGelicDlf512IC++PxmbSp8E2EvMVvpw0dOlAxriC3HfUNCZQL rHDiFyQZtM79j1TEBgqKKRO1BLvCH3JzofQZGSo7+yg2KoMOpO2iQLQIsIi7L/9wdy6x jaPn68+tDXi8jlT6e7qx7kv1vW7AEj5Q0rv5DnZZdNkEan15v6eZK0m+sN3M05JbfxjX WPcA== 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=Yek3sblzaw9lOHbL0D6H3sDvUA4RpWt2Dru2uzKzEmo=; b=L6VUmGcwoyJf3pbHQAfOufi01LHYjoTiz3T35S8eeWosu0vLFwQAE8UTxkpggN37Yg VCHQLmOKYoJM2OVewkXivtM/jwEZPC3fXDtbbKu9P5WuqUmm0gRaGnrGhKXoJpc7ykht 8g2sn9EEpWQ9ZBaLqkbgP9AuTPeA88u6fJQn1Der4/F+ikdMcFKyTa1tUcyHDI3uoTgj tisIjGYIbJ3Tr44nIA7awpszM1U6NmbGWMOx602CbWpfnbrSpbbxBd5C0M4PhtVjwZfg f4FOuaJ84wC4nQo6xsVr607bwOiYSyVggsfsIH3qM5gL2+Z7sS31kL9XLuDuBZCGBeCX QMSA== X-Gm-Message-State: AOAM531W2tSyOHqjazI0NUHoNSE7ho4dypSxKDh1oJEv6rJE2jUMzOxA JM5qNZeEa1nvBq043onjxYY= X-Google-Smtp-Source: ABdhPJxTl9O0UFcIGi/9k5jspxJolsTcDA5HmTEjtjj/+HhE/xM3SD6OmAM0wafDM3iNwx/QXAvKiA== X-Received: by 2002:a9d:222c:: with SMTP id o41mr845465ota.100.1630630470433; Thu, 02 Sep 2021 17:54:30 -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 186sm708396ood.39.2021.09.02.17.54.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Sep 2021 17:54:29 -0700 (PDT) From: Alexandru Gagniuc To: trini@konsulko.com, u-boot@lists.denx.de Cc: sjg@chromium.org, Alexandru Gagniuc Subject: [PATCH v2 5/6] image: Drop if/elseif hash selection in calculate_hash() Date: Thu, 2 Sep 2021 19:54:21 -0500 Message-Id: <20210903005422.1336362-6-mr.nuke.me@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210903005422.1336362-1-mr.nuke.me@gmail.com> References: <20210903005422.1336362-1-mr.nuke.me@gmail.com> MIME-Version: 1.0 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 calculate_hash() would try to select the appropriate hashing function by a if/elseif contruct. But that is exactly why hash_lookup_algo() exists, so use it instead. This does mean that we now have to 'select HASH' to make sure we get the hash_lookup_algo() symbol. However, the change makes sense because even basic FITs will have to deal with "hash" nodes. My only concern is that the 'select SPL_HASH' might cause some platform to grow above its SPL size allowance Signed-off-by: Alexandru Gagniuc --- common/Kconfig.boot | 2 ++ common/image-fit.c | 45 ++++++++++++++++---------------------- drivers/crypto/fsl/Kconfig | 2 ++ 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/common/Kconfig.boot b/common/Kconfig.boot index 314f1e50a0..c2d6c89372 100644 --- a/common/Kconfig.boot +++ b/common/Kconfig.boot @@ -13,6 +13,7 @@ config FIT bool "Support Flattened Image Tree" select MD5 select SHA1 + select HASH help This option allows you to boot the new uImage structure, Flattened Image Tree. FIT is formally a FDT, which can include @@ -133,6 +134,7 @@ if SPL config SPL_FIT bool "Support Flattened Image Tree within SPL" depends on SPL + select SPL_HASH select SPL_OF_LIBFDT config SPL_FIT_PRINT diff --git a/common/image-fit.c b/common/image-fit.c index aff4670be3..92d9141bcd 100644 --- a/common/image-fit.c +++ b/common/image-fit.c @@ -1193,6 +1193,12 @@ int fit_set_timestamp(void *fit, int noffset, time_t timestamp) return 0; } +static void crc32_uimage_fixup(void *value) +{ + /* TODO: In C, this type punning is undefined behavior: */ + *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); +} + /** * calculate_hash - calculate and return hash for provided input data * @data: pointer to the input data @@ -1211,37 +1217,24 @@ int fit_set_timestamp(void *fit, int noffset, time_t timestamp) * 0, on success * -1, when algo is unsupported */ -int calculate_hash(const void *data, int data_len, const char *algo, +int calculate_hash(const void *data, int data_len, const char *name, uint8_t *value, int *value_len) { - if (IMAGE_ENABLE_CRC32 && strcmp(algo, "crc32") == 0) { - *((uint32_t *)value) = crc32_wd(0, data, data_len, - CHUNKSZ_CRC32); - *((uint32_t *)value) = cpu_to_uimage(*((uint32_t *)value)); - *value_len = 4; - } else if (CONFIG_IS_ENABLED(SHA1) && strcmp(algo, "sha1") == 0) { - sha1_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA1); - *value_len = 20; - } else if (CONFIG_IS_ENABLED(SHA256) && strcmp(algo, "sha256") == 0) { - sha256_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA256); - *value_len = SHA256_SUM_LEN; - } else if (CONFIG_IS_ENABLED(SHA384) && strcmp(algo, "sha384") == 0) { - sha384_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA384); - *value_len = SHA384_SUM_LEN; - } else if (CONFIG_IS_ENABLED(SHA512) && strcmp(algo, "sha512") == 0) { - sha512_csum_wd((unsigned char *)data, data_len, - (unsigned char *)value, CHUNKSZ_SHA512); - *value_len = SHA512_SUM_LEN; - } else if (IMAGE_ENABLE_MD5 && strcmp(algo, "md5") == 0) { - md5_wd((unsigned char *)data, data_len, value, CHUNKSZ_MD5); - *value_len = 16; - } else { + struct hash_algo *algo; + int ret; + + ret = hash_lookup_algo(name, &algo); + if (ret < 0) { debug("Unsupported hash alogrithm\n"); return -1; } + + algo->hash_func_ws(data, data_len, value, algo->chunk_size); + *value_len = algo->digest_size; + + if (!strcmp(name, "crc32")) + crc32_uimage_fixup(value); + return 0; } diff --git a/drivers/crypto/fsl/Kconfig b/drivers/crypto/fsl/Kconfig index 1f5dfb94bb..e467ab1b71 100644 --- a/drivers/crypto/fsl/Kconfig +++ b/drivers/crypto/fsl/Kconfig @@ -1,6 +1,8 @@ config FSL_CAAM bool "Freescale Crypto Driver Support" select SHA_HW_ACCEL + # hw_sha1() under drivers/crypto, and needed with SHA_HW_ACCEL + imply SPL_CRYPTO imply CMD_HASH help Enables the Freescale's Cryptographic Accelerator and Assurance