Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2940297pxk; Mon, 21 Sep 2020 00:34:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTvrs4YVoG+5J6TJqyoeJlhZU/AnnURYlD4U4BxjHXMGXXJQQSRnayh7kmzGGVxS6eHe3m X-Received: by 2002:a50:e44b:: with SMTP id e11mr51806210edm.73.1600673646952; Mon, 21 Sep 2020 00:34:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1600673646; cv=pass; d=google.com; s=arc-20160816; b=Y3HOlgHbPBjZMtCahtjkQrYSHTkK/41iBW85AYCSzHOMnD1qNnau1mizSMdSZG3O0k WYZMVwVdEvyfqtKwpLyYfvYpjnBX9uVyNFCbSxWKgATXBkC1LbZlYuHI77bWKsoVl/nJ Y2M/8uPZM9J+5/FszU8sN8Npvkt8jgDcd83i3jCOrZ17xzt1vws28LESNoic0hgP7L1h TI25EpeD08I7gKQZnqveAfMiprrlLsiQr/HoeUC/e7UtOLPBlB78KX/GK0BQXU0cSl2r zoc4aFzKZsFV6eeZ/bHG9Uq8Wfr8MPFsMZVv4eX/YikjBWPRIz0/oNAORGyhNYW8mLkw nHbg== 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=wV5Jeqec6a6GKJwdyGstsaj3hp1Fvs3Dx6RmSZmBRQE=; b=EtxegBkLurah2/E5zKIYdIHni6h7HcpATVPou+2TKYfA7rTqj3dH9SRZZQaHG6lJm4 H2hbZbLLJBzXakA76b5JMWtO9vqx41omN5nqeJ8ClBIyuR2G7lg72UwhDX04oqtrxmua 6MPii0J5B2ld+qKheCMESM6ZicICZvFDafiWV70lZ1dU2PmONhE0TmXRnp6ZqxMcrwXZ BCe0vMnHpxgEL/UA7Hl79skEogiRDgA7PLs0MoyXUdaa/Q3szGYzWAryR9oyHLt/UIDO WOcEB579/K0vn5CkcEfu276umBUK0PCoNI9RlgQPUU7a6TLxls2ZucRpXcWK+q9SOyyC MEkA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@NXP1.onmicrosoft.com header.s=selector2-NXP1-onmicrosoft-com header.b=MGjmJXrN; 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-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 q6si7762458edw.446.2020.09.21.00.33.44; Mon, 21 Sep 2020 00:34:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=MGjmJXrN; 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-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 S1726563AbgIUHcc (ORCPT + 99 others); Mon, 21 Sep 2020 03:32:32 -0400 Received: from mail-eopbgr60075.outbound.protection.outlook.com ([40.107.6.75]:47332 "EHLO EUR04-DB3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726513AbgIUHc3 (ORCPT ); Mon, 21 Sep 2020 03:32:29 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jo3Yev+wo5CH05G3qPIPgy/ABGQR1BT2xS7lai5ihD8hmPkhUO3SH4XO9LotChJG370jm75fxz6+BQRFMfR6VLwXzVhlDZRi9b8N7DHZVMq+CCUa1HtxzgAKdEturAS9jVPTlBzARobxZbNuDiVKhPr7EsnFEj20QlBuACMSYP0OjVp2CgeTzVs+KXGTIoiDHaHJbKg1xOADiN0U7ZeCPaq8Y4dJDLe3xySgj8m7z4MOv3XqAg+5WTi6tio7yZjdsD5HUIPrZFr3KzZbpTIVbB8CAjHguyWaEEIuI2efTCfqchkzkvJ1UpEYJbazvBgzVX4heEFEQxyMWrL53ytM+w== 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=wV5Jeqec6a6GKJwdyGstsaj3hp1Fvs3Dx6RmSZmBRQE=; b=GvbUURqI29CLXHmzSVWT/kHOScRKB8usW2MOGQIg7MYEu/TqUeCz2VK2u1tQ8BqgazWOtMPYx+AS5khlLJssXCeRpnw2+sJjegMinMS396yKKDrhiwvjwFgaKAKCQWMw7DhzllkBI22TnRqruw7bUZEGBQR1difM54B2KZ+oK8/GZlIwPFp3BuyPnf/ftNqlakl/hOA2rbCAJ1/+C+8tMOVT06AvIjws1qCDRzb5zcRb4w9jSrNSmcvOxsgKrnA2qd9KUij0MYoGXormFJf0JCwzoDHZAtqrjIwWwyrFhKQ3W35XOXKHbc2WXovfLGaxDNgRWzBfzCwNtF8nJOxMjQ== 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=wV5Jeqec6a6GKJwdyGstsaj3hp1Fvs3Dx6RmSZmBRQE=; b=MGjmJXrNSpdpxU4MY2ldw/V+LrrhxZcq7AYAJUXd4+GgvB+3WToKD+swSRGbq33gPr1A6G+UQ6RW/GfFaj3IA+WYEOWQbpL5rRpozN5/k08Gjqa7AS88tJKlLq+eTEuUMe8WUlNLX+1G9nwRjF5LX52dDtdoaKmgndV1qSr2A1w= 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 VI1PR0402MB3903.eurprd04.prod.outlook.com (2603:10a6:803:21::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3391.15; Mon, 21 Sep 2020 07:32:23 +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.011; Mon, 21 Sep 2020 07:32:23 +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 v2 02/12] crypto: caam/qi - add fallback for XTS with more than 8B IV Date: Mon, 21 Sep 2020 10:31:55 +0300 Message-Id: <20200921073205.24742-3-andrei.botila@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200921073205.24742-1-andrei.botila@oss.nxp.com> References: <20200921073205.24742-1-andrei.botila@oss.nxp.com> Content-Type: text/plain X-ClientProxiedBy: AM0PR02CA0016.eurprd02.prod.outlook.com (2603:10a6:208:3e::29) 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 AM0PR02CA0016.eurprd02.prod.outlook.com (2603:10a6:208:3e::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3391.13 via Frontend Transport; Mon, 21 Sep 2020 07:32:23 +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: 8651a0bb-7862-4102-1b88-08d85e007b64 X-MS-TrafficTypeDiagnostic: VI1PR0402MB3903: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3631; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4kg+Ve7goSFuucjxG7RoqTRH0b6bmY4Dkp/Q09oNn9LC58/9o8RUhs8zWuVHnc6JP0jdZtIe2CdSaE+PXmZoe0cY6PBaTDGFvJoh/s0m99iaLi9cJtlXnek/z/wagFZvRV1lumriK9rUJLMAUp5sXBHZ19lZRL6gpmhS2Da0q25Z8ZyAbYDX3xtBFJ02F5Z7PTi+kIl89PaR5c9tH9Tbxg//Zu5tEdkwJet9yU3MAZo/NYCdy7OKXIC5/pY9PJM5qI0BGUL4WSfjclgpeEtFH0F53UHHXvx8oG00pMohyoX1PGOV/x7NXMM9t1/GEGS0xhq/yS7xj8a3KzGezQ9i2tf/jaAyJR6cplAsxq3+71yrYR+8YF9MV1XY/XdyXEuQXw80EUslymDJdnh+cNGT1Dgf4xGMP+zr6QUAY+fnUJc= 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)(366004)(376002)(39860400002)(136003)(346002)(396003)(66476007)(66556008)(2906002)(4326008)(44832011)(956004)(2616005)(6512007)(1076003)(8936002)(8676002)(6666004)(52116002)(66946007)(478600001)(316002)(5660300002)(6506007)(6486002)(83380400001)(186003)(16526019)(26005)(86362001)(110136005)(309714004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: h5a1t9ImLdGqMRXM6sASYscNJ2VshEFrLyGkC6SeF8x7T5qTDes9uwB4oHOkmbnoraCAXdIdEDaseplj6oPJGo5QZREqJAkcaCO2sJuhbS31uU+XYV+jAxenhOhG0Bxikj1XlrFMTli350p3U9TgThNvUXQz1MtRM26KMBf4JpUMTQ5ba/C2OFfIwqzXu0ADWth3/06NeceHuWJZ13351+aPetEQeFQBWWC9jy9o88egQfeeLHU3CtGPrDB7Ktjbv8Q020jEjSq3353h0rauDGai3EnFKa0zIVLbSXNW8VkpTB26CEcG4UlwFM4uiw+NnXVqSL5IWWTPkZUArlSnRw+fH6WuWJ3lUDjcsRLM+e1sbS64Zl0cqaJJ2EXeXseia67niscsdZa9guIIKB+Sd1/N9YAU8+uLQGUjtwJ8RcYI3DfhItl+DjHaJs47XQt7ngjTV+hG6Y7nyo0EtGKJ20ksRirdp/91VjcumKtrt8nqRAWvqhMONqnVdm+C5tUVl7PAe+lhbhml2myrOWeO2F203uhPEzQ3eKiOPjV0fcDthwC2CjwKL4qON6FeL6KdXSQzhWRbRD6RFqwQXpzqbpajgqoA9jPGtU/5MMvezRzrlPPQPPtq034D6JdjWJ23QSZhazko/uD0QEF4jm0E+Q== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8651a0bb-7862-4102-1b88-08d85e007b64 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6608.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2020 07:32:23.8313 (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: FZKjS5pdgiOaRlxG1mlJVvF8BC/Ly2MnV//fhsSkSvHCyNxH2oSsLfVJeS8gOHnteQXe+cm8eE1ClF3CgUSlzg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0402MB3903 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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: b189817cf789 ("crypto: caam/qi - add ablkcipher and authenc algorithms") Cc: # v4.12+ Signed-off-by: Andrei Botila --- drivers/crypto/caam/Kconfig | 1 + drivers/crypto/caam/caamalg_qi.c | 70 +++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/drivers/crypto/caam/Kconfig b/drivers/crypto/caam/Kconfig index dfeaad8dfe81..8169e6cd04e6 100644 --- a/drivers/crypto/caam/Kconfig +++ b/drivers/crypto/caam/Kconfig @@ -115,6 +115,7 @@ config CRYPTO_DEV_FSL_CAAM_CRYPTO_API_QI select CRYPTO_AUTHENC select CRYPTO_SKCIPHER select CRYPTO_DES + select CRYPTO_XTS help Selecting this will use CAAM Queue Interface (QI) for sending & receiving crypto jobs to/from CAAM. This gives better performance diff --git a/drivers/crypto/caam/caamalg_qi.c b/drivers/crypto/caam/caamalg_qi.c index bb1c0106a95c..2b5e694fdaf6 100644 --- a/drivers/crypto/caam/caamalg_qi.c +++ b/drivers/crypto/caam/caamalg_qi.c @@ -18,6 +18,7 @@ #include "qi.h" #include "jr.h" #include "caamalg_desc.h" +#include /* * crypto alg @@ -67,6 +68,11 @@ struct caam_ctx { struct device *qidev; spinlock_t lock; /* Protects multiple init of driver context */ struct caam_drv_ctx *drv_ctx[NUM_OP]; + struct crypto_skcipher *fallback; +}; + +struct caam_skcipher_req_ctx { + struct skcipher_request fallback_req; }; static int aead_set_sh_desc(struct crypto_aead *aead) @@ -726,12 +732,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; int ret = 0; + 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; @@ -1373,6 +1384,17 @@ static struct skcipher_edesc *skcipher_edesc_alloc(struct skcipher_request *req, return edesc; } +static bool xts_skcipher_ivsize(struct skcipher_request *req) +{ + struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); + unsigned int ivsize = crypto_skcipher_ivsize(skcipher); + u64 size = 0; + + size = get_unaligned((u64 *)(req->iv + (ivsize / 2))); + + return !!size; +} + static inline int skcipher_crypt(struct skcipher_request *req, bool encrypt) { struct skcipher_edesc *edesc; @@ -1383,6 +1405,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); + } + if (unlikely(caam_congested)) return -EAGAIN; @@ -1507,6 +1544,7 @@ static struct caam_skcipher_alg driver_algs[] = { .base = { .cra_name = "xts(aes)", .cra_driver_name = "xts-aes-caam-qi", + .cra_flags = CRYPTO_ALG_NEED_FALLBACK, .cra_blocksize = AES_BLOCK_SIZE, }, .setkey = xts_skcipher_setkey, @@ -2440,9 +2478,27 @@ static int caam_cra_init(struct crypto_skcipher *tfm) struct skcipher_alg *alg = crypto_skcipher_alg(tfm); struct caam_skcipher_alg *caam_alg = container_of(alg, typeof(*caam_alg), skcipher); + struct caam_ctx *ctx = crypto_skcipher_ctx(tfm); + u32 alg_aai = caam_alg->caam.class1_alg_type & OP_ALG_AAI_MASK; + + 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); + } - return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam, - false); + ctx->fallback = fallback; + crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx) + + crypto_skcipher_reqsize(fallback)); + } + + return caam_init_common(ctx, &caam_alg->caam, false); } static int caam_aead_init(struct crypto_aead *tfm) @@ -2468,7 +2524,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) @@ -2502,8 +2562,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