From patchwork Fri Mar 18 18:31:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tudor Ambarus X-Patchwork-Id: 8623201 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E2FA89F44D for ; Fri, 18 Mar 2016 18:48:18 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 72D7720306 for ; Fri, 18 Mar 2016 18:48:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id DD85C2022A for ; Fri, 18 Mar 2016 18:48:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756625AbcCRSsO (ORCPT ); Fri, 18 Mar 2016 14:48:14 -0400 Received: from mail-bn1on0070.outbound.protection.outlook.com ([157.56.110.70]:23796 "EHLO na01-bn1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754987AbcCRSsN (ORCPT ); Fri, 18 Mar 2016 14:48:13 -0400 X-Greylist: delayed 956 seconds by postgrey-1.27 at vger.kernel.org; Fri, 18 Mar 2016 14:48:12 EDT Received: from DM2PR03CA0042.namprd03.prod.outlook.com (10.141.96.41) by SN1PR0301MB1597.namprd03.prod.outlook.com (10.162.130.19) with Microsoft SMTP Server (TLS) id 15.1.443.7; Fri, 18 Mar 2016 18:32:14 +0000 Received: from BL2FFO11FD007.protection.gbl (2a01:111:f400:7c09::141) by DM2PR03CA0042.outlook.office365.com (2a01:111:e400:2428::41) with Microsoft SMTP Server (TLS) id 15.1.443.12 via Frontend Transport; Fri, 18 Mar 2016 18:32:14 +0000 Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=nxp.com; nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=none action=none header.from=nxp.com; Received-SPF: Fail (protection.outlook.com: domain of nxp.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; Received: from az84smr01.freescale.net (192.88.158.2) by BL2FFO11FD007.mail.protection.outlook.com (10.173.161.3) with Microsoft SMTP Server (TLS) id 15.1.443.6 via Frontend Transport; Fri, 18 Mar 2016 18:32:14 +0000 Received: from enigma.ea.freescale.net (enigma.ea.freescale.net [10.171.77.120]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id u2IIWAmK015418; Fri, 18 Mar 2016 11:32:11 -0700 From: Tudor Ambarus To: , CC: , , , Tudor Ambarus Subject: [PATCH 01/10] crypto: rsa - generalize ASN.1 sequences Date: Fri, 18 Mar 2016 20:31:58 +0200 Message-ID: <1458325927-14737-1-git-send-email-tudor-dan.ambarus@nxp.com> X-Mailer: git-send-email 1.8.3.1 X-EOPAttributedMessage: 0 X-Matching-Connectors: 131027995342364937; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.158.2; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(1109001)(1110001)(339900001)(199003)(189002)(4326007)(47776003)(33646002)(87936001)(50986999)(2906002)(5001770100001)(50226001)(5008740100001)(6806005)(19580395003)(189998001)(105606002)(106466001)(36756003)(85426001)(92566002)(19580405001)(5003940100001)(104016004)(575784001)(586003)(77096005)(11100500001)(86362001)(229853001)(48376002)(50466002)(81166005)(1220700001)(1096002)(7059030)(4720700001); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1PR0301MB1597; H:az84smr01.freescale.net; FPR:; SPF:Fail; MLV:sfv; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD007; 1:1CXYkN/L3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4vji7IG8N8SyZfUGQ8o2NHA8SmrD3/Gb3B+mqvt1zdid2J3agPyCa9+RTmzVLeJnT23tPiu3UtznIpaurgaebXyK8BFs6/p8szNZNCrPGO/QasqNVpDWK7OTNAEmBI+kvaY/nVIvSWRm2d8= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:SN1PR0301MB1597; X-Microsoft-Exchange-Diagnostics: 1; SN1PR0301MB1597; 25:I710oj8tOX73G6uHPX6d8BXzFEB0bLxF6rd8Ho5oKWt9PCwPD7b27ixSNSFaDLf/AUTItLnY3yVDIPWUuDsqd6HCwIstk95fUJMvc3GitrOz+ljXmO7rd/tnI84NdGx8MjJwbfYhdLNPJWxi2h7cuJ/1qnpscfzzbS7ffeeT8djC46qWglhQFTElpgRt/NSgZeOJeRH4NzTaIN+rTJtGWCSeFb2320P1RHmZuCjlrq72ctq9KsVEAk/wUiHww2HxCsJwByU3ixFhHIVTDAeMeMuDLIqo4qF3RofIPBYNHR5lLM/JbEHzfbHELsSskNOp8K61CjQgvdZ8Dmuw2qellGYdoZualREZZFoKj/AxS6UxNOVFbPPLo/So1F6YtfDOO8WZF2etpS5+y7AntLbcPz6QdQ1Nd34NKtuQqwm1kjRoq5k8XS1YDrB6r2HG+wJn7Jf5e/EMwpgZy6blS5+oQl7/FcuzTISAnYZVffAFQDDln3AKyxwN8Y8Dfjq06uCiHXceGWEaYlx23nEwTlbYTKepJIdhYwTkWCd69Pc9OFL6wd9uNmn4K6rG+OHBwyvb X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13024025)(5005006)(13018025)(13017025)(13023025)(8121501046)(13015025)(3002001)(10201501046); SRVR:SN1PR0301MB1597; BCL:0; PCL:0; RULEID:(400006); SRVR:SN1PR0301MB1597; X-Microsoft-Exchange-Diagnostics: 1; SN1PR0301MB1597; 4:2xACqVFSrHsJBQg8ltXxE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= =?us-ascii?Q?3Hwc7B1Lz+PYnO4/IiASYIP/T8x32Kg7Ww+3n9yzAiEmakCygnRWwS1i/ux8?= =?us-ascii?Q?5upKaZnWr6gM8cJRju2OPaJw0wroScXdBAic+jiNugg5OUxPQ67oIAVHsnug?= =?us-ascii?Q?wI5nTTo+FRXQbFJfwEacxG+vpy1tSs5lTiELMvF8JfIvqX7fpzvM2XPRhOe1?= =?us-ascii?Q?OYMSnZt867DHEQOt7HTpuBB0z8wkQyVbmu0q7U0mBGgVY8TDZfjCmSkBDnSq?= =?us-ascii?Q?vrAu5V7VqL0JyBFYAw+TPAmvdMsu8E+Uqhy4cerN7t09Bx81O5pOxxwFHZ79?= =?us-ascii?Q?mflV0IRsjtKRzauToY/PG9QKd8wCiwaj2c3y26/QNXSh7vEki+rvl7ngcI3+?= =?us-ascii?Q?KbRkJmsCk21EccGbDpmzUK6eN5nPIYi6f7dbvIcysJgz4Iok0jgX2azZ5iZt?= =?us-ascii?Q?KTyHf2zXqLisE6GrBI1K+LT13lpoSpFHEBTuYhoPOkadfez5HXLvYZdDJACd?= =?us-ascii?Q?lSPfkPKsLYMeqj0dn8/0xxrxE23x9/vNv91EbkdriT+fTDa/6kLBv/kwSLIG?= =?us-ascii?Q?O3dHf8EAeY3nmOhA+VTypJQ3XTAT9NPv9y7e/UEnUiVaC/sf9HcagA8Nm7PW?= =?us-ascii?Q?CwirWAr0=3D?= X-Microsoft-Exchange-Diagnostics: 1; SN1PR0301MB1597; 5:Csy381XG8/9vhb6/EmrJFJct1E1YkL+k06vCvhak/raY72f/az2T7b9EtqtNtvJuOqW28UVG3gohrmkJVmlm8rJNQlmeGWrlxW9zl0zW8/Xarci7USNT6YPQCcBvk/lZwRgD+ny4yTaMiqzz6kqTfwZAReGhUdB3wQSNAzoje0w=; 24:a32d46sh/vwvQw3hgXz+AF2atJg812RWHXFlqPeuUYSkxvoMGCihQkE7aYUj0s+weYtn7sW7bYRoDO/y60eFWP54CFpQPAiOIttHOyVUhEk= SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Mar 2016 18:32:14.0025 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.158.2]; Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR0301MB1597 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Use common ASN.1 sequences for all RSA implementations. Give hardware RSA implementations the chance to use the RSA's software implementation parser even if they are likely to want to use raw integers. The parser expects a context that contains at the first address a struct rsa_asn1_action with function pointers to specific parser actions (return MPI or raw integer keys), followed by a key representation structure (for MPI or raw integers). This approach has the advantage that users can select specific parser actions by using a general parser with function pointers to specific actions. Signed-off-by: Tudor Ambarus --- crypto/rsa.c | 48 +++++++++-------- crypto/rsa_helper.c | 117 +++++++++++++++++++++++++++++------------- include/crypto/internal/rsa.h | 28 +++++++--- 3 files changed, 130 insertions(+), 63 deletions(-) diff --git a/crypto/rsa.c b/crypto/rsa.c index 77d737f..2d53ad8 100644 --- a/crypto/rsa.c +++ b/crypto/rsa.c @@ -19,7 +19,7 @@ * RSAEP function [RFC3447 sec 5.1.1] * c = m^e mod n; */ -static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m) +static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m) { /* (1) Validate 0 <= m < n */ if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) @@ -33,7 +33,7 @@ static int _rsa_enc(const struct rsa_key *key, MPI c, MPI m) * RSADP function [RFC3447 sec 5.1.2] * m = c^d mod n; */ -static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c) +static int _rsa_dec(const struct rsa_mpi_key *key, MPI m, MPI c) { /* (1) Validate 0 <= c < n */ if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0) @@ -47,7 +47,7 @@ static int _rsa_dec(const struct rsa_key *key, MPI m, MPI c) * RSASP1 function [RFC3447 sec 5.2.1] * s = m^d mod n */ -static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m) +static int _rsa_sign(const struct rsa_mpi_key *key, MPI s, MPI m) { /* (1) Validate 0 <= m < n */ if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) @@ -61,7 +61,7 @@ static int _rsa_sign(const struct rsa_key *key, MPI s, MPI m) * RSAVP1 function [RFC3447 sec 5.2.2] * m = s^e mod n; */ -static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s) +static int _rsa_verify(const struct rsa_mpi_key *key, MPI m, MPI s) { /* (1) Validate 0 <= s < n */ if (mpi_cmp_ui(s, 0) < 0 || mpi_cmp(s, key->n) >= 0) @@ -71,15 +71,17 @@ static int _rsa_verify(const struct rsa_key *key, MPI m, MPI s) return mpi_powm(m, s, key->e, key->n); } -static inline struct rsa_key *rsa_get_key(struct crypto_akcipher *tfm) +static inline struct rsa_mpi_key *rsa_get_key(struct crypto_akcipher *tfm) { - return akcipher_tfm_ctx(tfm); + struct rsa_ctx *ctx = akcipher_tfm_ctx(tfm); + + return &ctx->key; } static int rsa_enc(struct akcipher_request *req) { struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); - const struct rsa_key *pkey = rsa_get_key(tfm); + const struct rsa_mpi_key *pkey = rsa_get_key(tfm); MPI m, c = mpi_alloc(0); int ret = 0; int sign; @@ -118,7 +120,7 @@ err_free_c: static int rsa_dec(struct akcipher_request *req) { struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); - const struct rsa_key *pkey = rsa_get_key(tfm); + const struct rsa_mpi_key *pkey = rsa_get_key(tfm); MPI c, m = mpi_alloc(0); int ret = 0; int sign; @@ -156,7 +158,7 @@ err_free_m: static int rsa_sign(struct akcipher_request *req) { struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); - const struct rsa_key *pkey = rsa_get_key(tfm); + const struct rsa_mpi_key *pkey = rsa_get_key(tfm); MPI m, s = mpi_alloc(0); int ret = 0; int sign; @@ -195,7 +197,7 @@ err_free_s: static int rsa_verify(struct akcipher_request *req) { struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); - const struct rsa_key *pkey = rsa_get_key(tfm); + const struct rsa_mpi_key *pkey = rsa_get_key(tfm); MPI s, m = mpi_alloc(0); int ret = 0; int sign; @@ -251,15 +253,18 @@ static int rsa_check_key_length(unsigned int len) static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) { - struct rsa_key *pkey = akcipher_tfm_ctx(tfm); + struct rsa_ctx *ctx = akcipher_tfm_ctx(tfm); + struct rsa_mpi_key *pkey = &ctx->key; int ret; - ret = rsa_parse_pub_key(pkey, key, keylen); + set_rsa_pub_action(&ctx->action); + + ret = rsa_parse_mpi_pub_key(ctx, key, keylen); if (ret) return ret; if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { - rsa_free_key(pkey); + rsa_free_mpi_key(pkey); ret = -EINVAL; } return ret; @@ -268,15 +273,18 @@ static int rsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) { - struct rsa_key *pkey = akcipher_tfm_ctx(tfm); + struct rsa_ctx *ctx = akcipher_tfm_ctx(tfm); + struct rsa_mpi_key *pkey = &ctx->key; int ret; - ret = rsa_parse_priv_key(pkey, key, keylen); + set_rsa_priv_action(&ctx->action); + + ret = rsa_parse_mpi_priv_key(ctx, key, keylen); if (ret) return ret; if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { - rsa_free_key(pkey); + rsa_free_mpi_key(pkey); ret = -EINVAL; } return ret; @@ -284,16 +292,16 @@ static int rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key, static int rsa_max_size(struct crypto_akcipher *tfm) { - struct rsa_key *pkey = akcipher_tfm_ctx(tfm); + struct rsa_mpi_key *pkey = rsa_get_key(tfm); return pkey->n ? mpi_get_size(pkey->n) : -EINVAL; } static void rsa_exit_tfm(struct crypto_akcipher *tfm) { - struct rsa_key *pkey = akcipher_tfm_ctx(tfm); + struct rsa_mpi_key *pkey = rsa_get_key(tfm); - rsa_free_key(pkey); + rsa_free_mpi_key(pkey); } static struct akcipher_alg rsa = { @@ -310,7 +318,7 @@ static struct akcipher_alg rsa = { .cra_driver_name = "rsa-generic", .cra_priority = 100, .cra_module = THIS_MODULE, - .cra_ctxsize = sizeof(struct rsa_key), + .cra_ctxsize = sizeof(struct rsa_ctx), }, }; diff --git a/crypto/rsa_helper.c b/crypto/rsa_helper.c index d226f48..1ed32af 100644 --- a/crypto/rsa_helper.c +++ b/crypto/rsa_helper.c @@ -21,7 +21,32 @@ int rsa_get_n(void *context, size_t hdrlen, unsigned char tag, const void *value, size_t vlen) { - struct rsa_key *key = context; + struct rsa_asn1_action *action = (struct rsa_asn1_action *)context; + + return action->get_n(context, hdrlen, tag, value, vlen); +} + +int rsa_get_e(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen) +{ + struct rsa_asn1_action *action = (struct rsa_asn1_action *)context; + + return action->get_e(context, hdrlen, tag, value, vlen); +} + +int rsa_get_d(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen) +{ + struct rsa_asn1_action *action = (struct rsa_asn1_action *)context; + + return action->get_d(context, hdrlen, tag, value, vlen); +} + +int rsa_get_mpi_n(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen) +{ + struct rsa_ctx *ctx = context; + struct rsa_mpi_key *key = &ctx->key; key->n = mpi_read_raw_data(value, vlen); @@ -39,10 +64,11 @@ int rsa_get_n(void *context, size_t hdrlen, unsigned char tag, return 0; } -int rsa_get_e(void *context, size_t hdrlen, unsigned char tag, - const void *value, size_t vlen) +int rsa_get_mpi_e(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen) { - struct rsa_key *key = context; + struct rsa_ctx *ctx = context; + struct rsa_mpi_key *key = &ctx->key; key->e = mpi_read_raw_data(value, vlen); @@ -52,10 +78,11 @@ int rsa_get_e(void *context, size_t hdrlen, unsigned char tag, return 0; } -int rsa_get_d(void *context, size_t hdrlen, unsigned char tag, - const void *value, size_t vlen) +int rsa_get_mpi_d(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen) { - struct rsa_key *key = context; + struct rsa_ctx *ctx = context; + struct rsa_mpi_key *key = &ctx->key; key->d = mpi_read_raw_data(value, vlen); @@ -73,7 +100,12 @@ int rsa_get_d(void *context, size_t hdrlen, unsigned char tag, return 0; } -static void free_mpis(struct rsa_key *key) +/** + * rsa_free_mpi_key() - frees rsa key allocated by rsa_parse_key() + * + * @rsa_mpi_key: struct rsa_mpi_key key representation + */ +void rsa_free_mpi_key(struct rsa_mpi_key *key) { mpi_free(key->n); mpi_free(key->e); @@ -82,68 +114,79 @@ static void free_mpis(struct rsa_key *key) key->e = NULL; key->d = NULL; } +EXPORT_SYMBOL_GPL(rsa_free_mpi_key); /** - * rsa_free_key() - frees rsa key allocated by rsa_parse_key() - * - * @rsa_key: struct rsa_key key representation - */ -void rsa_free_key(struct rsa_key *key) -{ - free_mpis(key); -} -EXPORT_SYMBOL_GPL(rsa_free_key); - -/** - * rsa_parse_pub_key() - extracts an rsa public key from BER encoded buffer - * and stores it in the provided struct rsa_key + * rsa_parse_mpi_pub_key() - extracts an RSA public key from BER encoded buffer + * and stores it in the provided context. * - * @rsa_key: struct rsa_key key representation + * @rsa_ctx: RSA internal context * @key: key in BER format * @key_len: length of key * * Return: 0 on success or error code in case of error */ -int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key, - unsigned int key_len) +int rsa_parse_mpi_pub_key(struct rsa_ctx *ctx, const void *key, + unsigned int key_len) { + struct rsa_mpi_key *rsa_key = &ctx->key; int ret; - free_mpis(rsa_key); - ret = asn1_ber_decoder(&rsapubkey_decoder, rsa_key, key, key_len); + /* Free the old key if any */ + rsa_free_mpi_key(rsa_key); + + ret = asn1_ber_decoder(&rsapubkey_decoder, ctx, key, key_len); if (ret < 0) goto error; return 0; error: - free_mpis(rsa_key); + rsa_free_mpi_key(rsa_key); return ret; } -EXPORT_SYMBOL_GPL(rsa_parse_pub_key); +EXPORT_SYMBOL_GPL(rsa_parse_mpi_pub_key); /** - * rsa_parse_pub_key() - extracts an rsa private key from BER encoded buffer - * and stores it in the provided struct rsa_key + * rsa_parse_mpi_priv_key() - extracts an RSA private key from BER encoded + * buffer and stores it in the provided context. * - * @rsa_key: struct rsa_key key representation + * @rsa_ctx: RSA internal context * @key: key in BER format * @key_len: length of key * * Return: 0 on success or error code in case of error */ -int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key, - unsigned int key_len) +int rsa_parse_mpi_priv_key(struct rsa_ctx *ctx, const void *key, + unsigned int key_len) { + struct rsa_mpi_key *rsa_key = &ctx->key; int ret; - free_mpis(rsa_key); - ret = asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len); + /* Free the old key if any */ + rsa_free_mpi_key(rsa_key); + + ret = asn1_ber_decoder(&rsaprivkey_decoder, ctx, key, key_len); if (ret < 0) goto error; return 0; error: - free_mpis(rsa_key); + rsa_free_mpi_key(rsa_key); return ret; } -EXPORT_SYMBOL_GPL(rsa_parse_priv_key); +EXPORT_SYMBOL_GPL(rsa_parse_mpi_priv_key); + +void set_rsa_pub_action(struct rsa_asn1_action *action) +{ + action->get_e = rsa_get_mpi_e; + action->get_n = rsa_get_mpi_n; +} +EXPORT_SYMBOL_GPL(set_rsa_pub_action); + +void set_rsa_priv_action(struct rsa_asn1_action *action) +{ + action->get_d = rsa_get_mpi_d; + action->get_e = rsa_get_mpi_e; + action->get_n = rsa_get_mpi_n; +} +EXPORT_SYMBOL_GPL(set_rsa_priv_action); diff --git a/include/crypto/internal/rsa.h b/include/crypto/internal/rsa.h index c7585bd..bf0f49d 100644 --- a/include/crypto/internal/rsa.h +++ b/include/crypto/internal/rsa.h @@ -14,19 +14,35 @@ #define _RSA_HELPER_ #include -struct rsa_key { +struct rsa_mpi_key { MPI n; MPI e; MPI d; }; -int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key, - unsigned int key_len); +struct rsa_asn1_action { + int (*get_n)(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen); + int (*get_e)(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen); + int (*get_d)(void *context, size_t hdrlen, unsigned char tag, + const void *value, size_t vlen); +}; + +struct rsa_ctx { + struct rsa_asn1_action action; + struct rsa_mpi_key key; +}; + +void rsa_free_mpi_key(struct rsa_mpi_key *key); -int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key, - unsigned int key_len); +int rsa_parse_mpi_pub_key(struct rsa_ctx *ctx, const void *key, + unsigned int key_len); +int rsa_parse_mpi_priv_key(struct rsa_ctx *ctx, const void *key, + unsigned int key_len); -void rsa_free_key(struct rsa_key *rsa_key); +void set_rsa_pub_action(struct rsa_asn1_action *action); +void set_rsa_priv_action(struct rsa_asn1_action *action); extern struct crypto_template rsa_pkcs1pad_tmpl; #endif