From patchwork Tue Jul 2 19:41:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 168362 Delivered-To: patch@linaro.org Received: by 2002:a92:4782:0:0:0:0:0 with SMTP id e2csp4653529ilk; Tue, 2 Jul 2019 12:42:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqynZNEWIPtnlBJUhri5st2X3e6igrATNhRQ4D5wXkK7OePsw7EHqtXCC8J7P2VONZuLHePP X-Received: by 2002:a17:90a:8c90:: with SMTP id b16mr7448758pjo.133.1562096555179; Tue, 02 Jul 2019 12:42:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562096555; cv=none; d=google.com; s=arc-20160816; b=wE0I3g7VzSlDDq/9uwBidMv69/yXIsaFS19fPhkh/0v53t2BeP0TPiaZ+qSc4vzTtp Xwwc2yX2SHe4bd27czRM7o+J44AtEYfiMIbUSXQjUefjOYb639Cs/xYMBTg2CXsokQqZ zVD+UO0wTF6ra9MXliKNXMQsCv0nJTwLBPE1PvARd0llbuaSnpulbkEdh3O4/+4ndlV2 cT8idKePRKkonB70GGCTyUBeClHjK0b22WardfWcRyoRLdPQb85lBgLhjxxHDZEX8Eb5 jt55ctuPPfEyAYmEEMdrbrRevr6LSXvtr1CajFCeFQy4EC77h9MQJhnzpBWlpgdoeXDD RXXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=cNds9JAEq1hTUuemrXbQh9sj10to3vETzPkNzlmk2jM=; b=GghvJt62TIytoIDYLpGkkWD09NnDDjbBsHf8T85m+ACYzQzuvvTNHzIAmBqrXH2yd/ IT/RaGIKQLpaYtcW4S/MD/9TGmr3M7WQ3oSzh1MT8DtYp0Qb0OgrI1pY2PX0ZB8gkH5+ 6Y470LDEYHJPBxwnUR63Lb6feIqG4QeBJE+Lnd786E9q8zmTD/ZkBqvovD3Lhah12Mqt 3rdKcVPiE3BTUgXm77M/9es51J7Noi9AF9PpOycBTgfHFWaxbFpS4xvsoCyP6YNqaXHO zJNyFPZ0H1xb5fb9gBHUoX6n0A1UFaW4324PFL2UXUsvGGMGmK/QSCppM6ztYOZ8MZp4 9EJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=u38mRObk; 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 x13si13373092pgh.116.2019.07.02.12.42.34; Tue, 02 Jul 2019 12:42:35 -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=u38mRObk; 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 S1727074AbfGBTme (ORCPT + 3 others); Tue, 2 Jul 2019 15:42:34 -0400 Received: from mail-lf1-f67.google.com ([209.85.167.67]:34597 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727069AbfGBTme (ORCPT ); Tue, 2 Jul 2019 15:42:34 -0400 Received: by mail-lf1-f67.google.com with SMTP id b29so2309621lfq.1 for ; Tue, 02 Jul 2019 12:42:32 -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; bh=cNds9JAEq1hTUuemrXbQh9sj10to3vETzPkNzlmk2jM=; b=u38mRObkPGCNBpkUfqFku8ZoUy0UumUJnobS0qIyeuCnatakwjiXwIbv5q+4gcD1/f hYpaqvE42a5zxZ/usTOvnSI8b3jOwElOCHwPQYQW3ROwY/T2690uL/bfxsmeTasOeTP3 9usz4Xg7U/zL5Xtoa92i8QccFmxZu5LiBt6Ipvj3Y2kEaHVTy6XUT+GE07S2tHMP/m+W YafR6IEU/XjJV87YhXqBOdImyaydmRwGupfGaakTMPiY7kH/v5V/mJoPhiR1Jz3TrGom WMZlYooS06YsixisHAUprj9ISqe2Sl8EmSfT7rydvgUt5MBpqvgTW13w//FgPcJYvY5R tmLg== 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; bh=cNds9JAEq1hTUuemrXbQh9sj10to3vETzPkNzlmk2jM=; b=TxQpWk94ywE5dwYcUijjmJ7td3fO8M8iHFNOpZejxv5quDMZlk93ADJS21iNaNk9F0 gbi2+NXVA8je/YlbJGmrW32tbaKvDWpvWREoRHaBZc9VxkNgCWeMKxDAwH8ACIZekLyX wuSgjXQUSSmc2hLg20qfu4CBAf+C7O6HjXqfa26LZSLlCZubgUQBW6y2NiQyqJoT20QN NDgHoznDaJUH69hwtDuxrYon2mRxCtMLtILdhZBvCaXNaFBPGTaUfDqib8GLqlRSVIB1 XEdjCUg6aaqUxdPinyTBBdfMPtQrmwmvNXdUV0NSpdh03Bo8CsOl4b2YgRXPJRSH7zAU bARA== X-Gm-Message-State: APjAAAWmanSc4WxJi6hCn5+oL3h4wCghKfeVWq7qh1IecxpMZWCGwOdU 8Q28y8O0zXHCXqpoBgVAfAnWs25A4NWA8OnF X-Received: by 2002:ac2:52b7:: with SMTP id r23mr1871076lfm.120.1562096551941; Tue, 02 Jul 2019 12:42:31 -0700 (PDT) Received: from e111045-lin.arm.com (89-212-78-239.static.t-2.net. [89.212.78.239]) by smtp.gmail.com with ESMTPSA id 24sm4475163ljs.63.2019.07.02.12.42.30 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 02 Jul 2019 12:42:31 -0700 (PDT) From: Ard Biesheuvel To: linux-crypto@vger.kernel.org Cc: herbert@gondor.apana.org.au, ebiggers@google.com, Ard Biesheuvel Subject: [PATCH v4 16/32] crypto: ctr - add helper for performing a CTR encryption walk Date: Tue, 2 Jul 2019 21:41:34 +0200 Message-Id: <20190702194150.10405-17-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190702194150.10405-1-ard.biesheuvel@linaro.org> References: <20190702194150.10405-1-ard.biesheuvel@linaro.org> 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.17.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 */