From f6c4f4b4a85b8e5af2a554f445be660bdb42e202 Mon Sep 17 00:00:00 2001 From: madchucky <33758259+madchucky@users.noreply.github.com> Date: Wed, 6 Dec 2017 17:01:58 +0100 Subject: [PATCH 1/7] Comment the unpacking operator I had to struggle to understand the unpacking operator '*' until I found following video https://www.youtube.com/watch?v=YWY4BZi_o28. Cool python feature. --- mega/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mega/utils.py b/mega/utils.py index 108f350..87bcd98 100644 --- a/mega/utils.py +++ b/mega/utils.py @@ -8,6 +8,7 @@ def a32_to_str(a): return struct.pack('>%dI' % len(a), *a) +# https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists just to explain the use of '*' in front of 'a' def aes_cbc_encrypt(data, key): From ffe4d4ad405da864b1cfd0e0d224b18a3c8c69be Mon Sep 17 00:00:00 2001 From: madchucky <33758259+madchucky@users.noreply.github.com> Date: Wed, 6 Dec 2017 17:20:10 +0100 Subject: [PATCH 2/7] Update utils.py --- mega/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mega/utils.py b/mega/utils.py index 87bcd98..c69f7ab 100644 --- a/mega/utils.py +++ b/mega/utils.py @@ -8,6 +8,7 @@ def a32_to_str(a): return struct.pack('>%dI' % len(a), *a) +# > , my guess is that choosing the big-endian and not letting it be native is aiming at making the code independent of the platform, in other words independent of the endianness of the platform: to be checked with @juanriaza https://docs.python.org/3.6/library/struct.html?highlight=struct#byte-order-size-and-alignment # https://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists just to explain the use of '*' in front of 'a' From 1585977736820bef0e6304f88f4c6424ca659616 Mon Sep 17 00:00:00 2001 From: madchucky <33758259+madchucky@users.noreply.github.com> Date: Wed, 6 Dec 2017 18:04:44 +0100 Subject: [PATCH 3/7] Initialisation of AES and length of data passed to encrypt --- mega/utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mega/utils.py b/mega/utils.py index c69f7ab..2659152 100644 --- a/mega/utils.py +++ b/mega/utils.py @@ -14,7 +14,9 @@ def a32_to_str(a): def aes_cbc_encrypt(data, key): encryptor = AES.new(key, AES.MODE_CBC, '\0' * 16) + # the last argument, the initiatlisation vector is optional http://pythonhosted.org/pycrypto/Crypto.Cipher.AES-module.html#new return encryptor.encrypt(data) + # length data must be a multiple of 16 http://pythonhosted.org/pycrypto/Crypto.Cipher.blockalgo.BlockAlgo-class.html#encrypt def aes_cbc_encrypt_a32(data, key): From 962884a49091d35e33000b594de92b96054dcb50 Mon Sep 17 00:00:00 2001 From: madchucky <33758259+madchucky@users.noreply.github.com> Date: Wed, 6 Dec 2017 18:44:29 +0100 Subject: [PATCH 4/7] Comment choice of CBC mode CBC mode is chosen because this is what mega is using. --- mega/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mega/utils.py b/mega/utils.py index 2659152..b205e78 100644 --- a/mega/utils.py +++ b/mega/utils.py @@ -14,6 +14,7 @@ def a32_to_str(a): def aes_cbc_encrypt(data, key): encryptor = AES.new(key, AES.MODE_CBC, '\0' * 16) + # choice of CBC mode is justified on https://mega.nz/doc 12.10 "A chunk MAC is computed as follows (this is essentially CBC-MAC, which was chosen instead of the more efficient OCB over intellectual property concerns)" # the last argument, the initiatlisation vector is optional http://pythonhosted.org/pycrypto/Crypto.Cipher.AES-module.html#new return encryptor.encrypt(data) # length data must be a multiple of 16 http://pythonhosted.org/pycrypto/Crypto.Cipher.blockalgo.BlockAlgo-class.html#encrypt From 046d798ac58fc7108dfff445f2a2ea95018732b8 Mon Sep 17 00:00:00 2001 From: madchucky <33758259+madchucky@users.noreply.github.com> Date: Thu, 7 Dec 2017 11:37:54 +0100 Subject: [PATCH 5/7] Comment padding need Adding a coming explaining why 'b' needs to be padded up to a length multiple of 4. If the need for padding repeats, we could define a function for it maybe. --- mega/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mega/utils.py b/mega/utils.py index b205e78..538938c 100644 --- a/mega/utils.py +++ b/mega/utils.py @@ -25,7 +25,7 @@ def aes_cbc_encrypt_a32(data, key): def str_to_a32(b): - if len(b) % 4: # Add padding, we need a string with a length multiple of 4 + if len(b) % 4: # Add padding, we need a string with a length multiple of 4 because the size of an unsigned integer as indicated in the call of the unpack method below is 4 bytes https://docs.python.org/3.6/library/struct.html#format-characters b += '\0' * (4 - len(b) % 4) return struct.unpack('>%dI' % (len(b) / 4), b) From 8785de9761cf20d80680d36fefd9bc913206c039 Mon Sep 17 00:00:00 2001 From: madchucky <33758259+madchucky@users.noreply.github.com> Date: Thu, 7 Dec 2017 11:55:06 +0100 Subject: [PATCH 6/7] Commenting mpi2int I still do not know the need of the function though... --- mega/utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mega/utils.py b/mega/utils.py index 538938c..cfe68f5 100644 --- a/mega/utils.py +++ b/mega/utils.py @@ -32,6 +32,7 @@ def str_to_a32(b): def mpi2int(s): return int(binascii.hexlify(s[2:]), 16) +# returns the string s, truncated of its 2 first caracters as an integer def aes_cbc_decrypt(data, key): From a36326267299284445a2f127151f7e505611b99f Mon Sep 17 00:00:00 2001 From: madchucky <33758259+madchucky@users.noreply.github.com> Date: Thu, 7 Dec 2017 11:56:51 +0100 Subject: [PATCH 7/7] Adding link to documentation of built-in int function https://docs.python.org/3.6/library/functions.html?highlight=int#int --- mega/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mega/utils.py b/mega/utils.py index cfe68f5..8004a66 100644 --- a/mega/utils.py +++ b/mega/utils.py @@ -32,7 +32,7 @@ def str_to_a32(b): def mpi2int(s): return int(binascii.hexlify(s[2:]), 16) -# returns the string s, truncated of its 2 first caracters as an integer +# returns the string s, truncated of its 2 first caracters as an integer https://docs.python.org/3.6/library/functions.html?highlight=int#int def aes_cbc_decrypt(data, key):