Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys small modifications in dealing with the unknown result s...



details:   https://anonhg.NetBSD.org/src/rev/0902624619bb
branches:  trunk
changeset: 762648:0902624619bb
user:      drochner <drochner%NetBSD.org@localhost>
date:      Thu Feb 24 20:03:41 2011 +0000

description:
small modifications in dealing with the unknown result size of compression/
decompression:
-seperate the IPCOMP specific rule that compression must not grow the
 data from general compression semantics: Introduce a special name
 CRYPTO_DEFLATE_COMP_NOGROW/comp_algo_deflate_nogrow to describe
 the IPCOMP semantics and use it there. (being here, fix the check
 so that equal size is considered failure as well as required by
 RFC2393)
 Customers of CRYPTO_DEFLATE_COMP/comp_algo_deflate now always get
 deflated data back, even if they are not smaller than the original.
-allow to pass a "size hint" to the DEFLATE decompression function
 which is used for the initial buffer allocation. Due to the changes
 done there, additional allocations and extra copies are avoided if the
 initial allocation is sufficient. Set the size hint to MCLBYTES (=2k)
 in IPCOMP which should be good for many use cases.

diffstat:

 sys/netipsec/xform_ipcomp.c       |   7 ++++---
 sys/opencrypto/cryptodev.h        |   7 ++++---
 sys/opencrypto/cryptosoft.c       |  21 +++++++++++++++------
 sys/opencrypto/cryptosoft_xform.c |  32 ++++++++++++++++++++------------
 sys/opencrypto/deflate.c          |  11 ++++++-----
 sys/opencrypto/deflate.h          |   6 +++---
 sys/opencrypto/xform.c            |   9 +++++++--
 sys/opencrypto/xform.h            |   3 ++-
 8 files changed, 61 insertions(+), 35 deletions(-)

diffs (truncated from 336 to 300 lines):

diff -r 6964aa635eb8 -r 0902624619bb sys/netipsec/xform_ipcomp.c
--- a/sys/netipsec/xform_ipcomp.c       Thu Feb 24 20:01:53 2011 +0000
+++ b/sys/netipsec/xform_ipcomp.c       Thu Feb 24 20:03:41 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xform_ipcomp.c,v 1.24 2011/02/18 20:40:58 drochner Exp $       */
+/*     $NetBSD: xform_ipcomp.c,v 1.25 2011/02/24 20:03:41 drochner Exp $       */
 /*     $FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $       */
 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
 
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.24 2011/02/18 20:40:58 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.25 2011/02/24 20:03:41 drochner Exp $");
 
 /* IP payload compression protocol (IPComp), see RFC 2393 */
 #include "opt_inet.h"
@@ -96,7 +96,7 @@
                return NULL;
        switch (alg) {
        case SADB_X_CALG_DEFLATE:
-               return &comp_algo_deflate;
+               return &comp_algo_deflate_nogrow;
        }
        return NULL;
 }
@@ -190,6 +190,7 @@
 
        /* Crypto operation descriptor */
        crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
+       crp->crp_olen = MCLBYTES; /* hint to decompression code */
        crp->crp_flags = CRYPTO_F_IMBUF;
        crp->crp_buf = m;
        crp->crp_callback = ipcomp_input_cb;
diff -r 6964aa635eb8 -r 0902624619bb sys/opencrypto/cryptodev.h
--- a/sys/opencrypto/cryptodev.h        Thu Feb 24 20:01:53 2011 +0000
+++ b/sys/opencrypto/cryptodev.h        Thu Feb 24 20:03:41 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cryptodev.h,v 1.17 2009/09/04 08:58:44 he Exp $ */
+/*     $NetBSD: cryptodev.h,v 1.18 2011/02/24 20:03:41 drochner Exp $ */
 /*     $FreeBSD: src/sys/opencrypto/cryptodev.h,v 1.2.2.6 2003/07/02 17:04:50 sam Exp $        */
 /*     $OpenBSD: cryptodev.h,v 1.33 2002/07/17 23:52:39 art Exp $      */
 
