def __init__(self, key = None, padding = padWithPadLen(), keySize=16): """ Initialize AES, keySize is in bytes """ if not (keySize == 16 or keySize == 24 or keySize == 32) : raise BadKeySizeError, 'Illegal AES key size, must be 16, 24, or 32 bytes' Rijndael.__init__( self, key, padding=padding, keySize=keySize, blockSize=16 ) self.name = 'AES'
def testIVuniqueness(self): """ Test that two different instances have different IVs """ key = a2b_p('2b7e151628aed2a6abf7158809cf4f3c') pt = "This is a test case" alg1 = CBC(Rijndael(key, blockSize=32)) alg2 = CBC(Rijndael(key, blockSize=32)) ct1 = alg1.encrypt(pt) ct2 = alg2.encrypt(pt) self.assertNotEqual(ct1, ct2)
def RijndaelTestVec(i, key, pt, ct): """ Run single AES test vector with any legal blockSize and any legal key size. """ bkey, plainText, cipherText = a2b_hex(key), a2b_hex(pt), a2b_hex(ct) kSize = len(bkey) bSize = len(cipherText) # set block size to length of block alg = Rijndael(bkey, keySize=kSize, blockSize=bSize, padding=noPadding()) self.assertEqual( alg.encrypt(plainText), cipherText ) self.assertEqual( alg.decrypt(cipherText), plainText )
def __init__(self, key=None, padding=padWithPadLen(), keySize=16): """ Initialize AES, keySize is in bytes """ if not (keySize == 16 or keySize == 24 or keySize == 32): raise BadKeySizeError, 'Illegal AES key size, must be 16, 24, or 32 bytes' Rijndael.__init__(self, key, padding=padding, keySize=keySize, blockSize=16) self.name = 'AES'
def RijndaelTestVec(i, key, pt, ct): """ Run single AES test vector with any legal blockSize and any legal key size. """ bkey, plainText, cipherText = a2b_hex(key), a2b_hex(pt), a2b_hex( ct) kSize = len(bkey) bSize = len(cipherText) # set block size to length of block alg = Rijndael(bkey, keySize=kSize, blockSize=bSize, padding=noPadding()) self.assertEqual(alg.encrypt(plainText), cipherText) self.assertEqual(alg.decrypt(cipherText), plainText)
def testIVmultencryptUnique(self): """ Test that two different encrypts have different IVs """ key = a2b_p('2b7e151628aed2a6abf7158809cf4f3c') pt = "This is yet another test case" alg1 = CBC(Rijndael(key, blockSize=32)) ct1 = alg1.encrypt(pt) ct2 = alg1.encrypt(pt) self.assertNotEqual(ct1, ct2) self.assertEqual(alg1.decrypt(ct1), pt) self.assertEqual(alg1.decrypt(ct1), alg1.decrypt(ct2))
def testCBC_Rijndael_variable_data(self): """ Rijndael CBC 256 """ key = '2b7e151628aed2a6abf7158809cf4f3c' iv = '000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f' key, iv = a2b_p(key), a2b_p(iv) alg = CBC(Rijndael(key, blockSize=32)) for i in range(100): pt = i * 'a' ct = alg.encrypt(pt, iv=iv) self.assertEqual(alg.decrypt(iv + ct), pt)
def testCBC_Rijndael_256(self): """ Rijndael CBC 256 """ key = '2b7e151628aed2a6abf7158809cf4f3c' iv = '000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f' pt = """6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51 30c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710""" key, iv, pt = a2b_p(key), a2b_p(iv), a2b_p(pt) alg = CBC(Rijndael(key, blockSize=32)) ct = alg.encrypt(pt, iv=iv) self.assertEqual(alg.decrypt(iv + ct), pt)
def testMultipassEncrypt(self): """ Test moreData usage """ alg = CBC(Rijndael(16 * chr(0), blockSize=32)) ct1 = '' for i in range(129): ct1 += alg.encrypt('a', more=1) ct1 += alg.encrypt('') # flush any remaining ct2 = alg.encrypt(129 * 'a') self.assertNotEqual(ct1, ct2) pt1 = alg.decrypt(ct1) pt2 = alg.decrypt(ct2) self.assertEqual(pt1, pt2) pt3 = alg.decrypt('', more=1) for i in range(len(ct2)): pt3 += alg.decrypt(ct2[i], more=1) pt3 += alg.decrypt('')
class PRN_Rijndael: """ A Psudeo Random Number Generator based on Rijndael_256k_256b The algorithm is based on Section 13.4 of: "AES Proposal: Rijndael", Joan Daemen, Vincent Rijmen """ def __init__(self, seed=defaultSeed): self.__algorithm = Rijndael(padding=noPadding(), keySize=32, blockSize=32) self.reset() self.reseed(seed) def reset(self): self.__algorithm.setKey(self.__algorithm.keySize * chr(0)) # set key to all zeros self.__state = self.__algorithm.blockSize * chr( 0) # a single block of zeros def reseed(self, seed): while len(seed) > 0: if len(seed) < self.__algorithm.blockSize: block = seed + (self.__algorithm.blockSize - len(seed)) * chr(0) seed = '' else: block = seed[:self.__algorithm.blockSize] seed = seed[self.__algorithm.blockSize:] self.__algorithm.setKey(self.__algorithm.encrypt(block)) def getBytes(self, numBytes): """ Return a psuedo random byte string of length numBytes """ bytes = '' while len(bytes) < numBytes: bytes = bytes + self.getSomeBytes() return bytes[:numBytes] # truncate to the requested length def getSomeBytes(self): """ Psuedo random bytes are generated 16 bytes at a time. The state is updated by applying Rijndael using the Cipher Key. The first 128 bits of the state are output as a pseudorandom number. """ self.__state = self.__algorithm.encrypt(self.__state) return self.__state[:16]
def __init__(self, seed=defaultSeed): self.__algorithm = Rijndael(padding=noPadding(), keySize=32, blockSize=32) self.reset() self.reseed(seed)