def CBCtestVector(key,iv,pt,kct): """ CBC test vectors using AES algorithm """ key,iv,pt,kct = a2b_hex(key),a2b_hex(iv),a2b_p(pt),a2b_p(kct) alg = AES_CBC(key, padding=noPadding()) self.assertEqual( alg.encrypt(pt,iv=iv), kct ) self.assertEqual( alg.decrypt(iv+kct), pt )
def __init__(self, blockCipherInstance, autoNonce=None, macSize=8, nonceSize=13): """ CCM algorithms are created by initializing with a BlockCipher instance blockCipherInstance -> typically AES_ECB autoNonce -> sets the intial value of a nonce for automatic nonce creation (not available yet) macSize -> size of MAC field can be = 4, 6, 8, 10, 12, 14, or 16 nonceSize -> size of nonce in bytes (default 13) the counter size is blockSize-nonceSize-1 """ self.baseCipher = blockCipherInstance self.name = self.baseCipher.name + '_CCM' self.blockSize = self.baseCipher.blockSize self.keySize = self.baseCipher.keySize self.baseCipher.padding = noPadding() # baseCipher should NOT pad!! self.M = macSize # Number of octets if not((3 < self.M < 17) and (macSize%2==0)) : raise InitCryptoError, 'CCM, M (size of auth field) is out of bounds' self.nonceSize = nonceSize self.L = self.baseCipher.blockSize - self.nonceSize - 1 if not(1 < self.L < 9) : raise InitCryptoError, 'CCM, L (size of length field) is out of bounds' self.reset()
def CBCtestVector(key, iv, pt, kct): """ CBC test vectors using AES algorithm """ key, iv, pt, kct = a2b_hex(key), a2b_hex(iv), a2b_p(pt), a2b_p(kct) alg = AES_CBC(key, padding=noPadding()) self.assertEqual(alg.encrypt(pt, iv=iv), kct) self.assertEqual(alg.decrypt(iv + kct), pt)
def __init__(self, blockCipherInstance, autoNonce=None, macSize=8, nonceSize=13): """ CCM algorithms are created by initializing with a BlockCipher instance blockCipherInstance -> typically AES_ECB autoNonce -> sets the intial value of a nonce for automatic nonce creation (not available yet) macSize -> size of MAC field can be = 4, 6, 8, 10, 12, 14, or 16 nonceSize -> size of nonce in bytes (default 13) the counter size is blockSize-nonceSize-1 """ self.baseCipher = blockCipherInstance self.name = self.baseCipher.name + '_CCM' self.blockSize = self.baseCipher.blockSize self.keySize = self.baseCipher.keySize self.baseCipher.padding = noPadding() # baseCipher should NOT pad!! self.M = macSize # Number of octets if not ((3 < self.M < 17) and (macSize % 2 == 0)): raise InitCryptoError, 'CCM, M (size of auth field) is out of bounds' self.nonceSize = nonceSize self.L = self.baseCipher.blockSize - self.nonceSize - 1 if not (1 < self.L < 9): raise InitCryptoError, 'CCM, L (size of length field) is out of bounds' self.reset()
def testAutoIV(self): k = a2b_hex('2b7e151628aed2a6abf7158809cf4f3c') alg = AES_CBC(key=k, padding=noPadding()) pt = a2b_hex('6bc1bee22e409f96e93d7e117393172a') ct = alg.encrypt(pt) dct = alg.decrypt(ct) self.assertEqual( dct, pt ) # 'AES_CBC auto IV error'
def testAutoIV(self): k = a2b_hex('2b7e151628aed2a6abf7158809cf4f3c') alg = AES_CBC(key=k, padding=noPadding()) pt = a2b_hex('6bc1bee22e409f96e93d7e117393172a') ct = alg.encrypt(pt) dct = alg.decrypt(ct) self.assertEqual(dct, pt) # 'AES_CBC auto IV error'
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, blockCipherInstance, padding = padWithPadLen()): """ CBC algorithms are created by initializing with a BlockCipher instance """ self.baseCipher = blockCipherInstance self.name = self.baseCipher.name + '_CBC' self.blockSize = self.baseCipher.blockSize self.keySize = self.baseCipher.keySize self.padding = padding self.baseCipher.padding = noPadding() # baseCipher should NOT pad!! self.r = Random() # for IV generation, currently uses # mediocre standard distro version <---------------- import time newSeed = time.ctime()+str(self.r) # seed with instance location self.r.seed(newSeed) # to make unique self.reset()
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 IcedollTestVec(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 = Icedoll(bkey, keySize=kSize, blockSize=bSize, padding=noPadding()) cct = alg.encrypt(plainText) print 'pt =',b2a_p(plainText) print 'ct =',b2a_p(cct) dcct = alg.decrypt(cct) #print '_dcct',b2a_p(dcct) self.assertEqual( dcct, plainText ) self.assertEqual( alg.encrypt(plainText), cipherText ) self.assertEqual( alg.decrypt(cipherText), plainText )
def testDctEqPt(self): """ test of plaintext = decrypt(encrypt(plaintext)) """ alg = Icedoll( 16*chr(0), padding=noPadding()) pt = 16*4*'a' # block aligned ct = alg.encrypt(pt) print 'ct = ',b2a_p(ct) dct = alg.decrypt(ct) print 'dct = ',b2a_p(dct) assert(pt == dct), 'pt != dct' alg = Icedoll( 16*chr(0)) # autoPad pt = 17*4*'a' # non-block aligned ct = alg.encrypt(pt) print 'ct = ',b2a_p(ct) dct = alg.decrypt(ct) print 'dct = ',b2a_p(dct) assert(pt == dct), 'pt != dct'
def testDctEqPt(self): """ test of plaintext = decrypt(encrypt(plaintext)) """ alg = Icedoll(16 * chr(0), padding=noPadding()) pt = 16 * 4 * 'a' # block aligned ct = alg.encrypt(pt) print 'ct = ', b2a_p(ct) dct = alg.decrypt(ct) print 'dct = ', b2a_p(dct) assert (pt == dct), 'pt != dct' alg = Icedoll(16 * chr(0)) # autoPad pt = 17 * 4 * 'a' # non-block aligned ct = alg.encrypt(pt) print 'ct = ', b2a_p(ct) dct = alg.decrypt(ct) print 'dct = ', b2a_p(dct) assert (pt == dct), 'pt != dct'
def IcedollTestVec(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 = Icedoll(bkey, keySize=kSize, blockSize=bSize, padding=noPadding()) cct = alg.encrypt(plainText) print 'pt =', b2a_p(plainText) print 'ct =', b2a_p(cct) dcct = alg.decrypt(cct) #print '_dcct',b2a_p(dcct) self.assertEqual(dcct, plainText) self.assertEqual(alg.encrypt(plainText), cipherText) self.assertEqual(alg.decrypt(cipherText), plainText)
def __init__(self, key, block_size=16): self.r = cryptopy.cipher.rijndael.Rijndael(key, padding=noPadding(), keySize=16, blockSize=block_size)
def __init__(self, key=None, padding=padWithPadLen(), keySize=16): CBC.__init__( self, AES(key, noPadding(), keySize), padding) self.name = 'AES_CBC'
def __init__(self, seed=defaultSeed): self.__algorithm = Rijndael(padding=noPadding(), keySize=32, blockSize=32) self.reset() self.reseed(seed)
def __init__(self, key=None, padding=padWithPadLen(), keySize=16): CBC.__init__(self, AES(key, noPadding(), keySize), padding) self.name = 'AES_CBC'