@@ -132,8 +132,9 @@
 #define CRYPTO_MD5_HMAC_96     19 
 #define CRYPTO_SHA1_HMAC_96    20
 #define CRYPTO_RIPEMD160_HMAC_96       21
-#define CRYPTO_GZIP_COMP       22 /* Deflate compression algorithm */
-#define CRYPTO_ALGORITHM_MAX   23 /* Keep updated - see below */
+#define CRYPTO_GZIP_COMP       22 /* gzip compression algorithm */
+#define CRYPTO_DEFLATE_COMP_NOGROW 23 /* Deflate, fail if not compressible */
+#define CRYPTO_ALGORITHM_MAX   24 /* Keep updated - see below */
 
 /* Algorithm flags */
 #define        CRYPTO_ALG_FLAG_SUPPORTED       0x01 /* Algorithm is supported */
diff -r 6964aa635eb8 -r 0902624619bb sys/opencrypto/cryptosoft.c
--- a/sys/opencrypto/cryptosoft.c       Thu Feb 24 20:01:53 2011 +0000
+++ b/sys/opencrypto/cryptosoft.c       Thu Feb 24 20:03:41 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cryptosoft.c,v 1.27 2011/02/10 21:00:42 drochner Exp $ */
+/*     $NetBSD: cryptosoft.c,v 1.28 2011/02/24 20:03:41 drochner Exp $ */
 /*     $FreeBSD: src/sys/opencrypto/cryptosoft.c,v 1.2.2.1 2002/11/21 23:34:23 sam Exp $       */
 /*     $OpenBSD: cryptosoft.c,v 1.35 2002/04/26 08:43:50 deraadt Exp $ */
 
@@ -24,7 +24,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.27 2011/02/10 21:00:42 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cryptosoft.c,v 1.28 2011/02/24 20:03:41 drochner Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -535,7 +535,8 @@
        if (crd->crd_flags & CRD_F_COMP)
                result = cxf->compress(data, crd->crd_len, &out);
        else
-               result = cxf->decompress(data, crd->crd_len, &out);
+               result = cxf->decompress(data, crd->crd_len, &out,
+                                        *res_size);
 
        free(data, M_CRYPTO_DATA);
        if (result == 0)
@@ -546,12 +547,12 @@
         */
        *res_size = (int)result;
        /* Check the compressed size when doing compression */
