From patchwork Fri Apr 29 12:51:46 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tudor Ambarus X-Patchwork-Id: 8981061 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-linux-crypto@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 12282BF29F for ; Fri, 29 Apr 2016 12:52:12 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 19753201DD for ; Fri, 29 Apr 2016 12:52:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 49A002015A for ; Fri, 29 Apr 2016 12:52:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753132AbcD2MwC (ORCPT ); Fri, 29 Apr 2016 08:52:02 -0400 Received: from mail-by2on0063.outbound.protection.outlook.com ([207.46.100.63]:42336 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753003AbcD2Mv7 (ORCPT ); Fri, 29 Apr 2016 08:51:59 -0400 Received: from BN3PR0301CA0038.namprd03.prod.outlook.com (10.160.180.176) by BN1PR0301MB0708.namprd03.prod.outlook.com (10.160.78.14) with Microsoft SMTP Server (TLS) id 15.1.477.8; Fri, 29 Apr 2016 12:51:57 +0000 Received: from BY2FFO11FD001.protection.gbl (2a01:111:f400:7c0c::124) by BN3PR0301CA0038.outlook.office365.com (2a01:111:e400:4000::48) with Microsoft SMTP Server (TLS) id 15.1.477.8 via Frontend Transport; Fri, 29 Apr 2016 12:51:56 +0000 Authentication-Results: spf=fail (sender IP is 192.88.168.50) 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.168.50 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.168.50; helo=tx30smr01.am.freescale.net; Received: from tx30smr01.am.freescale.net (192.88.168.50) by BY2FFO11FD001.mail.protection.outlook.com (10.1.14.123) with Microsoft SMTP Server (TLS) id 15.1.477.4 via Frontend Transport; Fri, 29 Apr 2016 12:51:56 +0000 Received: from enigma.ea.freescale.net (enigma.ea.freescale.net [10.171.77.120]) by tx30smr01.am.freescale.net (8.14.3/8.14.0) with ESMTP id u3TCpr7P016727; Fri, 29 Apr 2016 05:51:54 -0700 From: Tudor Ambarus To: CC: , Tudor Ambarus Subject: [PATCH] crypto: rsa - return raw integer for the ASN.1 parser Date: Fri, 29 Apr 2016 15:51:46 +0300 Message-ID: <1461934306-29190-1-git-send-email-tudor-dan.ambarus@nxp.com> X-Mailer: git-send-email 1.8.3.1 X-EOPAttributedMessage: 0 X-Matching-Connectors: 131064079163476776; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.168.50; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(2980300002)(1110001)(1109001)(339900001)(199003)(189002)(1220700001)(50226002)(2906002)(1096002)(6806005)(5008740100001)(586003)(104016004)(4326007)(50986999)(85426001)(2351001)(19580405001)(19580395003)(33646002)(47776003)(11100500001)(36756003)(50466002)(92566002)(81166005)(229853001)(77096005)(189998001)(48376002)(87936001)(106466001)(105606002)(575784001)(86362001)(110136002)(5003940100001)(7059030)(4720700001); DIR:OUT; SFP:1101; SCL:1; SRVR:BN1PR0301MB0708; H:tx30smr01.am.freescale.net; FPR:; SPF:Fail; MLV:sfv; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BY2FFO11FD001; 1:aUofzR6MUf6tYJ+ggNPpFytQlvHwn9agnuSyHaY4l3N5G2hsgICX0qJom0A0yQbgHNY5wg+y044ck0gx3+tZQXFr441KbSzIFrlX4a/O7t1zI20j3ptK60kr6CLK5NLcEWyFRBrbZFFMhtyjscH2JmH9gdS86gQalxs8CE5P5+SadlCXBB2Z16OAsK5zoXw9HMDTtw9eehk7ks98LblMGDlpaXh+YmGLFhxBWoy8735TdiX96BcR7w8cYa5NZ+EC96l1jeewvGuS6AhCzSEVAeI/MQco6N82iRdbH5kpJsXDO5CIIorupWtc3SIT+9vm4drIGu/rvM0mv5RxE/OvX9ONHOfyamPf6e1oISsK5a+ciu5ZFvSVz3E3vfrfyseW/PbnKlKWky0HFHBytZS2eekeRJeitzFza1/hD2GlNW8KCqToNGvsmBLGtT8XtH69/SGraxDlNDQDGXV1i0tBoIh6itqD0pME6DQbnvqfcl02umsSWtl8WgACPRVm3CPcdAZj635gNA5IzsiUdixYehAnvIzDuiqyJKINR0ddHbNT4MyxIU3i0Fojg2AluJOvIHI32ZqwuJxO8p6jQwnvy7xIMtZA1tFHDAbUeaI8kNPVJ0qfi+6f3e82xrc7QZffS55mhfyvKCxFLFykNFJYAehrLjJwnh4SNCTOKDfbljHz8K3M0vEYtERmRdxFTOiQZyZbTOEHUKb/mCHthe9kjA== MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 93648134-3444-4c51-4b28-08d3702d0bd1 X-Microsoft-Exchange-Diagnostics: 1; BN1PR0301MB0708; 2:syEfWKfkcjqQPW5ximPdCP8jGKUgeS17Qfjgy4tkm9tt5hzixk/JfKB76OnYRb0UFYdflZus8M0TyG7OeuSDQ2tKMtt0Xkca4YCevDmEcZ/VVXD+WekMoiZeizOOif1wcsCoKUZ9SiuJn7YrpCE3h24nQBj46izoCaCBBDWVmCykAgmOFymZSgaGfGpmNQmm; 3:aqQd23QvtJCiFhjQjc9RqPJ0Wism3FJRfNqzJKAoLkNXf7qLfD1JVLKEmJrQEIDDULjduNHQYySZIf99XI7fF1UQTFmTmgAlHgsh9bZ9U3wED7Y37etSLS/5/7Iq/6WQndo59bkPg09k66kNzs4912pFgD3ewSeC45wrpmYbPb4S3x6Xhd+busxIJSBJGvEMQNsFdP96MSZjd4P8W+mE1qEppjWBaNwoe/WOvVQ2hdM= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:BN1PR0301MB0708; X-Microsoft-Exchange-Diagnostics: 1; BN1PR0301MB0708; 25:Y9xgcf48/aJmTg6aeF9UrUKH2+MUg8/WSs2ncPaNS0FMbbSul1DdTuhHRjp5idHek75mA44TMyaWN30ZUtgUwo9j1Mm1nhU1uaFrGw6nJFVy1RbSnSdRTW6BqiA7Y4nrRAQRWws7U3BZKw1zv+1X035Kp1eejqO+MJ/cfvOJ1UcYHcWY3SOWU/rNZDGPfa+ctZCCgLO/U4aHHWYK9u4J1GK8eMKx6rhcR9wzr+8SmFfyIO4scpArYC3j23jDhi3NrqnoesiUf70T2rPU6Mqlbcdy2NYSEUzpekr21e4WSsgzJpcWkqCFfKwt5fb26EsnDnXmZCe7jxPMYc0i8KmqGbQE6Y7t4YqPrBcP0YTK8czsYi8SuiRvuL4eyeuHucrjQb9uhOvs2t5WIQ8idt4uvRyfdN5gB0nzLOGqx+wpjfj84wV5ZnjDCnwTzCxfVMprhgnrVnDMpTlmoCWlmi56MbzMHPYFwFMHBS02U/HznG6EywMlrfGAObjPjD95LMi70rC7PvcART1zBEQiF/9ohyR5HcE7lE6PO9euPAr4zmXp6d/SLe/1hPgFFfNuvgO2TcoTnjeOHKtAIG8wbnycAkK7M0tD6f+7jcyhN4OD8BE9r3ZgDuIy2zakZngkClU77t6DZZ/FMkYshbssKr2YPhx9tv1c+u9GR1D78T/sAHOJdOnbEiQm15PETQQrLBXP X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13024025)(13023025)(13015025)(13017025)(13018025)(8121501046)(5005006)(3002001)(10201501046)(6055026); SRVR:BN1PR0301MB0708; BCL:0; PCL:0; RULEID:(400006); SRVR:BN1PR0301MB0708; X-Microsoft-Exchange-Diagnostics: 1; BN1PR0301MB0708; 4:2GqzSQ4+F6llzXRJ0ohtAlSxxKhoODxeW/xlhElhi0DGq5O1bdbuABlVF6w4tZ8qJuX7S4J2wjYaSHekNKaW43jNPkeNUBJwNf4M9n7F+U7VpmtbcYjNG9QnKTE/fA0+rXz1d/Zxk9/ffS5BCwDimKgcrjAWVE9dch20XrrtR6k9IP8grvtwV5jFwxiOP0qsJzR0VhuTTwEkF0fT3dNT1yeyKWFtkRzy9HdzKpuwscmbuvxChisUyY0Gkyzyt+Pys5thstWWqYUDuWr8DQsw8N+WaRVgLYGjQ1D+T2jP5rdmu6ov0mLe1OLhjHMd8aIfHRBGUkyJX3ta/PD6IGm581zu90NxWz0ZpIx/1WJkR+RhVhzxcc/0rimTSWFJPKj6WK/viZNgfhsdL8iiHx3uIiWuM7Qf3GkFs3Qx95Fj3dfpiF+NdIcuUTZ1WRlOLT0syEf9PO+F09+dv1IjFH+ZOt2IQUzM6NRVOM/x0C4RX8s= X-Forefront-PRVS: 0927AA37C7 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BN1PR0301MB0708; 23:acJTsLJF2vP+pN/8lypcuDpGUOKad2WS/dSi702?= =?us-ascii?Q?uJw2eyVoNByIDRuLZljJ1YYmVVLED3yYhe4/DvHx0c3pD/aU0aXS32+ahExf?= =?us-ascii?Q?ByJO0JrEoytNqbnaqdeiPl6ORFETItc4fMhY/jBjBBnag2zDl/egib9G06vr?= =?us-ascii?Q?W2ARVggkmNsxLjo9rQn0KbvJA0CiUifwH/G+jxxICAkmD4+wDWWUcMze8y3E?= =?us-ascii?Q?wWwK7SI9pPEnHzXE0qIXuNanS0qeSPtyhGy1jx1QD6rryF+MTeqIWlaG8BZz?= =?us-ascii?Q?zMHzaV17qXUOh2HaIZQwKr+sCURnDw+vn9RuElSJK9fOaRPQKNGofQM93SY7?= =?us-ascii?Q?+4wDjslazgTSmrOcpZeOyZXLhtI8I1d90xw2FTz8BLWNKrbT2J4CFzikZHcm?= =?us-ascii?Q?fNpx4ajOQ4uIns1SweX6KiN59jarPJBC6ANk4oEiCpHYCVPFjhcJpMqMtod3?= =?us-ascii?Q?4VdGVDjs+29g/V7cbJHY2k4rupCE0AhsGeWufbuSeQIIaKOeUbZF6qtJJFpU?= =?us-ascii?Q?KdUK+ALRe/4bIDb6dbVLvaIJuSUm0U0VaaktBeLvDRoPKKO1Kt+hTIM0nhsp?= =?us-ascii?Q?3UyBqcdREf2Z8HtMk68v4vKuBuVtHDMOlFzqjwg/h5I5Boa92TDvYGo0/cKu?= =?us-ascii?Q?CAVKyjUcqJKE0u4OgyPN03J8aajwWMd79EqGa1bScPLpptREg+xbXdn/zffH?= =?us-ascii?Q?oeNOTiBaDsd9WzjGrUApfuumwgLnFlxDqn7r3sN5wb/phhWQ/a0XD3gxJOx0?= =?us-ascii?Q?kepJptnKymHZfo+i+uSLXyBmCWG7ZcpNTop9y95kOuzo32oBMrDpdpzTvo0m?= =?us-ascii?Q?3e9UD8Bnum05OSjbTCEWzQufhyJ7tco8qFYBdznbMvguzzEZkKjpZAflqyL5?= =?us-ascii?Q?49DSOw+5hEN1tHuQPBA10V+pUbWRBrkT7r7Ck7e5vZpvQTgKV56oMkRK5zBJ?= =?us-ascii?Q?C6+3b1TnYYb5uCPNbEqWyUtdDnrr3LtgM4gNqqmtkCTstPdzAFPpIBeqJgnV?= =?us-ascii?Q?xw/6eTjbVh0WANyKVUqBhIG3joHkT3CdPlXxVYhlfPV27ykw0qmR4iUMfxv6?= =?us-ascii?Q?1+yWdCxioZK1Ccscmv2/2qpFaquRV?= X-Microsoft-Exchange-Diagnostics: 1; BN1PR0301MB0708; 5:it8E6WkY96u82HUXrLZXVtVoKqY07QEpZm7y9G1THXDdlY9C+5pbzL2ikGp0XcbruyWNLdMOqMdfVeJe84g6l6u+bsaYvDfOW4hwJ/ivOYOUsfVFFPfEuAD9AzG+np6EtpHXBS215RIID8MwsZvCuGDkTRqOokMsemOn6rWlNog=; 24:cTNCi44bJUYP3695p5vGr0hYbUu+53coj8317Cq/UV5GJ5QVvpEskzyZo85YmTcheaStkl96VbJSriFNHMRB15mN1jCRWWeJtZzGgc9EUgY=; 7:N0/kagxkqdBOvWxsHnsJvfJiB1B9B4Np5vgYS/lIimpsxQouQUT9e2QQsSyNs2Yywza2RH5uJMITYAZjCPkDgNNfKESpI2dgSl8egx16MaIztd4l2awg1ZSr8gkEyJYwD0/fWG73Acn35kT0HpIHq4BG5EMI3K/v3w7nUQn+9zwvtB1fmV0L5jr3urx8dMpT SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Apr 2016 12:51:56.1448 (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.168.50]; Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN1PR0301MB0708 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-7.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 Return the raw integer with no other processing. The scope is to have only one ANS.1 parser for the RSA keys. Update the RSA software implementation so that it does the MPI conversion on top. Signed-off-by: Tudor Ambarus --- crypto/rsa.c | 122 ++++++++++++++--------- crypto/rsa_helper.c | 224 ++++++++++++++++++++++++++++++++---------- include/crypto/internal/rsa.h | 41 +++++++- 3 files changed, 287 insertions(+), 100 deletions(-) diff --git a/crypto/rsa.c b/crypto/rsa.c index 77d737f..4459cb7 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->mpi_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; @@ -233,67 +235,98 @@ err_free_m: return ret; } -static int rsa_check_key_length(unsigned int len) -{ - switch (len) { - case 512: - case 1024: - case 1536: - case 2048: - case 3072: - case 4096: - return 0; - } - - return -EINVAL; -} - 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_key *pkey = &ctx->key; + struct rsa_mpi_key *mpi_key = &ctx->mpi_key; int ret; - ret = rsa_parse_pub_key(pkey, key, keylen); + /* Free the old MPI key if any */ + rsa_free_mpi_key(mpi_key); + + ret = rsa_parse_pub_key(ctx, key, keylen); if (ret) return ret; - if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { - rsa_free_key(pkey); - ret = -EINVAL; - } - return ret; + mpi_key->e = mpi_read_raw_data(pkey->e, pkey->e_sz); + if (!mpi_key->e) + goto err; + + mpi_key->n = mpi_read_raw_data(pkey->n, pkey->n_sz); + if (!mpi_key->n) + goto err; + + return 0; + +err: + rsa_free_mpi_key(mpi_key); + rsa_free_key(ctx->dev, pkey); + return -ENOMEM; } 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_key *pkey = &ctx->key; + struct rsa_mpi_key *mpi_key = &ctx->mpi_key; int ret; - ret = rsa_parse_priv_key(pkey, key, keylen); + /* Free the old MPI key if any */ + rsa_free_mpi_key(mpi_key); + + ret = rsa_parse_priv_key(ctx, key, keylen); if (ret) return ret; - if (rsa_check_key_length(mpi_get_size(pkey->n) << 3)) { - rsa_free_key(pkey); - ret = -EINVAL; - } - return ret; + mpi_key->d = mpi_read_raw_data(pkey->d, pkey->n_sz); + if (!mpi_key->d) + goto err; + + mpi_key->e = mpi_read_raw_data(pkey->e, pkey->e_sz); + if (!mpi_key->e) + goto err; + + mpi_key->n = mpi_read_raw_data(pkey->n, pkey->n_sz); + if (!mpi_key->n) + goto err; + + return 0; + +err: + rsa_free_mpi_key(mpi_key); + rsa_free_key(ctx->dev, pkey); + return -ENOMEM; } static int rsa_max_size(struct crypto_akcipher *tfm) { - struct rsa_key *pkey = akcipher_tfm_ctx(tfm); + struct rsa_ctx *ctx = akcipher_tfm_ctx(tfm); + struct rsa_key *pkey = &ctx->key; + + return pkey->n ? pkey->n_sz : -EINVAL; +} - return pkey->n ? mpi_get_size(pkey->n) : -EINVAL; +static int rsa_init_tfm(struct crypto_akcipher *tfm) +{ + struct rsa_ctx *ctx = akcipher_tfm_ctx(tfm); + struct rsa_key *pkey = &ctx->key; + + pkey->flags = GFP_KERNEL; + + return 0; } static void rsa_exit_tfm(struct crypto_akcipher *tfm) { - struct rsa_key *pkey = akcipher_tfm_ctx(tfm); + struct rsa_ctx *ctx = akcipher_tfm_ctx(tfm); + struct rsa_key *pkey = &ctx->key; + struct rsa_mpi_key *mpi_key = &ctx->mpi_key; - rsa_free_key(pkey); + rsa_free_mpi_key(mpi_key); + rsa_free_key(ctx->dev, pkey); } static struct akcipher_alg rsa = { @@ -304,13 +337,14 @@ static struct akcipher_alg rsa = { .set_priv_key = rsa_set_priv_key, .set_pub_key = rsa_set_pub_key, .max_size = rsa_max_size, + .init = rsa_init_tfm, .exit = rsa_exit_tfm, .base = { .cra_name = "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..492f37f 100644 --- a/crypto/rsa_helper.c +++ b/crypto/rsa_helper.c @@ -14,136 +14,256 @@ #include #include #include +#include +#include +#include #include #include "rsapubkey-asn1.h" #include "rsaprivkey-asn1.h" +static int rsa_check_key_length(unsigned int len) +{ + switch (len) { + case 512: + case 1024: + case 1536: + case 2048: + case 3072: + case 4096: + return 0; + } + + return -EINVAL; +} + 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_ctx *ctx = context; + struct rsa_key *key = &ctx->key; + const char *ptr = value; + int ret = -EINVAL; - key->n = mpi_read_raw_data(value, vlen); - - if (!key->n) - return -ENOMEM; + while (!*ptr && vlen) { + ptr++; + vlen--; + } + key->n_sz = vlen; /* In FIPS mode only allow key size 2K & 3K */ - if (fips_enabled && (mpi_get_size(key->n) != 256 && - mpi_get_size(key->n) != 384)) { - pr_err("RSA: key size not allowed in FIPS mode\n"); - mpi_free(key->n); - key->n = NULL; - return -EINVAL; + if (fips_enabled && (key->n_sz != 256 && key->n_sz != 384)) { + dev_err(ctx->dev, "RSA: key size not allowed in FIPS mode\n"); + goto err; } + /* invalid key size provided */ + ret = rsa_check_key_length(key->n_sz << 3); + if (ret) + goto err; + + if (key->coherent) + key->n = dma_zalloc_coherent(ctx->dev, key->n_sz, &key->dma_n, + key->flags); + else + key->n = kzalloc(key->n_sz, key->flags); + + if (!key->n) { + ret = -ENOMEM; + goto err; + } + + memcpy(key->n, ptr, key->n_sz); + return 0; +err: + key->n_sz = 0; + key->n = NULL; + return ret; } int rsa_get_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_key *key = &ctx->key; + const char *ptr = value; + size_t offset = 0; + + while (!*ptr && vlen) { + ptr++; + vlen--; + } - key->e = mpi_read_raw_data(value, vlen); + key->e_sz = vlen; + + if (!key->n_sz || !vlen || vlen > key->n_sz) { + key->e = NULL; + return -EINVAL; + } + + if (key->coherent) { + key->e = dma_zalloc_coherent(ctx->dev, key->n_sz, &key->dma_e, + key->flags); + offset = key->n_sz - vlen; + } else { + key->e = kzalloc(key->e_sz, key->flags); + } if (!key->e) return -ENOMEM; + memcpy(key->e + offset, ptr, vlen); + return 0; } int rsa_get_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_key *key = &ctx->key; + const char *ptr = value; + size_t offset = 0; + int ret = -EINVAL; - key->d = mpi_read_raw_data(value, vlen); + while (!*ptr && vlen) { + ptr++; + vlen--; + } - if (!key->d) - return -ENOMEM; + if (!key->n_sz || !vlen || vlen > key->n_sz) + goto err; /* In FIPS mode only allow key size 2K & 3K */ - if (fips_enabled && (mpi_get_size(key->d) != 256 && - mpi_get_size(key->d) != 384)) { - pr_err("RSA: key size not allowed in FIPS mode\n"); - mpi_free(key->d); - key->d = NULL; - return -EINVAL; + if (fips_enabled && (vlen != 256 && vlen != 384)) { + dev_err(ctx->dev, "RSA: key size not allowed in FIPS mode\n"); + goto err; } - return 0; -} -static void free_mpis(struct rsa_key *key) -{ - mpi_free(key->n); - mpi_free(key->e); - mpi_free(key->d); - key->n = NULL; - key->e = NULL; + if (key->coherent) { + key->d = dma_zalloc_coherent(ctx->dev, key->n_sz, &key->dma_d, + key->flags); + offset = key->n_sz - vlen; + } else { + key->d = kzalloc(key->n_sz, key->flags); + } + + if (!key->d) { + ret = -ENOMEM; + goto err; + } + + memcpy(key->d + offset, ptr, vlen); + + return 0; +err: key->d = NULL; + return ret; } -/** - * 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) +void rsa_free_key(struct device *dev, struct rsa_key *key) { - free_mpis(key); + if (key->coherent) { + if (key->d) { + memset(key->d, 0, key->n_sz); + dma_free_coherent(dev, key->n_sz, key->d, key->dma_d); + } + if (key->e) + dma_free_coherent(dev, key->n_sz, key->e, key->dma_e); + if (key->n) + dma_free_coherent(dev, key->n_sz, key->n, key->dma_n); + } else { + kzfree(key->d); + kfree(key->e); + kfree(key->n); + } + + key->d = NULL; + key->e = NULL; + key->n = NULL; + key->n_sz = 0; + key->e_sz = 0; } 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 + * and stores it in the provided struct rsa_key. * - * @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, +int rsa_parse_pub_key(struct rsa_ctx *ctx, const void *key, unsigned int key_len) { + struct rsa_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_key(ctx->dev, rsa_key); + + ret = asn1_ber_decoder(&rsapubkey_decoder, ctx, key, key_len); if (ret < 0) goto error; + if (!rsa_key->n || !rsa_key->e) { + /* Invalid key provided */ + ret = -EINVAL; + goto error; + } + return 0; error: - free_mpis(rsa_key); + rsa_free_key(ctx->dev, rsa_key); return ret; } EXPORT_SYMBOL_GPL(rsa_parse_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_priv_key() - extracts an rsa private key from BER encoded buffer + * and stores it in the provided struct rsa_key. * - * @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, +int rsa_parse_priv_key(struct rsa_ctx *ctx, const void *key, unsigned int key_len) { + struct rsa_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_key(ctx->dev, rsa_key); + + ret = asn1_ber_decoder(&rsaprivkey_decoder, ctx, key, key_len); if (ret < 0) goto error; + if (!rsa_key->n || !rsa_key->e || !rsa_key->d) { + /* Invalid key provided */ + ret = -EINVAL; + goto error; + } + return 0; error: - free_mpis(rsa_key); + rsa_free_key(ctx->dev, rsa_key); return ret; } EXPORT_SYMBOL_GPL(rsa_parse_priv_key); + +void rsa_free_mpi_key(struct rsa_mpi_key *key) +{ + mpi_free(key->n); + mpi_free(key->e); + mpi_free(key->d); + key->n = NULL; + key->e = NULL; + key->d = NULL; +} +EXPORT_SYMBOL_GPL(rsa_free_mpi_key); diff --git a/include/crypto/internal/rsa.h b/include/crypto/internal/rsa.h index c7585bd..a0a7431 100644 --- a/include/crypto/internal/rsa.h +++ b/include/crypto/internal/rsa.h @@ -14,19 +14,52 @@ #define _RSA_HELPER_ #include +/** + * rsa_key - RSA key structure + * @n : RSA modulus raw byte stream + * @e : RSA public exponent raw byte stream + * @d : RSA private exponent raw byte stream + * @dma_n : DMA address of RSA modulus + * @dma_e : DMA address of RSA public exponent + * @dma_d : DMA address of RSA private exponent + * @n_sz : length in bytes of RSA modulus n + * @e_sz : length in bytes of RSA public exponent + * @coherent : set true to enforce software coherency for all key members + * @flags : gfp_t key allocation flags + */ struct rsa_key { + u8 *n; + u8 *e; + u8 *d; + dma_addr_t dma_n; + dma_addr_t dma_e; + dma_addr_t dma_d; + size_t n_sz; + size_t e_sz; + bool coherent; + gfp_t flags; +}; + +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_ctx { + struct rsa_key key; + struct rsa_mpi_key mpi_key; + struct device *dev; +}; + +void rsa_free_key(struct device *dev, struct rsa_key *key); -int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key, +int rsa_parse_pub_key(struct rsa_ctx *ctx, const void *key, + unsigned int key_len); +int rsa_parse_priv_key(struct rsa_ctx *ctx, const void *key, unsigned int key_len); -void rsa_free_key(struct rsa_key *rsa_key); +void rsa_free_mpi_key(struct rsa_mpi_key *key); extern struct crypto_template rsa_pkcs1pad_tmpl; #endif