From patchwork Thu Dec 6 00:46:34 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 204074 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id B74BF2C0134 for ; Thu, 6 Dec 2012 11:50:55 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D8D074A1A2; Thu, 6 Dec 2012 01:50:53 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id OJ8L-iA8k1aP; Thu, 6 Dec 2012 01:50:53 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id E22CB4A13A; Thu, 6 Dec 2012 01:50:24 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 82F974A145 for ; Thu, 6 Dec 2012 01:50:20 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uICy8PyECU2A for ; Thu, 6 Dec 2012 01:50:17 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-wg0-f74.google.com (mail-wg0-f74.google.com [74.125.82.74]) by theia.denx.de (Postfix) with ESMTPS id 2BF944A120 for ; Thu, 6 Dec 2012 01:50:15 +0100 (CET) Received: by mail-wg0-f74.google.com with SMTP id dt14so584027wgb.3 for ; Wed, 05 Dec 2012 16:50:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-gm-message-state; bh=zB1syNkqOYcWQzERZfUsm21scgsoeWwh/1CWqfrSkq8=; b=Y8pksqcPBnHJ3n6pNpQtXZUQrnMhieuvYW/ZyyOramMU89ijRerOkNdDBpcZ149a/F rcv/eA7Bnh2siJB8GPeiVxUQL6IpUIhrssmUs60rajhXBJXPyr7qHwa8NVk/qu4Gmmch xUDDUw3ZpRV97o/ooEWSSzq2awrfBMaQyJ9oZHAhM+8lbPkz4wcNzWqE5X09yO5wZZLp AQmy9HZ58ZLD/pJrLlsY+pZGafvHTpfS/iYBmSUeZa45OWgIoQdq79B9punaM/75ye/X ZmwC8FdFfD9lBXjVA4ToPnkeZLsaNwiP8mtULU7KjtWVK7lmnGGpPgEXo3NLbQQTSfyH abBw== Received: by 10.14.204.199 with SMTP id h47mr11046790eeo.4.1354755015652; Wed, 05 Dec 2012 16:50:15 -0800 (PST) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id g9si896585eeo.1.2012.12.05.16.50.15 (version=TLSv1/SSLv3 cipher=AES128-SHA); Wed, 05 Dec 2012 16:50:15 -0800 (PST) Received: from kaka.mtv.corp.google.com (kaka.mtv.corp.google.com [172.22.73.79]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 48A69200057; Wed, 5 Dec 2012 16:50:15 -0800 (PST) Received: by kaka.mtv.corp.google.com (Postfix, from userid 121222) id 9688C160AD3; Wed, 5 Dec 2012 16:50:14 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Date: Wed, 5 Dec 2012 16:46:34 -0800 Message-Id: <1354754807-21449-8-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1354754807-21449-1-git-send-email-sjg@chromium.org> References: <1354754807-21449-1-git-send-email-sjg@chromium.org> X-Gm-Message-State: ALoCoQl0psLGdTe8KQG+//MVPwNTcoToEqCcNu8F4vaAYmVJNqe+W/XtuWcpvrIkrL2dguzhAZ6vgzf8tET3TazJwBVu/B/DtDw95EbapeB96DR5nuHs9zv4NvmZ1/ZS3MKZMzkvZRWyQNbFhZcNqkYoMqDA382+b8Tuu1stTfgPlOEJyMcz3Eo57UGpiI012QkxOkxMboaC Cc: Tom Rini , Joe Hershberger Subject: [U-Boot] [PATCH v3 07/20] sha256: Use const where possible and add watchdog function X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de In preparation for making the hash function common, we may as well use const where we can. Also add a watchdog version of the hashing function. Signed-off-by: Simon Glass --- Changes in v3: None Changes in v2: - Add new patch to adjust sha256 functions to const and watchdog include/sha256.h | 8 +++++++- lib/sha256.c | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/include/sha256.h b/include/sha256.h index e38ea89..beadab3 100644 --- a/include/sha256.h +++ b/include/sha256.h @@ -3,6 +3,9 @@ #define SHA256_SUM_LEN 32 +/* Reset watchdog each time we process this many bytes */ +#define CHUNKSZ_SHA256 (64 * 1024) + typedef struct { uint32_t total[2]; uint32_t state[8]; @@ -10,7 +13,10 @@ typedef struct { } sha256_context; void sha256_starts(sha256_context * ctx); -void sha256_update(sha256_context * ctx, uint8_t * input, uint32_t length); +void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length); void sha256_finish(sha256_context * ctx, uint8_t digest[SHA256_SUM_LEN]); +void sha256_csum_wd(const unsigned char *input, unsigned int ilen, + unsigned char *output, unsigned int chunk_sz); + #endif /* _SHA256_H */ diff --git a/lib/sha256.c b/lib/sha256.c index deb63a4..ab2db48 100644 --- a/lib/sha256.c +++ b/lib/sha256.c @@ -60,7 +60,7 @@ void sha256_starts(sha256_context * ctx) ctx->state[7] = 0x5BE0CD19; } -void sha256_process(sha256_context * ctx, uint8_t data[64]) +static void sha256_process(sha256_context *ctx, const uint8_t data[64]) { uint32_t temp1, temp2; uint32_t W[64]; @@ -191,7 +191,7 @@ void sha256_process(sha256_context * ctx, uint8_t data[64]) ctx->state[7] += H; } -void sha256_update(sha256_context * ctx, uint8_t * input, uint32_t length) +void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length) { uint32_t left, fill; @@ -260,3 +260,36 @@ void sha256_finish(sha256_context * ctx, uint8_t digest[32]) PUT_UINT32_BE(ctx->state[6], digest, 24); PUT_UINT32_BE(ctx->state[7], digest, 28); } + +/* + * Output = SHA-256( input buffer ). Trigger the watchdog every 'chunk_sz' + * bytes of input processed. + */ +void sha256_csum_wd(const unsigned char *input, unsigned int ilen, + unsigned char *output, unsigned int chunk_sz) +{ + sha256_context ctx; +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) + unsigned char *end, *curr; + int chunk; +#endif + + sha256_starts(&ctx); + +#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) + curr = input; + end = input + ilen; + while (curr < end) { + chunk = end - curr; + if (chunk > chunk_sz) + chunk = chunk_sz; + sha256_update(&ctx, curr, chunk); + curr += chunk; + WATCHDOG_RESET(); + } +#else + sha256_update(&ctx, input, ilen); +#endif + + sha256_finish(&ctx, output); +}