@@ -44,7 +44,7 @@ class FileMap(object):
4444
4545 def __init__ (self , trezor , logger ):
4646 assert trezor is not None
47- self .blob = {}
47+ self .blob = None
4848 self .trezor = trezor
4949 self .outerKey = None # outer AES-CBC key, 1st-level encryption
5050 self .outerIv = None # IV for data blob encrypted with outerKey
@@ -76,32 +76,36 @@ def createDecFile(self, fname):
7676 #os.chmod(fname, stat.S_IRUSR | stat.S_IWUSR )
7777 self .logger .error ("File %s cannot be written. "
7878 "No write permissions. Skipping it." , fname )
79- return
79+ raise IOError ("File IO error: File %s cannot be written. "
80+ "No write permissions. Skipping it. "
81+ "Change file permissions and try again." % (fname ))
8082
8183 self .loadBlobFromEncFile (originalFilename )
8284 with open (fname , 'w+b' ) as f :
8385 s = len (self .blob )
8486 f .write (self .blob )
8587 if f .tell () != s :
86- raise IOError ("File IO problem - not enough data written" )
88+ self .logger .error ("File IO problem: not enough data written "
89+ "(file=%s, target=%d, done=%d)" % (fname , s , f .tell ()))
90+ raise IOError ("File IO problem - not enough data written "
91+ "(file=%s, target=%d, done=%d)" % (fname , s , f .tell ()))
8792 self .logger .debug ("Decryption wrote %d bytes to file %s." ,s ,fname )
88- f .flush ()
89- f .close ()
9093 # overwrite with nonsense to shred memory
9194 rng = Random .new ()
9295 self .outerKey = rng .read (KEYSIZE )
9396 return fname # output file name
9497
9598 def loadBlobFromEncFile (self , fname ):
9699 """
97- Load/read data from encrypted file, decrypt data amd store data in blob
100+ Load/read data from encrypted file,
101+ decrypt data amd store data in blob
98102 Requires Trezor connected.
99103
100104 @param fname: name of the encrypted file to decrypt
101105
102106 @throws IOError: if reading file failed
103107 """
104- with file (fname ) as f :
108+ with open (fname , 'rb' ) as f :
105109 header = f .read (len (Magic .headerStr ))
106110 if header != Magic .headerStr :
107111 raise IOError ("Bad header in storage file" )
@@ -161,7 +165,8 @@ def loadBlobFromEncFile(self, fname):
161165 for (ch1 , ch2 ) in zip (hmacDigest , newHmacDigest ):
162166 hmacCompare |= int (ch1 != ch2 )
163167 if hmacCompare != 0 :
164- raise IOError ("Corrupted disk format - HMAC does not match or bad passphrase" )
168+ raise IOError ("Corrupted disk format - HMAC does not match "
169+ "or bad passphrase. Try again with the correct passphrase." )
165170
166171 if self .noOfEncryptions == 2 :
167172 encrypted = self .decryptOnTrezorDevice (encrypted , Magic .levelTwoKey )
@@ -177,11 +182,12 @@ def createEncFile(self, fname, obfuscate, twice):
177182
178183 with open (fname , 'rb' ) as f :
179184 # Size 0 will read the ENTIRE file into memory!
180- m = mmap .mmap (f .fileno (), 0 , prot = mmap .PROT_READ ) #File is open read-only
185+ # File is open read-only
186+ # mmap does not implement __exit__ so we cannot use "with mmap... as m:"
187+ m = mmap .mmap (f .fileno (), 0 , prot = mmap .PROT_READ )
181188 s = m .size ()
182189 self .blob = m .read (s )
183- m .close ()
184- f .close ()
190+ del m
185191 if len (self .blob ) != s :
186192 raise IOError ("File IO problem - not enough data read" )
187193 self .logger .debug ("Read %d bytes from file %s." ,s ,fname )
@@ -228,7 +234,7 @@ def saveBlobToEncFile(self, fname, obfuscate, twice):
228234 #raise IOError("File " + fname + " cannot be written. "
229235 # "No write permissions. Skipping it.")
230236
231- with file (fname , "wb" ) as f :
237+ with open (fname , "wb" ) as f :
232238 version = basics .TSFEFILEFORMATVERSION
233239 futureUse = ""
234240 f .write (Magic .headerStr )
@@ -256,8 +262,6 @@ def saveBlobToEncFile(self, fname, obfuscate, twice):
256262 f .write (encrypted )
257263 f .write (hmacDigest )
258264 ww = f .tell ()
259- f .flush ()
260- f .close ()
261265 self .logger .debug ("Wrote %d bytes to file %s." , ww , fname )
262266 return fname
263267
@@ -267,8 +271,8 @@ def obfuscateFilename(self, plaintextFileName):
267271 --> homegrown padding == obfuscated filename
268272 """
269273 pad16 = Padding (BLOCKSIZE ).pad (plaintextFileName )
270- self .logger .debug ("Press confirm on Trezor device to encrypt file "
271- "name %s (if necessary)." , plaintextFileName )
274+ # self.logger.debug("Press confirm on Trezor device to encrypt file "
275+ # "name %s (if necessary).", plaintextFileName)
272276 # we do not use an IV here so that we can quickly deobfuscate
273277 # filenames without having to read the file
274278 encFn = self .trezor .encrypt_keyvalue (Magic .fileNameNode ,
@@ -321,6 +325,7 @@ def encrypt(self, plaintext, iv, key):
321325 """
322326 Pad plaintext with PKCS#5 and encrypt it.
323327 """
328+ self .logger .debug ("AES CBC encryption with key of size %d bits." % (len (key )* 8 ))
324329 cipher = AES .new (key , AES .MODE_CBC , iv )
325330 padded = Padding (BLOCKSIZE ).pad (plaintext )
326331 return cipher .encrypt (padded )
@@ -335,6 +340,7 @@ def decrypt(self, ciphertext, iv, key):
335340 """
336341 Decrypt ciphertext, unpad it and return
337342 """
343+ self .logger .debug ("AES CBC decryption with key of size %d bits." % (len (key )* 8 ))
338344 cipher = AES .new (key , AES .MODE_CBC , iv )
339345 plaintext = cipher .decrypt (ciphertext )
340346 unpadded = Padding (BLOCKSIZE ).unpad (plaintext )
0 commit comments