[caffeine-hx] r663 committed - Adds block CFB mode. Updated toString() methods

0 views
Skip to first unread message

caffe...@googlecode.com

unread,
Feb 26, 2012, 5:24:29 PM2/26/12
to caffein...@googlegroups.com
Revision: 663
Author: damon...@gmail.com
Date: Sun Feb 26 14:23:40 2012
Log: Adds block CFB mode. Updated toString() methods


http://code.google.com/p/caffeine-hx/source/detail?r=663

Added:
/trunk/ext3/chx/crypt/mode/CFB.hx
Modified:
/trunk/ext3/chx/crypt/IMode.hx
/trunk/ext3/chx/crypt/mode/CBC.hx
/trunk/ext3/chx/crypt/mode/CFB8.hx
/trunk/ext3/chx/crypt/mode/CTR8.hx
/trunk/ext3/chx/crypt/mode/ECB.hx
/trunk/ext3/chx/crypt/mode/ModeBase.hx
/trunk/ext3/chx/crypt/mode/OFB.hx

=======================================
--- /dev/null
+++ /trunk/ext3/chx/crypt/mode/CFB.hx Sun Feb 26 14:23:40 2012
@@ -0,0 +1,106 @@
+/*
+ * 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 Feedback block mode
+ **/
+class CFB extends IVBase, implements chx.crypt.IMode {
+
+ override public function toString() {
+ return "cfb" + (cipher == null ? "??" : Std.string(cipher.blockSize *
8));
+ }
+
+ override public function updateEncrypt( b : Bytes, out : Output) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateEncrypt: ");
+ var pt : String = b.toHex();
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length != n)
+ return 0;
+
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + currentIV.toHex());
+ #end
+ currentIV = cipher.encryptBlock(currentIV);
+
+ var tmp = Bytes.alloc(n);
+ for(i in 0...n)
+ tmp.set(i, currentIV.get(i) ^ b.get(i));
+
+ #if CAFFEINE_DEBUG
+ trace("Output Block: " + currentIV.toHex());
+ trace("Plaintext: " + pt);
+ trace("Ciphertext: " + tmp.toHex());
+ out = orig;
+ #end
+ out.writeBytes(tmp,0,n);
+ currentIV.blit(0, tmp, 0, n);
+ return n;
+ }
+
+ override public function updateDecrypt( b : Bytes, out : Output ) : Int {
+ #if CAFFEINE_DEBUG
+ trace("updateEncrypt: ");
+ var pt : String = b.toHex();
+ var orig = out;
+ out = new BytesOutput();
+ #end
+
+ var n = cipher.blockSize;
+ if(b.length != n)
+ return 0;
+
+ #if CAFFEINE_DEBUG
+ trace("Input Block: " + currentIV.toHex());
+ #end
+ currentIV = cipher.encryptBlock(currentIV);
+ var tmp : Bytes = b.sub(0,n);
+
+ for(i in 0...n)
+ b.set(i, currentIV.get(i) ^ b.get(i));
+
+ #if CAFFEINE_DEBUG
+ trace("Output Block: " + currentIV.toHex());
+ trace("Ciphertext: " + pt);
+ trace("Plaintext: " + b.toHex());
+ out = orig;
+ #end
+ currentIV.blit(0, tmp, 0, n);
+ out.writeBytes(b,0,n);
+ return n;
+ }
+
+}
=======================================
--- /trunk/ext3/chx/crypt/IMode.hx Sat Feb 25 21:43:48 2012
+++ /trunk/ext3/chx/crypt/IMode.hx Sun Feb 26 14:23:40 2012
@@ -48,4 +48,6 @@
**/
function updateDecrypt( b : Bytes, out : Output) : Int;
function finalDecrypt( b : Bytes, out : Output) : Int;
-}
+
+ function toString() : String;
+}
=======================================
--- /trunk/ext3/chx/crypt/mode/CBC.hx Sat Feb 25 21:43:48 2012
+++ /trunk/ext3/chx/crypt/mode/CBC.hx Sun Feb 26 14:23:40 2012
@@ -35,7 +35,7 @@
**/
class CBC extends IVBase, implements chx.crypt.IMode {

- public function toString() {
+ override public function toString() {
return "cbc";
}

=======================================
--- /trunk/ext3/chx/crypt/mode/CFB8.hx Sat Feb 25 21:43:48 2012
+++ /trunk/ext3/chx/crypt/mode/CFB8.hx Sun Feb 26 14:23:40 2012
@@ -35,7 +35,7 @@
**/
class CFB8 extends IVBase, implements chx.crypt.IMode {

- public function toString() {
+ override public function toString() {
return "cfb8";
}

=======================================
--- /trunk/ext3/chx/crypt/mode/CTR8.hx Sun Feb 26 12:13:55 2012
+++ /trunk/ext3/chx/crypt/mode/CTR8.hx Sun Feb 26 14:23:40 2012
@@ -46,7 +46,7 @@
ctr_inc = 1;
}

- public function toString() {
+ override public function toString() {
return "ctr";
}

=======================================
--- /trunk/ext3/chx/crypt/mode/ECB.hx Sat Feb 25 21:43:48 2012
+++ /trunk/ext3/chx/crypt/mode/ECB.hx Sun Feb 26 14:23:40 2012
@@ -29,7 +29,7 @@

class ECB extends ModeBase, implements chx.crypt.IMode {

- public function toString() {
+ override public function toString() {
return "ecb";
}

=======================================
--- /trunk/ext3/chx/crypt/mode/ModeBase.hx Sat Feb 25 21:43:48 2012
+++ /trunk/ext3/chx/crypt/mode/ModeBase.hx Sun Feb 26 14:23:40 2012
@@ -47,6 +47,10 @@
public function new() {
padding = new PadPkcs5();
}
+
+ public function toString() {
+ return "??";
+ }

public function updateEncrypt( b : Bytes, out : Output) : Int {
throw new chx.lang.FatalException("not implemented");
=======================================
--- /trunk/ext3/chx/crypt/mode/OFB.hx Sat Feb 25 21:43:48 2012
+++ /trunk/ext3/chx/crypt/mode/OFB.hx Sun Feb 26 14:23:40 2012
@@ -35,7 +35,7 @@
**/
class OFB extends IVBase, implements chx.crypt.IMode {

- public function toString() {
+ override public function toString() {
return "ofb";
}

Reply all
Reply to author
Forward
0 new messages