diff dcrypt/crypto/ciphers/AES.d @ 27:8b5eaf3c2979

Fixed error in hash message padding reported by Glenn Haecker.
author Thomas Dixon <reikon@reikon.us>
date Sat, 09 May 2009 23:29:20 -0400
parents 176c933827a8
children ad687db713a4
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/AES.d	Sun Mar 01 13:06:48 2009 -0500
+++ b/dcrypt/crypto/ciphers/AES.d	Sat May 09 23:29:20 2009 -0400
@@ -19,8 +19,10 @@
  * Conforms: FIPS-197
  * References: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
  */
-class AES : BlockCipher {
-    private {
+class AES : BlockCipher
+{
+    private
+    {
         // Round constants
         static const uint[10] RCON = [
             0x01000000u, 0x02000000u, 0x04000000u, 0x08000000u, 0x10000000u,
@@ -644,31 +646,37 @@
        
     } // end private
     
-    char[] name() {
+    char[] name()
+    {
         return "AES";
     }
     
-    uint rounds() {
+    uint rounds()
+    {
         if (!_initialized)
             throw new NotInitializedError(name()~": Cipher not initialized.");
         return ROUNDS;
     }
     
-    uint blockSize() {
+    uint blockSize()
+    {
         return BLOCK_SIZE;
     }
     
-    void init(bool encrypt, CipherParameters params) {
+    void init(bool encrypt, CipherParameters params)
+    {
         SymmetricKey keyParams = cast(SymmetricKey)params;
         if (!keyParams)
             throw new InvalidParameterError(
                     name()~": Invalid parameter passed to init");
+                    
         _encrypt = encrypt;
         
         uint len = keyParams.key.length;
         if (len != 16 && len != 24 && len != 32)
             throw new InvalidKeyError(
-                    name()~": Invalid key length (requires 16, 24 or 32 bytes)");     
+                    name()~": Invalid key length (requires 16, 24 or 32 bytes)");
+                        
         workingKey = keyParams.key;
         
         setup(workingKey);
@@ -676,11 +684,14 @@
         _initialized = true;
     }
     
-    private void encryptBlock() {
+    private void encryptBlock()
+    {
         uint i = 4,
              r = ROUNDS >> 1,
              t0, t1, t2, t3;
-        while (--r >= 0) {
+             
+        while (--r >= 0)
+        {
             t0 = w[i++] ^ TE0[s0 >> 24] ^
                           TE1[cast(ubyte)(s1 >> 16)] ^
                           TE2[cast(ubyte)(s2 >> 8)] ^ 
@@ -738,11 +749,14 @@
                        S[cast(ubyte) t2];
     }
     
-    private void decryptBlock() {
+    private void decryptBlock()
+    {
         uint i = 4,
              r = ROUNDS >> 1,
              t0, t1, t2, t3;
-        while (--r >= 0) {
+             
+        while (--r >= 0)
+        {
             t0 = w[i++] ^ TD0[s0 >> 24] ^
                           TD1[cast(ubyte)(s3 >> 16)] ^
                           TD2[cast(ubyte)(s2 >> 8)] ^ 
@@ -799,7 +813,8 @@
                        RS[cast(ubyte) t0];
     }
     
-    uint update(void[] input_, void[] output_) {
+    uint update(void[] input_, void[] output_)
+    {
         if (!_initialized)
             throw new NotInitializedError(name()~": Cipher not initialized.");
         
@@ -829,14 +844,16 @@
     
     void reset() {}
     
-    private uint subWord(uint x) {
+    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]));
     }
 
-    private void setup(ubyte[] key) {
+    private void setup(ubyte[] key)
+    {
         uint nk = key.length / 4;
         ROUNDS = nk + 6;
         w = new uint[4*(ROUNDS+1)];
@@ -844,7 +861,8 @@
         for (uint i = 0, j = 0; i < nk; i++, j+=4)
             w[i] = ByteConverter.BigEndian.to!(uint)(key[j..j+int.sizeof]);
         
-        for (uint i = nk; i < w.length; i++) {
+        for (uint i = nk; i < w.length; i++)
+        {
             uint t = w[i-1];
 
             if (i % nk == 0)
@@ -855,23 +873,27 @@
             w[i] = w[i-nk] ^ t;
         }
         
-        if (!_encrypt) {
+        if (!_encrypt)
+        {
             for (uint i = 0; i <= 4*ROUNDS; i+=4)
                 w[i..i+4].reverse;
             w.reverse;
             
-            for (uint i = 4; i < w.length-4; i++) {
-                w[i] = (TD0[S[w[i]>>24]] ^
+            for (uint i = 4; i < w.length-4; i++)
+            {
+                w[i] = (TD0[S[w[i]>>24]]              ^
                         TD1[S[cast(ubyte)(w[i]>>16)]] ^
-                        TD2[S[cast(ubyte)(w[i]>>8)]] ^
+                        TD2[S[cast(ubyte)(w[i]>>8)]]  ^
                         TD3[S[cast(ubyte)w[i]]]);
             }
         }
     }
     
     /** Some AES test vectors from the FIPS-197 paper and BC. */
-    debug (UnitTest) {
-        unittest {
+    debug (UnitTest)
+    {
+        unittest
+        {
             static const char[][] test_keys = [
                 "000102030405060708090a0b0c0d0e0f",
                 "000102030405060708090a0b0c0d0e0f1011121314151617",
@@ -901,7 +923,8 @@
             ];
                 
             AES t = new AES();
-            foreach (uint i, char[] test_key; test_keys) {
+            foreach (uint i, char[] test_key; test_keys)
+            {
                 ubyte[] buffer = new ubyte[t.blockSize];
                 char[] result;
                 SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key));