-       if (crd->crd_flags & CRD_F_COMP) {
-               if (result > crd->crd_len) {
+       if (crd->crd_flags & CRD_F_COMP &&
+           sw->sw_alg == CRYPTO_DEFLATE_COMP_NOGROW &&
+           result >= crd->crd_len) {
                        /* Compression was useless, we lost time */
                        free(out, M_CRYPTO_DATA);
                        return 0;
-               }
        }
 
        COPYBACK(outtype, buf, crd->crd_skip, result, out);
@@ -788,6 +789,11 @@
                        (*swd)->sw_cxf = cxf;
                        break;
 
+               case CRYPTO_DEFLATE_COMP_NOGROW:
+                       cxf = &swcr_comp_algo_deflate_nogrow;
+                       (*swd)->sw_cxf = cxf;
+                       break;
+
                case CRYPTO_GZIP_COMP:
                        cxf = &swcr_comp_algo_gzip;
                        (*swd)->sw_cxf = cxf;
@@ -884,6 +890,7 @@
                        break;
 
                case CRYPTO_DEFLATE_COMP:
+               case CRYPTO_DEFLATE_COMP_NOGROW:
                case CRYPTO_GZIP_COMP:
                        cxf = swd->sw_cxf;
                        break;
@@ -983,6 +990,7 @@
                        break;
 
                case CRYPTO_DEFLATE_COMP:
+               case CRYPTO_DEFLATE_COMP_NOGROW:
                case CRYPTO_GZIP_COMP:
                        DPRINTF(("swcr_process: compdec for %d\n", sw->sw_alg));
                        if ((crp->crp_etype = swcr_compdec(crd, sw,
@@ -1036,6 +1044,7 @@
        REGISTER(CRYPTO_SHA1);
        REGISTER(CRYPTO_RIJNDAEL128_CBC);
        REGISTER(CRYPTO_DEFLATE_COMP);
+       REGISTER(CRYPTO_DEFLATE_COMP_NOGROW);
        REGISTER(CRYPTO_GZIP_COMP);
 #undef REGISTER
 }
diff -r 6964aa635eb8 -r 0902624619bb sys/opencrypto/cryptosoft_xform.c
--- a/sys/opencrypto/cryptosoft_xform.c Thu Feb 24 20:01:53 2011 +0000
+++ b/sys/opencrypto/cryptosoft_xform.c Thu Feb 24 20:03:41 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cryptosoft_xform.c,v 1.13 2011/02/18 19:56:02 drochner Exp $ */
+/*     $NetBSD: cryptosoft_xform.c,v 1.14 2011/02/24 20:03:41 drochner Exp $ */
 /*     $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $    */
 /*     $OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $     */
 
@@ -40,7 +40,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.13 2011/02/18 19:56:02 drochner Exp $");
+__KERNEL_RCSID(1, "$NetBSD: cryptosoft_xform.c,v 1.14 2011/02/24 20:03:41 drochner Exp $");
 
 #include <crypto/blowfish/blowfish.h>
 #include <crypto/cast128/cast128.h>
@@ -70,9 +70,9 @@
 };
 
 struct swcr_comp_algo {
-       const struct comp_algo *comp_algo;
+       const struct comp_algo *unused_comp_algo;
        uint32_t (*compress)(uint8_t *, uint32_t, uint8_t **);
-       uint32_t (*decompress)(uint8_t *, uint32_t, uint8_t **);
+       uint32_t (*decompress)(uint8_t *, uint32_t, uint8_t **, int);
 };
 
 static void null_encrypt(void *, u_int8_t *);
@@ -124,9 +124,9 @@
 static int SHA512Update_int(void *, const u_int8_t *, u_int16_t);
 
 static u_int32_t deflate_compress(u_int8_t *, u_int32_t, u_int8_t **);
-static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **);
+static u_int32_t deflate_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
 static u_int32_t gzip_compress(u_int8_t *, u_int32_t, u_int8_t **);
-static u_int32_t gzip_decompress(u_int8_t *, u_int32_t, u_int8_t **);
+static u_int32_t gzip_decompress(u_int8_t *, u_int32_t, u_int8_t **, int);
 
 /* Encryption instances */
 static const struct swcr_enc_xform swcr_enc_xform_null = {
@@ -280,6 +280,12 @@
        deflate_decompress
 };
 
+static const struct swcr_comp_algo swcr_comp_algo_deflate_nogrow = {
+       &comp_algo_deflate_nogrow,
+       deflate_compress,
+       deflate_decompress
+};
+
 static const struct swcr_comp_algo swcr_comp_algo_gzip = {
        &comp_algo_deflate,
        gzip_compress,
@@ -638,23 +644,25 @@
 static u_int32_t
 deflate_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
 {
-       return deflate_global(data, size, 0, out);
+       return deflate_global(data, size, 0, out, 0);
 }
 
 static u_int32_t
-deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out)
+deflate_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
+                  int size_hint)
 {
-       return deflate_global(data, size, 1, out);
+       return deflate_global(data, size, 1, out, size_hint);
 }
 
 static u_int32_t
 gzip_compress(u_int8_t *data, u_int32_t size, u_int8_t **out)
 {
-       return gzip_global(data, size, 0, out);
+       return gzip_global(data, size, 0, out, 0);
 }
 
 static u_int32_t
-gzip_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out)
+gzip_decompress(u_int8_t *data, u_int32_t size, u_int8_t **out,
+               int size_hint)
 {
-       return gzip_global(data, size, 1, out);
+       return gzip_global(data, size, 1, out, size_hint);
 }
