From patchwork Tue Jan 26 08:15:11 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joonsoo Kim X-Patchwork-Id: 8118671 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 7B97BBEEE5 for ; Tue, 26 Jan 2016 08:16:26 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 896162026C for ; Tue, 26 Jan 2016 08:16:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8127C20270 for ; Tue, 26 Jan 2016 08:16:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933918AbcAZIQO (ORCPT ); Tue, 26 Jan 2016 03:16:14 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:33468 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933749AbcAZIQJ (ORCPT ); Tue, 26 Jan 2016 03:16:09 -0500 Received: by mail-pf0-f195.google.com with SMTP id e65so8018093pfe.0; Tue, 26 Jan 2016 00:16:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=yVJy5St3oCa4YNHXFUw9hGH3spQepmY4PrLSUfUIH70=; b=MmlFZxw5pD5z4VRUVm1wzbQGXtDaSPNb0t3r26RScZGocxE6aMB+U5k2k9gARJUDbT E+ixAUN308+C3q4m8zK7MR0Oa44Th0uITI9lPj8Q4wEJs4k+YQPMQ8APlmTN2nMfo4g9 7a7xJwJMdG+VUigIRJZtiOLZtiiJz6MZ/QImwe/oZIHc6XWC0kAWsgVZ0elp3/31dDJr y1lxrqPulJHLiERNCf/1LU3T5KEXXMpLFNPQ8dH6ySKDK/r+t+V60ktNBJq04tMILsFY 48+1M10A+kLmFrvOgIvIg31J+btxl/tz6Hk9kPYTb4vT/9UQMXzK2L7Tbqrlme7OKWaH DI6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=yVJy5St3oCa4YNHXFUw9hGH3spQepmY4PrLSUfUIH70=; b=VgtiDKrU8OrBSkLSZHKiVqRaGDFnwwUhjJJ4X/I1vIseKnJep49gOCaEkhS4ucU5nD QoAZZQLGE/RByJvSrW4iBfDIUHMazC3WFFBi2wUFDKi42pcq7nDdSVfGUav7o+UsOicd hYbaESO1FfngAKNG2VB2acvVzvFOR72VKnjm++8qQppYBGnFk4lgzmp16vpp7JwaC32h YADmELeBAhyYh+loLES4Ad+Cgk/KZ+xkz4XW79wJG5C/lN5k1U8HMg4XE9lqI9Uy1FNU hcjqUgS+9Mc3BSLC+PWef+jBMrg/0Sg8SRS7ZeUTv3o1xYDv3H+OjjO4q2W0Tb+/Kmrv 7yNg== X-Gm-Message-State: AG10YOT7oNqKWrhQWtrPhUisBBvHUHy2nL64624eO3nfwBsVFPvC5Iu/hiDjnJKaV0Msmw== X-Received: by 10.98.75.22 with SMTP id y22mr32671468pfa.147.1453796168971; Tue, 26 Jan 2016 00:16:08 -0800 (PST) Received: from localhost.localdomain ([119.69.155.252]) by smtp.gmail.com with ESMTPSA id d21sm342139pfj.32.2016.01.26.00.16.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 26 Jan 2016 00:16:07 -0800 (PST) From: Joonsoo Kim X-Google-Original-From: Joonsoo Kim To: Herbert Xu Cc: "David S. Miller" , Weigang Li , Sergey Senozhatsky , Minchan Kim , Dan Streetman , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Joonsoo Kim Subject: [PATCH v2 09/10] crypto/deflate: support new compression APIs Date: Tue, 26 Jan 2016 17:15:11 +0900 Message-Id: <1453796112-14273-10-git-send-email-iamjoonsoo.kim@lge.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1453796112-14273-1-git-send-email-iamjoonsoo.kim@lge.com> References: <1453796112-14273-1-git-send-email-iamjoonsoo.kim@lge.com> 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.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable 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 Now, new compression APIs are introduced and it has some benefits. Let's support it. Signed-off-by: Joonsoo Kim --- crypto/Kconfig | 1 + crypto/deflate.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 101 insertions(+), 10 deletions(-) diff --git a/crypto/Kconfig b/crypto/Kconfig index 351b859..728f88e 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -1492,6 +1492,7 @@ config CRYPTO_DEFLATE select CRYPTO_ALGAPI select ZLIB_INFLATE select ZLIB_DEFLATE + select CRYPTO_COMPRESS2 help This is the Deflate algorithm (RFC1951), specified for use in IPSec with the IPCOMP protocol (RFC3173, RFC2394). diff --git a/crypto/deflate.c b/crypto/deflate.c index 95d8d37..1f8f633 100644 --- a/crypto/deflate.c +++ b/crypto/deflate.c @@ -33,6 +33,8 @@ #include #include +#include + #define DEFLATE_DEF_LEVEL Z_DEFAULT_COMPRESSION #define DEFLATE_DEF_WINBITS 11 #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL @@ -101,9 +103,8 @@ static void deflate_decomp_exit(struct deflate_ctx *ctx) vfree(ctx->decomp_stream.workspace); } -static int deflate_init(struct crypto_tfm *tfm) +static int __deflate_init(void *ctx) { - struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); int ret; ret = deflate_comp_init(ctx); @@ -116,19 +117,54 @@ out: return ret; } -static void deflate_exit(struct crypto_tfm *tfm) +static void *deflate_alloc_ctx(struct crypto_scomp *tfm) +{ + void *ctx; + int ret; + + ctx = kzalloc(sizeof(struct deflate_ctx), GFP_KERNEL); + if (!ctx) + return ERR_PTR(-ENOMEM); + + ret = __deflate_init(ctx); + if (ret) { + kfree(ctx); + return ERR_PTR(ret); + } + + return ctx; +} + +static int deflate_init(struct crypto_tfm *tfm) { struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); + return __deflate_init(ctx); +} + +static void __deflate_exit(void *ctx) +{ deflate_comp_exit(ctx); deflate_decomp_exit(ctx); } -static int deflate_compress(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx) +{ + __deflate_exit(ctx); +} + +static void deflate_exit(struct crypto_tfm *tfm) +{ + struct deflate_ctx *ctx = crypto_tfm_ctx(tfm); + + __deflate_exit(ctx); +} + +static int __deflate_compress(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { int ret = 0; - struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + struct deflate_ctx *dctx = ctx; struct z_stream_s *stream = &dctx->comp_stream; ret = zlib_deflateReset(stream); @@ -153,12 +189,26 @@ out: return ret; } -static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src, - unsigned int slen, u8 *dst, unsigned int *dlen) +static int deflate_compress(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + + return __deflate_compress(src, slen, dst, dlen, dctx); +} + +static int deflate_scompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __deflate_compress(src, slen, dst, dlen, ctx); +} + +static int __deflate_decompress(const u8 *src, unsigned int slen, + u8 *dst, unsigned int *dlen, void *ctx) { int ret = 0; - struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + struct deflate_ctx *dctx = ctx; struct z_stream_s *stream = &dctx->decomp_stream; ret = zlib_inflateReset(stream); @@ -194,6 +244,20 @@ out: return ret; } +static int deflate_decompress(struct crypto_tfm *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen) +{ + struct deflate_ctx *dctx = crypto_tfm_ctx(tfm); + + return __deflate_decompress(src, slen, dst, dlen, dctx); +} + +static int deflate_sdecompress(struct crypto_scomp *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int *dlen, void *ctx) +{ + return __deflate_decompress(src, slen, dst, dlen, ctx); +} + static struct crypto_alg alg = { .cra_name = "deflate", .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, @@ -206,14 +270,40 @@ static struct crypto_alg alg = { .coa_decompress = deflate_decompress } } }; +static struct scomp_alg scomp = { + .alloc_ctx = deflate_alloc_ctx, + .free_ctx = deflate_free_ctx, + .compress = deflate_scompress, + .decompress = deflate_sdecompress, + .base = { + .cra_name = "deflate", + .cra_driver_name= "deflate-scomp", + .cra_flags = CRYPTO_ALG_TYPE_SCOMPRESS, + .cra_module = THIS_MODULE, + } +}; + static int __init deflate_mod_init(void) { - return crypto_register_alg(&alg); + int ret; + + ret = crypto_register_alg(&alg); + if (ret) + return ret; + + ret = crypto_register_scomp(&scomp); + if (ret) { + crypto_unregister_alg(&alg); + return ret; + } + + return ret; } static void __exit deflate_mod_fini(void) { crypto_unregister_alg(&alg); + crypto_unregister_scomp(&scomp); } module_init(deflate_mod_init);