diff dcrypt/crypto/ciphers/RC6.d @ 23:4589f8c5eb3c

Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
author Thomas Dixon <reikon@reikon.us>
date Sat, 14 Feb 2009 19:58:20 -0500
parents ec23779ee794
children 176c933827a8
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/RC6.d	Sat Jan 10 13:17:58 2009 -0500
+++ b/dcrypt/crypto/ciphers/RC6.d	Sat Feb 14 19:58:20 2009 -0500
@@ -8,7 +8,8 @@
 
 module dcrypt.crypto.ciphers.RC6;
 
-import dcrypt.misc.Util;
+import dcrypt.misc.Bitwise;
+import dcrypt.misc.ByteConverter;
 import dcrypt.crypto.BlockCipher;
 
 /**
@@ -76,10 +77,10 @@
         if (output.length < BLOCK_SIZE)
             throw new ShortBufferError(name()~": Output buffer too short");
         
-        uint A = Util.ubytesToUintLittle(input, 0),
-             B = Util.ubytesToUintLittle(input, 4),
-             C = Util.ubytesToUintLittle(input, 8),
-             D = Util.ubytesToUintLittle(input, 12),
+        uint A = ByteConverter.LittleEndian.to!(uint)(input[0..4]),
+             B = ByteConverter.LittleEndian.to!(uint)(input[4..8]),
+             C = ByteConverter.LittleEndian.to!(uint)(input[8..12]),
+             D = ByteConverter.LittleEndian.to!(uint)(input[12..16]),
              t,
              u;
              
@@ -87,10 +88,10 @@
             B += S[0];
             D += S[1];
             for (int i = 1; i <= ROUNDS; i++) {
-                t = Util.rotateLeft(B*((B<<1)+1), 5);
-                u = Util.rotateLeft(D*((D<<1)+1), 5);
-                A = Util.rotateLeft(A^t, u) + S[i<<1];
-                C = Util.rotateLeft(C^u, t) + S[(i<<1)+1];
+                t = Bitwise.rotateLeft(B*((B<<1)+1), 5);
+                u = Bitwise.rotateLeft(D*((D<<1)+1), 5);
+                A = Bitwise.rotateLeft(A^t, u) + S[i<<1];
+                C = Bitwise.rotateLeft(C^u, t) + S[(i<<1)+1];
                 t = A;
                 A = B;
                 B = C;
@@ -108,19 +109,19 @@
                 C = B;
                 B = A;
                 A = t;
-                u = Util.rotateLeft(D*((D<<1)+1), 5);
-                t = Util.rotateLeft(B*((B<<1)+1), 5);
-                C = Util.rotateRight(C-S[(i<<1)+1], t) ^ u;
-                A = Util.rotateRight(A-S[i<<1], u) ^ t;
+                u = Bitwise.rotateLeft(D*((D<<1)+1), 5);
+                t = Bitwise.rotateLeft(B*((B<<1)+1), 5);
+                C = Bitwise.rotateRight(C-S[(i<<1)+1], t) ^ u;
+                A = Bitwise.rotateRight(A-S[i<<1], u) ^ t;
             }
             D -= S[1];
             B -= S[0];
         }
 
-        Util.uintToUbytesLittle(A, output, 0);
-        Util.uintToUbytesLittle(B, output, 4);
-        Util.uintToUbytesLittle(C, output, 8);
-        Util.uintToUbytesLittle(D, output, 12);
+        output[0..4] = ByteConverter.LittleEndian.from!(uint)(A);
+        output[4..8] = ByteConverter.LittleEndian.from!(uint)(B);
+        output[8..12] = ByteConverter.LittleEndian.from!(uint)(C);
+        output[12..16] = ByteConverter.LittleEndian.from!(uint)(D);
         
         return BLOCK_SIZE;
     }
@@ -133,7 +134,7 @@
         uint c = key.length/4;
         uint[] L = new uint[c];
         for (int i = 0, j = 0; i < c; i++, j+=4)
-            L[i] = Util.ubytesToUintLittle(key, j);
+            L[i] = ByteConverter.LittleEndian.to!(uint)(key[j..j+int.sizeof]);
             
         S[0] = P;
         for (int i = 1; i <= 2*ROUNDS+3; i++)
@@ -141,8 +142,8 @@
             
         uint A, B, i, j, v = 3*(2*ROUNDS+4); // Relying on ints initializing to 0   
         for (int s = 1; s <= v; s++) {
-            A = S[i] = Util.rotateLeft(S[i]+A+B, 3);
-            B = L[j] = Util.rotateLeft(L[j]+A+B, A+B);
+            A = S[i] = Bitwise.rotateLeft(S[i]+A+B, 3);
+            B = L[j] = Bitwise.rotateLeft(L[j]+A+B, A+B);
             i = (i + 1) % (2*ROUNDS+4);
             j = (j + 1) % c;
         }
@@ -186,19 +187,19 @@
             foreach (uint i, char[] test_key; test_keys) {
                 ubyte[] buffer = new ubyte[t.blockSize];
                 char[] result;
-                SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key));
+                SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key));
                 
                 // Encryption
                 t.init(true, key);
-                t.update(Util.hexToUbytes(test_plaintexts[i]), buffer);
-                result = Util.ubytesToHex(buffer);
+                t.update(ByteConverter.hexDecode(test_plaintexts[i]), buffer);
+                result = ByteConverter.hexEncode(buffer);
                 assert(result == test_ciphertexts[i],
                         t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
             
                 // Decryption
                 t.init(false, key);
-                t.update(Util.hexToUbytes(test_ciphertexts[i]), buffer);
-                result = Util.ubytesToHex(buffer);
+                t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer);
+                result = ByteConverter.hexEncode(buffer);
                 assert(result == test_plaintexts[i],
                         t.name~": ("~result~") != ("~test_plaintexts[i]~")");
             }