From patchwork Wed Jan 28 10:51:10 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: gaurav rana X-Patchwork-Id: 433852 X-Patchwork-Delegate: yorksun@freescale.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 125611401F0 for ; Wed, 28 Jan 2015 22:26:01 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 579C74B7EA; Wed, 28 Jan 2015 12:25:56 +0100 (CET) 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 5F2yfgRE9Vbg; Wed, 28 Jan 2015 12:25:56 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id DD5954B7E3; Wed, 28 Jan 2015 12:25:55 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 9EB374B7E3 for ; Wed, 28 Jan 2015 12:25:51 +0100 (CET) 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 Kb8-Fu1NRrvN for ; Wed, 28 Jan 2015 12:25:51 +0100 (CET) X-Greylist: delayed 1046 seconds by postgrey-1.32 at theia; Wed, 28 Jan 2015 12:25:47 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 na01-by2-obe.outbound.protection.outlook.com (mail-by2on0121.outbound.protection.outlook.com [207.46.100.121]) by theia.denx.de (Postfix) with ESMTPS id B23D24B7DF for ; Wed, 28 Jan 2015 12:25:47 +0100 (CET) Received: from BN3PR0301CA0022.namprd03.prod.outlook.com (25.160.180.160) by BN3PR0301MB1298.namprd03.prod.outlook.com (25.161.210.153) with Microsoft SMTP Server (TLS) id 15.1.65.19; Wed, 28 Jan 2015 10:54:05 +0000 Received: from BY2FFO11FD027.protection.gbl (2a01:111:f400:7c0c::139) by BN3PR0301CA0022.outlook.office365.com (2a01:111:e400:4000::32) with Microsoft SMTP Server (TLS) id 15.1.65.19 via Frontend Transport; Wed, 28 Jan 2015 10:54:05 +0000 Received: from az84smr01.freescale.net (192.88.158.2) by BY2FFO11FD027.mail.protection.outlook.com (10.1.15.216) with Microsoft SMTP Server (TLS) id 15.1.75.11 via Frontend Transport; Wed, 28 Jan 2015 10:54:04 +0000 Received: from perf-idc04.ap.freescale.net (perf-idc04.ap.freescale.net [10.232.14.49]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id t0SAs04O015768; Wed, 28 Jan 2015 03:54:01 -0700 From: Gaurav Rana To: Date: Wed, 28 Jan 2015 16:21:10 +0530 Message-ID: <1422442270-29630-1-git-send-email-gaurav.rana@freescale.com> X-Mailer: git-send-email 1.8.1.4 X-EOPAttributedMessage: 0 Received-SPF: Fail (protection.outlook.com: domain of freescale.com does not designate 192.88.158.2 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.158.2; helo=az84smr01.freescale.net; Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=gaurav.rana@freescale.com; X-Forefront-Antispam-Report: CIP:192.88.158.2; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10019020)(6009001)(92566002)(62966003)(48376002)(15975445007)(77096005)(50986999)(50466002)(77156002)(50226001)(86362001)(36756003)(106466001)(47776003)(85426001)(87936001)(6806004)(105606002)(19580405001)(229853001)(2351001)(19580395003)(33646002)(46102003)(110136001)(104016003)(414714003)(473944003); DIR:OUT; SFP:1102; SCL:1; SRVR:BN3PR0301MB1298; H:az84smr01.freescale.net; FPR:; SPF:Fail; MLV:nov; PTR:InfoDomainNonexistent; LANG:en; MIME-Version: 1.0 X-DmarcAction-Test: None X-Microsoft-Antispam: UriScan:; X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(3005004);SRVR:BN3PR0301MB1298; X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004); SRVR:BN3PR0301MB1298; X-Forefront-PRVS: 047001DADA X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:; SRVR:BN3PR0301MB1298; X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jan 2015 10:54:04.8845 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d; Ip=[192.88.158.2] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN3PR0301MB1298 Cc: ruchika.gupta@freescale.com, scottwood@freescale.com, yorksun@freescale.com Subject: [U-Boot] [PATCH] crypto/fsl - Add progressive hashing support using hardware acceleration. X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.13 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Currently only normal hashing is supported using hardware acceleration. Added support for progressinve hashing using h/w. Signed-off-by: Ruchika Gupta Signed-off-by: Gaurav Rana CC: Simon Glass --- This patch is dependent on following series of 10 patches. https://patchwork.ozlabs.org/patch/432126/ . . . https://patchwork.ozlabs.org/patch/432135/ README | 4 ++ common/hash.c | 10 +++ drivers/crypto/fsl/fsl_hash.c | 141 ++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/fsl/fsl_hash.h | 32 ++++++++++ include/fsl_sec.h | 30 +++++++++ include/hw_sha.h | 77 +++++++++++++++++++++++ 6 files changed, 294 insertions(+) create mode 100644 drivers/crypto/fsl/fsl_hash.h diff --git a/README b/README index cac7978..98aa31f 100644 --- a/README +++ b/README @@ -3151,6 +3151,10 @@ CBFS (Coreboot Filesystem) support CONFIG_SHA1 - support SHA1 hashing CONFIG_SHA256 - support SHA256 hashing + CONFIG_SHA_HW_ACCEL - support SHA1 and SHA256 hashing using hw + acceleration + CONFIG_SHA_PROG_HW_ACCEL - support SHA1 and SHA256 progressive + hashing using hw acceleration Note: There is also a sha1sum command, which should perhaps be deprecated in favour of 'hash sha1'. diff --git a/common/hash.c b/common/hash.c index d154d02..d4becd3 100644 --- a/common/hash.c +++ b/common/hash.c @@ -127,11 +127,21 @@ static struct hash_algo hash_algo[] = { SHA1_SUM_LEN, hw_sha1, CHUNKSZ_SHA1, +#ifdef CONFIG_SHA_PROG_HW_ACCEL + hw_sha1_init, + hw_sha1_update, + hw_sha1_finish, +#endif }, { "sha256", SHA256_SUM_LEN, hw_sha256, CHUNKSZ_SHA256, +#ifdef CONFIG_SHA_PROG_HW_ACCEL + hw_sha256_init, + hw_sha256_update, + hw_sha256_finish, +#endif }, #endif #ifdef CONFIG_SHA1 diff --git a/drivers/crypto/fsl/fsl_hash.c b/drivers/crypto/fsl/fsl_hash.c index d77f257..1681705 100644 --- a/drivers/crypto/fsl/fsl_hash.c +++ b/drivers/crypto/fsl/fsl_hash.c @@ -10,6 +10,8 @@ #include "jobdesc.h" #include "desc.h" #include "jr.h" +#include "fsl_hash.h" +#include #define CRYPTO_MAX_ALG_NAME 80 #define SHA1_DIGEST_SIZE 20 @@ -39,6 +41,111 @@ static struct caam_hash_template driver_hash[] = { }, }; +/* Create the context for progressive hashing using h/w acceleration. + * + * @ctxp: Pointer to the pointer of the context for hashing + * @caam_algo: Enum for SHA1 or SHA256 + * @return 0 if ok, -1 on error + */ +static int caam_hash_init(void **ctxp, enum caam_hash_algos caam_algo) +{ + struct sha_ctx *ctx = malloc(sizeof(struct sha_ctx)); + memset(ctx, 0, sizeof(struct sha_ctx)); + *ctxp = ctx; + return 0; +} + +/* + * Update sg table for progressive hashing using h/w acceleration + * + * The context is freed by this function if an error occurs. + * + * @hash_ctx: Pointer to the context for hashing + * @buf: Pointer to the buffer being hashed + * @size: Size of the buffer being hashed + * @is_last: 1 if this is the last update; 0 otherwise + * @caam_algo: Enum for SHA1 or SHA256 + * @return 0 if ok, -1 on error + */ +static int caam_hash_update(void *hash_ctx, const void *buf, + unsigned int size, int is_last, + enum caam_hash_algos caam_algo) +{ + uint32_t final = 0; + dma_addr_t addr = virt_to_phys((void *)buf); + struct sha_ctx *ctx = (struct sha_ctx *)hash_ctx; + + if (ctx->sg_num >= MAX_SG) { + free(ctx); + return -1; + } + +#ifdef CONFIG_PHYS_64BIT + ctx->sg_tbl[ctx->sg_num].addr_hi = addr >> 32; +#else + ctx->sg_tbl[ctx->sg_num].addr_hi = 0x0; +#endif + ctx->sg_tbl[ctx->sg_num].addr_lo = addr; + + sec_out32(&ctx->sg_tbl[ctx->sg_num].len_flag, + (size & SG_ENTRY_LENGTH_MASK)); + + ctx->sg_num++; + + if (is_last) { + final = sec_in32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag) | + SG_ENTRY_FINAL_BIT; + sec_out32(&ctx->sg_tbl[ctx->sg_num - 1].len_flag, final); + } + + return 0; +} + +/* + * Perform progressive hashing on the given buffer and copy hash at + * destination buffer + * + * The context is freed after completion of hash operation. + * + * @hash_ctx: Pointer to the context for hashing + * @dest_buf: Pointer to the destination buffer where hash is to be copied + * @size: Size of the buffer being hashed + * @caam_algo: Enum for SHA1 or SHA256 + * @return 0 if ok, -1 on error + */ +static int caam_hash_finish(void *hash_ctx, void *dest_buf, + int size, enum caam_hash_algos caam_algo) +{ + uint32_t len = 0; + struct sha_ctx *ctx = (struct sha_ctx *)hash_ctx; + int i = 0, ret = 0; + + if (size < driver_hash[caam_algo].digestsize) { + free(ctx); + return -1; + } + + for (i = 0; i < ctx->sg_num; i++) + len += (sec_in32(&ctx->sg_tbl[i].len_flag) & + SG_ENTRY_LENGTH_MASK); + + inline_cnstr_jobdesc_hash(ctx->sha_desc, (uint8_t *)ctx->sg_tbl, len, + ctx->hash, + driver_hash[caam_algo].alg_type, + driver_hash[caam_algo].digestsize, + 1); + + ret = run_descriptor_jr(ctx->sha_desc); + + if (ret) + debug("Error %x\n", ret); + else + memcpy(dest_buf, ctx->hash, sizeof(ctx->hash)); + + free(ctx); + return ret; +} + int caam_hash(const unsigned char *pbuf, unsigned int buf_len, unsigned char *pout, enum caam_hash_algos algo) { @@ -69,9 +176,43 @@ void hw_sha256(const unsigned char *pbuf, unsigned int buf_len, printf("CAAM was not setup properly or it is faulty\n"); } +int hw_sha256_init(struct hash_algo *algo, void **ctxp) +{ + return caam_hash_init(ctxp, SHA256); +} + +int hw_sha256_update(struct hash_algo *algo, void *ctx, const void *buf, + unsigned int size, int is_last) +{ + return caam_hash_update(ctx, buf, size, is_last, SHA256); +} + +int hw_sha256_finish(struct hash_algo *algo, void *ctx, void *dest_buf, + int size) +{ + return caam_hash_finish(ctx, dest_buf, size, SHA256); +} + void hw_sha1(const unsigned char *pbuf, unsigned int buf_len, unsigned char *pout, unsigned int chunk_size) { if (caam_hash(pbuf, buf_len, pout, SHA1)) printf("CAAM was not setup properly or it is faulty\n"); } + +int hw_sha1_init(struct hash_algo *algo, void **ctxp) +{ + return caam_hash_init(ctxp, SHA1); +} + +int hw_sha1_update(struct hash_algo *algo, void *ctx, const void *buf, + unsigned int size, int is_last) +{ + return caam_hash_update(ctx, buf, size, is_last, SHA1); +} + +int hw_sha1_finish(struct hash_algo *algo, void *ctx, void *dest_buf, + int size) +{ + return caam_hash_finish(ctx, dest_buf, size, SHA1); +} diff --git a/drivers/crypto/fsl/fsl_hash.h b/drivers/crypto/fsl/fsl_hash.h new file mode 100644 index 0000000..7f07ea4 --- /dev/null +++ b/drivers/crypto/fsl/fsl_hash.h @@ -0,0 +1,32 @@ +/* + * Copyright 2014 Freescale Semiconductor, Inc. + * + * SPDX-License-Identifier: GPL-2.0+ + * + */ + +#ifndef _SHA_H +#define _SHA_H + +#include +#include "jr.h" + +#define MAX_SG 16 + +/* + * Hash context contain the following fields + * Sha Descriptor + * number of entries in sg table + * total length of buffer + * sg entry table + * index to the hash calculated + */ +struct sha_ctx { + uint32_t sha_desc[64]; + uint32_t sg_num; + uint32_t len; + struct sg_entry sg_tbl[MAX_SG]; + u8 hash[HASH_MAX_DIGEST_SIZE]; +}; + +#endif diff --git a/include/fsl_sec.h b/include/fsl_sec.h index aa850a3..cece820 100644 --- a/include/fsl_sec.h +++ b/include/fsl_sec.h @@ -175,6 +175,36 @@ struct jr_regs { u32 jrcr; }; +/* + * Scatter Gather Entry - Specifies the the Scatter Gather Format + * related information + */ +struct sg_entry { +#ifdef CONFIG_SYS_FSL_SEC_LE + uint32_t addr_lo; /* Memory Address - lo */ + uint16_t addr_hi; /* Memory Address of the start of the + * buffer - hi + */ + uint16_t reserved_zero; +#else + uint16_t reserved_zero; + uint16_t addr_hi; /* Memory Address of the start of the + * buffer - hi + */ + uint32_t addr_lo; /* Memory Address - lo */ +#endif + + uint32_t len_flag; /* Length of the data in the frame */ +#define SG_ENTRY_LENGTH_MASK 0x3FFFFFFF +#define SG_ENTRY_EXTENSION_BIT 0x80000000 +#define SG_ENTRY_FINAL_BIT 0x40000000 + uint32_t bpid_offset; +#define SG_ENTRY_BPID_MASK 0x00FF0000 +#define SG_ENTRY_BPID_SHIFT 16 +#define SG_ENTRY_OFFSET_MASK 0x00001FFF +#define SG_ENTRY_OFFSET_SHIFT 0 +}; + int sec_init(void); #endif diff --git a/include/hw_sha.h b/include/hw_sha.h index 783350d..6b040d2 100644 --- a/include/hw_sha.h +++ b/include/hw_sha.h @@ -22,6 +22,44 @@ void hw_sha256(const uchar * in_addr, uint buflen, uchar * out_addr, uint chunk_size); +/* + * Create the context for sha256 progressive hashing using h/w acceleration + * + * @algo: Pointer to the hash_algo struct + * @ctxp: Pointer to the pointer of the context for hashing + * @return 0 if ok, -1 on error + */ +int hw_sha256_init(struct hash_algo *algo, void **ctxp); + +/* + * Update buffer for sha256 progressive hashing using h/w acceleration + * + * The context is freed by this function if an error occurs. + * + * @algo: Pointer to the hash_algo struct + * @ctx: Pointer to the context for hashing + * @buf: Pointer to the buffer being hashed + * @size: Size of the buffer being hashed + * @is_last: 1 if this is the last update; 0 otherwise + * @return 0 if ok, -1 on error + */ +int hw_sha256_update(struct hash_algo *algo, void *ctx, const void *buf, + unsigned int size, int is_last); + +/* + * Copy sha256 hash result at destination location + * + * The context is freed after completion of hash operation. + * + * @algo: Pointer to the hash_algo struct + * @ctx: Pointer to the context for hashing + * @dest_buf: Pointer to the destination buffer where hash is to be copied + * @size: Size of the buffer being hashed + * @return 0 if ok, -1 on error + */ +int hw_sha256_finish(struct hash_algo *algo, void *ctx, void *dest_buf, + int size); + /** * Computes hash value of input pbuf using h/w acceleration * @@ -34,4 +72,43 @@ void hw_sha256(const uchar * in_addr, uint buflen, */ void hw_sha1(const uchar * in_addr, uint buflen, uchar * out_addr, uint chunk_size); + +/* + * Create the context for sha1 progressive hashing using h/w acceleration + * + * @algo: Pointer to the hash_algo struct + * @ctxp: Pointer to the pointer of the context for hashing + * @return 0 if ok, -1 on error + */ +int hw_sha1_init(struct hash_algo *algo, void **ctxp); + +/* + * Update buffer for sha1 progressive hashing using h/w acceleration + * + * The context is freed by this function if an error occurs. + * + * @algo: Pointer to the hash_algo struct + * @ctx: Pointer to the context for hashing + * @buf: Pointer to the buffer being hashed + * @size: Size of the buffer being hashed + * @is_last: 1 if this is the last update; 0 otherwise + * @return 0 if ok, -1 on error + */ +int hw_sha1_update(struct hash_algo *algo, void *ctx, const void *buf, + unsigned int size, int is_last); + +/* + * Copy sha1 hash result at destination location + * + * The context is freed after completion of hash operation. + * + * @algo: Pointer to the hash_algo struct + * @ctx: Pointer to the context for hashing + * @dest_buf: Pointer to the destination buffer where hash is to be copied + * @size: Size of the buffer being hashed + * @return 0 if ok, -1 on error + */ +int hw_sha1_finish(struct hash_algo *algo, void *ctx, void *dest_buf, + int size); + #endif