From patchwork Thu Apr 8 14:15:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Varad Gautam X-Patchwork-Id: 418314 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63314C433B4 for ; Thu, 8 Apr 2021 14:16:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3333461154 for ; Thu, 8 Apr 2021 14:16:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232033AbhDHORH (ORCPT ); Thu, 8 Apr 2021 10:17:07 -0400 Received: from de-smtp-delivery-102.mimecast.com ([194.104.109.102]:25127 "EHLO de-smtp-delivery-102.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231980AbhDHORA (ORCPT ); Thu, 8 Apr 2021 10:17:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1617891408; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yajnkEc27ntpQX9y5f1agIELyl3+Xvf1Su9bs6qert0=; b=IYWwnsTaYd7Ef/ZObP4sVrEKQv5rNeCHaE3u3G6ie3JIB2bxFv/0UO0VWTIqAHJn2kT3HJ uhx/nSCuX734A+GZw4Un+SwUiEV5DbRyL5l1JAXEEpY5plzGEMouzwbEbxPlqw/aPJPc17 YzD9yMDprzh5D3VCpusA1osZErX2zXY= Received: from EUR04-HE1-obe.outbound.protection.outlook.com (mail-he1eur04lp2057.outbound.protection.outlook.com [104.47.13.57]) (Using TLS) by relay.mimecast.com with ESMTP id de-mta-27-4bJ2o2H5PpiXblB8gvqBow-4; Thu, 08 Apr 2021 16:16:26 +0200 X-MC-Unique: 4bJ2o2H5PpiXblB8gvqBow-4 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PKTcxT4u4m+BC6xbGkMUAmrKX1YcEvKVtTEnLUO7x6mpYdgZSxK700x/1MPUHiNOTO1CJkJRx9ARAdV2eOxAtPNcqYPqtdRoeEAqGZxaLCQ/rmSqCL5JmUoePKQWpzj4gzFRZbKQvsXCV19QcB4AfBifPRP0rPtNwmeSxxmknG2VTViXChPszzpWfuTVsLoPyPOLCYLpMYzHutTgGVrFnk/l90Vq1HmvGAy2gdiOpfuPYswbrFRfw47cBXuGXjzrOknWvZilEFilXzL6VoV9/1HkOkewyVqnV3WQDXv6RFP6mFfSND/BTRbqceBsHX46O+Vshhw2p2yzXsGcFvVlAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=7T9VlDQvS45hhOSp2ljrxv0PI2XxklWAE2faxm4TbDk=; b=jNSSu+uYq5PfXijKdlIgW4sqsJRFaQz1GahRK/LPffxMtxUbnj42jP8LyIO8gMTkbdzcYNxhnudX0u5eimTf9/C4tGZ6zJsLIi3N/781LlR3LxhMUKD07zc5zIW4W266oqwcgo7Dl5bXZ7+aGqDe0mECEfoInebdWUn1XNq7O9wjD99qCk/fMgRIeXtv9miYdfVi5ZWQcbw9HTlXjHOSpnlkbHLNwFj/WIZQseXeuWxioBgRgeJVGlr/eFeYq80I2c0FrM+GWuqwl8SHR2LhLXwkdI2ktfJs2+d7vvSgWW73cWoWBTpjpP/EO05U7YAJZHtLG2yOyX2LuTiaCk3a5A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none; vger.kernel.org; dmarc=none action=none header.from=suse.com; Received: from AM0PR04MB5650.eurprd04.prod.outlook.com (2603:10a6:208:128::18) by AM0PR04MB6289.eurprd04.prod.outlook.com (2603:10a6:208:145::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4020.18; Thu, 8 Apr 2021 14:16:05 +0000 Received: from AM0PR04MB5650.eurprd04.prod.outlook.com ([fe80::756a:86b8:8283:733d]) by AM0PR04MB5650.eurprd04.prod.outlook.com ([fe80::756a:86b8:8283:733d%6]) with mapi id 15.20.3999.032; Thu, 8 Apr 2021 14:16:05 +0000 From: Varad Gautam To: linux-crypto@vger.kernel.org CC: varad.gautam@suse.com, dhowells@redhat.com, herbert@gondor.apana.org.au, davem@davemloft.net, vt@altlinux.org, tianjia.zhang@linux.alibaba.com, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, jarkko@kernel.org Subject: [PATCH v2 07/18] crypto: rsa: Move more common code to rsa-common Date: Thu, 8 Apr 2021 16:15:05 +0200 Message-ID: <20210408141516.11369-8-varad.gautam@suse.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210408141516.11369-1-varad.gautam@suse.com> References: <20210408141516.11369-1-varad.gautam@suse.com> X-Originating-IP: [95.90.93.216] X-ClientProxiedBy: ZR0P278CA0137.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:40::16) To AM0PR04MB5650.eurprd04.prod.outlook.com (2603:10a6:208:128::18) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from xps13.suse.cz (95.90.93.216) by ZR0P278CA0137.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:40::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4020.16 via Frontend Transport; Thu, 8 Apr 2021 14:16:04 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 562b625c-9a30-49ea-500a-08d8fa98d8a4 X-MS-TrafficTypeDiagnostic: AM0PR04MB6289: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:252; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: irSJHN+OYLOOsdHGoMiRHJ15zQt89a3DMHxqPOVepuObKhEVzgJ16XxanIcfLhKeMDFUFkoiG/SLo27P2E7IyEj91C5GkqfOhG21rVVk0afbIPhPmfp0VH+B8vcnMo8ssHxcHai9ttQat2o0RoVjrPKV3etpnWRIw24VsszCyIAIU3/jewauqgd5xV23oqbzRIm/vvoWQr8V6Z0QBblUSIDlyn2Mm4LXNH2CfwJAwtn7bkbknyCIBri70htj2+TNzHMcsYlo7dEPsWtYNcPckOmrHdUsFfjhO9GRfVi5xeIp2TntQKRFWYW5njSOiQZGmhVR5PpdVH9ULFQ/B9W1GYU5Pxdp4RVSI/aYmtmzPIvNdKYZ9rNgornACJgjFVxSB0m5d0CxXNjilqONdIT6qoKwqpc+7yg4q22O2d2Dv7VcSnIQ3ua0H5a48up7A1lYng4PXN2dMNxvcKgUtnWb2E5mOVWm1APbe6llDyZNAKgE7clTB9IFiTJYJhlmNgda6XEMb0nGgl+MmSC/UauMBLDlg3iOPrUZFA8DdaReyE4E/vM3E2bfNGEXWUriInpm0qVVwbM6bhN/3HsKjiCYpd/DcDj4JP3K85N08BaEN7V5fl1X8y/PEypaL/wFZvz2lqc9FQ+j77x6+xblI0ZFxbVH/n5zWd6kzQY0WtJ4+0Ff21cTYYnoMthV9J7qS/jX X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM0PR04MB5650.eurprd04.prod.outlook.com; PTR:; CAT:NONE; SFS:(366004)(396003)(346002)(376002)(136003)(39860400002)(186003)(83380400001)(16526019)(478600001)(6486002)(38100700001)(36756003)(30864003)(5660300002)(4326008)(6916009)(1076003)(66476007)(316002)(2906002)(66556008)(26005)(38350700001)(2616005)(956004)(8676002)(44832011)(8936002)(6506007)(86362001)(52116002)(66946007)(6512007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: bcSEJPJ1UANNX2hdaJU4P4GgjY1nklfla/xwO5isIEwv37Dm8kXUgK7JrZJtwtwoTeSni+8jwRgTabgcBEmL9ZYemlJso28UeWZt4tFV+ToyhBib2qHLs8WXE/Yt9cNEknb5DyBvooX4aMy1/OlunNeKaXDrIrhQdNtquFQ9HYtt/UACcCVXaWHlVDfUnCNNuRokbI2irjKLGoarlf/YcldCd/oVQa/7bOSMJiXWlupgtWiZ/bgTBJViMWTkErX2uEYIvdcdf2AChBOQ1lAnjJjgtPyJPL39bozGJDZNF3jGyn4uQ4+BEvCys6zxyJorNTOB7V1WV7esudK2Wq7JxwtPPUJZsIVrGO/X8kYGHlg/9cHX9jfBOQePw20tRC+P64zg9z8jOLRIxG0qCFD0Y/TM5dbjy+UsWKpRzwXACUpnG7U+PuxsP5R20An0mKKVMVsKwMaq1gV6JyAK4cVqODbEOSJZQ2AluauBLAPOOJi6r1IHNmVkBUZjINRctbYJelt3G5q0I17YqBtXZtBLuYd+KsHTkX0lLRZJG5mbaub3WKevm7IIV2U0GxUeuhVBNL2EQ4jEaYVcNTbTSvE6T0f2Vq203v9yJh1ueTNkElvkraLA5mWM4S+LDGwe0jb0GgWRX40QmkrNryH/2cnG8A35Etp0dEtSJzWK6eh+aIhKKbsGzDJID2WTB1LhTUD7DxQ+Mri9FKtXc2E8WlUpbZLDzL35gSppcDHgEegV/wQA9mC8jaVwj6RhZnSDR/jKPxISNt8eqOAUBW6d77XYjC6bCUG9GSndQH8W40M2JaaklJOd4BdM094rPIClkEknp9oi3+etQ8fxnk9sxS+KO2skTrS+MIkyR66f9avbZplQkYJlE1pmv/fq+yt2eSFPkXYCXlzCpdYzeFZTmxHapwDtypli46a7aAk8+T4sSwyPBwmYiCe1gRRc3qUokzRiE4j8bb455+l7z+nWXGEyD+W13EymHc96yW8397a9kkQLJqKlzmYV5nD7b8FUvp78q1gMaPV3GXE5FPx8Jcr83b4cZnvVPehTG80CVbt9EzD1HDxdLeKs38iPf1VExjqgZi6Hk2qkcGW/laV9a0JOhKhnMokVWPRO2neJx2HNxXodJV0E8IlpNQCjWlorYyJeXXY5i89+kYxPw/KoGoewT0YwXyqdZI09SAkxsdOUeRu/GE1hiiHihW/GSeHMNsAuIca9uqc9xgSe1/2KCTIh7q2d27jYlX8Q1DdRw3H+bBoLyKfqDfaEH2qqu+ZG61XmAHYXryrDVhwx4n69g9sBf+RAywL4JmG7S33UMiw+zOMIveHxh7yzZd3W5Sjbn1jL X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 562b625c-9a30-49ea-500a-08d8fa98d8a4 X-MS-Exchange-CrossTenant-AuthSource: AM0PR04MB5650.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Apr 2021 14:16:05.1344 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: KSOZCnNVLL0eeb0P29Odu6ii+DZznAThTrQAj8iMX5+4bwel7YanwSSRzX+qJYC5KgskyP+NkM2dkmkm8m1ekQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB6289 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Move helpers for setting public/private keys, RSA akcipher instance setup, keysize querying etc. to rsa-common.c. Signed-off-by: Varad Gautam --- crypto/rsa-common.c | 183 +++++++++++++++++++++++++++ crypto/rsa-pkcs1pad.c | 183 --------------------------- include/crypto/internal/rsa-common.h | 13 ++ 3 files changed, 196 insertions(+), 183 deletions(-) diff --git a/crypto/rsa-common.c b/crypto/rsa-common.c index 60073c56e3748..d70d7d405165f 100644 --- a/crypto/rsa-common.c +++ b/crypto/rsa-common.c @@ -74,3 +74,186 @@ const struct rsa_asn1_template *rsa_lookup_asn1(const char *name) return p; return NULL; } + +int rsapad_set_pub_key(struct crypto_akcipher *tfm, const void *key, + unsigned int keylen) +{ + struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); + int err; + + ctx->key_size = 0; + + err = crypto_akcipher_set_pub_key(ctx->child, key, keylen); + if (err) + return err; + + /* Find out new modulus size from rsa implementation */ + err = crypto_akcipher_maxsize(ctx->child); + if (err > PAGE_SIZE) + return -EOPNOTSUPP; + + ctx->key_size = err; + return 0; +} + +int rsapad_set_priv_key(struct crypto_akcipher *tfm, const void *key, + unsigned int keylen) +{ + struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); + int err; + + ctx->key_size = 0; + + err = crypto_akcipher_set_priv_key(ctx->child, key, keylen); + if (err) + return err; + + /* Find out new modulus size from rsa implementation */ + err = crypto_akcipher_maxsize(ctx->child); + if (err > PAGE_SIZE) + return -EOPNOTSUPP; + + ctx->key_size = err; + return 0; +} + +unsigned int rsapad_get_max_size(struct crypto_akcipher *tfm) +{ + struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); + + /* + * The maximum destination buffer size for the encrypt/sign operations + * will be the same as for RSA, even though it's smaller for + * decrypt/verify. + */ + + return ctx->key_size; +} + +void rsapad_akcipher_sg_set_buf(struct scatterlist *sg, void *buf, + size_t len, struct scatterlist *next) +{ + int nsegs = next ? 2 : 1; + + sg_init_table(sg, nsegs); + sg_set_buf(sg, buf, len); + + if (next) + sg_chain(sg, nsegs, next); +} + +int rsapad_akcipher_init_tfm(struct crypto_akcipher *tfm) +{ + struct akcipher_instance *inst = akcipher_alg_instance(tfm); + struct rsapad_inst_ctx *ictx = akcipher_instance_ctx(inst); + struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); + struct crypto_akcipher *child_tfm; + + child_tfm = crypto_spawn_akcipher(&ictx->spawn); + if (IS_ERR(child_tfm)) + return PTR_ERR(child_tfm); + + ctx->child = child_tfm; + return 0; +} + +void rsapad_akcipher_exit_tfm(struct crypto_akcipher *tfm) +{ + struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); + + crypto_free_akcipher(ctx->child); +} + +void rsapad_akcipher_free(struct akcipher_instance *inst) +{ + struct rsapad_inst_ctx *ctx = akcipher_instance_ctx(inst); + struct crypto_akcipher_spawn *spawn = &ctx->spawn; + + crypto_drop_akcipher(spawn); + kfree(inst); +} + +int rsapad_akcipher_create(struct crypto_template *tmpl, struct rtattr **tb, + struct akcipher_alg *alg) +{ + u32 mask; + struct akcipher_instance *inst; + struct rsapad_inst_ctx *ctx; + struct akcipher_alg *rsa_alg; + const char *hash_name; + int err; + + err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AKCIPHER, &mask); + if (err) + return err; + + inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); + if (!inst) + return -ENOMEM; + + ctx = akcipher_instance_ctx(inst); + + err = crypto_grab_akcipher(&ctx->spawn, akcipher_crypto_instance(inst), + crypto_attr_alg_name(tb[1]), 0, mask); + if (err) + goto err_free_inst; + + rsa_alg = crypto_spawn_akcipher_alg(&ctx->spawn); + + err = -ENAMETOOLONG; + hash_name = crypto_attr_alg_name(tb[2]); + if (IS_ERR(hash_name)) { + if (snprintf(inst->alg.base.cra_name, + CRYPTO_MAX_ALG_NAME, "%s(%s)", tmpl->name, + rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME) + goto err_free_inst; + + if (snprintf(inst->alg.base.cra_driver_name, + CRYPTO_MAX_ALG_NAME, "%s(%s)", tmpl->name, + rsa_alg->base.cra_driver_name) >= + CRYPTO_MAX_ALG_NAME) + goto err_free_inst; + } else { + ctx->digest_info = rsa_lookup_asn1(hash_name); + if (!ctx->digest_info) { + err = -EINVAL; + goto err_free_inst; + } + + if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, + "%s(%s,%s)", tmpl->name, rsa_alg->base.cra_name, + hash_name) >= CRYPTO_MAX_ALG_NAME) + goto err_free_inst; + + if (snprintf(inst->alg.base.cra_driver_name, + CRYPTO_MAX_ALG_NAME, "%s(%s,%s)", + tmpl->name, + rsa_alg->base.cra_driver_name, + hash_name) >= CRYPTO_MAX_ALG_NAME) + goto err_free_inst; + } + + inst->alg.base.cra_priority = rsa_alg->base.cra_priority; + inst->alg.base.cra_ctxsize = sizeof(struct rsapad_tfm_ctx); + + inst->alg.init = alg->init; + inst->alg.exit = alg->exit; + + inst->alg.encrypt = alg->encrypt; + inst->alg.decrypt = alg->decrypt; + inst->alg.sign = alg->sign; + inst->alg.verify = alg->verify; + inst->alg.set_pub_key = alg->set_pub_key; + inst->alg.set_priv_key = alg->set_priv_key; + inst->alg.max_size = alg->max_size; + inst->alg.reqsize = sizeof(struct rsapad_akciper_req_ctx) + rsa_alg->reqsize; + + inst->free = rsapad_akcipher_free; + + err = akcipher_register_instance(tmpl, inst); + if (err) { +err_free_inst: + rsapad_akcipher_free(inst); + } + return err; +} diff --git a/crypto/rsa-pkcs1pad.c b/crypto/rsa-pkcs1pad.c index ffb7220b3d10a..30b0193b7352a 100644 --- a/crypto/rsa-pkcs1pad.c +++ b/crypto/rsa-pkcs1pad.c @@ -9,73 +9,6 @@ #include #include -static int rsapad_set_pub_key(struct crypto_akcipher *tfm, const void *key, - unsigned int keylen) -{ - struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); - int err; - - ctx->key_size = 0; - - err = crypto_akcipher_set_pub_key(ctx->child, key, keylen); - if (err) - return err; - - /* Find out new modulus size from rsa implementation */ - err = crypto_akcipher_maxsize(ctx->child); - if (err > PAGE_SIZE) - return -ENOTSUPP; - - ctx->key_size = err; - return 0; -} - -static int rsapad_set_priv_key(struct crypto_akcipher *tfm, const void *key, - unsigned int keylen) -{ - struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); - int err; - - ctx->key_size = 0; - - err = crypto_akcipher_set_priv_key(ctx->child, key, keylen); - if (err) - return err; - - /* Find out new modulus size from rsa implementation */ - err = crypto_akcipher_maxsize(ctx->child); - if (err > PAGE_SIZE) - return -ENOTSUPP; - - ctx->key_size = err; - return 0; -} - -static unsigned int rsapad_get_max_size(struct crypto_akcipher *tfm) -{ - struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); - - /* - * The maximum destination buffer size for the encrypt/sign operations - * will be the same as for RSA, even though it's smaller for - * decrypt/verify. - */ - - return ctx->key_size; -} - -static void rsapad_akcipher_sg_set_buf(struct scatterlist *sg, void *buf, - size_t len, struct scatterlist *next) -{ - int nsegs = next ? 2 : 1; - - sg_init_table(sg, nsegs); - sg_set_buf(sg, buf, len); - - if (next) - sg_chain(sg, nsegs, next); -} - typedef int (*rsa_akcipher_complete_cb)(struct akcipher_request *, int); static void rsapad_akcipher_req_complete(struct crypto_async_request *child_async_req, int err, rsa_akcipher_complete_cb cb) @@ -461,37 +394,6 @@ static int pkcs1pad_verify(struct akcipher_request *req) return err; } -static int rsapad_akcipher_init_tfm(struct crypto_akcipher *tfm) -{ - struct akcipher_instance *inst = akcipher_alg_instance(tfm); - struct rsapad_inst_ctx *ictx = akcipher_instance_ctx(inst); - struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); - struct crypto_akcipher *child_tfm; - - child_tfm = crypto_spawn_akcipher(&ictx->spawn); - if (IS_ERR(child_tfm)) - return PTR_ERR(child_tfm); - - ctx->child = child_tfm; - return 0; -} - -static void rsapad_akcipher_exit_tfm(struct crypto_akcipher *tfm) -{ - struct rsapad_tfm_ctx *ctx = akcipher_tfm_ctx(tfm); - - crypto_free_akcipher(ctx->child); -} - -static void rsapad_akcipher_free(struct akcipher_instance *inst) -{ - struct rsapad_inst_ctx *ctx = akcipher_instance_ctx(inst); - struct crypto_akcipher_spawn *spawn = &ctx->spawn; - - crypto_drop_akcipher(spawn); - kfree(inst); -} - static struct akcipher_alg pkcs1pad_alg = { .init = rsapad_akcipher_init_tfm, .exit = rsapad_akcipher_exit_tfm, @@ -505,91 +407,6 @@ static struct akcipher_alg pkcs1pad_alg = { .max_size = rsapad_get_max_size }; -static int rsapad_akcipher_create(struct crypto_template *tmpl, struct rtattr **tb, - struct akcipher_alg *alg) -{ - u32 mask; - struct akcipher_instance *inst; - struct rsapad_inst_ctx *ctx; - struct akcipher_alg *rsa_alg; - const char *hash_name; - int err; - - err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_AKCIPHER, &mask); - if (err) - return err; - - inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL); - if (!inst) - return -ENOMEM; - - ctx = akcipher_instance_ctx(inst); - - err = crypto_grab_akcipher(&ctx->spawn, akcipher_crypto_instance(inst), - crypto_attr_alg_name(tb[1]), 0, mask); - if (err) - goto err_free_inst; - - rsa_alg = crypto_spawn_akcipher_alg(&ctx->spawn); - - err = -ENAMETOOLONG; - hash_name = crypto_attr_alg_name(tb[2]); - if (IS_ERR(hash_name)) { - if (snprintf(inst->alg.base.cra_name, - CRYPTO_MAX_ALG_NAME, "%s(%s)", tmpl->name, - rsa_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME) - goto err_free_inst; - - if (snprintf(inst->alg.base.cra_driver_name, - CRYPTO_MAX_ALG_NAME, "%s(%s)", tmpl->name, - rsa_alg->base.cra_driver_name) >= - CRYPTO_MAX_ALG_NAME) - goto err_free_inst; - } else { - ctx->digest_info = rsa_lookup_asn1(hash_name); - if (!ctx->digest_info) { - err = -EINVAL; - goto err_free_inst; - } - - if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, - "%s(%s,%s)", tmpl->name, rsa_alg->base.cra_name, - hash_name) >= CRYPTO_MAX_ALG_NAME) - goto err_free_inst; - - if (snprintf(inst->alg.base.cra_driver_name, - CRYPTO_MAX_ALG_NAME, "%s(%s,%s)", - tmpl->name, - rsa_alg->base.cra_driver_name, - hash_name) >= CRYPTO_MAX_ALG_NAME) - goto err_free_inst; - } - - inst->alg.base.cra_priority = rsa_alg->base.cra_priority; - inst->alg.base.cra_ctxsize = sizeof(struct rsapad_tfm_ctx); - - inst->alg.init = alg->init; - inst->alg.exit = alg->exit; - - inst->alg.encrypt = alg->encrypt; - inst->alg.decrypt = alg->decrypt; - inst->alg.sign = alg->sign; - inst->alg.verify = alg->verify; - inst->alg.set_pub_key = alg->set_pub_key; - inst->alg.set_priv_key = alg->set_priv_key; - inst->alg.max_size = alg->max_size; - inst->alg.reqsize = sizeof(struct rsapad_akciper_req_ctx) + rsa_alg->reqsize; - - inst->free = rsapad_akcipher_free; - - err = akcipher_register_instance(tmpl, inst); - if (err) { -err_free_inst: - rsapad_akcipher_free(inst); - } - return err; -} - static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb) { return rsapad_akcipher_create(tmpl, tb, &pkcs1pad_alg); diff --git a/include/crypto/internal/rsa-common.h b/include/crypto/internal/rsa-common.h index ecdce0cdafaa3..a6f20cce610ab 100644 --- a/include/crypto/internal/rsa-common.h +++ b/include/crypto/internal/rsa-common.h @@ -34,4 +34,17 @@ struct rsapad_akciper_req_ctx { struct akcipher_request child_req; }; +int rsapad_set_pub_key(struct crypto_akcipher *tfm, const void *key, + unsigned int keylen); +int rsapad_set_priv_key(struct crypto_akcipher *tfm, const void *key, + unsigned int keylen); +unsigned int rsapad_get_max_size(struct crypto_akcipher *tfm); +void rsapad_akcipher_sg_set_buf(struct scatterlist *sg, void *buf, + size_t len, struct scatterlist *next); +int rsapad_akcipher_init_tfm(struct crypto_akcipher *tfm); +void rsapad_akcipher_exit_tfm(struct crypto_akcipher *tfm); +void rsapad_akcipher_free(struct akcipher_instance *inst); +int rsapad_akcipher_create(struct crypto_template *tmpl, struct rtattr **tb, + struct akcipher_alg *alg); + #endif