PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 1ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 142 lines | 107 code | 10 blank | 25 comment | 4 complexity | 3cfc54c6c87288574d5d2c545d45435a MD5 | raw file
  1/**
  2 * TLSPRF
  3 * 
  4 * An ActionScript 3 implementation of a pseudo-random generator
  5 * that follows the TLS specification
  6 * Copyright (c) 2007 Henri Torgemane
  7 * 
  8 * See LICENSE.txt for full license information.
  9 */
 10package com.hurlant.crypto.prng
 11{
 12	import flash.utils.ByteArray;
 13	import com.hurlant.crypto.hash.HMAC;
 14	import com.hurlant.crypto.hash.MD5;
 15	import com.hurlant.crypto.hash.SHA1;
 16	import com.hurlant.util.Memory;
 17	import com.hurlant.util.Hex;
 18	import flash.utils.IDataOutput;
 19
 20	/**
 21	 * There's "Random", and then there's TLS Random.
 22	 * .
 23	 * Still Pseudo-random, though.
 24	 */
 25	public class TLSPRF
 26	{
 27		// XXX WAY TOO MANY STRUCTURES HERE
 28		
 29		// seed
 30		private var seed:ByteArray;
 31		// P_MD5's secret
 32		private var s1:ByteArray;
 33		// P_SHA-1's secret
 34		private var s2:ByteArray;
 35		// HMAC_MD5's A
 36		private var a1:ByteArray;
 37		// HMAC_SHA1's A
 38		private var a2:ByteArray;
 39		// Pool for P_MD5
 40		private var p1:ByteArray;
 41		// Pool for P_SHA1
 42		private var p2:ByteArray;
 43		// Data for HMAC_MD5
 44		private var d1:ByteArray;
 45		// Data for HMAC_SHA1
 46		private var d2:ByteArray;
 47		
 48		
 49		private var hmac_md5:HMAC;
 50		private var hmac_sha1:HMAC;
 51		
 52		public function TLSPRF(secret:ByteArray, label:String, seed:ByteArray) {
 53			var l:int = Math.ceil(secret.length/2);
 54			var s1:ByteArray = new ByteArray;
 55			var s2:ByteArray = new ByteArray;
 56			s1.writeBytes(secret, 0, l);
 57			s2.writeBytes(secret, secret.length-l, l);
 58			var s:ByteArray = new ByteArray;
 59			s.writeUTFBytes(label);
 60			s.writeBytes(seed);
 61			this.seed = s;
 62			this.s1 = s1;
 63			this.s2 = s2;
 64			hmac_md5 = new HMAC(new MD5);
 65			hmac_sha1 = new HMAC(new SHA1);
 66			
 67			this.a1 = hmac_md5.compute(s1, this.seed);
 68			this.a2 = hmac_sha1.compute(s2, this.seed);
 69			
 70			p1 = new ByteArray;
 71			p2 = new ByteArray;
 72			
 73			d1 = new ByteArray;
 74			d2 = new ByteArray;
 75			d1.position = MD5.HASH_SIZE;
 76			d1.writeBytes(this.seed);
 77			d2.position = SHA1.HASH_SIZE;
 78			d2.writeBytes(this.seed);
 79		}
 80		
 81		// XXX HORRIBLY SLOW. REWRITE.
 82		public function nextBytes(buffer:IDataOutput, length:int):void {
 83			while (length--) {
 84				buffer.writeByte(nextByte());
 85			}
 86		}
 87		public function nextByte():int {
 88			if (p1.bytesAvailable==0) {
 89				more_md5();
 90			}
 91			if (p2.bytesAvailable==0) {
 92				more_sha1();
 93			}
 94			return p1.readUnsignedByte()^p2.readUnsignedByte();
 95		}
 96		public function dispose():void {
 97			seed = dba(seed);
 98			s1 = dba(s1);
 99			s2 = dba(s2);
100			a1 = dba(a1);
101			a2 = dba(a2);
102			p1 = dba(p1);
103			p2 = dba(p2);
104			d1 = dba(d1);
105			d2 = dba(d2);
106			hmac_md5.dispose();
107			hmac_md5 = null;
108			hmac_sha1.dispose();
109			hmac_sha1 = null;
110			Memory.gc();
111		}
112		public function toString():String {
113			return "tls-prf";
114		}
115		private function dba(ba:ByteArray):ByteArray {
116			for (var i:uint=0;i<ba.length;i++) {
117				ba[i]=0;
118			}
119			ba.length=0;
120			return null;
121		}
122		private function more_md5():void {
123			d1.position=0;
124			d1.writeBytes(a1);
125			var p:int = p1.position;
126			var more:ByteArray = hmac_md5.compute(s1, d1);
127			a1 = hmac_md5.compute(s1, a1);
128			p1.writeBytes(more);
129			p1.position=p;
130		}
131		private function more_sha1():void {
132			d2.position=0;
133			d2.writeBytes(a2);
134			var p:int = p2.position;
135			var more:ByteArray = hmac_sha1.compute(s2, d2);
136			a2 = hmac_sha1.compute(s2, a2);
137			p2.writeBytes(more);
138			p2.position=p;
139		}
140		
141	}
142}