gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r25650 - in gnunet-update/src: gnunet_update tests


From: gnunet
Subject: [GNUnet-SVN] r25650 - in gnunet-update/src: gnunet_update tests
Date: Tue, 25 Dec 2012 20:15:13 +0100

Author: harsha
Date: 2012-12-25 20:15:13 +0100 (Tue, 25 Dec 2012)
New Revision: 25650

Modified:
   gnunet-update/src/gnunet_update/util.py
   gnunet-update/src/tests/Makefile.am
   gnunet-update/src/tests/compatibility.c
   gnunet-update/src/tests/test_util.py
Log:
- fixing AES encryption

Modified: gnunet-update/src/gnunet_update/util.py
===================================================================
--- gnunet-update/src/gnunet_update/util.py     2012-12-25 18:14:26 UTC (rev 
25649)
+++ gnunet-update/src/gnunet_update/util.py     2012-12-25 19:15:13 UTC (rev 
25650)
@@ -452,11 +452,51 @@
         assert (len(self.key) == self.KEY_SIZE)
         assert (len(self.iv) == self.IV_SIZE)
 
+def setup_aes_cipher_ (aes_key):
+    """Initializes the AES object with settings similar to those in GNUnet.
+    
+    aes_key: the AESKey object
+    Returns the newly initialized AES object
+    """
+    return AES.new (aes_key.key, AES.MODE_CFB, aes_key.iv, segment_size=128)
+
+def aes_pad_ (data):
+    """Adds padding to the data such that the size of the data is a multiple of
+    16 bytes
+    
+    data: the data string
+    Returns a tuple:(pad_len, data). pad_len denotes the number of bytes added
+    as padding; data is the new data string with padded bytes at the end
+    """
+    pad_len = len(data) % 16
+    if (0 != pad_len):
+        pad_len = 16 - pad_len
+        pad_bytes = bytearray (15)
+        data += str(pad_bytes[:pad_len])
+    return (pad_len, data)
+
 def aes_encrypt (aes_key, data):
-    cipher = AES.new (aes_key.key, AES.MODE_CFB, aes_key.iv)
-    return cipher.encrypt (data)
+    """Encrypts the given data using AES.
 
+    aes_key: the AESKey object to use for AES encryption
+    data: the data string to encrypt
+    """
+    (pad_len, data) = aes_pad_ (data)
+    cipher = setup_aes_cipher_ (aes_key)
+    enc_data = cipher.encrypt (data)
+    if (0 != pad_len):
+        enc_data = enc_data[:-pad_len]
+    return enc_data
+
 def aes_decrypt (aes_key, data):
-    cipher = AES.new (aes_key.key, AES.MODE_CFB, aes_key.iv)
-    ptext = cipher.decrypt (aes_key.iv + data)
-    return ptext[aes_key.IV_SIZE:]
+    """Decrypts the given data using AES
+    
+    aes_key: the AESKey object to use for AES decryption
+    data: the data string to decrypt
+    """
+    (pad_len, data) = aes_pad_ (data)
+    cipher = setup_aes_cipher_ (aes_key)
+    ptext = cipher.decrypt (data)
+    if (0 != pad_len):
+        ptext = ptext[:-pad_len]
+    return ptext

Modified: gnunet-update/src/tests/Makefile.am
===================================================================
--- gnunet-update/src/tests/Makefile.am 2012-12-25 18:14:26 UTC (rev 25649)
+++ gnunet-update/src/tests/Makefile.am 2012-12-25 19:15:13 UTC (rev 25650)
@@ -11,6 +11,7 @@
   compatibility_test_hash_to_aes_key \
   compatibility_test_aes_enc
 
+rebuild_compatibility_checks: $(check_PROGRAMS)
 
 gen_user_home: gen_user_home.sh.in Makefile
        $(do_subst) < $(srcdir)/gen_user_home.sh.in > gen_user_home.sh

Modified: gnunet-update/src/tests/compatibility.c
===================================================================
--- gnunet-update/src/tests/compatibility.c     2012-12-25 18:14:26 UTC (rev 
25649)
+++ gnunet-update/src/tests/compatibility.c     2012-12-25 19:15:13 UTC (rev 
25650)
@@ -237,7 +237,11 @@
       return 1;
     }
     if (strlen(data) != fwrite (enc_data, 1, strlen(data), stdout))
+    {
+      GNUNET_free (enc_data);
       return 1;
+    }
+    GNUNET_free (enc_data);
     return 0;
   default:
     return 2;

Modified: gnunet-update/src/tests/test_util.py
===================================================================
--- gnunet-update/src/tests/test_util.py        2012-12-25 18:14:26 UTC (rev 
25649)
+++ gnunet-update/src/tests/test_util.py        2012-12-25 19:15:13 UTC (rev 
25650)
@@ -254,9 +254,11 @@
                                  stdout = subprocess.PIPE)
         (proc_out, proc_err) = proc.communicate ()
         proc.stdout.close()
+        self.assertEqual (proc.returncode, 0)
         hash_obj = sha512()
         hash_obj.update(self.data)
         aes_key = util.AESKey(hash_obj.digest())
+        self.assertEqual (len (aes_key.key), 32)
         self.assertEqual (aes_key.key + aes_key.iv, proc_out)
 
     def test_aes_enc (self):
@@ -265,12 +267,20 @@
                                  stdout = subprocess.PIPE)
         (proc_out, proc_err) = proc.communicate ()
         proc.stdout.close()
+        self.assertEqual (proc.returncode, 0)
         hash_obj = sha512()
         hash_obj.update(self.data)
         aes_key = util.AESKey(hash_obj.digest())
         enc_data = util.aes_encrypt (aes_key, self.data)
         self.assertEqual (len(enc_data), len(proc_out))
         self.assertEqual (enc_data, proc_out)
+
+    def test_aes_enc_dec (self):
+        """Tests AES encryption and decryption."""
+        aes_key = util.AESKey (self.sample_test_data)
+        enc_data = util.aes_encrypt (aes_key, self.data)
+        orig_data = util.aes_decrypt (aes_key, enc_data)
+        self.assertEqual (orig_data, self.data)
         
         
 if __name__ == '__main__':




reply via email to

[Prev in Thread] Current Thread [Next in Thread]