<?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"
	>

<channel>
	<title>unitstep.net &#187; chap-php</title>
	<atom:link href="http://unitstep.net/blog/category/chap-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://unitstep.net</link>
	<description>the home of peter chng</description>
	<pubDate>Sun, 05 Oct 2008 17:42:16 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Password salting and the modified Challenge-Response system</title>
		<link>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/</link>
		<comments>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 01:12:42 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[CHAP]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[authentication]]></category>

		<category><![CDATA[chap-php]]></category>

		<category><![CDATA[login]]></category>

		<category><![CDATA[passwords]]></category>

		<category><![CDATA[security]]></category>

		<category><![CDATA[challenge-response]]></category>

		<category><![CDATA[salting]]></category>

		<guid isPermaLink="false">http://unitstep.net/?p=312</guid>
		<description><![CDATA[Since I released my demo/example modified Challenge-Response Ajax PHP Login System about a month ago (which was based on ideas from Paul Johnston), I&#8217;ve been receiving some questions about why salting was not incorporated into the system.  In particular, there was a discussion at a Dutch-language forums, which I somewhat understood after Google translation. [...]]]></description>
			<content:encoded><![CDATA[<p>Since I released my demo/example <a href="/blog/2008/03/29/a-challenge-response-ajax-php-login-system/">modified Challenge-Response Ajax <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> Login System</a> about a month ago (which was based on ideas from <a href="http://pajhome.org.uk/crypt/md5/auth.html">Paul Johnston</a>), I&#8217;ve been receiving some questions about why <a href="http://en.wikipedia.org/wiki/Salt_(cryptography)">salting</a> was not incorporated into the system.  In particular, there was a <a href="http://gathering.tweakers.net/forum/list_messages/1284731///salt">discussion</a> at a Dutch-language forums, which I somewhat understood after <a href="http://www.google.com/translate?u=http%3A%2F%2Fgathering.tweakers.net%2Fforum%2Flist_messages%2F1284731%2F%2F%2Fsalt&#038;langpair=nl%7Cen&#038;hl=en&#038;ie=UTF8">Google translation</a>. </p>
<p>Here, I&#8217;ll try to address some of these concerns and answer some questions.</p>
<h3>Salt of the earth</h3>
<p>To understand why salting is used, we must first understand exactly what it is.  Just like regular salt, in cryptography, salting is used to alter the &#8220;taste&#8221; or output of some process. </p>
<p>In the case of password storage, most people would realize that you should <em>never</em> store lists of users&#8217; passwords in plaintext, because if that data is ever compromised, attackers will gain access not only the compromised accounts but could potentially use the passwords to gain access to user accounts on other services/sites.  This is because people often reuse the same passwords across multiple account/services.</p>
<p>The easiest way to avoid this is to store the <em>hash</em> of the password.  A hash is a one-way function, that is, once you compute the hash of a value, you cannot obtain the original from just the hashed value.  Some typical hash functions are MD5 or the more secure family of SHA hash functions.</p>
<p>However, this still doesn&#8217;t fully conceal passwords.  If an attacker were to obtain the list of hashed passwords, they could try a dictionary-based attack to discover the original inputs.  This involves hashing common words to see if they hash to the correct value.  Since people often use common words or combinations of such, a dictionary-based attack has the advantage of having far fewer combinations that the attacker needs to try compared to a true brute-force attack.</p>
<h3>Adding variability</h3>
<p>This problem can be mitigated by salting, which basically amounts to combining the password with additional input before passing it into the hash function.  This alters the end output from the hash function so that a dictionary-based attack cannot be used, <strong>provided the salt is kept a secret</strong>.  A comparison example:</p>
<pre><code>Without salting:
hash(A) -&gt; B;

With salting:
hash (A + S) -&gt; C;</code></pre>
<p>In the first example, salting was not used before hashing.  Assuming the value &#8216;A&#8217; is a common word or phrase, an attacker can use a dictionary-attack to determine what the value of &#8216;A&#8217; was. (I.E. what value hashes to the value of &#8216;C&#8217;)</p>
<p>With salting, things become more difficult.  If the attacker does not know the value of the salt (S), they cannot use a dictionary-based attack because the actual input will not be a common word or phrase.  Instead, they must try all values in the dictionary <em>with</em> all possible values of the salt.  In fact, every bit of the added salt value doubles the number of computations in a dictionary-based attack.  The important point to retain here is the <strong>salt value must be kept a secret</strong> in order to obtain this benefit.</p>
<p>One other benefit of salting is to ensure that two accounts with the same password don&#8217;t produce the same hash.  This can be accomplished by making different accounts have different salts.  The obvious benefit of this is to decrease the information leakage from the hashes, as otherwise, equivalent hashes would infer equivalent passwords.</p>
<h3>Salting and the modified Challenge-Response System</h3>
<p>With the <a href="/blog/2008/03/29/a-challenge-response-ajax-php-login-system/">modified challenge-response system</a>, it isn&#8217;t clear to me how salting could be used to improve it.  Here&#8217;s a quick re-cap of how it works:</p>
<blockquote><p>
Signup:</p>
<p>1. Server sends random1<br />
2. Client sends hex_sha1(hex_hmac_sha1(password, random1))</p>
<p>Login:</p>
<p>1. Server sends random1 and random2<br />
2. Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))
</p></blockquote>
<p>To login, the user must present two values: One to verify that they know the password, and the second value, which is used to set the response that must be computed for the <em>next login</em>.</p>
<p>Because the client must compute the next response value, it&#8217;s a bit tricky to implement salting in a way that&#8217;s beneficial. </p>
<p>One possible method would be to further hash the second value (<code>hex_sha1(hex_hmac_sha1(password, random1))</code>) with a <strong>server secret</strong> before storing it in the database.  This would complicate things should the database be compromised but not the server secret, since a dictionary attack would become much harder with the extra variability of a server secret.  In this case, the work flow would look something like this:</p>
<h4>Signup</h4>
<ol>
<li>Server sends random1</li>
<li>Client sends hex_sha1(hex_hmac_sha1(password, random1)) [<strong>Let's call this value `hashed_challenge_response` for brevity</strong>]</li>
<li>Server stores hex_hmac_sha1(server_secret, hashed_challenge_response)</li>
</ol>
<h4>Login:</h4>
<ol>
<li>Server sends random1 and random2</li>
<li>Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))</li>
<li>Server computes hex_sha1(hex_hmac_sha1(password, random1)) [<strong>Call this `hashed_challenge_response_received`</strong>]</li>
<li>Server checks if hex_hmac_sha1(server_secret, hashed_challenge_response_received) equals the value previously stored.  If so, authentication was successful and a new challenge-response is stored based on the second value received.</li>
</ol>
<p>Note that this method <strong>would not improve the login security anymore</strong>, since an attacker who captured the intermediate traffic of a successful login could still conduct an offline dictionary attack, which this challenge-response system is unfortunately susceptible to.</p>
<p>However, the modified system does benefit from the use of the `random1` and `random2` challenge strings, which are stored alongside the response values.  Since these are random and different for each user (and for each subsequent login), accounts with the same password will not have the same hash-response.  This effectively gives the second lesser benefit of salting.</p>
<p>The modified challenge-response system also suffers from the inability of the server to enforce strong passwords.  Because the initial value sent during registration is a hashed value of the password and a random value, the server cannot be aware of any properties of the password such as its length or composition.  The only aspect that could be enforced server-side would be to make sure the password was not blank!  My suggestion is to (attempt to) enforce password complexity using JavaScript on the client side.  Such rules can obviously be circumvented but are better than nothing.</p>
<h3>Conclusion</h3>
<p>Hopefully I&#8217;ve shed some light on the topic of salting in relation to the modified challenge-response Ajax <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> login system.  I&#8217;m no security expert, so you should not take my advise as scripture.  Please don&#8217;t hesitate to give me your comments or feedback!</p>
<hr/>Copyright &copy; 2008 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/04/28/password-salting-and-the-modified-challenge-response-system/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Challenge-Response Ajax PHP Login System</title>
		<link>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/</link>
		<comments>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/#comments</comments>
		<pubDate>Sun, 30 Mar 2008 01:53:11 +0000</pubDate>
		<dc:creator>Peter Chng</dc:creator>
		
		<category><![CDATA[Ajax]]></category>

		<category><![CDATA[CHAP]]></category>

		<category><![CDATA[JavaScript]]></category>

		<category><![CDATA[PHP]]></category>

		<category><![CDATA[authentication]]></category>

		<category><![CDATA[chap-php]]></category>

		<category><![CDATA[login]]></category>

		<category><![CDATA[passwords]]></category>

		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/</guid>
		<description><![CDATA[A while ago, (okay, a long while ago) I wrote about a way to improve the security of login/authentication with web applications.  The process involved using challenge-response during authentication to prevent passwords from being transmitted in plaintext.  The idea was not mine, but instead the work of a smart fellow named Paul Johnston. [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, (okay, a <em>long</em> while ago) I wrote about <a href="/blog/2006/09/19/using-a-chap-login-system-to-improve-security/">a way to improve the security</a> of login/authentication with web applications.  The process involved using challenge-response during authentication to prevent passwords from being transmitted in plaintext.  The <a href="http://pajhome.org.uk/crypt/md5/auth.html">idea was not mine</a>, but instead the work of a smart fellow named <a href="http://pajhome.org.uk/">Paul Johnston</a>.   At the time, I &#8220;hoped to present an actual implementation&#8221; sometime in the future, but never got around to it.  I finally had some time and decided to put together a working example using <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> and JavaScript. </p>
<h3>Download the source</h3>
<p>Please feel free to download and try out the first public release of the CHAP-PHP login system.  The zip file has the full source and provides an example how to implement the system both on the client and server side.</p>
<div class="download">
<a class="icon" href='http://unitstep.net/wordpress/wp-content/uploads/2008/03/chap-php-051.zip' title='CHAP-PHP-0.5.1'>Download CHAP-PHP-0.5.1</a>
</div>
<p>The same demo available in the zip file is also <a href="/projects/CHAP-PHP/src/demo/index.php">available here</a>.</p>
<h3>Improving authentication security with Challenge-Response</h3>
<p>Challenge-Response is the basis for many authentication systems.  In such a situation, a server may have to authenticate a user by verifying their credentials, usually in the form of a password.  However, transmitting plaintext passwords over connections that are not secure can lead to compromises.  In such a situation, <dfn>challenge-response</dfn> may be used.  This usually involves the server sending a <strong>random challenge</strong> string to the client, which must then produce an appropriate <strong>response</strong> that can only be computed using the challenge and the password.  This response is then sent to the server, which can then verify if the right password was used to generate it.  The response is usually computed by hashing a value that depends on the challenge and the password, thus it is not possible to obtain the password from the response, which might have been sniffed on an insecure connection. </p>
<p>However, such a traditional challenge-response has the downside that the plaintext password (or a password-equivalent) must be known to the server at some point.  Paul Johnston came up with <a href="http://pajhome.org.uk/crypt/md5/auth.html">an idea for an alternative system</a> a while ago that overcomes these shortcomings.  (Though it is not free from weaknesses itself)  It is this &#8220;Alternative System&#8221; that the above release is based upon.  Here is a quick explanation of the system, adapted from Johnston&#8217;s site:</p>
<blockquote cite="http://pajhome.org.uk/crypt/md5/auth.html"><p>
Signup:</p>
<p>   1. Server sends random1<br />
   2. Client sends hex_sha1(hex_hmac_sha1(password, random1))</p>
<p>Login:</p>
<p>   1. Server sends random1 and random2<br />
   2. Client sends hex_hmac_sha1(password, random1) and hex_sha1(hex_hmac_sha1(password, random2))
</p></blockquote>
<p>During registration, the value sent by the client is stored.  During login, the user must present a value <em>that when hashed</em> produces the value provided at registration.  Because of the non-reversibility property of hashes, knowing the value passed during registration does not allow an attacker to login.  The only way to produce the valid response is to know the actual plaintext password, which is never transmitted or known by the server.  In this system, challenges are linked to a user and must be stored, since it must be known what challenge was used to produce the response.</p>
<p>The second random challenge (random2) and the second value sent by the client during login are used to prevent replay attacks.  Upon successful login, the second value provided by the client becomes the next response, equivalent to the value first provided during registration.  Thus, for the next login, the value that is sent must hash to equal this value.  This also means that the challenges are updated/changed each time a login is successful.  This has the unfortunate downside of revealing when a user has logged in, since the challenge presented at login will be different.  (Challenges must be publicly available)</p>
<p>For a more thorough explanation, I suggest that you <a href="http://pajhome.org.uk/crypt/md5/auth.html">read Johnston&#8217;s article on the subject</a>.</p>
<h3>Getting it to work</h3>
<p>Understanding the process above leads to the conclusion that user-login is now a two-stage process.  Since the challenges are tied to a user, the username must first be known to the server in order to retrieve the challenge for that user.  The client can then use the challenge to produce the response to send to the server for authentication.</p>
<p>Having a two-stage login form would be very unfriendly to users.  Thus, the main challenge is to make it <em>appear</em> as if nothing out of the ordinary is happening.  This is where Ajax comes into play.  When the form is submitted, the event is prevented from occurring normally.  Instead, the username is first retrieved from the form and sent to the server via an Ajax call in order to retrieve the associated challenge.  Once the challenge is received, the appropriate responses are computed, inserted into the form and then the form is submitted.</p>
<p>The current system also works in the case that JavaScript is not enabled/available on the client side.  In this case, challenge-response will not be available, since JavaScript is used to compute the responses.  The server-side <acronym class="uttInitialism" title="PHP: Hypertext Preprocessor">PHP</acronym> scripts infer that JavaScript <em>was not</em> enabled on the client-side if proper challenge-responses are not received, and thus treat the password as plaintext.  In this case, passwords are transmitted in plaintext.  With this code, you have the choice of allowing this &#8220;insecure&#8221; login to proceed or not.</p>
<p>Note that the CHAP-PHP is more of a module than a full-fledged system, since it&#8217;s not intended to be used on its own but instead as part of some application.  It might be a bit confusing if you&#8217;re a non-developer, but I&#8217;ve tried to make it as straightforward and simple as possible so that it will be easy to integrate with existing code bases/frameworks/sites. </p>
<p>Please don&#8217;t hesitate to <a href="/contact">contact me</a> with your questions, comments or suggestions.</p>
<h3>Disclaimer and warning</h3>
<p>You should not use this as the basis for authentication for sensitive data/websites.  I am not a security expert.  At this point, this is more of a proof-of-concept then something concrete.  It is intended to be the starting point for perhaps something more secure and to show that there are alternatives for more secure authentication when SSL is not available.</p>
<h4>Revision History</h4>
<ul class="note less">
<li>0.5 - First Public Release - 2008-03-29</li>
<li>0.5.1 - Fixed file-based storage to be more robust - 2008-03-30</li>
</ul>
<hr/>Copyright &copy; 2008 <strong><a href="http://unitstep.net">unitstep.net</a></strong>. This Feed is for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact <strong><a href="mailto:webmaster@unitstep.net">webmaster@unitstep.net</a></strong> for more information.<br/><span style="float: right;font-size: 7pt"><a href="http://blog.taragana.com/index.php/archive/wordpress-plugins-provided-by-taraganacom/">Plugin</a> by <a href="http://www.taragana.com/">Taragana</a></span>]]></content:encoded>
			<wfw:commentRss>http://unitstep.net/blog/2008/03/29/a-challenge-response-ajax-php-login-system/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
