Changeset 58 for trunk/lib


Ignore:
Timestamp:
08/13/13 00:28:04 (11 years ago)
Author:
tmorgan
Message:

updated docs

improvements to CBC-R and edge cases

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/bletchley/CBC/__init__.py

    r52 r58  
    197197
    198198
    199     def decrypt_next_byte(self, prior, block, known_bytes):
     199    def decrypt_next_byte(self, prior, block, known_bytes, cache=True):
    200200        """Decrypts one byte of ciphertext by modifying the prior
    201201        ciphertext block at the same relative offset.
     
    257257       
    258258        decrypted = struct.pack("B",self._thread_result^base^(numKnownBytes+1))
    259         self.decrypted = decrypted + self.decrypted
     259        if cache:
     260            self.decrypted = decrypted + self.decrypted
    260261        #  Return previous bytes together with current byte
    261262        return decrypted+known_bytes
    262263   
    263264
    264     def decrypt_block(self, prior, block, last_bytes=b''):
     265    def decrypt_block(self, prior, block, last_bytes=b'', cache=True):
    265266        """Decrypts the block of ciphertext provided as a parameter.
    266267
     
    268269
    269270        while(len(last_bytes)!=self.block_size):
    270             last_bytes = self.decrypt_next_byte(prior, block, last_bytes)
     271            last_bytes = self.decrypt_next_byte(prior, block, last_bytes, cache)
    271272
    272273        self.log_message("Decrypted block: %s" % repr(last_bytes))
     
    336337            raise InvalidBlockError(self.block_size,len(plaintext))
    337338
    338         ptext = self.decrypt_block(b'\x00'*self.block_size, ciphertext)
     339        ptext = self.decrypt_block(b'\x00'*self.block_size, ciphertext, cache=False)
    339340        prior = buffertools.xorBuffers(ptext, plaintext)
     341        self.log_message("Encrypted block: %s to %s with prior %s" % (repr(plaintext), repr(ciphertext), repr(prior)))
    340342        return prior,ciphertext
    341343   
    342344   
    343     def encrypt(self,plaintext):
     345    def encrypt(self,plaintext, ciphertext=None):
    344346        """Encrypts a plaintext value through "CBC-R" style prior-block
    345347        propagation.
     
    355357
    356358        """
    357 
     359       
    358360        blocks = buffertools.splitBuffer(buffertools.pkcs7PadBuffer(plaintext, self.block_size),
    359361                                         self.block_size)
    360 
    361         if (len(self.decrypted) >= self.block_size
     362        if ciphertext != None:
     363            if len(ciphertext) % self.block_size != 0:
     364                raise InvalidBlockError(self.block_size,len(ciphertext))
     365            num_cblocks = (len(ciphertext) // self.block_size) - 1
     366            del blocks[0-num_cblocks:] # we've already encrypted these
     367            prior = ciphertext[0:self.block_size]
     368           
     369        elif (len(self.decrypted) >= self.block_size
    362370            and len(self._ciphertext) >= 2*self.block_size):
    363371            # If possible, reuse work from prior decryption efforts on original
     
    368376                                           buffertools.xorBuffers(final_plaintext, blocks[-1]))
    369377            ciphertext = self._ciphertext[0-self.block_size:]
     378            del blocks[-1]
    370379        else:
    371380            # Otherwise, select a random last block and generate the prior block
    372             ciphertext = struct.pack("B"*self.block_size,
     381            prior = struct.pack("B"*self.block_size,
    373382                                     *[random.getrandbits(8) for i in range(self.block_size)])
    374             prior,ciphertext = self.encrypt_block(blocks[-1], ciphertext)
    375 
    376         # Continue generating all prior blocks
    377         for i in range(len(blocks)-2, -1, -1):
    378             prior,cblock = self.encrypt_block(blocks[i],prior)
    379             ciphertext = cblock+ciphertext
    380        
     383            ciphertext = b''
     384
     385        try:
     386            # Continue generating all prior blocks
     387            for i in range(len(blocks)-1, -1, -1):
     388                prior,cblock = self.encrypt_block(blocks[i],prior)
     389                ciphertext = cblock+ciphertext
     390        except Exception as e:
     391            self.log_message("Encryption failure. prior+ciphertext: %s" % repr(prior+ciphertext))
     392
    381393        # prior as IV
    382394        return prior,ciphertext
Note: See TracChangeset for help on using the changeset viewer.