<?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>Softcore software development &#187; programming</title>
	<atom:link href="http://tea.cesaroliveira.net/archives/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://tea.cesaroliveira.net</link>
	<description>It&#039;s all about the cycles</description>
	<lastBuildDate>Sun, 01 Aug 2010 02:04:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Endianness, how I loathe you</title>
		<link>http://tea.cesaroliveira.net/archives/172</link>
		<comments>http://tea.cesaroliveira.net/archives/172#comments</comments>
		<pubDate>Thu, 13 May 2010 05:26:21 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[do'h]]></category>
		<category><![CDATA[endian]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[sha1]]></category>

		<guid isPermaLink="false">http://tea.cesaroliveira.net/?p=172</guid>
		<description><![CDATA[(originally posted in February, but got lost in time) I have been busy making my own implementation of SHA-1. To better learn about why so many people depend on it for basically everything from SSL to tamper detection mechanism. I have a bigger project idea, but that is not important right now. What is important [...]]]></description>
			<content:encoded><![CDATA[<p>(originally posted in February, but got lost in time)</p>
<p>I have been busy making my own implementation of SHA-1. To better learn about why so many people depend on it for basically everything from SSL to tamper detection mechanism. I have a bigger project idea, but that is not important right now. What is important is that SHA-1 does everything in big endian, and I am on x86-64 which is a little endian machine.</p>
<p>Remember that a big endian machine has the most significant byte first, and little endian has the most significant byte last.</p>
<p>For example, let&#8217;s say I want a 64-bit integer to hold the number 1. This is how it&#8217;ll be stored:<br />
Big endian:<br />
1 = 0000 0000 0000 0000 0000 0000 0000 0001<br />
Little endian:<br />
1 = 0001 0000 0000 0000 0000 0000 0000 0000</p>
<p>SHA-1 stores the size of the message as a 64-bit integer in the last block during padding (each block is 512 bits). Since I have a little-endian machine, I wrote a function that correctly switches endian and now, the 1 appears as the as it should.</p>
<p>However, SHA-1 loops through each block in 32-bit integers.</p>
<p>*((unsigned int*)0000 0000 0000 0000) = 0<br />
*((unsigned int*)0000 0000 0000 0001) = 16 million and change on little endian machine instead of 1 as I expect</p>
<p>so the second time, I have to do another endian change, this time a 32-bit endian change, so that it appears as :<br />
0001 0000 0000 0000</p>
<p>so I get back 1.</p>
<p>This is a PITA, and a frustrating one. Mainly because I couldn&#8217;t figure it out for a few days. But feel so accomplished for figuring it out. Accomplished and embarrassed.</p>
]]></content:encoded>
			<wfw:commentRss>http://tea.cesaroliveira.net/archives/172/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AES and CBC</title>
		<link>http://tea.cesaroliveira.net/archives/156</link>
		<comments>http://tea.cesaroliveira.net/archives/156#comments</comments>
		<pubDate>Tue, 22 Dec 2009 05:21:06 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[crypto]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://tea.cesaroliveira.net/?p=156</guid>
		<description><![CDATA[If you ever want to use a crypto library in Python, you&#8217;ll be sad to note that there isn&#8217;t one built into Python impressive repertoire of modules. In fact, you&#8217;ll most likely hit pycrypt on your Google search to find one. And there is some dirty work you&#8217;ll have to do if you want to [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever want to use a crypto library in Python, you&#8217;ll be sad to note that there isn&#8217;t one built into Python impressive repertoire of modules. In fact, you&#8217;ll most likely hit <a href="http://www.dlitz.net/software/pycrypto/" onclick="pageTracker._trackPageview('/outgoing/www.dlitz.net/software/pycrypto/?referer=');">pycrypt</a> on your Google search to find one. And there is some dirty work you&#8217;ll have to do if you want to use symmetric cryptography using this library. And one of the hard/easy parts is knowing the difference between ECB and CBC.</p>
<p>Here, we start initializing the AES object using CBC mode:</p>
<p><code>&gt;&gt;&gt; from Crypto.Cipher import AES;<br />
&gt;&gt;&gt; aes = AES.new('some key here', AES.MODE_CBC, 'INIT_VECTOR')<br />
Traceback (most recent call last):<br />
  File "&lt;console&gt;", line 1, in &lt;module&gt;<br />
ValueError: IV must be 16 bytes long</code></p>
<p>oops. You&#8217;ll have to make you&#8217;re initialization vector 16 bytes long. Also, your key has to be 16, 24, or 32 bytes long as well. Let&#8217;s do something better :</p>
<p><code>&gt;&gt;&gt; aes = AES.new('J2-+sfd%932mIt:{', AES.MODE_CBC, 'wir&amp;/&gt;H54mgd9a";')</code></p>
<p>ah! much better. Even if it was me smashing my hand against the keyboard. Now let&#8217;s encrypt/decrypt something important.</p>
<p><code>&gt;&gt;&gt; aes.encrypt('the answer to life the universe and everything is 42')<br />
Traceback (most recent call last):<br />
  File "&lt;console&gt;", line 1, in &lt;module&gt;<br />
ValueError: Input strings must be a multiple of 16 in length</code></p>
<p>You&#8217;ll have to do the dirty work remember:</p>
<p><code>>&gt;&gt;&gt; ciphertext = aes.encrypt('the answer to life the universe and everything is 42195479204957')<br />
>>> ciphertext<br />
'f0\xa9\xf9f&#038;X)\x0e\x08=\x06\x97\xcbF\xddK\x1a\xa6i\x1d\x02"}\xd9\\\xaa\xb6\xd9J\xe3Q\x07\xaev\x012\xbf\rPN\xd2\xf9\xf7$\x93\xe0/\xcb\xae9\x91K\xd01\xab\xb7\xdb\reR\xff\xef\x1c'</code></p>
<p>Much better. Now lets decrypt it:</p>
<p><code>&gt;&gt;&gt; aes.decrypt(ciphertext)<br />
'\xc8\xaf.\x97\x05\x80\n\xe9\xe6\xc4Ju\x04\xbe\xa1Nfe the universe and everything is 42195479204957'</code></p>
<p>Woah! That isn&#8217;t the whole message! So what&#8217;s going on?</p>
<p>Remember that initialization vector you set in the beginning? That sets the stage for the first block. But each block becomes the initialization vector for the second block, and so on. So when you decrypt, it is using the initialization vector from the block before. That&#8217;s why the first 16 bytes are screwed up. This is a feature of CBC, but not ECB:</p>
<p><code>&gt;&gt;&gt; aes = AES.new('J2-+sfd%932mIt:{', AES.MODE_ECB, 'wir&amp;/&gt;H54mgd9a";')<br />
&gt;&gt;&gt; ciphertext = aes.encrypt('the answer to life the universe and everything is 42195479204957')<br />
&gt;&gt;&gt; aes.decrypt(ciphertext)'the answer to life the universe and everything is 42195479204957'</code></p>
<p>And yes, this is a feature. Read the <a href="http://en.wikipedia.org/wiki/Cipher_block_chaining" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Cipher_block_chaining?referer=');">block cipher modes wikipedia article</a> for a better explination. So what&#8217;s the answer? Simply, to call aes.new() again before calling decrypt!</p>
]]></content:encoded>
			<wfw:commentRss>http://tea.cesaroliveira.net/archives/156/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Maps and geolocation</title>
		<link>http://tea.cesaroliveira.net/archives/61</link>
		<comments>http://tea.cesaroliveira.net/archives/61#comments</comments>
		<pubDate>Fri, 10 Jul 2009 05:37:34 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[hugs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[browser compatibility]]></category>
		<category><![CDATA[google chrome]]></category>

		<guid isPermaLink="false">http://tea.cesaroliveira.net/?p=61</guid>
		<description><![CDATA[I was first made aware of the fact that maps.google.com now uses geolocation by sdwilsh, which is new in Firefox 3.5. But when I loaded maps, I was surprised to see that it didn&#8217;t work when I visited the site. And I was using something even more recent than Firefox 3.5, Minefield. Surely, it has [...]]]></description>
			<content:encoded><![CDATA[<p>I was first made aware of the fact that <a href="http://maps.google.com" onclick="pageTracker._trackPageview('/outgoing/maps.google.com?referer=');">maps.google.com</a> now uses <a href="https://developer.mozilla.org/En/Using_geolocation" onclick="pageTracker._trackPageview('/outgoing/developer.mozilla.org/En/Using_geolocation?referer=');">geolocation</a> by <a href="http://twitter.com/sdwilsh/status/2553543365" onclick="pageTracker._trackPageview('/outgoing/twitter.com/sdwilsh/status/2553543365?referer=');">sdwilsh</a>, which is new in <a href="http://www.spreadfirefox.com" onclick="pageTracker._trackPageview('/outgoing/www.spreadfirefox.com?referer=');">Firefox 3.5</a>. But when I loaded maps, I was surprised to see that it didn&#8217;t work when I visited the site. And I was using something even more recent than Firefox 3.5, Minefield. Surely, it has geolocation, so what is going on?</p>
<p>The reason maps doesn&#8217;t support Minefield is because of  *<a href="http://www.youtube.com/watch?v=NNaZedAWmlE" onclick="pageTracker._trackPageview('/outgoing/www.youtube.com/watch?v=NNaZedAWmlE&amp;referer=');">drumrolls</a>* &#8230; browser sniffing. Developers&#8230; no wait&#8230; GOOGLE web developers, I thought we moved on?</p>
<p>The actual bit of code is here unminimized and tidied up ;<br />
<code>
<pre>
function isBrowserGeolocationSupported(){
    if (window.navigator &#038;&#038;
        navigator.userAgent.search("Firefox") != -1 &#038;&#038;
        navigator.geolocation)
        return true;
    if (window.navigator &#038;&#038;
        navigator.userAgent.search("Chrome") != -1)
        return Number(String(/Chrome\/[0-9]+/.exec(navigator.userAgent)).substr(7))>=2;
    var gearsFactory=null;
</pre>
<p></code></p>
<p>The hell? Ok, so I understand they do a bit of browser sniffing because it looks like Chrome had a old/broken implementation of geolocation. But I wish there was a more graceful way of doing this (maybe something like navigator.geolocation.version < 1). One that didn't break every application that may implement geolocation that isn't named Firefox. Because, those <a href="http://flock.com/" onclick="pageTracker._trackPageview('/outgoing/flock.com/?referer=');">exist</a> <a href="http://www.getsongbird.com/" onclick="pageTracker._trackPageview('/outgoing/www.getsongbird.com/?referer=');">too</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://tea.cesaroliveira.net/archives/61/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Not even bytecode can save me now&#8230;</title>
		<link>http://tea.cesaroliveira.net/archives/35</link>
		<comments>http://tea.cesaroliveira.net/archives/35#comments</comments>
		<pubDate>Tue, 16 Sep 2008 07:57:18 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[addons]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[editor]]></category>
		<category><![CDATA[seneca]]></category>

		<guid isPermaLink="false">http://www.cesaroliveira.net/?p=35</guid>
		<description><![CDATA[I&#8217;ve been spending a few days on trying to develop a few tools for editors to use to quickly reject addons for obvious defects, such as loading remote scripts. But I wanted to get deeper into the javascript stuff mainly because it&#8217;s a) it&#8217;s harder and b) it&#8217;s where the real problems lie. But as [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been spending a few days on trying to develop a few tools for editors to use to quickly reject addons for obvious defects, such as loading remote scripts. But I wanted to get deeper into the javascript stuff mainly because it&#8217;s a) it&#8217;s harder and b) it&#8217;s where the real problems lie.</p>
<p>But as anyone can tell you, it&#8217;s not an easy task (going towards damn near impossible). Firstly, you can&#8217;t really use a lexical parser. Well, you can, but it won&#8217;t be dependable. Let&#8217;s take an example out of the Reviewer&#8217;s guide :</p>
<p><code>document["crea" + "teElement"]("s" + "c" + "r" + ["i", "p", "t"].join(""));</code></p>
<p>Which is sneaky way of creating a script element, though I question the competence of someone who will use this as their main line of attack (it&#8217;s practically spelled out for you). But taking this as a use case, and ignoring the fact that they can use document[cheese] instead, I wondering if parsing the javascript would make figuring this stuff out any better.</p>
<p>Happily, I have spidermonkey and a <a href="http://developer.mozilla.org/en/Introduction_to_the_JavaScript_shell" onclick="pageTracker._trackPageview('/outgoing/developer.mozilla.org/en/Introduction_to_the_JavaScript_shell?referer=');">js shell</a> to do any parsing I wish. But I found out some cool things that you can do in the shell, such as looking at the bytecode for an entire function using the dis() command.</p>
<p>This was interesting. Certainly, there are some optimizations you can do for :<br />
<code>document["crea" + "teElement"]("s" + "c" + "r" + ["i", "p", "t"].join("")); </code><br />
I would be shocked if it didn&#8217;t end up spelling out :<br />
<code>document["createElement"]("script"); </code></p>
<p>I had a few hurdles to overcome. Firstly, document is not defined in the javascript shell. Thinking it was defined in the xpcshell (owww. I was misled by some apparently <a href="http://mxr.mozilla.org/mozilla-central/search?find=%2Fjs%2Fsrc%2Fxpconnect%2Ftests%2F&amp;string=document" onclick="pageTracker._trackPageview('/outgoing/mxr.mozilla.org/mozilla-central/search?find=_2Fjs_2Fsrc_2Fxpconnect_2Ftests_2F_amp_string=document&amp;referer=');">unused tests</a> and my general ignorance that xpcshell tests went into unit/ and not js/ directory) I went through the added trouble of coping dis() and related functions from <a href="http://mxr.mozilla.org/mozilla-central/source/js/src/js.cpp#1373" onclick="pageTracker._trackPageview('/outgoing/mxr.mozilla.org/mozilla-central/source/js/src/js.cpp_1373?referer=');">js.cpp</a> to xpcshell.cpp. Once I realized that document wasn&#8217;t defined, I made a document mock object just to see what the blasted bytecode would look like.</p>
<p>I was a little disappointed. This source:<br />
<!--start_raw--><code>
<pre>var document = {
createElement : function(s) {
print("damn");
}
};

function foo() {
document["crea" + "teElement"]("s" + "c" + "r" + ["i", "p", "t"].join(""));
}

dis(foo);</pre>
<p></code><!--end_raw--></p>
<p>Ended up being this bytecode :<br />
<!--start_raw--><br />
<code style="font-size:smaller;">
<pre>
00000:  name "document"
00003:  string "createElement"
00006:  callelem
00007:  string "s"
00010:  string "c"
00013:  add
00014:  string "r"
00017:  add
00018:  newinit 3
00020:  zero
00021:  string "i"
00024:  initelem
00025:  one
00026:  string "p"
00029:  initelem
00030:  int8 2
00032:  string "t"
00035:  initelem
00036:  endinit
00037:  callprop "join"
00040:  string ""
00043:  call 1
00046:  add
00047:  call 1
00050:  pop
00051:  stop

Source notes:
  0:     0 [   0] newline
  1:     6 [   6] pcbase   offset 6
  3:    37 [  31] xdelta
  4:    37 [   0] pcbase   offset 19
  6:    43 [   6] pcbase   offset 25
  8:    47 [   4] pcbase   offset 47</pre>
<p></code><!--end_raw--></p>
<p>So, almost. The document["createElement"] part was correct, but the .join() wasn&#8217;t optimized. Although I wasn&#8217;t overly estatic, I knew that this was just one (somewhat lame) use case in the countless of possible others.</p>
<p>This is making me rethink whether lexical tools <em>are</em> the way to go. While they don&#8217;t give you any definitive proof that there is a possible security hole, they can still be useful. For example, if you want to use XMLHttpRequest, then you have to call it at least once in your program (even if you say <code>var Widget = XMLHttpRequest</code>). And at least that can bring up warning flags, or at least give editors a place to look.</p>
<p>I don&#8217;t think any tool can completely replace a human being. But hopefully, tools will help make the review process easier because you can start looking at high-risk areas first rather than starting from a arbitrary point and not coming across something until 10 minutes later.</p>
]]></content:encoded>
			<wfw:commentRss>http://tea.cesaroliveira.net/archives/35/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The many ways around a problem</title>
		<link>http://tea.cesaroliveira.net/archives/17</link>
		<comments>http://tea.cesaroliveira.net/archives/17#comments</comments>
		<pubDate>Wed, 28 May 2008 16:11:13 +0000</pubDate>
		<dc:creator>Cesar</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[intern]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[seneca]]></category>

		<guid isPermaLink="false">http://www.cesaroliveira.net/?p=17</guid>
		<description><![CDATA[I came across a bug in the zipfile python module yesterday that I had to fix today. The problem occurs when you try to create a ZipFile object and passing it a corrupt zip file. It doesn&#8217;t handle it gracefully like returning None or throwing an exception. Rather it heads into an infinite loop. This [...]]]></description>
			<content:encoded><![CDATA[<p>I came across a bug in the zipfile python module yesterday that I had to fix today. The problem occurs when you try to create a ZipFile object and passing it a corrupt zip file. It doesn&#8217;t handle it gracefully like returning None or throwing an exception. Rather it heads into an infinite loop.</p>
<p>This is rather unfortunate for me. How would I get around this problem? The first thing I did was check for an updated python. Which there was a minor version upgrade. I found the changelog (why do they hide these things?) and noticed a few bugs resolved with the zipfile module. So I installed. Unfortunately, this didn&#8217;t solve my problem.</p>
<p>I managed to find a <a href="http://bugs.python.org/issue1622" onclick="pageTracker._trackPageview('/outgoing/bugs.python.org/issue1622?referer=');">bug number</a> in the python bug tracking software about people having similar problems. There was a patch, but hasn&#8217;t landed. I downloaded the latest stable version, but the patch wouldn&#8217;t go through. So I had to cvs checkout trunk and apply it. Once installed, I tried it and it worked! Success.</p>
<p>However, it broke other library I was using (PyXML). Unfortunate for me, the recent trunk build didn&#8217;t seem to fair any better.</p>
<p>At this point, I wasn&#8217;t in the mood for debugging. I had a few options at my disposal :</p>
<ol>
<li>Ignore this particular file</li>
<li>Suck it up and debug it.</li>
<li>Find a whacky work-around</li>
</ol>
<p>Option 1 isn&#8217;t an option. Option 2 I tried for a fair while, but nothing worked. So Option 3 was my only option!</p>
<p>I tried using a lower level library to see if I can fix the problem (zlib library), but that didn&#8217;t work well at all.</p>
<p>I finally thought I had no choice but to initiate a thread to try and unzip the xpi, and if it took longer than 10 seconds, to kill the thread somehow. While seriously looking into this, and fighting the temptation to take tequelia shots at work. I came across signals (which I thought I could use to send to the thread. I&#8217;m so naive). It turns out, you can throw a signal after a specific number of seconds and it throws the SIGALRM. This was <strong>exactly</strong> what I needed without the extra complexity. The <a href="http://docs.python.org/lib/node545.html" onclick="pageTracker._trackPageview('/outgoing/docs.python.org/lib/node545.html?referer=');">example provided</a> was almost exactly what I did too! Here is my solution to the problem :<br />
<code>
<pre>
		signal.signal(signal.SIGALRM, signal_handler)
		signal.alarm(10)
		try:
			zippy = zipfile.ZipFile(io, 'r')
			signal.alarm(0)
		except:
			print "\tZipFile Timeout"
			continue
</pre>
<p></code></p>
<p>Maybe python isn&#8217;t just for programming sissies after all.</p>
]]></content:encoded>
			<wfw:commentRss>http://tea.cesaroliveira.net/archives/17/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
