From patchwork Thu Aug 6 11:41:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Botila X-Patchwork-Id: 253200 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=-10.2 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, UNWANTED_LANGUAGE_BODY, 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 65BFEC433DF for ; Thu, 6 Aug 2020 17:08:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B18B020786 for ; Thu, 6 Aug 2020 17:08:43 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="gjrdsm2Z" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729935AbgHFRIW (ORCPT ); Thu, 6 Aug 2020 13:08:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729610AbgHFRGe (ORCPT ); Thu, 6 Aug 2020 13:06:34 -0400 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on20601.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e1b::601]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B42B0C0D941B; Thu, 6 Aug 2020 08:43:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=COqCVjVaLaRxygQQU0cl/j85ZcQiT/+O4amCXDk+nUc90yIUCBMVu7kZ7yTLSNYxjVB9dw4aOwvWGUtZniAbEeIFKwcGGlDWOouDvOrIRgPd2TL5oakF5dUued2Gx0ZQlIvZDz3rSphu3kojbWW2/ngpjIb+GMj0GsmGUq7BdpnnKCA4AFfZPDc6ZgojTbNN18BFdtKEN7D1ABFSyCgqWzZBfRRwWPxktN8jx42+/R7IdtrXsTTvM04YUB7NZjfv2FG7fe1sYxIMhpvT6UozHfj2nKhqricjrPCQVnrTeAusJRn+eZQ4P7kXaMdMzNWp50++cxOlNyPVoO+1v+xzNQ== 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=rUOaFR+51/hMGHZ1OqrVUqDtVfGLQpzNtoWYvUv9n6w=; b=G00rQj5SdKLJQENNUgM3uSMoUsQRGyZ052+BXq9lDeefFYea0WHoRMCk+PML9TtneW/GxlSsAoD6/KoQg9/Na7ca3ZEIGcA03XZl1GcCbfv+mnlb8Zdphkom/FN/XqrlmPT1dc5Pm6FiYSiBEjqWSTkWviiwuo0h2ZQKpOZFt/0zfxliusUeb0WFDUpcw7oRB1yVVDoG5fmGwqxTpQJxW7R0jJSymwAI7b8JrrNb1m1Hh7cigqmg5UYuIfwRibLBurQWhSYFmGdaguckCYn2gFKPaj9nqjKknEcn4UFcHIyJ2ILHVfuFl0HUKFydApKK1bj5O3r/gO027NAfc38Eqg== 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=rUOaFR+51/hMGHZ1OqrVUqDtVfGLQpzNtoWYvUv9n6w=; b=gjrdsm2ZPet2qX0EIqPzUH69K5ptFCyIfmBg+tZ+BSmw+vg7l4DIHuYgZpIxooaU913C6FG5Hajoe7/DKroGlVFu19PtSVk1r5QOLZvLIExlc3EfahOArHNmDUc/QSbB5FC6sR393xIk/tiLLEK91zzKv81XgoRoR0YeWU72wh0= 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 VI1PR0402MB3440.eurprd04.prod.outlook.com (2603:10a6:803:6::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.19; Thu, 6 Aug 2020 11:41:50 +0000 Received: from VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d]) by VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d%6]) with mapi id 15.20.3261.019; Thu, 6 Aug 2020 11:41:50 +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 1/9] crypto: caam/jr - add fallback for XTS with more than 8B IV Date: Thu, 6 Aug 2020 14:41:19 +0300 Message-Id: <20200806114127.8650-2-andrei.botila@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200806114127.8650-1-andrei.botila@oss.nxp.com> References: <20200806114127.8650-1-andrei.botila@oss.nxp.com> X-ClientProxiedBy: AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) 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 AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.18 via Frontend Transport; Thu, 6 Aug 2020 11:41:50 +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: a3c44d73-21cc-46d2-98a5-08d839fdb577 X-MS-TrafficTypeDiagnostic: VI1PR0402MB3440: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2887; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5XQG7rfJNRK7SuLZjcrbro65L8hm+qoxQd7KicAEuJMWILnA13DbjNpiVijKu1ae+1ElxEAhwcA5eAwmCuMkQe/oJo6e5KUssQVtz7tFdFgnBLf7KWCRRQnJMNGLD4DTQlGgP0e8CeIbT4KnHZkA/5Qdz5nVvsOSi1b35GV1kVyoEjD2vdyzH5N+9eRqh6DpNkH7LKjIKGjTkyUOORENTgABc1nskRvSZCkKGiZN8prkWghAwCVHKBzC+JpWUmrT9p+UNSZUbNmvLIWH7H7+6iQaDRb0bHS8e7IA3O1F4P1Dks90t2i39NITCP0K6RtXvO1I5vUJI9XhVq8Hib/grCop4PEP1YpbEA1ycemdK24rSlHVapahEmjJKPGScyBI 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; SFTY:; SFS:(4636009)(366004)(396003)(376002)(39860400002)(346002)(136003)(110136005)(316002)(6666004)(6512007)(478600001)(956004)(2616005)(6486002)(4326008)(1076003)(44832011)(52116002)(6506007)(86362001)(186003)(26005)(16526019)(66556008)(66476007)(2906002)(66946007)(83380400001)(8676002)(5660300002)(8936002)(309714004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: inWQFGIeqPitX9KqITvYXTU4MTuVmC58FqJER65MT4HWdLbqA6Lk0/PqwyqaillBNvZlGymC+hH+QINJWJiOOCJ+V7ezexczbDgDiSdMCXAVEZW7xNpQSU8YvUnY16KZNPumw+ayU61U4fzkDEc3sZtwj/oG/7uEiaSUJx+ZJ3OoOQ/2lRsisrL+OPuIBBjY+W3T3xLkv8+OGgmFeUGbm1jGzBkt0QVuETKBSyALOPT5ZshYiMWMQMExgnl/ivl2ggFFvim9rRfCSviLPLowzSZ/I3H/Dq1Miu74/S5VDoSMGZTMPp7GhZjwYkrighdOiwwwXeDxv72zDF1cIEXMtuQ5Frhr/w9Yex7UDzi9nVvOJDzqf2fwrbx2S262VzOnnQBEeOEd/H65NVa7oUx3eGWaUyMjQtLoT9AOjLvsdFI+l7dG8YK0HrB4hpSj8CaiIQQwgIUy8YAdiMpfc2UZRldFmB23XqoNuZy69Ba+XFLYPTg90XVU05p3L13k474/TWQtZKhRKKGFxoZFzsjsdeSn0qu27IhJJ02D3i+w1wNpsuQw8pAUzRNzaNVe0B9GfNtScCviV0ZQhnCgKsEUZjVbfJRj6bkDF3Q4UAheH8o8eKOQLgytoYtxIKYcT5Y81G6AKw1ryX3zWwR/vu+sBA== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: a3c44d73-21cc-46d2-98a5-08d839fdb577 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6608.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2020 11:41:50.8387 (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: OpZfIWz2+Wv4YdflcRdO5itm1IF5wG0lRcyN7cJC1O2ivTJOLt+L71RXyGbSceXUm2IvyrXafpbaFTaF2GzgxA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0402MB3440 Sender: linux-crypto-owner@vger.kernel.org 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/caamalg.c | 68 ++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index 91feda5b63f6..ebf4dc87ca2e 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,20 @@ static int skcipher_do_one_req(struct crypto_engine *engine, void *areq) return ret; } +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; + + if (IS_ALIGNED((unsigned long)req->iv, __alignof__(u64))) + size = *(u64 *)(req->iv + (ivsize / 2)); + else + 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; @@ -1768,6 +1790,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 +1942,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,12 +3382,30 @@ 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); + u32 alg_aai = caam_alg->caam.class1_alg_type & OP_ALG_AAI_MASK; crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_skcipher_req_ctx)); ctx->enginectx.op.do_one_request = skcipher_do_one_req; - return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam, + 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)) { + pr_err("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)); + } + + return caam_init_common(ctx, &caam_alg->caam, false); } @@ -3378,7 +3434,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 +3472,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; From patchwork Thu Aug 6 11:41:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Botila X-Patchwork-Id: 253206 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=-13.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, 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 1913DC433DF for ; Thu, 6 Aug 2020 14:26:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1A3972312B for ; Thu, 6 Aug 2020 14:26:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="UW2ZXsQZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727828AbgHFOYI (ORCPT ); Thu, 6 Aug 2020 10:24:08 -0400 Received: from mail-eopbgr00067.outbound.protection.outlook.com ([40.107.0.67]:20322 "EHLO EUR02-AM5-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726375AbgHFOUT (ORCPT ); Thu, 6 Aug 2020 10:20:19 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EVRFihfmwv6puV6exPglWlB1H4ZIL1nriK+7hBArKvMVgNTauq0DURuBP2ZrwTpmlPQKEmlAQ/CjA4bAG2cR8SHrgHxSEhCulRXElrEk+lNTLy4cn0QaueapYWbjMWUww1pHzG8mtLmjmEmACM41nnlwMIs7ieDxXN6TML60ti17NemWU6eZGyz1xzPuUqplR30+hl6kKbQeVfIXBzkoF9FZewx4PNsmzlzQvcJL7v1KwOo5v/rg2+NsHcAZcdK7DuUYOgEOHZAowdpTZA+Eb5ezWZuPzSIfjHlylR+4OQdeHzPXWuZFM5qxZXrx0WjYw4E6YrpUsSmQ3onJv3EjNg== 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=sDRzb6gtsB5DKKZ1Z/MpbHsUH5Rk8IEXaG8qesk44IQ=; b=WrsIYrQRyfw96MG4fKMWqMAYP8vPmW87zkXtsJ0t0a8Adlb+JM07jW3A5Z3+MmuZ8R1fpqQMZ5dy1oBa+yP32Bhpk81PweBIvUBqQEWAnuCnHPW0P3A7KR9dbPpCAW1fOotdLHoyiQUspfHCNzcCN2VUeA+ziqd6YDQXOaJjoMKMXuUEa3JRx3PahJfbpSXC1/S52lIh5XGgWPjsd8B5XeBzMpd6SI65nkyc0qZ1KWSw7Qe9/com/uPQlzXp9J/t3jgF2UyZLyKMxy5Cn7cIAVuWHfFgyW/qOsEjxvCYtHQN1WdomD2vXArV4WYev0Jt5Sfjd9wz0BwzVA3+H5GM3g== 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=sDRzb6gtsB5DKKZ1Z/MpbHsUH5Rk8IEXaG8qesk44IQ=; b=UW2ZXsQZ5cBaa6xK8Pul5vKMckadpnF66xEpczNbBPP5UFXT4gZPl1FgPMIn4TY+X723PweHVF2bYt8HNI4VIgIByge+pzWHYtQX0UVLfI9YgVQDmw0iC7v2IxL/IFJUQjJvkOfy9gGJ3BpECmQ15ANJ98IUklJAEiJO8pjk7Hs= 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 VI1PR0402MB3440.eurprd04.prod.outlook.com (2603:10a6:803:6::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.19; Thu, 6 Aug 2020 11:41:51 +0000 Received: from VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d]) by VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d%6]) with mapi id 15.20.3261.019; Thu, 6 Aug 2020 11:41:51 +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 2/9] crypto: caam/qi - add fallback for XTS with more than 8B IV Date: Thu, 6 Aug 2020 14:41:20 +0300 Message-Id: <20200806114127.8650-3-andrei.botila@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200806114127.8650-1-andrei.botila@oss.nxp.com> References: <20200806114127.8650-1-andrei.botila@oss.nxp.com> X-ClientProxiedBy: AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) 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 AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.18 via Frontend Transport; Thu, 6 Aug 2020 11:41:50 +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: 9b5a8478-f459-41e3-8fd7-08d839fdb5f5 X-MS-TrafficTypeDiagnostic: VI1PR0402MB3440: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2887; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1PpB3H/I+wBXdrlK4nUH2BPBE48/43QXld4nkzZ2crb9+iDugMOwvcpuiLrGHmuyzhKSvqxABGTfz5nmbWjLo8/WLylkVpPnGucHreoPGWszzR3J8ca0V8Pm8paL1ucSDmfZPiW+3dRrSPhSMDZReTx3JekLXHXHdyi768M0c3h6riecaB931beEwOe/EZk+lM7fqW/0CMAb+gvmZB2AZoD4h18kN4wAUvC/LtYLON0LkCnKRtBP0/DhiY6P1OiTnYas19LpShicZCZQJQunhRaDPJoAjKHf4Tf+hF1k8uttiRHQciF2AKPGVx2yGQshlR4rPr8ukdUbAGNZzFHC/KYyg7IOecp0HDD8rS6LZatb7UAmV/Xg8WDB2AMzGjqK 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; SFTY:; SFS:(4636009)(366004)(396003)(376002)(39860400002)(346002)(136003)(110136005)(316002)(6666004)(6512007)(478600001)(956004)(2616005)(6486002)(4326008)(1076003)(44832011)(52116002)(6506007)(86362001)(186003)(26005)(16526019)(66556008)(66476007)(2906002)(66946007)(83380400001)(8676002)(5660300002)(8936002)(309714004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: qsTLQOXAvg7TD6gzjUZFI6qD9df5PckzgaUT5sarNHT+KOupB9wucm+s0sqU3MCEimK84c3dyAqHiZmkDzIYYr6LPChjB2oukgARE8ZW0VfwcMt7UBr5bpMMqkjlnp4MrCHHE7OJBrFkh+KHgujuH5mYdALe8SWjBlDdFZKfCy3qae2f0XZGX95pr8Z/apRcegbivtf/u2uorFOgKbSgEaGvaaFbSqkyU1gnjy0+PTHe26Qp4uX5shmmPMbOgNF9wJCPeNtYGBvO36DP+yEo5Lu9TqMIb3WDXWd2qAJSzu5/9VGn3Fs8T41wnuCuS5L9py9SmfmSXGhZgHUDdxd5CmMGqrhb4BRYxaX317J+9nyaq4v65IVaST64yIlHbtz/NHtOtjtSf6VLuF+j6jHCs4rumD/iJBJM0ZHgd7yBzGzll3UdsbXHzut6OelJuCJ4+roA66rwf9TSWFpFTh11upv9H/ZCGMefrwZyXeIvzC0B+WOjkUIoWMsUxaDDu1M22CEi4qs288eTHXUo4+kVBm9kkNXlpp0aGbrTnxon02AmL8Bfoyw8dncLXzwKdN3jptUZe7R5EkLwPfR8LFj3THmkyCYO8k5KCI/6hxjfUkD3ViaWujHnSEPBI49VxWugOEPH1NeITraFWxpq3sdmzA== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9b5a8478-f459-41e3-8fd7-08d839fdb5f5 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6608.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2020 11:41:51.6422 (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: D1fcihcdq98AgG2p81ebMK8n97TckmDy8FA0pQyrxItz3Z4xEcJaituTi8qoXt9N2tmxDOzcwtLRDZUI/LCm7A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0402MB3440 Sender: linux-crypto-owner@vger.kernel.org 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: b189817cf789 ("crypto: caam/qi - add ablkcipher and authenc algorithms") Cc: # v4.12+ Signed-off-by: Andrei Botila --- drivers/crypto/caam/caamalg_qi.c | 73 +++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/drivers/crypto/caam/caamalg_qi.c b/drivers/crypto/caam/caamalg_qi.c index bb1c0106a95c..05cb50561381 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,20 @@ 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; + + if (IS_ALIGNED((unsigned long)req->iv, __alignof__(u64))) + size = *(u64 *)(req->iv + (ivsize / 2)); + else + 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 +1408,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 +1547,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 +2481,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)) { + pr_err("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 skcipher_request) + + crypto_skcipher_reqsize(fallback)); + } - return caam_init_common(crypto_skcipher_ctx(tfm), &caam_alg->caam, - false); + return caam_init_common(ctx, &caam_alg->caam, false); } static int caam_aead_init(struct crypto_aead *tfm) @@ -2468,7 +2527,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 +2565,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; From patchwork Thu Aug 6 11:41:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Botila X-Patchwork-Id: 253201 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=-13.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, 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 29D9FC433E1 for ; Thu, 6 Aug 2020 17:08:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 661A220786 for ; Thu, 6 Aug 2020 17:08:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="ejZMFpcY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730142AbgHFRIX (ORCPT ); Thu, 6 Aug 2020 13:08:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45808 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729577AbgHFRGb (ORCPT ); Thu, 6 Aug 2020 13:06:31 -0400 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on20601.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e1b::601]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F0DCC0D941A; Thu, 6 Aug 2020 08:43:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ody5IIUsvP1JQjQT2v+5+Q9UVmzFMcRhe68HD5EucDSr8GrZKC7uAuGdGVcoBN/yug+1Y65xGx2iT9ETvn4bNKbr9khJOHFkYCwGzlLfV80WDGUyfOrbgJX0xRwjSR2GjXu8kgIJy341CXvTFtmJLhefeUuVem+N+kYusX5WplrbeN1xOTopCElccMuqMKw5XuDbcVK+7fr61pq+IvJAdJqp6yMzvNoMncVUF8pehbuFwbdqApI8uJsvXbFIljtSVWwzeCgOBSTgTjgqfYBXWn469bXp7CFXQPiMEm2OosRBn+hyXVkNsbJcbVbcs/WunHG2dQNKj/4dvaDrqyXY9Q== 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=NG2G62cTP09RqCfsYGO6Dz0qWH/f02540G3LYJKbnCY=; b=VRCr0OvXZPY9rBdTJTELbcEO7mQGAizQPxtnlO1QttN8lfp9OqVmbj5oJzpdiIAbG0uHG9qDsa7RI9Ecbf20kr0bkVyXIOjZlGub1IJQgP4T24Fgu+mB2jHx/kWKi/dEWGzHfQ7lUsiJnrBo1OVF+ZpQvGW7g2NgxKqMTI2Y2GJ1f/YthsN5Ia35kJIGcNU2vaDpM+JeMZs7m0o7l4kYuzEZuqscKdQMpf3aiPRW7tJauzutXuBQxoWRXhwbtSjc1HOhQStn/Pylv7XnqGz3M2x2NBRPg+Y6XS8+UdB7SPFJxVQE+P+WRgR4OB0Ob1tI3ybapnz4Ywc8xr9EuHjteg== 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=NG2G62cTP09RqCfsYGO6Dz0qWH/f02540G3LYJKbnCY=; b=ejZMFpcY3NYhd8A1PYvOsJ5HtwMfyfRk6aWHa7gNV8ufbENwnok1+7j2oZLZd0DlG2LQGvCDQAqpvOszWeCo4/OrMKWBGSVQ3np1eX7k1ywdXVk//okZnMe56SS9Kz7pn1WcwoeJr4W8jTi29eHWVFB4ZRn+GiCVQIHQN/aTJGo= 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 VI1PR0402MB3440.eurprd04.prod.outlook.com (2603:10a6:803:6::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.19; Thu, 6 Aug 2020 11:41:52 +0000 Received: from VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d]) by VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d%6]) with mapi id 15.20.3261.019; Thu, 6 Aug 2020 11:41:52 +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 3/9] crypto: caam/qi2 - add fallback for XTS with more than 8B IV Date: Thu, 6 Aug 2020 14:41:21 +0300 Message-Id: <20200806114127.8650-4-andrei.botila@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200806114127.8650-1-andrei.botila@oss.nxp.com> References: <20200806114127.8650-1-andrei.botila@oss.nxp.com> X-ClientProxiedBy: AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) 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 AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.18 via Frontend Transport; Thu, 6 Aug 2020 11:41:51 +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: 85085e00-6cd1-4c0c-a0f9-08d839fdb670 X-MS-TrafficTypeDiagnostic: VI1PR0402MB3440: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: S3yAXEsBQMDeo3TOPXrkmZuTPI528/THA+tnkZWxb0YBSoMFrmxOS83G+EUXyT7LK1RFBRBfCtPXRk2JG4rl0fr5ORtcbHO84LADcT7GWY/Jk4FiQalIeXJM0RgGxz0ZhB9ofX4nFJL8sh2ZdLWC75I+8Il4jxyIlbQsCh2NND0c2SyvsnXyb+pN/jkJBp4iUo/USijMexVeJjxaqy2KkoUufZMBMQvJl24WlH9UsehB8z46OVUNuGaQnwpjzgqa9TADcwTzLClchXRHMNwYKxX9R2PDd4yu9ebajhvxuERwxRTX/rq4wsK3C6wvsMWjztvuak+aAgzqWTpELQD49dS99vLXeX1Cbm/NyVzbC4koYJoAI8B0rqhJ7S1wfUWe 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; SFTY:; SFS:(4636009)(366004)(396003)(376002)(39860400002)(346002)(136003)(110136005)(316002)(6666004)(6512007)(478600001)(956004)(2616005)(6486002)(4326008)(1076003)(44832011)(52116002)(6506007)(86362001)(186003)(26005)(16526019)(66556008)(66476007)(2906002)(66946007)(83380400001)(8676002)(5660300002)(8936002)(309714004); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 2o5FALxxi50DcCReNdw1BcWFpRAm4zAE1H1LZgVUAQB3V98oUTp+Se47Vf7F4J/W6bJ1/9PcTiCAQDOeRWkOCbiybCNoNJ0PR8W3foGMo0bp1Y7HnX07yCM/bsCMhnM7e386c2szXZ7+FReGw1nMVQm3mYRD/5PnzX0xfXMnvZ4dZplisE2YZ4Oi+JBVpepDtQZGUE8A6I9m7na0lOKQy4CbMJuYqFB2tp0RHTG4+1C8C2+W5rQxMlLhq1dQBqJddOUx81XAQPXa+rPK0mBsfCX1n6dm+XCPBylQaII9Wt2OOPAEmGNKciTlndilRadwXjvbS0fPqLhJ1MVKemp/9BDA4xHt55YKmexhfK3+Bxde66x4iFQsWbDTnOPOF/0jzMVDQApLwysdoyiWmo3Ia1V6Ba5LVPf7nhoy3xGLV7437rlDbTK1UtOwNNBb+RZsw5oZ++zdW8w4jkRqOZj80e+I2nBS1cC7y/ovl3ARIUL04hfx+qzcMaXe0Og6wstXgLcFL2psC5B85N9FpRftFe/spNjDyvq4UHMxp7wVKUNrGMEW/sVpJ6p593YOQxleu4nmWjG0O4zpY6hYvjjmAYPra7CG5GsAItQUcfNQ48oN+Z4sDb9VsSuJNpeKB6JuLsPKzCLBVbTVd7t7cWnLow== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 85085e00-6cd1-4c0c-a0f9-08d839fdb670 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6608.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2020 11:41:52.4578 (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: GGjIqgD9wnvL97MXPjOhHu58Q+/2Rpnk8OgniV8xZK+Bd3CqaaeSUdORpIbB/UvZ2rAAv8mpxzEVQsOT8IS5jg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0402MB3440 Sender: linux-crypto-owner@vger.kernel.org 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: 226853ac3ebe ("crypto: caam/qi2 - add skcipher algorithms") Cc: # v4.20+ Signed-off-by: Andrei Botila --- drivers/crypto/caam/caamalg_qi2.c | 79 +++++++++++++++++++++++++++++-- drivers/crypto/caam/caamalg_qi2.h | 2 + 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/drivers/crypto/caam/caamalg_qi2.c b/drivers/crypto/caam/caamalg_qi2.c index 66ae1d581168..a0b13bf6b528 100644 --- a/drivers/crypto/caam/caamalg_qi2.c +++ b/drivers/crypto/caam/caamalg_qi2.c @@ -19,6 +19,7 @@ #include #include #include +#include #define CAAM_CRA_PRIORITY 2000 @@ -80,6 +81,7 @@ struct caam_ctx { struct alginfo adata; struct alginfo cdata; unsigned int authsize; + struct crypto_skcipher *fallback; }; static void *dpaa2_caam_iova_to_virt(struct dpaa2_caam_priv *priv, @@ -1056,12 +1058,17 @@ static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, struct device *dev = ctx->dev; struct caam_flc *flc; u32 *desc; + int err; if (keylen != 2 * AES_MIN_KEY_SIZE && keylen != 2 * AES_MAX_KEY_SIZE) { dev_dbg(dev, "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; @@ -1443,6 +1450,20 @@ static void skcipher_decrypt_done(void *cbk_ctx, u32 status) skcipher_request_complete(req, ecode); } +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; + + if (IS_ALIGNED((unsigned long)req->iv, __alignof__(u64))) + size = *(u64 *)(req->iv + (ivsize / 2)); + else + size = get_unaligned((u64 *)(req->iv + (ivsize / 2))); + + return !!size; +} + static int skcipher_encrypt(struct skcipher_request *req) { struct skcipher_edesc *edesc; @@ -1454,6 +1475,18 @@ static int skcipher_encrypt(struct skcipher_request *req) if (!req->cryptlen) return 0; + if (ctx->fallback && xts_skcipher_ivsize(req)) { + skcipher_request_set_tfm(&caam_req->fallback_req, ctx->fallback); + skcipher_request_set_callback(&caam_req->fallback_req, + req->base.flags, + req->base.complete, + req->base.data); + skcipher_request_set_crypt(&caam_req->fallback_req, req->src, + req->dst, req->cryptlen, req->iv); + + return crypto_skcipher_encrypt(&caam_req->fallback_req); + } + /* allocate extended descriptor */ edesc = skcipher_edesc_alloc(req); if (IS_ERR(edesc)) @@ -1484,6 +1517,19 @@ static int skcipher_decrypt(struct skcipher_request *req) if (!req->cryptlen) return 0; + + if (ctx->fallback && xts_skcipher_ivsize(req)) { + skcipher_request_set_tfm(&caam_req->fallback_req, ctx->fallback); + skcipher_request_set_callback(&caam_req->fallback_req, + req->base.flags, + req->base.complete, + req->base.data); + skcipher_request_set_crypt(&caam_req->fallback_req, req->src, + req->dst, req->cryptlen, req->iv); + + return crypto_skcipher_decrypt(&caam_req->fallback_req); + } + /* allocate extended descriptor */ edesc = skcipher_edesc_alloc(req); if (IS_ERR(edesc)) @@ -1537,9 +1583,29 @@ static int caam_cra_init_skcipher(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; crypto_skcipher_set_reqsize(tfm, sizeof(struct caam_request)); - return caam_cra_init(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)) { + pr_err("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_request) + + crypto_skcipher_reqsize(fallback)); + } + + return caam_cra_init(ctx, &caam_alg->caam, false); } static int caam_cra_init_aead(struct crypto_aead *tfm) @@ -1562,7 +1628,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_cra_exit_aead(struct crypto_aead *tfm) @@ -1665,6 +1735,7 @@ static struct caam_skcipher_alg driver_algs[] = { .base = { .cra_name = "xts(aes)", .cra_driver_name = "xts-aes-caam-qi2", + .cra_flags = CRYPTO_ALG_NEED_FALLBACK, .cra_blocksize = AES_BLOCK_SIZE, }, .setkey = xts_skcipher_setkey, @@ -2912,8 +2983,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_skcipher; alg->exit = caam_cra_exit; diff --git a/drivers/crypto/caam/caamalg_qi2.h b/drivers/crypto/caam/caamalg_qi2.h index f29cb7bd7dd3..d35253407ade 100644 --- a/drivers/crypto/caam/caamalg_qi2.h +++ b/drivers/crypto/caam/caamalg_qi2.h @@ -13,6 +13,7 @@ #include #include "dpseci.h" #include "desc_constr.h" +#include #define DPAA2_CAAM_STORE_SIZE 16 /* NAPI weight *must* be a multiple of the store size. */ @@ -186,6 +187,7 @@ struct caam_request { void (*cbk)(void *ctx, u32 err); void *ctx; void *edesc; + struct skcipher_request fallback_req; }; /** From patchwork Thu Aug 6 11:41:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Botila X-Patchwork-Id: 253202 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=-13.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, 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 1B041C433E1 for ; Thu, 6 Aug 2020 16:59:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6BB172086A for ; Thu, 6 Aug 2020 16:59:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="bqXbDYjJ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729000AbgHFQ7d (ORCPT ); Thu, 6 Aug 2020 12:59:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729024AbgHFQkk (ORCPT ); Thu, 6 Aug 2020 12:40:40 -0400 Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-ve1eur03on0616.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe09::616]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06682C002161; Thu, 6 Aug 2020 09:04:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EIOGUPi59+CXJwUOBYDnIUH8XrVbxDtYowQsG8WI5aUep10yOCZDouz0tAURLitiUhIYDp15gxdB/rsWrNAgFF1Yy7GKIQo0pbMKs6zxjx+hyy3bgdPGX1dNfW+E3IaWcfob645S8Di8m27tP8LQ8Lsj6o9tNauiMOOlPEk4lsZKX5WQUM9xKj+wBMPWB7BtY3GlPQShcYpD4TV71BoDCDaVj2tXS9JrL7Xk0alECgJyjBszVmfe/0/J+n18WE09mW8SmXIK4J1Z817ej+ja0u7VKnmZyljy9+M3MxRuJRR9jL1/i6XLoyxyFlvO3lTzLQFImyxQ5tHShs0cJAgLdw== 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=LNBBN/dlZHg6cx7Sgg/A0XQBWdADCmK/YWU/bQrtkn8=; b=R7zGcE5nLlDjNkm3dyyLdoHXfqZbqRWZ7016GRHZ60jlRdlRsyKnGXOqnZaNDUIl4Ln47YjAmZn5VGGvxLXFsouR+MxJAvmsTdHhjIlmkvRlimTcYhl24P0KjGI/mogmrQxp3PIudrcPBD8QegpYsEorRX/LGChW8K6P6IDOpUAkc+mv/7bsHdqT5hyOjXzSnzviW35lE3WeC/sNRc4ivrH+FRlQDWbg0hM3RCRYZOGoffo5lMeuKXlaCkQ0CHmzwFY11aCZRciRKozE9EtorozUJUxhRpxAHeqrkhIGC6wTuO4fhVFUH2gnrxNJlljTZR7emMvOvFC4JrGlzDcDiw== 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=LNBBN/dlZHg6cx7Sgg/A0XQBWdADCmK/YWU/bQrtkn8=; b=bqXbDYjJS8VnFlcIMrKpZejPI0lA/Oo+/DPPAljNzdTIful/DtFvV0miyXEcBW3jzbfq9zlJGllKbIk5jePKVKbrIzYX5j4KxUcxkhUya1j6tn0xn8llcZLQ7eZ0TkIfolSWnVQbugIf9iH9Ad1qF/3Ko0sUlKK0hrbnHms53Vw= 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 VI1PR0402MB3440.eurprd04.prod.outlook.com (2603:10a6:803:6::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.19; Thu, 6 Aug 2020 11:41:57 +0000 Received: from VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d]) by VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d%6]) with mapi id 15.20.3261.019; Thu, 6 Aug 2020 11:41:57 +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 7/9] crypto: caam/jr - add support for XTS with 16B IV Date: Thu, 6 Aug 2020 14:41:25 +0300 Message-Id: <20200806114127.8650-8-andrei.botila@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200806114127.8650-1-andrei.botila@oss.nxp.com> References: <20200806114127.8650-1-andrei.botila@oss.nxp.com> X-ClientProxiedBy: AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) 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 AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.18 via Frontend Transport; Thu, 6 Aug 2020 11:41:55 +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: 986d1839-cbeb-4fac-4948-08d839fdb865 X-MS-TrafficTypeDiagnostic: VI1PR0402MB3440: X-MS-Exchange-SharedMailbox-RoutingAgent-Processed: True X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: O++reUXOap61eD/NCN2LR75x+CB+Hw1k9QJF8bfGh/I9rQMHmkRyQa94PPqt++PzQ78Ib7g4Xmw/Vfp4GiT7ecOHFhMY+D51WEeFEBiI7ryRdrevjg0rvzTokBKCfigivat4NL35FmAIqyJcqWVluiX9QX7nnNS5Zg639mLoPxLlSle9Rmh2MICWKzdlUVlk+P+FrDROVvZi7t0BMzZ/zd7aUcpPZ4EJ14yBKgItx6FB5BomBu1TjECm6DNKywoNmoAJqyTe3XteKq+mPC+I99Kr7gk5U1MrYS+o0al59oJ0fQlcHbP29v/TqfiUY9xOUtHCtVJKgYRj19T2qffX9A== 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; SFTY:; SFS:(4636009)(366004)(396003)(376002)(39860400002)(346002)(136003)(110136005)(316002)(6666004)(6512007)(478600001)(956004)(2616005)(6486002)(4326008)(1076003)(44832011)(52116002)(6506007)(86362001)(186003)(26005)(16526019)(66556008)(66476007)(2906002)(66946007)(83380400001)(8676002)(5660300002)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 6G+WfOgWvtHi8CQax6+MXY/AoduSNjfjemyH/cUch9tbwwvVA+rsobUzRCLPZFwOmPwoECctD/BDUMVa/nRYcuQoBMxCY61toM6ztC14/gqYwrvLgbEGq4+IgqaAM6frXMPhNG7Dt4+w5fEL3vHdY36eIAh5dXkUO59+rRkOYGL9C1mP5VejwnFfBqPxMcwL1FQDG2boc3HPl5SUg5DhKft/RhqLCdqWWyM9yEVfsXWYmugMm6/16O4fYct3oCvspfQH/NcXBldsP6R+WZQMZMebbD0OV0NOb+GfsZfCv09I1m9mu1Z5R4lGgxrRWccdA1V7NO3amDwHtdc5lJhaX8lPqEfPJZPzlVa2t5nAyc3cX7w3tTY/Ol+VeRYP1ydp6zaN7KN5DWw5RBWCjP1LS0A7NXEIH8CRlSuvhNdmgUnl2pZmCowiicLMFMVR9zN/SWl4fsB90LoLnYa/6NLx2uGmaYjKHwztUatyuiSocOUOjEeiu7AjyXNU9mcmfxfbYj1KlIFCLjBxfwU6gMjz69ZJPpE+/BXnPZXN9tgOUU29PK/ljJ4VrbHaIcM9wo+28aDccO78eXNzzQvNul3CQQF7K+uY6/9/CPQfQ/kj9HwpkUTmVSZNhdQ2nrTB2fVFEeYEEmA3hibx3M5QP9BJcA== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: 986d1839-cbeb-4fac-4948-08d839fdb865 X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6608.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2020 11:41:55.7489 (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: DYfWlP4nTM1cSE/+0ZVu7Bfu7lCZbbHpe8OLxg0+45+YOjtbBtFMeSxToUifnuT+sW+PU8vretLiGwuFwa+22g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0402MB3440 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Andrei Botila Newer CAAM versions (Era 9+) support 16B IVs. Since for these devices the HW limitation is no longer present newer version should process the requests containing 16B IVs directly in hardware without using a fallback. Signed-off-by: Andrei Botila --- drivers/crypto/caam/caamalg.c | 13 +++++++++---- drivers/crypto/caam/caamalg_desc.c | 27 ++++++++++++++++----------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index a5447ae430b0..7e03854252b0 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -833,6 +833,7 @@ 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; + struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent); u32 *desc; int err; @@ -842,9 +843,12 @@ static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, return err; } - err = crypto_skcipher_setkey(ctx->fallback, key, keylen); - if (err) - return err; + if (ctrlpriv->era <= 8 || (keylen != 2 * AES_KEYSIZE_128 && + keylen != 2 * AES_KEYSIZE_256)) { + err = crypto_skcipher_setkey(ctx->fallback, key, keylen); + if (err) + return err; + } ctx->cdata.keylen = keylen; ctx->cdata.key_virt = key; @@ -1786,13 +1790,14 @@ static inline int skcipher_crypt(struct skcipher_request *req, bool encrypt) struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher); struct device *jrdev = ctx->jrdev; struct caam_drv_private_jr *jrpriv = dev_get_drvdata(jrdev); + struct caam_drv_private *ctrlpriv = dev_get_drvdata(jrdev->parent); u32 *desc; int ret = 0; if (!req->cryptlen) return 0; - if (ctx->fallback && (xts_skcipher_ivsize(req) || + if (ctx->fallback && ((ctrlpriv->era <= 8 && xts_skcipher_ivsize(req)) || (ctx->cdata.keylen != 2 * AES_KEYSIZE_128 && ctx->cdata.keylen != 2 * AES_KEYSIZE_256))) { struct caam_skcipher_req_ctx *rctx = skcipher_request_ctx(req); diff --git a/drivers/crypto/caam/caamalg_desc.c b/drivers/crypto/caam/caamalg_desc.c index d6c58184bb57..433d6d5cd582 100644 --- a/drivers/crypto/caam/caamalg_desc.c +++ b/drivers/crypto/caam/caamalg_desc.c @@ -1550,13 +1550,14 @@ void cnstr_shdsc_xts_skcipher_encap(u32 * const desc, struct alginfo *cdata) set_jump_tgt_here(desc, key_jump_cmd); /* - * create sequence for loading the sector index - * Upper 8B of IV - will be used as sector index - * Lower 8B of IV - will be discarded + * create sequence for loading the sector index / 16B tweak value + * Lower 8B of IV - sector index / tweak lower half + * Upper 8B of IV - upper half of 16B tweak */ append_seq_load(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT)); - append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP); + append_seq_load(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | + (0x30 << LDST_OFFSET_SHIFT)); /* Load operation */ append_operation(desc, cdata->algtype | OP_ALG_AS_INITFINAL | @@ -1565,9 +1566,11 @@ void cnstr_shdsc_xts_skcipher_encap(u32 * const desc, struct alginfo *cdata) /* Perform operation */ skcipher_append_src_dst(desc); - /* Store upper 8B of IV */ + /* Store lower 8B and upper 8B of IV */ append_seq_store(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT)); + append_seq_store(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | + (0x30 << LDST_OFFSET_SHIFT)); print_hex_dump_debug("xts skcipher enc shdesc@" __stringify(__LINE__) ": ", DUMP_PREFIX_ADDRESS, 16, 4, @@ -1609,23 +1612,25 @@ void cnstr_shdsc_xts_skcipher_decap(u32 * const desc, struct alginfo *cdata) set_jump_tgt_here(desc, key_jump_cmd); /* - * create sequence for loading the sector index - * Upper 8B of IV - will be used as sector index - * Lower 8B of IV - will be discarded + * create sequence for loading the sector index / 16B tweak value + * Lower 8B of IV - sector index / tweak lower half + * Upper 8B of IV - upper half of 16B tweak */ append_seq_load(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT)); - append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP); - + append_seq_load(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | + (0x30 << LDST_OFFSET_SHIFT)); /* Load operation */ append_dec_op1(desc, cdata->algtype); /* Perform operation */ skcipher_append_src_dst(desc); - /* Store upper 8B of IV */ + /* Store lower 8B and upper 8B of IV */ append_seq_store(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT)); + append_seq_store(desc, 8, LDST_SRCDST_BYTE_CONTEXT | LDST_CLASS_1_CCB | + (0x30 << LDST_OFFSET_SHIFT)); print_hex_dump_debug("xts skcipher dec shdesc@" __stringify(__LINE__) ": ", DUMP_PREFIX_ADDRESS, 16, 4, desc, From patchwork Thu Aug 6 11:41:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrei Botila X-Patchwork-Id: 253199 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=-13.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, MSGID_FROM_MTA_HEADER, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, 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 84A69C433E0 for ; Thu, 6 Aug 2020 17:22:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D9BD12311C for ; Thu, 6 Aug 2020 17:22:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.b="YCo9N4vY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727053AbgHFRW3 (ORCPT ); Thu, 6 Aug 2020 13:22:29 -0400 Received: from mail-vi1eur05on2047.outbound.protection.outlook.com ([40.107.21.47]:5856 "EHLO EUR05-VI1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729811AbgHFRWK (ORCPT ); Thu, 6 Aug 2020 13:22:10 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k5s97EfATDWCJEMSbvDLsttovRbEl+y1TJVlDERXZCns9edxRkJg/fG8g8orLbnXRGv9Lwp3+1ti7SF6k4xezzR1yZU3ih6/sgagQ+cfl/ErR015ePK3ezqqegAQIfNFuLb7HOMrmCbeLLRT5nbJd0B7hkXzVm2lVfjQld7H59kbLtFmkHNNO07iloTpErlSyEpO5sgxUhu2ozZvOSFV1rj7whSCyyj3H9X642Lc6nt+UP/6uyL09Ku09hfhYA6KUfazG1c/FPsAjoLyB8zsjvY8jdK/jg1hhYhDduwYPRdUyn8W0D9TaKOPIc/MzTwsNAxXrVcoMWjBLg7i0ycPFQ== 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=y4FFxvCtd5FGH5hWLm5Lv9sEb67r44oV0tHVJRKUXLw=; b=jNHccwq4t1uX/tnWLvAPuixDjEGK1Nzbmy4EUezXXgNy0TE8O0W88rrEhsgThiRi7MV4ZdaaDASbhia012t5SthBt4wedobouhr1QbESZwI8hI7PXV4mXES0rHcs1bhvuUU2lJUBHd27NbfRJGQtg/cRM6+iiHIRtq88LaExWwYQnknMF9W5PTI/J9Br+/fUAAKb0W02KM9CEgHJSWVerw4hXWD2VHsJgC5cndhx7I0c1+agSogBvrgSMdLI3IK/b+U+Q915nl1s3AnWAsSA13rBo96yj45ZWXRxFNtJUUzxzAOpnNSJqBfnXzpCPe+bccr22DzGMEjJ3ivOKumYmQ== 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=y4FFxvCtd5FGH5hWLm5Lv9sEb67r44oV0tHVJRKUXLw=; b=YCo9N4vYTUNCuJAPrBAktKAhZpok3ST2xRLqNtTfvtPK7eR50hDrw+BcwjHFvVkn2P8SCBlrfU5RDLoiyHRnAQdZRxZ8ZIbgpkrSmpoT/FA5RfSbkhKWPmbV+9sjbV13rP7AZjKqprxmdHGppI1zu4mGLwwD6qN4xkrLugzrIU0= 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 VI1PR0402MB3440.eurprd04.prod.outlook.com (2603:10a6:803:6::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.19; Thu, 6 Aug 2020 11:41:59 +0000 Received: from VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d]) by VE1PR04MB6608.eurprd04.prod.outlook.com ([fe80::a856:c104:11c7:258d%6]) with mapi id 15.20.3261.019; Thu, 6 Aug 2020 11:41:59 +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 9/9] crypto: caam/qi2 - add support for XTS with 16B IV Date: Thu, 6 Aug 2020 14:41:27 +0300 Message-Id: <20200806114127.8650-10-andrei.botila@oss.nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200806114127.8650-1-andrei.botila@oss.nxp.com> References: <20200806114127.8650-1-andrei.botila@oss.nxp.com> X-ClientProxiedBy: AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) 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 AM0PR06CA0101.eurprd06.prod.outlook.com (2603:10a6:208:fa::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3261.18 via Frontend Transport; Thu, 6 Aug 2020 11:41:56 +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: db75bd79-b086-4ee5-6bd9-08d839fdb95b X-MS-TrafficTypeDiagnostic: VI1PR0402MB3440: 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: UfVR2x6EOP4a3KRHtlb50513VFGih+QKs4dFBaCyo33DY46lX5ay9eYbFaA29R/Kdz3DemeUu5g4r8fN6bsHozCSAvV65AlZwFirB78cUDFKlQjEuRx9QaoWB6S/2Y8cWDi0Lf9ee60tIIG7P3foF1mG6ULTkNVeVFrH9m+DclTsNx4PjGjZNR23oo5FsyA96ua5ZYEe3yCJjH8JxwH3kQq2PHVeDcGiRdxF9QU2tIBvnzeeAcjcwTOB7MVw5a1UZtIiQ9TM3xZ7o86X3K5o3W+XFdJ/ZEGvHxyrVYE5qBV2BOLykftDk5a95ky1JGL1x3gZi1vfF029Og0Q3zil4A== 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; SFTY:; SFS:(4636009)(366004)(396003)(376002)(39860400002)(346002)(136003)(110136005)(316002)(6666004)(6512007)(478600001)(956004)(2616005)(6486002)(4326008)(1076003)(44832011)(52116002)(6506007)(86362001)(186003)(26005)(16526019)(66556008)(66476007)(2906002)(66946007)(83380400001)(8676002)(5660300002)(8936002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: qPacvdCh9sZhvj+wQ5uYw++pkdddMC6Q2vC6rkcKKWStD5qsOsavHCc5B77lREucDO6NPrGgwomxd/QcCD0/cyiuyLvKf6a6RPJb7DIHu+7yhrwsOfRt/ZT6CqhRsTwVmhuDe6sD2wNjNJI7NBkBDZvpd9WSFNsUNei5IGu9XY6fDYZmO5lFrdmzeW7N5S0i/wdmlLB7o68UM9nw1tMt47UPl+tXTJiyMVkOeNt3D9HJPDRvmSy2APe7WB2GuWH+A/AWh9G3lmCUtZpJpQCGwo4E/MCLt1u6Gh9z9qLbInc/em/wfev4nR2HY+mMUyUfKA8OTbuen3p9ITilWBVZlepIQnYxeF1w8RAbQLxCuuzPiaF9kbQA8fd3KW3N0xC1HkBNYe/xx88Qud2EBBVN5yxoH638SkY883lXzF3tCS/1x9IYoS0KuyTbC0UbUMA6TJS96xgK5tREslM13+NI6KWAof0hW8UIVGRXVnqjs2fPdasTCNAGqlCYE35W/68SfQqdU8xGR2j90cF+2pFa+IewjNrDxKAxHC04y94ZrOBNY0Pqz7t7cUydbCwdDh4TRDYbkvn1y/DcZM6ZScFcU0GVpr3cqABHpSHnESkSJhslsFcDGoJZSU8YdEIgQt92yo1an+gOmq7DywpBEm/L/A== X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: db75bd79-b086-4ee5-6bd9-08d839fdb95b X-MS-Exchange-CrossTenant-AuthSource: VE1PR04MB6608.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Aug 2020 11:41:57.3519 (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: g3tIpvG0R+26SFplJC8YPEWaq4g3G8/sAu8P/GLtJCunzzRGef1vowILlxqb1IM6UkcoBpSopc1hGegBNVavoQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0402MB3440 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Andrei Botila Newer CAAM versions (Era 9+) support 16B IVs. Since for these devices the HW limitation is no longer present newer version should process the requests containing 16B IVs directly in hardware without using a fallback. Signed-off-by: Andrei Botila --- drivers/crypto/caam/caamalg_qi2.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/drivers/crypto/caam/caamalg_qi2.c b/drivers/crypto/caam/caamalg_qi2.c index f2f137f47972..99b1267b1a0b 100644 --- a/drivers/crypto/caam/caamalg_qi2.c +++ b/drivers/crypto/caam/caamalg_qi2.c @@ -1057,6 +1057,7 @@ static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, { struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher); struct device *dev = ctx->dev; + struct dpaa2_caam_priv *priv = dev_get_drvdata(dev); struct caam_flc *flc; u32 *desc; int err; @@ -1067,9 +1068,12 @@ static int xts_skcipher_setkey(struct crypto_skcipher *skcipher, const u8 *key, return err; } - err = crypto_skcipher_setkey(ctx->fallback, key, keylen); - if (err) - return err; + if (priv->sec_attr.era <= 8 || (keylen != 2 * AES_KEYSIZE_128 && + keylen != 2 * AES_KEYSIZE_256)) { + err = crypto_skcipher_setkey(ctx->fallback, key, keylen); + if (err) + return err; + } ctx->cdata.keylen = keylen; ctx->cdata.key_virt = key; @@ -1472,12 +1476,13 @@ static int skcipher_encrypt(struct skcipher_request *req) struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher); struct caam_request *caam_req = skcipher_request_ctx(req); + struct dpaa2_caam_priv *priv = dev_get_drvdata(ctx->dev); int ret; if (!req->cryptlen) return 0; - if (ctx->fallback && (xts_skcipher_ivsize(req) || + if (ctx->fallback && ((priv->sec_attr.era <= 8 && xts_skcipher_ivsize(req)) || (ctx->cdata.keylen != 2 * AES_KEYSIZE_128 && ctx->cdata.keylen != 2 * AES_KEYSIZE_256))) { skcipher_request_set_tfm(&caam_req->fallback_req, ctx->fallback); @@ -1517,12 +1522,13 @@ static int skcipher_decrypt(struct skcipher_request *req) struct crypto_skcipher *skcipher = crypto_skcipher_reqtfm(req); struct caam_ctx *ctx = crypto_skcipher_ctx(skcipher); struct caam_request *caam_req = skcipher_request_ctx(req); + struct dpaa2_caam_priv *priv = dev_get_drvdata(ctx->dev); int ret; if (!req->cryptlen) return 0; - if (ctx->fallback && (xts_skcipher_ivsize(req) || + if (ctx->fallback && ((priv->sec_attr.era <= 8 && xts_skcipher_ivsize(req)) || (ctx->cdata.keylen != 2 * AES_KEYSIZE_128 && ctx->cdata.keylen != 2 * AES_KEYSIZE_256))) { skcipher_request_set_tfm(&caam_req->fallback_req, ctx->fallback);