<?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>Brad Conte &#187; My Code</title>
	<atom:link href="http://bradconte.com/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://bradconte.com</link>
	<description>Why know the ordinary when you can understand the extraordinary?</description>
	<lastBuildDate>Fri, 20 Jan 2012 04:12:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Blowfish &#8211; C</title>
		<link>http://bradconte.com/blowfish_c.html</link>
		<comments>http://bradconte.com/blowfish_c.html#comments</comments>
		<pubDate>Tue, 08 Aug 2006 07:28:42 +0000</pubDate>
		<dc:creator>Brad Conte</dc:creator>
				<category><![CDATA[My Code]]></category>

		<guid isPermaLink="false">http://test.b-con.us/code/blowfish-c</guid>
		<description><![CDATA[Algorithm This is an implementation of the Blowfish block cipher. There are two steps to the cipher. First, five sets of tables are initialized using the user&#8217;s key and the blowfish encryption algorithm itself (making the algorithm recursive). Second, the data is encrypted using the permutated tables initialized previously. Code Documentation void key_schedule(uchar user_key[], BLOWFISH_KEY [...]]]></description>
			<content:encoded><![CDATA[<div class="section-title">Algorithm</div>
This is an implementation of the <a href="http://www.schneier.com/paper-blowfish-fse.html">Blowfish</a> block cipher. 
<br /><br />

There are two steps to the cipher. First, five sets of tables are initialized using the user&#8217;s key and the blowfish encryption algorithm itself (making the algorithm recursive). Second, the data is encrypted using the permutated tables initialized previously.
<br /><br />

<div class="section-title">Code Documentation</div>

<ul> 

   <li> <span class="code_function">void key_schedule(uchar user_key[], BLOWFISH_KEY *keystruct, int len)</span> <br />
   This generates an encryption key from a user-supplied key.
   <br /><br />
   
   <ul>

      <li><span class="code_function">uchar user_key[]</span><br />
      This is the user-supplied key.</li>

      <li><span class="code_function">BLOWFISH_KEY *keystruct</span><br />
      This is the key structure that will be used to encrypt data later in the encryption functions.</li>

      <li><span class="code_function">int len</span><br />
      This is the length, in bytes, of the key in the first parameter. It may be any value up to 448.</li>
   
   </ul>
   <br /><br />
   </li>


   <li> <span class="code_function">blowfish_encrypt(uchar in[], uchar out[], BLOWFISH_KEY *keystruct)</span> <br />
        <span class="code_function">blowfish_decrypt(uchar in[], uchar out[], BLOWFISH_KEY *keystruct)</span> <br />
   These functions encrypt and decrypt data, accordingly, using a Blowfish key structure. <br /><br />

   <ul>

      <li><span class="code_function">uchar in[]</span><br />
      This is the data to be encrypted, for the blowfish_encrypt() function, and the data to be decrypted for the blowfish_decrypt() function.</li>

      <li><span class="code_function">uchar out[]</span><br />
      This is where to store the output encrypted data, for the blowfish_encrypt() function, and the output decrypted data for the blowfish_decrypt() function.</li>

      <li><span class="code_function">BLOWFISH_KEY *keystruct</span><br />
      This is the key structure that is generated by the key_schedule() function (above). It is the same for both encrypted and decryption.</li>

   </ul>
   </li>
   
</ul>


<div class="section-title">Code Usage</div>
<ol>
<li>Create the arrays to hold both input and output data for the encryption and/or decryption functions and a BLOWFISH_KEY structure.</li>
<li>Call the key_schedule() function with the key, the BLOWFISH_KEY structure, and the length of the key in bytes.</li>
<li>To encrypt data, call the blowfish_encrypt() function passing the array with the plaintext, the array to hold the output ciphertext, and the key structure.</li>
<li>Do decrypt data, call the blowfish_decrypt() function passing the array with the ciphertext, the array to hold the output plaintext, and the key structure.</li>
</ol>


<div class="section-title">Code</div>
&bull; <a href="/files/projects/code/blowfish.c">Source Code</a> <br />
&bull; <a href="/files/projects/code/blowfish_example.c">Sample Driver Program</a>
<br /><br />

<div class="section-title">Notes</div>
In this implementation, all the initial P-Box and S-Box constants are stored in static memory location and copied to temporary memory locations for use in the key-initialization step. This is because the S-Box and P-Box values will be altered during the key initialization, so if the implementor wishes to encrypt data using multiple keys in the same session, then the key initialization function must be called more than once. Each time it is called it must start with the same constant values, but these values are altered during the key initialization. Since the P/S-Boxes are crafted uniquely for each encryption key, using a single universal array to store those values would allow multiple keys to be used sequentially, but it would prevent the developer from using multiple keys at the same time. Thus the blowfish key is represented as a structure that houses both the P-Box and S-Boxes. Using structures allows the developer to create and manage as many keys as desired by creating one structure per key.
<br /><br />

The slight drawback to this method is  that copying all the initial data introduces a minor performance hit when calling the key schedule, and using multiple memory locations boosts the amount of memory necessary (every Blowfish key requires just over 1KB of memory). However, on modern PCs, the memory performance hit is completely negligible. Assuming the developer only needs to use one Blowfish key, this code could be optimized to just use the permanent S/P-Boxes. These factors are only of practical concern for extremely computationally-weak machines (very low-end PCs, specialized hardware). 
<br /><br />

Cryptanalysis of the Blowfish algorithm itself to date has yielded but a few minor flaws, placing Blowfish as one of the best 64-bit block ciphers in existence. However, 64-bit block ciphers are known to leak information about the plaintext and should not be used if possible. So although no reasonable attacks exist against Blowfish itself, its 64-bit block size make it less than undesirable in the modern cryptographic world.]]></content:encoded>
			<wfw:commentRss>http://bradconte.com/blowfish_c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Base 64 &#8211; C</title>
		<link>http://bradconte.com/base64_c.html</link>
		<comments>http://bradconte.com/base64_c.html#comments</comments>
		<pubDate>Wed, 26 Jul 2006 07:11:42 +0000</pubDate>
		<dc:creator>Brad Conte</dc:creator>
				<category><![CDATA[My Code]]></category>

		<guid isPermaLink="false">http://test.b-con.us/code/base-64-c</guid>
		<description><![CDATA[Algorithm This is an implementation of Base64 encoding. Base64 is an encoding scheme that allows binary data to be represented as text. It accepts a bit string of any length, splits it up into chucks of 6 bits, and replaces each chunk with its corresponding value from the list of characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/ When the input [...]]]></description>
			<content:encoded><![CDATA[<div class="section-title">Algorithm</div>
This is an implementation of <a href="http://www.faqs.org/rfcs/rfc3548.html">Base64 encoding</a>.
<br /><br />

Base64 is an encoding scheme that allows binary data to be represented as text. It accepts a bit string of any length, splits it up into chucks of 6 bits, and replaces each chunk with its corresponding value from the list of characters:
<code>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/</code>
When the input is not an even multiple of 6 bits, the character &#8216;=&#8217; is appended to the output as padding as needed.
<br /><br />

Since the input has 8 bits of significant data and the output has 6 <em>significant</em> bits of data (there are 2<sup>6</sup>=64 characters in the array above), the input to output ratio is very close to 3:4.
<br /><br />

<div class="section-title">Code Documentation</div>
<ul>
	<li> <span class="code_function">void base64_encode(unsigned char in[], unsigned char out[], int len)</span> <br />
This encodes raw data to Base64.
<ul>
	<li> <span class="code_function">unsigned char in[]</span> <br />
This contains the data to be encoded.</li>
	<li> <span class="code_function">unsigned char out[]</span> <br />
This will contains the output. The array should be larger than the input by a scale of 4:3, with a two character buffer.</li>
	<li> <span class="code_function">int len</span> <br />
This is the length, in bytes, of the input.</li>
	<li> <span class="code_function">int newline_flag</span> <br />
This is a flag, which can be set to true or false, to indicate whether or not the function should insert a newline in the base64 output every 76 characters. The technical specification for Base64 calls for this value, but this was based on technical standards/difficulties of the past and isn&#8217;t always desirable. By setting it to a non-zero value, a newline character will be added every 76 output characters (to wrap it in a rough rectangle shape), if it is set to 0 then the output will be one long string.</li>
</ul>
</li>
	<li> <span class="code_function">base64_decode(unsigned char in[], unsigned char out[], int len)</span> <br />
This function decodes Base64 code into its original form.
<ul>
	<li> <span class="code_function">unsigned char in[]</span> <br />
This contains the Base64 data to be decoded.</li>
	<li> <span class="code_function">unsigned char out[]</span> <br />
This is the output array, it should be at least 3/4 the length of the input data, with an extra character of buffer.</li>
	<li> <span class="code_function">int len</span> <br />
This is the length of the input Base64 data.</li>
	<li> Note: There is no need to include a newline flag for the decoding function, as it automatically parses out newline characters.</li>
</ul>
</li>
</ul>
<div class="section-title">Code Usage</div>
<ol>
	<li> First create/allocate two arrays to contain your original and encoded data.</li>
	<li> Determine the length of the data you wish to encode. Pass the input data, the array to contain the output, and the length of the data as arguments to the base64_encode() function.</li>
	<li> When you wish to decode the data, use the base64_decode() function. It works the same way, except the data gets decoded.</li>
</ol>
<div class="section-title">Code</div>

â€¢ <a href="/files/projects/code/base64.c">Source Code</a> <br />
â€¢ <a href="/files/projects/code/base64_example.c">Sample Driver Program</a>
<br /><br />

<div class="section-title">Notes</div>

This is the traditional base64 encoding scheme. The &#8220;Internet Base64&#8243;, in order to be URL friendly, replaces the last two characters of the substitution string, &#8220;+&#8221; and &#8220;/&#8221;, with &#8220;*&#8221; and &#8220;-&#8221;, respectively.
<br /><br />

&bull; Thanks to Christoph Otto for fixing a NULL termination bug (7-26-06).
&bull; ]]></content:encoded>
			<wfw:commentRss>http://bradconte.com/base64_c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AES &#8211; C</title>
		<link>http://bradconte.com/aes_c.html</link>
		<comments>http://bradconte.com/aes_c.html#comments</comments>
		<pubDate>Tue, 25 Jul 2006 08:20:36 +0000</pubDate>
		<dc:creator>Brad Conte</dc:creator>
				<category><![CDATA[My Code]]></category>

		<guid isPermaLink="false">http://test.b-con.us/code/aes-c</guid>
		<description><![CDATA[Algorithm This is an implementation of the AES block cipher in 128, 192, and 256 bit modes, where the bit size corresponds to the key length. The algorithm encrypts plaintext in block sizes of 128 bits. Code Documentation KeySchedule(unsigned char key[], unsigned int w[]) This function takes the generated key (128, 192, or 256 bits [...]]]></description>
			<content:encoded><![CDATA[<div class="section-title">Algorithm</div>
This is an implementation of the <a href="http://csrc.nist.gov/CryptoToolkit/aes/rijndael/">AES</a> block cipher in 128, 192, and 256 bit modes, where the bit size corresponds to the key length. The algorithm encrypts plaintext in block sizes of 128 bits.
<br /><br />

<div class="section-title">Code Documentation</div>
<ul>
	<li> <span class="code_function">KeySchedule(unsigned char key[], unsigned int w[])</span> <br />
This function takes the generated key (128, 192, or 256 bits in length) and generates a key schedule to use in encryption.
<ul>
	<li> <span class="code_function">unsigned char key[]</span> <br />
Must contain 16, 24, or 32 bytes of data to be used as the key.</li>
	<li> <span class="code_function">unsigned int w[]</span> <br />
Must be 60 32-bit integers in size. This will be the outputted key schedule.</li>
	<li> <span class="code_function">int keysize</span> <br />
Must be the size of the key in bits, limited to the values 128, 192, or 256.</li>
</ul>
</li>
	<li> <span class="code_function">aes_encrypt(unsigned char in, unsigned char out, unsigned int key[], int keysize)</span> <br />
<span class="code_function">aes_decrypt(unsigned char in, unsigned char out, unsigned int key[], int keysize)</span> <br />
These functions encrypt and decrypt data, respectively.
<ul>
	<li> <span class="code_function">unsigned char in[]</span> <br />
The plaintext to be encrypted. The array must be 16 bytes in size.</li>
	<li> <span class="code_function">unsigned char out[]</span> <br />
The output ciphertext. The array must be 16 bytes in size.</li>
	<li> <span class="code_function">unsigned int key[]</span> <br />
The &#8220;w&#8221; array key schedule from the KeySchedule() function.</li>
	<li> <span class="code_function">int keysize</span> <br />
Ensure it&#8217;s the same value as the &#8220;keysize&#8221; value in KeySchedule() function.</li>
</ul>
</li>
</ul>
<div class="section-title">Code Usage</div>
<ol>
	<li>Call the KeyExpantion() function with the encryption key you want to use, the key schedule array, and the number of bits in length of the key.</li>
	<li>To decrypt data, call the aes_encrypt() function with the input plaintext array, an array to hold the output ciphertext, and the size of the original key in bits (same as the value passed to the KeyExpantion() function).</li>
	<li>To decrypt data, call the aes_decrypt() function with the input ciphertext array, an array to hold the output plaintext, and the size of the original key in bits (same as the value passed to the KeyExpantion() function).</li>
</ol>
<div class="section-title">Code</div>
â€¢ <a href="/files/projects/code/aes.c">Source Code</a> <br />
â€¢ <a href="/files/projects/code/aes_example.c">Sample Driver Program</a>
<br /><br />

<div class="section-title">Notes</div>
This implementation is designed for little endian processors, but I think should work for big endian processors as well.
<br /><br />

I&#8217;ve taken some steps towards optimizing it, (basic things like unrolling loops), but the implementation is fairly slow. I hope to be able to optimize it more in the future.
<br /><br />

Also note that this is not a hardened implementation of AES. It has benchmarked to be fairly slow and has not been designed to be resistant to side-channel attacks. Optimizing it and attack-proofing it are on my to-do list.
<br /><br />

There are a few extra functions included in this code, printstate() and print_rnd_key(), and print_arry(), that fall under the heading of &#8220;Debugging Functions&#8221;. These are not a part of the AES implementation and may be removed, they only exist to aid in debugging the code should the implementor seek to optimize it or tinker with it in any way. I used them excessively when originally implementing the code, and figured they might come in handy to someone else as well. They&#8217;re not complex, but useful.]]></content:encoded>
			<wfw:commentRss>http://bradconte.com/aes_c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SHA-256 &#8211; C</title>
		<link>http://bradconte.com/sha256_c.html</link>
		<comments>http://bradconte.com/sha256_c.html#comments</comments>
		<pubDate>Tue, 25 Jul 2006 07:27:34 +0000</pubDate>
		<dc:creator>Brad Conte</dc:creator>
				<category><![CDATA[My Code]]></category>

		<guid isPermaLink="false">http://test.b-con.us/code/sha256-c</guid>
		<description><![CDATA[Algorithm This is an implementation of the SHA-256 hash algorithm. Code Documentation SHA256_CTX A MD5 structure that will hold all hash-related data and calculations as the hash is calculated. SHA256_init(SHA256_CTX *ctx) Initializes the SHA256_CTX object. SHA1_update(SHA256_CTX *ctx, unsigned char data[], int len) Once an object has been created and initialized, the data to be hashed [...]]]></description>
			<content:encoded><![CDATA[<div class="section-title">Algorithm</div>
This is an implementation of the <a href="http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf">SHA-256</a> hash algorithm. 
<br /><br />

<div class="section-title">Code Documentation</div>
<ul> 

   <li> <span class="code_function">SHA256_CTX</span> <br />
        A MD5 structure that will hold all hash-related data and calculations as the hash is calculated. <br /><br /></li>

   <li> <span class="code_function">SHA256_init(SHA256_CTX *ctx)</span><br />
        Initializes the SHA256_CTX object. <br /><br /></li> 

   <li> <span class="code_function">SHA1_update(SHA256_CTX *ctx, unsigned char data[], int len)</span> <br />
        Once an object has been created and initialized, the data to be hashed must be added. Due to practical limitations, it may not be optimal (or possible) to add all the data to the SHA256 hash in one data chunk, so the function inputs, stores, and calculates data as it is received, allowing the data to be added in as many chunks as necessary. <br /><br />

   <ul>
      <li> <span class="code_function">unsigned char data[]</span> <br />
           This is the data to be added to the hash. </li>

      <li> <span class="code_function">int len</span> <br />
           This is the length, in bytes, of the data in the &#8220;data&#8221; array. </li>
   </ul>
   <br /><br />
   </li>

   <li> <span class="code_function">SHA256_final(SHA256_CTX *ctx, unsigned char hash[])</span> <br />
        Finalize and output the hash.<br /><br /></li>

   <ul>

      <li> <span class="code_function">unsigned char hash[]</span> <br />
           This is the array to store the output hash. It must be at least 16 bytes in size.</li>

   </ul>

</ul>

<div class="section-title">Code Usage</div>
<ol>
<li> Create an SHA256_CTX object.
<li> Initialize it with sha256_init().
<li> Read some/all of the data to hash into an array, calculate the size of the data, and add it to the hash with sha256_update().
<li> Repeat the previous step for all the data you want to hash.
<li> Finalize and output the hash with sha256_final().
</ol>
Repeat steps (2) to (5) for as many hashes as you want to calculate.
<br /><br />


<div class="section-title">Code</div> 
&bull; <a href="/files/projects/code/sha256.c">Source Code</a> <br />
&bull; <a href="/files/projects/code/sha256_example.c">Sample Driver Program</a>
<br /><br />

<div class="section-title">Notes</div>
The 32-bit words (which in this case are unsigned integers) used in the code use little endian byte ordering. The SHA-256 specification uses the big endian byte order, so some byte-reversals are made when copying data into and out of integers in this code.
<br /><br />

This algorithm can hash data of any length, although 2<sup>64</sup> bits (2,147,483,648 gigabytes) is the recommended limit. 
<br /><br />

This algorithm has not been implemented in an optimized manner. This algorithm has passed testing against numerous test vectors, including all official vectors.]]></content:encoded>
			<wfw:commentRss>http://bradconte.com/sha256_c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>(3)DES &#8211; C</title>
		<link>http://bradconte.com/3des_c.html</link>
		<comments>http://bradconte.com/3des_c.html#comments</comments>
		<pubDate>Mon, 10 Jul 2006 07:25:35 +0000</pubDate>
		<dc:creator>Brad Conte</dc:creator>
				<category><![CDATA[My Code]]></category>

		<guid isPermaLink="false">http://test.b-con.us/code/3des-c</guid>
		<description><![CDATA[Algorithm This is an implementation of the DES and 3 DES block ciphers. 3DES (is simply the DES algorithm iterated three times and sometimes refered to as Triple DES. This algorithm encrypts plaintext in block sizes of 8 bytes. Code Documentation For DES: void key_schedule(unsigned char key[], unsigned char schedule[][6], unsigned int mode) This function [...]]]></description>
			<content:encoded><![CDATA[<div class="section-title">Algorithm</div>
This is an implementation of the <a href="">DES and 3 DES</a> block ciphers. 3DES (is simply the DES algorithm iterated three times and sometimes refered to as Triple DES. This algorithm encrypts plaintext in block sizes of 8 bytes. 
<br /><br />

<div class="section-title">Code Documentation</div> 
For DES:

<ul>

   <li> <span class="code_function">void key_schedule(unsigned char key[], unsigned char schedule[][6], unsigned int mode)</span> <br />
        This function generates a multi-dimensional key schedule from a user-supplied key, the key schedule will be used for encryption.<br /><br />
        
   <ul>
      
      <li> <span class="code_function">unsigned char key[]</span> <br />
           Must contain 8 bytes of data to be used as the key. <br /></li>

      <li> <span class="code_function">unsigned int schedule[][6]</span> <br />
           Must be 16 arrays of 6 bytes each. This will contain the final key output. </li>

      <li> <span class="code_function">unsigned int mode</span> <br />
           This value should be set to appropriate the macro-defined ENCRYPT or DECRYPT value. </li>

   </ul>
   <br /><br />
   </li>

   <li> <span class="code_function">void des_crypt(unsigned char in[], unsigned char out[], unsigned char key[][6])</span> <br />
        This function both encrypts and decrypts text using the key schedule. <br /><br />

   <ul>

      <li> <span class="code_function">unsigned char in[]</span> <br /> 
           This contains the plaintext/ciphertext to be encrypted/decrypted. It must contain 8 bytes of data. Padding may be necessary if it is the last block of the plaintext. </li>

      <li> <span class="code_function">unsigned char out[]</span> <br />
           This contains the encrypted/decrypted ciphertext/plaintext output. It must be 8 bytes in size. </li> 

      <li> <span class="code_function">unsigned int key[][6]</span> <br />
           The &#8220;schedule&#8221; array from the key_schedule() function.</li>
   
   </ul>
   <br /><br />
   </li>
        
</ul>

For 3DES:
<ul>

   <li> <span class="code_function">void three_des_key_schedule(unsigned char key[], unsigned char schedule[][16][6], unsigned int mode)</span> <br/><br />
        
   <ul>

      <li> <span class="code_function">unsigned char key[]</span> <br />
           This array must contain 24 bytes of data to be used as the encryption key.</li>

       <li> <span class="code_function">unsigned int schedule[][16][6]</span> <br />
            Must be 3 arrays each with 16 arrays of 6 bytes each.</li>

       <li> <span class="code_function">unsigned int mode</span> <br /> 
            Set this equal to the macro-defined ENCRYPT or DECRYPT values. </li>

   </ul>
   <br /><br />
   </li>

   <li> <span class="code_function">three_des_crypt()</span> <br />
        function must be called, the same function both encrypts or decrypts the data. <br /><br />

   <ul>
      
      <li> <span class="code_function">unsigned char in[]</span> <br />
           Must contain 8 bytes of data to be encrypted/decrypted.</li> 

      <li> <span class="code_function">unsigned char out[]</span> <br />
           Must be 8 bytes in size to hold the encrypted/decrypted data.</li> 

      <li> <span class="code_function">unsigned int key[][16][6]</span> <br /> 
           The &#8220;schedule&#8221; array from three_key_schedule() function.</li> 

   </ul>
   <br /><br />
   </li>

</ul>

<br /><br />
<div class="section-title">Code Usage</div>
For DES: 

<ol>

   <li> Generate an 8 byte key. Note that only the seven most significant bits of each byte will actually be used.</li>
   <li> Use the key_schedule() function to generate a key schedule from the eight byte key.</li>
   <li> Use the des_crypt() function to encrypt your plaintext in blocks of 8 bytes. (You will likely have to pad your last block of plaintext.) The output is the ciphertext.</li>
   <li> To decrypt the ciphertext, switch the &#8220;mode&#8221; argument for des_crypt() and pass said function the ciphertext as input.</li>
</ol> 

For 3DES: 

<ol>
   <li> Generate a 24 byte key. Note that only the seven most significant bits of each byte will actually be used.</li>
   <li> Use the three_des_key_schedule() function to expand the key into a key schedule.</li>
   <li> Use the three_des_crypt() function to encrypt your plaintext in blocks of 8 bytes. (You will likely have to pad your last block of plaintext.) The output is the ciphertext.</li>
   <li> To decrypt the ciphertext, switch the &#8220;mode&#8221; argument for three_des_crypt() and pass said function the ciphertext as input.</li>
</ol>

<div class="section-title">Code</div> 
&bull; <a href="/files/projects/code/des.c">Source Code</a> <br />
&bull; <a href="/files/projects/code/des_example.c">Sample Driver Program</a>
<br /><br />

<div class="section-title">Notes</div> 
This implementation adheres fully to the official DES specification and includes the Initial Permutation and Inverse Initial Permutation steps that are often neglected for convenience, as they serve no cryptographic purposes. 
<br /><br />

This is an amature implementation of DES. It has benchmarked to be fairly slow and has not been designed to be resistant to any sort of side-channel attacks. Hopefully I will optimize and attack-proof it in the future.]]></content:encoded>
			<wfw:commentRss>http://bradconte.com/3des_c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SHA-1 &#8211; C</title>
		<link>http://bradconte.com/sha1_c.html</link>
		<comments>http://bradconte.com/sha1_c.html#comments</comments>
		<pubDate>Wed, 28 Jun 2006 07:22:36 +0000</pubDate>
		<dc:creator>Brad Conte</dc:creator>
				<category><![CDATA[My Code]]></category>

		<guid isPermaLink="false">http://test.b-con.us/code/sha1-c</guid>
		<description><![CDATA[Algorithm This is an implementation of the SHA-1 hash algorithm. Code Documentation SHA1_CTX A MD5 structure that will hold all hash-related data and calculations as the hash is calculated. SHA1_init(SHA1_CTX *ctx) Initializes the SHA1_CTX object. SHA1_update(SHA1_CTX *ctx, unsigned char data[], int len) Once an object has been created and initialized, the data to be hashed [...]]]></description>
			<content:encoded><![CDATA[<div class="section-title">Algorithm</div>
This is an implementation of the <a href="http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf">SHA-1</a> hash algorithm. 
<br /><br />

<div class="section-title">Code Documentation</div>
<ul> 

   <li> <span class="code_function">SHA1_CTX</span> <br />
        A MD5 structure that will hold all hash-related data and calculations as the hash is calculated. <br /><br /></li>

   <li> <span class="code_function">SHA1_init(SHA1_CTX *ctx)</span><br />
        Initializes the SHA1_CTX object. <br /><br /></li> 

   <li> <span class="code_function">SHA1_update(SHA1_CTX *ctx, unsigned char data[], int len)</span> <br />
        Once an object has been created and initialized, the data to be hashed must be added. Due to practical limitations, it may not be optimal (or possible) to add all the data to the SHA1 hash in one data chunk, so the function inputs, stores, and calculates data as it is received, allowing the data to be added in as many chunks as necessary. <br /><br />

   <ul>
      <li> <span class="code_function">unsigned char data[]</span> <br />
           This is the data to be added to the hash. </li>

      <li> <span class="code_function">int len</span> <br />
           This is the length, in bytes, of the data in the &#8220;data&#8221; array. </li>
   </ul>
   <br /><br />
   </li>

   <li> <span class="code_function">SHA1_final(SHA1_CTX *ctx, unsigned char hash[])</span> <br />
        Finalize and output the hash.<br /><br /></li>

   <ul>

      <li> <span class="code_function">unsigned char hash[]</span> <br />
           This is the array to store the output hash. It must be at least 16 bytes in size.</li>

   </ul>

</ul>

<div class="section-title">Code Usage</div>
<ol>
<li> Create an SHA1_CTX object.
<li> Initialize it with sha1_init().
<li> Read some/all of the data to hash into an array, calculate the size of the data, and add it to the hash with sha1_update().
<li> Repeat the previous step for all the data you want to hash.
<li> Finalize and output the hash with sha1_final().
</ol>
Repeat steps (2) to (5) for as many hashes as you want to calculate.
<br /><br />

<div class="section-title">Code</div> 
&bull; <a href="/files/projects/code/sha1.c">Source Code</a> <br />
&bull; <a href="/files/projects/code/sha1_example.c">Sample Driver Program</a>
<br /><br />

<div class="section-title">Notes</div> 
The 32-bit words (which in this case are unsigned integers) used in the code use little endian byte ordering. The SHA-1 specification uses the big endian byte order, so some byte-reversals are made when copying data into and out of integers in this code. 
<br /><br />

This algorithm has not been optimized, although some general attempts have been made to that effect. This algorithm has been tested against numerous test vectors (including all official ones) and passed the tests.
<br /><br />

This algorithm can hash data of any length, although 2<sup>64</sup> bits (2,147,483,648 gigabytes) is the recommended limit. 
<br /><br />

It is worth noting that SHA-1 is no longer considered a perfectly secure hash algorithm. It is recommended that a hash algorithm such as Whirlpool or RIPE-MD, or even SHA-1 with double the number of standard rounds, be used in when security is critical. ]]></content:encoded>
			<wfw:commentRss>http://bradconte.com/sha1_c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MD5 &#8211; C</title>
		<link>http://bradconte.com/md5_c.html</link>
		<comments>http://bradconte.com/md5_c.html#comments</comments>
		<pubDate>Wed, 21 Jun 2006 07:21:38 +0000</pubDate>
		<dc:creator>Brad Conte</dc:creator>
				<category><![CDATA[My Code]]></category>

		<guid isPermaLink="false">http://test.b-con.us/code/md5-c</guid>
		<description><![CDATA[Algorithm This is an implementation of the MD5 hash algorithm. Code Documentation MD5_CTX A MD5 structure that will hold all hash-related data and calculations as the hash is calculated. md5_init(MD5_CTX *ctx) Initializes the MD5_CTX object. md5_update(MD5_CTX *ctx, unsigned char data[], int len) Once an object has been created and initialized, the data to be hashed [...]]]></description>
			<content:encoded><![CDATA[<div class="section-title">Algorithm</div>
This is an implementation of the <a href="http://www.faqs.org/rfcs/rfc1321.html">MD5</a> hash algorithm. 
<br /><br />

<div class="section-title">Code Documentation</div>
<ul> 

   <li> <span class="code_function">MD5_CTX</span> <br />
        A MD5 structure that will hold all hash-related data and calculations as the hash is calculated. <br /><br /></li>

   <li> <span class="code_function">md5_init(MD5_CTX *ctx)</span><br />
        Initializes the MD5_CTX object. <br /><br /></li> 

   <li> <span class="code_function">md5_update(MD5_CTX *ctx, unsigned char data[], int len)</span> <br />
        Once an object has been created and initialized, the data to be hashed must be added. Due to practical limitations, it may not be optimal (or possible) to add all the data to the MD5 hash in one data chunk, so the function inputs, stores, and calculates data as it is received, allowing the data to be added in as many chunks as necessary. <br /><br />

   <ul>
      <li> <span class="code_function">unsigned char data[]</span> <br />
           This is the data to be added to the hash. </li>

      <li> <span class="code_function">int len</span> <br />
           This is the length, in bytes, of the data in the &#8220;data&#8221; array. </li>
   </ul>
   <br /><br />
   </li>

   <li> <span class="code_function">md5_final(MD5_CTX *ctx, unsigned char hash[])</span> <br />
        Finalize and output the hash.<br /><br /></li>

   <ul>

      <li> <span class="code_function">unsigned char hash[]</span> <br />
           This is the array to store the output hash. It must be at least 16 bytes in size.</li>

   </ul>

</ul>

<div class="section-title">Code Usage</div>
<ol>
<li> Create an MD5_CTX object.
<li> Initialize it with md5_init().
<li> Read some/all of the data to hash into an array, calculate the size of the data, and add it to the hash with md5_update().
<li> Repeat the previous step for all the data you want to hash.
<li> Finalize and output the hash with md5_final().
</ol>
Repeat steps (2) to (5) for as many hashes as you want to calculate.
<br /><br />

<div class="section-title">Code</div>
&bull; <a href="/files/projects/code/md5.c">Source Code</a> <br />
&bull; <a href="/files/projects/code/md5_example.c">Sample Driver Program</a>
<br /><br />

<div class="section-title">Notes</div>
The 32-bit words (which in this case are unsigned integers) used in the code assume little endian byte ordering. The MD5 specification uses the big endian byte order, so some byte-reversals are made when copying data into and out of integers in this code. 
<br /><br />

This algorithm has not actually been optimized, although some general attempts have been made to that effect. This algorithm has been tested against numerous test vectors (including all official ones) and has proved to be accurate. 
<br /><br /> 

This algorithm can hash data of any length, although 2<sup>64</sup> bits (2,147,483,648 gigabytes) is the recommended limit.  
<br /><br />

Note, that MD5 is no longer considered a reasonably secure hash algorithm. It is recommended that a hash algorithm such as Whirlpool, RIPEMD, or SHA1 (with double the number of standard rounds) be used rather than MD5. ]]></content:encoded>
			<wfw:commentRss>http://bradconte.com/md5_c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

