From patchwork Thu Jun 27 10:26:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 167914 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp2148730ilk; Thu, 27 Jun 2019 03:28:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqzaozLubP841x0tb7t9i7vBs5dY1lOFM4rsY+I8XwRLqRiHV5hb97RUwCLwd4Qt+od1OQ40 X-Received: by 2002:a17:902:788e:: with SMTP id q14mr3826418pll.234.1561631292519; Thu, 27 Jun 2019 03:28:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1561631292; cv=none; d=google.com; s=arc-20160816; b=Vtube7U0u/6Liot3V4+etfv/nu743HDcrc1dvJwmMR4i3XpjBAZXhmPzLNcg7SU9EA jimNb07glOyaKB8HXkcfDfTUBkvsK7YTQNlFIofoiZLR/xIp3YRoa0O9SKaUvtXhPUcD eeRbGJ5l2eK1ESKGZ/gC4veUm6GIHaddeFQDuTb939iAHnq1Wjp0DTgbZsyzjXVMKb7U xQ/z4hAH6VCvMjr6vDfAMbDFN6lMBfg5YLh8RjSpyiuZ069/25ILZtO8Ii6Pf7NcFGK+ UzF1+fnZyxrYNL8EutkyEdy6VCypHcQHPaUqFiCxrOQfWzHzB+mac1jx7U4zZ1ItboOM UvYw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=u6Q45pPiPnuFNJpKPysKttSzbQMmfbBttYB0PrnTC+o=; b=mXeC++GBHUGOBYbVLs/kCRZSzKkZQqxU+g6Xfxt4ays2472GSyEAkUuXC+2FmTlcnN 2jTglCC0lAVyMGpOwoqO7uPDRwkPCjeZJbPrPo2kGr7qNQBBKpMAUMGzXkQUqpIm/4Ou UU5O+H1sdgxXi9yZjAwDX7QxHKU2CHDyp5OHLb0PSVkGGYT+zCtpnAzh7EF3toNY9CPy pSlZXXJIkRuIqBCQDGS+xQB1MTYuWjeLjyh60pXVn5kAxeCScXKXApzbC5omjxv9DI/e T6lGRiWoSHFSTdXNaL48oknmDs9I6zHL3f0tOeeKQ0F8xhBQoEuK6Va288PmJ/MkBEcU bKOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=yMCUEiJ1; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f32si4829522pjg.42.2019.06.27.03.28.12; Thu, 27 Jun 2019 03:28:12 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-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=@linaro.org header.s=google header.b=yMCUEiJ1; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726659AbfF0K2L (ORCPT + 3 others); Thu, 27 Jun 2019 06:28:11 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:40814 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726674AbfF0K2K (ORCPT ); Thu, 27 Jun 2019 06:28:10 -0400 Received: by mail-wm1-f66.google.com with SMTP id v19so5106135wmj.5 for ; Thu, 27 Jun 2019 03:28:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=u6Q45pPiPnuFNJpKPysKttSzbQMmfbBttYB0PrnTC+o=; b=yMCUEiJ1wmfKpip7QF0/jBbHIedZntLzBTadFMegGfUEfCiOukmCkANW6wl5JX5rnZ +EB1PLnwabwL8NvVy1tBiGhk0qlyeLMLpGp/1heEFvlMA+TJoF4d6I0UNYgMS/MBGrKM wKa1B85dJI9lxm2mK/GLdr9pDSRkWFsWYBzNQrUAFKz4Fhv4xOPI4yP5lxPV2VjrIFYa 0YgD6d9oBFnB3MBbEZkDhSqEqQ/lsePZd8SydDG1xLdrE3RIoi7w1jF1/UQM8ec3j9dH KkNiD43dGdQ0pb9wxUK1wixPV9JUrj08x1n6xrHW4Jd3Q0TKTMygGgdyXC2ApFwnRIk5 JDAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=u6Q45pPiPnuFNJpKPysKttSzbQMmfbBttYB0PrnTC+o=; b=FfBzfiJ4UawB6Mf9cY/cUKdlC1uQbrrPrqvXXpw+TbefV7zuBp0uFldMlxzC6sy5Ck 1Qb9pVa+Pdju9KcjQuzEQcIqHixPgWKOiAdIspnDQAGrMdnt4djVsFi8Dp/QGRi/60Rq /qSbDvCxGstGnIthc09lbtfPe34PDlNCIpYInoq6/EBD8yxXIrGtTcyxaq3HWzD/erTr iF+/kR7tV6RVdaSBa66aTo+s8McX5Y5tdBfoCRHhC6OubIDvHOxriMTd+mB5C0Jp98H0 DcnWhmcVowzdGisDpV08KQebIV6qbBClDIe7hbM2n0wIaKgv3oXMyO/GdtxCaSt/MaQs zwfw== X-Gm-Message-State: APjAAAU1aom8ByObHh2xhzCVy5qEOhT22DgJLzg0jzDhqQ1mJ+o+t4Ba TvJXRfZs5GdMl7bMf5q75w1FjE96AgU= X-Received: by 2002:a7b:cc97:: with SMTP id p23mr2833802wma.120.1561631288174; Thu, 27 Jun 2019 03:28:08 -0700 (PDT) Received: from localhost.localdomain (aaubervilliers-681-1-8-173.w90-88.abo.wanadoo.fr. [90.88.13.173]) by smtp.gmail.com with ESMTPSA id g2sm5584533wmh.0.2019.06.27.03.28.06 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 27 Jun 2019 03:28:07 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@kernel.org, Ard Biesheuvel Subject: [PATCH v3 16/32] crypto: ctr - add helper for performing a CTR encryption walk Date: Thu, 27 Jun 2019 12:26:31 +0200 Message-Id: <20190627102647.2992-17-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190627102647.2992-1-ard.biesheuvel@linaro.org> References: <20190627102647.2992-1-ard.biesheuvel@linaro.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Add a static inline helper modeled after crypto_cbc_encrypt_walk() that can be reused for SIMD algorithms that need to implement a non-SIMD fallback for performing CTR encryption. Signed-off-by: Ard Biesheuvel --- include/crypto/ctr.h | 50 ++++++++++++++++++++ 1 file changed, 50 insertions(+) -- 2.20.1 diff --git a/include/crypto/ctr.h b/include/crypto/ctr.h index 4180fc080e3b..d64017fae41c 100644 --- a/include/crypto/ctr.h +++ b/include/crypto/ctr.h @@ -13,8 +13,58 @@ #ifndef _CRYPTO_CTR_H #define _CRYPTO_CTR_H +#include +#include +#include +#include + #define CTR_RFC3686_NONCE_SIZE 4 #define CTR_RFC3686_IV_SIZE 8 #define CTR_RFC3686_BLOCK_SIZE 16 +static inline int crypto_ctr_encrypt_walk(struct skcipher_request *req, + void (*fn)(struct crypto_skcipher *, + const u8 *, u8 *)) +{ + struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); + int blocksize = crypto_skcipher_chunksize(tfm); + u8 buf[MAX_CIPHER_BLOCKSIZE]; + struct skcipher_walk walk; + int err; + + /* avoid integer division due to variable blocksize parameter */ + if (WARN_ON_ONCE(!is_power_of_2(blocksize))) + return -EINVAL; + + err = skcipher_walk_virt(&walk, req, false); + + while (walk.nbytes > 0) { + u8 *dst = walk.dst.virt.addr; + u8 *src = walk.src.virt.addr; + int nbytes = walk.nbytes; + int tail = 0; + + if (nbytes < walk.total) { + tail = walk.nbytes & (blocksize - 1); + nbytes -= tail; + } + + do { + int bsize = min(nbytes, blocksize); + + fn(tfm, walk.iv, buf); + + crypto_xor_cpy(dst, src, buf, bsize); + crypto_inc(walk.iv, blocksize); + + dst += bsize; + src += bsize; + nbytes -= bsize; + } while (nbytes > 0); + + err = skcipher_walk_done(&walk, tail); + } + return err; +} + #endif /* _CRYPTO_CTR_H */