From patchwork Thu Apr 8 14:15:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Varad Gautam X-Patchwork-Id: 417577 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, USER_AGENT_GIT autolearn=ham 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 BE78CC43462 for ; Thu, 8 Apr 2021 14:16:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92EAD6113E for ; Thu, 8 Apr 2021 14:16:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231938AbhDHOQx (ORCPT ); Thu, 8 Apr 2021 10:16:53 -0400 Received: from de-smtp-delivery-102.mimecast.com ([194.104.109.102]:58412 "EHLO de-smtp-delivery-102.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231965AbhDHOQo (ORCPT ); Thu, 8 Apr 2021 10:16:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=mimecast20200619; t=1617891391; 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=RLInDh1h/QkEsygzJTUoZgnIz9dQEBlOY2EEntpAdgI=; b=l18Pn2sw4/x3B8dikKtbRgQ3csaWQDSsNZ4WgJhk4opqetLb5DYlMaDQbouM1WILb8ogJP stx9uCEn1nIDMz57rk1bgKn6YpiZYE+WPT3k6qkzaOafji0ahwFN9l66Rge+y1jGc7HqRf mNGP4Xh10pNCWKRh5/NfN9NnY4YO4Kk= Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03lp2054.outbound.protection.outlook.com [104.47.9.54]) (Using TLS) by relay.mimecast.com with ESMTP id de-mta-1-ZTpLxAohNKOtORKYzWW-Gw-3; Thu, 08 Apr 2021 16:16:20 +0200 X-MC-Unique: ZTpLxAohNKOtORKYzWW-Gw-3 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IdgX0vw8HPRAKR2YiKZecFBktziDxWS7x9rsFb6rtoIVM5YLZti2NNyPBsBOiHU5e/Gj0Navz80fKWsdG5imEPGWbPdbVWR+q5GUnRw34S31vfLdGipJHg8cF8u/0OMWR53NVqanKp5omExKbH12Z8jLtV9lCzXSPHS5az2dE//6yswsYDQoupozySp6VEb5UiHXTS3TD2GpffGgsHeKcaD9vQbM+X2ip5cqCWxQElh14NRPWc7Bty+VfCZUjlKfgXA+ZFQlR7jWFib3tL94kqpujV7Onm/rQvIF4WmTJNjId7/Zqs/KReZhxs3DcM4QliQedeU6fNfh42xEBBoHkg== 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=68wfWvHe5jcomxWSlTIKYqj5qEWMMKnldpc+63TNMwI=; b=PxS+fe2L6nVUjvTHFzgwOfRUJK1nW9Di/6t2NKB65HVE9KsjPH1DXMKwij1HMlR8mVbZyTYfEe1129p/b7+G51qfFfhNaH1tQGjUsMV8AxreGZ/hokQER3jT77Xh3GykoW+RwRSkpZol7SPUSY+HyCE/Z+aBB8VDixmIIqQ/L5FMv8IpzvB/ZO/irIir7Dzcov9/pbNf6WYNtF1eHIdCt33zYgRQmjzfRT+gr7vlnUepgb54WzqzdyCiH3QyhJycQ8jP5Lm0N81hTHZ/3Lqz9YVLLYoz8jEV/BZyYWiZgJPC0I8CGOHjgbo7Yj5TQyq3Td8PGjXB8OC2VZLMR3ypjQ== 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:02 +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:02 +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 06/18] crypto: rsa: Start moving RSA common code to rsa-common Date: Thu, 8 Apr 2021 16:15:04 +0200 Message-ID: <20210408141516.11369-7-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:01 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: af2984c0-cfc6-4209-626b-08d8fa98d6df X-MS-TrafficTypeDiagnostic: AM0PR04MB6289: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2657; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hCkcAUtDkQJ+h5Vu7vycDSApD4kZQEAxWrq0Etf0sKpe7G7nHAgpB+o0JQkOrj9we67cVu7t/t9iafagrVdfMy1JDBIxj0oAKUvU3J7jxgw3UrBuUCpj2j88H/mVfg1NiO2P5Qd4FRTl4Aittl3bQFiwPyeGcMzeCpK8K0Fq//unY1hgy9MMCPM089p5ydxU9NkiLiUaSakE6EtZk/cXDszBypD6uzFQSLP96cW9lKAopx5+WCvXQvMFsanA3ysFddpMf9U9LA7hpIhbTqsrmFyqqYTEPDb79MWKhxZK/KnbYqYnGxRhEVmtFLbqetx1GaUrXLtnhkkRNSYGrdHfeSEcxAxVYZeAczmVpCpBzv8FrzUT5UssuJMQwI7rq0skyL2zrUmFzxnv8oXyfYBR3WZvjnB/JzFJcoEljBlHdjXVGXuaO4u7r37umLe26K+WaayeYQKv/pQGdI4hFtpnAUFolcfUxTDbsnybRcTnzhsxn2N6+ksYEwkBCgkCWhAwtIiaJLa63HzpdjUUr/S6uNHiNB71IO4J5cvRiOWaedcEBYIvDN5+uMjw9D39yUrzJya4PE8bXOGM4263mgPqI0aurxjiIQ+U1MJKKF2MBPCUseMFZfXQA+HEsSJbFfYjqdt1YRUeyAC07tCahlQ0NDK7rT3YA9HOhjk9kRwH1MX3xgpvgU6lGtjMa1XJAljn 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)(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: YnxqARE6H4PoM7bVRgxsTYxMErgdOQbK5jZaGBDF/ucK6ef+OaSWQPHqLhneLDROyd744gFrPX1seN8oBrdANoOJPy5WNxQO2qTiaWD1/8AWh/d60pkOm7SkuigHQfGtu03vs7emaPa2fi7SJMedoCOMA2UmkWzgJRME62MhLkemz3J4p4jdrROOPQdZn8aRsijPqOIGswGhu37ld8g6wN44RoUj5Lng9b0Cokf8dle8meKnZAUUQj7g2TTc+cbr/f4+YmAEDyPCHdqFDkoWFMnnWnw7Y0sdMYeerMxpqxT8z6JlCJs4QNjVBky1yLDG6B+NY1uCXg9NEoXcES2sSwScPnN/RpGH0nhGQQ+ZPWDlGrcCEFIyCgDM5nEKudGsFe/RGprFYOBLTxdw3Kt7IdyC7Cxk4uZ/D/pYHihHld+dtu91Eb9nWlT9g32ytnRxWYA1+5LCjh/Y2mIc+6FpJOaoXpQYUGQ9u6j3MwMvSjYG0cn83AqiavFzji6gpTst682vxVBcNTxyJiljnJSmdH8aqvJJmBdajLDRHKeWZNcTj/doLzOoQY3Vni4E69TVUuW/hiWy1UxzMCSn1XSBmLl8Im6C1W/diOv6HWV+ud07AcZk06968ibc4xwbeINzayj5Et7XdPf6ozO5hcPuqaWlEWGkHJL8hiq+n9cQ+Ko36gMF0xHUkIOJBJYlmXSfZ879n9ZMttvqedDNtvx8qnQHDkb7tUx8QgFNLqHXdRg5+/fKhoPbx7seGXjJ8qOkbMqBSVoIZwGj8aejAYvslTglRPwGEHN7yeGg9SUSvT2I8owr3+XCyGuedJqBgmTA77ziUJPW/xPXBQLGE5QiNzqAs+pwmqQsstJK0mN+JGp2ZfKVtCe/ZtSeq2SWPoGQ5z4yHXSdY+Oc+XhwcxEmMLqHHVL78as+n55KQgIa8vTKw26+LFurmHA5XrTzAAyCT87/ndxZ2j45jW0ZQtUkpIVSZO8n7lD86ngu2hV8rZlIDLtSsZ0ENw2tGvOEKal+mvF221o3XVbFjWMAR4HOy/5/lhR1vyreSYeeN0OifHNnE7nHDNkD/B5qJxGxq4/mTrXK9/m4s4Ydt1JGPEs16Av76apav3cq19aa7VLS966VfReqiiSDY/FXOcXO7YKyyWMu1ve1QwVY49CRngDAXiNva55UdOtCv4DubWUcFXfid1d3Okc6LUSH15LibB+YN9EuzZr77HkDxdhU7ym+B+OtRMMD4rXS/RN8tNqkbKq/X49U/jFTy9WoblVR/+HBfYaw4Um4KeBSA9zGREs/6aI+elyzVnn3kfjdU5a/ZjdKPXWQwFq1zp6/EogMpY0R X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: af2984c0-cfc6-4209-626b-08d8fa98d6df 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:02.2100 (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: LYPs0OM7EK3PkqC/C5ZisX56OCbUHTeZHi8P48bZRIYka1BW01o0hcgcqaWteXvGYkVJT/KYcbMewFR5aesAJg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR04MB6289 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Move out helpers from rsa-pkcs1pad.c which will be shared across rsa-*pad implementations. Signed-off-by: Varad Gautam --- crypto/Makefile | 1 + crypto/rsa-common.c | 76 ++++++++++++++++++++++ crypto/rsa-pkcs1pad.c | 97 +--------------------------- include/crypto/internal/rsa-common.h | 37 +++++++++++ 4 files changed, 115 insertions(+), 96 deletions(-) create mode 100644 crypto/rsa-common.c create mode 100644 include/crypto/internal/rsa-common.h diff --git a/crypto/Makefile b/crypto/Makefile index cf23affb16780..57a85b2b3429b 100644 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -39,6 +39,7 @@ rsa_generic-y := rsapubkey.asn1.o rsa_generic-y += rsaprivkey.asn1.o rsa_generic-y += rsa.o rsa_generic-y += rsa_helper.o +rsa_generic-y += rsa-common.o rsa_generic-y += rsa-pkcs1pad.o obj-$(CONFIG_CRYPTO_RSA) += rsa_generic.o diff --git a/crypto/rsa-common.c b/crypto/rsa-common.c new file mode 100644 index 0000000000000..60073c56e3748 --- /dev/null +++ b/crypto/rsa-common.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2015 Intel Corporation + * Copyright (C) 2021 SUSE + * + */ + +#include + +/* + * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2]. + */ +static const u8 rsa_digest_info_md5[] = { + 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, + 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */ + 0x05, 0x00, 0x04, 0x10 +}; + +static const u8 rsa_digest_info_sha1[] = { + 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, + 0x2b, 0x0e, 0x03, 0x02, 0x1a, + 0x05, 0x00, 0x04, 0x14 +}; + +static const u8 rsa_digest_info_rmd160[] = { + 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, + 0x2b, 0x24, 0x03, 0x02, 0x01, + 0x05, 0x00, 0x04, 0x14 +}; + +static const u8 rsa_digest_info_sha224[] = { + 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, + 0x05, 0x00, 0x04, 0x1c +}; + +static const u8 rsa_digest_info_sha256[] = { + 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, + 0x05, 0x00, 0x04, 0x20 +}; + +static const u8 rsa_digest_info_sha384[] = { + 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, + 0x05, 0x00, 0x04, 0x30 +}; + +static const u8 rsa_digest_info_sha512[] = { + 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, + 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, + 0x05, 0x00, 0x04, 0x40 +}; + +static const struct rsa_asn1_template rsa_asn1_templates[] = { +#define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) } + _(md5), + _(sha1), + _(rmd160), + _(sha256), + _(sha384), + _(sha512), + _(sha224), + { NULL } +#undef _ +}; + +const struct rsa_asn1_template *rsa_lookup_asn1(const char *name) +{ + const struct rsa_asn1_template *p; + + for (p = rsa_asn1_templates; p->name; p++) + if (strcmp(name, p->name) == 0) + return p; + return NULL; +} diff --git a/crypto/rsa-pkcs1pad.c b/crypto/rsa-pkcs1pad.c index e76fc98a537a4..ffb7220b3d10a 100644 --- a/crypto/rsa-pkcs1pad.c +++ b/crypto/rsa-pkcs1pad.c @@ -5,104 +5,9 @@ * Copyright (c) 2015 Intel Corporation */ -#include -#include -#include -#include -#include -#include -#include +#include #include #include -#include - -/* - * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2]. - */ -static const u8 rsa_digest_info_md5[] = { - 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, - 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */ - 0x05, 0x00, 0x04, 0x10 -}; - -static const u8 rsa_digest_info_sha1[] = { - 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, - 0x2b, 0x0e, 0x03, 0x02, 0x1a, - 0x05, 0x00, 0x04, 0x14 -}; - -static const u8 rsa_digest_info_rmd160[] = { - 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, - 0x2b, 0x24, 0x03, 0x02, 0x01, - 0x05, 0x00, 0x04, 0x14 -}; - -static const u8 rsa_digest_info_sha224[] = { - 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, - 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, - 0x05, 0x00, 0x04, 0x1c -}; - -static const u8 rsa_digest_info_sha256[] = { - 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, - 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, - 0x05, 0x00, 0x04, 0x20 -}; - -static const u8 rsa_digest_info_sha384[] = { - 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, - 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, - 0x05, 0x00, 0x04, 0x30 -}; - -static const u8 rsa_digest_info_sha512[] = { - 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, - 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, - 0x05, 0x00, 0x04, 0x40 -}; - -static const struct rsa_asn1_template { - const char *name; - const u8 *data; - size_t size; -} rsa_asn1_templates[] = { -#define _(X) { #X, rsa_digest_info_##X, sizeof(rsa_digest_info_##X) } - _(md5), - _(sha1), - _(rmd160), - _(sha256), - _(sha384), - _(sha512), - _(sha224), - { NULL } -#undef _ -}; - -static const struct rsa_asn1_template *rsa_lookup_asn1(const char *name) -{ - const struct rsa_asn1_template *p; - - for (p = rsa_asn1_templates; p->name; p++) - if (strcmp(name, p->name) == 0) - return p; - return NULL; -} - -struct rsapad_tfm_ctx { - struct crypto_akcipher *child; - unsigned int key_size; -}; - -struct rsapad_inst_ctx { - struct crypto_akcipher_spawn spawn; - const struct rsa_asn1_template *digest_info; -}; - -struct rsapad_akciper_req_ctx { - struct scatterlist in_sg[2], out_sg[1]; - uint8_t *in_buf, *out_buf; - struct akcipher_request child_req; -}; static int rsapad_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) diff --git a/include/crypto/internal/rsa-common.h b/include/crypto/internal/rsa-common.h new file mode 100644 index 0000000000000..ecdce0cdafaa3 --- /dev/null +++ b/include/crypto/internal/rsa-common.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (c) 2015 Intel Corporation + * Copyright (C) 2021 SUSE + * + */ +#ifndef _RSA_COMMON_ +#define _RSA_COMMON_ + +#include +#include +#include + +struct rsa_asn1_template { + const char *name; + const u8 *data; + size_t size; +}; +const struct rsa_asn1_template *rsa_lookup_asn1(const char *name); + +struct rsapad_tfm_ctx { + struct crypto_akcipher *child; + unsigned int key_size; +}; + +struct rsapad_inst_ctx { + struct crypto_akcipher_spawn spawn; + const struct rsa_asn1_template *digest_info; +}; + +struct rsapad_akciper_req_ctx { + struct scatterlist in_sg[2], out_sg[1]; + uint8_t *in_buf, *out_buf; + struct akcipher_request child_req; +}; + +#endif