diff dcrypt/crypto/ciphers/XTEA.d @ 8:23c62e28b3a4

Reworked symmetric cipher classes to have SymmetricCipher as their superclass, and follow the general interface of init(), process(), etc. Made sure everything still passed test vectors. Removed Cipher class. I'll worry about that shit when we support something other than symmetric ciphers.
author Thomas Dixon <reikon@reikon.us>
date Mon, 18 Aug 2008 01:14:37 -0400
parents 5cb17e09d685
children 8c7f8fecdd75
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/XTEA.d	Sat Aug 16 22:55:38 2008 -0400
+++ b/dcrypt/crypto/ciphers/XTEA.d	Mon Aug 18 01:14:37 2008 -0400
@@ -64,21 +64,17 @@
         initialized = true;
     }
     
-    uint processBlock(void[] input_, uint inOff, void[] output_, uint outOff) {
+    ubyte[] process(void[] input_) {
         if (!initialized)
             throw new NotInitializedError(name()~": Cipher not initialized");
             
         ubyte[] input = cast(ubyte[]) input_;
-        ubyte[] output = cast(ubyte[]) output_;
                     
-        if ((inOff + BLOCK_SIZE) > input.length)
+        if (input.length < blockSize)
             throw new ShortBufferError(name()~": Input buffer too short");
-            
-        if ((outOff + BLOCK_SIZE) > output.length)
-            throw new ShortBufferError(name()~": Output buffer too short");
         
-        uint v0 = Util.ubytesToUintBig(input, inOff),
-             v1 = Util.ubytesToUintBig(input, inOff+4);
+        uint v0 = Util.ubytesToUintBig(input, 0),
+             v1 = Util.ubytesToUintBig(input, 4);
              
         if (encrypt) {
             for (int i = 0; i < ROUNDS; i++) {
@@ -92,10 +88,11 @@
             }
         }
         
-        Util.uintToUbytesBig(v0, output, outOff);
-        Util.uintToUbytesBig(v1, output, outOff+4);
+        ubyte[] output = new ubyte[blockSize];
+        Util.uintToUbytesBig(v0, output, 0);
+        Util.uintToUbytesBig(v1, output, 4);
         
-        return BLOCK_SIZE;
+        return output;
     }
     
     /** Some XTEA test vectors. */
@@ -148,14 +145,14 @@
                 
                 // Encryption
                 t.init(true, key);
-                t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0);
+                buffer = t.process(Util.hexToUbytes(test_plaintexts[i]));
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_ciphertexts[i],
                         t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
     
                 // Decryption
                 t.init(false, key);
-                t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0);
+                buffer = t.process(Util.hexToUbytes(test_ciphertexts[i]));
                 result = Util.ubytesToHex(buffer);
                 assert(result == test_plaintexts[i],
                         t.name~": ("~result~") != ("~test_plaintexts[i]~")");