From patchwork Fri Oct 2 10:13:18 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Catalin Vasile X-Patchwork-Id: 7314471 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 643D2BEEA4 for ; Fri, 2 Oct 2015 10:13:34 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4C6152089D for ; Fri, 2 Oct 2015 10:13:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id F23322089B for ; Fri, 2 Oct 2015 10:13:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750730AbbJBKNb (ORCPT ); Fri, 2 Oct 2015 06:13:31 -0400 Received: from mail-bl2on0104.outbound.protection.outlook.com ([65.55.169.104]:25216 "EHLO na01-bl2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750709AbbJBKN3 (ORCPT ); Fri, 2 Oct 2015 06:13:29 -0400 Received: from BN3PR0301CA0067.namprd03.prod.outlook.com (10.160.152.163) by DM2PR03MB576.namprd03.prod.outlook.com (10.141.84.19) with Microsoft SMTP Server (TLS) id 15.1.286.20; Fri, 2 Oct 2015 10:13:26 +0000 Received: from BY2FFO11OLC014.protection.gbl (2a01:111:f400:7c0c::185) by BN3PR0301CA0067.outlook.office365.com (2a01:111:e400:401e::35) with Microsoft SMTP Server (TLS) id 15.1.286.20 via Frontend Transport; Fri, 2 Oct 2015 10:13:26 +0000 Authentication-Results: spf=fail (sender IP is 192.88.158.2) smtp.mailfrom=freescale.com; freescale.mail.onmicrosoft.com; dkim=none (message not signed) header.d=none; freescale.mail.onmicrosoft.com; dmarc=none action=none header.from=freescale.com; Received-SPF: Fail (protection.outlook.com: domain of freescale.com does not designate 192.88.158.2 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.158.2; helo=az84smr01.freescale.net; Received: from az84smr01.freescale.net (192.88.158.2) by BY2FFO11OLC014.mail.protection.outlook.com (10.1.15.48) with Microsoft SMTP Server (TLS) id 15.1.286.14 via Frontend Transport; Fri, 2 Oct 2015 10:13:25 +0000 Received: from enigma.ea.freescale.net (enigma.ea.freescale.net [10.171.77.120]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id t92ADNhW012350; Fri, 2 Oct 2015 03:13:23 -0700 From: Catalin Vasile To: CC: Catalin Vasile , Cristian Hristea , Horia Geanta , "Alex Porosanu" Subject: [PATCH] crypto: caam - add support for acipher xts(aes) Date: Fri, 2 Oct 2015 13:13:18 +0300 Message-ID: <1443780798-11056-1-git-send-email-catalin.vasile@freescale.com> X-Mailer: git-send-email 1.8.3.1 X-EOPAttributedMessage: 0 X-Microsoft-Exchange-Diagnostics: 1; BY2FFO11OLC014; 1:Sdvhpv+U8UpuI2FgJKpaztfAeS6BY8CJdS0HMuns3yf3HNHZFaoXVm2JwPxGknrYGSsZCJG4WlrwbJtoSFRTd8zoIMmXXv1FrEFQJnmjSpVDs3XMrZhS2bl3wa/4zrAKocwW/twtRo/uuyAxtUGgcRADF8HFI2Q3w+tmpx64QiZwkQH+8XewTat3PiYcMINRQAvjcjGVxVkZ2i7CP8SmGLLXUiJ6gT5x6OLHd9uj9WPIdTM4DMNIhHD2iG3sZkBjx/3S+4vqpkgyLqF71yY95ZkNRU93AGEYQX7S52irJejvUbzaz4YY2pdjFUVdcF+r+TFyghegFJscZfBvTnoxLwCRjmYdQa9EC5VJlC/7hqFnjjeItjvl4+6nMpGTDLx3XxmjtDkVb2GfWVLcrHL/GA== X-Forefront-Antispam-Report: CIP:192.88.158.2; CTRY:US; IPV:NLI; EFV:NLI; SFV:NSPM; SFS:(10019020)(6009001)(2980300002)(1110001)(1109001)(339900001)(189002)(199003)(50986999)(5001860100001)(85426001)(19580395003)(19580405001)(69596002)(575784001)(86362001)(5008740100001)(5003940100001)(104016004)(6806005)(81156007)(77096005)(68736005)(4001540100001)(62966003)(97736004)(77156002)(5001830100001)(5007970100001)(11100500001)(64706001)(47776003)(105606002)(5001960100002)(106466001)(110136002)(107886002)(229853001)(189998001)(87936001)(2351001)(48376002)(33646002)(50466002)(92566002)(46102003)(50226001)(36756003)(4001430100001); DIR:OUT; SFP:1102; SCL:1; SRVR:DM2PR03MB576; H:az84smr01.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; MIME-Version: 1.0 X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB576; 2:BQmHK+1hIDqXvrwt1gK8XC43ME+GZJvM/Z1b8+u9S/sBLqu6zcnIR2PedfEB2DeOhZ/T+ynF3GpRXzk6gzkr6l+FdmiHNjJQ8GX/Q80JQxxsGkSgKfKlQtuUvuu5J7VcIRS7eyoXjBcnB5jHc3LHk97oqZK2fm0ryfMwEbZ8uk4=; 3:EobpvygMMOmcsaTuyNa+DidHgVLxTXYruJzMYae6aQNGnAExlSI+UPEjAY2J3GyjqQwGocgOL4S6X5U+FAA9qEOYR/NSim9wt3gHqc/tA/SDy0n8rxBXoeH0GJm71vc50oTg+GaWS4V6NnHds38HDPVq5b6aRrnTcUfyVBIkhiCzi9F6HVufFuFotN53WUI6hH84dUiXMNFZzwrn+z4mhsBdgs28IGluenhkWn9bbSc=; 25:0pDzh5oaE4ugHVOCCkCEC/cD4TZtH4EEki7xgDdVLUexYL5xKC4OzarEqfXUVVb5RN1S2sLA2+bVNbL/2QBPMFABDQVs3rpQ1wK+0MXga9UHHBhPsfi1z+ZPV2xCLl24s9xNmKV+adY4YKu60Gmp38qgy4T4TCnQzTL94h3UTpyX/YZBYtOvtTqn9mIgAtAgZj4QDoiuBjv5sTbvCyOAabXHzJbrZ3CeqoRuCknZD+zQiCZ1lac3/c8PpUibGdPPmcwrvr7aa/WcynXS6CKLrA== X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM2PR03MB576; X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB576; 20:pR0XcYMt/luivGynpXvNVTfXl7NvkW9jkw3hpshmz9iw772ebLl3oEt1pfdMfNfS1YaMuqGMBcZHcZ065eB8rluvYMhy2Fej1m2h1K5uCpQSwYTkj/63O3w8WfBt2lGFvblrSfjU66veJhSy5VYC3YLISloErxGS1wksFLMZQB+us1QRrrhgISisXMLPZlAH+iCWufGZslDEYTB4cTQcspeKHEAImP2xYi9UxdWLhXHKoD3yrh+TE1BdsaT5eZscJO+Ci5bm/3d7HaGGNUA55H2DLtlc/nCHisCMWJMwVnasYmblHnjP4AlHrQGRUM/DBpz/5WzOTnweergZZB+R/stSAxb2SiNZ4f0xIXBSFkw=; 4:vHEq6IUdPfRlrnp5grRr7pWVyQiPAxjuNOGxWYk0bfrQ76QlYsuE5hUVC5aSIS0EWPerZmPy3bsZxpiqr9j83YIx+m8Y75Wsbjgy5M/WBe82K7v23lCX2v2NshVihLnFRSLbXMIkjPxQFHwEl5nBc+Ql2NGdvvov94096D6oritE2vM9kBD3hh+XVzz+7kD8jGyCWtgsKRSGmnBcN6SjxYQuaHM9LYttU1M9u0dn7bOFs9Cvd7FC0YJpS1MBX44b08FxDlUtknGDbufU8oH/EpiMnIC6JhJGc27UwT9GKcuNASb60GKAPNJKa99Tljs04lb+vSQhSM6ahmTrgp+ckG8gh0XwcIIa5mK3/q6KptE= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(5005006)(520078)(8121501046)(3002001); SRVR:DM2PR03MB576; BCL:0; PCL:0; RULEID:; SRVR:DM2PR03MB576; X-Forefront-PRVS: 0717E25089 X-Microsoft-Exchange-Diagnost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p1tJTo6KMc7ei7I6lqAn?= =?us-ascii?Q?7ZvGnoghWhQ7kDsxhlMz1rxEXnf47cfNqSa0Ff3A6AH0Cu06BII8Pz/zuUQ2?= =?us-ascii?Q?7t91c5V6Smw50BHOOmyWKe+es2KTm9DQa/xVgqZm9lsRFf+KlOGILmbeYucU?= =?us-ascii?Q?jlu4cci9JMUzXmcM78lBKl45obZKFkVU3iA5aa90XGHsjJq57dRKdk0rurE6?= =?us-ascii?Q?XJtJ8LClWOUYfiPgW38nmqyAZaQzc7yqJq/CJnuAau7lS4fnkt6cbz7fqJEf?= =?us-ascii?Q?bNkO3MvZXbrtu3D99TL4lvHNT3ZKb5nIfaSUiRxOsGgVn5cO4ByZ9prNeXAK?= =?us-ascii?Q?g/hPzkqL5SCD4u6wGk7Aoghb6vvLXvvTFDvL45GCP+EkOlmAeACSlFfTW/ar?= =?us-ascii?Q?xx4VmxZkuClX1P76DtJbuNIAq6fUrT/wFiUz/KcLVvS8Ke6bXk/3m+0CO+MQ?= =?us-ascii?Q?=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1; DM2PR03MB576; 5:lBq6bXkfstdyNVENACvYdO1m9DcZcLdVfebKRMlp+Ssd1JAI5hYnmxH5T0N7e+Hfwq2UewLFd0fEtStUB+8790LiNKwFaIll+nznHXg6SJSvxMAH920/0YnzQRNEL3gEDhQv2OHq0E7Hp8X/s3HvOQ==; 24:7TEcS90ELSfSH87gLrhQttL193S4UPpo5/WKk7sXU7Jz+aI7msAW73bh8yvI6oWiu2nPzI2RL/RHaCIGfnbjBad6uXmyrqtvocezdferdzI=; 20:D/0EJ4zKKIolyOW5wIkisQf/ZoeNwB0OLNkFTSaJVA3Bz54v4+Rz8ZRFplJO/P6GfHJXuYmdx4FrnhRBGlebsA== X-OriginatorOrg: freescale.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Oct 2015 10:13:25.4460 (UTC) X-MS-Exchange-CrossTenant-Id: 710a03f5-10f6-4d38-9ff4-a80b81da590d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=710a03f5-10f6-4d38-9ff4-a80b81da590d; Ip=[192.88.158.2]; Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR03MB576 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_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 Add support for AES working in XEX-based Tweaked-codebook mode with ciphertext Stealing (XTS) sector index - HW limitation: CAAM device supports sector index of only 8 bytes to be used for sector index inside IV, instead of whole 16 bytes received on request. This represents 2 ^ 64 = 16,777,216 Tera of possible values for sector index. Signed-off-by: Cristian Hristea Signed-off-by: Horia Geanta Signed-off-by: Alex Porosanu Signed-off-by: Catalin Vasile --- drivers/crypto/caam/caamalg.c | 138 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 137 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c index ad0d1ec..ea8189f 100644 --- a/drivers/crypto/caam/caamalg.c +++ b/drivers/crypto/caam/caamalg.c @@ -1705,6 +1705,126 @@ static int ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher, return ret; } +static int xts_ablkcipher_setkey(struct crypto_ablkcipher *ablkcipher, + const u8 *key, unsigned int keylen) +{ + struct caam_ctx *ctx = crypto_ablkcipher_ctx(ablkcipher); + struct device *jrdev = ctx->jrdev; + u32 *key_jump_cmd, *desc; + __be64 sector_size = cpu_to_be64(512); + + if (keylen != 2 * AES_MIN_KEY_SIZE && keylen != 2 * AES_MAX_KEY_SIZE) { + crypto_ablkcipher_set_flags(ablkcipher, + CRYPTO_TFM_RES_BAD_KEY_LEN); + dev_err(jrdev, "key size mismatch\n"); + return -EINVAL; + } + + memcpy(ctx->key, key, keylen); + ctx->key_dma = dma_map_single(jrdev, ctx->key, keylen, DMA_TO_DEVICE); + if (dma_mapping_error(jrdev, ctx->key_dma)) { + dev_err(jrdev, "unable to map key i/o memory\n"); + return -ENOMEM; + } + ctx->enckeylen = keylen; + + /* xts_ablkcipher_encrypt shared descriptor */ + desc = ctx->sh_desc_enc; + init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX); + /* Skip if already shared */ + key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | + JUMP_COND_SHRD); + + /* Load class1 keys only */ + append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen, + ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); + + /* Load sector size with index 40 bytes (0x28) */ + append_cmd(desc, CMD_LOAD | IMMEDIATE | LDST_SRCDST_BYTE_CONTEXT | + LDST_CLASS_1_CCB | (0x28 << LDST_OFFSET_SHIFT) | 8); + append_data(desc, (void *)§or_size, 8); + + 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 + */ + append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT | + LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT) | 8); + append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP); + + /* Load operation */ + append_operation(desc, ctx->class1_alg_type | OP_ALG_AS_INITFINAL | + OP_ALG_ENCRYPT); + + /* Perform operation */ + ablkcipher_append_src_dst(desc); + + ctx->sh_desc_enc_dma = dma_map_single(jrdev, desc, desc_bytes(desc), + DMA_TO_DEVICE); + if (dma_mapping_error(jrdev, ctx->sh_desc_enc_dma)) { + dev_err(jrdev, "unable to map shared descriptor\n"); + return -ENOMEM; + } +#ifdef DEBUG + print_hex_dump(KERN_ERR, + "xts ablkcipher enc shdesc@" __stringify(__LINE__) ": ", + DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); +#endif + + /* xts_ablkcipher_decrypt shared descriptor */ + desc = ctx->sh_desc_dec; + + init_sh_desc(desc, HDR_SHARE_SERIAL | HDR_SAVECTX); + /* Skip if already shared */ + key_jump_cmd = append_jump(desc, JUMP_JSL | JUMP_TEST_ALL | + JUMP_COND_SHRD); + + /* Load class1 key only */ + append_key_as_imm(desc, (void *)ctx->key, ctx->enckeylen, + ctx->enckeylen, CLASS_1 | KEY_DEST_CLASS_REG); + + /* Load sector size with index 40 bytes (0x28) */ + append_cmd(desc, CMD_LOAD | IMMEDIATE | LDST_SRCDST_BYTE_CONTEXT | + LDST_CLASS_1_CCB | (0x28 << LDST_OFFSET_SHIFT) | 8); + append_data(desc, (void *)§or_size, 8); + + 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 + */ + append_cmd(desc, CMD_SEQ_LOAD | LDST_SRCDST_BYTE_CONTEXT | + LDST_CLASS_1_CCB | (0x20 << LDST_OFFSET_SHIFT) | 8); + append_seq_fifo_load(desc, 8, FIFOLD_CLASS_SKIP); + + /* Load operation */ + append_dec_op1(desc, ctx->class1_alg_type); + + /* Perform operation */ + ablkcipher_append_src_dst(desc); + + ctx->sh_desc_dec_dma = dma_map_single(jrdev, desc, desc_bytes(desc), + DMA_TO_DEVICE); + if (dma_mapping_error(jrdev, ctx->sh_desc_dec_dma)) { + dma_unmap_single(jrdev, ctx->sh_desc_enc_dma, + desc_bytes(ctx->sh_desc_enc), DMA_TO_DEVICE); + dev_err(jrdev, "unable to map shared descriptor\n"); + return -ENOMEM; + } +#ifdef DEBUG + print_hex_dump(KERN_ERR, + "xts ablkcipher dec shdesc@" __stringify(__LINE__) ": ", + DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); +#endif + + return 0; +} + /* * aead_edesc - s/w-extended aead descriptor * @assoc_nents: number of segments in associated data (SPI+Seq) scatterlist @@ -2845,7 +2965,23 @@ static struct caam_alg_template driver_algs[] = { .ivsize = CTR_RFC3686_IV_SIZE, }, .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_CTR_MOD128, - } + }, + { + .name = "xts(aes)", + .driver_name = "xts-aes-caam", + .blocksize = AES_BLOCK_SIZE, + .type = CRYPTO_ALG_TYPE_ABLKCIPHER, + .template_ablkcipher = { + .setkey = xts_ablkcipher_setkey, + .encrypt = ablkcipher_encrypt, + .decrypt = ablkcipher_decrypt, + .geniv = "eseqiv", + .min_keysize = 2 * AES_MIN_KEY_SIZE, + .max_keysize = 2 * AES_MAX_KEY_SIZE, + .ivsize = AES_BLOCK_SIZE, + }, + .class1_alg_type = OP_ALG_ALGSEL_AES | OP_ALG_AAI_XTS, + }, }; static struct caam_aead_alg driver_aeads[] = {