PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 15ms RepoModel.GetById 25ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/crypto/tests/ECBModeTest.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 151 lines | 129 code | 4 blank | 18 comment | 2 complexity | a1a22fd3e04f758939f99d2afe3f9634 MD5 | raw file
  1/**
  2 * ECBModeTest
  3 * 
  4 * A test class for ECBMode
  5 * Copyright (c) 2007 Henri Torgemane
  6 * 
  7 * See LICENSE.txt for full license information.
  8 */
  9package com.hurlant.crypto.tests
 10{
 11	import com.hurlant.crypto.symmetric.AESKey;
 12	import com.hurlant.crypto.symmetric.ECBMode;
 13	import com.hurlant.crypto.symmetric.NullPad;
 14	import com.hurlant.crypto.symmetric.XTeaKey;
 15	import com.hurlant.util.Hex;
 16	
 17	import flash.utils.ByteArray;
 18	
 19	public class ECBModeTest extends TestCase
 20	{
 21		public function ECBModeTest(h:ITestHarness) {
 22			super(h, "ECBMode Test");
 23			runTest(testAES,"ECB AES Test Vectors");
 24			runTest(testXTea,"ECB XTea Test Vectors");
 25			runTest(testECB_AES128,"ECB AES-128 Test Vectors");
 26			runTest(testECB_AES192,"ECB AES-192 Test Vectors");
 27			runTest(testECB_AES256,"ECB AES-256 Test Vectors");
 28			h.endTestCase();
 29		}
 30		/**
 31		 * For now the main goal is to show we can decrypt what we encrypt in this mode.
 32		 * Eventually, this should get correlated with some well known vectors.
 33		 * yay. found hawt test vectors: http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
 34		 */
 35		public function testECB_AES128():void {
 36			var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c");
 37			var pt:ByteArray = Hex.toArray(
 38				"6bc1bee22e409f96e93d7e117393172a" + 
 39				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 40				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 41				"f69f2445df4f9b17ad2b417be66c3710");
 42			var ct:ByteArray = Hex.toArray(
 43				"3ad77bb40d7a3660a89ecaf32466ef97" + 
 44				"f5d3d58503b9699de785895a96fdbaaf" + 
 45				"43b1cd7f598ece23881b00e3ed030688" + 
 46				"7b0c785e27e8ad3f8223207104725dd4");
 47			var ecb:ECBMode = new ECBMode(new AESKey(key), new NullPad);
 48			var src:ByteArray = new ByteArray;
 49			src.writeBytes(pt);
 50			ecb.encrypt(src);
 51			assert("ECB_AES128 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 52			ecb.decrypt(src);
 53			assert("ECB_AES128 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 54		}
 55		public function testECB_AES192():void {
 56			var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
 57			var pt:ByteArray = Hex.toArray(
 58				"6bc1bee22e409f96e93d7e117393172a" + 
 59				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 60				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 61				"f69f2445df4f9b17ad2b417be66c3710");
 62			var ct:ByteArray = Hex.toArray(
 63				"bd334f1d6e45f25ff712a214571fa5cc" + 
 64				"974104846d0ad3ad7734ecb3ecee4eef" + 
 65				"ef7afd2270e2e60adce0ba2face6444e" + 
 66				"9a4b41ba738d6c72fb16691603c18e0e");
 67			var ecb:ECBMode = new ECBMode(new AESKey(key), new NullPad);
 68			var src:ByteArray = new ByteArray;
 69			src.writeBytes(pt);
 70			ecb.encrypt(src);
 71			assert("ECB_AES192 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 72			ecb.decrypt(src);
 73			assert("ECB_AES192 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 74		}
 75		public function testECB_AES256():void {
 76			var key:ByteArray = Hex.toArray(
 77					"603deb1015ca71be2b73aef0857d7781" + 
 78					"1f352c073b6108d72d9810a30914dff4");
 79			var pt:ByteArray = Hex.toArray(
 80				"6bc1bee22e409f96e93d7e117393172a" + 
 81				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 82				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 83				"f69f2445df4f9b17ad2b417be66c3710");
 84			var ct:ByteArray = Hex.toArray(
 85				"f3eed1bdb5d2a03c064b5a7e3db181f8" + 
 86				"591ccb10d410ed26dc5ba74a31362870" + 
 87				"b6ed21b99ca6f4f9f153e7b1beafed1d" + 
 88				"23304b7a39f9f3ff067d8d8f9e24ecc7");
 89			var ecb:ECBMode = new ECBMode(new AESKey(key), new NullPad);
 90			var src:ByteArray = new ByteArray;
 91			src.writeBytes(pt);
 92			ecb.encrypt(src);
 93			assert("ECB_AES256 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 94			ecb.decrypt(src);
 95			assert("ECB_AES256 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 96		}
 97		// crappier, older testing. keeping around for no good reason.
 98		public function testAES():void {
 99			var keys:Array = [
100			"00010203050607080A0B0C0D0F101112",
101			"14151617191A1B1C1E1F202123242526"];
102			var pts:Array = [
103			"506812A45F08C889B97F5980038B8359506812A45F08C889B97F5980038B8359506812A45F08C889B97F5980038B8359",
104			"5C6D71CA30DE8B8B00549984D2EC7D4B5C6D71CA30DE8B8B00549984D2EC7D4B5C6D71CA30DE8B8B00549984D2EC7D4B"];
105			var cts:Array = [
106			"D8F532538289EF7D06B506A4FD5BE9C9D8F532538289EF7D06B506A4FD5BE9C9D8F532538289EF7D06B506A4FD5BE9C96DE5F607AB7EB8202F3957703B04E8B5",
107			"59AB30F4D4EE6E4FF9907EF65B1FB68C59AB30F4D4EE6E4FF9907EF65B1FB68C59AB30F4D4EE6E4FF9907EF65B1FB68C2993487785CB1CFDA6BB4F0F345F76C7"];
108
109			for (var i:uint=0;i<keys.length;i++) {
110				var key:ByteArray = Hex.toArray(keys[i]);
111				var pt:ByteArray = Hex.toArray(pts[i]);
112				var aes:AESKey = new AESKey(key);
113				var ecb:ECBMode = new ECBMode(aes);
114				ecb.encrypt(pt);
115				var str:String = Hex.fromArray(pt).toUpperCase();
116				assert("comparing "+cts[i]+" to "+str, cts[i]==str);
117				// back to pt
118				ecb.decrypt(pt);
119				str = Hex.fromArray(pt).toUpperCase();
120				assert("comparing "+pts[i]+" to "+str, pts[i]==str);
121			}
122		}
123		public function testXTea():void {
124			var keys:Array=[
125			"00000000000000000000000000000000",
126			"2b02056806144976775d0e266c287843"];
127			var pts:Array=[
128			"0000000000000000000000000000000000000000000000000000000000000000",
129			"74657374206d652e74657374206d652e74657374206d652e74657374206d652e"];
130			var cts:Array=[
131			"2dc7e8d3695b05382dc7e8d3695b05382dc7e8d3695b05382dc7e8d3695b053820578a874233632d",
132			"79095821381987837909582138198783790958213819878379095821381987830e4dc3c48b2edf32"];
133			// self-fullfilling vectors.
134			// oh well, at least I can decrypt what I produce. :(
135			
136			for (var i:uint=0;i<keys.length;i++) {
137				var key:ByteArray = Hex.toArray(keys[i]);
138				var pt:ByteArray = Hex.toArray(pts[i]);
139				var tea:XTeaKey = new XTeaKey(key);
140				var ecb:ECBMode = new ECBMode(tea);
141				ecb.encrypt(pt);
142				var str:String = Hex.fromArray(pt);
143				assert("comparing "+cts[i]+" to "+str, cts[i]==str);
144				// now go back to plaintext.
145				ecb.decrypt(pt);
146				str = Hex.fromArray(pt);
147				assert("comparing "+pts[i]+" to "+str, pts[i]==str);
148			}
149		}
150	}
151}