Received: by 10.223.185.116 with SMTP id b49csp2574995wrg; Mon, 12 Feb 2018 11:54:03 -0800 (PST) X-Google-Smtp-Source: AH8x225TxGKDCUpbD/4LBLutQsbBHUOSojpWj20j9RWrOIq+asD1gQnbcTeZjq0CECI+lBzu1yMn X-Received: by 10.101.101.149 with SMTP id u21mr10420306pgv.251.1518465243067; Mon, 12 Feb 2018 11:54:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518465243; cv=none; d=google.com; s=arc-20160816; b=JdwqNqa4pCCjvzCRPSUno0+6u9rCh1gS6PfpxABM9T3pFX7I84+CDsbaYl+CM/6sLV QNXaGCBnapk0yNaslgky/wtlPlJR1hqyxXgLoM04qfr8aBCxV+u4GiTu9yADuovCsr3e +bkzIo7SQaqj3aHkoP/15EBagSZUbmx4Vv+tyXo2vU5GyGSbLF8S4DFx0hH81sfBplLk ZNHeF+eXdYChd+7auk7p8acQbxpNdVAESCx6YqNVm2FIUuc8AD48P74uKjkQQ5ourAFg +wxSgwzFz85c08CtM531MRM2e9IaNe6r6ACMaBnOVaWaaBI0RpWPEbBDU6a/yaWGRtc3 /aWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:spamdiagnosticmetadata :spamdiagnosticoutput:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature:dkim-signature:arc-authentication-results; bh=I/MtYJOWJl6KZ8iwz6RMBP29GqZ9inwEuQ4UEdERB3A=; b=XEWP7Yr5kd2Li4ZeO5wdmRzyVqxwY4MFQD+CwclFBxu32rLdbQMiOhCm0oEFK5UrXX h9+AuNDbC8b83uEvAQM3fcY65YH/a44CfdxlDGNBV4XLU2llQ6rZJ+Jqx0ZVh/Hz82+t 7nXFX0pm1Glqc5uJwdZhAGlBksMzV+d9JgZV6CB2QumKXEMDnqKBPypUONWo//pisrb2 2J8Plm6fMzkOnH9C/GGW8+ptg9Qv/JyicAFWA9BgUJNLXKW0O4g+N3JoHcJjrQGb5pVz IGOqZO54v5HKYRfZeflyVA/z0Hho95wdp7jr0C/GvJeN0XfmhYZKTvETPQE6jh6xYX+1 JYHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=IOOFCYzn; dkim=fail header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=cEJmaoXj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v62si33105pfi.376.2018.02.12.11.53.48; Mon, 12 Feb 2018 11:54:03 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@fb.com header.s=facebook header.b=IOOFCYzn; dkim=fail header.i=@fb.onmicrosoft.com header.s=selector1-fb-com header.b=cEJmaoXj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=fb.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754061AbeBLTwL (ORCPT + 99 others); Mon, 12 Feb 2018 14:52:11 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:55768 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751208AbeBLTv5 (ORCPT ); Mon, 12 Feb 2018 14:51:57 -0500 Received: from pps.filterd (m0044008.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w1CJmwen018061; Mon, 12 Feb 2018 11:51:41 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : in-reply-to; s=facebook; bh=I/MtYJOWJl6KZ8iwz6RMBP29GqZ9inwEuQ4UEdERB3A=; b=IOOFCYznHad7BoA+2wqgffpFcBVsumnk1zcT819pDpoyiU7S/nR7BxliGBJwZMzNX1kZ 8GBXNX/d+4o+EprFz4GR8fY4KwlQZZIbrj2rLRW48MSuCTI+45mSjQ1LrK4XqGt3WPXo r5LDcEBJa2fv60R5b+ZelSEm1wiwvGDSALU= Received: from maileast.thefacebook.com ([199.201.65.23]) by mx0a-00082601.pphosted.com with ESMTP id 2g3fh28cym-2 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NOT); Mon, 12 Feb 2018 11:51:41 -0800 Received: from NAM01-SN1-obe.outbound.protection.outlook.com (192.168.183.28) by o365-in.thefacebook.com (192.168.177.29) with Microsoft SMTP Server (TLS) id 14.3.361.1; Mon, 12 Feb 2018 14:51:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.onmicrosoft.com; s=selector1-fb-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=I/MtYJOWJl6KZ8iwz6RMBP29GqZ9inwEuQ4UEdERB3A=; b=cEJmaoXjOA7pxsFl247Wj4VN4uhYOIVa7wWiB/4a1ImHig/LLXTXZPuTyWNjISinyOg4cNdaQOI5oJkE7PPxoFmFGp7zbQIFkLbmRhx0M5/tbt7N4H79SXUQpDMKZyPjOCcgcgKZrvThGRRzNxx0QPGWTFsGl7zRw5rzeubUHEo= Received: from localhost (2620:10d:c090:200::6:842f) by CY4PR15MB1751.namprd15.prod.outlook.com (10.174.53.141) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.485.10; Mon, 12 Feb 2018 19:51:35 +0000 Date: Mon, 12 Feb 2018 11:51:28 -0800 From: Dave Watson To: Herbert Xu , Junaid Shahid , Steffen Klassert , CC: "David S. Miller" , Hannes Frederic Sowa , Tim Chen , Sabrina Dubroca , , Stephan Mueller , Ilya Lesokhin Subject: [PATCH 14/14] x86/crypto: aesni: Update aesni-intel_glue to use scatter/gather Message-ID: <20180212195128.GA61087@davejwatson-mba.local> References: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.6.0 (2016-04-01) X-Originating-IP: [2620:10d:c090:200::6:842f] X-ClientProxiedBy: DM5PR19CA0040.namprd19.prod.outlook.com (10.173.167.154) To CY4PR15MB1751.namprd15.prod.outlook.com (10.174.53.141) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8bbdf3a2-cdc2-4154-f3eb-08d572520652 X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(7020095)(4652020)(5600026)(4604075)(4534165)(4627221)(201703031133081)(201702281549075)(2017052603307)(7153060)(7193020);SRVR:CY4PR15MB1751; X-Microsoft-Exchange-Diagnostics: 1;CY4PR15MB1751;3:/DKnvTSyII+C5PlaxULNjBUZ5Y1qBtn6N5MgMql49iE8s4JX8X5GSqP+shjY7IE7Uz7Twii9+QBHFW0iudk/XDIirfdmFhwKmeR60aFCfa19lQCCqIqh3Ebd+wqkr/6xMxrgKfvH9s39KmtD9884wzWnnZy7d/UQ+Pi7h6RMMdIFMYDq1u3JWzlcBDMn/UbTExZulfMWCAIAN9KSKRh2w8bv2rIm5fP0ZdEY6F5KT3gM+IbWw+ts/u3HQfBLEqcl;25:TJKzV2mf/rAoi6IJDams7P9h+IouGHVgcp28CAP7jU6iO335zT7jbb4oZQmxP5gaBaIXY0rfBe0eEXQOQrdDH/mSzBd3ECeNelUOyoNIxkxBbGxXoiXrQl/vFed/B4xq7W4pOowTPg10PXcX1VHvMBAx4wP9N/eMEqc+bvX1CbSvh6uPUfjaAFheTNa8oner4qCggUJTDqg1LxZaVJ4/Yq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raBkLFu9LA0BdJCWwufxo0sUecAx3A1GexkH9ZOwETY/3cRxnXgmJhGyZ1TFC44KWMtOQURL2N2ECnAIzfys/M8JMjsMDy3P/HJ8oIm9VxykUFOi50agsIGkNcUJ3Z9obP7TCpoSvVlDHBMK2FC/E6r1lshfTGLvu59jaJGukBD/OET4VVTaON4ZFRg+1uO4PgVUQWtrbW1n9qCjsxtFBi/vzUpZSOadZGbJbaaNfp3w4eopZjlLkXI= X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(67672495146484)(266576461109395); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040501)(2401047)(5005006)(8121501046)(3002001)(3231101)(11241501184)(944501161)(93006095)(93001095)(10201501046)(6041288)(20161123560045)(20161123564045)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123558120)(20161123562045)(6072148)(201708071742011);SRVR:CY4PR15MB1751;BCL:0;PCL:0;RULEID:;SRVR:CY4PR15MB1751; X-Forefront-PRVS: 0581B5AB35 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10019020)(6069001)(39860400002)(376002)(366004)(346002)(396003)(39380400002)(199004)(189003)(68736007)(33656002)(23726003)(6496006)(33896004)(106356001)(6116002)(54906003)(86362001)(316002)(15650500001)(7416002)(52396003)(9686003)(110136005)(58126008)(47776003)(1076002)(16586007)(50466002)(59450400001)(7736002)(305945005)(76506005)(83506002)(98436002)(76176011)(53936002)(386003)(25786009)(186003)(105586002)(16526019)(8936002)(2950100002)(6666003)(97736004)(8676002)(478600001)(4326008)(6486002)(81156014)(2906002)(81166006)(52116002)(5660300001)(18370500001);DIR:OUT;SFP:1102;SCL:1;SRVR:CY4PR15MB1751;H:localhost;FPR:;SPF:None;PTR:InfoNoRecords;A:1;MX:1;LANG:en; Received-SPF: None (protection.outlook.com: fb.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;CY4PR15MB1751;23:qORhojCMzgiJ2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paEoftat3WTc=;24:+4N3ff/m31SafJB4omRxWmRLKnmFLD+1UPZxQUmv4iILTbDM5yFsmLjKHjM1grfyz/RKIQtD+2xvWle9wQbzKlpp9xV8f5H4Fz1D9KbKin0=;7:UvyRnkxNl3OuAGKyOFLtuBDT0g6IPERo9fR6KuGJ0KjQQK5bKw+77zNwIV/Cv4BIKxaaakm0IzZBb5ZY2ESkQik2zSBFkH41EQ0gW7IIe4U0bqy67UDdNxwF++SxiojdG21g/QIvEZjeJXi3DSZps4tBC9tLgApxyOLGpoeYmplv1B49Gnyrw8z2kWrYiWsQFvkjXHDBR52sHbfZBzDnWIue4YQkwhEXAQ1Ts9G/FzuPPPHdmpFv0jGrNTl3EzE3 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;CY4PR15MB1751;20:S+iI0XoewMGtOqso/TpGmy2Kw0vjJTwA1O/0hWcr+JT9DQKJWViMHjb6sDv2buFWH0itphfM5ww4LIXjVhkKrw+Q/8BLRxdDzTAWX+U6iagHkrjkvr++zUGBOBabGJiwsH0h6gymzJuuCsHnTIlLBmKtzwkTxzCO3sWmTBGd/YA= X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Feb 2018 19:51:35.9902 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8bbdf3a2-cdc2-4154-f3eb-08d572520652 X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ae927fe-1255-47a7-a2af-5f3a069daaa2 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR15MB1751 X-OriginatorOrg: fb.com X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2018-02-12_08:,, signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add gcmaes_en/decrypt_sg routines, that will do scatter/gather by sg. Either src or dst may contain multiple buffers, so iterate over both at the same time if they are different. If the input is the same as the output, iterate only over one. Currently both the AAD and TAG must be linear, so copy them out with scatterlist_map_and_copy. Only the SSE routines are updated so far, so leave the previous gcmaes_en/decrypt routines, and branch to the sg ones if the keysize is inappropriate for avx, or we are SSE only. Signed-off-by: Dave Watson --- arch/x86/crypto/aesni-intel_glue.c | 166 +++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) diff --git a/arch/x86/crypto/aesni-intel_glue.c b/arch/x86/crypto/aesni-intel_glue.c index de986f9..1e32fbe 100644 --- a/arch/x86/crypto/aesni-intel_glue.c +++ b/arch/x86/crypto/aesni-intel_glue.c @@ -791,6 +791,82 @@ static int generic_gcmaes_set_authsize(struct crypto_aead *tfm, return 0; } +static int gcmaes_encrypt_sg(struct aead_request *req, unsigned int assoclen, + u8 *hash_subkey, u8 *iv, void *aes_ctx) +{ + struct crypto_aead *tfm = crypto_aead_reqtfm(req); + unsigned long auth_tag_len = crypto_aead_authsize(tfm); + struct gcm_context_data data AESNI_ALIGN_ATTR; + struct scatter_walk dst_sg_walk = {}; + unsigned long left = req->cryptlen; + unsigned long len, srclen, dstlen; + struct scatter_walk src_sg_walk; + struct scatterlist src_start[2]; + struct scatterlist dst_start[2]; + struct scatterlist *src_sg; + struct scatterlist *dst_sg; + u8 *src, *dst, *assoc; + u8 authTag[16]; + + assoc = kmalloc(assoclen, GFP_ATOMIC); + if (unlikely(!assoc)) + return -ENOMEM; + scatterwalk_map_and_copy(assoc, req->src, 0, assoclen, 0); + + src_sg = scatterwalk_ffwd(src_start, req->src, req->assoclen); + scatterwalk_start(&src_sg_walk, src_sg); + if (req->src != req->dst) { + dst_sg = scatterwalk_ffwd(dst_start, req->dst, req->assoclen); + scatterwalk_start(&dst_sg_walk, dst_sg); + } + + kernel_fpu_begin(); + aesni_gcm_init(aes_ctx, &data, iv, + hash_subkey, assoc, assoclen); + if (req->src != req->dst) { + while (left) { + src = scatterwalk_map(&src_sg_walk); + dst = scatterwalk_map(&dst_sg_walk); + srclen = scatterwalk_clamp(&src_sg_walk, left); + dstlen = scatterwalk_clamp(&dst_sg_walk, left); + len = min(srclen, dstlen); + if (len) + aesni_gcm_enc_update(aes_ctx, &data, + dst, src, len); + left -= len; + + scatterwalk_unmap(src); + scatterwalk_unmap(dst); + scatterwalk_advance(&src_sg_walk, len); + scatterwalk_advance(&dst_sg_walk, len); + scatterwalk_done(&src_sg_walk, 0, left); + scatterwalk_done(&dst_sg_walk, 1, left); + } + } else { + while (left) { + dst = src = scatterwalk_map(&src_sg_walk); + len = scatterwalk_clamp(&src_sg_walk, left); + if (len) + aesni_gcm_enc_update(aes_ctx, &data, + src, src, len); + left -= len; + scatterwalk_unmap(src); + scatterwalk_advance(&src_sg_walk, len); + scatterwalk_done(&src_sg_walk, 1, left); + } + } + aesni_gcm_finalize(aes_ctx, &data, authTag, auth_tag_len); + kernel_fpu_end(); + + kfree(assoc); + + /* Copy in the authTag */ + scatterwalk_map_and_copy(authTag, req->dst, + req->assoclen + req->cryptlen, + auth_tag_len, 1); + return 0; +} + static int gcmaes_encrypt(struct aead_request *req, unsigned int assoclen, u8 *hash_subkey, u8 *iv, void *aes_ctx) { @@ -802,6 +878,11 @@ static int gcmaes_encrypt(struct aead_request *req, unsigned int assoclen, struct scatter_walk dst_sg_walk = {}; struct gcm_context_data data AESNI_ALIGN_ATTR; + if (((struct crypto_aes_ctx *)aes_ctx)->key_length != AES_KEYSIZE_128 || + aesni_gcm_enc_tfm == aesni_gcm_enc) { + return gcmaes_encrypt_sg(req, assoclen, hash_subkey, iv, + aes_ctx); + } if (sg_is_last(req->src) && (!PageHighMem(sg_page(req->src)) || req->src->offset + req->src->length <= PAGE_SIZE) && @@ -854,6 +935,86 @@ static int gcmaes_encrypt(struct aead_request *req, unsigned int assoclen, return 0; } +static int gcmaes_decrypt_sg(struct aead_request *req, unsigned int assoclen, + u8 *hash_subkey, u8 *iv, void *aes_ctx) +{ + struct crypto_aead *tfm = crypto_aead_reqtfm(req); + unsigned long auth_tag_len = crypto_aead_authsize(tfm); + unsigned long left = req->cryptlen - auth_tag_len; + struct gcm_context_data data AESNI_ALIGN_ATTR; + struct scatter_walk dst_sg_walk = {}; + unsigned long len, srclen, dstlen; + struct scatter_walk src_sg_walk; + struct scatterlist src_start[2]; + struct scatterlist dst_start[2]; + struct scatterlist *src_sg; + struct scatterlist *dst_sg; + u8 *src, *dst, *assoc; + u8 authTagGen[16]; + u8 authTag[16]; + + assoc = kmalloc(assoclen, GFP_ATOMIC); + if (unlikely(!assoc)) + return -ENOMEM; + scatterwalk_map_and_copy(assoc, req->src, 0, assoclen, 0); + + src_sg = scatterwalk_ffwd(src_start, req->src, req->assoclen); + scatterwalk_start(&src_sg_walk, src_sg); + if (req->src != req->dst) { + dst_sg = scatterwalk_ffwd(dst_start, req->dst, req->assoclen); + scatterwalk_start(&dst_sg_walk, dst_sg); + } + + kernel_fpu_begin(); + aesni_gcm_init(aes_ctx, &data, iv, + hash_subkey, assoc, assoclen); + if (req->src != req->dst) { + while (left) { + src = scatterwalk_map(&src_sg_walk); + dst = scatterwalk_map(&dst_sg_walk); + srclen = scatterwalk_clamp(&src_sg_walk, left); + dstlen = scatterwalk_clamp(&dst_sg_walk, left); + len = min(srclen, dstlen); + if (len) + aesni_gcm_dec_update(aes_ctx, &data, + dst, src, len); + left -= len; + + scatterwalk_unmap(src); + scatterwalk_unmap(dst); + scatterwalk_advance(&src_sg_walk, len); + scatterwalk_advance(&dst_sg_walk, len); + scatterwalk_done(&src_sg_walk, 0, left); + scatterwalk_done(&dst_sg_walk, 1, left); + } + } else { + while (left) { + dst = src = scatterwalk_map(&src_sg_walk); + len = scatterwalk_clamp(&src_sg_walk, left); + if (len) + aesni_gcm_dec_update(aes_ctx, &data, + src, src, len); + left -= len; + scatterwalk_unmap(src); + scatterwalk_advance(&src_sg_walk, len); + scatterwalk_done(&src_sg_walk, 1, left); + } + } + aesni_gcm_finalize(aes_ctx, &data, authTagGen, auth_tag_len); + kernel_fpu_end(); + + kfree(assoc); + + /* Copy out original authTag */ + scatterwalk_map_and_copy(authTag, req->src, + req->assoclen + req->cryptlen - auth_tag_len, + auth_tag_len, 0); + + /* Compare generated tag with passed in tag. */ + return crypto_memneq(authTagGen, authTag, auth_tag_len) ? + -EBADMSG : 0; +} + static int gcmaes_decrypt(struct aead_request *req, unsigned int assoclen, u8 *hash_subkey, u8 *iv, void *aes_ctx) { @@ -868,6 +1029,11 @@ static int gcmaes_decrypt(struct aead_request *req, unsigned int assoclen, struct gcm_context_data data AESNI_ALIGN_ATTR; int retval = 0; + if (((struct crypto_aes_ctx *)aes_ctx)->key_length != AES_KEYSIZE_128 || + aesni_gcm_enc_tfm == aesni_gcm_enc) { + return gcmaes_decrypt_sg(req, assoclen, hash_subkey, iv, + aes_ctx); + } tempCipherLen = (unsigned long)(req->cryptlen - auth_tag_len); if (sg_is_last(req->src) && -- 2.9.5