Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp4047999pxk; Tue, 22 Sep 2020 09:04:48 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwD50iMnLMrZB/LNZcZVlttVxV/NihPHqxUb9FjriHnx45zcwWFAzMzkCSiBfE+1wYoJvXk X-Received: by 2002:aa7:dad8:: with SMTP id x24mr4716167eds.19.1600790688203; Tue, 22 Sep 2020 09:04:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1600790688; cv=pass; d=google.com; s=arc-20160816; b=g+Cc2NIB0asK60+dFK7GcemR8a5nVsR7qGPJQ6GOFzRX/h0AqCUb6XEFSyYDXOYutF LhnESYJrtAMnzGcXkQNAC25PiDkh5jPocmR+tkp1OPYNSrQnhZgp5fytcgdZr9PsYneY JmHWhBhv8gXEgpASySq2Ex6OE4Bht6g/XIik0ZB83YauQEu0JHOuuu9Iusm8MsJujj4X vd6MvlNOVvYVkoDpGV4wvuUqehX5oabX29CMOeC48zd2m8w40C351hv5PxDvOP3Br8aP ZIvk9S9V470QgkqzulKl9fqh69i5zvn7DtpIZVFwx1Lw/49BDPazjFcLwG0+Z8o3PK8N wmWQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=G9IswZaaECesAyJAPZilmBumSFjPTWEh7lQ3za6oqHQ=; b=nlFvhAKUzRzvZdwrMkf/h8Bq9vy5VtzKGzeHSNlR8IKeRKYxexYq7DdIkg/SJ4ubNo 5QEOnX0s06J7mi6/Ie5Bl6LUqCzRLFpHEOgCONeWTpzIqZidiBTQZ2ud1HgYjGibGcfG 8EkYFnmM/CBLOU/G356ENsXT252G3sSQqtYtRXbIeiPpu1bq+ifnTqxcTFTIGw05jvS1 W9S4A66myBcekKbERfV5hRmfY+ygcQZpxAf35OCh/h07VY9isBysSkDBAkgW6rdj01WN kgJ5TfzxEMXcEz+ZpKUGBZ4KFN6adwtBdMUkDQPZh6vPrT2cQZtomX+KWGLzJqmEhyxe UxfQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@NXP1.onmicrosoft.com header.s=selector2-NXP1-onmicrosoft-com header.b=PLJeQv7P; arc=pass (i=1 spf=pass spfdomain=oss.nxp.com dkim=pass dkdomain=oss.nxp.com dmarc=pass fromdomain=oss.nxp.com); spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si10892315edn.26.2020.09.22.09.04.23; Tue, 22 Sep 2020 09:04:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@NXP1.onmicrosoft.com header.s=selector2-NXP1-onmicrosoft-com header.b=PLJeQv7P; arc=pass (i=1 spf=pass spfdomain=oss.nxp.com dkim=pass dkdomain=oss.nxp.com dmarc=pass fromdomain=oss.nxp.com); spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726623AbgIVQEH (ORCPT + 99 others); Tue, 22 Sep 2020 12:04:07 -0400 Received: from mail-eopbgr60083.outbound.protection.outlook.com ([40.107.6.83]:40665 "EHLO EUR04-DB3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726586AbgIVQEG (ORCPT ); Tue, 22 Sep 2020 12:04:06 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SHb2DASt23ZLFKeyuTwh+lSVOk8+p38r/GTehayBRIGB2CwblxSczvkgriCjNvBsWxK99uunN4LkDwsXI9uKxze7NfcDZaa2F0fUrBAzwdnn4udTxVKzllDV9bcq9yc0QnqLsnAdbL7TJZuSLniXsz62jxJTu8fPiqYMG83x7drEe9k1Pmnno6MlbrcMLQ1RUqBP8HQ7EdYdnryhrIpVasmujDOIWZdxtDUlA36bABdNIVKFQ/NZc7aLFNm09USCjI8wt2FLbAwxtRpbwcCpJSo412wAsNfPFDT8CrRRelD/UtR0YWLxBzjVZxyN6zAOnruPkRNxiyXcerDd8hu+MQ== 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=G9IswZaaECesAyJAPZilmBumSFjPTWEh7lQ3za6oqHQ=; b=IofoAx3z8uxEHMp4WclU888CUF5ZbgmuN/Soa2+DJsachfuGS+OOeJCNxdWusIJqBEQQRJq0k1h5S9ywl6rlOl0vPmsVzl8Q/qZ3O3oKs0Su54DhvLcwpjtvvIE6L3mtGwrU0jyssUOrCdQGgMQRzZL/peD52go3QBQhyitbH5V/3+DcQiEpuJ6pxpLkmDPaMXuiKbhC2c4eOLq/rgQLChRk3pUV3KEs/UZgyc+NS2htmLmeTKIZXv0M84kw65od7mrdNgxt9+DcIbCIb746koxLUWwM3uVrOOeUSCD7npveyYC5e+4ngSsv9m6u4KjSkoLgauIrvGBknO8LxpJpjA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector2-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=G9IswZaaECesAyJAPZilmBumSFjPTWEh7lQ3za6oqHQ=; b=PLJeQv7Pkx/86u8CsUhiqa+k6l+tZmLgcaxw4Yj5T+25lOblocoVZRDECLfAFuWEBCCkTdFGTW4KVifS30vFJp6vEMdAtnYbby3pWMu4pMLL3XL7AVHLamKNMyJxo0iyybH6DVqkKh/WCQlnUFvVbDRk7dn6xmo+uZsm7DLnQoI= Authentication-Results: nxp.com; dkim=none (message not signed) header.d=none;nxp.com; dmarc=none action=none header.from=oss.nxp.com; Received: from VE1PR04MB6608.eurprd04.prod.outlook.com (2603:10a6:803:125::12) by VI1PR04MB7072.eurprd04.prod.outlook.com (2603:10a6:800:12c::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3370.16; Tue, 22 Sep 2020 16:04:00 +0000 Received: from VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::34b6:beed:6762:a67c]) by VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::34b6:beed:6762:a67c%7]) with mapi id 15.20.3391.027; Tue, 22 Sep 2020 16:04:00 +0000 From: Andrei Botila To: Horia Geanta , Aymen Sghaier , Herbert Xu , "David S. Miller" Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 01/10] crypto: caam/jr - add fallback for XTS with more than 8B IV Date: Tue, 22 Sep 2020 19:03:19 +0300 Message-Id: <20200922160328.28926-2-andrei.botila@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200922160328.28926-1-andrei.botila@oss.nxp.com> References: <20200922160328.28926-1-andrei.botila@oss.nxp.com> Content-Type: text/plain X-ClientProxiedBy: AM4PR0302CA0022.eurprd03.prod.outlook.com (2603:10a6:205:2::35) To VE1PR04MB6608.eurprd04.prod.outlook.com (2603:10a6:803:125::12) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from lsv15007.swis.ro-buh01.nxp.com (83.217.231.2) by AM4PR0302CA0022.eurprd03.prod.outlook.com (2603:10a6:205:2::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3391.13 via Frontend Transport; Tue, 22 Sep 2020 16:03:59 +0000 X-Mailer: git-send-email 2.17.1 X-Originating-IP: [83.217.231.2] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: fc5000a8-eb89-4e8f-a3d1-08d85f111e50 X-MS-TrafficTypeDiagnostic: VI1PR04MB7072: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nz4ZX4p/dfHnZiBbZXCerT/JcaxfgDDE6OIkzl2HrakCyuK3VMCjPjIwk5I12IyPhNLuNIQxcHJ0DO1pVeGaaosw8TLkJI1thki+h2/z0ZoachGEtIFjDdSlPgMsZBm7yoLJQPBvhgczPwiZ7mLQslFGON6j7Om1NamabFhai1b7C2jtb3wgLpy0UXIObaEUyIddzwWJjvOrdiqvuFiTfiwxq71JsN/nye8vrYUN0mOmvLd0kXFH/LAdRNvYCf3rx8RcMuPK3ns1Ayj6UoP9kiVJCvCfVDKLpBuNWIxIPO7Fp1SQuq8UngRLcC4/r474wD8cewHigFXTt/1cAtErB4zI92mm7V+br43L+8W6LXhoUhG/yMcHcWK4YkNckzfg X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VE1PR04MB6608.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(376002)(366004)(346002)(39860400002)(136003)(1076003)(4326008)(2616005)(6512007)(83380400001)(186003)(16526019)(2906002)(956004)(5660300002)(66556008)(66476007)(8936002)(316002)(6506007)(44832011)(66946007)(52116002)(6486002)(478600001)(110136005)(8676002)(26005)(86362001)(6666004)(309714004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: jnvEX1axivGDzgGlX10JrGG6EfklCEAyyuu+4iULAAOllTZ3k0kCPGU8Q94MNKvosLnXIDY5BGLhsDg0XvTtCWXsH5+idHu0VLbltnuhbjg3sLNG7wX29xQnnYm3c6mD0vBxU7YFaGz4ZqNnknSKbAblE7HjNUtPw+ZWbfV4983svs11CtaVwH/9U3PHwpsy7ilKmh7NChTdth0TP+MzIo5BJnZ98qHixBK960SvMIqO3erSsU0otboJc3tSUL/cFoZvBIVEKp5WOTollHBMn1U+P3TXva4dr9Llriqu4mm/KFTpJYwHC9HoCrFZBF/P6iE9IQtkDF1shzEeqmi5KLvJ80Foi20KNe6wrsZz9/SYXT+vwk/ge10XBgvnkut8va1FtngMv/8jm/nDgEZ6/oUUeO5zl42ccgI5p6vbgKBsdnFgnHfpP0vlY2uMM65k/S7JRy5Ma7pMwk+dqHrbxYGzdgKQBClVUJWtCxsJHbTObeL9Te3g2XCWtWAoy61qauMbc1xpCdS5d7h1eeQLiSBDn3aRrKzFkdY6dFLElqZ5X0GuksMNI+qxQ14+wNfFg6PcP79KYnoDauOfpC5PT9GeI6a12x+knq+Al6fFhDCyqNCeHQg1nfRHVpCs8J5Lz8JNxyLF/Tw4O4ronCLy2w== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: fc5000a8-eb89-4e8f-a3d1-08d85f111e50 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6608.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Sep 2020 16:04:00.2578 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: i2E+/H1dgLdVFUUR12QI4peK0OHzSgvvv9q2uoNoLkC+JoemP2fdEPloaiUdvfYJBdOotq0dmWLd5m5UW6VVeA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR04MB7072 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Andrei Botila A hardware limitation exists for CAAM until Era 9 which restricts the accelerator to IVs with only 8 bytes. When CAAM has a lower era a fallback is necessary to process 16 bytes IV. Fixes: c6415a6016bf ("crypto: caam - add support for acipher xts(aes)") Cc: # v4.4+ Signed-off-by: Andrei Botila --- drivers/crypto/caam/Kconfig | 1 + drivers/crypto/caam/caamalg.c | 72 +++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig index bc35aa0ec07a..dfeaad8dfe81 100644 --- a/drivers/crypto/caam/Kconfig +++ b/drivers/crypto/caam/Kconfig @@ -101,6 +101,7 @@ config CRYPTO_DEV_FSL_CAAM_CRYPTO_API select CRYPTO_AUTHENC select CRYPTO_SKCIPHER select CRYPTO_LIB_DES + select CRYPTO_XTS help Selecting this will offload crypto for users of the scatterlist crypto API (such as the linux native IPSec diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index 91feda5b63f6..adb2c05a8bde 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -57,6 +57,7 @@ #include "key_gen.h" #include "caamalg_desc.h" #include +#include /* * crypto alg @@ -114,10 +115,12 @@ struct caam_ctx { struct alginfo adata; struct alginfo cdata; unsigned int authsize; + struct crypto_skcipher *fallback; }; struct caam_skcipher_req_ctx { struct skcipher_edesc *edesc; + struct skcipher_request fallback_req; }; struct caam_aead_req_ctx { @@ -830,12 +833,17 @@ static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher); struct device *jrdev = ctx->jrdev; u32 *desc; + int err; if (keylen != 2 * AES_MIN_KEY_SIZE && keylen != 2 * AES_MAX_KEY_SIZE) { dev_dbg(jrdev, "key size mismatch\n"); return -EINVAL; } + err = crypto_skcipher_setkey(ctx->fallback, key, keylen); + if (err) + return err; + ctx->cdata.keylen = keylen; ctx->cdata.key_virt = key; ctx->cdata.key_inline = true; @@ -1755,6 +1763,14 @@ static int skcipher_do_one_req(struct crypto_engine *engine, void *areq) return ret; } +static inline bool xts_skcipher_ivsize(struct skcipher_request *req) +{ + struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); + unsigned int ivsize = crypto_skcipher_ivsize(skcipher); + + return !!get_unaligned((u64 *)(req->iv + (ivsize / 2))); +} + static inline int skcipher_crypt(struct skcipher_request *req, bool encrypt) { struct skcipher_edesc *edesc; @@ -1768,6 +1784,21 @@ static inline int skcipher_crypt(struct skcipher_request *req, bool encrypt) if (!req->cryptlen) return 0; + if (ctx->fallback && xts_skcipher_ivsize(req)) { + struct caam_skcipher_req_ctx *rctx = skcipher_request_ctx(req); + + skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback); + skcipher_request_set_callback(&rctx->fallback_req, + req->base.flags, + req->base.complete, + req->base.data); + skcipher_request_set_crypt(&rctx->fallback_req, req->src, + req->dst, req->cryptlen, req->iv); + + return encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) : + crypto_skcipher_decrypt(&rctx->fallback_req); + } + /* allocate extended descriptor */ edesc = skcipher_edesc_alloc(req, DESC_JOB_IO_LEN * CAAM_CMD_SZ); if (IS_ERR(edesc)) @@ -1905,6 +1936,7 @@ static struct caam_skcipher_alg driver_algs[] = { .base = { .cra_name = "xts(aes)", .cra_driver_name = "xts-aes-caam", + .cra_flags = CRYPTO_ALG_NEED_FALLBACK, .cra_blocksize = AES_BLOCK_SIZE, }, .setkey = xts_skcipher_setkey, @@ -3344,13 +3376,35 @@ static int caam_cra_init(struct crypto_skcipher *tfm) struct caam_skcipher_alg *caam_alg = container_of(alg, typeof(*caam_alg), skcipher); struct caam_ctx *ctx = crypto_skcipher_ctx(tfm); - - crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx)); + u32 alg_aai = caam_alg->caam.class1_alg_type & OP_ALG_AAI_MASK; + int ret = 0; ctx->enginectx.op.do_one_request = skcipher_do_one_req; - return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam, - false); + if (alg_aai == OP_ALG_AAI_XTS) { + const char *tfm_name = crypto_tfm_alg_name(&tfm->base); + struct crypto_skcipher *fallback; + + fallback = crypto_alloc_skcipher(tfm_name, 0, + CRYPTO_ALG_NEED_FALLBACK); + if (IS_ERR(fallback)) { + dev_err(ctx->jrdev, "Failed to allocate %s fallback: %ld\n", + tfm_name, PTR_ERR(fallback)); + return PTR_ERR(fallback); + } + + ctx->fallback = fallback; + crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx) + + crypto_skcipher_reqsize(fallback)); + } else { + crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx)); + } + + ret = caam_init_common(ctx, &caam_alg->caam, false); + if (ret && ctx->fallback) + crypto_free_skcipher(ctx->fallback); + + return ret; } static int caam_aead_init(struct crypto_aead *tfm) @@ -3378,7 +3432,11 @@ static void caam_exit_common(struct caam_ctx *ctx) static void caam_cra_exit(struct crypto_skcipher *tfm) { - caam_exit_common(crypto_skcipher_ctx(tfm)); + struct caam_ctx *ctx = crypto_skcipher_ctx(tfm); + + if (ctx->fallback) + crypto_free_skcipher(ctx->fallback); + caam_exit_common(ctx); } static void caam_aead_exit(struct crypto_aead *tfm) @@ -3412,8 +3470,8 @@ static void caam_skcipher_alg_init(struct caam_skcipher_alg *t_alg) alg->base.cra_module = THIS_MODULE; alg->base.cra_priority = CAAM_CRA_PRIORITY; alg->base.cra_ctxsize = sizeof(struct caam_ctx); - alg->base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | - CRYPTO_ALG_KERN_DRIVER_ONLY; + alg->base.cra_flags |= (CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY | + CRYPTO_ALG_KERN_DRIVER_ONLY); alg->init = caam_cra_init; alg->exit = caam_cra_exit; -- 2.17.1