diff dcrypt/crypto/ciphers/XTEA.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/XTEA.d	Sun Mar 01 13:06:48 2009 -0500
+++ b/dcrypt/crypto/ciphers/XTEA.d	Sat May 09 23:29:20 2009 -0400
@@ -13,12 +13,14 @@
 
 /** Implementation of the XTEA cipher designed by
     David Wheeler and Roger Needham. */
-class XTEA : BlockCipher {
-    private {
+class XTEA : BlockCipher
+{
+    private
+    {
         const uint ROUNDS = 32,
                    KEY_SIZE = 16,
                    BLOCK_SIZE = 8,
-                   DELTA = 0x9e3779b9;
+                   DELTA = 0x9e3779b9u;
         uint[] subkeys,
                sum0,
                sum1;
@@ -26,19 +28,23 @@
     
     void reset(){}
     
-    char[] name() {
+    char[] name()
+    {
         return "XTEA";
     }
     
-    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;
                     
         if (keyParams.key.length != KEY_SIZE)
@@ -54,15 +60,18 @@
             subkeys[i] = ByteConverter.BigEndian.to!(uint)(keyParams.key[j..j+int.sizeof]);
             
         // Precompute the values of sum + k[] to speed up encryption
-        for (i = j = 0; i < ROUNDS; i++) {
+        for (i = j = 0; i < ROUNDS; i++)
+        {
             sum0[i] = (j + subkeys[j & 3]);
             j += DELTA;
             sum1[i] = (j + subkeys[j >> 11 & 3]);
         }
+        
         _initialized = true;
     }
     
-    uint update(void[] input_, void[] output_) {
+    uint update(void[] input_, void[] output_)
+    {
         if (!_initialized)
             throw new NotInitializedError(name()~": Cipher not initialized");
             
@@ -78,13 +87,18 @@
         uint v0 = ByteConverter.BigEndian.to!(uint)(input[0..4]),
              v1 = ByteConverter.BigEndian.to!(uint)(input[4..8]);
              
-        if (_encrypt) {
-            for (int i = 0; i < ROUNDS; i++) {
+        if (_encrypt)
+        {
+            for (int i = 0; i < ROUNDS; i++)
+            {
                 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ sum0[i];
                 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ sum1[i];
             }
-        } else {
-            for (int i = ROUNDS-1; i >= 0; i--) {
+        }
+        else
+        {
+            for (int i = ROUNDS-1; i >= 0; i--)
+            {
                 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ sum1[i];
                 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ sum0[i];
             }
@@ -97,8 +111,10 @@
     }
     
     /** Some XTEA test vectors. */
-    debug (UnitTest) {
-        unittest {
+    debug (UnitTest)
+    {
+        unittest
+        {
             static const char[][] test_keys = [
                 "00000000000000000000000000000000",
                 "00000000000000000000000000000000",
@@ -139,7 +155,8 @@
             ];
                 
             XTEA t = new XTEA();
-            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));