CINXE.COM

LKML: "Artem B. Bityuckiy": Re: [RFC] CryptoAPI & Compression

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>LKML: "Artem B. Bityuckiy": Re: [RFC] CryptoAPI &amp; Compression</title><link href="/css/message.css" rel="stylesheet" type="text/css" /><link href="/css/wrap.css" rel="alternate stylesheet" type="text/css" title="wrap" /><link href="/css/nowrap.css" rel="stylesheet" type="text/css" title="nowrap" /><link href="/favicon.ico" rel="shortcut icon" /><script src="/js/simple-calendar.js" type="text/javascript"></script><script src="/js/styleswitcher.js" type="text/javascript"></script><link rel="alternate" type="application/rss+xml" title="lkml.org : last 100 messages" href="/rss.php" /><link rel="alternate" type="application/rss+xml" title="lkml.org : last messages by &quot;Artem B. Bityuckiy&quot;" href="/groupie.php?aid=28528" /><!--Matomo--><script> var _paq = window._paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//m.lkml.org/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '1']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script><!--End Matomo Code--></head><body onload="es.jasper.simpleCalendar.init();" itemscope="itemscope" itemtype="http://schema.org/BlogPosting"><table border="0" cellpadding="0" cellspacing="0"><tr><td width="180" align="center"><a href="/"><img style="border:0;width:135px;height:32px" src="/images/toprowlk.gif" alt="lkml.org" /></a></td><td width="32">聽</td><td class="nb"><div><a class="nb" href="/lkml"> [lkml]</a> 聽 <a class="nb" href="/lkml/2005"> [2005]</a> 聽 <a class="nb" href="/lkml/2005/3"> [Mar]</a> 聽 <a class="nb" href="/lkml/2005/3/28"> [28]</a> 聽 <a class="nb" href="/lkml/last100"> [last100]</a> 聽 <a href="/rss.php"><img src="/images/rss-or.gif" border="0" alt="RSS Feed" /></a></div><div>Views: <a href="#" class="nowrap" onclick="setActiveStyleSheet('wrap');return false;">[wrap]</a><a href="#" class="wrap" onclick="setActiveStyleSheet('nowrap');return false;">[no wrap]</a> 聽 <a class="nb" href="/lkml/mheaders/2005/3/28/118" onclick="this.href='/lkml/headers'+'/2005/3/28/118';">[headers]</a>聽 <a href="/lkml/bounce/2005/3/28/118">[forward]</a>聽 </div></td><td width="32">聽</td></tr><tr><td valign="top"><div class="es-jasper-simpleCalendar" baseurl="/lkml/"></div><div class="threadlist">Messages in this thread</div><ul class="threadlist"><li class="root"><a href="/lkml/2005/3/25/104">First message in thread</a></li><li><a href="/lkml/2005/3/25/104">"Artem B. Bityuckiy"</a><ul><li><a href="/lkml/2005/3/25/277">Herbert Xu</a><ul><li><a href="/lkml/2005/3/26/34">"Artem B. Bityuckiy"</a></li><li class="origin"><a href="/lkml/2005/3/29/92">"Artem B. Bityuckiy"</a><ul><li><a href="/lkml/2005/3/29/92">Herbert Xu</a><ul><li><a href="/lkml/2005/3/29/122">"Artem B. Bityuckiy"</a></li></ul></li><li><a href="/lkml/2005/3/31/74">Herbert Xu</a><ul><li><a href="/lkml/2005/4/3/58">"Artem B. Bityuckiy"</a></li></ul></li></ul></li></ul></li></ul></li></ul><div class="threadlist">Patches in this message</div><ul class="threadlist"><li><a href="/lkml/diff/2005/3/28/118/1">Get diff 1</a></li><li><a href="/lkml/diff/2005/3/28/118/2">Get diff 2</a></li></ul></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerl.gif" width="32" height="32" alt="/" /></td><td class="c" rowspan="2" valign="top" style="padding-top: 1em"><table><tr><td><table><tr><td class="lp">Subject</td><td class="rp" itemprop="name">Re: [RFC] CryptoAPI &amp; Compression</td></tr><tr><td class="lp">From</td><td class="rp" itemprop="author">"Artem B. Bityuckiy" &lt;&gt;</td></tr><tr><td class="lp">Date</td><td class="rp" itemprop="datePublished">Mon, 28 Mar 2005 21:22:36 +0400</td></tr></table></td><td></td></tr></table><pre itemprop="articleBody">On Sat, 2005-03-26 at 15:44 +1100, Herbert Xu wrote: <br />&gt; I've whipped up something quick and called it crypto_comp_pcompress.<br />&gt; How does this interface look to you?<br /><br />Hello Herbert,<br /><br />I've done some work. Here are 2 patches:<br />1. pcompress-deflate-1.diff<br />2. uncompress-1.diff<br />(should be applied in that order). And I also imply your patch is<br />applied as well.<br /><br />The first patch is the implementation of the deflate_pcompress()<br />function. It was ported from JFFS2 with some changes.<br />The second patch is my implementation of the deflate_decompr() function<br />and I'd like to hear some comments on this.<br /><br />I made the changes to deflate_decompr() because the old version doesn't<br />work properly for me. There are 2 changes.<br /><br />1. I've added the following code:<br /><br />------------------------------------------------------------------------<br />if (slen &gt; 2 &amp;&amp; !(src[1] &amp; PRESET_DICT) /* No preset dictionary */<br /> &amp;&amp; ((src[0] &amp; 0x0f) == Z_DEFLATED) /* Comp. method byte is OK */<br /> &amp;&amp; !(((src[0] &lt;&lt; 8) + src[1]) % 31)) { /* CMF*256 + FLG */<br /> stream-&gt;next_in += 2;<br /> stream-&gt;avail_in -= 2;<br />}<br />------------------------------------------------------------------------<br /><br />This peace of code checks if there are 2 header bytes (see RFC-1950)<br />present in the stream to be inflated. If they are present, we should<br />skip them because we use a negative windowBits parameter when we<br />initialize inflate. When this "undocumented" feature is used, zlib<br />ignores the stream header and doesn't check the adler32 checksum (I've<br />found this roughly exploring the inflate source code). If we don't skip<br />the first 2 bytes, inflate will treat them as the part of the input data<br />and will work incorrectly.<br /><br />I don't know how deflate_decompr() worked before, it shouldn't have<br />worked AFAICS. So, I'm in doubt and would like to see your or James'<br />comments.<br /><br />2. I've removed the "strange" (for me) uncompress sequence:<br /><br />------------------------------------------------------------------------<br />ret = zlib_inflate(stream, Z_SYNC_FLUSH);<br />/*<br /> * Work around a bug in zlib, which sometimes wants to taste an extra<br /> * byte when being used in the (undocumented) raw deflate mode.<br /> * (From USAGI).<br /> */<br />if (ret == Z_OK &amp;&amp; !stream-&gt;avail_in &amp;&amp; stream-&gt;avail_out) {<br /> u8 zerostuff = 0;<br /> stream-&gt;next_in = &amp;zerostuff;<br /> stream-&gt;avail_in = 1;<br /> ret = zlib_inflate(stream, Z_FINISH);<br />}<br />------------------------------------------------------------------------<br /><br />and have changed it to the following:<br /><br />------------------------------------------------------------------------<br />ret = zlib_inflate(stream, Z_FINISH);<br />if (ret != Z_STREAM_END)<br /> return -EINVAL;<br />------------------------------------------------------------------------<br /><br />I made this because the old sequence didn't uncompress data compressed<br />by my deflate_pcompress(). Frankly, I just don't understand the meaning<br />of that replaced part and changed it to what seems correct and obvious<br />for me. My argument is the documentation of zlib_inflate() from<br />include/linux/zlib.h:<br /><br />------------------------------------------------------------------------<br />inflate() should normally be called until it returns Z_STREAM_END or an<br />error. However if all decompression is to be performed in a single step<br />(a single call of inflate), the parameter flush should be set to<br />Z_FINISH. In this case all pending input is processed and all pending<br />output is flushed; avail_out must be large enough to hold all the<br />uncompressed data.<br />------------------------------------------------------------------------<br /><br />I don't know anything about the declared bug "(From USAGI)".<br />So I'd like to hear some comment on this as well.<br /><br />Also I've simplified error handling a bit.<br /><br />The stuff I'm sending works for me. I also run the tcrypt.c test module,<br />and the deflate/inflate tests passed.<br /><br />-- <br />Best Regards,<br />Artem B. Bityuckiy,<br />St.-Petersburg, Russia.<br />diff -auNrp linux-2.6.11.5/crypto/deflate.c linux-2.6.11.5_changed/crypto/deflate.c<br />--- linux-2.6.11.5/crypto/deflate.c 2005-03-28 20:39:27.000000000 +0400<br />+++ linux-2.6.11.5_changed/crypto/deflate.c 2005-03-28 20:40:02.295158356 +0400<br />&#64;&#64; -5,6 +5,7 &#64;&#64;<br /> * by IPCOMP (RFC 3173 &amp; RFC 2394).<br /> *<br /> * Copyright (c) 2003 James Morris &lt;jmorris&#64;intercode.com.au&gt;<br />+ * Copyright (c) 2005 David Woodhouse &lt;dwmw2&#64;infradead.org&gt;<br /> * <br /> * This program is free software; you can redistribute it and/or modify it<br /> * under the terms of the GNU General Public License as published by the Free<br />&#64;&#64; -38,6 +39,8 &#64;&#64;<br /> #define DEFLATE_DEF_WINBITS 11<br /> #define DEFLATE_DEF_MEMLEVEL MAX_MEM_LEVEL<br /> <br />+#define DEFLATE_PCOMPR_RESERVE 12<br />+<br /> struct deflate_ctx {<br /> struct z_stream_s comp_stream;<br /> struct z_stream_s decomp_stream;<br />&#64;&#64; -150,6 +153,47 &#64;&#64; out:<br /> return ret;<br /> }<br /> <br />+static int deflate_pcompress(void *ctx, const u8 *src, unsigned int *slen,<br />+ u8 *dst, unsigned int *dlen)<br />+{<br />+ int ret;<br />+ struct deflate_ctx *dctx = ctx;<br />+ struct z_stream_s *stream = &amp;dctx-&gt;comp_stream;<br />+<br />+ if (*dlen &lt;= DEFLATE_PCOMPR_RESERVE)<br />+ return -EINVAL;<br />+<br />+ ret = zlib_deflateReset(stream);<br />+ if (ret != Z_OK)<br />+ return -EINVAL;<br />+<br />+ stream-&gt;next_in = (u8 *)src;<br />+ stream-&gt;next_out = dst;<br />+<br />+ while (stream-&gt;total_in &lt; *slen<br />+ &amp;&amp; stream-&gt;total_out &lt; *dlen - DEFLATE_PCOMPR_RESERVE) {<br />+<br />+ stream-&gt;avail_out = *dlen - DEFLATE_PCOMPR_RESERVE - stream-&gt;total_out;<br />+ stream-&gt;avail_in = min((unsigned int)(*slen - stream-&gt;total_in), stream-&gt;avail_out);<br />+<br />+ ret = zlib_deflate(stream, Z_SYNC_FLUSH);<br />+ if (ret != Z_OK)<br />+ return -EINVAL;<br />+ }<br />+<br />+ stream-&gt;avail_out += DEFLATE_PCOMPR_RESERVE;<br />+ stream-&gt;avail_in = 0;<br />+<br />+ ret = zlib_deflate(stream, Z_FINISH);<br />+ if (ret != Z_STREAM_END)<br />+ return -EINVAL;<br />+<br />+ *dlen = stream-&gt;total_out;<br />+ *slen = stream-&gt;total_in;<br />+<br />+ return 0;<br />+}<br />+ <br /> static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,<br /> u8 *dst, unsigned int *dlen)<br /> {<br />&#64;&#64; -201,6 +245,7 &#64;&#64; static struct crypto_alg alg = {<br /> .coa_init = deflate_init,<br /> .coa_exit = deflate_exit,<br /> .coa_compress = deflate_compress,<br />+ .coa_pcompress = deflate_pcompress,<br /> .coa_decompress = deflate_decompress } }<br /> };<br /> diff -auNrp linux-2.6.11.5/crypto/deflate.c linux-2.6.11.5_changed/crypto/deflate.c<br />--- linux-2.6.11.5/crypto/deflate.c 2005-03-28 21:04:53.279048342 +0400<br />+++ linux-2.6.11.5_changed/crypto/deflate.c 2005-03-28 21:02:38.000000000 +0400<br />&#64;&#64; -6,6 +6,7 &#64;&#64;<br /> *<br /> * Copyright (c) 2003 James Morris &lt;jmorris&#64;intercode.com.au&gt;<br /> * Copyright (c) 2005 David Woodhouse &lt;dwmw2&#64;infradead.org&gt;<br />+ * Copyright (c) 2005 Artem B. Bityuckiy &lt;dedekind&#64;infradead.org&gt;<br /> * <br /> * This program is free software; you can redistribute it and/or modify it<br /> * under the terms of the GNU General Public License as published by the Free<br />&#64;&#64; -29,6 +30,7 &#64;&#64;<br /> #include &lt;linux/module.h&gt;<br /> #include &lt;linux/crypto.h&gt;<br /> #include &lt;linux/zlib.h&gt;<br />+#include &lt;linux/zutil.h&gt;<br /> #include &lt;linux/vmalloc.h&gt;<br /> #include &lt;linux/interrupt.h&gt;<br /> #include &lt;linux/mm.h&gt;<br />&#64;&#64; -197,42 +199,41 &#64;&#64; static int deflate_pcompress(void *ctx, <br /> static int deflate_decompress(void *ctx, const u8 *src, unsigned int slen,<br /> u8 *dst, unsigned int *dlen)<br /> {<br />- <br />- int ret = 0;<br />+ int ret;<br /> struct deflate_ctx *dctx = ctx;<br /> struct z_stream_s *stream = &amp;dctx-&gt;decomp_stream;<br /> <br /> ret = zlib_inflateReset(stream);<br />- if (ret != Z_OK) {<br />- ret = -EINVAL;<br />- goto out;<br />- }<br />+ if (ret != Z_OK)<br />+ return -EINVAL;<br /> <br /> stream-&gt;next_in = (u8 *)src;<br /> stream-&gt;avail_in = slen;<br /> stream-&gt;next_out = (u8 *)dst;<br /> stream-&gt;avail_out = *dlen;<br /> <br />- ret = zlib_inflate(stream, Z_SYNC_FLUSH);<br />- /*<br />- * Work around a bug in zlib, which sometimes wants to taste an extra<br />- * byte when being used in the (undocumented) raw deflate mode.<br />- * (From USAGI).<br />- */<br />- if (ret == Z_OK &amp;&amp; !stream-&gt;avail_in &amp;&amp; stream-&gt;avail_out) {<br />- u8 zerostuff = 0;<br />- stream-&gt;next_in = &amp;zerostuff;<br />- stream-&gt;avail_in = 1; <br />- ret = zlib_inflate(stream, Z_FINISH);<br />- }<br />- if (ret != Z_STREAM_END) {<br />- ret = -EINVAL;<br />- goto out;<br />+<br />+ /* Since we use the undocumented Inflate feature passing the<br />+ * negative windowBits parameter to the zlib_inflateInit2()<br />+ * function, we must not pass the first 2 stream bytes to<br />+ * zlib_inflate() since it will treat them as data, not as<br />+ * CFM/FLG bytes (see RFC-1950).<br />+ * Thus, wi check if the first 2 bytes are the stream header and<br />+ * skip them if they are.*/<br />+ if (slen &gt; 2 &amp;&amp; !(src[1] &amp; PRESET_DICT) /* No preset dictionary */<br />+ &amp;&amp; ((src[0] &amp; 0x0f) == Z_DEFLATED) /* Comp. method byte is OK */<br />+ &amp;&amp; !(((src[0] &lt;&lt; 8) + src[1]) % 31)) { /* CMF*256 + FLG */<br />+ stream-&gt;next_in += 2;<br />+ stream-&gt;avail_in -= 2;<br /> }<br />- ret = 0;<br />+<br />+ ret = zlib_inflate(stream, Z_FINISH);<br />+ if (ret != Z_STREAM_END)<br />+ return -EINVAL;<br />+<br /> *dlen = stream-&gt;total_out;<br />-out:<br />- return ret;<br />+<br />+ return 0;<br /> }<br /> <br /> static struct crypto_alg alg = {</pre></td><td width="32" rowspan="2" class="c" valign="top"><img src="/images/icornerr.gif" width="32" height="32" alt="\" /></td></tr><tr><td align="right" valign="bottom"> 聽 </td></tr><tr><td align="right" valign="bottom">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerl.gif" width="32" height="32" alt="\" /></td><td class="c">聽</td><td class="c" valign="bottom" style="padding-bottom: 0px"><img src="/images/bcornerr.gif" width="32" height="32" alt="/" /></td></tr><tr><td align="right" valign="top" colspan="2"> 聽 </td><td class="lm">Last update: 2005-04-06 13:31 聽聽 [from the cache]<br />漏2003-2020 <a href="http://blog.jasper.es/"><span itemprop="editor">Jasper Spaans</span></a>|hosted at <a href="https://www.digitalocean.com/?refcode=9a8e99d24cf9">Digital Ocean</a> and my Meterkast|<a href="http://blog.jasper.es/categories.html#lkml-ref">Read the blog</a></td><td>聽</td></tr></table><script language="javascript" src="/js/styleswitcher.js" type="text/javascript"></script></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10