From patchwork Thu Oct 13 14:53:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gary R Hook X-Patchwork-Id: 9375057 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D05E260839 for ; Thu, 13 Oct 2016 14:54:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C03FF2A100 for ; Thu, 13 Oct 2016 14:54:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B4DAC2A114; Thu, 13 Oct 2016 14:54:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8C2AF2A112 for ; Thu, 13 Oct 2016 14:54:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752558AbcJMOyA (ORCPT ); Thu, 13 Oct 2016 10:54:00 -0400 Received: from mail-bn3nam01on0066.outbound.protection.outlook.com ([104.47.33.66]:45479 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752763AbcJMOx5 (ORCPT ); Thu, 13 Oct 2016 10:53:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=vTySxdnux73iP8fLnkvZ5XML36vMHPkB0qulvFjGvVA=; b=3gWl1hScu9l/9sfACxJzhqSDxwJ0qN97+QAuEvcPKNY7dmEiAgFpT2wrRnDJk3tJ5nTn3dyRYYvlRqsrWzRz9gSkOliJbROC5mBoU5JZ/CWj6jAB0EaeoV3P/7moRwCDD4WwUdmtYZKechJCSCVuQ9S3PmeYHf3BpiC3fdIq0u8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Gary.Hook@amd.com; Received: from [10.236.19.42] (165.204.77.1) by BN6PR12MB1395.namprd12.prod.outlook.com (10.168.228.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.629.8; Thu, 13 Oct 2016 14:53:26 +0000 Subject: [PATCH 4/6] crypto: ccp - Add RSA support for a v5 ccp From: Gary R Hook To: CC: , , Date: Thu, 13 Oct 2016 09:53:19 -0500 Message-ID: <20161013145319.19759.70911.stgit@taos> In-Reply-To: <20161013144542.19759.6924.stgit@taos> References: <20161013144542.19759.6924.stgit@taos> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BY2PR1001CA0015.namprd10.prod.outlook.com (10.164.163.153) To BN6PR12MB1395.namprd12.prod.outlook.com (10.168.228.141) X-MS-Office365-Filtering-Correlation-Id: dd8abbea-0aeb-456e-6de9-08d3f378b098 X-Microsoft-Exchange-Diagnostics: 1; BN6PR12MB1395; 2:yycHIJCydqVpkFu1xHkEmw2HrjwfSU1E99rYKz/Gh8eIuiWbMvgVooRee6k4qQm88yrofTIpsRgnwchqHQQ+3+GmtEWbAOUtq6U3Y9z+X2MvtSl2e91wkXTzym+ISzZ5Vo0nk4tmu17VbSRhhuU3WfefnPa7rDFKOUsAcNESVyl4ijE2St1ssMt0313wvVGv; 3:ZJs/kYYNlChiSgLeQf76gf8Pn3TkTfVPADY+uw8w726WbA03Mml9fLc8Sfn3SnzXxscrYSRXVsh8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1iSZ7szWy4C0fw3RtSMmlL3yV7RIGk8G+QQkiuUHSIAoQECZ032NQgUAFy8h+/Vl+FxsJdFe+NbkJKef8gFoViTFNuzeWgkMCQP7MRbhgVx/z5IbMWL+FWiYOsQolWkz/7XAKdRKaIfkLby6WxqciWDY0fkJz3b+u4Fj7MULGsEsDGLmeug4VenzcEQWzYfEXa1vSIgaEKxVLImZ/cwD+z12l/Bl2utcz8GGis6mQ0IuVVZrBtD X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6055026); SRVR:BN6PR12MB1395; BCL:0; PCL:0; RULEID:; SRVR:BN6PR12MB1395; X-Microsoft-Exchange-Diagnostics: 1; BN6PR12MB1395; 4:YvD4pCiQB9SfYCYgoC3ZxJo34UNgL28kpXkfuuHOm8dpF4Zqhxxt4afHOLZ2uMQ8zzkbhdwvGY+GhgePljr+i7LS39Tgu8uLuNzt0gSIe3vYIyce+Xk7wdumtxX5qGLwAiWHPWlEHoQByTS2pSXql3LduBhIAyi8x+2bm0VBqrE0NLmGVi47jzFR3gAlDyuc1PpoVoWLYwqazXqDD2ODf1VVqCLBgZk+h7NDgxogIGv+d8n57d1xz/FKjO6S1Ma6HR2gP03ZOrKtIPndNBt0e1kfhZ3Y5NggOTPvgbYgUnskKagfaPHtJ+TRRmhdedqoZZAPf/8VvqcnuZAnrG4tmNMZbN0fbSYQWaxOquyiDblqLIbBMZ5IflM1TxbYaY3uLvO5C++YDokDwlB2fSVAYvEJF4luxIwPykbgrFgKkn0gdNb9qdJe1dfTgGgdsizZ X-Forefront-PRVS: 0094E3478A X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(189002)(199003)(83506001)(101416001)(586003)(50986999)(76176999)(6116002)(1076002)(33716001)(2906002)(2351001)(229853001)(54356999)(42186005)(97736004)(189998001)(106356001)(4001350100001)(3846002)(8676002)(81166006)(230700001)(81156014)(33646002)(105586002)(23676002)(97746001)(7846002)(103116003)(4326007)(92566002)(305945005)(7736002)(77096005)(19580395003)(5660300001)(6916009)(19580405001)(47776003)(86362001)(66066001)(110136003)(6666003)(50466002)(2950100002)(68736007)(9686002); DIR:OUT; SFP:1101; SCL:1; SRVR:BN6PR12MB1395; H:[10.236.19.42]; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCTjZQUjEyTUIxMzk1OzIzOjJVY2N2N1ZsamdlMi9tUXRiN3ljbTYyK3hq?= =?utf-8?B?YzM5WE5kRTh2VTIxaXNERGlVZTdQdFRHdzlNRld6THMvTU8yM2YzVEozeDlp?= =?utf-8?B?NjhkNGp4L1B0TFZMVVdPNTZFblBCSVJheXV1c25aSjFqZ1g4aDdidkVBSTNF?= =?utf-8?B?NjYvZXBOM0x3K1ZQYjVuUkZvZnZEcFNpR01wUXFmN3pMZXgxd3RkNi9vWjZY?= =?utf-8?B?N0FiSDhNUU1EOWgrTEg3dmhOTERlbWU3N0NhbjUvQmNMUWVvcmhEVTJZZjZH?= =?utf-8?B?dmV1MnBjQ2kxMVcxY2V1RGFSSjVWME44dkJsdTFLQ2NCTG9uVzlsVGFtTGZw?= =?utf-8?B?amg1OTgrcFVMUm4ySm84RnZqK0RUbzVzRXdQOEZaU2NOR1RZcnhTTkxSTm5Q?= =?utf-8?B?aEN6Mmw3M2ZZL0E3aVRicVg5QUd2aVNmMm52QVdRa1dnWlNTdlpib2xtRFA2?= =?utf-8?B?SDVwNll3azlZb0Z0bHhrSkdIeWo1WjNIUnNQbnMrUjdQQzI0S1YzamU3ajQr?= =?utf-8?B?Q0FVVXpRM2pNREozd0IzUERVU3FwYjlmajlSQ2VPTUc1cklZWitUcHFRQU8w?= =?utf-8?B?Q0NaT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nSxbdI4GQ4YPmv1iOnnR56Ai1QIZHA/Lr+JZrA7VkONDpg84Ym74cFrq28jLH+RnBR+htAoJ7FJmWOs9bxKBig62tXE5IPQGW75+NKKG2DPVtCLJkC0VvuNT8ohF9ZTfPPihpMMyozLghaOXOB0JJMXP3IvxEs2VaFliiZT+mAFxhy9M gaJESXVuCrbNGANu4w== SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; BN6PR12MB1395; 20:1bNHPfZ30FiU5jKMSixXoU3gUyL/R0qZZDhYefgU6D3TegVIHg1aJz0PLqlEWFlfQq7uTu3BelqzPOEPDRQWK+s4aW4PzrllKohp0T0Vg7KMML324AH6b9m+kMj48C//9tzHr46U7wxeJlMhYdg4FxDRQWI9T1JQolcsgdZsnBpnxdfMw2mS1wbjGsHEwKTpveX/YT2T4vIQYgwAd6N8KQMSa1PqFGHGAhPEGrDFmimSh7zDKfJTrfl0p+1CDrU1 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2016 14:53:26.7291 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1395 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Take into account device implementation differences for RSA. Signed-off-by: Gary R Hook --- drivers/crypto/ccp/ccp-crypto-rsa.c | 14 +++-- drivers/crypto/ccp/ccp-crypto.h | 3 - drivers/crypto/ccp/ccp-dev.h | 2 - drivers/crypto/ccp/ccp-ops.c | 97 +++++++++++++++++++++++------------ 4 files changed, 73 insertions(+), 43 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-crypto" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/crypto/ccp/ccp-crypto-rsa.c b/drivers/crypto/ccp/ccp-crypto-rsa.c index 7dab43b..94411de 100644 --- a/drivers/crypto/ccp/ccp-crypto-rsa.c +++ b/drivers/crypto/ccp/ccp-crypto-rsa.c @@ -125,7 +125,7 @@ static void ccp_rsa_free_key_bufs(struct ccp_ctx *ctx) } static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key, - unsigned int keylen, bool public) + unsigned int keylen, bool private) { struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm); struct rsa_key raw_key; @@ -139,10 +139,10 @@ static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key, memset(&raw_key, 0, sizeof(raw_key)); /* Code borrowed from crypto/rsa.c */ - if (public) - ret = rsa_parse_pub_key(&raw_key, key, keylen); - else + if (private) ret = rsa_parse_priv_key(&raw_key, key, keylen); + else + ret = rsa_parse_pub_key(&raw_key, key, keylen); if (ret) goto e_ret; @@ -169,7 +169,7 @@ static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key, goto e_nkey; sg_init_one(&ctx->u.rsa.n_sg, ctx->u.rsa.n_buf, ctx->u.rsa.n_len); - if (!public) { + if (private) { ctx->u.rsa.pkey.d = mpi_read_raw_data(raw_key.d, raw_key.d_sz); if (!ctx->u.rsa.pkey.d) goto e_nkey; @@ -196,13 +196,13 @@ e_ret: static int ccp_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) { - return ccp_rsa_setkey(tfm, key, keylen, false); + return ccp_rsa_setkey(tfm, key, keylen, true); } static int ccp_rsa_setpubkey(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) { - return ccp_rsa_setkey(tfm, key, keylen, true); + return ccp_rsa_setkey(tfm, key, keylen, false); } static int ccp_rsa_init_tfm(struct crypto_akcipher *tfm) diff --git a/drivers/crypto/ccp/ccp-crypto.h b/drivers/crypto/ccp/ccp-crypto.h index 4a1d206..c6cf318 100644 --- a/drivers/crypto/ccp/ccp-crypto.h +++ b/drivers/crypto/ccp/ccp-crypto.h @@ -138,8 +138,7 @@ struct ccp_aes_cmac_exp_ctx { u8 buf[AES_BLOCK_SIZE]; }; -/* - * SHA-related defines +/* SHA-related defines * These values must be large enough to accommodate any variant */ #define MAX_SHA_CONTEXT_SIZE SHA512_DIGEST_SIZE diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h index 0d996fe..143f00f 100644 --- a/drivers/crypto/ccp/ccp-dev.h +++ b/drivers/crypto/ccp/ccp-dev.h @@ -193,6 +193,7 @@ #define CCP_SHA_SB_COUNT 1 #define CCP_RSA_MAX_WIDTH 4096 +#define CCP5_RSA_MAX_WIDTH 16384 #define CCP_PASSTHRU_BLOCKSIZE 256 #define CCP_PASSTHRU_MASKSIZE 32 @@ -515,7 +516,6 @@ struct ccp_op { struct ccp_passthru_op passthru; struct ccp_ecc_op ecc; } u; - struct ccp_mem key; }; static inline u32 ccp_addr_lo(struct ccp_dma_info *info) diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c index 826782d..07b8dfb 100644 --- a/drivers/crypto/ccp/ccp-ops.c +++ b/drivers/crypto/ccp/ccp-ops.c @@ -1283,49 +1283,72 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) int i = 0; int ret = 0; - if (rsa->key_size > CCP_RSA_MAX_WIDTH) - return -EINVAL; + if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)) { + if (rsa->key_size > CCP_RSA_MAX_WIDTH) + return -EINVAL; + } else { + if (rsa->key_size > CCP5_RSA_MAX_WIDTH) + return -EINVAL; + } if (!rsa->exp || !rsa->mod || !rsa->src || !rsa->dst) return -EINVAL; - /* The RSA modulus must precede the message being acted upon, so - * it must be copied to a DMA area where the message and the - * modulus can be concatenated. Therefore the input buffer - * length required is twice the output buffer length (which - * must be a multiple of 256-bits). - */ - o_len = ((rsa->key_size + 255) / 256) * 32; - i_len = o_len * 2; - - sb_count = o_len / CCP_SB_BYTES; - memset(&op, 0, sizeof(op)); op.cmd_q = cmd_q; - op.jobid = ccp_gen_jobid(cmd_q->ccp); - op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q, sb_count); + op.jobid = CCP_NEW_JOBID(cmd_q->ccp); - if (!op.sb_key) - return -EIO; + if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)) { + /* The RSA modulus must precede the message being acted upon, so + * it must be copied to a DMA area where the message and the + * modulus can be concatenated. Therefore the input buffer + * length required is twice the output buffer length (which + * must be a multiple of 256-bits). + */ + sb_count = (rsa->key_size + CCP_SB_BYTES - 1) / CCP_SB_BYTES; + o_len = sb_count * 32; /* bytes */ + i_len = o_len * 2; /* bytes */ + + op.sb_key = cmd_q->ccp->vdata->perform->sballoc(cmd_q, + sb_count); + if (!op.sb_key) + return -EIO; + } else { + /* A version 5 device allows the key to be in memory */ + o_len = rsa->mod_len; + i_len = o_len * 2; /* bytes */ + op.sb_key = cmd_q->sb_key; + } - /* The RSA exponent may span multiple (32-byte) SB entries and must - * be in little endian format. Reverse copy each 32-byte chunk - * of the exponent (En chunk to E0 chunk, E(n-1) chunk to E1 chunk) - * and each byte within that chunk and do not perform any byte swap - * operations on the passthru operation. - */ ret = ccp_init_dm_workarea(&exp, cmd_q, o_len, DMA_TO_DEVICE); if (ret) goto e_sb; - ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, rsa->exp_len); + if (rsa->mode == CCP_RSA_ENCRYPT) + ret = ccp_reverse_set_dm_area(&exp, 0, rsa->exp, 0, + rsa->exp_len); + else + ret = ccp_reverse_set_dm_area(&exp, 0, rsa->d_sg, 0, + rsa->d_len); if (ret) goto e_exp; - ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key, - CCP_PASSTHRU_BYTESWAP_NOOP); - if (ret) { - cmd->engine_error = cmd_q->cmd_error; - goto e_exp; + + if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)) { + /* The RSA exponent may span multiple (32-byte) KSB entries and + * must be in little endian format. Reverse copy each 32-byte + * chunk of the exponent (En chunk to E0 chunk, E(n-1) chunk to + * E1 chunk) and each byte within that chunk and do not perform + * any byte swap operations on the passthru operation. + */ + ret = ccp_copy_to_sb(cmd_q, &exp, op.jobid, op.sb_key, + CCP_PASSTHRU_BYTESWAP_NOOP); + if (ret) { + cmd->engine_error = cmd_q->cmd_error; + goto e_exp; + } + } else { + op.exp.u.dma.address = exp.dma.address; + op.exp.u.dma.offset = 0; } /* Concatenate the modulus and the message. Both the modulus and @@ -1345,7 +1368,7 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) src.address -= o_len; /* Reset the address to original value */ /* Prepare the output area for the operation */ - ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->mod_len, + ret = ccp_init_data(&dst, cmd_q, rsa->dst, rsa->dst_len, o_len, DMA_FROM_DEVICE); if (ret) goto e_src; @@ -1358,7 +1381,10 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) op.dst.u.dma.offset = 0; op.dst.u.dma.length = o_len; - op.u.rsa.mod_size = rsa->key_size; + if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)) + op.u.rsa.mod_size = rsa->key_size * 8; /* In bits */ + else + op.u.rsa.mod_size = rsa->key_size; op.u.rsa.input_len = i_len; ret = cmd_q->ccp->vdata->perform->rsa(&op); @@ -1366,8 +1392,12 @@ static int ccp_run_rsa_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd) cmd->engine_error = cmd_q->cmd_error; goto e_dst; } + /* Return the length of the result, too */ + for (i = o_len; !dst.dm_wa.address[--i]; ) + ; + rsa->d_len = i + 1; - ccp_reverse_get_dm_area(&dst.dm_wa, 0, rsa->dst, 0, rsa->mod_len); + ccp_reverse_get_dm_area(&dst.dm_wa, 0, rsa->dst, 0, rsa->d_len); e_dst: ccp_free_data(&dst, cmd_q); @@ -1379,7 +1409,8 @@ e_exp: ccp_dm_free(&exp); e_sb: - cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count); + if (cmd_q->ccp->vdata->version < CCP_VERSION(4, 0)) + cmd_q->ccp->vdata->perform->sbfree(cmd_q, op.sb_key, sb_count); return ret; }