PageRenderTime 94ms CodeModel.GetById 51ms app.highlight 15ms RepoModel.GetById 25ms app.codeStats 1ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 160 lines | 134 code | 7 blank | 19 comment | 2 complexity | 5860eb32eb3298467fa301f2bea68796 MD5 | raw file
  1/**
  2 * CBCModeTest
  3 * 
  4 * A test class for CBCMode
  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.CBCMode;
 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 CBCModeTest extends TestCase
 20	{
 21		public function CBCModeTest(h:ITestHarness) {
 22			super(h, "CBCMode Test");
 23			runTest(testAES,"CBC AES Test Vectors");
 24			runTest(testXTea,"CBC XTea Test Vectors");
 25			//
 26			runTest(testCBC_AES128, "CBC AES-128 Test Vectors");
 27			runTest(testCBC_AES192, "CBC AES-192 Test Vectors");
 28			runTest(testCBC_AES256, "CBC AES-256 Test Vectors");
 29			h.endTestCase();
 30		}
 31		
 32		/**
 33		 * Hawt NIST Vectors: http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
 34		 * Section F.2.1 and below.
 35		 */
 36		public function testCBC_AES128():void {
 37			var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c");
 38			var pt:ByteArray = Hex.toArray(
 39				"6bc1bee22e409f96e93d7e117393172a" + 
 40				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 41				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 42				"f69f2445df4f9b17ad2b417be66c3710");
 43			var ct:ByteArray = Hex.toArray(
 44				"7649abac8119b246cee98e9b12e9197d" + 
 45				"5086cb9b507219ee95db113a917678b2" + 
 46				"73bed6b8e3c1743b7116e69e22229516" + 
 47				"3ff1caa1681fac09120eca307586e1a7");
 48			var cbc:CBCMode = new CBCMode(new AESKey(key), new NullPad);
 49			cbc.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
 50			var src:ByteArray = new ByteArray;
 51			src.writeBytes(pt);
 52			cbc.encrypt(src);
 53			assert("CBC_AES128 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 54			cbc.decrypt(src);
 55			assert("CBC_AES128 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 56		}
 57		public function testCBC_AES192():void {
 58			var key:ByteArray = Hex.toArray("8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b");
 59			var pt:ByteArray = Hex.toArray(
 60				"6bc1bee22e409f96e93d7e117393172a" + 
 61				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 62				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 63				"f69f2445df4f9b17ad2b417be66c3710");
 64			var ct:ByteArray = Hex.toArray(
 65				"4f021db243bc633d7178183a9fa071e8" + 
 66				"b4d9ada9ad7dedf4e5e738763f69145a" + 
 67				"571b242012fb7ae07fa9baac3df102e0" + 
 68				"08b0e27988598881d920a9e64f5615cd");
 69			var cbc:CBCMode = new CBCMode(new AESKey(key), new NullPad);
 70			cbc.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
 71			var src:ByteArray = new ByteArray;
 72			src.writeBytes(pt);
 73			cbc.encrypt(src);
 74			assert("CBC_AES192 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 75			cbc.decrypt(src);
 76			assert("CBC_AES192 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
 77		}
 78		public function testCBC_AES256():void {
 79			var key:ByteArray = Hex.toArray(
 80				"603deb1015ca71be2b73aef0857d7781" + 
 81				"1f352c073b6108d72d9810a30914dff4");
 82			var pt:ByteArray = Hex.toArray(
 83				"6bc1bee22e409f96e93d7e117393172a" + 
 84				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 85				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 86				"f69f2445df4f9b17ad2b417be66c3710");
 87			var ct:ByteArray = Hex.toArray(
 88				"f58c4c04d6e5f1ba779eabfb5f7bfbd6" + 
 89				"9cfc4e967edb808d679f777bc6702c7d" + 
 90				"39f23369a9d9bacfa530e26304231461" + 
 91				"b2eb05e2c39be9fcda6c19078c6a9d1b");
 92			var cbc:CBCMode = new CBCMode(new AESKey(key), new NullPad);
 93			cbc.IV = Hex.toArray("000102030405060708090a0b0c0d0e0f");
 94			var src:ByteArray = new ByteArray;
 95			src.writeBytes(pt);
 96			cbc.encrypt(src);
 97			assert("CBC_AES256 test 1", Hex.fromArray(src)==Hex.fromArray(ct));
 98			cbc.decrypt(src);
 99			assert("CBC_AES256 test 2", Hex.fromArray(src)==Hex.fromArray(pt));
100		}
101		
102		
103		/**
104		 * For now the main goal is to show we can decrypt what we encrypt in this mode.
105		 * Eventually, this should get correlated with some well known vectors.
106		 */
107		public function testAES():void {
108			var keys:Array = [
109			"00010203050607080A0B0C0D0F101112",
110			"14151617191A1B1C1E1F202123242526"];
111			var cts:Array = [
112			"D8F532538289EF7D06B506A4FD5BE9C94894C5508A8D8E29AB600DB0261F0555A8FA287B89E65C0973F1F8283E70C72863FE1C8F1F782084CE05626E961A67B3",
113			"59AB30F4D4EE6E4FF9907EF65B1FB68C96890CE217689B1BE0C93ED51CF21BB5A0101A8C30714EC4F52DBC9C6F4126067D363F67ABE58463005E679B68F0B496"];
114			var pts:Array = [
115			"506812A45F08C889B97F5980038B8359506812A45F08C889B97F5980038B8359506812A45F08C889B97F5980038B8359",
116			"5C6D71CA30DE8B8B00549984D2EC7D4B5C6D71CA30DE8B8B00549984D2EC7D4B5C6D71CA30DE8B8B00549984D2EC7D4B"];
117
118			for (var i:uint=0;i<keys.length;i++) {
119				var key:ByteArray = Hex.toArray(keys[i]);
120				var pt:ByteArray = Hex.toArray(pts[i]);
121				var aes:AESKey = new AESKey(key);
122				var cbc:CBCMode = new CBCMode(aes);
123				cbc.IV = Hex.toArray("00000000000000000000000000000000");
124				cbc.encrypt(pt);
125				var str:String = Hex.fromArray(pt).toUpperCase();
126				assert("comparing "+cts[i]+" to "+str, cts[i]==str);
127				// back to pt
128				cbc.decrypt(pt);
129				str = Hex.fromArray(pt).toUpperCase();
130				assert("comparing "+pts[i]+" to "+str, pts[i]==str);
131			}
132		}
133		public function testXTea():void {
134			var keys:Array=[
135			"00000000000000000000000000000000",
136			"2b02056806144976775d0e266c287843"];
137			var cts:Array = [
138			"2dc7e8d3695b0538d8f1640d46dca717790af2ab545e11f3b08e798eb3f17b1744299d4d20b534aa",
139			"790958213819878370eb8251ffdac371081c5a457fc42502c63910306fea150be8674c3b8e675516"];
140			var pts:Array=[
141			"0000000000000000000000000000000000000000000000000000000000000000",
142			"74657374206d652e74657374206d652e74657374206d652e74657374206d652e"];
143			
144			for (var i:uint=0;i<keys.length;i++) {
145				var key:ByteArray = Hex.toArray(keys[i]);
146				var pt:ByteArray = Hex.toArray(pts[i]);
147				var tea:XTeaKey = new XTeaKey(key);
148				var cbc:CBCMode = new CBCMode(tea);
149				cbc.IV = Hex.toArray("0000000000000000");
150				cbc.encrypt(pt);
151				var str:String = Hex.fromArray(pt);
152				assert("comparing "+cts[i]+" to "+str, cts[i]==str);
153				// now go back to plaintext.
154				cbc.decrypt(pt);
155				str = Hex.fromArray(pt);
156				assert("comparing "+pts[i]+" to "+str, pts[i]==str);
157			}
158		}
159	}
160}