PageRenderTime 203ms CodeModel.GetById 103ms app.highlight 11ms RepoModel.GetById 87ms app.codeStats 0ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 109 lines | 94 code | 3 blank | 12 comment | 0 complexity | 2b3d6860cdf3df457171cff0b325990f MD5 | raw file
  1/**
  2 * CTRModeTest
  3 * 
  4 * A test class for CTRMode
  5 * Copyright (c) 2007 Henri Torgemane
  6 * 
  7 * See LICENSE.txt for full license information.
  8 */
  9package com.hurlant.crypto.tests
 10{
 11	import flash.utils.ByteArray;
 12	import com.hurlant.util.Hex;
 13	import com.hurlant.crypto.symmetric.CTRMode;
 14	import com.hurlant.crypto.symmetric.AESKey;
 15	import com.hurlant.crypto.symmetric.NullPad;
 16	
 17	public class CTRModeTest extends TestCase
 18	{
 19		public function CTRModeTest(h:ITestHarness)
 20		{
 21			super(h, "CTRMode Test");
 22			runTest(testCTR_AES128, "CTR AES-128 Test Vectors");
 23			runTest(testCTR_AES192, "CTR AES-192 Test Vectors");
 24			runTest(testCTR_AES256, "CTR AES-256 Test Vectors");
 25			h.endTestCase();
 26		}
 27		
 28		/**
 29		 * Vectors from http://csrc.nist.gov/CryptoToolkit/modes/800-38_Series_Publications/SP800-38A.pdf
 30		 * Section F.5.1 and below.
 31		 */
 32		public function testCTR_AES128():void {
 33			var key:ByteArray = Hex.toArray("2b7e151628aed2a6abf7158809cf4f3c");
 34			var pt:ByteArray = Hex.toArray(
 35				"6bc1bee22e409f96e93d7e117393172a" +
 36				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 37				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 38				"f69f2445df4f9b17ad2b417be66c3710");
 39			var ct:ByteArray = Hex.toArray(
 40				"874d6191b620e3261bef6864990db6ce" + 
 41				"9806f66b7970fdff8617187bb9fffdff" + 
 42				"5ae4df3edbd5d35e5b4f09020db03eab" + 
 43				"1e031dda2fbe03d1792170a0f3009cee");
 44			var ctr:CTRMode = new CTRMode(new AESKey(key), new NullPad);
 45			ctr.IV = Hex.toArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
 46			var src:ByteArray = new ByteArray;
 47			src.writeBytes(pt);
 48			ctr.encrypt(src);
 49			var hsrc:String = Hex.fromArray(src);
 50			var hct:String = Hex.fromArray(ct);
 51			assert("CTR_AES128 test 1: "+hsrc+" != "+hct, hsrc==hct);
 52			ctr.decrypt(src);
 53			hsrc = Hex.fromArray(src);
 54			var hpt:String = Hex.fromArray(pt);
 55			assert("CTR_AES128 test 2: "+hsrc+" != "+hpt, hsrc==hpt);
 56		}
 57		public function testCTR_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				"1abc932417521ca24f2b0459fe7e6e0b" + 
 66				"090339ec0aa6faefd5ccc2c6f4ce8e94" + 
 67				"1e36b26bd1ebc670d1bd1d665620abf7" + 
 68				"4f78a7f6d29809585a97daec58c6b050");
 69			var ctr:CTRMode = new CTRMode(new AESKey(key), new NullPad);
 70			ctr.IV = Hex.toArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
 71			var src:ByteArray = new ByteArray;
 72			src.writeBytes(pt);
 73			ctr.encrypt(src);
 74			var hsrc:String = Hex.fromArray(src);
 75			var hct:String = Hex.fromArray(ct);
 76			assert("CTR_AES192 test 1: "+hsrc+" != "+hct, hsrc==hct);
 77			ctr.decrypt(src);
 78			hsrc = Hex.fromArray(src);
 79			var hpt:String = Hex.fromArray(pt);
 80			assert("CTR_AES192 test 2: "+hsrc+" != "+hpt, hsrc==hpt);
 81		}
 82		public function testCTR_AES256():void {
 83			var key:ByteArray = Hex.toArray("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
 84			var pt:ByteArray = Hex.toArray(
 85				"6bc1bee22e409f96e93d7e117393172a" +
 86				"ae2d8a571e03ac9c9eb76fac45af8e51" + 
 87				"30c81c46a35ce411e5fbc1191a0a52ef" + 
 88				"f69f2445df4f9b17ad2b417be66c3710");
 89			var ct:ByteArray = Hex.toArray(
 90				"601ec313775789a5b7a7f504bbf3d228" + 
 91				"f443e3ca4d62b59aca84e990cacaf5c5" + 
 92				"2b0930daa23de94ce87017ba2d84988d" + 
 93				"dfc9c58db67aada613c2dd08457941a6");
 94			var ctr:CTRMode = new CTRMode(new AESKey(key), new NullPad);
 95			ctr.IV = Hex.toArray("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
 96			var src:ByteArray = new ByteArray;
 97			src.writeBytes(pt);
 98			ctr.encrypt(src);
 99			var hsrc:String = Hex.fromArray(src);
100			var hct:String = Hex.fromArray(ct);
101			assert("CTR_AES256 test 1: "+hsrc+" != "+hct, hsrc==hct);
102			ctr.decrypt(src);
103			hsrc = Hex.fromArray(src);
104			var hpt:String = Hex.fromArray(pt);
105			assert("CTR_AES256 test 2: "+hsrc+" != "+hpt, hsrc==hpt);
106		}
107		
108	}
109}