- Timestamp:
- 03/11/15 14:06:31 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/bletchley/CBC/__init__.py
r69 r76 3 3 4 4 Copyright (C) 2010 ELOI SANFÈLIX 5 Copyright (C) 2012-201 3Timothy D. Morgan5 Copyright (C) 2012-2015 Timothy D. Morgan 6 6 @author: Eloi Sanfelix < eloi AT limited-entropy.com > 7 7 @author: Timothy D. Morgan < tmorgan {a} vsecurity . com > … … 118 118 def log_message(self, s): 119 119 if self.log_fh != None: 120 self.log_fh.write( s+'\n')120 self.log_fh.write('BLETCHLEY: %s\n' % s) 121 121 122 122 … … 343 343 ptext = self.decrypt_block(b'\x00'*self.block_size, ciphertext, cache=False) 344 344 prior = buffertools.xorBuffers(ptext, plaintext) 345 self.log_message("Encrypted block: %s to %s with prior %s" % (repr(plaintext), repr(ciphertext), repr(prior))) 345 self.log_message("Encrypted block: %s to %s with prior %s" % (repr(plaintext), 346 repr(bytes(ciphertext)), 347 repr(bytes(prior)))) 346 348 return prior,ciphertext 347 349 348 350 349 def encrypt(self, plaintext, ciphertext=None):351 def encrypt(self, plaintext, ciphertext=None): 350 352 """Encrypts a plaintext value through "CBC-R" style prior-block 351 353 propagation. … … 364 366 blocks = buffertools.splitBuffer(buffertools.pkcs7PadBuffer(plaintext, self.block_size), 365 367 self.block_size) 366 if ciphertext != None:368 if ciphertext not in (None, b''): 367 369 if len(ciphertext) % self.block_size != 0: 368 370 raise InvalidBlockError(self.block_size,len(ciphertext)) 369 num_cblocks = (len(ciphertext) // self.block_size) - 1 370 del blocks[0-num_cblocks:] # we've already encrypted these 371 prior = ciphertext[0:self.block_size] 371 372 cblocks = buffertools.splitBuffer(ciphertext, self.block_size) 373 prior = cblocks[0] 374 375 # remove first block from ciphertext since it'll be re-added later 376 # after the prior is converted to finished ciphertext. 377 del cblocks[0] 378 ciphertext = b''.join(cblocks) 379 380 # now remove the plaintext blocks we've already completed 381 num_finished = len(cblocks) 382 del blocks[len(blocks)-num_finished:] 383 self.log_message("Reusing previous decryption of final %d blocks" % num_finished) 372 384 373 385 elif (len(self.decrypted) >= self.block_size 374 386 and len(self._ciphertext) >= 2*self.block_size): 387 388 self.log_message("Reusing previous decryption of final block") 389 375 390 # If possible, reuse work from prior decryption efforts on original 376 391 # message for last block … … 382 397 del blocks[-1] 383 398 else: 399 self.log_message("Starting decryption from scratch with random final block") 400 384 401 # Otherwise, select a random last block and generate the prior block 385 402 prior = struct.pack("B"*self.block_size, … … 387 404 ciphertext = b'' 388 405 406 self.log_message("Encrypting %d blocks..." % len(blocks)) 389 407 try: 390 408 # Continue generating all prior blocks
Note: See TracChangeset
for help on using the changeset viewer.