http://code.google.com/p/caffeine-hx/source/detail?r=660
Added:
/trunk/ext3/chx/crypt/Cipher.hx
/trunk/ext3/chx/crypt/CipherDirection.hx
/trunk/ext3/chx/crypt/CipherParams.hx
/trunk/ext3/chx/crypt/mode
/trunk/ext3/chx/crypt/mode/CBC.hx
/trunk/ext3/chx/crypt/mode/CFB8.hx
/trunk/ext3/chx/crypt/mode/CTR.hx
/trunk/ext3/chx/crypt/mode/ECB.hx
/trunk/ext3/chx/crypt/mode/IVBase.hx
/trunk/ext3/chx/crypt/mode/ModeBase.hx
/trunk/ext3/chx/crypt/mode/OFB.hx
/trunk/ext3/chx/crypt/padding
/trunk/ext3/chx/crypt/padding/PadBase.hx
/trunk/ext3/chx/crypt/padding/PadNone.hx
/trunk/ext3/chx/crypt/padding/PadNull.hx
/trunk/ext3/chx/crypt/padding/PadPkcs5.hx
/trunk/ext3/chx/crypt/padding/PadSSL.hx
/trunk/ext3/chx/crypt/padding/PadTLS.hx
/trunk/ext3/chx/crypt/symmetric
/trunk/ext3/chx/crypt/symmetric/Aes.hx
/trunk/ext3/chx/crypt/symmetric/Des.hx
/trunk/ext3/chx/crypt/symmetric/TripleDes.hx
/trunk/ext3/chx/crypt/symmetric/XXTea.hx
Deleted:
/trunk/ext3/chx/crypt/Aes.hx
/trunk/ext3/chx/crypt/Des.hx
/trunk/ext3/chx/crypt/IV.hx
/trunk/ext3/chx/crypt/ModeCBC.hx
/trunk/ext3/chx/crypt/ModeECB.hx
/trunk/ext3/chx/crypt/PadBase.hx
/trunk/ext3/chx/crypt/PadNone.hx
/trunk/ext3/chx/crypt/PadNull.hx
/trunk/ext3/chx/crypt/PadPkcs5.hx
/trunk/ext3/chx/crypt/PadSSL.hx
/trunk/ext3/chx/crypt/PadTLS.hx
/trunk/ext3/chx/crypt/TripleDes.hx
/trunk/ext3/chx/crypt/XXTea.hx
Modified:
/trunk/ext3/chx/crypt/IBlockCipher.hx
/trunk/ext3/chx/crypt/IMode.hx
/trunk/ext3/chx/crypt/rsa/IBlockPad.hx
/trunk/ext3/chx/crypt/rsa/PadBlockBase.hx
/trunk/ext3/chx/crypt/rsa/PadPkcs1Type1.hx
/trunk/ext3/chx/crypt/rsa/PadPkcs1Type2.hx
/trunk/ext3/chx/crypt/rsa/RSA.hx
/trunk/ext3/chx/crypt/rsa/RSAEncrypt.hx
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/Cipher.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt;
+import chx.crypt.padding.PadPkcs5;
+import chx.io.Output;
+
+/**
+ * To encrypt or decrypt in multiple steps, use the update followed by the
final
+ * method. To encrypt or decrypt in a single step, the 'final' method can
be used
+ * without a preceding 'update'.
+ **/
+class Cipher {
+ public var params(default,null) : CipherParams;
+ var direction : CipherDirection;
+ var algo : IBlockCipher;
+ var mode : IMode;
+ var pad : IPad;
+ var buf : Bytes;
+ var ptr : Int;
+ var blockSize : Int;
+
+ var modeUpdate : Bytes->Output->Int;
+ var modeFinal : Bytes->Output->Int;
+
+ /**
+ * Create a cipher from a decryption algorithm, a mode and a padding
method.
+ **/
+ public function new(algo:IBlockCipher, mode:IMode, pad:IPad) {
+
+ this.algo = algo;
+ this.mode = mode;
+ this.pad = pad;
+
+ if(pad == null)
+ this.pad = new PadPkcs5();
+ else
+ this.pad = pad;
+ }
+
+ /**
+ * Initialize the Cipher for encryption or decryption.
+ * @param direction For encrypt or decrypt, overrides direction setting
in params
+ * @param params
+ **/
+ public function init(direction:CipherDirection, params :
CipherParams=null) : Void {
+ this.direction = direction;
+ switch(direction) {
+ case ENCRYPT:
+ modeUpdate = mode.updateEncrypt;
+ modeFinal = mode.finalEncrypt;
+ case DECRYPT:
+ modeUpdate = mode.updateDecrypt;
+ modeFinal = mode.finalDecrypt;
+ }
+ if(params == null)
+ this.params = new CipherParams();
+ else
+ this.params = params.clone();
+ this.params.direction = direction;
+
+ mode.cipher = algo;
+ mode.padding = pad;
+
+ // streaming modes will have blocksizes less than that of the
+ // underlying crypt
+ this.blockSize = mode.blockSize;
+ buf = Bytes.alloc(this.blockSize);
+ ptr = 0;
+
+ //algo.init(params);
+ mode.init(params);
+ //pad.init();
+ }
+
+ /**
+ * Update the cipher with any number of bytes.
+ * @param input Bytes object with bytes to encrypt or decrypt
+ * @param inputOffset Offset into 'input' to read from
+ * @param inputLen Number of bytes to read from 'input'
+ * @param out An Output stream of any kind
+ **/
+ public function update(input:Bytes, inputOffset:Int, inputLen:Int,
out:Output) : Int {
+ if(inputLen <= 0)
+ return 0;
+ var rv = 0;
+ while(true) {
+ var num = Std.int(Math.min(blockSize-ptr, inputLen - rv));
+ if(num <= 0) break;
+ for(i in 0...num) {
+ Assert.isTrue(ptr + i < blockSize);
+ buf.set(i+ptr, input.get(i + inputOffset));
+ }
+ inputOffset += num;
+ ptr += num;
+ Assert.isTrue(ptr <= blockSize);
+ if(ptr == blockSize) {
+ var written = modeUpdate(buf, out);
+ Assert.isTrue(written == blockSize);
+ ptr = 0;
+ }
+ rv += num;
+ }
+ return rv;
+ }
+
+ /**
+ * Update and finalize the cipher with any number of bytes.
+ * @param input Bytes object with bytes to encrypt or decrypt
+ * @param inputOffset Offset into 'input' to read from
+ * @param inputLen Number of bytes to read from 'input'
+ * @param out An Output stream of any kind
+ **/
+ public function final(input:Bytes, inputOffset:Int, inputLen:Int,
out:Output) : Int {
+ var rv : Int = 0;
+ var read : Int = 1;
+ while(read > 0) {
+ read = update(input,inputOffset,inputLen,out);
+ rv += read;
+ inputOffset += read;
+ inputLen -= read;
+ }
+ var rem : Bytes = buf.sub(0,ptr);
+ rv += modeFinal(rem, out);
+ return rv;
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/CipherDirection.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt;
+
+enum CipherDirection {
+ ENCRYPT;
+ DECRYPT;
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/CipherParams.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt;
+
+class CipherParams {
+ /** the starting iv **/
+ public var iv : Bytes;
+ /** random generator used to generate IVs **/
+ public var prng : math.prng.Random;
+ /** Does not have to be set manually, is set by Cipher **/
+ public var direction : CipherDirection;
+
+ public function new() {
+ prng = new math.prng.Random();
+ }
+
+ public function clone() : CipherParams {
+ var o = new CipherParams();
+ if(iv != null)
+ o.iv = iv.sub(0,iv.length);
+ o.prng = this.prng;
+ o.direction = this.direction;
+ return o;
+ }
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/CBC.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.mode;
+
+import chx.io.BytesOutput;
+import chx.io.Output;
+
+/**
+ * Cipher Block Chaining mode
+ **/
+class CBC extends IVBase, implements chx.crypt.IMode {
+
+ public function toString() {
+ return "cbc";
+ }
+
+ override public function updateEncrypt( b : Bytes, out : Output) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateEncrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Plaintext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length != n)
+ return 0;
+ for(i in 0...n)
+ b.set(i, b.get(i) ^ iv.get(i));
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + b.toHex());
+ #end
+ var crypted = cipher.encryptBlock(b);
+ out.writeBytes(crypted,0,n);
+ iv = crypted;
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ trace("Output Block: " + db.toHex());
+ trace("Ciphertext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+
+ return n;
+ }
+
+ override public function updateDecrypt( b : Bytes, out : Output ) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateDecrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Plaintext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length != n)
+ return 0;
+ var tmp = Bytes.alloc(n);
+ tmp.blit(0, b, 0, n);
+ var tb = cipher.decryptBlock(b);
+ for(i in 0...cipher.blockSize)
+ tb.set(i, tb.get(i) ^ iv.get(i));
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + b.toHex());
+ #end
+ out.writeBytes(tb, 0, n);
+ iv = tmp;
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ trace("Output Block: " + db.toHex());
+ trace("Ciphertext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+
+ return n;
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/CFB8.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.mode;
+
+import chx.io.BytesOutput;
+import chx.io.Output;
+
+/**
+ * CFB8 mode - a 1 byte block streaming mode
+ **/
+class CFB8 extends IVBase, implements chx.crypt.IMode {
+
+ public function toString() {
+ return "cfb8";
+ }
+
+ override function getBlockSize() : Int {
+ return 1;
+ }
+
+ override public function updateEncrypt( b : Bytes, out : Output) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateEncrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Plaintext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length == 0)
+ return 0;
+
+ for(i in 0...b.length) {
+ var val = b.get(i);
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + b.toHex());
+ #end
+ var tmp = iv.sub(0, n);
+ iv = cipher.encryptBlock(iv);
+ #if CAFFEINE_DEBUG
+ trace("Output Block: " + iv.toHex());
+ #end
+ val = val ^ iv.get(0);
+
+ for(j in 0...n-1)
+ iv.set(j, tmp.get(j+1));
+ iv.set(n-1, val);
+
+ out.writeByte(val);
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ trace("Ciphertext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+ }
+
+ return b.length;
+ }
+
+ override public function updateDecrypt( b : Bytes, out : Output ) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateDecrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Ciphertext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length == 0)
+ return 0;
+
+ for(i in 0...b.length) {
+ var val = b.get(i);
+ //var orig = val;
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + iv.toHex());
+ #end
+ var tmp = iv.sub(0, n);
+ iv = cipher.encryptBlock(iv);
+ val = val ^ iv.get(0);
+ #if CAFFEINE_DEBUG
+ trace("Output Block: " + iv.toHex());
+ #end
+
+ for(j in 0...n-1)
+ iv.set(j, tmp.get(j+1));
+ iv.set(n-1, b.get(i));
+
+ out.writeByte(val);
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ //trace("Output Block: " + db.toHex());
+ trace("Plaintext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+ }
+
+ return b.length;
+ }
+
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/CTR.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.mode;
+
+import chx.io.BytesOutput;
+import chx.io.Output;
+
+/**
+ * CTR mode
+ **/
+class CTR extends IVBase, implements chx.crypt.IMode {
+ var acc : Bytes;
+
+ public function toString() {
+ return "ctr";
+ }
+
+ override public function init(params : CipherParams) : Void {
+ super.init(params);
+ acc = iv.sub(0, blockSize);
+ }
+
+ override public function updateEncrypt( b : Bytes, out : Output) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateEncrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Plaintext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = blockSize;
+ if(b.length != n)
+ return 0;
+ common(b, out);
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ trace("Output Block: " + db.toHex());
+ trace("Ciphertext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+
+ return n;
+ }
+
+ override public function updateDecrypt( b : Bytes, out : Output ) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateDecrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Plaintext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = blockSize;
+ if(b.length != n)
+ return 0;
+ common(b, out);
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ trace("Output Block: " + db.toHex());
+ trace("Ciphertext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+
+ return n;
+ }
+
+ private function common(b:Bytes, out:Output) : Int {
+ var n = blockSize;
+ if(b.length != n)
+ return 0;
+
+ var e : Bytes = cipher.encryptBlock(acc.sub(0, blockSize));
+
+ for(i in 0...n)
+ b.set(i, b.get(i) ^ e.get(i));
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + b.toHex());
+ #end
+ var i = n-1;
+ while(i>=0) {
+ acc.set(i, acc.get(i) + 1);
+ if(acc.get(i) != 0)
+ break;
+ i--;
+ }
+ return n;
+ }
+
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/ECB.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.mode;
+
+class ECB extends ModeBase, implements chx.crypt.IMode {
+
+ public function toString() {
+ return "ecb";
+ }
+
+ override public function updateEncrypt( b : Bytes, out : Output) : Int {
+ int n = blockSize;
+ if(b.length != n)
+ return 0;
+ var enc = cipher.encryptBlock(b);
+ Assert.isEqual(n, enc.length);
+ out.writeBytes(enc, 0, n);
+ return n;
+ }
+
+ override public function updateDecrypt( b : Bytes, out : Output ) : Int {
+ var n = blockSize;
+ if(b.length != n)
+ return 0;
+ var dec = cipher.decryptBlock(b);
+ Assert.isEqual(n, dec.length);
+ out.writeBytes(dec, 0, n);
+ return n;
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/IVBase.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 20082012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.mode;
+
+import chx.crypt.CipherDirection;
+import math.prng.IPrng;
+
+/**
+* IV is an abstract base class.
+**/
+class IVBase extends ModeBase {
+ /**
+ * Beware that this value changes with each crypt operation.
+ * For the original value, consult params.iv
+ **/
+ public var iv(getIV, setIV) : Bytes;
+ var currentIV : Bytes;
+
+ override public function init(params : CipherParams) : Void {
+ super.init(params);
+ if(params.prng == null)
+ params.prng = new math.prng.Random();
+
+ if(params.iv == null) {
+ if(params.direction == DECRYPT)
+ throw "IV must be set before decryption";
+ var sb = new BytesBuffer();
+ for(x in 0...cipher.blockSize)
+ sb.addByte(params.prng.next());
+ params.iv = sb.getBytes();
+ }
+ currentIV = params.iv.sub(0);
+ }
+
+ public function getIV() : Bytes {
+ return currentIV;
+ }
+
+ public function setIV( s : Bytes ) : Bytes {
+ // here we use cipher.blockSize, as it may be different
+ // than out mode blockSize
+ if(s.length % cipher.blockSize != 0 || s.length == 0)
+ throw("crypt.iv: invalid length. Expected "+cipher.blockSize+ "
bytes.");
+ for(i in 0...cipher.blockSize)
+ currentIV.set(i, s.get(i));
+ return s;
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/ModeBase.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.mode;
+
+import chx.crypt.CipherParams;
+import chx.crypt.IBlockCipher;
+import chx.crypt.IPad;
+import chx.crypt.padding.PadPkcs5;
+import chx.io.BytesOutput;
+import chx.io.Output;
+
+/**
+ * Abstract base class for crypto modes. Default mode is PKCS5
+ **/
+class ModeBase implements IMode {
+ public var cipher(default, setCipher) : IBlockCipher;
+ public var padding(default,setPadding) : IPad;
+ public var blockSize(getBlockSize,never) : Int;
+
+ var params : CipherParams;
+
+ public function new() {
+ padding = new PadPkcs5();
+ }
+
+ public function updateEncrypt( b : Bytes, out : Output) : Int {
+ throw new chx.lang.FatalException("not implemented");
+ return 0;
+ }
+
+ public function updateDecrypt( b : Bytes, out : Output ) : Int {
+ throw new chx.lang.FatalException("not implemented");
+ return 0;
+ }
+
+ // true except for streaming modes, which should override this
+ function getBlockSize() : Int {
+ return cipher.blockSize;
+ }
+
+ function setCipher(v:IBlockCipher) {
+ this.cipher = v;
+ if(padding != null)
+ padding.blockSize = cipher.blockSize;
+ return v;
+ }
+
+ function setPadding(v:IPad) {
+ this.padding = v;
+ if(this.cipher != null)
+ this.padding.blockSize = this.cipher.blockSize;
+ return v;
+ }
+
+ public function init(params : CipherParams) : Void {
+ this.params = params;
+ }
+
+ public function finalEncrypt( b : Bytes, out : Output) : Int {
+ var n = blockSize;
+ var buf = padding.pad(b);
+ Assert.isEqual(0, buf.length % n);
+
+ var ptr = 0;
+ var rv = 0;
+ while(ptr < buf.length) {
+ n = updateEncrypt(buf.sub(ptr,n), out);
+ ptr += n;
+ rv += n;
+ if(n == 0)
+ throw "error";
+ }
+ return rv;
+ }
+
+ public function finalDecrypt( b : Bytes, out : Output ) : Int {
+ var n = blockSize;
+ Assert.isTrue(b.length % n == 0);
+ var bo = new BytesOutput();
+ var ptr = 0;
+ var rv = 0;
+ while(ptr < b.length) {
+ n = updateDecrypt(b.sub(ptr,n), bo);
+ ptr += n;
+ rv += n;
+ if(n == 0)
+ throw "error";
+ }
+ var u = padding.unpad(bo.getBytes());
+ out.writeBytes(u, 0, u.length);
+ return rv;
+ }
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/OFB.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.mode;
+
+import chx.io.BytesOutput;
+import chx.io.Output;
+
+/**
+ * Output Feedback mode
+ **/
+class OFB extends IVBase, implements chx.crypt.IMode {
+
+ public function toString() {
+ return "ofb";
+ }
+
+ override public function updateEncrypt( b : Bytes, out : Output) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateEncrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Plaintext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length != n)
+ return 0;
+ common(b, out);
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ trace("Output Block: " + db.toHex());
+ trace("Ciphertext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+
+ return n;
+ }
+
+ override public function updateDecrypt( b : Bytes, out : Output ) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateDecrypt: ");
+ trace("IV " + iv.toHex());
+ trace("Plaintext: " + b.toHex());
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length != n)
+ return 0;
+ common(b, out);
+
+ #if CAFFEINE_DEBUG
+ var db : Bytes = untyped out.getBytes();
+ out = orig;
+ trace("Output Block: " + db.toHex());
+ trace("Ciphertext: " + db.toHex());
+ trace("");
+ out.writeBytes(db,0,db.length);
+ #end
+
+ return n;
+ }
+
+ private function common(b:Bytes, out:Output) : Int {
+ var n = cipher.blockSize;
+ if(b.length != n)
+ return 0;
+ iv = cipher.encryptBlock(iv);
+
+ for(i in 0...n)
+ b.set(i, b.get(i) ^ iv.get(i));
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + b.toHex());
+ #end
+ out.writeBytes(b, 0, n);
+ return n;
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/padding/PadBase.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2011, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.padding;
+
+class PadBase implements IPad {
+
+ public var blockSize(default,setBlockSize) : Int;
+
+ public function new( blockSize : Null<Int> = null ) {
+ if(blockSize != null)
+ setBlockSize(blockSize);
+ }
+
+ public function pad( s : Bytes ) : Bytes {
+ return throw new chx.lang.FatalException("not implemented");
+ }
+
+ public function unpad( s : Bytes ) : Bytes {
+ return throw new chx.lang.FatalException("not implemented");
+ }
+
+ function setBlockSize(len : Int) : Int {
+ blockSize = len;
+ return len;
+ }
+
+ public function calcNumBlocks(len : Int) : Int {
+ if(len == 0) return 0;
+ var n : Int = Math.ceil(len/blockSize);
+ // most pads will require an extra block if the input length
+ // is an exact multiple of the block size
+ if(len % blockSize == 0)
+ n++;
+ return n;
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/padding/PadNone.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2012, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.padding;
+
+/**
+ * Perform no padding.
+ **/
+class PadNone extends PadBase, implements IPad {
+
+ override public function pad( s : Bytes ) : Bytes {
+ return s;
+ }
+
+ override public function unpad( s : Bytes ) : Bytes {
+ return s;
+ }
+
+ override public function calcNumBlocks(len : Int) : Int {
+ return Math.ceil(len/blockSize);
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/padding/PadNull.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2008, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.padding;
+
+/**
+ * Pads with NULL (0) bytes
+ **/
+class PadNull implements IPad {
+ public var blockSize(default,setBlockSize) : Int;
+ public var textSize(default,null) : Int;
+
+ public function new( blockSize : Null<Int> = null ) {
+ if(blockSize != null)
+ setBlockSize(blockSize);
+ }
+
+ public function pad( s : Bytes ) : Bytes {
+ var r = blockSize - (s.length % blockSize);
+ if(r == blockSize)
+ return s;
+ var sb = new BytesBuffer();
+ sb.add(s);
+ for(x in 0...r) {
+ sb.addByte(0);
+ }
+ return sb.getBytes();
+ }
+
+ /**
+ * Null padded strings can't be reliably unpadded, since the
+ * source may contain nulls. It is up to the implementation to
+ * keep track of how many bytes in the packet are used.
+ **/
+ public function unpad( s : Bytes ) : Bytes {
+ return s;
+ }
+
+ public function calcNumBlocks(len : Int) : Int {
+ return Math.ceil(len/blockSize);
+ }
+
+ private function setBlockSize( x : Int ) : Int {
+ this.blockSize = x;
+ this.textSize = x;
+ return x;
+ }
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/padding/PadPkcs5.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2008, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.padding;
+
+class PadPkcs5 extends PadBase, implements IPad {
+
+ override public function calcNumBlocks(len : Int) : Int {
+ var chr : Int = blockSize - (len % blockSize);
+ Assert.isEqual(0, (len + chr) % blockSize);
+ return Math.floor((len + chr) / blockSize);
+ }
+
+ override public function pad( s : Bytes ) : Bytes {
+ var sb = new BytesBuffer();
+ if(s.length > 0)
+ sb.add ( s );
+ var chr : Int = blockSize - (s.length % blockSize);
+ if(s.length == blockSize)
+ chr = blockSize;
+ for( i in 0...chr) {
+ sb.addByte( chr );
+ }
+ var rv = sb.getBytes();
+ return rv;
+ }
+
+ override public function unpad( s : Bytes ) : Bytes {
+ if( s.length % blockSize != 0)
+ throw "crypt.padpkcs5 unpad: buffer length "+s.length+" not multiple of
block size " + blockSize;
+ var c : Int = s.get(s.length-1);
+ var i = c;
+ var pos = s.length - 1;
+ while(i > 0) {
+ var n = s.get(pos);
+ if (c != n)
+ throw "crypt.padpkcs5 unpad: invalid byte";
+ pos--;
+ i--;
+ }
+ return s.sub(0, s.length - c);
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/padding/PadSSL.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2011, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.padding;
+
+/**
+ * SSL padding. Just like TLS padding, but bytes other than last one
+ * are arbitrary.
+ * @todo pad could insert random data, other than last byte
+ **/
+class PadSSL extends PadTLS, implements IPad {
+ override public function unpad( s : Bytes ) : Bytes {
+ if( s.length % blockSize != 0)
+ throw new chx.lang.Exception("PadTLS unpad: buffer length "+s.length+"
not multiple of block size " + blockSize);
+ return s.sub(0, s.length - s.get(s.length-1) - 1);
+ }
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/padding/PadTLS.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2011, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.padding;
+/**
+ * Very similar to PKCS5 padding, but adds one extra byte of the pad length
+ * @todo In TLS, the padding may be any random length up to 255 bytes,
+ * as per RFC 4346 Section 6.2.3.2, to decrease attacks on the
protocol.
+ * Should add a method to allow for random pad lengths.
+ **/
+class PadTLS extends PadBase, implements IPad {
+
+ override public function pad( s : Bytes ) : Bytes {
+ var c = blockSize - ((s.length+1) % blockSize);
+ if (c <= 0) return s;
+ var bb = new BytesBuffer();
+ bb.add(s);
+ for(i in 0...c+1) {
+ bb.addByte(c);
+ }
+ return bb.getBytes();
+ }
+
+ override public function unpad( s : Bytes ) : Bytes {
+ if( s.length % blockSize != 0)
+ throw new chx.lang.Exception("PadTLS unpad: buffer length "+s.length+"
not multiple of block size " + blockSize);
+ var c = s.get(s.length-1);
+ var i:Int = c;
+ var len = s.length;
+ while(i > -1) {
+ var n = s.get(pos);
+ if (c != n)
+ throw new chx.lang.Exception("PadTLS unpad: invalid byte");
+ len--;
+ i--;
+ }
+ return s.sub(0, len);
+ }
+
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/symmetric/Aes.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,1376 @@
+/*
+ * Copyright (c) 2008, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.symmetric;
+
+#if (neko || useNCrypt)
+private typedef Keycontext = Dynamic;
+#else
+private typedef Keycontext = {
+ var rounds : Int;
+ var rk : Array<Array<Int>>;
+};
+#end
+
+class Aes implements IBlockCipher {
+ public static var AES_BLOCK_SIZE : Int = 16;
+
+ public var keylen(default,__setKeylen) : Int;
+ public var passphrase(default,__setPassphrase) : Bytes;
+ public var blockSize(__getBlockSize,null) : Int;
+ //TODO: neko needs to respect this flag
+ var initialized : Bool;
+ var encKey : Keycontext;
+ var decKey : Keycontext;
+
+
+ public function new(keylen : Int, phrase:Bytes) {
+ __setKeylen(keylen);
+ __setPassphrase(phrase);
+ blockSize = AES_BLOCK_SIZE;
+ initialized = true;
+ initKeys();
+ }
+
+ public function toString() : String {
+ return "aes-" + keylen;
+ }
+
+
+ function __getBlockSize() : Int {
+ return this.blockSize;
+ }
+
+ function initKeys() {
+ encKey = makeKey(true, keylen, passphrase);
+ decKey = makeKey(false, keylen, passphrase, encKey);
+ }
+
+ public function encryptBlock( block : Bytes ) : Bytes {
+ if(block.length != blockSize)
+ throw("bad block size");
+ #if (neko || useNCrypt)
+ var rv = Bytes.ofData(aes_encrypt_block( encKey, block.getData()));
+ if(blockSize != rv.length)
+ throw("returned buffer is " + rv.length + " bytes");
+ return rv;
+ #else
+ return AESencrypt( block, encKey );
+ #end
+ }
+
+ public function decryptBlock( block : Bytes ) : Bytes {
+ #if (neko || useNCrypt)
+ var rv = Bytes.ofData(aes_decrypt_block( decKey, block.getData()));
+ if(blockSize != rv.length)
+ throw("returned buffer is " + rv.length + " bytes");
+ return rv;
+ #else
+ return AESdecrypt( block, decKey );
+ #end
+ }
+
+ /**
+ * Transform buffer to a key. If making a decryption key, and the encrypt
key
+ * for the same buffer and keylen exists, it may be passed in as context to
+ * reduce key generation time.
+ *
+ * @param encrypt True for an encryption key, false for decrypt
+ * @param keylen length of key to generate
+ * @param buf Bytes buffer of key material
+ * @param context Optional encrypt key for generating decrypt key
+ **/
+ static function makeKey( encrypt : Bool, keylen : Int, buf :
Bytes, ?context : Keycontext ) : Keycontext
+ {
+#if (neko || useNCrypt)
+ return aes_create_key(encrypt, keylen, buf.getData());
+#else
+ if(encrypt)
+ return keyExpansionEnc( buf, keylen );
+ return keyExpansionDec( buf, keylen, context );
+#end
+ }
+
+/*
+ override public function encrypt(msg : String) {
+ var rv;
+ switch(mode) {
+#if (neko || useNCrypt)
+ case ECB:
+ rv = new String(naes_ecb_encrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
+ case CBC:
+ rv = new String(naes_cbc_encrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
+#else
+ case ECB:
+ rv = ecb_encrypt(msg);
+ case CBC:
+ rv = cbc_encrypt(msg);
+#end
+ default:
+ modeError();
+ }
+ if(rv == null)
+ return "";
+ return rv;
+ }
+
+ override public function decrypt(msg : String) {
+ var rv;
+ switch(mode) {
+#if (neko || useNCrypt)
+ case ECB:
+ rv = new String(naes_ecb_decrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
+ case CBC:
+ rv = new String(naes_cbc_decrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
+#else
+ case ECB:
+ rv = ecb_decrypt(msg);
+ case CBC:
+ rv = cbc_decrypt(msg);
+#end
+ default:
+ modeError();
+ }
+ if(rv == null)
+ return "";
+ return rv;
+ }
+*/
+
+ private function __setKeylen(len : Int) : Int {
+ if(len != 128 && len != 192 && len != 256)
+ throw "Invalid key length";
+ keylen = len;
+ if(initialized)
+ initKeys();
+ return len;
+ }
+
+ private function __setPassphrase(buf : Bytes) {
+ passphrase = buf;
+ if(initialized)
+ initKeys();
+ return buf;
+ }
+
+#if !(neko || useNCrypt)
+
+ static var maxkc : Int = 8;
+ static var maxrk : Int = 14;
+
+/*
+ public function ecb_encrypt( msg : String ) : String {
+ var buf : String;
+ if(!usePadding)
+ buf = ByteStringTools.nullPadString(msg, AES_BLOCK_SIZE);
+ else {
+ var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
+ buf = pkcs5.pad(msg);
+ }
+ var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
+ var offset : Int = 0;
+ var sb = new StringBuf();
+ for (i in 0...numBlocks) {
+ var rv = AESencrypt(buf.substr(offset, AES_BLOCK_SIZE), encKey);
+ offset += AES_BLOCK_SIZE;
+ sb.add(rv);
+ }
+ return sb.toString();
+ }
+
+ public function cbc_encrypt( msg : String ) : String {
+ var buf : String;
+ if(!usePadding)
+ buf = ByteStringTools.nullPadString(msg, AES_BLOCK_SIZE);
+ else {
+ var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
+ buf = pkcs5.pad(msg);
+ }
+ var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
+ var offset : Int = 0;
+ var sb = new StringBuf();
+ var iv = ByteStringTools.nullString( AES_BLOCK_SIZE);
+
+ for (i in 0...numBlocks) {
+ var sb2 = new StringBuf();
+ for(x in 0...blockSize) {
+ sb2.addChar( buf.charCodeAt(offset + x) ^ iv.charCodeAt(x));
+ }
+ var outBuffer = AESencrypt(sb2.toString(), encKey);
+ sb.add(outBuffer);
+ iv = outBuffer;
+ offset += AES_BLOCK_SIZE;
+ }
+ return sb.toString();
+ }
+
+ public function ecb_decrypt( buf : String ) : String {
+ if(buf.length % AES_BLOCK_SIZE != 0)
+ throw "Invalid message length";
+ var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
+ var offset : Int = 0;
+ var sb = new StringBuf();
+ for (i in 0...numBlocks) {
+ var rv = AESdecrypt(buf.substr(offset, AES_BLOCK_SIZE), decKey);
+ offset += AES_BLOCK_SIZE;
+ sb.add(rv);
+ }
+ if(usePadding) {
+ var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
+ return pkcs5.unpad(sb.toString());
+ }
+ return sb.toString();
+ }
+
+ public function cbc_decrypt( buf : String ) : String {
+ if(buf.length % AES_BLOCK_SIZE != 0)
+ throw "Invalid message length";
+ var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
+ var iv = ByteStringTools.nullString( AES_BLOCK_SIZE);
+
+ var i = numBlocks;
+ var offset : Int = 0;
+ var sb = new StringBuf();
+
+ for (i in 0...numBlocks) {
+ var rv : String = AESdecrypt(
+ buf.substr(offset, AES_BLOCK_SIZE),
+ decKey
+ );
+ var sb2 = new StringBuf();
+ for(x in 0...blockSize) {
+ sb2.addChar( rv.charCodeAt(x) ^ iv.charCodeAt(x));
+ }
+ sb.add(sb2.toString());
+ iv = buf.substr(offset, AES_BLOCK_SIZE);
+ offset += AES_BLOCK_SIZE;
+ }
+ if(usePadding) {
+ var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
+ return pkcs5.unpad(sb.toString());
+ }
+ return sb.toString();
+ }
+*/
+
+
+ /* Adaptations from Javascript source
+ * Rijndael (AES) Encryption
+ * Copyright 2005 Herbert Hanewinkel, www.haneWIN.de
+ * version 1.1, check www.haneWIN.de for the latest version
+
+ * This software is provided as-is, without express or implied warranty.
+ * Permission to use, copy, modify, distribute or sell this software, with
or
+ * without fee, for any purpose and by any individual or organization, is
hereby
+ * granted, provided that the above copyright notice and this paragraph
appear
+ * in all copies. Distribution as a part of an application or binary must
+ * include the above copyright notice in the documentation and/or other
+ * materials provided with the application or distribution.
+ */
+ // http://www.hanewin.net/encrypt/aes/aes.htm
+ static function keyExpansionEnc( key : Bytes, keylen : Int)
+ {
+ var i:Int, j:Int, r:Int, t:Int;
+ var keybytes : Int;
+ var rounds : Int;
+ var kc : Int;
+
+ var keySched=new Array<Array<Int>>(); // maxrk+1
+ var k=new Array<Int>(); // maxkc
+ var tk=new Array<Int>(); // maxkc
+ var rconpointer=0;
+
+ switch(keylen) {
+ case 128:
+ keybytes = 16;
+ rounds = 10;
+ kc = 4;
+ case 192:
+ keybytes = 24;
+ rounds = 12;
+ kc = 6;
+ case 256:
+ keybytes = 32;
+ rounds = 14;
+ kc = 8;
+ default:
+ throw "Invalid keylen";
+ }
+
+ for(i in 0...maxrk+1)
+ keySched[i]=new Array();
+
+ i = 0;
+ for(j in 0...keybytes) {
+ k[j] = key.get(i) | (key.get(i+1)<<8)
+ | (key.get(i+2)<<16) | (key.get(i+3)<<24);
+ i += 4;
+ }
+
+ j = kc - 1;
+ while(j >= 0) {
+ tk[j] = k[j];
+ j--;
+ }
+
+ r=0;
+ t=0;
+ j=0;
+ while( (j<kc) && (r<rounds+1) ) {
+ while( (j<kc)&&(t<4) )
+ {
+ keySched[r][t]=tk[j];
+ j++;
+ t++;
+ }
+ if( t == 4 )
+ {
+ r++;
+ t=0;
+ }
+ }
+
+ while(r<rounds+1)
+ {
+ var temp = tk[kc-1];
+
+ tk[0] ^= S[B1(temp)] | (S[B2(temp)]<<8) | (S[B3(temp)]<<16) |
(S[B0(temp)]<<24);
+ tk[0] ^= Rcon[rconpointer++];
+
+ if(kc != 8)
+ {
+ for(j in 1...kc)
+ tk[j] ^= tk[j-1];
+ }
+ else
+ {
+ var iKc2 = Std.int(kc/2);
+ for(j in 1...iKc2)
+ tk[j] ^= tk[j-1];
+ temp = tk[Std.int(iKc2-1)];
+ tk[iKc2] ^= S[B0(temp)] | (S[B1(temp)]<<8) | (S[B2(temp)]<<16) |
(S[B3(temp)]<<24);
+
+ for(j in iKc2 + 1 ... kc)
+ tk[j] ^= tk[j-1];
+ }
+
+ j = 0;
+ while( (j<kc)&&(r<rounds+1) )
+ {
+ while( (j<kc)&&(t<4) )
+ {
+ keySched[r][t]=tk[j];
+ j++;
+ t++;
+ }
+ if(t == 4)
+ {
+ r++;
+ t=0;
+ }
+ }
+ }
+ return { rounds : rounds, rk : keySched };
+ } // keyExpansionEnc
+
+ static function keyExpansionDec(key : Bytes, keylen : Int, ?context :
Keycontext) {
+ var w;
+ var rk2 = new Array<Array<Int>>(); // maxrk+1
+ var ctx : Keycontext;
+ if(context == null)
+ ctx = keyExpansionEnc(key, keylen);
+ else
+ ctx = context;
+ var rounds=ctx.rounds;
+
+ for(r in 0...maxrk+1)
+ {
+ rk2[r]=new Array();
+ rk2[r][0] = ctx.rk[r][0];
+ rk2[r][1] = ctx.rk[r][1];
+ rk2[r][2] = ctx.rk[r][2];
+ rk2[r][3] = ctx.rk[r][3];
+ }
+
+ for(r in 1...rounds)
+ {
+ w=rk2[r][0]; rk2[r][0] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
+ w=rk2[r][1]; rk2[r][1] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
+ w=rk2[r][2]; rk2[r][2] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
+ w=rk2[r][3]; rk2[r][3] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
+ }
+ return { rounds : rounds, rk : rk2 };
+ }
+
+ function AESencrypt(block : Bytes, ctx : Keycontext)
+ {
+ var r;
+ var t0,t1,t2,t3;
+
+ var b = I32.unpackLE(block);
+ var rounds = ctx.rounds;
+ var b0 = b[0];
+ var b1 = b[1];
+ var b2 = b[2];
+ var b3 = b[3];
+
+ for(r in 0 ... rounds-1)
+ {
+ t0 = b0 ^ ctx.rk[r][0];
+ t1 = b1 ^ ctx.rk[r][1];
+ t2 = b2 ^ ctx.rk[r][2];
+ t3 = b3 ^ ctx.rk[r][3];
+
+ b0 = T1[t0&255] ^ T2[(t1>>8)&255] ^ T3[(t2>>16)&255] ^ T4[t3>>>24];
+ b1 = T1[t1&255] ^ T2[(t2>>8)&255] ^ T3[(t3>>16)&255] ^ T4[t0>>>24];
+ b2 = T1[t2&255] ^ T2[(t3>>8)&255] ^ T3[(t0>>16)&255] ^ T4[t1>>>24];
+ b3 = T1[t3&255] ^ T2[(t0>>8)&255] ^ T3[(t1>>16)&255] ^ T4[t2>>>24];
+ }
+
+ // last round is special
+ r = rounds-1;
+
+ t0 = b0 ^ ctx.rk[r][0];
+ t1 = b1 ^ ctx.rk[r][1];
+ t2 = b2 ^ ctx.rk[r][2];
+ t3 = b3 ^ ctx.rk[r][3];
+
+ b[0] = F1(t0, t1, t2, t3) ^ ctx.rk[rounds][0];
+ b[1] = F1(t1, t2, t3, t0) ^ ctx.rk[rounds][1];
+ b[2] = F1(t2, t3, t0, t1) ^ ctx.rk[rounds][2];
+ b[3] = F1(t3, t0, t1, t2) ^ ctx.rk[rounds][3];
+
+ return I32.packLE(b);
+ }
+
+ function AESdecrypt(block : Bytes, ctx)
+ {
+ var t0:Int,t1:Int,t2:Int,t3:Int;
+ var b = I32.unpackLE(block);
+ var r = ctx.rounds;
+ while( r>1 )
+ {
+ t0 = b[0] ^ ctx.rk[r][0];
+ t1 = b[1] ^ ctx.rk[r][1];
+ t2 = b[2] ^ ctx.rk[r][2];
+ t3 = b[3] ^ ctx.rk[r][3];
+
+ b[0] = T5[B0(t0)] ^ T6[B1(t3)] ^ T7[B2(t2)] ^ T8[B3(t1)];
+ b[1] = T5[B0(t1)] ^ T6[B1(t0)] ^ T7[B2(t3)] ^ T8[B3(t2)];
+ b[2] = T5[B0(t2)] ^ T6[B1(t1)] ^ T7[B2(t0)] ^ T8[B3(t3)];
+ b[3] = T5[B0(t3)] ^ T6[B1(t2)] ^ T7[B2(t1)] ^ T8[B3(t0)];
+ r --;
+ }
+
+ // last round is special
+ t0 = b[0] ^ ctx.rk[1][0];
+ t1 = b[1] ^ ctx.rk[1][1];
+ t2 = b[2] ^ ctx.rk[1][2];
+ t3 = b[3] ^ ctx.rk[1][3];
+
+ b[0] = S5[B0(t0)] | (S5[B1(t3)]<<8) | (S5[B2(t2)]<<16) |
(S5[B3(t1)]<<24);
+ b[1] = S5[B0(t1)] | (S5[B1(t0)]<<8) | (S5[B2(t3)]<<16) |
(S5[B3(t2)]<<24);
+ b[2] = S5[B0(t2)] | (S5[B1(t1)]<<8) | (S5[B2(t0)]<<16) |
(S5[B3(t3)]<<24);
+ b[3] = S5[B0(t3)] | (S5[B1(t2)]<<8) | (S5[B2(t1)]<<16) |
(S5[B3(t0)]<<24);
+
+ b[0] ^= ctx.rk[0][0];
+ b[1] ^= ctx.rk[0][1];
+ b[2] ^= ctx.rk[0][2];
+ b[3] ^= ctx.rk[0][3];
+
+ return I32.packLE(b);
+ }
+
+
+ static function B0(x:Int) { return (x&255); }
+ static function B1(x:Int) { return ((x>>8)&255); }
+ static function B2(x:Int) { return ((x>>16)&255); }
+ static function B3(x:Int) { return ((x>>24)&255); }
+
+ static function F1(x0 : Int, x1:Int, x2:Int, x3:Int)
+ {
+ return B1(T1[x0&255]) | (B1(T1[(x1>>8)&255])<<8)
+ | (B1(T1[(x2>>16)&255])<<16) | (B1(T1[x3>>>24])<<24);
+ }
+
+ // The round constants used in subkey expansion
+ static var Rcon : Array<Int> = [
+ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
+ 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4,
+ 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91 ];
+
+ // Precomputed lookup table for the SBox
+ static var S : Array<Int> = [
+ 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171,
+ 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164,
+ 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113,
+ 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226,
+ 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214,
+ 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203,
+ 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69,
+ 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245,
+ 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68,
+ 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42,
+ 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73,
+ 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109,
+ 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37,
+ 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62,
+ 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225,
+ 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
+ 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187,
+ 22 ];
+ // Precomputed lookup table for the inverse SBox FOR DECRYPTION
+ static var S5 : Array<Int> = [
+ 82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215,
+ 251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222,
+ 233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66,
+ 250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73,
+ 109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92,
+ 204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21,
+ 70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247,
+ 228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2,
+ 193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220,
+ 234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173,
+ 53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29,
+ 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75,
+ 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168,
+ 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81,
+ 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160,
+ 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
+ 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12,
+ 125 ];
+
+ static var T1 : Array<Int> = [
+ 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6,
+ 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591,
+ 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56,
+ 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec,
+ 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa,
+ 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb,
+ 0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45,
+ 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b,
+ 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c,
+ 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83,
+ 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9,
+ 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a,
+ 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d,
+ 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f,
+ 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df,
+ 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea,
+ 0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34,
+ 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b,
+ 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d,
+ 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413,
+ 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1,
+ 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6,
+ 0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972,
+ 0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85,
+ 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed,
+ 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511,
+ 0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe,
+ 0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b,
+ 0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05,
+ 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1,
+ 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142,
+ 0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf,
+ 0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3,
+ 0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e,
+ 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a,
+ 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6,
+ 0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3,
+ 0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b,
+ 0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428,
+ 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad,
+ 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14,
+ 0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8,
+ 0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4,
+ 0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2,
+ 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda,
+ 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949,
+ 0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf,
+ 0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810,
+ 0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c,
+ 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697,
+ 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e,
+ 0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f,
+ 0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc,
+ 0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c,
+ 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969,
+ 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27,
+ 0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122,
+ 0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433,
+ 0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9,
+ 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5,
+ 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a,
+ 0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0,
+ 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e,
+ 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c ];
+
+ static var T2 : Array<Int> = [
+ 0x6363c6a5, 0x7c7cf884, 0x7777ee99, 0x7b7bf68d,
+ 0xf2f2ff0d, 0x6b6bd6bd, 0x6f6fdeb1, 0xc5c59154,
+ 0x30306050, 0x01010203, 0x6767cea9, 0x2b2b567d,
+ 0xfefee719, 0xd7d7b562, 0xabab4de6, 0x7676ec9a,
+ 0xcaca8f45, 0x82821f9d, 0xc9c98940, 0x7d7dfa87,
+ 0xfafaef15, 0x5959b2eb, 0x47478ec9, 0xf0f0fb0b,
+ 0xadad41ec, 0xd4d4b367, 0xa2a25ffd, 0xafaf45ea,
+ 0x9c9c23bf, 0xa4a453f7, 0x7272e496, 0xc0c09b5b,
+ 0xb7b775c2, 0xfdfde11c, 0x93933dae, 0x26264c6a,
+ 0x36366c5a, 0x3f3f7e41, 0xf7f7f502, 0xcccc834f,
+ 0x3434685c, 0xa5a551f4, 0xe5e5d134, 0xf1f1f908,
+ 0x7171e293, 0xd8d8ab73, 0x31316253, 0x15152a3f,
+ 0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e,
+ 0x18183028, 0x969637a1, 0x05050a0f, 0x9a9a2fb5,
+ 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d,
+ 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f,
+ 0x0909121b, 0x83831d9e, 0x2c2c5874, 0x1a1a342e,
+ 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb,
+ 0x5252a4f6, 0x3b3b764d, 0xd6d6b761, 0xb3b37dce,
+ 0x2929527b, 0xe3e3dd3e, 0x2f2f5e71, 0x84841397,
+ 0x5353a6f5, 0xd1d1b968, 0x00000000, 0xededc12c,
+ 0x20204060, 0xfcfce31f, 0xb1b179c8, 0x5b5bb6ed,
+ 0x6a6ad4be, 0xcbcb8d46, 0xbebe67d9, 0x3939724b,
+ 0x4a4a94de, 0x4c4c98d4, 0x5858b0e8, 0xcfcf854a,
+ 0xd0d0bb6b, 0xefefc52a, 0xaaaa4fe5, 0xfbfbed16,
+ 0x434386c5, 0x4d4d9ad7, 0x33336655, 0x85851194,
+ 0x45458acf, 0xf9f9e910, 0x02020406, 0x7f7ffe81,
+ 0x5050a0f0, 0x3c3c7844, 0x9f9f25ba, 0xa8a84be3,
+ 0x5151a2f3, 0xa3a35dfe, 0x404080c0, 0x8f8f058a,
+ 0x92923fad, 0x9d9d21bc, 0x38387048, 0xf5f5f104,
+ 0xbcbc63df, 0xb6b677c1, 0xdadaaf75, 0x21214263,
+ 0x10102030, 0xffffe51a, 0xf3f3fd0e, 0xd2d2bf6d,
+ 0xcdcd814c, 0x0c0c1814, 0x13132635, 0xececc32f,
+ 0x5f5fbee1, 0x979735a2, 0x444488cc, 0x17172e39,
+ 0xc4c49357, 0xa7a755f2, 0x7e7efc82, 0x3d3d7a47,
+ 0x6464c8ac, 0x5d5dbae7, 0x1919322b, 0x7373e695,
+ 0x6060c0a0, 0x81811998, 0x4f4f9ed1, 0xdcdca37f,
+ 0x22224466, 0x2a2a547e, 0x90903bab, 0x88880b83,
+ 0x46468cca, 0xeeeec729, 0xb8b86bd3, 0x1414283c,
+ 0xdedea779, 0x5e5ebce2, 0x0b0b161d, 0xdbdbad76,
+ 0xe0e0db3b, 0x32326456, 0x3a3a744e, 0x0a0a141e,
+ 0x494992db, 0x06060c0a, 0x2424486c, 0x5c5cb8e4,
+ 0xc2c29f5d, 0xd3d3bd6e, 0xacac43ef, 0x6262c4a6,
+ 0x919139a8, 0x959531a4, 0xe4e4d337, 0x7979f28b,
+ 0xe7e7d532, 0xc8c88b43, 0x37376e59, 0x6d6ddab7,
+ 0x8d8d018c, 0xd5d5b164, 0x4e4e9cd2, 0xa9a949e0,
+ 0x6c6cd8b4, 0x5656acfa, 0xf4f4f307, 0xeaeacf25,
+ 0x6565caaf, 0x7a7af48e, 0xaeae47e9, 0x08081018,
+ 0xbaba6fd5, 0x7878f088, 0x25254a6f, 0x2e2e5c72,
+ 0x1c1c3824, 0xa6a657f1, 0xb4b473c7, 0xc6c69751,
+ 0xe8e8cb23, 0xdddda17c, 0x7474e89c, 0x1f1f3e21,
+ 0x4b4b96dd, 0xbdbd61dc, 0x8b8b0d86, 0x8a8a0f85,
+ 0x7070e090, 0x3e3e7c42, 0xb5b571c4, 0x6666ccaa,
+ 0x484890d8, 0x03030605, 0xf6f6f701, 0x0e0e1c12,
+ 0x6161c2a3, 0x35356a5f, 0x5757aef9, 0xb9b969d0,
+ 0x86861791, 0xc1c19958, 0x1d1d3a27, 0x9e9e27b9,
+ 0xe1e1d938, 0xf8f8eb13, 0x98982bb3, 0x11112233,
+ 0x6969d2bb, 0xd9d9a970, 0x8e8e0789, 0x949433a7,
+ 0x9b9b2db6, 0x1e1e3c22, 0x87871592, 0xe9e9c920,
+ 0xcece8749, 0x5555aaff, 0x28285078, 0xdfdfa57a,
+ 0x8c8c038f, 0xa1a159f8, 0x89890980, 0x0d0d1a17,
+ 0xbfbf65da, 0xe6e6d731, 0x424284c6, 0x6868d0b8,
+ 0x414182c3, 0x999929b0, 0x2d2d5a77, 0x0f0f1e11,
+ 0xb0b07bcb, 0x5454a8fc, 0xbbbb6dd6, 0x16162c3a ];
+
+ static var T3 : Array<Int> = [
+ 0x63c6a563, 0x7cf8847c, 0x77ee9977, 0x7bf68d7b,
+ 0xf2ff0df2, 0x6bd6bd6b, 0x6fdeb16f, 0xc59154c5,
+ 0x30605030, 0x01020301, 0x67cea967, 0x2b567d2b,
+ 0xfee719fe, 0xd7b562d7, 0xab4de6ab, 0x76ec9a76,
+ 0xca8f45ca, 0x821f9d82, 0xc98940c9, 0x7dfa877d,
+ 0xfaef15fa, 0x59b2eb59, 0x478ec947, 0xf0fb0bf0,
+ 0xad41ecad, 0xd4b367d4, 0xa25ffda2, 0xaf45eaaf,
+ 0x9c23bf9c, 0xa453f7a4, 0x72e49672, 0xc09b5bc0,
+ 0xb775c2b7, 0xfde11cfd, 0x933dae93, 0x264c6a26,
+ 0x366c5a36, 0x3f7e413f, 0xf7f502f7, 0xcc834fcc,
+ 0x34685c34, 0xa551f4a5, 0xe5d134e5, 0xf1f908f1,
+ 0x71e29371, 0xd8ab73d8, 0x31625331, 0x152a3f15,
+ 0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3,
+ 0x18302818, 0x9637a196, 0x050a0f05, 0x9a2fb59a,
+ 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2,
+ 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75,
+ 0x09121b09, 0x831d9e83, 0x2c58742c, 0x1a342e1a,
+ 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0,
+ 0x52a4f652, 0x3b764d3b, 0xd6b761d6, 0xb37dceb3,
+ 0x29527b29, 0xe3dd3ee3, 0x2f5e712f, 0x84139784,
+ 0x53a6f553, 0xd1b968d1, 0x00000000, 0xedc12ced,
+ 0x20406020, 0xfce31ffc, 0xb179c8b1, 0x5bb6ed5b,
+ 0x6ad4be6a, 0xcb8d46cb, 0xbe67d9be, 0x39724b39,
+ 0x4a94de4a, 0x4c98d44c, 0x58b0e858, 0xcf854acf,
+ 0xd0bb6bd0, 0xefc52aef, 0xaa4fe5aa, 0xfbed16fb,
+ 0x4386c543, 0x4d9ad74d, 0x33665533, 0x85119485,
+ 0x458acf45, 0xf9e910f9, 0x02040602, 0x7ffe817f,
+ 0x50a0f050, 0x3c78443c, 0x9f25ba9f, 0xa84be3a8,
+ 0x51a2f351, 0xa35dfea3, 0x4080c040, 0x8f058a8f,
+ 0x923fad92, 0x9d21bc9d, 0x38704838, 0xf5f104f5,
+ 0xbc63dfbc, 0xb677c1b6, 0xdaaf75da, 0x21426321,
+ 0x10203010, 0xffe51aff, 0xf3fd0ef3, 0xd2bf6dd2,
+ 0xcd814ccd, 0x0c18140c, 0x13263513, 0xecc32fec,
+ 0x5fbee15f, 0x9735a297, 0x4488cc44, 0x172e3917,
+ 0xc49357c4, 0xa755f2a7, 0x7efc827e, 0x3d7a473d,
+ 0x64c8ac64, 0x5dbae75d, 0x19322b19, 0x73e69573,
+ 0x60c0a060, 0x81199881, 0x4f9ed14f, 0xdca37fdc,
+ 0x22446622, 0x2a547e2a, 0x903bab90, 0x880b8388,
+ 0x468cca46, 0xeec729ee, 0xb86bd3b8, 0x14283c14,
+ 0xdea779de, 0x5ebce25e, 0x0b161d0b, 0xdbad76db,
+ 0xe0db3be0, 0x32645632, 0x3a744e3a, 0x0a141e0a,
+ 0x4992db49, 0x060c0a06, 0x24486c24, 0x5cb8e45c,
+ 0xc29f5dc2, 0xd3bd6ed3, 0xac43efac, 0x62c4a662,
+ 0x9139a891, 0x9531a495, 0xe4d337e4, 0x79f28b79,
+ 0xe7d532e7, 0xc88b43c8, 0x376e5937, 0x6ddab76d,
+ 0x8d018c8d, 0xd5b164d5, 0x4e9cd24e, 0xa949e0a9,
+ 0x6cd8b46c, 0x56acfa56, 0xf4f307f4, 0xeacf25ea,
+ 0x65caaf65, 0x7af48e7a, 0xae47e9ae, 0x08101808,
+ 0xba6fd5ba, 0x78f08878, 0x254a6f25, 0x2e5c722e,
+ 0x1c38241c, 0xa657f1a6, 0xb473c7b4, 0xc69751c6,
+ 0xe8cb23e8, 0xdda17cdd, 0x74e89c74, 0x1f3e211f,
+ 0x4b96dd4b, 0xbd61dcbd, 0x8b0d868b, 0x8a0f858a,
+ 0x70e09070, 0x3e7c423e, 0xb571c4b5, 0x66ccaa66,
+ 0x4890d848, 0x03060503, 0xf6f701f6, 0x0e1c120e,
+ 0x61c2a361, 0x356a5f35, 0x57aef957, 0xb969d0b9,
+ 0x86179186, 0xc19958c1, 0x1d3a271d, 0x9e27b99e,
+ 0xe1d938e1, 0xf8eb13f8, 0x982bb398, 0x11223311,
+ 0x69d2bb69, 0xd9a970d9, 0x8e07898e, 0x9433a794,
+ 0x9b2db69b, 0x1e3c221e, 0x87159287, 0xe9c920e9,
+ 0xce8749ce, 0x55aaff55, 0x28507828, 0xdfa57adf,
+ 0x8c038f8c, 0xa159f8a1, 0x89098089, 0x0d1a170d,
+ 0xbf65dabf, 0xe6d731e6, 0x4284c642, 0x68d0b868,
+ 0x4182c341, 0x9929b099, 0x2d5a772d, 0x0f1e110f,
+ 0xb07bcbb0, 0x54a8fc54, 0xbb6dd6bb, 0x162c3a16 ];
+
+ static var T4 : Array<Int> = [
+ 0xc6a56363, 0xf8847c7c, 0xee997777, 0xf68d7b7b,
+ 0xff0df2f2, 0xd6bd6b6b, 0xdeb16f6f, 0x9154c5c5,
+ 0x60503030, 0x02030101, 0xcea96767, 0x567d2b2b,
+ 0xe719fefe, 0xb562d7d7, 0x4de6abab, 0xec9a7676,
+ 0x8f45caca, 0x1f9d8282, 0x8940c9c9, 0xfa877d7d,
+ 0xef15fafa, 0xb2eb5959, 0x8ec94747, 0xfb0bf0f0,
+ 0x41ecadad, 0xb367d4d4, 0x5ffda2a2, 0x45eaafaf,
+ 0x23bf9c9c, 0x53f7a4a4, 0xe4967272, 0x9b5bc0c0,
+ 0x75c2b7b7, 0xe11cfdfd, 0x3dae9393, 0x4c6a2626,
+ 0x6c5a3636, 0x7e413f3f, 0xf502f7f7, 0x834fcccc,
+ 0x685c3434, 0x51f4a5a5, 0xd134e5e5, 0xf908f1f1,
+ 0xe2937171, 0xab73d8d8, 0x62533131, 0x2a3f1515,
+ 0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3,
+ 0x30281818, 0x37a19696, 0x0a0f0505, 0x2fb59a9a,
+ 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2,
+ 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575,
+ 0x121b0909, 0x1d9e8383, 0x58742c2c, 0x342e1a1a,
+ 0x362d1b1b, 0xdcb26e6e, 0xb4ee5a5a, 0x5bfba0a0,
+ 0xa4f65252, 0x764d3b3b, 0xb761d6d6, 0x7dceb3b3,
+ 0x527b2929, 0xdd3ee3e3, 0x5e712f2f, 0x13978484,
+ 0xa6f55353, 0xb968d1d1, 0x00000000, 0xc12ceded,
+ 0x40602020, 0xe31ffcfc, 0x79c8b1b1, 0xb6ed5b5b,
+ 0xd4be6a6a, 0x8d46cbcb, 0x67d9bebe, 0x724b3939,
+ 0x94de4a4a, 0x98d44c4c, 0xb0e85858, 0x854acfcf,
+ 0xbb6bd0d0, 0xc52aefef, 0x4fe5aaaa, 0xed16fbfb,
+ 0x86c54343, 0x9ad74d4d, 0x66553333, 0x11948585,
+ 0x8acf4545, 0xe910f9f9, 0x04060202, 0xfe817f7f,
+ 0xa0f05050, 0x78443c3c, 0x25ba9f9f, 0x4be3a8a8,
+ 0xa2f35151, 0x5dfea3a3, 0x80c04040, 0x058a8f8f,
+ 0x3fad9292, 0x21bc9d9d, 0x70483838, 0xf104f5f5,
+ 0x63dfbcbc, 0x77c1b6b6, 0xaf75dada, 0x42632121,
+ 0x20301010, 0xe51affff, 0xfd0ef3f3, 0xbf6dd2d2,
+ 0x814ccdcd, 0x18140c0c, 0x26351313, 0xc32fecec,
+ 0xbee15f5f, 0x35a29797, 0x88cc4444, 0x2e391717,
+ 0x9357c4c4, 0x55f2a7a7, 0xfc827e7e, 0x7a473d3d,
+ 0xc8ac6464, 0xbae75d5d, 0x322b1919, 0xe6957373,
+ 0xc0a06060, 0x19988181, 0x9ed14f4f, 0xa37fdcdc,
+ 0x44662222, 0x547e2a2a, 0x3bab9090, 0x0b838888,
+ 0x8cca4646, 0xc729eeee, 0x6bd3b8b8, 0x283c1414,
+ 0xa779dede, 0xbce25e5e, 0x161d0b0b, 0xad76dbdb,
+ 0xdb3be0e0, 0x64563232, 0x744e3a3a, 0x141e0a0a,
+ 0x92db4949, 0x0c0a0606, 0x486c2424, 0xb8e45c5c,
+ 0x9f5dc2c2, 0xbd6ed3d3, 0x43efacac, 0xc4a66262,
+ 0x39a89191, 0x31a49595, 0xd337e4e4, 0xf28b7979,
+ 0xd532e7e7, 0x8b43c8c8, 0x6e593737, 0xdab76d6d,
+ 0x018c8d8d, 0xb164d5d5, 0x9cd24e4e, 0x49e0a9a9,
+ 0xd8b46c6c, 0xacfa5656, 0xf307f4f4, 0xcf25eaea,
+ 0xcaaf6565, 0xf48e7a7a, 0x47e9aeae, 0x10180808,
+ 0x6fd5baba, 0xf0887878, 0x4a6f2525, 0x5c722e2e,
+ 0x38241c1c, 0x57f1a6a6, 0x73c7b4b4, 0x9751c6c6,
+ 0xcb23e8e8, 0xa17cdddd, 0xe89c7474, 0x3e211f1f,
+ 0x96dd4b4b, 0x61dcbdbd, 0x0d868b8b, 0x0f858a8a,
+ 0xe0907070, 0x7c423e3e, 0x71c4b5b5, 0xccaa6666,
+ 0x90d84848, 0x06050303, 0xf701f6f6, 0x1c120e0e,
+ 0xc2a36161, 0x6a5f3535, 0xaef95757, 0x69d0b9b9,
+ 0x17918686, 0x9958c1c1, 0x3a271d1d, 0x27b99e9e,
+ 0xd938e1e1, 0xeb13f8f8, 0x2bb39898, 0x22331111,
+ 0xd2bb6969, 0xa970d9d9, 0x07898e8e, 0x33a79494,
+ 0x2db69b9b, 0x3c221e1e, 0x15928787, 0xc920e9e9,
+ 0x8749cece, 0xaaff5555, 0x50782828, 0xa57adfdf,
+ 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d,
+ 0x65dabfbf, 0xd731e6e6, 0x84c64242, 0xd0b86868,
+ 0x82c34141, 0x29b09999, 0x5a772d2d, 0x1e110f0f,
+ 0x7bcbb0b0, 0xa8fc5454, 0x6dd6bbbb, 0x2c3a1616 ];
+
+ // ONLY USED FOR DECRYPTION
+ static var T5 : Array<Int> = [
+ 0x50a7f451,0x5365417e,0xc3a4171a,0x965e273a,
+ 0xcb6bab3b,0xf1459d1f,0xab58faac,0x9303e34b,
+ 0x55fa3020,0xf66d76ad,0x9176cc88,0x254c02f5,
+ 0xfcd7e54f,0xd7cb2ac5,0x80443526,0x8fa362b5,
+ 0x495ab1de,0x671bba25,0x980eea45,0xe1c0fe5d,
+ 0x02752fc3,0x12f04c81,0xa397468d,0xc6f9d36b,
+ 0xe75f8f03,0x959c9215,0xeb7a6dbf,0xda595295,
+ 0x2d83bed4,0xd3217458,0x2969e049,0x44c8c98e,
+ 0x6a89c275,0x78798ef4,0x6b3e5899,0xdd71b927,
+ 0xb64fe1be,0x17ad88f0,0x66ac20c9,0xb43ace7d,
+ 0x184adf63,0x82311ae5,0x60335197,0x457f5362,
+ 0xe07764b1,0x84ae6bbb,0x1ca081fe,0x942b08f9,
+ 0x58684870,0x19fd458f,0x876cde94,0xb7f87b52,
+ 0x23d373ab,0xe2024b72,0x578f1fe3,0x2aab5566,
+ 0x0728ebb2,0x03c2b52f,0x9a7bc586,0xa50837d3,
+ 0xf2872830,0xb2a5bf23,0xba6a0302,0x5c8216ed,
+ 0x2b1ccf8a,0x92b479a7,0xf0f207f3,0xa1e2694e,
+ 0xcdf4da65,0xd5be0506,0x1f6234d1,0x8afea6c4,
+ 0x9d532e34,0xa055f3a2,0x32e18a05,0x75ebf6a4,
+ 0x39ec830b,0xaaef6040,0x069f715e,0x51106ebd,
+ 0xf98a213e,0x3d06dd96,0xae053edd,0x46bde64d,
+ 0xb58d5491,0x055dc471,0x6fd40604,0xff155060,
+ 0x24fb9819,0x97e9bdd6,0xcc434089,0x779ed967,
+ 0xbd42e8b0,0x888b8907,0x385b19e7,0xdbeec879,
+ 0x470a7ca1,0xe90f427c,0xc91e84f8,0x00000000,
+ 0x83868009,0x48ed2b32,0xac70111e,0x4e725a6c,
+ 0xfbff0efd,0x5638850f,0x1ed5ae3d,0x27392d36,
+ 0x64d90f0a,0x21a65c68,0xd1545b9b,0x3a2e3624,
+ 0xb1670a0c,0x0fe75793,0xd296eeb4,0x9e919b1b,
+ 0x4fc5c080,0xa220dc61,0x694b775a,0x161a121c,
+ 0x0aba93e2,0xe52aa0c0,0x43e0223c,0x1d171b12,
+ 0x0b0d090e,0xadc78bf2,0xb9a8b62d,0xc8a91e14,
+ 0x8519f157,0x4c0775af,0xbbdd99ee,0xfd607fa3,
+ 0x9f2601f7,0xbcf5725c,0xc53b6644,0x347efb5b,
+ 0x7629438b,0xdcc623cb,0x68fcedb6,0x63f1e4b8,
+ 0xcadc31d7,0x10856342,0x40229713,0x2011c684,
+ 0x7d244a85,0xf83dbbd2,0x1132f9ae,0x6da129c7,
+ 0x4b2f9e1d,0xf330b2dc,0xec52860d,0xd0e3c177,
+ 0x6c16b32b,0x99b970a9,0xfa489411,0x2264e947,
+ 0xc48cfca8,0x1a3ff0a0,0xd82c7d56,0xef903322,
+ 0xc74e4987,0xc1d138d9,0xfea2ca8c,0x360bd498,
+ 0xcf81f5a6,0x28de7aa5,0x268eb7da,0xa4bfad3f,
+ 0xe49d3a2c,0x0d927850,0x9bcc5f6a,0x62467e54,
+ 0xc2138df6,0xe8b8d890,0x5ef7392e,0xf5afc382,
+ 0xbe805d9f,0x7c93d069,0xa92dd56f,0xb31225cf,
+ 0x3b99acc8,0xa77d1810,0x6e639ce8,0x7bbb3bdb,
+ 0x097826cd,0xf418596e,0x01b79aec,0xa89a4f83,
+ 0x656e95e6,0x7ee6ffaa,0x08cfbc21,0xe6e815ef,
+ 0xd99be7ba,0xce366f4a,0xd4099fea,0xd67cb029,
+ 0xafb2a431,0x31233f2a,0x3094a5c6,0xc066a235,
+ 0x37bc4e74,0xa6ca82fc,0xb0d090e0,0x15d8a733,
+ 0x4a9804f1,0xf7daec41,0x0e50cd7f,0x2ff69117,
+ 0x8dd64d76,0x4db0ef43,0x544daacc,0xdf0496e4,
+ 0xe3b5d19e,0x1b886a4c,0xb81f2cc1,0x7f516546,
+ 0x04ea5e9d,0x5d358c01,0x737487fa,0x2e410bfb,
+ 0x5a1d67b3,0x52d2db92,0x335610e9,0x1347d66d,
+ 0x8c61d79a,0x7a0ca137,0x8e14f859,0x893c13eb,
+ 0xee27a9ce,0x35c961b7,0xede51ce1,0x3cb1477a,
+ 0x59dfd29c,0x3f73f255,0x79ce1418,0xbf37c773,
+ 0xeacdf753,0x5baafd5f,0x146f3ddf,0x86db4478,
+ 0x81f3afca,0x3ec468b9,0x2c342438,0x5f40a3c2,
+ 0x72c31d16,0x0c25e2bc,0x8b493c28,0x41950dff,
+ 0x7101a839,0xdeb30c08,0x9ce4b4d8,0x90c15664,
+ 0x6184cb7b,0x70b632d5,0x745c6c48,0x4257b8d0 ];
+
+ static var T6 : Array<Int> = [
+ 0xa7f45150,0x65417e53,0xa4171ac3,0x5e273a96,
+ 0x6bab3bcb,0x459d1ff1,0x58faacab,0x03e34b93,
+ 0xfa302055,0x6d76adf6,0x76cc8891,0x4c02f525,
+ 0xd7e54ffc,0xcb2ac5d7,0x44352680,0xa362b58f,
+ 0x5ab1de49,0x1bba2567,0x0eea4598,0xc0fe5de1,
+ 0x752fc302,0xf04c8112,0x97468da3,0xf9d36bc6,
+ 0x5f8f03e7,0x9c921595,0x7a6dbfeb,0x595295da,
+ 0x83bed42d,0x217458d3,0x69e04929,0xc8c98e44,
+ 0x89c2756a,0x798ef478,0x3e58996b,0x71b927dd,
+ 0x4fe1beb6,0xad88f017,0xac20c966,0x3ace7db4,
+ 0x4adf6318,0x311ae582,0x33519760,0x7f536245,
+ 0x7764b1e0,0xae6bbb84,0xa081fe1c,0x2b08f994,
+ 0x68487058,0xfd458f19,0x6cde9487,0xf87b52b7,
+ 0xd373ab23,0x024b72e2,0x8f1fe357,0xab55662a,
+ 0x28ebb207,0xc2b52f03,0x7bc5869a,0x0837d3a5,
+ 0x872830f2,0xa5bf23b2,0x6a0302ba,0x8216ed5c,
+ 0x1ccf8a2b,0xb479a792,0xf207f3f0,0xe2694ea1,
+ 0xf4da65cd,0xbe0506d5,0x6234d11f,0xfea6c48a,
+ 0x532e349d,0x55f3a2a0,0xe18a0532,0xebf6a475,
+ 0xec830b39,0xef6040aa,0x9f715e06,0x106ebd51,
+ 0x8a213ef9,0x06dd963d,0x053eddae,0xbde64d46,
+ 0x8d5491b5,0x5dc47105,0xd406046f,0x155060ff,
+ 0xfb981924,0xe9bdd697,0x434089cc,0x9ed96777,
+ 0x42e8b0bd,0x8b890788,0x5b19e738,0xeec879db,
+ 0x0a7ca147,0x0f427ce9,0x1e84f8c9,0x00000000,
+ 0x86800983,0xed2b3248,0x70111eac,0x725a6c4e,
+ 0xff0efdfb,0x38850f56,0xd5ae3d1e,0x392d3627,
+ 0xd90f0a64,0xa65c6821,0x545b9bd1,0x2e36243a,
+ 0x670a0cb1,0xe757930f,0x96eeb4d2,0x919b1b9e,
+ 0xc5c0804f,0x20dc61a2,0x4b775a69,0x1a121c16,
+ 0xba93e20a,0x2aa0c0e5,0xe0223c43,0x171b121d,
+ 0x0d090e0b,0xc78bf2ad,0xa8b62db9,0xa91e14c8,
+ 0x19f15785,0x0775af4c,0xdd99eebb,0x607fa3fd,
+ 0x2601f79f,0xf5725cbc,0x3b6644c5,0x7efb5b34,
+ 0x29438b76,0xc623cbdc,0xfcedb668,0xf1e4b863,
+ 0xdc31d7ca,0x85634210,0x22971340,0x11c68420,
+ 0x244a857d,0x3dbbd2f8,0x32f9ae11,0xa129c76d,
+ 0x2f9e1d4b,0x30b2dcf3,0x52860dec,0xe3c177d0,
+ 0x16b32b6c,0xb970a999,0x489411fa,0x64e94722,
+ 0x8cfca8c4,0x3ff0a01a,0x2c7d56d8,0x903322ef,
+ 0x4e4987c7,0xd138d9c1,0xa2ca8cfe,0x0bd49836,
+ 0x81f5a6cf,0xde7aa528,0x8eb7da26,0xbfad3fa4,
+ 0x9d3a2ce4,0x9278500d,0xcc5f6a9b,0x467e5462,
+ 0x138df6c2,0xb8d890e8,0xf7392e5e,0xafc382f5,
+ 0x805d9fbe,0x93d0697c,0x2dd56fa9,0x1225cfb3,
+ 0x99acc83b,0x7d1810a7,0x639ce86e,0xbb3bdb7b,
+ 0x7826cd09,0x18596ef4,0xb79aec01,0x9a4f83a8,
+ 0x6e95e665,0xe6ffaa7e,0xcfbc2108,0xe815efe6,
+ 0x9be7bad9,0x366f4ace,0x099fead4,0x7cb029d6,
+ 0xb2a431af,0x233f2a31,0x94a5c630,0x66a235c0,
+ 0xbc4e7437,0xca82fca6,0xd090e0b0,0xd8a73315,
+ 0x9804f14a,0xdaec41f7,0x50cd7f0e,0xf691172f,
+ 0xd64d768d,0xb0ef434d,0x4daacc54,0x0496e4df,
+ 0xb5d19ee3,0x886a4c1b,0x1f2cc1b8,0x5165467f,
+ 0xea5e9d04,0x358c015d,0x7487fa73,0x410bfb2e,
+ 0x1d67b35a,0xd2db9252,0x5610e933,0x47d66d13,
+ 0x61d79a8c,0x0ca1377a,0x14f8598e,0x3c13eb89,
+ 0x27a9ceee,0xc961b735,0xe51ce1ed,0xb1477a3c,
+ 0xdfd29c59,0x73f2553f,0xce141879,0x37c773bf,
+ 0xcdf753ea,0xaafd5f5b,0x6f3ddf14,0xdb447886,
+ 0xf3afca81,0xc468b93e,0x3424382c,0x40a3c25f,
+ 0xc31d1672,0x25e2bc0c,0x493c288b,0x950dff41,
+ 0x01a83971,0xb30c08de,0xe4b4d89c,0xc1566490,
+ 0x84cb7b61,0xb632d570,0x5c6c4874,0x57b8d042 ];
+
+ static var T7 : Array<Int> = [
+ 0xf45150a7,0x417e5365,0x171ac3a4,0x273a965e,
+ 0xab3bcb6b,0x9d1ff145,0xfaacab58,0xe34b9303,
+ 0x302055fa,0x76adf66d,0xcc889176,0x02f5254c,
+ 0xe54ffcd7,0x2ac5d7cb,0x35268044,0x62b58fa3,
+ 0xb1de495a,0xba25671b,0xea45980e,0xfe5de1c0,
+ 0x2fc30275,0x4c8112f0,0x468da397,0xd36bc6f9,
+ 0x8f03e75f,0x9215959c,0x6dbfeb7a,0x5295da59,
+ 0xbed42d83,0x7458d321,0xe0492969,0xc98e44c8,
+ 0xc2756a89,0x8ef47879,0x58996b3e,0xb927dd71,
+ 0xe1beb64f,0x88f017ad,0x20c966ac,0xce7db43a,
+ 0xdf63184a,0x1ae58231,0x51976033,0x5362457f,
+ 0x64b1e077,0x6bbb84ae,0x81fe1ca0,0x08f9942b,
+ 0x48705868,0x458f19fd,0xde94876c,0x7b52b7f8,
+ 0x73ab23d3,0x4b72e202,0x1fe3578f,0x55662aab,
+ 0xebb20728,0xb52f03c2,0xc5869a7b,0x37d3a508,
+ 0x2830f287,0xbf23b2a5,0x0302ba6a,0x16ed5c82,
+ 0xcf8a2b1c,0x79a792b4,0x07f3f0f2,0x694ea1e2,
+ 0xda65cdf4,0x0506d5be,0x34d11f62,0xa6c48afe,
+ 0x2e349d53,0xf3a2a055,0x8a0532e1,0xf6a475eb,
+ 0x830b39ec,0x6040aaef,0x715e069f,0x6ebd5110,
+ 0x213ef98a,0xdd963d06,0x3eddae05,0xe64d46bd,
+ 0x5491b58d,0xc471055d,0x06046fd4,0x5060ff15,
+ 0x981924fb,0xbdd697e9,0x4089cc43,0xd967779e,
+ 0xe8b0bd42,0x8907888b,0x19e7385b,0xc879dbee,
+ 0x7ca1470a,0x427ce90f,0x84f8c91e,0x00000000,
+ 0x80098386,0x2b3248ed,0x111eac70,0x5a6c4e72,
+ 0x0efdfbff,0x850f5638,0xae3d1ed5,0x2d362739,
+ 0x0f0a64d9,0x5c6821a6,0x5b9bd154,0x36243a2e,
+ 0x0a0cb167,0x57930fe7,0xeeb4d296,0x9b1b9e91,
+ 0xc0804fc5,0xdc61a220,0x775a694b,0x121c161a,
+ 0x93e20aba,0xa0c0e52a,0x223c43e0,0x1b121d17,
+ 0x090e0b0d,0x8bf2adc7,0xb62db9a8,0x1e14c8a9,
+ 0xf1578519,0x75af4c07,0x99eebbdd,0x7fa3fd60,
+ 0x01f79f26,0x725cbcf5,0x6644c53b,0xfb5b347e,
***The diff for this file has been truncated for email.***
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/symmetric/Des.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,425 @@
+/*
+ * Copyright (c) 2011, The Caffeine-hx project contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+/*
+* DESKey
+*
+* Derived from:
+* An Actionscript 3 implementation of the Data Encryption
Standard (DES)
+* Copyright (c) 2007 Henri Torgemane
+* Which in turn derives from:
+* The Bouncy Castle Crypto package,
+* Copyright (c) 2000-2004 The Legion Of The Bouncy Castle
+* (http://www.bouncycastle.org)
+*
+* See LICENSE.txt for full license information.
+*/
+package chx.crypt.symmetric;
+import I32;
+
+/**
+* DES Key. In neko requires the openssl ndll.
+**/
+class Des implements IBlockCipher
+{
+ public var blockSize(__getBlockSize,null) : Int;
+ #if (neko || useOpenSSL)
+ var key:Dynamic;
+ #else
+ /*
+ * what follows is mainly taken from "Applied Cryptography", by Bruce
+ * Schneier, however it also bears great resemblance to Richard
+ * Outerbridge's D3DES...
+ */
+ private static var Df_Key:Array<Int32> = [ 0x01, 0x23, 0x45, 0x67, 0x89,
0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
+ 0x10, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 ];
+
+ private static var bytebit:Array<Int32> = [ 128, 64, 32, 16, 8, 4, 2, 1 ];
+
+ private static var bigbyte:Array<Int32> = [ 0x800000, 0x400000, 0x200000,
0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 0x8000,
+ 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20,
0x10, 0x8, 0x4, 0x2, 0x1 ];
+
+ /*
+ * Use the key schedule specified in the Standard (ANSI X3.92-1981).
+ */
+
+ private static var pc1:Array<Int32> = [ 56, 48, 40, 32, 24, 16, 8, 0, 57,
49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2,
+ 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 60, 52, 44, 36, 28, 20, 12,
+ 4, 27, 19, 11, 3 ];
+
+ private static var totrot:Array<Int32> = [ 1, 2, 4, 6, 8, 10, 12, 14, 15,
17, 19, 21, 23, 25, 27, 28 ];
+
+ private static var pc2:Array<Int32> = [ 13, 16, 10, 23, 0, 4, 2, 27, 14,
5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40,
+ 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52,
45, 41, 49, 35, 28, 31 ];
+
+ private static var SP1:Array<Int32> = [ 0x01010400, 0x00000000,
0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004,
+ 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
0x01000404, 0x01010004, 0x01000000, 0x00000004,
+ 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400,
0x01010000, 0x01010000, 0x01000404, 0x00010004,
+ 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404,
0x00010404, 0x01000000, 0x00010000, 0x01010404,
+ 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000,
0x00000400, 0x01010004, 0x00010000, 0x00010400,
+ 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404,
+ 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404,
0x01000400, 0x01000400, 0x00000000, 0x00010004,
+ 0x00010400, 0x00000000, 0x01010004 ];
+
+ private static var SP2:Array<Int32> = [ 0x80108020, 0x80008000,
0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020,
+ 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000,
0x80008000, 0x00100000, 0x00000020, 0x80100020,
+ 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000,
0x00008000, 0x00108020, 0x80100000, 0x00100020,
+ 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000,
0x80100000, 0x00008020, 0x00000000, 0x00108020,
+ 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000,
0x00008000, 0x80100000, 0x80008000, 0x00000020,
+ 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020,
+ 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000,
0x00000000, 0x80008000, 0x00008020, 0x80000000,
+ 0x80100020, 0x80108020, 0x00108000 ];
+
+ private static var SP3:Array<Int32> = [ 0x00000208, 0x08020200,
0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208,
+ 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000,
0x08020208, 0x00020008, 0x08020000, 0x00000208,
+ 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200,
0x08020000, 0x08020008, 0x00020208, 0x08000208,
+ 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208,
0x00000200, 0x08000000, 0x08020200, 0x08000000,
+ 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200,
0x00000000, 0x00000200, 0x00020008, 0x08020208,
+ 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208,
+ 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000,
0x08000208, 0x00000208, 0x08020000, 0x00020208,
+ 0x00000008, 0x08020008, 0x00020200 ];
+
+ private static var SP4:Array<Int32> = [ 0x00802001, 0x00002081,
0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001,
+ 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00800080, 0x00800001,
+ 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080,
0x00800000, 0x00002001, 0x00002080, 0x00800081,
+ 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080,
0x00802081, 0x00000081, 0x00800080, 0x00800001,
+ 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000,
0x00802000, 0x00002080, 0x00800080, 0x00800081,
+ 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000,
+ 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001,
0x00002080, 0x00800000, 0x00802001, 0x00000080,
+ 0x00800000, 0x00002000, 0x00802080 ];
+
+ private static var SP5:Array<Int32> = [ 0x00000100, 0x02080100,
0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000,
+ 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100,
0x42000100, 0x42080000, 0x00080100, 0x40000000,
+ 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100,
0x42080100, 0x42080100, 0x02000100, 0x42080000,
+ 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000,
0x42000000, 0x00080100, 0x00080000, 0x42000100,
+ 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100,
0x40080100, 0x02000100, 0x40000000, 0x42080000,
+ 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
0x42080100, 0x00080100, 0x42000000, 0x42080100,
+ 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100,
0x02000100, 0x40000100, 0x00080000, 0x00000000,
+ 0x40080000, 0x02080100, 0x40000100 ];
+
+ private static var SP6:Array<Int32> = [ 0x20000010, 0x20400000,
0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010,
+ 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010,
0x00400010, 0x20004000, 0x20000000, 0x00004010,
+ 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000,
0x20004010, 0x00000010, 0x20400010, 0x20400010,
+ 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000,
0x20404000, 0x20000000, 0x20004000, 0x00000010,
+ 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010,
0x20000010, 0x00400000, 0x20004000, 0x20000000,
+ 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
0x00404010, 0x20404000, 0x00000000, 0x20400010,
+ 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000,
0x00400010, 0x20004010, 0x00000000, 0x20404000,
+ 0x20000000, 0x00400010, 0x20004010 ];
+
+ private static var SP7:Array<Int32> = [ 0x00200000, 0x04200002,
0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802,
+ 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002,
0x00000002, 0x04000000, 0x04200002, 0x00000802,
+ 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002,
0x04200000, 0x04200800, 0x00200002, 0x04200000,
+ 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002,
0x04000000, 0x00200800, 0x04000000, 0x00200800,
+ 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002,
0x00000002, 0x00200002, 0x04000000, 0x04000800,
+ 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
0x00000802, 0x04000002, 0x04200802, 0x04200000,
+ 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000,
0x00200802, 0x04200000, 0x00000800, 0x04000002,
+ 0x04000800, 0x00000800, 0x00200002 ];
+
+ private static var SP8:Array<Int32> = [ 0x10001040, 0x00001000,
0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040,
+ 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000,
0x10041000, 0x00041040, 0x00001000, 0x00000040,
+ 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000,
0x00040040, 0x10040040, 0x10041000, 0x00001040,
+ 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000,
0x00041040, 0x00040000, 0x00041040, 0x00040000,
+ 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000,
0x00041040, 0x10001000, 0x00000040, 0x10000040,
+ 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
0x00000000, 0x10041040, 0x00040040, 0x10000040,
+ 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040,
0x00041000, 0x00041000, 0x00001040, 0x00001040,
+ 0x00040040, 0x10000000, 0x10041000 ];
+
+ var key:Bytes;
+ var encKey:Array<Int32>;
+ var decKey:Array<Int32>;
+ #end
+
+ public function new(key:Bytes) {
+ if(key.length < 8)
+ throw new chx.lang.OutsideBoundsException("Must be 8 bytes of key
data");
+ #if (neko || useOpenSSL)
+ this.key = des_create_key(key.sub(0,8).getData());
+ #else
+ this.key = key;
+ this.encKey = generateWorkingKey(true, key, 0);
+ this.decKey = generateWorkingKey(false, key, 0);
+ #end
+ }
+
+ public function getBlockSize():Int
+ {
+ return 8;
+ }
+
+ function __getBlockSize():Int {
+ return 8;
+ }
+
+ public function decryptBlock(block:Bytes):Bytes
+ {
+ #if (neko || useOpenSSL)
+ return Bytes.ofData(des_decrypt_block(key, block.getData()));
+ #else
+ var outBlock = Bytes.alloc(block.length);
+ desFunc(decKey, block, 0, outBlock, 0);
+ return outBlock;
+ #end
+ }
+
+ public function dispose():Void
+ {
+ #if (neko || useOpenSSL)
+ des_destroy_key(key);
+ #else
+ for (i in 0...encKey.length) { encKey[i]=0; }
+ for (i in 0...decKey.length) { decKey[i]=0; }
+ encKey=null;
+ decKey=null;
+ for (i in 0...key.length) { key.set(i, 0); }
+ key = null;
+ #end
+ }
+
+ public function encryptBlock(block:Bytes):Bytes
+ {
+ #if (neko || useOpenSSL)
+ return Bytes.ofData(des_encrypt_block(key, block.getData()));
+ #else
+ var outBlock = Bytes.alloc(block.length);
+ desFunc(encKey, block, 0, outBlock, 0);
+ return outBlock;
+ #end
+ }
+
+ #if !(neko || useOpenSSL)
+ /**
+ * generate an integer based working key based on our secret key and what
we
+ * processing we are planning to do.
+ *
+ * Acknowledgements for this routine go to James Gillogly & Phil Karn.
+ */
+ function generateWorkingKey(encrypting:Bool, key:Bytes,
off:Int):Array<Int32>
+ {
+ //int[] newKey = new int[32];
+ var newKey:Array<Int32> = [];
+ //boolean[] pc1m = new boolean[56], pcr = new boolean[56];
+ var pc1m:Array<Bool> = new Array();
+ var pcr:Array<Bool> = new Array();
+
+ var l:Int;
+
+ for (j in 0...56)
+ {
+ l = pc1[j];
+ pc1m[j] = ((key.get(off + (l >>> 3)) & bytebit[l & 07]) != 0);
+ }
+
+ for (i in 0...16)
+ {
+ var m:Int;
+ var n:Int;
+
+ if (encrypting)
+ {
+ m = i << 1;
+ }
+ else
+ {
+ m = (15 - i) << 1;
+ }
+
+ n = m + 1;
+ newKey[m] = newKey[n] = 0;
+
+ for (j in 0...28)
+ {
+ l = j + totrot[i];
+ if (l < 28)
+ {
+ pcr[j] = pc1m[l];
+ }
+ else
+ {
+ pcr[j] = pc1m[l - 28];
+ }
+ }
+
+ for (j in 28...56)
+ {
+ l = j + totrot[i];
+ if (l < 56)
+ {
+ pcr[j] = pc1m[l];
+ }
+ else
+ {
+ pcr[j] = pc1m[l - 28];
+ }
+ }
+
+ for (j in 0...24)
+ {
+ if (pcr[pc2[j]])
+ {
+ newKey[m] |= bigbyte[j];
+ }
+
+ if (pcr[pc2[j + 24]])
+ {
+ newKey[n] |= bigbyte[j];
+ }
+ }
+ }
+
+ //
+ // store the processed key
+ //
+ var i:Int = 0;
+ while(i < 32)
+ {
+ var i1:Int32;
+ var i2:Int32;
+
+ i1 = newKey[i];
+ i2 = newKey[i + 1];
+
+ newKey[i] = ((i1 & 0x00fc0000) << 6) | ((i1 & 0x00000fc0) << 10) | ((i2
& 0x00fc0000) >>> 10)
+ | ((i2 & 0x00000fc0) >>> 6);
+
+ newKey[i + 1] = ((i1 & 0x0003f000) << 12) | ((i1 & 0x0000003f) << 16) |
((i2 & 0x0003f000) >>> 4)
+ | (i2 & 0x0000003f);
+ i += 2;
+ }
+ return newKey;
+ }
+
+ /**
+ * the DES engine.
+ */
+ private function desFunc(wKey:Array<Int32>, inp:Bytes, inOff:Int,
out:Bytes, outOff:Int):Void
+ {
+ var work:Int32 = 0;
+ var right:Int32 = 0;
+ var left:Int32 = 0;
+
+ left = (inp.get(inOff + 0) & 0xff) << 24;
+ left |= (inp.get(inOff + 1) & 0xff) << 16;
+ left |= (inp.get(inOff + 2) & 0xff) << 8;
+ left |= (inp.get(inOff + 3) & 0xff);
+
+ right = (inp.get(inOff + 4) & 0xff) << 24;
+ right |= (inp.get(inOff + 5) & 0xff) << 16;
+ right |= (inp.get(inOff + 6) & 0xff) << 8;
+ right |= (inp.get(inOff + 7) & 0xff);
+
+ work = ((left >>> 4) ^ right) & 0x0f0f0f0f;
+ right ^= work;
+ left ^= (work << 4);
+ work = ((left >>> 16) ^ right) & 0x0000ffff;
+ right ^= work;
+ left ^= (work << 16);
+ work = ((right >>> 2) ^ left) & 0x33333333;
+ left ^= work;
+ right ^= (work << 2);
+ work = ((right >>> 8) ^ left) & 0x00ff00ff;
+ left ^= work;
+ right ^= (work << 8);
+ right = ((right << 1) | ((right >>> 31) & 1)) & 0xffffffff;
+ work = (left ^ right) & 0xaaaaaaaa;
+ left ^= work;
+ right ^= work;
+ left = ((left << 1) | ((left >>> 31) & 1)) & 0xffffffff;
+
+ for (round in 0...8)
+ {
+ var fval:Int32 = 0;
+
+ work = (right << 28) | (right >>> 4);
+ work ^= wKey[round * 4 + 0];
+ fval = SP7[work & 0x3f];
+ fval |= SP5[(work >>> 8) & 0x3f];
+ fval |= SP3[(work >>> 16) & 0x3f];
+ fval |= SP1[(work >>> 24) & 0x3f];
+ work = right ^ wKey[round * 4 + 1];
+ fval |= SP8[work & 0x3f];
+ fval |= SP6[(work >>> 8) & 0x3f];
+ fval |= SP4[(work >>> 16) & 0x3f];
+ fval |= SP2[(work >>> 24) & 0x3f];
+ left ^= fval;
+ work = (left << 28) | (left >>> 4);
+ work ^= wKey[round * 4 + 2];
+ fval = SP7[work & 0x3f];
+ fval |= SP5[(work >>> 8) & 0x3f];
+ fval |= SP3[(work >>> 16) & 0x3f];
+ fval |= SP1[(work >>> 24) & 0x3f];
+ work = left ^ wKey[round * 4 + 3];
+ fval |= SP8[work & 0x3f];
+ fval |= SP6[(work >>> 8) & 0x3f];
+ fval |= SP4[(work >>> 16) & 0x3f];
+ fval |= SP2[(work >>> 24) & 0x3f];
+ right ^= fval;
+ }
+
+ right = (right << 31) | (right >>> 1);
+ work = (left ^ right) & 0xaaaaaaaa;
+ left ^= work;
+ right ^= work;
+ left = (left << 31) | (left >>> 1);
+ work = ((left >>> 8) ^ right) & 0x00ff00ff;
+ right ^= work;
+ left ^= (work << 8);
+ work = ((left >>> 2) ^ right) & 0x33333333;
+ right ^= work;
+ left ^= (work << 2);
+ work = ((right >>> 16) ^ left) & 0x0000ffff;
+ left ^= work;
+ right ^= (work << 16);
+ work = ((right >>> 4) ^ left) & 0x0f0f0f0f;
+ left ^= work;
+ right ^= (work << 4);
+
+ out.set(outOff + 0, ((right >>> 24) & 0xff));
+ out.set(outOff + 1, ((right >>> 16) & 0xff));
+ out.set(outOff + 2, ((right >>> 8) & 0xff));
+ out.set(outOff + 3, (right & 0xff));
+ out.set(outOff + 4, ((left >>> 24) & 0xff));
+ out.set(outOff + 5, ((left >>> 16) & 0xff));
+ out.set(outOff + 6, ((left >>> 8) & 0xff));
+ out.set(outOff + 7, (left & 0xff));
+ }
+ #end
+
+ public function toString():String {
+ return "DES";
+ }
+
+
+#if (neko || useOpenSSL)
+ public static function __init__()
+ {
+ chx.Lib.initDll("openssl");
+ }
+
+ private static var des_create_key =
chx.Lib.load("openssl","des_create_key",1);
+ private static var des_destroy_key =
chx.Lib.load("openssl","des_create_key",1);
+ private static var des_encrypt_block =
chx.Lib.load("openssl","des_encrypt_block",2);
+ private static var des_decrypt_block =
chx.Lib.load("openssl","des_decrypt_block",2);
+#end
+}
+
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/symmetric/TripleDes.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,146 @@
+/*
+ * Copyright (c) 2011, The Caffeine-hx project contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+/*
+ * TripleDESKey
+ *
+ * An Actionscript 3 implementation of Triple DES
+ * Copyright (c) 2007 Henri Torgemane
+ *
+ * Derived from:
+ * The Bouncy Castle Crypto package,
+ * Copyright (c) 2000-2004 The Legion Of The Bouncy Castle
+ * (http://www.bouncycastle.org)
+ *
+ * See LICENSE.txt for full license information.
+ */
+package chx.crypt.symmetric;
+import I32;
+
+/**
+* This supports 2TDES and 3TDES.
+* If the key passed is 128 bits, 2TDES is used.
+* If the key has 192 bits, 3TDES is used.
+* Other key lengths give "undefined" results.
+*/
+class TripleDes extends Des
+{
+ #if (neko || cpp)
+ private var key2:Dynamic;
+ private var key3:Dynamic;
+ #else
+ private var encKey2:Array<Int32>;
+ private var encKey3:Array<Int32>;
+ private var decKey2:Array<Int32>;
+ private var decKey3:Array<Int32>;
+ #end
+
+ public function new(key:Bytes)
+ {
+ if(key.length < 16)
+ throw new chx.lang.OutsideBoundsException("Must be at least 16 bytes of
key data");
+ super(key);
+ #if (neko || cpp)
+ this.key2 = des_create_key(key.sub(8,8).getData());
+ if(key.length > 16)
+ this.key3 = des_create_key(key.sub(16,8).getData());
+ else
+ this.key3 = des_create_key(key.sub(0,8).getData());
+ #else
+ encKey2 = generateWorkingKey(false, key, 8);
+ decKey2 = generateWorkingKey(true, key, 8);
+ if (key.length>16) {
+ encKey3 = generateWorkingKey(true, key, 16);
+ decKey3 = generateWorkingKey(false, key, 16);
+ } else {
+ encKey3 = encKey;
+ decKey3 = decKey;
+ }
+ #end
+ }
+
+ public override function dispose():Void
+ {
+ super.dispose();
+ #if (neko || cpp)
+ des_destroy_key(key2);
+ des_destroy_key(key3);
+ #else
+ var i:Int = 0;
+ if (encKey2!=null) {
+ for (i in 0...encKey2.length) { encKey2[i]=0; }
+ encKey2=null;
+ }
+ if (encKey3!=null) {
+ for (i in 0...encKey3.length) { encKey3[i]=0; }
+ encKey3=null;
+ }
+ if (decKey2!=null) {
+ for (i in 0...decKey2.length) { decKey2[i]=0; }
+ decKey2=null;
+ }
+ if (decKey3!=null) {
+ for (i in 0...decKey3.length) { decKey3[i]=0; }
+ decKey3=null;
+ }
+ #end
+ }
+
+ public override function encryptBlock(block:Bytes):Bytes
+ {
+ #if (neko || cpp)
+ return Bytes.ofData(des3_encrypt_block(key, key2, key3,
block.getData()));
+ #else
+ var outBlock = Bytes.alloc(block.length);
+ desFunc(encKey, block, 0, outBlock, 0);
+ desFunc(encKey2, outBlock, 0, outBlock, 0);
+ desFunc(encKey3, outBlock, 0, outBlock, 0);
+ return outBlock;
+ #end
+ }
+
+ public override function decryptBlock(block:Bytes):Bytes
+ {
+ #if (neko || cpp)
+ return Bytes.ofData(des3_decrypt_block(key, key2, key3,
block.getData()));
+ #else
+ var outBlock = Bytes.alloc(block.length);
+ desFunc(decKey3, block, 0, outBlock, 0);
+ desFunc(decKey2, outBlock, 0, outBlock, 0);
+ desFunc(decKey, outBlock, 0, outBlock, 0);
+ return outBlock;
+ #end
+ }
+
+ public override function toString():String {
+ return "3des";
+ }
+
+#if (neko || cpp)
+ private static var des_create_key =
chx.Lib.load("openssl","des_create_key",1);
+ private static var des_destroy_key =
chx.Lib.load("openssl","des_create_key",1);
+ private static var des3_encrypt_block =
chx.Lib.load("openssl","des3_encrypt_block",4);
+ private static var des3_decrypt_block =
chx.Lib.load("openssl","des3_decrypt_block",4);
+#end
+}
=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/symmetric/XXTea.hx Sat Feb 25 21:43:48 2012
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2008, The Caffeine-hx project contributors
+ * Original author : Russell Weir
+ * Contributors:
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package chx.crypt.symmetric;
+
+import I32;
+
+class XXTea implements IBlockCipher {
+#if (neko || useNCrypt)
+ var k : Void;
+#else
+ var k : Array<Int>; // 16 bytes of key material
+#end
+ public var blockSize(__getBlockSize,null) : Int;
+
+ public function new(key : Bytes) {
+ var l = key.length;
+ if(l > 16)
+ l = 16;
+ var m = BytesUtil.bytesToInt32LE(
+ BytesUtil.nullPad(key.sub(0,l), 16)
+ );
+#if (neko || useNCrypt)
+ k = xxtea_create_key(I32.mkNekoArray(m));
+#else
+ k = I32.toNativeArray(m);
+#end
+ blockSize = 8;
+ }
+
+ public function toString() : String {
+ return "xxtea";
+ }
+
+ function __getBlockSize() : Int {
+ return this.blockSize;
+ }
+
+ public function setBlocksize( i : Int ) : Int {
+ if(i == 0 || i % 4 != 0)
+ throw "xxtea: block size must be multiple of 4";
+ blockSize = i;
+ return i;
+ }
+
+ public function encryptBlock(plaintext : Bytes) : Bytes {
+ if (plaintext.length == 0) return BytesUtil.EMPTY;
+#if (neko || useNCrypt)
+ var v : Array<Int32> = BytesUtil.bytesToInt32LE(plaintext);
+ var n = v.length;
+ if (n == 1)
+ v[n++] = Int32.ofInt(0);
+ var rv = xxtea_encrypt_block(
+ I32.mkNekoArray(v),
+ n,
+ k);
+ return Bytes.ofData(rv);
+#else
+ var v : Array<Int> =
I32.toNativeArray(BytesUtil.bytesToInt32LE(plaintext));
+ var n = v.length;
+ if (n == 1)
+ v[n++] = 0;
+
+ var delta = 0x9e3779B9;
+ var e : Int;
+ var mx : Int;
+ var q = Std.int(6 + 52/n);
+ var y = v[0];
+ var z = v[n-1];
+ var sum = 0;
+
+ while (q-- > 0) {
+ sum += delta;
+ e = sum >>> 2 & 3;
+ //for (p=0; p<n-1; p++) y = v[p+1], z = v[p] += MX;
+ var p = 0;
+ while(p < n-1) {
+ y = v[(p+1)];
+ mx = (((z>>>5)^(y<<2)) + ((y>>>3)^(z<<4))) ^ ((sum^y) +
(k[(p&3)^e]^z));
+ z = v[p] += mx;
+ p ++;
+ }
+ y = v[0];
+ z = v[n-1] += (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3^e]^z);
+ }
+ return I32.packLE(cast v);
+#end
+ }
+
+ public function decryptBlock(ciphertext : Bytes) : Bytes
+ {
+ if (ciphertext.length == 0) return BytesUtil.EMPTY;
+#if (neko || useNCrypt)
+ var v = BytesUtil.bytesToInt32LE(ciphertext);
+ var n = v.length;
+ var rv = xxtea_decrypt_block(
+ I32.mkNekoArray(v),
+ n,
+ k);
+ return Bytes.ofData(rv);
+#else
+ var v : Array<Int> =
I32.toNativeArray(BytesUtil.bytesToInt32LE(ciphertext));
+ var n = v.length;
+
+ var delta = 0x9e3779B9;
+ var e : Int;
+ var mx : Int;
+ var q : Int = Std.int(6 + 52/n);
+ var y = v[0];
+ var z = v[n-1];
+ var sum = q * delta;
+
+ while (sum != 0) {
+ e = sum >>> 2 & 3;
+ var p = n - 1;
+ while(p > 0 ) {
+ z = v[p-1];
+ //mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3^e]^z);
+ mx = (((z>>>5)^(y<<2)) + ((y>>>3)^(z<<4))) ^ ((sum^y) +
(k[(p&3)^e]^z));
+ y = v[p] -= mx;
+ p--;
+ }
+ z = v[n-1];
+ y = v[0] -= (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3^e]^z);
+ sum -= delta;
+ }
+ return I32.packLE(cast v);
+#end
+ }
+
+
+#if (neko || useNCrypt)
+ private static var xxtea_create_key =
chx.Lib.load("ncrypt","xxtea_create_key",1);
+ private static var xxtea_encrypt_block =
chx.Lib.load("ncrypt","xxtea_encrypt_block",3);
+ private static var xxtea_decrypt_block =
chx.Lib.load("ncrypt","xxtea_decrypt_block",3);
+#end
+}
+
=======================================
--- /trunk/ext3/chx/crypt/Aes.hx Sun Feb 12 19:59:39 2012
+++ /dev/null
@@ -1,1376 +0,0 @@
-/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-#if (neko || useNCrypt)
-private typedef Keycontext = Dynamic;
-#else
-private typedef Keycontext = {
- var rounds : Int;
- var rk : Array<Array<Int>>;
-};
-#end
-
-class Aes implements IBlockCipher {
- public static var AES_BLOCK_SIZE : Int = 16;
-
- public var keylen(default,__setKeylen) : Int;
- public var passphrase(default,__setPassphrase) : Bytes;
- public var blockSize(__getBlockSize,null) : Int;
- //TODO: neko needs to respect this flag
- var initialized : Bool;
- var encKey : Keycontext;
- var decKey : Keycontext;
-
-
- public function new(keylen : Int, phrase:Bytes) {
- __setKeylen(keylen);
- __setPassphrase(phrase);
- blockSize = AES_BLOCK_SIZE;
- initialized = true;
- initKeys();
- }
-
- public function toString() : String {
- return "aes-" + keylen;
- }
-
-
- function __getBlockSize() : Int {
- return this.blockSize;
- }
-
- function initKeys() {
- encKey = makeKey(true, keylen, passphrase);
- decKey = makeKey(false, keylen, passphrase, encKey);
- }
-
- public function encryptBlock( block : Bytes ) : Bytes {
- if(block.length != blockSize)
- throw("bad block size");
- #if (neko || useNCrypt)
- var rv = Bytes.ofData(aes_encrypt_block( encKey, block.getData()));
- if(blockSize != rv.length)
- throw("returned buffer is " + rv.length + " bytes");
- return rv;
- #else
- return AESencrypt( block, encKey );
- #end
- }
-
- public function decryptBlock( block : Bytes ) : Bytes {
- #if (neko || useNCrypt)
- var rv = Bytes.ofData(aes_decrypt_block( decKey, block.getData()));
- if(blockSize != rv.length)
- throw("returned buffer is " + rv.length + " bytes");
- return rv;
- #else
- return AESdecrypt( block, decKey );
- #end
- }
-
- /**
- * Transform buffer to a key. If making a decryption key, and the encrypt
key
- * for the same buffer and keylen exists, it may be passed in as context to
- * reduce key generation time.
- *
- * @param encrypt True for an encryption key, false for decrypt
- * @param keylen length of key to generate
- * @param buf Bytes buffer of key material
- * @param context Optional encrypt key for generating decrypt key
- **/
- static function makeKey( encrypt : Bool, keylen : Int, buf :
Bytes, ?context : Keycontext ) : Keycontext
- {
-#if (neko || useNCrypt)
- return aes_create_key(encrypt, keylen, buf.getData());
-#else
- if(encrypt)
- return keyExpansionEnc( buf, keylen );
- return keyExpansionDec( buf, keylen, context );
-#end
- }
-
-/*
- override public function encrypt(msg : String) {
- var rv;
- switch(mode) {
-#if (neko || useNCrypt)
- case ECB:
- rv = new String(naes_ecb_encrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
- case CBC:
- rv = new String(naes_cbc_encrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
-#else
- case ECB:
- rv = ecb_encrypt(msg);
- case CBC:
- rv = cbc_encrypt(msg);
-#end
- default:
- modeError();
- }
- if(rv == null)
- return "";
- return rv;
- }
-
- override public function decrypt(msg : String) {
- var rv;
- switch(mode) {
-#if (neko || useNCrypt)
- case ECB:
- rv = new String(naes_ecb_decrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
- case CBC:
- rv = new String(naes_cbc_decrypt(untyped passphrase.__s, untyped
msg.__s, keylen));
-#else
- case ECB:
- rv = ecb_decrypt(msg);
- case CBC:
- rv = cbc_decrypt(msg);
-#end
- default:
- modeError();
- }
- if(rv == null)
- return "";
- return rv;
- }
-*/
-
- private function __setKeylen(len : Int) : Int {
- if(len != 128 && len != 192 && len != 256)
- throw "Invalid key length";
- keylen = len;
- if(initialized)
- initKeys();
- return len;
- }
-
- private function __setPassphrase(buf : Bytes) {
- passphrase = buf;
- if(initialized)
- initKeys();
- return buf;
- }
-
-#if !(neko || useNCrypt)
-
- static var maxkc : Int = 8;
- static var maxrk : Int = 14;
-
-/*
- public function ecb_encrypt( msg : String ) : String {
- var buf : String;
- if(!usePadding)
- buf = ByteStringTools.nullPadString(msg, AES_BLOCK_SIZE);
- else {
- var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
- buf = pkcs5.pad(msg);
- }
- var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
- var offset : Int = 0;
- var sb = new StringBuf();
- for (i in 0...numBlocks) {
- var rv = AESencrypt(buf.substr(offset, AES_BLOCK_SIZE), encKey);
- offset += AES_BLOCK_SIZE;
- sb.add(rv);
- }
- return sb.toString();
- }
-
- public function cbc_encrypt( msg : String ) : String {
- var buf : String;
- if(!usePadding)
- buf = ByteStringTools.nullPadString(msg, AES_BLOCK_SIZE);
- else {
- var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
- buf = pkcs5.pad(msg);
- }
- var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
- var offset : Int = 0;
- var sb = new StringBuf();
- var iv = ByteStringTools.nullString( AES_BLOCK_SIZE);
-
- for (i in 0...numBlocks) {
- var sb2 = new StringBuf();
- for(x in 0...blockSize) {
- sb2.addChar( buf.charCodeAt(offset + x) ^ iv.charCodeAt(x));
- }
- var outBuffer = AESencrypt(sb2.toString(), encKey);
- sb.add(outBuffer);
- iv = outBuffer;
- offset += AES_BLOCK_SIZE;
- }
- return sb.toString();
- }
-
- public function ecb_decrypt( buf : String ) : String {
- if(buf.length % AES_BLOCK_SIZE != 0)
- throw "Invalid message length";
- var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
- var offset : Int = 0;
- var sb = new StringBuf();
- for (i in 0...numBlocks) {
- var rv = AESdecrypt(buf.substr(offset, AES_BLOCK_SIZE), decKey);
- offset += AES_BLOCK_SIZE;
- sb.add(rv);
- }
- if(usePadding) {
- var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
- return pkcs5.unpad(sb.toString());
- }
- return sb.toString();
- }
-
- public function cbc_decrypt( buf : String ) : String {
- if(buf.length % AES_BLOCK_SIZE != 0)
- throw "Invalid message length";
- var numBlocks = Std.int(buf.length/AES_BLOCK_SIZE);
- var iv = ByteStringTools.nullString( AES_BLOCK_SIZE);
-
- var i = numBlocks;
- var offset : Int = 0;
- var sb = new StringBuf();
-
- for (i in 0...numBlocks) {
- var rv : String = AESdecrypt(
- buf.substr(offset, AES_BLOCK_SIZE),
- decKey
- );
- var sb2 = new StringBuf();
- for(x in 0...blockSize) {
- sb2.addChar( rv.charCodeAt(x) ^ iv.charCodeAt(x));
- }
- sb.add(sb2.toString());
- iv = buf.substr(offset, AES_BLOCK_SIZE);
- offset += AES_BLOCK_SIZE;
- }
- if(usePadding) {
- var pkcs5 = new PKCS5(AES_BLOCK_SIZE);
- return pkcs5.unpad(sb.toString());
- }
- return sb.toString();
- }
-*/
-
-
- /* Adaptations from Javascript source
- * Rijndael (AES) Encryption
- * Copyright 2005 Herbert Hanewinkel, www.haneWIN.de
- * version 1.1, check www.haneWIN.de for the latest version
-
- * This software is provided as-is, without express or implied warranty.
- * Permission to use, copy, modify, distribute or sell this software, with
or
- * without fee, for any purpose and by any individual or organization, is
hereby
- * granted, provided that the above copyright notice and this paragraph
appear
- * in all copies. Distribution as a part of an application or binary must
- * include the above copyright notice in the documentation and/or other
- * materials provided with the application or distribution.
- */
- // http://www.hanewin.net/encrypt/aes/aes.htm
- static function keyExpansionEnc( key : Bytes, keylen : Int)
- {
- var i:Int, j:Int, r:Int, t:Int;
- var keybytes : Int;
- var rounds : Int;
- var kc : Int;
-
- var keySched=new Array<Array<Int>>(); // maxrk+1
- var k=new Array<Int>(); // maxkc
- var tk=new Array<Int>(); // maxkc
- var rconpointer=0;
-
- switch(keylen) {
- case 128:
- keybytes = 16;
- rounds = 10;
- kc = 4;
- case 192:
- keybytes = 24;
- rounds = 12;
- kc = 6;
- case 256:
- keybytes = 32;
- rounds = 14;
- kc = 8;
- default:
- throw "Invalid keylen";
- }
-
- for(i in 0...maxrk+1)
- keySched[i]=new Array();
-
- i = 0;
- for(j in 0...keybytes) {
- k[j] = key.get(i) | (key.get(i+1)<<8)
- | (key.get(i+2)<<16) | (key.get(i+3)<<24);
- i += 4;
- }
-
- j = kc - 1;
- while(j >= 0) {
- tk[j] = k[j];
- j--;
- }
-
- r=0;
- t=0;
- j=0;
- while( (j<kc) && (r<rounds+1) ) {
- while( (j<kc)&&(t<4) )
- {
- keySched[r][t]=tk[j];
- j++;
- t++;
- }
- if( t == 4 )
- {
- r++;
- t=0;
- }
- }
-
- while(r<rounds+1)
- {
- var temp = tk[kc-1];
-
- tk[0] ^= S[B1(temp)] | (S[B2(temp)]<<8) | (S[B3(temp)]<<16) |
(S[B0(temp)]<<24);
- tk[0] ^= Rcon[rconpointer++];
-
- if(kc != 8)
- {
- for(j in 1...kc)
- tk[j] ^= tk[j-1];
- }
- else
- {
- var iKc2 = Std.int(kc/2);
- for(j in 1...iKc2)
- tk[j] ^= tk[j-1];
- temp = tk[Std.int(iKc2-1)];
- tk[iKc2] ^= S[B0(temp)] | (S[B1(temp)]<<8) | (S[B2(temp)]<<16) |
(S[B3(temp)]<<24);
-
- for(j in iKc2 + 1 ... kc)
- tk[j] ^= tk[j-1];
- }
-
- j = 0;
- while( (j<kc)&&(r<rounds+1) )
- {
- while( (j<kc)&&(t<4) )
- {
- keySched[r][t]=tk[j];
- j++;
- t++;
- }
- if(t == 4)
- {
- r++;
- t=0;
- }
- }
- }
- return { rounds : rounds, rk : keySched };
- } // keyExpansionEnc
-
- static function keyExpansionDec(key : Bytes, keylen : Int, ?context :
Keycontext) {
- var w;
- var rk2 = new Array<Array<Int>>(); // maxrk+1
- var ctx : Keycontext;
- if(context == null)
- ctx = keyExpansionEnc(key, keylen);
- else
- ctx = context;
- var rounds=ctx.rounds;
-
- for(r in 0...maxrk+1)
- {
- rk2[r]=new Array();
- rk2[r][0] = ctx.rk[r][0];
- rk2[r][1] = ctx.rk[r][1];
- rk2[r][2] = ctx.rk[r][2];
- rk2[r][3] = ctx.rk[r][3];
- }
-
- for(r in 1...rounds)
- {
- w=rk2[r][0]; rk2[r][0] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
- w=rk2[r][1]; rk2[r][1] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
- w=rk2[r][2]; rk2[r][2] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
- w=rk2[r][3]; rk2[r][3] = U1[B0(w)] ^ U2[B1(w)] ^ U3[B2(w)] ^ U4[B3(w)];
- }
- return { rounds : rounds, rk : rk2 };
- }
-
- function AESencrypt(block : Bytes, ctx : Keycontext)
- {
- var r;
- var t0,t1,t2,t3;
-
- var b = I32.unpackLE(block);
- var rounds = ctx.rounds;
- var b0 = b[0];
- var b1 = b[1];
- var b2 = b[2];
- var b3 = b[3];
-
- for(r in 0 ... rounds-1)
- {
- t0 = b0 ^ ctx.rk[r][0];
- t1 = b1 ^ ctx.rk[r][1];
- t2 = b2 ^ ctx.rk[r][2];
- t3 = b3 ^ ctx.rk[r][3];
-
- b0 = T1[t0&255] ^ T2[(t1>>8)&255] ^ T3[(t2>>16)&255] ^ T4[t3>>>24];
- b1 = T1[t1&255] ^ T2[(t2>>8)&255] ^ T3[(t3>>16)&255] ^ T4[t0>>>24];
- b2 = T1[t2&255] ^ T2[(t3>>8)&255] ^ T3[(t0>>16)&255] ^ T4[t1>>>24];
- b3 = T1[t3&255] ^ T2[(t0>>8)&255] ^ T3[(t1>>16)&255] ^ T4[t2>>>24];
- }
-
- // last round is special
- r = rounds-1;
-
- t0 = b0 ^ ctx.rk[r][0];
- t1 = b1 ^ ctx.rk[r][1];
- t2 = b2 ^ ctx.rk[r][2];
- t3 = b3 ^ ctx.rk[r][3];
-
- b[0] = F1(t0, t1, t2, t3) ^ ctx.rk[rounds][0];
- b[1] = F1(t1, t2, t3, t0) ^ ctx.rk[rounds][1];
- b[2] = F1(t2, t3, t0, t1) ^ ctx.rk[rounds][2];
- b[3] = F1(t3, t0, t1, t2) ^ ctx.rk[rounds][3];
-
- return I32.packLE(b);
- }
-
- function AESdecrypt(block : Bytes, ctx)
- {
- var t0:Int,t1:Int,t2:Int,t3:Int;
- var b = I32.unpackLE(block);
- var r = ctx.rounds;
- while( r>1 )
- {
- t0 = b[0] ^ ctx.rk[r][0];
- t1 = b[1] ^ ctx.rk[r][1];
- t2 = b[2] ^ ctx.rk[r][2];
- t3 = b[3] ^ ctx.rk[r][3];
-
- b[0] = T5[B0(t0)] ^ T6[B1(t3)] ^ T7[B2(t2)] ^ T8[B3(t1)];
- b[1] = T5[B0(t1)] ^ T6[B1(t0)] ^ T7[B2(t3)] ^ T8[B3(t2)];
- b[2] = T5[B0(t2)] ^ T6[B1(t1)] ^ T7[B2(t0)] ^ T8[B3(t3)];
- b[3] = T5[B0(t3)] ^ T6[B1(t2)] ^ T7[B2(t1)] ^ T8[B3(t0)];
- r --;
- }
-
- // last round is special
- t0 = b[0] ^ ctx.rk[1][0];
- t1 = b[1] ^ ctx.rk[1][1];
- t2 = b[2] ^ ctx.rk[1][2];
- t3 = b[3] ^ ctx.rk[1][3];
-
- b[0] = S5[B0(t0)] | (S5[B1(t3)]<<8) | (S5[B2(t2)]<<16) |
(S5[B3(t1)]<<24);
- b[1] = S5[B0(t1)] | (S5[B1(t0)]<<8) | (S5[B2(t3)]<<16) |
(S5[B3(t2)]<<24);
- b[2] = S5[B0(t2)] | (S5[B1(t1)]<<8) | (S5[B2(t0)]<<16) |
(S5[B3(t3)]<<24);
- b[3] = S5[B0(t3)] | (S5[B1(t2)]<<8) | (S5[B2(t1)]<<16) |
(S5[B3(t0)]<<24);
-
- b[0] ^= ctx.rk[0][0];
- b[1] ^= ctx.rk[0][1];
- b[2] ^= ctx.rk[0][2];
- b[3] ^= ctx.rk[0][3];
-
- return I32.packLE(b);
- }
-
-
- static function B0(x:Int) { return (x&255); }
- static function B1(x:Int) { return ((x>>8)&255); }
- static function B2(x:Int) { return ((x>>16)&255); }
- static function B3(x:Int) { return ((x>>24)&255); }
-
- static function F1(x0 : Int, x1:Int, x2:Int, x3:Int)
- {
- return B1(T1[x0&255]) | (B1(T1[(x1>>8)&255])<<8)
- | (B1(T1[(x2>>16)&255])<<16) | (B1(T1[x3>>>24])<<24);
- }
-
- // The round constants used in subkey expansion
- static var Rcon : Array<Int> = [
- 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
- 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4,
- 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91 ];
-
- // Precomputed lookup table for the SBox
- static var S : Array<Int> = [
- 99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171,
- 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164,
- 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113,
- 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226,
- 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214,
- 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203,
- 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69,
- 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245,
- 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68,
- 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42,
- 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73,
- 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109,
- 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37,
- 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62,
- 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225,
- 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
- 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187,
- 22 ];
- // Precomputed lookup table for the inverse SBox FOR DECRYPTION
- static var S5 : Array<Int> = [
- 82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215,
- 251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222,
- 233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66,
- 250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73,
- 109, 139, 209, 37, 114, 248, 246, 100, 134, 104, 152, 22, 212, 164, 92,
- 204, 93, 101, 182, 146, 108, 112, 72, 80, 253, 237, 185, 218, 94, 21,
- 70, 87, 167, 141, 157, 132, 144, 216, 171, 0, 140, 188, 211, 10, 247,
- 228, 88, 5, 184, 179, 69, 6, 208, 44, 30, 143, 202, 63, 15, 2,
- 193, 175, 189, 3, 1, 19, 138, 107, 58, 145, 17, 65, 79, 103, 220,
- 234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116, 34, 231, 173,
- 53, 133, 226, 249, 55, 232, 28, 117, 223, 110, 71, 241, 26, 113, 29,
- 41, 197, 137, 111, 183, 98, 14, 170, 24, 190, 27, 252, 86, 62, 75,
- 198, 210, 121, 32, 154, 219, 192, 254, 120, 205, 90, 244, 31, 221, 168,
- 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81,
- 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160,
- 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97,
- 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12,
- 125 ];
-
- static var T1 : Array<Int> = [
- 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6,
- 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591,
- 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56,
- 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec,
- 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa,
- 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb,
- 0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45,
- 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b,
- 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c,
- 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83,
- 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9,
- 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a,
- 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d,
- 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f,
- 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df,
- 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea,
- 0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34,
- 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b,
- 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d,
- 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413,
- 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1,
- 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6,
- 0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972,
- 0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85,
- 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed,
- 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511,
- 0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe,
- 0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b,
- 0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05,
- 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1,
- 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142,
- 0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf,
- 0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3,
- 0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e,
- 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a,
- 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6,
- 0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3,
- 0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b,
- 0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428,
- 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad,
- 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14,
- 0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8,
- 0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4,
- 0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2,
- 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda,
- 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949,
- 0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf,
- 0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810,
- 0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c,
- 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697,
- 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e,
- 0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f,
- 0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc,
- 0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c,
- 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969,
- 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27,
- 0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122,
- 0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433,
- 0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9,
- 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5,
- 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a,
- 0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0,
- 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e,
- 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c ];
-
- static var T2 : Array<Int> = [
- 0x6363c6a5, 0x7c7cf884, 0x7777ee99, 0x7b7bf68d,
- 0xf2f2ff0d, 0x6b6bd6bd, 0x6f6fdeb1, 0xc5c59154,
- 0x30306050, 0x01010203, 0x6767cea9, 0x2b2b567d,
- 0xfefee719, 0xd7d7b562, 0xabab4de6, 0x7676ec9a,
- 0xcaca8f45, 0x82821f9d, 0xc9c98940, 0x7d7dfa87,
- 0xfafaef15, 0x5959b2eb, 0x47478ec9, 0xf0f0fb0b,
- 0xadad41ec, 0xd4d4b367, 0xa2a25ffd, 0xafaf45ea,
- 0x9c9c23bf, 0xa4a453f7, 0x7272e496, 0xc0c09b5b,
- 0xb7b775c2, 0xfdfde11c, 0x93933dae, 0x26264c6a,
- 0x36366c5a, 0x3f3f7e41, 0xf7f7f502, 0xcccc834f,
- 0x3434685c, 0xa5a551f4, 0xe5e5d134, 0xf1f1f908,
- 0x7171e293, 0xd8d8ab73, 0x31316253, 0x15152a3f,
- 0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e,
- 0x18183028, 0x969637a1, 0x05050a0f, 0x9a9a2fb5,
- 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d,
- 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f,
- 0x0909121b, 0x83831d9e, 0x2c2c5874, 0x1a1a342e,
- 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb,
- 0x5252a4f6, 0x3b3b764d, 0xd6d6b761, 0xb3b37dce,
- 0x2929527b, 0xe3e3dd3e, 0x2f2f5e71, 0x84841397,
- 0x5353a6f5, 0xd1d1b968, 0x00000000, 0xededc12c,
- 0x20204060, 0xfcfce31f, 0xb1b179c8, 0x5b5bb6ed,
- 0x6a6ad4be, 0xcbcb8d46, 0xbebe67d9, 0x3939724b,
- 0x4a4a94de, 0x4c4c98d4, 0x5858b0e8, 0xcfcf854a,
- 0xd0d0bb6b, 0xefefc52a, 0xaaaa4fe5, 0xfbfbed16,
- 0x434386c5, 0x4d4d9ad7, 0x33336655, 0x85851194,
- 0x45458acf, 0xf9f9e910, 0x02020406, 0x7f7ffe81,
- 0x5050a0f0, 0x3c3c7844, 0x9f9f25ba, 0xa8a84be3,
- 0x5151a2f3, 0xa3a35dfe, 0x404080c0, 0x8f8f058a,
- 0x92923fad, 0x9d9d21bc, 0x38387048, 0xf5f5f104,
- 0xbcbc63df, 0xb6b677c1, 0xdadaaf75, 0x21214263,
- 0x10102030, 0xffffe51a, 0xf3f3fd0e, 0xd2d2bf6d,
- 0xcdcd814c, 0x0c0c1814, 0x13132635, 0xececc32f,
- 0x5f5fbee1, 0x979735a2, 0x444488cc, 0x17172e39,
- 0xc4c49357, 0xa7a755f2, 0x7e7efc82, 0x3d3d7a47,
- 0x6464c8ac, 0x5d5dbae7, 0x1919322b, 0x7373e695,
- 0x6060c0a0, 0x81811998, 0x4f4f9ed1, 0xdcdca37f,
- 0x22224466, 0x2a2a547e, 0x90903bab, 0x88880b83,
- 0x46468cca, 0xeeeec729, 0xb8b86bd3, 0x1414283c,
- 0xdedea779, 0x5e5ebce2, 0x0b0b161d, 0xdbdbad76,
- 0xe0e0db3b, 0x32326456, 0x3a3a744e, 0x0a0a141e,
- 0x494992db, 0x06060c0a, 0x2424486c, 0x5c5cb8e4,
- 0xc2c29f5d, 0xd3d3bd6e, 0xacac43ef, 0x6262c4a6,
- 0x919139a8, 0x959531a4, 0xe4e4d337, 0x7979f28b,
- 0xe7e7d532, 0xc8c88b43, 0x37376e59, 0x6d6ddab7,
- 0x8d8d018c, 0xd5d5b164, 0x4e4e9cd2, 0xa9a949e0,
- 0x6c6cd8b4, 0x5656acfa, 0xf4f4f307, 0xeaeacf25,
- 0x6565caaf, 0x7a7af48e, 0xaeae47e9, 0x08081018,
- 0xbaba6fd5, 0x7878f088, 0x25254a6f, 0x2e2e5c72,
- 0x1c1c3824, 0xa6a657f1, 0xb4b473c7, 0xc6c69751,
- 0xe8e8cb23, 0xdddda17c, 0x7474e89c, 0x1f1f3e21,
- 0x4b4b96dd, 0xbdbd61dc, 0x8b8b0d86, 0x8a8a0f85,
- 0x7070e090, 0x3e3e7c42, 0xb5b571c4, 0x6666ccaa,
- 0x484890d8, 0x03030605, 0xf6f6f701, 0x0e0e1c12,
- 0x6161c2a3, 0x35356a5f, 0x5757aef9, 0xb9b969d0,
- 0x86861791, 0xc1c19958, 0x1d1d3a27, 0x9e9e27b9,
- 0xe1e1d938, 0xf8f8eb13, 0x98982bb3, 0x11112233,
- 0x6969d2bb, 0xd9d9a970, 0x8e8e0789, 0x949433a7,
- 0x9b9b2db6, 0x1e1e3c22, 0x87871592, 0xe9e9c920,
- 0xcece8749, 0x5555aaff, 0x28285078, 0xdfdfa57a,
- 0x8c8c038f, 0xa1a159f8, 0x89890980, 0x0d0d1a17,
- 0xbfbf65da, 0xe6e6d731, 0x424284c6, 0x6868d0b8,
- 0x414182c3, 0x999929b0, 0x2d2d5a77, 0x0f0f1e11,
- 0xb0b07bcb, 0x5454a8fc, 0xbbbb6dd6, 0x16162c3a ];
-
- static var T3 : Array<Int> = [
- 0x63c6a563, 0x7cf8847c, 0x77ee9977, 0x7bf68d7b,
- 0xf2ff0df2, 0x6bd6bd6b, 0x6fdeb16f, 0xc59154c5,
- 0x30605030, 0x01020301, 0x67cea967, 0x2b567d2b,
- 0xfee719fe, 0xd7b562d7, 0xab4de6ab, 0x76ec9a76,
- 0xca8f45ca, 0x821f9d82, 0xc98940c9, 0x7dfa877d,
- 0xfaef15fa, 0x59b2eb59, 0x478ec947, 0xf0fb0bf0,
- 0xad41ecad, 0xd4b367d4, 0xa25ffda2, 0xaf45eaaf,
- 0x9c23bf9c, 0xa453f7a4, 0x72e49672, 0xc09b5bc0,
- 0xb775c2b7, 0xfde11cfd, 0x933dae93, 0x264c6a26,
- 0x366c5a36, 0x3f7e413f, 0xf7f502f7, 0xcc834fcc,
- 0x34685c34, 0xa551f4a5, 0xe5d134e5, 0xf1f908f1,
- 0x71e29371, 0xd8ab73d8, 0x31625331, 0x152a3f15,
- 0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3,
- 0x18302818, 0x9637a196, 0x050a0f05, 0x9a2fb59a,
- 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2,
- 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75,
- 0x09121b09, 0x831d9e83, 0x2c58742c, 0x1a342e1a,
- 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0,
- 0x52a4f652, 0x3b764d3b, 0xd6b761d6, 0xb37dceb3,
- 0x29527b29, 0xe3dd3ee3, 0x2f5e712f, 0x84139784,
- 0x53a6f553, 0xd1b968d1, 0x00000000, 0xedc12ced,
- 0x20406020, 0xfce31ffc, 0xb179c8b1, 0x5bb6ed5b,
- 0x6ad4be6a, 0xcb8d46cb, 0xbe67d9be, 0x39724b39,
- 0x4a94de4a, 0x4c98d44c, 0x58b0e858, 0xcf854acf,
- 0xd0bb6bd0, 0xefc52aef, 0xaa4fe5aa, 0xfbed16fb,
- 0x4386c543, 0x4d9ad74d, 0x33665533, 0x85119485,
- 0x458acf45, 0xf9e910f9, 0x02040602, 0x7ffe817f,
- 0x50a0f050, 0x3c78443c, 0x9f25ba9f, 0xa84be3a8,
- 0x51a2f351, 0xa35dfea3, 0x4080c040, 0x8f058a8f,
- 0x923fad92, 0x9d21bc9d, 0x38704838, 0xf5f104f5,
- 0xbc63dfbc, 0xb677c1b6, 0xdaaf75da, 0x21426321,
- 0x10203010, 0xffe51aff, 0xf3fd0ef3, 0xd2bf6dd2,
- 0xcd814ccd, 0x0c18140c, 0x13263513, 0xecc32fec,
- 0x5fbee15f, 0x9735a297, 0x4488cc44, 0x172e3917,
- 0xc49357c4, 0xa755f2a7, 0x7efc827e, 0x3d7a473d,
- 0x64c8ac64, 0x5dbae75d, 0x19322b19, 0x73e69573,
- 0x60c0a060, 0x81199881, 0x4f9ed14f, 0xdca37fdc,
- 0x22446622, 0x2a547e2a, 0x903bab90, 0x880b8388,
- 0x468cca46, 0xeec729ee, 0xb86bd3b8, 0x14283c14,
- 0xdea779de, 0x5ebce25e, 0x0b161d0b, 0xdbad76db,
- 0xe0db3be0, 0x32645632, 0x3a744e3a, 0x0a141e0a,
- 0x4992db49, 0x060c0a06, 0x24486c24, 0x5cb8e45c,
- 0xc29f5dc2, 0xd3bd6ed3, 0xac43efac, 0x62c4a662,
- 0x9139a891, 0x9531a495, 0xe4d337e4, 0x79f28b79,
- 0xe7d532e7, 0xc88b43c8, 0x376e5937, 0x6ddab76d,
- 0x8d018c8d, 0xd5b164d5, 0x4e9cd24e, 0xa949e0a9,
- 0x6cd8b46c, 0x56acfa56, 0xf4f307f4, 0xeacf25ea,
- 0x65caaf65, 0x7af48e7a, 0xae47e9ae, 0x08101808,
- 0xba6fd5ba, 0x78f08878, 0x254a6f25, 0x2e5c722e,
- 0x1c38241c, 0xa657f1a6, 0xb473c7b4, 0xc69751c6,
- 0xe8cb23e8, 0xdda17cdd, 0x74e89c74, 0x1f3e211f,
- 0x4b96dd4b, 0xbd61dcbd, 0x8b0d868b, 0x8a0f858a,
- 0x70e09070, 0x3e7c423e, 0xb571c4b5, 0x66ccaa66,
- 0x4890d848, 0x03060503, 0xf6f701f6, 0x0e1c120e,
- 0x61c2a361, 0x356a5f35, 0x57aef957, 0xb969d0b9,
- 0x86179186, 0xc19958c1, 0x1d3a271d, 0x9e27b99e,
- 0xe1d938e1, 0xf8eb13f8, 0x982bb398, 0x11223311,
- 0x69d2bb69, 0xd9a970d9, 0x8e07898e, 0x9433a794,
- 0x9b2db69b, 0x1e3c221e, 0x87159287, 0xe9c920e9,
- 0xce8749ce, 0x55aaff55, 0x28507828, 0xdfa57adf,
- 0x8c038f8c, 0xa159f8a1, 0x89098089, 0x0d1a170d,
- 0xbf65dabf, 0xe6d731e6, 0x4284c642, 0x68d0b868,
- 0x4182c341, 0x9929b099, 0x2d5a772d, 0x0f1e110f,
- 0xb07bcbb0, 0x54a8fc54, 0xbb6dd6bb, 0x162c3a16 ];
-
- static var T4 : Array<Int> = [
- 0xc6a56363, 0xf8847c7c, 0xee997777, 0xf68d7b7b,
- 0xff0df2f2, 0xd6bd6b6b, 0xdeb16f6f, 0x9154c5c5,
- 0x60503030, 0x02030101, 0xcea96767, 0x567d2b2b,
- 0xe719fefe, 0xb562d7d7, 0x4de6abab, 0xec9a7676,
- 0x8f45caca, 0x1f9d8282, 0x8940c9c9, 0xfa877d7d,
- 0xef15fafa, 0xb2eb5959, 0x8ec94747, 0xfb0bf0f0,
- 0x41ecadad, 0xb367d4d4, 0x5ffda2a2, 0x45eaafaf,
- 0x23bf9c9c, 0x53f7a4a4, 0xe4967272, 0x9b5bc0c0,
- 0x75c2b7b7, 0xe11cfdfd, 0x3dae9393, 0x4c6a2626,
- 0x6c5a3636, 0x7e413f3f, 0xf502f7f7, 0x834fcccc,
- 0x685c3434, 0x51f4a5a5, 0xd134e5e5, 0xf908f1f1,
- 0xe2937171, 0xab73d8d8, 0x62533131, 0x2a3f1515,
- 0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3,
- 0x30281818, 0x37a19696, 0x0a0f0505, 0x2fb59a9a,
- 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2,
- 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575,
- 0x121b0909, 0x1d9e8383, 0x58742c2c, 0x342e1a1a,
- 0x362d1b1b, 0xdcb26e6e, 0xb4ee5a5a, 0x5bfba0a0,
- 0xa4f65252, 0x764d3b3b, 0xb761d6d6, 0x7dceb3b3,
- 0x527b2929, 0xdd3ee3e3, 0x5e712f2f, 0x13978484,
- 0xa6f55353, 0xb968d1d1, 0x00000000, 0xc12ceded,
- 0x40602020, 0xe31ffcfc, 0x79c8b1b1, 0xb6ed5b5b,
- 0xd4be6a6a, 0x8d46cbcb, 0x67d9bebe, 0x724b3939,
- 0x94de4a4a, 0x98d44c4c, 0xb0e85858, 0x854acfcf,
- 0xbb6bd0d0, 0xc52aefef, 0x4fe5aaaa, 0xed16fbfb,
- 0x86c54343, 0x9ad74d4d, 0x66553333, 0x11948585,
- 0x8acf4545, 0xe910f9f9, 0x04060202, 0xfe817f7f,
- 0xa0f05050, 0x78443c3c, 0x25ba9f9f, 0x4be3a8a8,
- 0xa2f35151, 0x5dfea3a3, 0x80c04040, 0x058a8f8f,
- 0x3fad9292, 0x21bc9d9d, 0x70483838, 0xf104f5f5,
- 0x63dfbcbc, 0x77c1b6b6, 0xaf75dada, 0x42632121,
- 0x20301010, 0xe51affff, 0xfd0ef3f3, 0xbf6dd2d2,
- 0x814ccdcd, 0x18140c0c, 0x26351313, 0xc32fecec,
- 0xbee15f5f, 0x35a29797, 0x88cc4444, 0x2e391717,
- 0x9357c4c4, 0x55f2a7a7, 0xfc827e7e, 0x7a473d3d,
- 0xc8ac6464, 0xbae75d5d, 0x322b1919, 0xe6957373,
- 0xc0a06060, 0x19988181, 0x9ed14f4f, 0xa37fdcdc,
- 0x44662222, 0x547e2a2a, 0x3bab9090, 0x0b838888,
- 0x8cca4646, 0xc729eeee, 0x6bd3b8b8, 0x283c1414,
- 0xa779dede, 0xbce25e5e, 0x161d0b0b, 0xad76dbdb,
- 0xdb3be0e0, 0x64563232, 0x744e3a3a, 0x141e0a0a,
- 0x92db4949, 0x0c0a0606, 0x486c2424, 0xb8e45c5c,
- 0x9f5dc2c2, 0xbd6ed3d3, 0x43efacac, 0xc4a66262,
- 0x39a89191, 0x31a49595, 0xd337e4e4, 0xf28b7979,
- 0xd532e7e7, 0x8b43c8c8, 0x6e593737, 0xdab76d6d,
- 0x018c8d8d, 0xb164d5d5, 0x9cd24e4e, 0x49e0a9a9,
- 0xd8b46c6c, 0xacfa5656, 0xf307f4f4, 0xcf25eaea,
- 0xcaaf6565, 0xf48e7a7a, 0x47e9aeae, 0x10180808,
- 0x6fd5baba, 0xf0887878, 0x4a6f2525, 0x5c722e2e,
- 0x38241c1c, 0x57f1a6a6, 0x73c7b4b4, 0x9751c6c6,
- 0xcb23e8e8, 0xa17cdddd, 0xe89c7474, 0x3e211f1f,
- 0x96dd4b4b, 0x61dcbdbd, 0x0d868b8b, 0x0f858a8a,
- 0xe0907070, 0x7c423e3e, 0x71c4b5b5, 0xccaa6666,
- 0x90d84848, 0x06050303, 0xf701f6f6, 0x1c120e0e,
- 0xc2a36161, 0x6a5f3535, 0xaef95757, 0x69d0b9b9,
- 0x17918686, 0x9958c1c1, 0x3a271d1d, 0x27b99e9e,
- 0xd938e1e1, 0xeb13f8f8, 0x2bb39898, 0x22331111,
- 0xd2bb6969, 0xa970d9d9, 0x07898e8e, 0x33a79494,
- 0x2db69b9b, 0x3c221e1e, 0x15928787, 0xc920e9e9,
- 0x8749cece, 0xaaff5555, 0x50782828, 0xa57adfdf,
- 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d,
- 0x65dabfbf, 0xd731e6e6, 0x84c64242, 0xd0b86868,
- 0x82c34141, 0x29b09999, 0x5a772d2d, 0x1e110f0f,
- 0x7bcbb0b0, 0xa8fc5454, 0x6dd6bbbb, 0x2c3a1616 ];
-
- // ONLY USED FOR DECRYPTION
- static var T5 : Array<Int> = [
- 0x50a7f451,0x5365417e,0xc3a4171a,0x965e273a,
- 0xcb6bab3b,0xf1459d1f,0xab58faac,0x9303e34b,
- 0x55fa3020,0xf66d76ad,0x9176cc88,0x254c02f5,
- 0xfcd7e54f,0xd7cb2ac5,0x80443526,0x8fa362b5,
- 0x495ab1de,0x671bba25,0x980eea45,0xe1c0fe5d,
- 0x02752fc3,0x12f04c81,0xa397468d,0xc6f9d36b,
- 0xe75f8f03,0x959c9215,0xeb7a6dbf,0xda595295,
- 0x2d83bed4,0xd3217458,0x2969e049,0x44c8c98e,
- 0x6a89c275,0x78798ef4,0x6b3e5899,0xdd71b927,
- 0xb64fe1be,0x17ad88f0,0x66ac20c9,0xb43ace7d,
- 0x184adf63,0x82311ae5,0x60335197,0x457f5362,
- 0xe07764b1,0x84ae6bbb,0x1ca081fe,0x942b08f9,
- 0x58684870,0x19fd458f,0x876cde94,0xb7f87b52,
- 0x23d373ab,0xe2024b72,0x578f1fe3,0x2aab5566,
- 0x0728ebb2,0x03c2b52f,0x9a7bc586,0xa50837d3,
- 0xf2872830,0xb2a5bf23,0xba6a0302,0x5c8216ed,
- 0x2b1ccf8a,0x92b479a7,0xf0f207f3,0xa1e2694e,
- 0xcdf4da65,0xd5be0506,0x1f6234d1,0x8afea6c4,
- 0x9d532e34,0xa055f3a2,0x32e18a05,0x75ebf6a4,
- 0x39ec830b,0xaaef6040,0x069f715e,0x51106ebd,
- 0xf98a213e,0x3d06dd96,0xae053edd,0x46bde64d,
- 0xb58d5491,0x055dc471,0x6fd40604,0xff155060,
- 0x24fb9819,0x97e9bdd6,0xcc434089,0x779ed967,
- 0xbd42e8b0,0x888b8907,0x385b19e7,0xdbeec879,
- 0x470a7ca1,0xe90f427c,0xc91e84f8,0x00000000,
- 0x83868009,0x48ed2b32,0xac70111e,0x4e725a6c,
- 0xfbff0efd,0x5638850f,0x1ed5ae3d,0x27392d36,
- 0x64d90f0a,0x21a65c68,0xd1545b9b,0x3a2e3624,
- 0xb1670a0c,0x0fe75793,0xd296eeb4,0x9e919b1b,
- 0x4fc5c080,0xa220dc61,0x694b775a,0x161a121c,
- 0x0aba93e2,0xe52aa0c0,0x43e0223c,0x1d171b12,
- 0x0b0d090e,0xadc78bf2,0xb9a8b62d,0xc8a91e14,
- 0x8519f157,0x4c0775af,0xbbdd99ee,0xfd607fa3,
- 0x9f2601f7,0xbcf5725c,0xc53b6644,0x347efb5b,
- 0x7629438b,0xdcc623cb,0x68fcedb6,0x63f1e4b8,
- 0xcadc31d7,0x10856342,0x40229713,0x2011c684,
- 0x7d244a85,0xf83dbbd2,0x1132f9ae,0x6da129c7,
- 0x4b2f9e1d,0xf330b2dc,0xec52860d,0xd0e3c177,
- 0x6c16b32b,0x99b970a9,0xfa489411,0x2264e947,
- 0xc48cfca8,0x1a3ff0a0,0xd82c7d56,0xef903322,
- 0xc74e4987,0xc1d138d9,0xfea2ca8c,0x360bd498,
- 0xcf81f5a6,0x28de7aa5,0x268eb7da,0xa4bfad3f,
- 0xe49d3a2c,0x0d927850,0x9bcc5f6a,0x62467e54,
- 0xc2138df6,0xe8b8d890,0x5ef7392e,0xf5afc382,
- 0xbe805d9f,0x7c93d069,0xa92dd56f,0xb31225cf,
- 0x3b99acc8,0xa77d1810,0x6e639ce8,0x7bbb3bdb,
- 0x097826cd,0xf418596e,0x01b79aec,0xa89a4f83,
- 0x656e95e6,0x7ee6ffaa,0x08cfbc21,0xe6e815ef,
- 0xd99be7ba,0xce366f4a,0xd4099fea,0xd67cb029,
- 0xafb2a431,0x31233f2a,0x3094a5c6,0xc066a235,
- 0x37bc4e74,0xa6ca82fc,0xb0d090e0,0x15d8a733,
- 0x4a9804f1,0xf7daec41,0x0e50cd7f,0x2ff69117,
- 0x8dd64d76,0x4db0ef43,0x544daacc,0xdf0496e4,
- 0xe3b5d19e,0x1b886a4c,0xb81f2cc1,0x7f516546,
- 0x04ea5e9d,0x5d358c01,0x737487fa,0x2e410bfb,
- 0x5a1d67b3,0x52d2db92,0x335610e9,0x1347d66d,
- 0x8c61d79a,0x7a0ca137,0x8e14f859,0x893c13eb,
- 0xee27a9ce,0x35c961b7,0xede51ce1,0x3cb1477a,
- 0x59dfd29c,0x3f73f255,0x79ce1418,0xbf37c773,
- 0xeacdf753,0x5baafd5f,0x146f3ddf,0x86db4478,
- 0x81f3afca,0x3ec468b9,0x2c342438,0x5f40a3c2,
- 0x72c31d16,0x0c25e2bc,0x8b493c28,0x41950dff,
- 0x7101a839,0xdeb30c08,0x9ce4b4d8,0x90c15664,
- 0x6184cb7b,0x70b632d5,0x745c6c48,0x4257b8d0 ];
-
- static var T6 : Array<Int> = [
- 0xa7f45150,0x65417e53,0xa4171ac3,0x5e273a96,
- 0x6bab3bcb,0x459d1ff1,0x58faacab,0x03e34b93,
- 0xfa302055,0x6d76adf6,0x76cc8891,0x4c02f525,
- 0xd7e54ffc,0xcb2ac5d7,0x44352680,0xa362b58f,
- 0x5ab1de49,0x1bba2567,0x0eea4598,0xc0fe5de1,
- 0x752fc302,0xf04c8112,0x97468da3,0xf9d36bc6,
- 0x5f8f03e7,0x9c921595,0x7a6dbfeb,0x595295da,
- 0x83bed42d,0x217458d3,0x69e04929,0xc8c98e44,
- 0x89c2756a,0x798ef478,0x3e58996b,0x71b927dd,
- 0x4fe1beb6,0xad88f017,0xac20c966,0x3ace7db4,
- 0x4adf6318,0x311ae582,0x33519760,0x7f536245,
- 0x7764b1e0,0xae6bbb84,0xa081fe1c,0x2b08f994,
- 0x68487058,0xfd458f19,0x6cde9487,0xf87b52b7,
- 0xd373ab23,0x024b72e2,0x8f1fe357,0xab55662a,
- 0x28ebb207,0xc2b52f03,0x7bc5869a,0x0837d3a5,
- 0x872830f2,0xa5bf23b2,0x6a0302ba,0x8216ed5c,
- 0x1ccf8a2b,0xb479a792,0xf207f3f0,0xe2694ea1,
- 0xf4da65cd,0xbe0506d5,0x6234d11f,0xfea6c48a,
- 0x532e349d,0x55f3a2a0,0xe18a0532,0xebf6a475,
- 0xec830b39,0xef6040aa,0x9f715e06,0x106ebd51,
- 0x8a213ef9,0x06dd963d,0x053eddae,0xbde64d46,
- 0x8d5491b5,0x5dc47105,0xd406046f,0x155060ff,
- 0xfb981924,0xe9bdd697,0x434089cc,0x9ed96777,
- 0x42e8b0bd,0x8b890788,0x5b19e738,0xeec879db,
- 0x0a7ca147,0x0f427ce9,0x1e84f8c9,0x00000000,
- 0x86800983,0xed2b3248,0x70111eac,0x725a6c4e,
- 0xff0efdfb,0x38850f56,0xd5ae3d1e,0x392d3627,
- 0xd90f0a64,0xa65c6821,0x545b9bd1,0x2e36243a,
- 0x670a0cb1,0xe757930f,0x96eeb4d2,0x919b1b9e,
- 0xc5c0804f,0x20dc61a2,0x4b775a69,0x1a121c16,
- 0xba93e20a,0x2aa0c0e5,0xe0223c43,0x171b121d,
- 0x0d090e0b,0xc78bf2ad,0xa8b62db9,0xa91e14c8,
- 0x19f15785,0x0775af4c,0xdd99eebb,0x607fa3fd,
- 0x2601f79f,0xf5725cbc,0x3b6644c5,0x7efb5b34,
- 0x29438b76,0xc623cbdc,0xfcedb668,0xf1e4b863,
- 0xdc31d7ca,0x85634210,0x22971340,0x11c68420,
- 0x244a857d,0x3dbbd2f8,0x32f9ae11,0xa129c76d,
- 0x2f9e1d4b,0x30b2dcf3,0x52860dec,0xe3c177d0,
- 0x16b32b6c,0xb970a999,0x489411fa,0x64e94722,
- 0x8cfca8c4,0x3ff0a01a,0x2c7d56d8,0x903322ef,
- 0x4e4987c7,0xd138d9c1,0xa2ca8cfe,0x0bd49836,
- 0x81f5a6cf,0xde7aa528,0x8eb7da26,0xbfad3fa4,
- 0x9d3a2ce4,0x9278500d,0xcc5f6a9b,0x467e5462,
- 0x138df6c2,0xb8d890e8,0xf7392e5e,0xafc382f5,
- 0x805d9fbe,0x93d0697c,0x2dd56fa9,0x1225cfb3,
- 0x99acc83b,0x7d1810a7,0x639ce86e,0xbb3bdb7b,
- 0x7826cd09,0x18596ef4,0xb79aec01,0x9a4f83a8,
- 0x6e95e665,0xe6ffaa7e,0xcfbc2108,0xe815efe6,
- 0x9be7bad9,0x366f4ace,0x099fead4,0x7cb029d6,
- 0xb2a431af,0x233f2a31,0x94a5c630,0x66a235c0,
- 0xbc4e7437,0xca82fca6,0xd090e0b0,0xd8a73315,
- 0x9804f14a,0xdaec41f7,0x50cd7f0e,0xf691172f,
- 0xd64d768d,0xb0ef434d,0x4daacc54,0x0496e4df,
- 0xb5d19ee3,0x886a4c1b,0x1f2cc1b8,0x5165467f,
- 0xea5e9d04,0x358c015d,0x7487fa73,0x410bfb2e,
- 0x1d67b35a,0xd2db9252,0x5610e933,0x47d66d13,
- 0x61d79a8c,0x0ca1377a,0x14f8598e,0x3c13eb89,
- 0x27a9ceee,0xc961b735,0xe51ce1ed,0xb1477a3c,
- 0xdfd29c59,0x73f2553f,0xce141879,0x37c773bf,
- 0xcdf753ea,0xaafd5f5b,0x6f3ddf14,0xdb447886,
- 0xf3afca81,0xc468b93e,0x3424382c,0x40a3c25f,
- 0xc31d1672,0x25e2bc0c,0x493c288b,0x950dff41,
- 0x01a83971,0xb30c08de,0xe4b4d89c,0xc1566490,
- 0x84cb7b61,0xb632d570,0x5c6c4874,0x57b8d042 ];
-
- static var T7 : Array<Int> = [
- 0xf45150a7,0x417e5365,0x171ac3a4,0x273a965e,
- 0xab3bcb6b,0x9d1ff145,0xfaacab58,0xe34b9303,
- 0x302055fa,0x76adf66d,0xcc889176,0x02f5254c,
- 0xe54ffcd7,0x2ac5d7cb,0x35268044,0x62b58fa3,
- 0xb1de495a,0xba25671b,0xea45980e,0xfe5de1c0,
- 0x2fc30275,0x4c8112f0,0x468da397,0xd36bc6f9,
- 0x8f03e75f,0x9215959c,0x6dbfeb7a,0x5295da59,
- 0xbed42d83,0x7458d321,0xe0492969,0xc98e44c8,
- 0xc2756a89,0x8ef47879,0x58996b3e,0xb927dd71,
- 0xe1beb64f,0x88f017ad,0x20c966ac,0xce7db43a,
- 0xdf63184a,0x1ae58231,0x51976033,0x5362457f,
- 0x64b1e077,0x6bbb84ae,0x81fe1ca0,0x08f9942b,
- 0x48705868,0x458f19fd,0xde94876c,0x7b52b7f8,
- 0x73ab23d3,0x4b72e202,0x1fe3578f,0x55662aab,
- 0xebb20728,0xb52f03c2,0xc5869a7b,0x37d3a508,
- 0x2830f287,0xbf23b2a5,0x0302ba6a,0x16ed5c82,
- 0xcf8a2b1c,0x79a792b4,0x07f3f0f2,0x694ea1e2,
- 0xda65cdf4,0x0506d5be,0x34d11f62,0xa6c48afe,
- 0x2e349d53,0xf3a2a055,0x8a0532e1,0xf6a475eb,
- 0x830b39ec,0x6040aaef,0x715e069f,0x6ebd5110,
- 0x213ef98a,0xdd963d06,0x3eddae05,0xe64d46bd,
- 0x5491b58d,0xc471055d,0x06046fd4,0x5060ff15,
- 0x981924fb,0xbdd697e9,0x4089cc43,0xd967779e,
- 0xe8b0bd42,0x8907888b,0x19e7385b,0xc879dbee,
- 0x7ca1470a,0x427ce90f,0x84f8c91e,0x00000000,
- 0x80098386,0x2b3248ed,0x111eac70,0x5a6c4e72,
- 0x0efdfbff,0x850f5638,0xae3d1ed5,0x2d362739,
- 0x0f0a64d9,0x5c6821a6,0x5b9bd154,0x36243a2e,
- 0x0a0cb167,0x57930fe7,0xeeb4d296,0x9b1b9e91,
- 0xc0804fc5,0xdc61a220,0x775a694b,0x121c161a,
- 0x93e20aba,0xa0c0e52a,0x223c43e0,0x1b121d17,
- 0x090e0b0d,0x8bf2adc7,0xb62db9a8,0x1e14c8a9,
- 0xf1578519,0x75af4c07,0x99eebbdd,0x7fa3fd60,
- 0x01f79f26,0x725cbcf5,0x6644c53b,0xfb5b347e,
***The diff for this file has been truncated for email.***
=======================================
--- /trunk/ext3/chx/crypt/Des.hx Sun Feb 12 19:59:39 2012
+++ /dev/null
@@ -1,425 +0,0 @@
-/*
- * Copyright (c) 2011, The Caffeine-hx project contributors
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-/*
-* DESKey
-*
-* Derived from:
-* An Actionscript 3 implementation of the Data Encryption
Standard (DES)
-* Copyright (c) 2007 Henri Torgemane
-* Which in turn derives from:
-* The Bouncy Castle Crypto package,
-* Copyright (c) 2000-2004 The Legion Of The Bouncy Castle
-* (http://www.bouncycastle.org)
-*
-* See LICENSE.txt for full license information.
-*/
-package chx.crypt;
-import I32;
-
-/**
-* DES Key. In neko requires the openssl ndll.
-**/
-class Des implements IBlockCipher
-{
- public var blockSize(__getBlockSize,null) : Int;
- #if (neko || useOpenSSL)
- var key:Dynamic;
- #else
- /*
- * what follows is mainly taken from "Applied Cryptography", by Bruce
- * Schneier, however it also bears great resemblance to Richard
- * Outerbridge's D3DES...
- */
- private static var Df_Key:Array<Int32> = [ 0x01, 0x23, 0x45, 0x67, 0x89,
0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
- 0x10, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67 ];
-
- private static var bytebit:Array<Int32> = [ 128, 64, 32, 16, 8, 4, 2, 1 ];
-
- private static var bigbyte:Array<Int32> = [ 0x800000, 0x400000, 0x200000,
0x100000, 0x80000, 0x40000, 0x20000, 0x10000, 0x8000,
- 0x4000, 0x2000, 0x1000, 0x800, 0x400, 0x200, 0x100, 0x80, 0x40, 0x20,
0x10, 0x8, 0x4, 0x2, 0x1 ];
-
- /*
- * Use the key schedule specified in the Standard (ANSI X3.92-1981).
- */
-
- private static var pc1:Array<Int32> = [ 56, 48, 40, 32, 24, 16, 8, 0, 57,
49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2,
- 59, 51, 43, 35, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 60, 52, 44, 36, 28, 20, 12,
- 4, 27, 19, 11, 3 ];
-
- private static var totrot:Array<Int32> = [ 1, 2, 4, 6, 8, 10, 12, 14, 15,
17, 19, 21, 23, 25, 27, 28 ];
-
- private static var pc2:Array<Int32> = [ 13, 16, 10, 23, 0, 4, 2, 27, 14,
5, 20, 9, 22, 18, 11, 3, 25, 7, 15, 6, 26, 19, 12, 1, 40,
- 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47, 43, 48, 38, 55, 33, 52,
45, 41, 49, 35, 28, 31 ];
-
- private static var SP1:Array<Int32> = [ 0x01010400, 0x00000000,
0x00010000, 0x01010404, 0x01010004, 0x00010404, 0x00000004,
- 0x00010000, 0x00000400, 0x01010400, 0x01010404, 0x00000400,
0x01000404, 0x01010004, 0x01000000, 0x00000004,
- 0x00000404, 0x01000400, 0x01000400, 0x00010400, 0x00010400,
0x01010000, 0x01010000, 0x01000404, 0x00010004,
- 0x01000004, 0x01000004, 0x00010004, 0x00000000, 0x00000404,
0x00010404, 0x01000000, 0x00010000, 0x01010404,
- 0x00000004, 0x01010000, 0x01010400, 0x01000000, 0x01000000,
0x00000400, 0x01010004, 0x00010000, 0x00010400,
- 0x01000004, 0x00000400, 0x00000004, 0x01000404, 0x00010404,
0x01010404, 0x00010004, 0x01010000, 0x01000404,
- 0x01000004, 0x00000404, 0x00010404, 0x01010400, 0x00000404,
0x01000400, 0x01000400, 0x00000000, 0x00010004,
- 0x00010400, 0x00000000, 0x01010004 ];
-
- private static var SP2:Array<Int32> = [ 0x80108020, 0x80008000,
0x00008000, 0x00108020, 0x00100000, 0x00000020, 0x80100020,
- 0x80008020, 0x80000020, 0x80108020, 0x80108000, 0x80000000,
0x80008000, 0x00100000, 0x00000020, 0x80100020,
- 0x00108000, 0x00100020, 0x80008020, 0x00000000, 0x80000000,
0x00008000, 0x00108020, 0x80100000, 0x00100020,
- 0x80000020, 0x00000000, 0x00108000, 0x00008020, 0x80108000,
0x80100000, 0x00008020, 0x00000000, 0x00108020,
- 0x80100020, 0x00100000, 0x80008020, 0x80100000, 0x80108000,
0x00008000, 0x80100000, 0x80008000, 0x00000020,
- 0x80108020, 0x00108020, 0x00000020, 0x00008000, 0x80000000,
0x00008020, 0x80108000, 0x00100000, 0x80000020,
- 0x00100020, 0x80008020, 0x80000020, 0x00100020, 0x00108000,
0x00000000, 0x80008000, 0x00008020, 0x80000000,
- 0x80100020, 0x80108020, 0x00108000 ];
-
- private static var SP3:Array<Int32> = [ 0x00000208, 0x08020200,
0x00000000, 0x08020008, 0x08000200, 0x00000000, 0x00020208,
- 0x08000200, 0x00020008, 0x08000008, 0x08000008, 0x00020000,
0x08020208, 0x00020008, 0x08020000, 0x00000208,
- 0x08000000, 0x00000008, 0x08020200, 0x00000200, 0x00020200,
0x08020000, 0x08020008, 0x00020208, 0x08000208,
- 0x00020200, 0x00020000, 0x08000208, 0x00000008, 0x08020208,
0x00000200, 0x08000000, 0x08020200, 0x08000000,
- 0x00020008, 0x00000208, 0x00020000, 0x08020200, 0x08000200,
0x00000000, 0x00000200, 0x00020008, 0x08020208,
- 0x08000200, 0x08000008, 0x00000200, 0x00000000, 0x08020008,
0x08000208, 0x00020000, 0x08000000, 0x08020208,
- 0x00000008, 0x00020208, 0x00020200, 0x08000008, 0x08020000,
0x08000208, 0x00000208, 0x08020000, 0x00020208,
- 0x00000008, 0x08020008, 0x00020200 ];
-
- private static var SP4:Array<Int32> = [ 0x00802001, 0x00002081,
0x00002081, 0x00000080, 0x00802080, 0x00800081, 0x00800001,
- 0x00002001, 0x00000000, 0x00802000, 0x00802000, 0x00802081,
0x00000081, 0x00000000, 0x00800080, 0x00800001,
- 0x00000001, 0x00002000, 0x00800000, 0x00802001, 0x00000080,
0x00800000, 0x00002001, 0x00002080, 0x00800081,
- 0x00000001, 0x00002080, 0x00800080, 0x00002000, 0x00802080,
0x00802081, 0x00000081, 0x00800080, 0x00800001,
- 0x00802000, 0x00802081, 0x00000081, 0x00000000, 0x00000000,
0x00802000, 0x00002080, 0x00800080, 0x00800081,
- 0x00000001, 0x00802001, 0x00002081, 0x00002081, 0x00000080,
0x00802081, 0x00000081, 0x00000001, 0x00002000,
- 0x00800001, 0x00002001, 0x00802080, 0x00800081, 0x00002001,
0x00002080, 0x00800000, 0x00802001, 0x00000080,
- 0x00800000, 0x00002000, 0x00802080 ];
-
- private static var SP5:Array<Int32> = [ 0x00000100, 0x02080100,
0x02080000, 0x42000100, 0x00080000, 0x00000100, 0x40000000,
- 0x02080000, 0x40080100, 0x00080000, 0x02000100, 0x40080100,
0x42000100, 0x42080000, 0x00080100, 0x40000000,
- 0x02000000, 0x40080000, 0x40080000, 0x00000000, 0x40000100,
0x42080100, 0x42080100, 0x02000100, 0x42080000,
- 0x40000100, 0x00000000, 0x42000000, 0x02080100, 0x02000000,
0x42000000, 0x00080100, 0x00080000, 0x42000100,
- 0x00000100, 0x02000000, 0x40000000, 0x02080000, 0x42000100,
0x40080100, 0x02000100, 0x40000000, 0x42080000,
- 0x02080100, 0x40080100, 0x00000100, 0x02000000, 0x42080000,
0x42080100, 0x00080100, 0x42000000, 0x42080100,
- 0x02080000, 0x00000000, 0x40080000, 0x42000000, 0x00080100,
0x02000100, 0x40000100, 0x00080000, 0x00000000,
- 0x40080000, 0x02080100, 0x40000100 ];
-
- private static var SP6:Array<Int32> = [ 0x20000010, 0x20400000,
0x00004000, 0x20404010, 0x20400000, 0x00000010, 0x20404010,
- 0x00400000, 0x20004000, 0x00404010, 0x00400000, 0x20000010,
0x00400010, 0x20004000, 0x20000000, 0x00004010,
- 0x00000000, 0x00400010, 0x20004010, 0x00004000, 0x00404000,
0x20004010, 0x00000010, 0x20400010, 0x20400010,
- 0x00000000, 0x00404010, 0x20404000, 0x00004010, 0x00404000,
0x20404000, 0x20000000, 0x20004000, 0x00000010,
- 0x20400010, 0x00404000, 0x20404010, 0x00400000, 0x00004010,
0x20000010, 0x00400000, 0x20004000, 0x20000000,
- 0x00004010, 0x20000010, 0x20404010, 0x00404000, 0x20400000,
0x00404010, 0x20404000, 0x00000000, 0x20400010,
- 0x00000010, 0x00004000, 0x20400000, 0x00404010, 0x00004000,
0x00400010, 0x20004010, 0x00000000, 0x20404000,
- 0x20000000, 0x00400010, 0x20004010 ];
-
- private static var SP7:Array<Int32> = [ 0x00200000, 0x04200002,
0x04000802, 0x00000000, 0x00000800, 0x04000802, 0x00200802,
- 0x04200800, 0x04200802, 0x00200000, 0x00000000, 0x04000002,
0x00000002, 0x04000000, 0x04200002, 0x00000802,
- 0x04000800, 0x00200802, 0x00200002, 0x04000800, 0x04000002,
0x04200000, 0x04200800, 0x00200002, 0x04200000,
- 0x00000800, 0x00000802, 0x04200802, 0x00200800, 0x00000002,
0x04000000, 0x00200800, 0x04000000, 0x00200800,
- 0x00200000, 0x04000802, 0x04000802, 0x04200002, 0x04200002,
0x00000002, 0x00200002, 0x04000000, 0x04000800,
- 0x00200000, 0x04200800, 0x00000802, 0x00200802, 0x04200800,
0x00000802, 0x04000002, 0x04200802, 0x04200000,
- 0x00200800, 0x00000000, 0x00000002, 0x04200802, 0x00000000,
0x00200802, 0x04200000, 0x00000800, 0x04000002,
- 0x04000800, 0x00000800, 0x00200002 ];
-
- private static var SP8:Array<Int32> = [ 0x10001040, 0x00001000,
0x00040000, 0x10041040, 0x10000000, 0x10001040, 0x00000040,
- 0x10000000, 0x00040040, 0x10040000, 0x10041040, 0x00041000,
0x10041000, 0x00041040, 0x00001000, 0x00000040,
- 0x10040000, 0x10000040, 0x10001000, 0x00001040, 0x00041000,
0x00040040, 0x10040040, 0x10041000, 0x00001040,
- 0x00000000, 0x00000000, 0x10040040, 0x10000040, 0x10001000,
0x00041040, 0x00040000, 0x00041040, 0x00040000,
- 0x10041000, 0x00001000, 0x00000040, 0x10040040, 0x00001000,
0x00041040, 0x10001000, 0x00000040, 0x10000040,
- 0x10040000, 0x10040040, 0x10000000, 0x00040000, 0x10001040,
0x00000000, 0x10041040, 0x00040040, 0x10000040,
- 0x10040000, 0x10001000, 0x10001040, 0x00000000, 0x10041040,
0x00041000, 0x00041000, 0x00001040, 0x00001040,
- 0x00040040, 0x10000000, 0x10041000 ];
-
- var key:Bytes;
- var encKey:Array<Int32>;
- var decKey:Array<Int32>;
- #end
-
- public function new(key:Bytes) {
- if(key.length < 8)
- throw new chx.lang.OutsideBoundsException("Must be 8 bytes of key
data");
- #if (neko || useOpenSSL)
- this.key = des_create_key(key.sub(0,8).getData());
- #else
- this.key = key;
- this.encKey = generateWorkingKey(true, key, 0);
- this.decKey = generateWorkingKey(false, key, 0);
- #end
- }
-
- public function getBlockSize():Int
- {
- return 8;
- }
-
- function __getBlockSize():Int {
- return 8;
- }
-
- public function decryptBlock(block:Bytes):Bytes
- {
- #if (neko || useOpenSSL)
- return Bytes.ofData(des_decrypt_block(key, block.getData()));
- #else
- var outBlock = Bytes.alloc(block.length);
- desFunc(decKey, block, 0, outBlock, 0);
- return outBlock;
- #end
- }
-
- public function dispose():Void
- {
- #if (neko || useOpenSSL)
- des_destroy_key(key);
- #else
- for (i in 0...encKey.length) { encKey[i]=0; }
- for (i in 0...decKey.length) { decKey[i]=0; }
- encKey=null;
- decKey=null;
- for (i in 0...key.length) { key.set(i, 0); }
- key = null;
- #end
- }
-
- public function encryptBlock(block:Bytes):Bytes
- {
- #if (neko || useOpenSSL)
- return Bytes.ofData(des_encrypt_block(key, block.getData()));
- #else
- var outBlock = Bytes.alloc(block.length);
- desFunc(encKey, block, 0, outBlock, 0);
- return outBlock;
- #end
- }
-
- #if !(neko || useOpenSSL)
- /**
- * generate an integer based working key based on our secret key and what
we
- * processing we are planning to do.
- *
- * Acknowledgements for this routine go to James Gillogly & Phil Karn.
- */
- function generateWorkingKey(encrypting:Bool, key:Bytes,
off:Int):Array<Int32>
- {
- //int[] newKey = new int[32];
- var newKey:Array<Int32> = [];
- //boolean[] pc1m = new boolean[56], pcr = new boolean[56];
- var pc1m:Array<Bool> = new Array();
- var pcr:Array<Bool> = new Array();
-
- var l:Int;
-
- for (j in 0...56)
- {
- l = pc1[j];
- pc1m[j] = ((key.get(off + (l >>> 3)) & bytebit[l & 07]) != 0);
- }
-
- for (i in 0...16)
- {
- var m:Int;
- var n:Int;
-
- if (encrypting)
- {
- m = i << 1;
- }
- else
- {
- m = (15 - i) << 1;
- }
-
- n = m + 1;
- newKey[m] = newKey[n] = 0;
-
- for (j in 0...28)
- {
- l = j + totrot[i];
- if (l < 28)
- {
- pcr[j] = pc1m[l];
- }
- else
- {
- pcr[j] = pc1m[l - 28];
- }
- }
-
- for (j in 28...56)
- {
- l = j + totrot[i];
- if (l < 56)
- {
- pcr[j] = pc1m[l];
- }
- else
- {
- pcr[j] = pc1m[l - 28];
- }
- }
-
- for (j in 0...24)
- {
- if (pcr[pc2[j]])
- {
- newKey[m] |= bigbyte[j];
- }
-
- if (pcr[pc2[j + 24]])
- {
- newKey[n] |= bigbyte[j];
- }
- }
- }
-
- //
- // store the processed key
- //
- var i:Int = 0;
- while(i < 32)
- {
- var i1:Int32;
- var i2:Int32;
-
- i1 = newKey[i];
- i2 = newKey[i + 1];
-
- newKey[i] = ((i1 & 0x00fc0000) << 6) | ((i1 & 0x00000fc0) << 10) | ((i2
& 0x00fc0000) >>> 10)
- | ((i2 & 0x00000fc0) >>> 6);
-
- newKey[i + 1] = ((i1 & 0x0003f000) << 12) | ((i1 & 0x0000003f) << 16) |
((i2 & 0x0003f000) >>> 4)
- | (i2 & 0x0000003f);
- i += 2;
- }
- return newKey;
- }
-
- /**
- * the DES engine.
- */
- private function desFunc(wKey:Array<Int32>, inp:Bytes, inOff:Int,
out:Bytes, outOff:Int):Void
- {
- var work:Int32 = 0;
- var right:Int32 = 0;
- var left:Int32 = 0;
-
- left = (inp.get(inOff + 0) & 0xff) << 24;
- left |= (inp.get(inOff + 1) & 0xff) << 16;
- left |= (inp.get(inOff + 2) & 0xff) << 8;
- left |= (inp.get(inOff + 3) & 0xff);
-
- right = (inp.get(inOff + 4) & 0xff) << 24;
- right |= (inp.get(inOff + 5) & 0xff) << 16;
- right |= (inp.get(inOff + 6) & 0xff) << 8;
- right |= (inp.get(inOff + 7) & 0xff);
-
- work = ((left >>> 4) ^ right) & 0x0f0f0f0f;
- right ^= work;
- left ^= (work << 4);
- work = ((left >>> 16) ^ right) & 0x0000ffff;
- right ^= work;
- left ^= (work << 16);
- work = ((right >>> 2) ^ left) & 0x33333333;
- left ^= work;
- right ^= (work << 2);
- work = ((right >>> 8) ^ left) & 0x00ff00ff;
- left ^= work;
- right ^= (work << 8);
- right = ((right << 1) | ((right >>> 31) & 1)) & 0xffffffff;
- work = (left ^ right) & 0xaaaaaaaa;
- left ^= work;
- right ^= work;
- left = ((left << 1) | ((left >>> 31) & 1)) & 0xffffffff;
-
- for (round in 0...8)
- {
- var fval:Int32 = 0;
-
- work = (right << 28) | (right >>> 4);
- work ^= wKey[round * 4 + 0];
- fval = SP7[work & 0x3f];
- fval |= SP5[(work >>> 8) & 0x3f];
- fval |= SP3[(work >>> 16) & 0x3f];
- fval |= SP1[(work >>> 24) & 0x3f];
- work = right ^ wKey[round * 4 + 1];
- fval |= SP8[work & 0x3f];
- fval |= SP6[(work >>> 8) & 0x3f];
- fval |= SP4[(work >>> 16) & 0x3f];
- fval |= SP2[(work >>> 24) & 0x3f];
- left ^= fval;
- work = (left << 28) | (left >>> 4);
- work ^= wKey[round * 4 + 2];
- fval = SP7[work & 0x3f];
- fval |= SP5[(work >>> 8) & 0x3f];
- fval |= SP3[(work >>> 16) & 0x3f];
- fval |= SP1[(work >>> 24) & 0x3f];
- work = left ^ wKey[round * 4 + 3];
- fval |= SP8[work & 0x3f];
- fval |= SP6[(work >>> 8) & 0x3f];
- fval |= SP4[(work >>> 16) & 0x3f];
- fval |= SP2[(work >>> 24) & 0x3f];
- right ^= fval;
- }
-
- right = (right << 31) | (right >>> 1);
- work = (left ^ right) & 0xaaaaaaaa;
- left ^= work;
- right ^= work;
- left = (left << 31) | (left >>> 1);
- work = ((left >>> 8) ^ right) & 0x00ff00ff;
- right ^= work;
- left ^= (work << 8);
- work = ((left >>> 2) ^ right) & 0x33333333;
- right ^= work;
- left ^= (work << 2);
- work = ((right >>> 16) ^ left) & 0x0000ffff;
- left ^= work;
- right ^= (work << 16);
- work = ((right >>> 4) ^ left) & 0x0f0f0f0f;
- left ^= work;
- right ^= (work << 4);
-
- out.set(outOff + 0, ((right >>> 24) & 0xff));
- out.set(outOff + 1, ((right >>> 16) & 0xff));
- out.set(outOff + 2, ((right >>> 8) & 0xff));
- out.set(outOff + 3, (right & 0xff));
- out.set(outOff + 4, ((left >>> 24) & 0xff));
- out.set(outOff + 5, ((left >>> 16) & 0xff));
- out.set(outOff + 6, ((left >>> 8) & 0xff));
- out.set(outOff + 7, (left & 0xff));
- }
- #end
-
- public function toString():String {
- return "DES";
- }
-
-
-#if (neko || useOpenSSL)
- public static function __init__()
- {
- chx.Lib.initDll("openssl");
- }
-
- private static var des_create_key =
chx.Lib.load("openssl","des_create_key",1);
- private static var des_destroy_key =
chx.Lib.load("openssl","des_create_key",1);
- private static var des_encrypt_block =
chx.Lib.load("openssl","des_encrypt_block",2);
- private static var des_decrypt_block =
chx.Lib.load("openssl","des_decrypt_block",2);
-#end
-}
-
=======================================
--- /trunk/ext3/chx/crypt/IV.hx Thu Mar 31 15:35:58 2011
+++ /dev/null
@@ -1,180 +0,0 @@
-/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-private enum IvState {
- IV_UNINIT;
- IV_BLOCK;
- IV_STREAM_UNINIT;
- IV_STREAM_CONTINUE;
-}
-
-/**
- IV itself is not a block encryptor, and should not be called directly.
- Use a Mode that extends IV, like ModeCBC
-**/
-class IV {
- /** Setting the iv value only affects the next encryption process.
- The value returned from a get may not match the last set. Once
- an ecryption is complete, the next get on iv will reflect the
- changes.
- **/
- public var iv(getIV, setNextIV) : Bytes;
- public var cipher(default,null) : IBlockCipher;
- public var padding : IPad;
- var prepend : Bool;
- var startIV : Bytes;
- var currentIV : Bytes;
- var curValue : Bytes;
- var nextValue : Bytes;
- var state : IvState;
-
- public function new(bCipher: IBlockCipher, ?padMethod : IPad) {
- if(bCipher == null)
- throw "crypt.iv: null crypt";
- cipher = bCipher;
- if(padMethod == null)
- padding = new PadPkcs5(cipher.blockSize);
- else
- padding = padMethod;
- padding.blockSize = bCipher.blockSize;
- prepend = true;
- state = IV_UNINIT;
- }
-
- /**
- Prepending the IV to the crypted text is the default
- behaviour.
- **/
- public function setPrependMode( p : Bool ) : Void {
- prepend = p;
- }
-
- public function getIV() : Bytes {
- #if CAFFEINE_DEBUG
- trace("Cipher blockSize " + cipher.blockSize);
- if(curValue != null)
- trace("curValue: " + curValue.toHex());
- if(nextValue != null) {
- trace(nextValue.length);
- trace("nextValue: " + nextValue.toHex());
- }
- #end
- if(curValue == null) {
- if(nextValue == null) {
- var sb = new BytesBuffer();
- for(x in 0...cipher.blockSize) {
- sb.addByte(randomByte());
- }
- nextValue = sb.getBytes();
- }
- curValue = nextValue;
- nextValue = null;
- currentIV = curValue;
- }
- return curValue;
- }
-
- public function setNextIV( s : Bytes ) : Bytes {
- if(s.length % cipher.blockSize != 0 || s.length == 0)
- throw("crypt.iv: invalid length. Expected "+cipher.blockSize+ "
bytes.");
- var sb = new BytesBuffer();
- sb.add(s);
- nextValue = sb.getBytes().sub(0,cipher.blockSize);
- return s;
- }
-
- function prepareEncrypt( s : Bytes ) : Bytes {
- var buf = padding.pad(s);
- if(buf.length % cipher.blockSize != 0)
- throw "crypt.iv: padding error";
- // queues up the next iv and destroys the nextValue if it exists
- getIV();
- return buf;
- }
-
- /**
- In prepend mode, this will attach the IV to the
- beginning of the buffer. Destroys the current IV
- in preparation for next crypt function.
- **/
- function finishEncrypt( sb : Bytes ) : Bytes {
- var rv : Bytes = sb;
- if(prepend) {
- var buf : BytesBuffer = new BytesBuffer();
- buf.add(getIV());
- buf.add(sb);
- rv = buf.getBytes();
- }
- // don't destroy before call to getIV above
- curValue = null;
- //trace("Finished buffer: "+rv.toHex());
- return rv;
- }
-
- function prepareDecrypt( s : Bytes ) : Bytes {
- var buf : Bytes;
-
- if(prepend) {
- var biv = s.sub(0,cipher.blockSize);
- iv = biv;
- if(!BytesUtil.eq(iv, biv))
- throw "crypt.iv: invalid state";
- if(s.length - cipher.blockSize >= 0)
- buf = s.sub(cipher.blockSize, s.length - cipher.blockSize);
- else
- buf = BytesUtil.EMPTY;
- }
- else {
- buf = s;
- }
- if(buf.length % cipher.blockSize != 0)
- throw "crypt.iv: length error";
- return buf;
- }
-
- function finishDecrypt( s : Bytes ) : Bytes {
- var buf = padding.unpad(s);
- curValue = null;
- return buf;
- }
-
- private inline function randomByte() : Int {
- return Std.int(Math.random() * 256);
- }
-
- public function startStreamMode() : Void {
- if(state != IV_UNINIT)
- throw "Cipher in initialized state";
- state = IV_STREAM_UNINIT;
- }
-
- public function endStreamMode() : Void {
- state = IV_UNINIT;
- }
-}
=======================================
--- /trunk/ext3/chx/crypt/ModeCBC.hx Mon Jan 9 05:45:54 2012
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-class ModeCBC extends IV, implements IMode {
- public function new(bCipher: IBlockCipher, ?pad : IPad) {
- super(bCipher, pad);
- }
-
- public function toString() {
- if(cipher != null)
- return Std.string(cipher) + "-cbc";
- return "???-???-cbc";
- }
-
- /**
- * @TODO proper block padding, refer to ModeECB
- **/
- public function encrypt( s : Bytes ) : Bytes {
- var buf = prepareEncrypt( s );
- var bsize = cipher.blockSize;
- var numBlocks = Std.int(buf.length/bsize);
- var offset : Int = 0;
- var sb = new BytesBuffer();
-
- var curIV = iv;
- //trace("Starting IV: " + curIV.toHex());
- for (i in 0...numBlocks) {
- var tb = Bytes.alloc(cipher.blockSize);
- for(x in 0...cipher.blockSize) {
- var bc : Int = buf.get(offset + x);
- var ic : Int = curIV.get(x);
- tb.set(x, bc ^ ic );
- }
- var crypted = cipher.encryptBlock(tb);
- sb.add(crypted);
- curIV = crypted;
- offset += cipher.blockSize;
- }
- return finishEncrypt(sb.getBytes());
- }
-
- public function decrypt( s : Bytes ) : Bytes {
- var buf = prepareDecrypt( s );
- var bsize = cipher.blockSize;
- if(buf.length % bsize != 0)
- throw "Invalid buffer length";
- var numBlocks = Std.int(buf.length/bsize);
- var offset : Int = 0;
- var sb = new BytesBuffer();
-
- for (i in 0...numBlocks) {
- var rv = cipher.decryptBlock(buf.sub(offset, bsize));
- var tb = Bytes.alloc(bsize);
- for(x in 0...cipher.blockSize) {
- tb.set(x, rv.get(x) ^ currentIV.get(x));
- }
- sb.add(tb);
- currentIV = buf.sub(offset, cipher.blockSize);
- offset += bsize;
- }
- return finishDecrypt(sb.getBytes());
- }
-}
=======================================
--- /trunk/ext3/chx/crypt/ModeECB.hx Tue Jan 10 15:06:09 2012
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-class ModeECB implements IMode {
- public var cipher(default,null) : IBlockCipher;
- public var padding : IPad;
-
- public function new(bCipher: IBlockCipher, ?padMethod : IPad) {
- if(bCipher == null)
- throw "null crypt";
- cipher = bCipher;
- if(padMethod == null)
- padding = new PadPkcs5(bCipher.blockSize);
- else
- padding = padMethod;
- padding.blockSize = bCipher.blockSize;
- }
-
- public function toString() {
- if(cipher != null)
- return Std.string(cipher) + "-ecb";
- return "???-???-ecb";
- }
-
- public function encrypt( s : Bytes ) : Bytes {
- var buf : Bytes = null;
- var bsize = padding.blockSize;
- var numBlocks = padding.calcNumBlocks(s.length);
- var offset : Int = 0;
- var len : Int = 0;
- var rem : Int = s.length;
- var sb = new BytesBuffer();
-
- buf = padding.pad(s);
- for (i in 0...numBlocks) {
- var rv : Bytes = null;
- rv = buf.sub(offset, bsize);
- offset += bsize;
- var enc = cipher.encryptBlock(rv);
- if(enc.length != bsize)
- throw("block encryption to wrong block size");
- sb.add(enc);
- }
- return sb.getBytes();
- }
-
- public function decrypt( s : Bytes ) : Bytes {
- var bsize = padding.blockSize;
- if(s.length % bsize != 0)
- throw "Invalid message length " + s.length;
- var numBlocks = Std.int(s.length/bsize);
- var offset : Int = 0;
- var sb = new BytesBuffer();
- for (i in 0...numBlocks) {
- var rv : Bytes = cipher.decryptBlock(s.sub(offset, bsize));
- sb.add(rv);
- offset += bsize;
- }
- var b = sb.getBytes();
- return padding.unpad(b);
- }
-
- // These have no effect when using ECB mode.
- public function startStreamMode() : Void {}
- public function endStreamMode() : Void {}
-}
=======================================
--- /trunk/ext3/chx/crypt/PadBase.hx Tue Jan 10 15:06:09 2012
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (c) 2011, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-class PadBase implements IPad {
-
- public var blockSize(default,setBlockSize) : Int;
-
- public function new( blockSize : Null<Int> = null ) {
- if(blockSize != null)
- setBlockSize(blockSize);
- }
-
- public function pad( s : Bytes ) : Bytes {
- return throw new chx.lang.FatalException("not implemented");
- }
-
- public function unpad( s : Bytes ) : Bytes {
- return throw new chx.lang.FatalException("not implemented");
- }
-
- function setBlockSize(len : Int) : Int {
- blockSize = len;
- return len;
- }
-
- public function calcNumBlocks(len : Int) : Int {
- if(len == 0) return 0;
- var n : Int = Math.ceil(len/blockSize);
- // most pads will require an extra block if the input length
- // is an exact multiple of the block size
- if(len % blockSize == 0)
- n++;
- return n;
- }
-
-}
=======================================
--- /trunk/ext3/chx/crypt/PadNone.hx Tue Jan 10 15:06:09 2012
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (c) 2012, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-/**
- * Perform no padding.
- **/
-class PadNone extends PadBase, implements IPad {
-
- override public function pad( s : Bytes ) : Bytes {
- return s;
- }
-
- override public function unpad( s : Bytes ) : Bytes {
- return s;
- }
-
- override public function calcNumBlocks(len : Int) : Int {
- return Math.ceil(len/blockSize);
- }
-
-}
=======================================
--- /trunk/ext3/chx/crypt/PadNull.hx Tue Jan 10 15:06:09 2012
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-/**
- * Pads with NULL (0) bytes
- **/
-class PadNull implements IPad {
- public var blockSize(default,setBlockSize) : Int;
- public var textSize(default,null) : Int;
-
- public function new( blockSize : Null<Int> = null ) {
- if(blockSize != null)
- setBlockSize(blockSize);
- }
-
- public function pad( s : Bytes ) : Bytes {
- var r = blockSize - (s.length % blockSize);
- if(r == blockSize)
- return s;
- var sb = new BytesBuffer();
- sb.add(s);
- for(x in 0...r) {
- sb.addByte(0);
- }
- return sb.getBytes();
- }
-
- /**
- * Null padded strings can't be reliably unpadded, since the
- * source may contain nulls. It is up to the implementation to
- * keep track of how many bytes in the packet are used.
- **/
- public function unpad( s : Bytes ) : Bytes {
- return s;
- }
-
- public function calcNumBlocks(len : Int) : Int {
- return Math.ceil(len/blockSize);
- }
-
- private function setBlockSize( x : Int ) : Int {
- this.blockSize = x;
- this.textSize = x;
- return x;
- }
-}
=======================================
--- /trunk/ext3/chx/crypt/PadPkcs5.hx Mon Apr 11 20:26:42 2011
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-class PadPkcs5 extends PadBase, implements IPad {
-
- override public function calcNumBlocks(len : Int) : Int {
- var chr : Int = blockSize - (len % blockSize);
- Assert.isEqual(0, (len + chr) % blockSize);
- return Math.floor((len + chr) / blockSize);
- }
-
- override public function pad( s : Bytes ) : Bytes {
- var sb = new BytesBuffer();
- sb.add ( s );
- var chr : Int = blockSize - (s.length % blockSize);
- if(s.length == blockSize)
- chr = blockSize;
- for( i in 0...chr) {
- sb.addByte( chr );
- }
- var rv = sb.getBytes();
- return rv;
- }
-
- override public function unpad( s : Bytes ) : Bytes {
- if( s.length % blockSize != 0)
- throw "crypt.padpkcs5 unpad: buffer length "+s.length+" not multiple of
block size " + blockSize;
- var c : Int = s.get(s.length-1);
- var i = c;
- var pos = s.length - 1;
- while(i > 0) {
- var n = s.get(pos);
- if (c != n)
- throw "crypt.padpkcs5 unpad: invalid byte";
- pos--;
- i--;
- }
- return s.sub(0, s.length - c);
- }
-
-}
=======================================
--- /trunk/ext3/chx/crypt/PadSSL.hx Mon Apr 11 20:26:42 2011
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2011, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-/**
- * SSL padding. Just like TLS padding, but bytes other than last one
- * are arbitrary.
- * @todo pad could insert random data, other than last byte
- **/
-class PadSSL extends PadTLS, implements IPad {
- override public function unpad( s : Bytes ) : Bytes {
- if( s.length % blockSize != 0)
- throw new chx.lang.Exception("PadTLS unpad: buffer length "+s.length+"
not multiple of block size " + blockSize);
- return s.sub(0, s.length - s.get(s.length-1) - 1);
- }
-}
=======================================
--- /trunk/ext3/chx/crypt/PadTLS.hx Mon Apr 11 20:26:42 2011
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (c) 2011, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-/**
- * Very similar to PKCS5 padding, but adds one extra byte of the pad length
- * @todo In TLS, the padding may be any random length up to 255 bytes,
- * as per RFC 4346 Section 6.2.3.2, to decrease attacks on the
protocol.
- * Should add a method to allow for random pad lengths.
- **/
-class PadTLS extends PadBase, implements IPad {
-
- override public function pad( s : Bytes ) : Bytes {
- var c = blockSize - ((s.length+1) % blockSize);
- if (c <= 0) return s;
- var bb = new BytesBuffer();
- bb.add(s);
- for(i in 0...c+1) {
- bb.addByte(c);
- }
- return bb.getBytes();
- }
-
- override public function unpad( s : Bytes ) : Bytes {
- if( s.length % blockSize != 0)
- throw new chx.lang.Exception("PadTLS unpad: buffer length "+s.length+"
not multiple of block size " + blockSize);
- var c = s.get(s.length-1);
- var i:Int = c;
- var len = s.length;
- while(i > -1) {
- var n = s.get(pos);
- if (c != n)
- throw new chx.lang.Exception("PadTLS unpad: invalid byte");
- len--;
- i--;
- }
- return s.sub(0, len);
- }
-
-}
=======================================
--- /trunk/ext3/chx/crypt/TripleDes.hx Fri Apr 1 12:30:17 2011
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * Copyright (c) 2011, The Caffeine-hx project contributors
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-/*
- * TripleDESKey
- *
- * An Actionscript 3 implementation of Triple DES
- * Copyright (c) 2007 Henri Torgemane
- *
- * Derived from:
- * The Bouncy Castle Crypto package,
- * Copyright (c) 2000-2004 The Legion Of The Bouncy Castle
- * (http://www.bouncycastle.org)
- *
- * See LICENSE.txt for full license information.
- */
-package chx.crypt;
-import I32;
-
-/**
-* This supports 2TDES and 3TDES.
-* If the key passed is 128 bits, 2TDES is used.
-* If the key has 192 bits, 3TDES is used.
-* Other key lengths give "undefined" results.
-*/
-class TripleDes extends Des
-{
- #if (neko || cpp)
- private var key2:Dynamic;
- private var key3:Dynamic;
- #else
- private var encKey2:Array<Int32>;
- private var encKey3:Array<Int32>;
- private var decKey2:Array<Int32>;
- private var decKey3:Array<Int32>;
- #end
-
- public function new(key:Bytes)
- {
- if(key.length < 16)
- throw new chx.lang.OutsideBoundsException("Must be at least 16 bytes of
key data");
- super(key);
- #if (neko || cpp)
- this.key2 = des_create_key(key.sub(8,8).getData());
- if(key.length > 16)
- this.key3 = des_create_key(key.sub(16,8).getData());
- else
- this.key3 = des_create_key(key.sub(0,8).getData());
- #else
- encKey2 = generateWorkingKey(false, key, 8);
- decKey2 = generateWorkingKey(true, key, 8);
- if (key.length>16) {
- encKey3 = generateWorkingKey(true, key, 16);
- decKey3 = generateWorkingKey(false, key, 16);
- } else {
- encKey3 = encKey;
- decKey3 = decKey;
- }
- #end
- }
-
- public override function dispose():Void
- {
- super.dispose();
- #if (neko || cpp)
- des_destroy_key(key2);
- des_destroy_key(key3);
- #else
- var i:Int = 0;
- if (encKey2!=null) {
- for (i in 0...encKey2.length) { encKey2[i]=0; }
- encKey2=null;
- }
- if (encKey3!=null) {
- for (i in 0...encKey3.length) { encKey3[i]=0; }
- encKey3=null;
- }
- if (decKey2!=null) {
- for (i in 0...decKey2.length) { decKey2[i]=0; }
- decKey2=null;
- }
- if (decKey3!=null) {
- for (i in 0...decKey3.length) { decKey3[i]=0; }
- decKey3=null;
- }
- #end
- }
-
- public override function encryptBlock(block:Bytes):Bytes
- {
- #if (neko || cpp)
- return Bytes.ofData(des3_encrypt_block(key, key2, key3,
block.getData()));
- #else
- var outBlock = Bytes.alloc(block.length);
- desFunc(encKey, block, 0, outBlock, 0);
- desFunc(encKey2, outBlock, 0, outBlock, 0);
- desFunc(encKey3, outBlock, 0, outBlock, 0);
- return outBlock;
- #end
- }
-
- public override function decryptBlock(block:Bytes):Bytes
- {
- #if (neko || cpp)
- return Bytes.ofData(des3_decrypt_block(key, key2, key3,
block.getData()));
- #else
- var outBlock = Bytes.alloc(block.length);
- desFunc(decKey3, block, 0, outBlock, 0);
- desFunc(decKey2, outBlock, 0, outBlock, 0);
- desFunc(decKey, outBlock, 0, outBlock, 0);
- return outBlock;
- #end
- }
-
- public override function toString():String {
- return "3des";
- }
-
-#if (neko || cpp)
- private static var des_create_key =
chx.Lib.load("openssl","des_create_key",1);
- private static var des_destroy_key =
chx.Lib.load("openssl","des_create_key",1);
- private static var des3_encrypt_block =
chx.Lib.load("openssl","des3_encrypt_block",4);
- private static var des3_decrypt_block =
chx.Lib.load("openssl","des3_decrypt_block",4);
-#end
-}
=======================================
--- /trunk/ext3/chx/crypt/XXTea.hx Sun Feb 12 19:59:39 2012
+++ /dev/null
@@ -1,162 +0,0 @@
-/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
- * Original author : Russell Weir
- * Contributors:
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS
IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-package chx.crypt;
-
-import I32;
-
-class XXTea implements IBlockCipher {
-#if (neko || useNCrypt)
- var k : Void;
-#else
- var k : Array<Int>; // 16 bytes of key material
-#end
- public var blockSize(__getBlockSize,null) : Int;
-
- public function new(key : Bytes) {
- var l = key.length;
- if(l > 16)
- l = 16;
- var m = BytesUtil.bytesToInt32LE(
- BytesUtil.nullPad(key.sub(0,l), 16)
- );
-#if (neko || useNCrypt)
- k = xxtea_create_key(I32.mkNekoArray(m));
-#else
- k = I32.toNativeArray(m);
-#end
- blockSize = 8;
- }
-
- public function toString() : String {
- return "xxtea";
- }
-
- function __getBlockSize() : Int {
- return this.blockSize;
- }
-
- public function setBlocksize( i : Int ) : Int {
- if(i == 0 || i % 4 != 0)
- throw "xxtea: block size must be multiple of 4";
- blockSize = i;
- return i;
- }
-
- public function encryptBlock(plaintext : Bytes) : Bytes {
- if (plaintext.length == 0) return BytesUtil.EMPTY;
-#if (neko || useNCrypt)
- var v : Array<Int32> = BytesUtil.bytesToInt32LE(plaintext);
- var n = v.length;
- if (n == 1)
- v[n++] = Int32.ofInt(0);
- var rv = xxtea_encrypt_block(
- I32.mkNekoArray(v),
- n,
- k);
- return Bytes.ofData(rv);
-#else
- var v : Array<Int> =
I32.toNativeArray(BytesUtil.bytesToInt32LE(plaintext));
- var n = v.length;
- if (n == 1)
- v[n++] = 0;
-
- var delta = 0x9e3779B9;
- var e : Int;
- var mx : Int;
- var q = Std.int(6 + 52/n);
- var y = v[0];
- var z = v[n-1];
- var sum = 0;
-
- while (q-- > 0) {
- sum += delta;
- e = sum >>> 2 & 3;
- //for (p=0; p<n-1; p++) y = v[p+1], z = v[p] += MX;
- var p = 0;
- while(p < n-1) {
- y = v[(p+1)];
- mx = (((z>>>5)^(y<<2)) + ((y>>>3)^(z<<4))) ^ ((sum^y) +
(k[(p&3)^e]^z));
- z = v[p] += mx;
- p ++;
- }
- y = v[0];
- z = v[n-1] += (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3^e]^z);
- }
- return I32.packLE(cast v);
-#end
- }
-
- public function decryptBlock(ciphertext : Bytes) : Bytes
- {
- if (ciphertext.length == 0) return BytesUtil.EMPTY;
-#if (neko || useNCrypt)
- var v = BytesUtil.bytesToInt32LE(ciphertext);
- var n = v.length;
- var rv = xxtea_decrypt_block(
- I32.mkNekoArray(v),
- n,
- k);
- return Bytes.ofData(rv);
-#else
- var v : Array<Int> =
I32.toNativeArray(BytesUtil.bytesToInt32LE(ciphertext));
- var n = v.length;
-
- var delta = 0x9e3779B9;
- var e : Int;
- var mx : Int;
- var q : Int = Std.int(6 + 52/n);
- var y = v[0];
- var z = v[n-1];
- var sum = q * delta;
-
- while (sum != 0) {
- e = sum >>> 2 & 3;
- var p = n - 1;
- while(p > 0 ) {
- z = v[p-1];
- //mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3^e]^z);
- mx = (((z>>>5)^(y<<2)) + ((y>>>3)^(z<<4))) ^ ((sum^y) +
(k[(p&3)^e]^z));
- y = v[p] -= mx;
- p--;
- }
- z = v[n-1];
- y = v[0] -= (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3^e]^z);
- sum -= delta;
- }
- return I32.packLE(cast v);
-#end
- }
-
-
-#if (neko || useNCrypt)
- private static var xxtea_create_key =
chx.Lib.load("ncrypt","xxtea_create_key",1);
- private static var xxtea_encrypt_block =
chx.Lib.load("ncrypt","xxtea_encrypt_block",3);
- private static var xxtea_decrypt_block =
chx.Lib.load("ncrypt","xxtea_decrypt_block",3);
-#end
-}
-
=======================================
--- /trunk/ext3/chx/crypt/IBlockCipher.hx Mon Dec 20 16:52:29 2010
+++ /trunk/ext3/chx/crypt/IBlockCipher.hx Sat Feb 25 21:43:48 2012
@@ -28,7 +28,7 @@
package chx.crypt;
interface IBlockCipher {
- public var blockSize(__getBlockSize,null) : Int;
- public function encryptBlock( plain : Bytes ) : Bytes;
- public function decryptBlock( enc : Bytes ) : Bytes;
-}
+ var blockSize(__getBlockSize,null) : Int;
+ function encryptBlock( plain : Bytes ) : Bytes;
+ function decryptBlock( enc : Bytes ) : Bytes;
+}
=======================================
--- /trunk/ext3/chx/crypt/IMode.hx Mon Dec 20 16:52:29 2010
+++ /trunk/ext3/chx/crypt/IMode.hx Sat Feb 25 21:43:48 2012
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, The Caffeine-hx project contributors
+ * Copyright (c) 2012, The Caffeine-hx project contributors
* Original author : Russell Weir
* Contributors:
* All rights reserved.
@@ -27,17 +27,25 @@
package chx.crypt;
+import chx.io.Output;
+
interface IMode {
- //var blockSize : Int;
- var cipher(default, null) : IBlockCipher;
- var padding : IPad;
-
- function encrypt( s : Bytes ) : Bytes;
-
- function decrypt( s : Bytes ) : Bytes;
-
- function startStreamMode() : Void;
-
- function endStreamMode() : Void;
-
-}
+ var cipher(default, setCipher) : IBlockCipher;
+ var padding(default,setPadding) : IPad;
+ var blockSize(getBlockSize,never) : Int;
+
+ function init(params : CipherParams) : Void;
+ /**
+ * This method requires exactly the number of bytes in the
+ * cipher blockSize, and overwrites b
+ **/
+ function updateEncrypt( b : Bytes, out : Output) : Int;
+ function finalEncrypt( b : Bytes, out : Output) : Int;
+
+ /**
+ * This method requires exactly the number of bytes in the
+ * cipher blockSize, and overwrites b
+ **/
+ function updateDecrypt( b : Bytes, out : Output) : Int;
+ function finalDecrypt( b : Bytes, out : Output) : Int;
+}
=======================================
--- /trunk/ext3/chx/crypt/rsa/IBlockPad.hx Mon Feb 20 10:13:32 2012
+++ /trunk/ext3/chx/crypt/rsa/IBlockPad.hx Sat Feb 25 21:43:48 2012
@@ -25,7 +25,7 @@
* THE POSSIBILITY OF SUCH DAMAGE.
*/
-package chx.crypt;
+package chx.crypt.rsa;
interface IBlockPad {
/** finished padded block size **/
=======================================
--- /trunk/ext3/chx/crypt/rsa/PadBlockBase.hx Mon Feb 20 10:13:32 2012
+++ /trunk/ext3/chx/crypt/rsa/PadBlockBase.hx Sat Feb 25 21:43:48 2012
@@ -25,27 +25,25 @@
* THE POSSIBILITY OF SUCH DAMAGE.
*/
-package chx.crypt;
+package chx.crypt.rsa;
/**
* Pads that work on blocks, and not on the full buffer.
**/
-class PadBlockBase extends PadBase {
+class PadBlockBase extends chx.crypt.PadBase, implements IBlockPad {
/** the number of bytes that can fit in each block **/
public var textSize(default,null) : Int;
-
- override public function isBlockPad() : Bool { return true; }
override public function calcNumBlocks(len : Int) : Int {
- var ch : Int = blockSize - blockOverhead();
+ var ch : Int = getBytesReadPerBlock();
var n : Int = Math.ceil(len/ch);
- if(len % blockSize == 0)
- n++;
+ //if(len % blockSize == 0)
+ // n++;
return n;
}
override public function getBytesReadPerBlock() : Int {
- return blockSize - blockOverhead;
+ return blockSize - blockOverhead();
}
/**
=======================================
--- /trunk/ext3/chx/crypt/rsa/PadPkcs1Type1.hx Mon Feb 20 10:13:32 2012
+++ /trunk/ext3/chx/crypt/rsa/PadPkcs1Type1.hx Sat Feb 25 21:43:48 2012
@@ -25,14 +25,17 @@
* THE POSSIBILITY OF SUCH DAMAGE.
*/
-package chx.crypt;
+package chx.crypt.rsa;
+
+import chx.lang.Exception;
+import chx.lang.IllegalArgumentException;
+
+//http://tools.ietf.org/html/rfc2313 section 8.1
/**
- Pads string with 0xFF bytes
-**/
-class PadPkcs1Type1 implements IBlockPad {
- public var blockSize(default,setBlockSize) : Int;
- public var textSize(default,null) : Int;
+ * Pads with 0xFF bytes
+ **/
+class PadPkcs1Type1 extends PadBlockBase, implements IBlockPad {
/** only for Type1, the byte to pad with, default 0xFF **/
public var padByte(getPadByte,setPadByte) : Int;
var padCount : Int;
@@ -51,7 +54,7 @@
public function pad( s : Bytes ) : Bytes {
if(s.length > textSize)
- throw "Unable to pad block: provided buffer is " + s.length + " max
is " + textSize;
+ throw new Exception("Unable to pad block: provided buffer is " +
s.length + " max is " + textSize);
var sb = new BytesBuffer();
sb.addByte(0);
sb.addByte(typeByte);
@@ -81,10 +84,10 @@
while(i < s.length) {
while( i < s.length && s.get(i) == 0) ++i;
if(s.length-i-3-padCount < 0) {
- throw("Unexpected short message");
+ throw new Exception("Unexpected short message");
}
if(s.get(i) != typeByte)
- throw("Expected marker "+ typeByte + " at position "+i + " [" +
BytesUtil.hexDump(s) + "]");
+ throw new Exception("Expected marker "+ typeByte + " at position "+i
+ " [" + BytesUtil.hexDump(s) + "]");
if(++i >= s.length)
return sb.getBytes();
while(i < s.length && s.get(i) != 0) ++i;
@@ -99,12 +102,9 @@
public function calcNumBlocks(len : Int) : Int {
return Math.ceil(len/textSize);
}
-
- /** pads by block? **/
- public function isBlockPad() : Bool { return true; }
/** number of bytes padding needs per block **/
- public function blockOverhead() : Int { return 3 + padCount; }
+ override public function blockOverhead() : Int { return 3 + padCount; }
/**
PKCS1 has a 3 + padCount byte overhead per block. For RSA
@@ -113,7 +113,7 @@
**/
public function setPadCount(x : Int) : Int {
if(x + 3 >= blockSize)
- throw("Internal padding size exceeds crypt block size");
+ throw new IllegalArgumentException("Internal padding size exceeds crypt
block size");
padCount = x;
textSize = blockSize - 3 - padCount;
return x;
@@ -123,7 +123,7 @@
this.blockSize = x;
this.textSize = x - 3 - padCount;
if(textSize <= 0)
- throw "Block size " + x + " to small for Pkcs1 with padCount "+padCount;
+ throw new IllegalArgumentException("Block size " + x + " to small for
Pkcs1 with padCount "+padCount);
return x;
}
=======================================
--- /trunk/ext3/chx/crypt/rsa/PadPkcs1Type2.hx Mon Feb 20 10:13:32 2012
+++ /trunk/ext3/chx/crypt/rsa/PadPkcs1Type2.hx Sat Feb 25 21:43:48 2012
@@ -25,7 +25,7 @@
* THE POSSIBILITY OF SUCH DAMAGE.
*/
-package chx.crypt;
+package chx.crypt.rsa;
import math.prng.Random;
=======================================
--- /trunk/ext3/chx/crypt/rsa/RSA.hx Mon Feb 20 10:13:32 2012
+++ /trunk/ext3/chx/crypt/rsa/RSA.hx Sat Feb 25 21:43:48 2012
@@ -28,7 +28,7 @@
* Derived from javascript implementation Copyright (c) 2005 Tom Wu
*/
-package chx.crypt;
+package chx.crypt.rsa;
import math.BigInteger;
@@ -62,9 +62,9 @@
}
/**
- * Return the PKCS#1 RSA decryption of "ctext", an even-length hex string.
+ * Return the PKCS#1 RSA decryption of buf
*
- * @param ctext Hexadecimal string
+ * @param buf Bytes of any length
**/
public function decrypt( buf : Bytes ) : Bytes {
return doBufferDecrypt(buf, doPrivate, new PadPkcs1Type2(blockSize));
=======================================
--- /trunk/ext3/chx/crypt/rsa/RSAEncrypt.hx Mon Feb 20 10:13:32 2012
+++ /trunk/ext3/chx/crypt/rsa/RSAEncrypt.hx Sat Feb 25 21:43:48 2012
@@ -25,12 +25,7 @@
* THE POSSIBILITY OF SUCH DAMAGE.
*/
-/*
- * Derived from javascript implementation Copyright (c) 2005 Tom Wu
- *
- */
-
-package chx.crypt;
+package chx.crypt.rsa;
import math.BigInteger;
import math.prng.Random;
@@ -42,21 +37,68 @@
class RSAEncrypt implements IBlockCipher {
// public key
/** modulus **/
- public var n : BigInteger;
+ public var n(get_n,set_n) : BigInteger;
/** exponent. <2^31 **/
- public var e : Int;
+ public var e(get_e,set_e) : Int;
public var blockSize(__getBlockSize,null) : Int;
-
- public function new(nHex:String,eHex:String) {
+ public var blockPad(getBlockPad,setBlockPad) : IBlockPad;
+ #if useOpenSSL
+ var handle:Dynamic;
+ var iBlockPad : Int;
+ #end
+
+ public function new(nHex:String=null,eHex:String=null) {
init();
if(nHex != null)
setPublic(nHex, eHex);
}
private function init() {
+ #if useOpenSSL
+ if(handle == null)
+ handle = rsa_new();
+ #end
this.n = null;
this.e = 0;
}
+
+ public function getBlockPad() : IBlockPad {
+ return this.blockPad;
+ }
+
+ public function setBlockPad(v:IBlockPad) : IBlockPad {
+ this.blockPad = v;
+ return v;
+ }
+
+ function get_n() : BigInteger {
+ #if useOpenSSL
+ return BigInteger.hndToBigInt(rsa_get_n(handle));
+ #else
+ return this.n;
+ #end
+ }
+ function set_n(v:BigInteger) : BigInteger {
+ #if useOpenSSL
+ rsa_set_n(handle, BigInteger.bigIntToHnd(v));
+ #end
+ this.n = v;
+ return v;
+ }
+ function get_e() : Int {
+ #if useOpenSSL
+ return BigInteger.hndToBigInt(rsa_get_e(handle)).toInt();
+ #else
+ return this.e;
+ #end
+ }
+ function set_e(v:Int) : Int {
+ #if useOpenSSL
+ rsa_set_e(handle, BigInteger.bigIntToHnd(BigInteger.ofInt(v)));
+ #end
+ this.e = v;
+ return v;
+ }
/**
* Decrypts a pre-padded buffer.
@@ -65,7 +107,7 @@
* @return blockSize buffer with decrypted data.
**/
public function decryptBlock( enc : Bytes ) : Bytes {
- throw("Not a private key");
+ throw new chx.lang.UnsupportedException("Not a private key");
return null;
}
@@ -76,7 +118,12 @@
* TODO: Return Binary string, not text. Use padding etc...
**/
public function encrypt( buf : Bytes ) : Bytes {
+ #if useOpenSSL
+ var bd = rsa_public_encrypt(handle,RSA_PKCS1_PADDING(),buf,0,buf.length);
+ return Bytes.ofData(bd);
+ #else
return doBufferEncrypt(buf, doPublic, new PadPkcs1Type2(blockSize));
+ #end
}
/**
@@ -90,6 +137,10 @@
if(block.length != bsize)
throw("bad block size");
+ #if useOpenSSL
+ var bd = rsa_public_encrypt(handle,RSA_NO_PADDING(),untyped
block.getData(),0,block.length);
+ return Bytes.ofData(bd);
+ #else
var biv:BigInteger = BigInteger.ofBytes(block, true);
var biRes = doPublic(biv).toBytesUnsigned();
@@ -114,6 +165,7 @@
biRes = bb.getBytes();
}
return biRes;
+ #end
}
/**
@@ -132,6 +184,8 @@
/**
* Set the public key fields N (modulus) and E (public exponent)
* from hex strings.
+ * @throw chx.lang.NullPointerException null argument
+ * @throw chx.lang.IllegalArgumentError unparsable argument
**/
public function setPublic(nHex : String, eHex:String) : Void {
init();
@@ -139,22 +193,19 @@
throw new chx.lang.NullPointerException("nHex not set: " + nHex);
if(eHex == null || eHex.length == 0)
throw new chx.lang.NullPointerException("eHex not set: " + eHex);
- //try {
- var s : String = BytesUtil.cleanHexFormat(nHex);
- n = BigInteger.ofString(s, 16);
- if(n == null) throw 2;
- var ie : Null<Int> = Std.parseInt("0x" +
BytesUtil.cleanHexFormat(eHex));
- if(ie == null || ie == 0) throw 3;
- e = ie;
- //}
- //catch(e:Dynamic)
- // throw("Invalid RSA public key: " + e);
+
+ var s : String = BytesUtil.cleanHexFormat(nHex);
+ n = BigInteger.ofString(s, 16);
+ if(n == null)
+ throw new chx.lang.IllegalArgumentException("nHex not a valid big
integer: "+nHex);
+ var ie : Null<Int> = Std.parseInt("0x" +
BytesUtil.cleanHexFormat(eHex));
+ if(ie == null || ie == 0)
+ throw new chx.lang.IllegalArgumentException("eHex not a vlaid big
integer: "+eHex);
+ e = ie;
}
/**
* Verify a signature
- *
- * @todo http://www.imc.org/ietf-openpgp/mail-archive/msg14307.html
* @todo verify implementation
**/
public function verify( data : Bytes ) : Bytes {
@@ -174,12 +225,8 @@
**/
private function doBufferEncrypt(src:Bytes, f : BigInteger->BigInteger,
pf : IBlockPad) : Bytes
{
- //trace("source: " + src.toHex());
var bs = blockSize;
var ts : Int = bs - 11;
- #if CAFFEINE_DEBUG
- trace(">>>> Encrypting. Blocksize is "+bs + " src length:"+src.length
+ "["+src.toHex()+"]");
- #end
var idx : Int = 0;
var msg = new BytesBuffer();
while(idx < src.length) {
@@ -188,22 +235,10 @@
var m:BigInteger = BigInteger.ofBytes(pf.pad(src.sub(idx,ts)), true);
var c:BigInteger = f(m);
- #if CAFFEINE_DEBUG
- var d = m.toBytesUnsigned();
- var e = c.toBytesUnsigned();
- trace("m (padded) len " + d.length + " "+d.toHex(":"));
- trace("c (crypted) len " + e.length + " "+e.toHex(":"));
- #end
-
var h = c.toBytesUnsigned();
- //var
if((h.length & 1) != 0)
msg.addByte( 0 );
- #if CAFFEINE_DEBUG
- trace(">>>> crypted ("+h.length+"): " + h.toHex());
- #end
-
msg.add(h);
idx += ts;
}
@@ -212,13 +247,8 @@
private function doBufferDecrypt(src: Bytes, f : BigInteger->BigInteger,
pf : IBlockPad) : Bytes
{
- //trace("source: " + src.toHex());
var bs = blockSize;
- //bs *= 2; // hex string, 2 bytes per char
var ts : Int = bs - 11;
- #if CAFFEINE_DEBUG
- trace(">>>> Decrypting. Blocksize is "+ bs + " src length:"+src.length
+ "["+src.toHex()+"]");
- #end
var idx : Int = 0;
var msg = new BytesBuffer();
while(idx < src.length) {
@@ -228,14 +258,6 @@
var m = f(c);
if(m == null)
return null;
-
- #if CAFFEINE_DEBUG
- var d = m.toBytesUnsigned();
- var e = c.toBytesUnsigned();
- trace("c (crypted) len " + e.length + " "+e.toHex(":"));
- trace("m (padded) len " + d.length + " "+d.toHex(":"));
- #end
-
var up : Bytes = pf.unpad(m.toBytesUnsigned());
if(up.length > ts)
throw "block text length error";
@@ -254,9 +276,13 @@
// getters/setters //
//////////////////////////////////////////////////
function __getBlockSize() : Int {
+ #if useOpenSSL
+ return rsa_size(handle);
+ #else
if(n == null)
return 0;
return (n.bitLength()+7)>>3;
+ #end
}
//////////////////////////////////////////////////
@@ -274,5 +300,27 @@
return sb.toString();
*/
}
+
+ #if useOpenSSL
+ public static function __init__()
+ {
+ chx.Lib.initDll("openssl");
+ }
+
+ private static var rsa_new = chx.Lib.load("openssl","rsa_new",0);
+ private static var rsa_size = chx.Lib.load("openssl","rsa_size",1);
+ private static var rsa_set_n = chx.Lib.load("openssl","rsa_set_n",2);
+ private static var rsa_set_e = chx.Lib.load("openssl","rsa_set_e",2);
+ private static var rsa_get_n = chx.Lib.load("openssl","rsa_get_n",1);
+ private static var rsa_get_e = chx.Lib.load("openssl","rsa_get_e",1);
+
+ private static var rsa_public_encrypt =
chx.Lib.load("openssl","rsa_public_encrypt",5);
+
+ private static var RSA_PKCS1_PADDING =
chx.Lib.load("openssl","_RSA_PKCS1_PADDING",0);
+ private static var RSA_PKCS1_OAEP_PADDING =
chx.Lib.load("openssl","_RSA_PKCS1_OAEP_PADDING",0);
+ private static var RSA_SSLV23_PADDING =
chx.Lib.load("openssl","_RSA_SSLV23_PADDING",0);
+ private static var RSA_NO_PADDING =
chx.Lib.load("openssl","_RSA_NO_PADDING",0);
+
+ #end
}