diff -r 6964aa635eb8 -r 0902624619bb sys/opencrypto/deflate.c
--- a/sys/opencrypto/deflate.c  Thu Feb 24 20:01:53 2011 +0000
+++ b/sys/opencrypto/deflate.c  Thu Feb 24 20:03:41 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: deflate.c,v 1.18 2011/02/18 22:02:09 drochner Exp $ */
+/*     $NetBSD: deflate.c,v 1.19 2011/02/24 20:03:41 drochner Exp $ */
 /*     $FreeBSD: src/sys/opencrypto/deflate.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $  */
 /* $OpenBSD: deflate.c,v 1.3 2001/08/20 02:45:22 hugh Exp $ */
 
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: deflate.c,v 1.18 2011/02/18 22:02:09 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: deflate.c,v 1.19 2011/02/24 20:03:41 drochner Exp $");
 
 #include <sys/types.h>
 #include <sys/malloc.h>
@@ -71,7 +71,8 @@
 }
 
 u_int32_t
-deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out)
+deflate_global(u_int8_t *data, u_int32_t size, int decomp, u_int8_t **out,
+              int size_hint)
 {
        /* decomp indicates whether we compress (0) or decompress (1) */
 
@@ -100,7 +101,7 @@
                 * updated while the decompression is going on
                 */
 
-               buf[0].size = size * 4;
+               buf[0].size = MAX(size * 4, size_hint);
        }
        buf[0].out = malloc(buf[0].size, M_CRYPTO_DATA, M_NOWAIT);
        if (buf[0].out == NULL)
@@ -218,7 +219,7 @@
 
 u_int32_t
 gzip_global(u_int8_t *data, u_int32_t size,
-       int decomp, u_int8_t **out)
+       int decomp, u_int8_t **out, int size_hint)
 {
        /* decomp indicates whether we compress (0) or decompress (1) */
        z_stream zbuf;
diff -r 6964aa635eb8 -r 0902624619bb sys/opencrypto/deflate.h
--- a/sys/opencrypto/deflate.h  Thu Feb 24 20:01:53 2011 +0000
+++ b/sys/opencrypto/deflate.h  Thu Feb 24 20:03:41 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: deflate.h,v 1.7 2011/02/16 19:08:58 drochner Exp $ */
+/*     $NetBSD: deflate.h,v 1.8 2011/02/24 20:03:41 drochner Exp $ */
 /*     $FreeBSD: src/sys/opencrypto/deflate.h,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $  */
 /* $OpenBSD: deflate.h,v 1.3 2002/03/14 01:26:51 millert Exp $ */
 
@@ -44,8 +44,8 @@
 #define MINCOMP                2       /* won't be used, but must be defined */
 #define ZBUF           10
 
-u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **);
-u_int32_t gzip_global(u_int8_t *, u_int32_t, int, u_int8_t **);
+u_int32_t deflate_global(u_int8_t *, u_int32_t, int, u_int8_t **, int);
+u_int32_t gzip_global(u_int8_t *, u_int32_t, int, u_int8_t **, int);
 void *z_alloc(void *, u_int, u_int);
 void z_free(void *, void *);
 
diff -r 6964aa635eb8 -r 0902624619bb sys/opencrypto/xform.c
--- a/sys/opencrypto/xform.c    Thu Feb 24 20:01:53 2011 +0000
+++ b/sys/opencrypto/xform.c    Thu Feb 24 20:03:41 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xform.c,v 1.18 2009/03/25 01:26:13 darran Exp $ */
+/*     $NetBSD: xform.c,v 1.19 2011/02/24 20:03:41 drochner Exp $ */
 /*     $FreeBSD: src/sys/opencrypto/xform.c,v 1.1.2.1 2002/11/21 23:34:23 sam Exp $    */
 /*     $OpenBSD: xform.c,v 1.19 2002/08/16 22:47:25 dhartmei Exp $     */
 
@@ -40,7 +40,7 @@
  */



Home | Main Index | Thread Index | Old Index