From patchwork Fri Nov 4 16:04:32 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: 9412773 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 BC2FE6022E for ; Fri, 4 Nov 2016 16:04:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AC5E42B189 for ; Fri, 4 Nov 2016 16:04:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A11A72B1BC; Fri, 4 Nov 2016 16:04:43 +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 B342A2B189 for ; Fri, 4 Nov 2016 16:04:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936132AbcKDQEm (ORCPT ); Fri, 4 Nov 2016 12:04:42 -0400 Received: from mail-dm3nam03on0064.outbound.protection.outlook.com ([104.47.41.64]:34136 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S935059AbcKDQEl (ORCPT ); Fri, 4 Nov 2016 12:04:41 -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=959/gYnctqX6h2VCjzVD/1M5u0GO6DCVvb8jH5f9LwY=; b=YN7WPN7tt5rpYONXnasvYcGnbmhFQNCL6ZFGYpYOSDROkMdr9e+UyTZhjk55ik11/W26hmJzvGNvqdV+NmYW+MYxJ33ARMh0brezDGbmIEOEGIWMNqUWEJN9gQ+i8pnPMod/1DpEJAvqpFqjoli7lrc+/VghDK0VmfRr+rPRtwU= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=Gary.Hook@amd.com; Received: from [10.236.19.42] (165.204.77.1) by DM5PR12MB1404.namprd12.prod.outlook.com (10.168.238.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.693.12; Fri, 4 Nov 2016 16:04:38 +0000 Subject: [PATCH V2 6/9] crypto: ccp - Add support for RSA on the CCP From: Gary R Hook To: CC: , , Date: Fri, 4 Nov 2016 11:04:32 -0500 Message-ID: <20161104160432.18155.29136.stgit@taos> In-Reply-To: <20161104160140.18155.75618.stgit@taos> References: <20161104160140.18155.75618.stgit@taos> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BLUPR01CA052.prod.exchangelabs.com (10.160.23.42) To DM5PR12MB1404.namprd12.prod.outlook.com (10.168.238.143) X-MS-Office365-Filtering-Correlation-Id: 4fc83658-f84a-45dd-1c98-08d404cc47b5 X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1404; 2:JiwTNsZGU7ei/30mnieqwkK69Wp//8sggXNKJ/gS/eWIN3ZMVipQcmC6tIdiWBNNb8YA/U/G6Lhgqh4AUDjr62AJL1v50vftIZlYAFRclcfmHXgpBzMMA7cKGLb7hANu7MAxlKn/3Zth0W7vBV5PBNuY6fAL0g/Fs9+zV1/RK9L2pMBA1UUOsvb2tZT7oXxchNhExGSpoVv76HwEVZ99fg==; 3:7OMvCYWagEMln8451qtbVdXlCe3Ihy2yriI6rxLWDgKBPTMqJvZ+8O26sthqwr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4K8vP6pUvRiMR8kPPf7QlkGr90A6qF3ZbkASnWmw4nEhbe4yRMY/kbuGxjIACLAFOc9Q995f38bG1ZiNtLZRsujlX8mI1vpmKUJ7LKXYWAjQaozU0iqlJMV/R1/M20QZNDcSmab9Xs28TXqhWEbAXg+; 20:CfDVP9UEmk3mgBhMhelT71gxWr9vAx4GTqoEa6VlyNwN75SDL8eiMAJjzFovxA0CFqj0KrAR7TVGm6T4emWymN5DNTi1ycHMsQGSvLgItoY9EvDmAQ/6/epBMTIir8KEOkPvVb/Xgw/vw/9RzutqiTNW4tIb4AVEYiDoYOc0yLzljhb1FlgXfs/BoPYWPHsxle9zeMyKo3rmrwg8tYn27tjpHDdeoSUbXlSMPYhsQHkRCaFjZRWFBOkaJa6+m635/zAJl2WB4QaIbNIwpv9E6v1gM61SRKioNjdKa1FZQKE/+u3fT9ZoLcz5rdWnO6n0JPCtpZ8oMRzpw+Nd5sV7ksBdtXpR6zVd3Vz1V195ftmVIB+TCoE6vV3zHRoHFPaVaG3J3S7m9JB/u/DRxErV6RRJgJ1PxkH9idCbueoaLf2uoYL58u2nAHn/uqUV24K/gFbJFaRQUtc91y0KSFP2sP/ZMjdhD1QHjLYwUv6nk+iQCTq2fiC5+yrsqZPfbuu9 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:DM5PR12MB1404; BCL:0; PCL:0; RULEID:; SRVR:DM5PR12MB1404; X-Microsoft-Exchange-Diagnostics: 1; DM5PR12MB1404; 4:VBzzZ81svh/NN4iv8CnluLbgP28dYCQGfQy+4AXHVrYFQ350baI0AZLeN5+OdMTThSL3VXa34F1xrhjRpI4wTX6xtF8ppBPbZiXxtSIA53ldlfTMmfpaPf6jV3JQKixM6gqliIVWe+BF4bLptjdOJ9uv2JYSM6QTk4XgWZsLiem7LaKCEQAzMdF65eWwar6R6QYHywJvSntgItwcsQpY8U5zFoeZwAetr0gZZa06XwSQ6ahSa0oSo1WLhPyQqnA8UuzOCl+GTCeP8rB7kT8DfmxKC0W0wYYHzqXNLg8PpfzoQPvsTzw1ttGgdhCbe1tUa8nZKulv5N86QWx9eu7wQEv2+zCASuu82wO8aYiGoUZxXvhhKEBbfhgh+/Qrqkx0QPoiDrDVNRvlyCiR+GXDYR94sPxlRAMyOik/415FDnLZnuC8Uj99dhl8ECyABUbVAFM2PxyAzZ9S/rpR5PDCeg== X-Forefront-PRVS: 01165471DB X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6049001)(6009001)(7916002)(199003)(189002)(2906002)(86362001)(77096005)(50986999)(5660300001)(23676002)(33716001)(6916009)(68736007)(229853001)(92566002)(76176999)(8676002)(4326007)(103116003)(2950100002)(54356999)(6666003)(305945005)(6116002)(7736002)(105586002)(106356001)(66066001)(4001350100001)(2351001)(47776003)(42186005)(50466002)(97736004)(101416001)(19580395003)(110136003)(33646002)(83506001)(7846002)(189998001)(3846002)(81166006)(81156014)(97746001)(19580405001)(586003)(1076002)(9686002)(230700001)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:DM5PR12MB1404; H:[10.236.19.42]; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtETTVQUjEyTUIxNDA0OzIzOnVoR0h5TU9QWHV6VmdjRXh4cHJyZzdVSU0v?= =?utf-8?B?c2hNeWI0aUliUFBWTDNSMk5Tc3pKUU1QemI5eEtXbVdmczJZempzY2EvbC84?= =?utf-8?B?K3owcUFXWGZGZWluZUdWZ3FEdGUzU2pNbENDUDlmeFJ0eE9iS0M5dDFGSUkz?= =?utf-8?B?RHpZZEJlWEFEUk14ODIzSjBNQnBxaXdWbkpBaTR6VjZzamwzSWt4MFhEWThY?= =?utf-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4 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Nov 2016 16:04:38.4239 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR12MB1404 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 Wire up the CCP as an RSA cipher provider. Signed-off-by: Gary R Hook --- drivers/crypto/ccp/Makefile | 1 drivers/crypto/ccp/ccp-crypto-main.c | 19 ++ drivers/crypto/ccp/ccp-crypto-rsa.c | 294 ++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/ccp-crypto.h | 32 ++++ include/linux/ccp.h | 1 5 files changed, 346 insertions(+), 1 deletion(-) create mode 100644 drivers/crypto/ccp/ccp-crypto-rsa.c -- 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/Makefile b/drivers/crypto/ccp/Makefile index 346ceb8..23f89b7 100644 --- a/drivers/crypto/ccp/Makefile +++ b/drivers/crypto/ccp/Makefile @@ -12,4 +12,5 @@ ccp-crypto-objs := ccp-crypto-main.o \ ccp-crypto-aes.o \ ccp-crypto-aes-cmac.o \ ccp-crypto-aes-xts.o \ + ccp-crypto-rsa.o \ ccp-crypto-sha.o diff --git a/drivers/crypto/ccp/ccp-crypto-main.c b/drivers/crypto/ccp/ccp-crypto-main.c index e0380e5..38d4466 100644 --- a/drivers/crypto/ccp/ccp-crypto-main.c +++ b/drivers/crypto/ccp/ccp-crypto-main.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "ccp-crypto.h" @@ -33,9 +34,14 @@ module_param(sha_disable, uint, 0444); MODULE_PARM_DESC(sha_disable, "Disable use of SHA - any non-zero value"); +static unsigned int rsa_disable; +module_param(rsa_disable, uint, 0444); +MODULE_PARM_DESC(rsa_disable, "Disable use of RSA - any non-zero value"); + /* List heads for the supported algorithms */ static LIST_HEAD(hash_algs); static LIST_HEAD(cipher_algs); +static LIST_HEAD(akcipher_algs); /* For any tfm, requests for that tfm must be returned on the order * received. With multiple queues available, the CCP can process more @@ -343,6 +349,12 @@ static int ccp_register_algs(void) return ret; } + if (!rsa_disable) { + ret = ccp_register_rsa_algs(&akcipher_algs); + if (ret) + return ret; + } + return 0; } @@ -350,6 +362,7 @@ static void ccp_unregister_algs(void) { struct ccp_crypto_ahash_alg *ahash_alg, *ahash_tmp; struct ccp_crypto_ablkcipher_alg *ablk_alg, *ablk_tmp; + struct ccp_crypto_akcipher_alg *ak_alg, *ak_tmp; list_for_each_entry_safe(ahash_alg, ahash_tmp, &hash_algs, entry) { crypto_unregister_ahash(&ahash_alg->alg); @@ -362,6 +375,12 @@ static void ccp_unregister_algs(void) list_del(&ablk_alg->entry); kfree(ablk_alg); } + + list_for_each_entry_safe(ak_alg, ak_tmp, &akcipher_algs, entry) { + crypto_unregister_akcipher(&ak_alg->alg); + list_del(&ak_alg->entry); + kfree(ak_alg); + } } static int ccp_crypto_init(void) diff --git a/drivers/crypto/ccp/ccp-crypto-rsa.c b/drivers/crypto/ccp/ccp-crypto-rsa.c new file mode 100644 index 0000000..6cb6c6f --- /dev/null +++ b/drivers/crypto/ccp/ccp-crypto-rsa.c @@ -0,0 +1,294 @@ +/* + * AMD Cryptographic Coprocessor (CCP) RSA crypto API support + * + * Copyright (C) 2016 Advanced Micro Devices, Inc. + * + * Author: Gary R Hook + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "ccp-crypto.h" + +static inline struct akcipher_request *akcipher_request_cast( + struct crypto_async_request *req) +{ + return container_of(req, struct akcipher_request, base); +} + +static int ccp_rsa_complete(struct crypto_async_request *async_req, int ret) +{ + struct akcipher_request *req = akcipher_request_cast(async_req); + struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req); + + + if (!ret) + req->dst_len = rctx->cmd.u.rsa.mod_len; + + ret = 0; + + return ret; +} + +static int ccp_rsa_maxsize(struct crypto_akcipher *tfm) +{ + return CCP_RSA_MAXMOD; +} + +static int ccp_rsa_crypt(struct akcipher_request *req, bool encrypt) +{ + struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); + struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm); + struct ccp_rsa_req_ctx *rctx = akcipher_request_ctx(req); + int ret = 0; + + if (!ctx->u.rsa.pkey.d && !ctx->u.rsa.pkey.e) + return -EINVAL; + + memset(&rctx->cmd, 0, sizeof(rctx->cmd)); + INIT_LIST_HEAD(&rctx->cmd.entry); + rctx->cmd.engine = CCP_ENGINE_RSA; + + rctx->cmd.u.rsa.key_size = ctx->u.rsa.key_len; /* in bits */ + if (encrypt) { + rctx->cmd.u.rsa.exp = &ctx->u.rsa.e_sg; + rctx->cmd.u.rsa.exp_len = ctx->u.rsa.e_len; + } else { + rctx->cmd.u.rsa.exp = &ctx->u.rsa.d_sg; + rctx->cmd.u.rsa.exp_len = ctx->u.rsa.d_len; + } + rctx->cmd.u.rsa.mod = &ctx->u.rsa.n_sg; + rctx->cmd.u.rsa.mod_len = ctx->u.rsa.n_len; + + rctx->cmd.u.rsa.src = req->src; + rctx->cmd.u.rsa.src_len = req->src_len; + rctx->cmd.u.rsa.dst = req->dst; + + ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd); + + return ret; +} + +static int ccp_rsa_encrypt(struct akcipher_request *req) +{ + return ccp_rsa_crypt(req, true); +} + +static int ccp_rsa_decrypt(struct akcipher_request *req) +{ + return ccp_rsa_crypt(req, false); +} + +static int ccp_check_key_length(unsigned int len) +{ + /* In bits */ + if (len < 8 || len > 4096) + return -EINVAL; + return 0; +} + +static void ccp_rsa_free_key_bufs(struct ccp_ctx *ctx) +{ + /* Clean up old key data */ + kfree(ctx->u.rsa.e_buf); + ctx->u.rsa.e_buf = NULL; + ctx->u.rsa.e_len = 0; + kfree(ctx->u.rsa.n_buf); + ctx->u.rsa.n_buf = NULL; + ctx->u.rsa.n_len = 0; + kfree(ctx->u.rsa.d_buf); + ctx->u.rsa.d_buf = NULL; + ctx->u.rsa.d_len = 0; +} + +static int ccp_rsa_setkey(struct crypto_akcipher *tfm, const void *key, + unsigned int keylen, bool private) +{ + struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm); + struct rsa_key raw_key; + unsigned int n_size; + int ret; + + ccp_rsa_free_key_bufs(ctx); + memset(&raw_key, 0, sizeof(raw_key)); + + /* Code borrowed from crypto/rsa.c */ + 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; + + ret = -EINVAL; + + ctx->u.rsa.pkey.e = mpi_read_raw_data(raw_key.e, raw_key.e_sz); + if (!ctx->u.rsa.pkey.e) + goto e_ret; + ctx->u.rsa.e_buf = mpi_get_buffer(ctx->u.rsa.pkey.e, + &ctx->u.rsa.e_len, NULL); + if (!ctx->u.rsa.e_buf) + goto e_key; + sg_init_one(&ctx->u.rsa.e_sg, ctx->u.rsa.e_buf, ctx->u.rsa.e_len); + + ctx->u.rsa.pkey.n = mpi_read_raw_data(raw_key.n, raw_key.n_sz); + n_size = mpi_get_size(ctx->u.rsa.pkey.n) << 3; /* bytes to bits */ + if (ccp_check_key_length(n_size)) + goto e_nkey; + ctx->u.rsa.key_len = n_size; /* bits */ + ctx->u.rsa.n_buf = mpi_get_buffer(ctx->u.rsa.pkey.n, + &ctx->u.rsa.n_len, NULL); + if (!ctx->u.rsa.n_buf) + goto e_nkey; + sg_init_one(&ctx->u.rsa.n_sg, ctx->u.rsa.n_buf, ctx->u.rsa.n_len); + + 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_dkey; + ctx->u.rsa.d_buf = mpi_get_buffer(ctx->u.rsa.pkey.d, + &ctx->u.rsa.d_len, NULL); + if (!ctx->u.rsa.d_buf) + goto e_dkey; + sg_init_one(&ctx->u.rsa.d_sg, ctx->u.rsa.d_buf, + ctx->u.rsa.d_len); + } + + return 0; + +e_dkey: + kfree(ctx->u.rsa.n_buf); +e_nkey: + kfree(ctx->u.rsa.e_buf); +e_key: + rsa_free_mpi_key(&ctx->u.rsa.pkey); +e_ret: + return ret; +} + +static int ccp_rsa_setprivkey(struct crypto_akcipher *tfm, const void *key, + unsigned int keylen) +{ + 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, false); +} + +static int ccp_rsa_init_tfm(struct crypto_akcipher *tfm) +{ + struct ccp_ctx *ctx = akcipher_tfm_ctx(tfm); + + ctx->complete = ccp_rsa_complete; + + return 0; +} + +static void ccp_rsa_exit_tfm(struct crypto_akcipher *tfm) +{ + struct ccp_ctx *ctx = crypto_tfm_ctx(&tfm->base); + + ccp_rsa_free_key_bufs(ctx); +} + +static struct akcipher_alg ccp_rsa_defaults = { + .encrypt = ccp_rsa_encrypt, + .decrypt = ccp_rsa_decrypt, + .sign = NULL, + .verify = NULL, + .set_pub_key = ccp_rsa_setpubkey, + .set_priv_key = ccp_rsa_setprivkey, + .max_size = ccp_rsa_maxsize, + .init = ccp_rsa_init_tfm, + .exit = ccp_rsa_exit_tfm, + .base = { + .cra_name = "rsa", + .cra_driver_name = "rsa-ccp", + .cra_priority = CCP_CRA_PRIORITY, + .cra_module = THIS_MODULE, + .cra_ctxsize = sizeof(struct ccp_ctx), + }, +}; + +struct ccp_rsa_def { + unsigned int version; + const char *name; + const char *driver_name; + unsigned int reqsize; + struct akcipher_alg *alg_defaults; +}; + +static struct ccp_rsa_def rsa_algs[] = { + { + .version = CCP_VERSION(3, 0), + .name = "rsa", + .driver_name = "rsa-ccp", + .reqsize = sizeof(struct ccp_rsa_req_ctx), + .alg_defaults = &ccp_rsa_defaults, + } +}; + +int ccp_register_rsa_alg(struct list_head *head, const struct ccp_rsa_def *def) +{ + struct ccp_crypto_akcipher_alg *ccp_alg; + struct akcipher_alg *alg; + int ret; + + ccp_alg = kzalloc(sizeof(*ccp_alg), GFP_KERNEL); + if (!ccp_alg) + return -ENOMEM; + + INIT_LIST_HEAD(&ccp_alg->entry); + + alg = &ccp_alg->alg; + *alg = *def->alg_defaults; + snprintf(alg->base.cra_name, CRYPTO_MAX_ALG_NAME, "%s", def->name); + snprintf(alg->base.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s", + def->driver_name); + ret = crypto_register_akcipher(alg); + if (ret) { + pr_err("%s akcipher algorithm registration error (%d)\n", + alg->base.cra_name, ret); + kfree(ccp_alg); + return ret; + } + + list_add(&ccp_alg->entry, head); + + return 0; +} + +int ccp_register_rsa_algs(struct list_head *head) +{ + int i, ret; + unsigned int ccpversion = ccp_version(); + + /* Register the RSA algorithm in standard mode + * This works for CCP v3 and later + */ + for (i = 0; i < ARRAY_SIZE(rsa_algs); i++) { + if (rsa_algs[i].version > ccpversion) + continue; + ret = ccp_register_rsa_alg(head, &rsa_algs[i]); + if (ret) + return ret; + } + + return 0; +} diff --git a/drivers/crypto/ccp/ccp-crypto.h b/drivers/crypto/ccp/ccp-crypto.h index 95cce27..aa525e6 100644 --- a/drivers/crypto/ccp/ccp-crypto.h +++ b/drivers/crypto/ccp/ccp-crypto.h @@ -22,6 +22,8 @@ #include #include #include +#include +#include #define CCP_CRA_PRIORITY 300 @@ -46,6 +48,12 @@ struct ccp_crypto_ahash_alg { struct ahash_alg alg; }; +struct ccp_crypto_akcipher_alg { + struct list_head entry; + + struct akcipher_alg alg; +}; + static inline struct ccp_crypto_ablkcipher_alg * ccp_crypto_ablkcipher_alg(struct crypto_tfm *tfm) { @@ -194,12 +202,35 @@ struct ccp_sha_exp_ctx { u8 buf[MAX_SHA_BLOCK_SIZE]; }; +/***** RSA related defines *****/ + +struct ccp_rsa_ctx { + unsigned int key_len; /* in bits */ + struct rsa_mpi_key pkey; + struct scatterlist e_sg; + u8 *e_buf; + unsigned int e_len; + struct scatterlist n_sg; + u8 *n_buf; + unsigned int n_len; + struct scatterlist d_sg; + u8 *d_buf; + unsigned int d_len; +}; + +struct ccp_rsa_req_ctx { + struct ccp_cmd cmd; +}; + +#define CCP_RSA_MAXMOD (4 * 1024 / 8) + /***** Common Context Structure *****/ struct ccp_ctx { int (*complete)(struct crypto_async_request *req, int ret); union { struct ccp_aes_ctx aes; + struct ccp_rsa_ctx rsa; struct ccp_sha_ctx sha; } u; }; @@ -213,5 +244,6 @@ struct scatterlist *ccp_crypto_sg_table_add(struct sg_table *table, int ccp_register_aes_cmac_algs(struct list_head *head); int ccp_register_aes_xts_algs(struct list_head *head); int ccp_register_sha_algs(struct list_head *head); +int ccp_register_rsa_algs(struct list_head *head); #endif diff --git a/include/linux/ccp.h b/include/linux/ccp.h index 90a1fbe..dd90670 100644 --- a/include/linux/ccp.h +++ b/include/linux/ccp.h @@ -20,7 +20,6 @@ #include #include - struct ccp_device; struct ccp_cmd;