<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matthew J. Little</title>
	<atom:link href="http://matthewjlittle.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthewjlittle.com</link>
	<description></description>
	<lastBuildDate>Sat, 02 Jul 2011 21:18:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PL/SQL Implementation of PKCS #7 Padding</title>
		<link>http://matthewjlittle.com/2011/07/02/plsql-pkcs-7-padding/#utm_source=matthewjlittle&#038;utm_medium=matthewjlittle&#038;utm_campaign=matthewjlittle</link>
		<comments>http://matthewjlittle.com/2011/07/02/plsql-pkcs-7-padding/#comments</comments>
		<pubDate>Sat, 02 Jul 2011 21:07:22 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AES]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Padding]]></category>
		<category><![CDATA[PKCS7]]></category>
		<category><![CDATA[pl/sql]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://matthewjlittle.com/?p=169</guid>
		<description><![CDATA[I&#8217;ve been working on an Oracle package for encrypting/decrypting data using AES256.  I need the package to be compatible with a separate C# implementation of AES256 I&#8217;m also working on.  In the .NET AESManaged Class, the default padding mode is PKCS #7, which the Oracle DBMS_CRYPTO package does not support. So I&#8217;ve thrown together an implementation of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on an Oracle package for encrypting/decrypting data using AES256.  I need the package to be compatible with a separate C# implementation of AES256 I&#8217;m also working on.  In the .NET <a title="AesManaged Class" href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.aspx" target="_blank">AESManaged Class</a>, the default padding mode is PKCS #7, which the Oracle DBMS_CRYPTO package does not support.</p>
<p>So I&#8217;ve thrown together an implementation of PKCS7 padding in PL/SQL to use on the Oracle side.  Details about the PKCS7 padding are described in <a title="RFC 5652" href="http://tools.ietf.org/html/rfc5652#section-6.3" target="_blank">RFC 5652</a>.</p>
<h4><strong>fn_PKCS7_Pad:</strong></h4>
<p>Pads the raw data out to a multiple of the block size. Block size is in bytes.</p>
<pre class="brush: sql; light: true; title: ; notranslate">
CREATE OR REPLACE FUNCTION fn_PKCS7_Pad(pData IN RAW, pBlockSize IN PLS_INTEGER)
  RETURN RAW
IS
  vPadSize  PLS_INTEGER;
  vPadValue RAW(2);
BEGIN
  vPadSize  := pBlockSize - MOD(utl_raw.length(pData), pBlockSize);
  vPadValue := hextoraw(ltrim(to_char(vPadSize, 'XX')));

  RETURN utl_raw.concat(pData, utl_raw.copies(vPadValue, vPadSize));
END;
/
</pre>
<p>Note: If the data is already a multiple of the block size then the data is padded with one full block. This is per the RFC.</p>
<h4><strong>fn_PKCS7_Trim:</strong></h4>
<p>Function trims the padding off of the incoming raw data.</p>
<pre class="brush: sql; light: true; title: ; notranslate">
CREATE OR REPLACE FUNCTION fn_PKCS7_Trim(pData IN RAW)
  RETURN RAW
IS
  vPadSize  PLS_INTEGER;
  vPadValue RAW(2);
BEGIN
  vPadValue := utl_raw.substr(pData, -1, 1);
  vPadSize  := to_number(rawtohex(vPadValue), 'XX');

  RETURN utl_raw.substr(pData, 1, utl_raw.length(pData) - vPadSize);
END;
/
</pre>
<p>The trim function is currently making some fairly broad assumptions about the data. One assumption is that the data is in fact padded.  Also it is trimming off the number of bytes dictated by the last byte without actually checking to see if the previous bytes have the same value and the correct number of pad bytes are present.  When I get some time, I&#8217;ll go back and add some sanity checks.</p>
<h4><strong>Test Code:</strong></h4>
<pre class="brush: sql; light: true; title: ; notranslate">
DECLARE
  PROCEDURE sp_test_pkcs7 (pData IN RAW, pBlockSize IN PLS_INTEGER)
  IS
    vInData RAW(100);
    vDataPadded RAW(100);
    vDataTrimmed RAW(100);
  BEGIN
    vInData := hextoraw(pData);
    vDataPadded := fn_pkcs7_pad(vInData, pBlockSize);
    vDataTrimmed := fn_pkcs7_trim(vDataPadded);

    dbms_output.put_line('------------------------------------');
    dbms_output.put_line('IN:         ' || rawtohex(vInData));
    dbms_output.put_line('BLOCK SIZE: ' || pBlockSize);
    dbms_output.put_line('PADDED:     ' || rawtohex(vDataPadded));
    dbms_output.put_line('TRIMMED:    ' ||rawtohex(vDataTrimmed));
    dbms_output.put_line('------------------------------------');
  END;
BEGIN
  sp_test_pkcs7('AABBCC', 8); --3 bytes of data
  sp_test_pkcs7('AABBCCDDEEFF', 8); --6 byes of data
  sp_test_pkcs7('AABBCCDDEEFFAABB', 8); --8 bytes of data
END;
/
</pre>
<h4><strong>Output:</strong></h4>
<pre class="brush: plain; light: true; title: ; notranslate">
------------------------------------
IN:         AABBCC
BLOCK SIZE: 8
PADDED:     AABBCC0505050505
TRIMMED:    AABBCC
------------------------------------
------------------------------------
IN:         AABBCCDDEEFF
BLOCK SIZE: 8
PADDED:     AABBCCDDEEFF0202
TRIMMED:    AABBCCDDEEFF
------------------------------------
------------------------------------
IN:         AABBCCDDEEFFAABB
BLOCK SIZE: 8
PADDED:     AABBCCDDEEFFAABB0808080808080808
TRIMMED:    AABBCCDDEEFFAABB
------------------------------------
</pre>
<address><strong><a href="http://matthewjlittle.com/wp-content/uploads/2011/07/plsql_pkcs7_padding.txt#utm_source=matthewjlittle&amp;utm_medium=matthewjlittle&amp;utm_campaign=matthewjlittle">Full Code in Text File</a></strong></address>
]]></content:encoded>
			<wfw:commentRss>http://matthewjlittle.com/2011/07/02/plsql-pkcs-7-padding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Favicons in Google Reader</title>
		<link>http://matthewjlittle.com/2011/03/22/favicons-in-google-reader/#utm_source=matthewjlittle&#038;utm_medium=matthewjlittle&#038;utm_campaign=matthewjlittle</link>
		<comments>http://matthewjlittle.com/2011/03/22/favicons-in-google-reader/#comments</comments>
		<pubDate>Tue, 22 Mar 2011 22:09:49 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[Favicons]]></category>
		<category><![CDATA[Google Reader]]></category>

		<guid isPermaLink="false">http://matthewjlittle.com/?p=148</guid>
		<description><![CDATA[I stumbled upon a setting in Google Reader today that shows the Favicons for your subscriptions.  Apparently the feature has been in there forever&#8230; But it&#8217;s new to me!]]></description>
			<content:encoded><![CDATA[<p>I stumbled upon a setting in Google Reader today that shows the Favicons for your subscriptions.  Apparently the feature has been in there forever&#8230; But it&#8217;s new to me!</p>
<p><img class="size-full wp-image-159 alignleft" style="margin: 5px; border: 1px solid black;" title="reader_favicon_setting" src="http://matthewjlittle.com/wp-content/uploads/2011/03/reader_favicon_setting.png" alt="" width="209" height="294" /><img class="size-full wp-image-158 alignleft" style="margin: 5px; border: 1px solid black;" title="google_reader_favicon_before_after" src="http://matthewjlittle.com/wp-content/uploads/2011/03/google_reader_favicon_before_after.png" alt="" width="337" height="270" /></p>
]]></content:encoded>
			<wfw:commentRss>http://matthewjlittle.com/2011/03/22/favicons-in-google-reader/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>.NET IPAddress Class</title>
		<link>http://matthewjlittle.com/2010/12/05/net-ipaddress-class/#utm_source=matthewjlittle&#038;utm_medium=matthewjlittle&#038;utm_campaign=matthewjlittle</link>
		<comments>http://matthewjlittle.com/2010/12/05/net-ipaddress-class/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 00:06:54 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MSDN]]></category>

		<guid isPermaLink="false">http://matthewjlittle.com/?p=65</guid>
		<description><![CDATA[I&#8217;ve been working on a C# version of a COM component our applications at work use to integrate with an Avaya predictive dialing system. While implementing the code for the communication piece, I found a quirk about the .NET IPAddress class I had not run into before. The communication is being done with asynchronous sockets. The Socket [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a C# version of a COM component our applications at work use to integrate with an Avaya predictive dialing system. While implementing the code for the communication piece, I found a quirk about the .NET IPAddress class I had not run into before.</p>
<p>The communication is being done with asynchronous sockets. The Socket class requires an IPEndPoint, which in turn takes an IPAddress and port (integer).  Fair enough.  For my purposes I just needed to connect using the IP not the host name.   So the next logical step in my mind would be to new up an instance of IPAddress with the IP I&#8217;m attempting to connect to.  The problem I ran into is that the IPAddress constructor doesn&#8217;t accept an IP! At least not in the <a title="Dot-decimal notation" href="http://en.wikipedia.org/wiki/Dot-decimal_notation">Dot-decimal notation</a> (x.x.x.x). Instead, I would need to know the byte array, int64, or long representation of the address.</p>
<p style="text-align: left;"><img class="aligncenter" title="IPAddress" src="http://matthewjlittle.com/wp-content/uploads/2010/07/IPAddress1-e1280089688435.png" alt="" width="500" height="134" /></p>
<p>Well it turns out you have to call the static <a href="http://msdn.microsoft.com/en-us/library/system.net.ipaddress.parse.aspx" target="_blank">Parse()</a> method on the IPAddress class passing in the IP which in turn will return an instance of the class.  Parse() takes dotted-quad IPv4 or colon-hexadecimal IPv6. It seems obvious now and only took a minute or so digging around in the MSDN documentation to figure out.   Just wasn&#8217;t what I was expecting.  Apparently I need to brush up on my &#8220;converting ip address to byte[]&#8221; in my head skills.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewjlittle.com/2010/12/05/net-ipaddress-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When 1 equals 2</title>
		<link>http://matthewjlittle.com/2009/11/07/when-1-equals-2/#utm_source=matthewjlittle&#038;utm_medium=matthewjlittle&#038;utm_campaign=matthewjlittle</link>
		<comments>http://matthewjlittle.com/2009/11/07/when-1-equals-2/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 22:40:45 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://matthewjlittle.com/?p=4</guid>
		<description><![CDATA[This is not intended to be a discussion if 1 actually equals 2.  But has to do with a line of sql from the other day. I had a request come in at work to disable a tab in one of our applications that showed historical data for customer accounts.  My initial thought was that [...]]]></description>
			<content:encoded><![CDATA[<p>This is not intended to be a discussion if <a title="Two Equals One" href="http://mh1823.com/two_equals_one.htm" target="_blank">1 actually equals 2</a>.  But has to do with a line of sql from the other day.</p>
<p>I had a request come in at work to disable a tab in one of our applications that showed historical data for customer accounts.  My initial thought was that we would have to make a change to the application and redeploy.  Pain in the arse!  When it dawned on me (which possibly was Adam slapping me up the side of the head) that since the application depended on the results of a query in Oracle, I could just force the query to return no rows, effectively showing no history in the application.</p>
<p>So the quick fix was to disable the query by adding to the where clause &#8220;and 1=2&#8243;. So regardless what the <a title="1=2: A Proof using Complex Numbers" href="http://www.math.toronto.edu/mathnet/plain/falseProofs/second1eq2.html" target="_blank">skeptics say</a>, in Oracle land 1 will never equal 2.  So the query returns 0 rows no matter what parameters are passed in.</p>
<p>Fantastic! One quick after hour change (b/c of packages supporting &#8220;State&#8221;, discussion for another time), we had history disabled in the application.</p>
<h3>So what does 1=2 actually do?</h3>
<p>Now what was really interesting was running an explain on the query and discovering how Oracle deals with 1=2.  As with most compilers, Oracle will attempt to simplify code as best it can to optimize performance.</p>
<p>Consider the following example in C#:</p>
<pre class="brush: csharp; title: ; notranslate">
static void Test1()
{
    if (1 == 2)
    {
        Console.WriteLine(&quot;True Statement&quot;);
    }
    else
    {
        Console.WriteLine(&quot;False Statement&quot;);
    }
}
</pre>
<p>The IF statement is pointless since at runtime you will always end up in the ELSE.  Checking the code in Reflector after compiling in Release mode shows that the optimizer catches the waste of cpu cycles and space and consolidates:</p>
<pre class="brush: csharp; title: ; notranslate">
private static void Test1()
{
    Console.WriteLine(&quot;False Statement&quot;);
}
</pre>
<p>Now this change made by the compiler makes sense in this situation.</p>
<h3>What if 1=2 is crucial?</h3>
<p>Consider the following from Oracle:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT uo.object_name,
       uo.object_type
  FROM user_objects uo
 WHERE uo.object_type = 'TABLE';
</pre>
<p>The query will return a list of table(s) owned by the user (assuming the user owns at least one table).</p>
<p>Adding the 1=2 condition:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT uo.object_name,
       uo.object_type
  FROM user_objects uo
 WHERE uo.object_type = 'TABLE'
       AND 1=2;
</pre>
<p>The 1=2 will cause the query to always return 0 rows.  Now of course the optimizer can&#8217;t remove the 1=2 at runtime without changing the intended results.  So what does Oracle do?</p>
<p>Taking a look at the explain: &#8220;1=2&#8243; is translated to &#8220;NULL IS NOT NULL&#8221;.</p>
<p><img class="alignleft size-full wp-image-25" title="1equal2SqlExplain" src="http://matthewjlittle.com/wp-content/uploads/2009/11/SqlExplain.png" alt="1equal2SqlExplain" width="696" height="173" /></p>
<p>So the Resulting SQL more or less becomes:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT uo.object_name,
       uo.object_type
  FROM user_objects uo
 WHERE uo.object_type = 'TABLE'
       AND NULL IS NOT NULL;
</pre>
<h3>Brilliant!</h3>
<p>I about fell in the floor laughing when I saw this when making the aforementioned code change.   I proceeded to change my where clause to &#8220;NULL IS NOT NULL&#8221;.  And look forward to the occasional laugh when a co-worker stumbles upon the code.</p>
<p>Now I haven&#8217;t checked to see how Oracle deals with 1=2 in a IF statement in pl/sql.  I would assume it would handle it in a similar fashion as .net does.  This situation would actually make a better comparison to how .NET vs Oracle deals with optimizing code.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewjlittle.com/2009/11/07/when-1-equals-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://matthewjlittle.com/2009/09/04/hello-world/#utm_source=matthewjlittle&#038;utm_medium=matthewjlittle&#038;utm_campaign=matthewjlittle</link>
		<comments>http://matthewjlittle.com/2009/09/04/hello-world/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 00:05:15 +0000</pubDate>
		<dc:creator>Matt</dc:creator>
				<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://www.matthewjlittle.com/?p=1</guid>
		<description><![CDATA[Yay! I managed to get WordPress up and going on my own domain. Actually not that big of deal considering how sweet the WordPress installer is.]]></description>
			<content:encoded><![CDATA[<p>Yay! I managed to get WordPress up and going on my own domain.  Actually not that big of deal considering how sweet the WordPress installer is.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthewjlittle.com/2009/09/04/hello-world/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

