From patchwork Thu Aug 2 21:51:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 10554271 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3AB1E15E9 for ; Thu, 2 Aug 2018 21:52:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2BB5D2C037 for ; Thu, 2 Aug 2018 21:52:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1FFE52C26F; Thu, 2 Aug 2018 21:52:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E7C642C037 for ; Thu, 2 Aug 2018 21:52:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729676AbeHBXpY (ORCPT ); Thu, 2 Aug 2018 19:45:24 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:33831 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732367AbeHBXoa (ORCPT ); Thu, 2 Aug 2018 19:44:30 -0400 Received: by mail-pf1-f194.google.com with SMTP id k19-v6so2075694pfi.1 for ; Thu, 02 Aug 2018 14:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MvbNFc7J67UoB1P19UBzk47KUOfTw9hDrlQkJaXB1ws=; b=nYio86QuhOWHpnAL5T7WiLnnwS5QEEhFwEQ0zTGvAohb/EnkIwU4yAOk+mTGjOMsnv zq8Q2mLSV3Gz7fzVln8tDy4j+C8V/Nm+MFDQN4BHpY+uoyuI+lHj2rJdPGBr/O/zLcdX +h0Rv+oQ9m954BN4f3Znp0HJe/l6u5PfsN6lY= 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=MvbNFc7J67UoB1P19UBzk47KUOfTw9hDrlQkJaXB1ws=; b=YilNsUxeReTxI2WCtaXKnwcbum2RpeUgbc5gbLO7/Pc97QWlZbrXWv+7bIMmrkiUEa nOoLY+UvwCk6601FyT7HGiVOIbKMn86wo5uBincaOIlzZOXkJ4uukTUcp/Lt8P/NVoBE 5xC3U1x0lYyUUG94dAOoxIZjfxPtyT3gkhAmQ/LVh1/nNVNWxRoQEHW0JhTU9r70SUNu ZBt+NPNwxlel7rYrR4svAJ9iklzi3+1LLXCnmCSi9NWH8zGbfS+7rzkABdtS9Y7AhbST 6hg9EMiewB1nEL9DoF0QQqF4waZQySQaWtHrXG6tNmnyLIspHxXzGrzopKLzzmGrIe1y XUpA== X-Gm-Message-State: AOUpUlHh6Dte5my47Q7P/St9Tyvcg2ml+wQFbeHL5T+8FXqrcWBHQQ6h cLnLcDq83SMMCXtolUkLSOGIcg== X-Google-Smtp-Source: AAOMgpcta7HkStKHr1CZCAhA1VwoeiRogr4gz2vXVehyew/VoH3h7vNgtlWoJd+dc6+9kLZToTjTZg== X-Received: by 2002:a62:3ece:: with SMTP id y75-v6mr1290956pfj.7.1533246687428; Thu, 02 Aug 2018 14:51:27 -0700 (PDT) Received: from www.outflux.net (173-164-112-133-Oregon.hfc.comcastbusiness.net. [173.164.112.133]) by smtp.gmail.com with ESMTPSA id t9-v6sm6624376pgi.87.2018.08.02.14.51.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 02 Aug 2018 14:51:23 -0700 (PDT) From: Kees Cook To: Herbert Xu Cc: Kees Cook , Geliang Tang , Arnd Bergmann , Haren Myneni , Anton Vorontsov , Colin Cross , Tony Luck , Zhou Wang , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/9] crypto: add zbufsize() interface Date: Thu, 2 Aug 2018 14:51:10 -0700 Message-Id: <20180802215118.17752-2-keescook@chromium.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180802215118.17752-1-keescook@chromium.org> References: <20180802215118.17752-1-keescook@chromium.org> Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When pstore was refactored to use the crypto compress API in: commit cb3bee0369bc ("pstore: Use crypto compress API") nearly all the pstore-specific compression routines were replaced with the existing crypto compression API. One case remained: calculating the "worst case" compression sizes ahead of time so it could have a buffer preallocated for doing compression (which was called "zbufsize"). To make pstore fully algorithm-agnostic, the compression API needs to grow this functionality. This adds the interface to support querying the "worst case" estimate, with a new "zbufsize" routine that each compressor can implement. The per-compressor implementations come in later commits. Signed-off-by: Kees Cook --- crypto/compress.c | 9 +++++++++ include/crypto/internal/scompress.h | 11 +++++++++++ include/linux/crypto.h | 12 ++++++++++++ 3 files changed, 32 insertions(+) diff --git a/crypto/compress.c b/crypto/compress.c index f2d522924a07..29a80bb3b9d3 100644 --- a/crypto/compress.c +++ b/crypto/compress.c @@ -33,12 +33,21 @@ static int crypto_decompress(struct crypto_tfm *tfm, dlen); } +static int crypto_zbufsize(struct crypto_tfm *tfm, + unsigned int slen, unsigned int *dlen) +{ + if (!tfm->__crt_alg->cra_compress.coa_zbufsize) + return -ENOTSUPP; + return tfm->__crt_alg->cra_compress.coa_zbufsize(tfm, slen, dlen); +} + int crypto_init_compress_ops(struct crypto_tfm *tfm) { struct compress_tfm *ops = &tfm->crt_compress; ops->cot_compress = crypto_compress; ops->cot_decompress = crypto_decompress; + ops->cot_zbufsize = crypto_zbufsize; return 0; } diff --git a/include/crypto/internal/scompress.h b/include/crypto/internal/scompress.h index 0f6ddac1acfc..a4a2a55080ad 100644 --- a/include/crypto/internal/scompress.h +++ b/include/crypto/internal/scompress.h @@ -39,6 +39,8 @@ struct scomp_alg { int (*decompress)(struct crypto_scomp *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx); + int (*zbufsize)(struct crypto_scomp *tfm, unsigned int slen, + unsigned int *dlen, void *ctx); struct crypto_alg base; }; @@ -94,6 +96,15 @@ static inline int crypto_scomp_decompress(struct crypto_scomp *tfm, ctx); } +static inline int crypto_scomp_zbufsize(struct crypto_scomp *tfm, + unsigned int slen, + unsigned int *dlen, void *ctx) +{ + if (!crypto_scomp_alg(tfm)->zbufsize) + return -ENOTSUPP; + return crypto_scomp_alg(tfm)->zbufsize(tfm, slen, dlen, ctx); +} + int crypto_init_scomp_ops_async(struct crypto_tfm *tfm); struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req); void crypto_acomp_scomp_free_ctx(struct acomp_req *req); diff --git a/include/linux/crypto.h b/include/linux/crypto.h index 6eb06101089f..376c056447e7 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h @@ -362,6 +362,8 @@ struct compress_alg { unsigned int slen, u8 *dst, unsigned int *dlen); int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen); + int (*coa_zbufsize)(struct crypto_tfm *tfm, unsigned int slen, + unsigned int *dlen); }; @@ -578,6 +580,8 @@ struct compress_tfm { int (*cot_decompress)(struct crypto_tfm *tfm, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen); + int (*cot_zbufsize)(struct crypto_tfm *tfm, + unsigned int slen, unsigned int *dlen); }; #define crt_ablkcipher crt_u.ablkcipher @@ -1660,5 +1664,13 @@ static inline int crypto_comp_decompress(struct crypto_comp *tfm, src, slen, dst, dlen); } +static inline int crypto_comp_zbufsize(struct crypto_comp *tfm, + unsigned int slen, + unsigned int *dlen) +{ + return crypto_comp_crt(tfm)->cot_zbufsize(crypto_comp_tfm(tfm), + slen, dlen); +} + #endif /* _LINUX_CRYPTO_H */