2323"""
2424from __future__ import print_function
2525from __future__ import division
26+
2627try :
2728 from builtins import str
2829 from builtins import hex
2930 from builtins import range
3031 from builtins import object
3132except ImportError :
3233 pass
33- #from past.utils import old_div
34+ # from past.utils import old_div
3435
35- from Crypto .Cipher import AES
36- from Crypto .Util import Counter
37- from Crypto .Util .number import long_to_bytes , bytes_to_long
36+ from Cryptodome .Cipher import AES
37+ from Cryptodome .Util import Counter
38+ from Cryptodome .Util .number import bytes_to_long
39+ from Cryptodome .Util .number import long_to_bytes
3840
3941
4042# GF(2^128) defined by 1 + a + a^2 + a^7 + a^128
@@ -74,8 +76,8 @@ def __init__(self, master_key):
7476 self .change_key (master_key )
7577
7678 def change_key (self , master_key ):
77- #RLB: Need to allow 192-, 256-bit keys
78- #if master_key >= (1 << 128):
79+ # RLB: Need to allow 192-, 256-bit keys
80+ # if master_key >= (1 << 128):
7981 # raise InvalidInputException('Master key should be 128-bit')
8082
8183 self ._master_key = long_to_bytes (master_key , 16 )
@@ -146,7 +148,7 @@ def encrypt(self, init_value, plaintext, auth_data=b''):
146148
147149 if 0 != len_plaintext % 16 :
148150 padded_plaintext = plaintext + \
149- b'\x00 ' * (16 - len_plaintext % 16 )
151+ b'\x00 ' * (16 - len_plaintext % 16 )
150152 else :
151153 padded_plaintext = plaintext
152154 ciphertext = aes_ctr .encrypt (padded_plaintext )[:len_plaintext ]
@@ -157,7 +159,7 @@ def encrypt(self, init_value, plaintext, auth_data=b''):
157159 auth_tag = self .__ghash (auth_data , ciphertext )
158160 # print 'GHASH\t', hex(auth_tag)
159161 auth_tag ^= bytes_to_long (self ._aes_ecb .encrypt (
160- long_to_bytes ((init_value << 32 ) | 1 , 16 )))
162+ long_to_bytes ((init_value << 32 ) | 1 , 16 )))
161163
162164 # assert len(ciphertext) == len(plaintext)
163165 assert auth_tag < (1 << 128 )
@@ -171,8 +173,8 @@ def decrypt(self, init_value, ciphertext, auth_tag, auth_data=b''):
171173
172174 if auth_tag != self .__ghash (
173175 auth_data , ciphertext ) ^ bytes_to_long (
174- self ._aes_ecb .encrypt (
175- long_to_bytes ((init_value << 32 ) | 1 , 16 ))):
176+ self ._aes_ecb .encrypt (
177+ long_to_bytes ((init_value << 32 ) | 1 , 16 ))):
176178 raise InvalidTagException
177179
178180 len_ciphertext = len (ciphertext )
@@ -186,7 +188,7 @@ def decrypt(self, init_value, ciphertext, auth_tag, auth_data=b''):
186188
187189 if 0 != len_ciphertext % 16 :
188190 padded_ciphertext = ciphertext + \
189- b'\x00 ' * (16 - len_ciphertext % 16 )
191+ b'\x00 ' * (16 - len_ciphertext % 16 )
190192 else :
191193 padded_ciphertext = ciphertext
192194 plaintext = aes_ctr .decrypt (padded_ciphertext )[:len_ciphertext ]
0 commit comments