def testNonDupIV(self): """ Test to ensure that two instances of CBC don't get duplicate IV """ k = a2b_hex('2b7e151628aed2a6abf7158809cf4f3c') alg1 = AES_CBC(k) alg2 = AES_CBC(k) pt = a2b_hex('6bc1bee22e409f96e93d7e117393172a') ct1 = alg1.encrypt(pt) ct2 = alg2.encrypt(pt) assert( ct1!= ct2 ), 'AES_CBC dup IV error'
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 testAutoIVandPadding(self): k = a2b_hex('2b7e151628aed2a6abf7158809cf4f3c') alg = AES_CBC(key=k) # should default to padWithPadLen pt = a2b_hex('6bc1bee22e409f96e93d7e117393172a') ct = alg.encrypt(pt) dct = alg.decrypt(ct) self.assertEqual( dct, pt ) # 'AES_CBC auto IV and pad 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 encrypt(secret_token, data): data = data.encode('utf-8') iv = secret_token[:len(secret_token) // 2] key = secret_token[len(secret_token) // 2:] cipher = AES_CBC(key=key, keySize=16) encrypted_data = base64.b64encode(cipher.encrypt(data, iv)) return encrypted_data
def generate_mic(data): '''Generate the MIC over the given data''' alg = AES_CBC(key=OTAP_KEY, keySize=len(OTAP_KEY), padding=padWithZeros()) outdata = alg.encrypt(data, iv=OTAP_NONCE) # use the first 4 bytes of the CBC-MAC result as the MIC (per CCM spec) return struct.unpack('!L', outdata[-16:-12])[0]