diff dcrypt/crypto/ciphers/AES.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 5ce3012f1def
children 176c933827a8
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/AES.d	Sat Jan 10 13:17:58 2009 -0500
+++ b/dcrypt/crypto/ciphers/AES.d	Sat Feb 14 19:58:20 2009 -0500
@@ -8,7 +8,8 @@
 
 module dcrypt.crypto.ciphers.AES;
 
-import dcrypt.misc.Util;
+import dcrypt.misc.ByteConverter;
+import dcrypt.misc.Bitwise;
 import dcrypt.crypto.BlockCipher;
 
 /**
@@ -811,17 +812,17 @@
         if (output.length < BLOCK_SIZE)
             throw new ShortBufferError(name()~": Output buffer too short");
         
-        s0 = w[0] ^ Util.ubytesToUintBig(input, 0);
-        s1 = w[1] ^ Util.ubytesToUintBig(input, 4);
-        s2 = w[2] ^ Util.ubytesToUintBig(input, 8);
-        s3 = w[3] ^ Util.ubytesToUintBig(input, 12);
+        s0 = w[0] ^ ByteConverter.BigEndian.to!(uint)(input[0..4]);
+        s1 = w[1] ^ ByteConverter.BigEndian.to!(uint)(input[4..8]);
+        s2 = w[2] ^ ByteConverter.BigEndian.to!(uint)(input[8..12]);
+        s3 = w[3] ^ ByteConverter.BigEndian.to!(uint)(input[12..16]);
              
-        if (_encrypt) encryptBlock(); else decryptBlock();
+        (_encrypt) ? encryptBlock() : decryptBlock();
 
-        Util.uintToUbytesBig(s0, output, 0);
-        Util.uintToUbytesBig(s1, output, 4);
-        Util.uintToUbytesBig(s2, output, 8);
-        Util.uintToUbytesBig(s3, output, 12);
+        output[0..4] = ByteConverter.BigEndian.from!(uint)(s0);
+        output[4..8] = ByteConverter.BigEndian.from!(uint)(s1);
+        output[8..12] = ByteConverter.BigEndian.from!(uint)(s2);
+        output[12..16] = ByteConverter.BigEndian.from!(uint)(s3);
         
         return BLOCK_SIZE;
     }
@@ -829,10 +830,10 @@
     void reset() {}
     
     private uint subWord(uint x) {
-        return ((S[x>>24] << 24)            |
-               (S[cast(ubyte)(x>>8)] << 8)  |
-               (S[cast(ubyte)(x>>16)] << 16)|
-               (S[cast(ubyte)x]));
+        return ((S[x>>24] << 24)             |
+                (S[cast(ubyte)(x>>8)] << 8)  |
+                (S[cast(ubyte)(x>>16)] << 16)|
+                (S[cast(ubyte)x]));
     }
 
     private void setup(ubyte[] key) {
@@ -841,13 +842,13 @@
         w = new uint[4*(ROUNDS+1)];
         
         for (uint i = 0, j = 0; i < nk; i++, j+=4)
-            w[i] = Util.ubytesToUintBig(key, j);
+            w[i] = ByteConverter.BigEndian.to!(uint)(key[j..j+int.sizeof]);
         
         for (uint i = nk; i < w.length; i++) {
             uint t = w[i-1];
 
             if (i % nk == 0)
-                t = subWord(Util.rotateLeft(t, 8)) ^ RCON[(i/nk)-1];
+                t = subWord(Bitwise.rotateLeft(t, 8)) ^ RCON[(i/nk)-1];
             else if (nk > 6 && (i % nk == 4))
                 t = subWord(t);
             w[i] = w[i-nk] ^ t;
@@ -902,19 +903,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]~")");
             }