def test_CTR_test_vectors(self): # test CTR encryption and decryption using official RFC test vectors key = Fernet.generate_key() fernet = Fernet(key) KEY = 'AE6852F8121067CC4BF7A5765577F39E'.decode('hex') IV = '00000030000000000000000000000001'.decode('hex') PLAINTEXT = '53696E676C6520626C6F636B206D7367'.decode('hex') CIPHERTEXT = 'E4095D4FB7A7B3792D6175A3261311B8'.decode('hex') assert fernet._AES_CTR_encrypt(PLAINTEXT, IV, KEY) == CIPHERTEXT assert fernet._AES_CTR_decrypt(CIPHERTEXT, IV, KEY) == PLAINTEXT KEY = '7E24067817FAE0D743D6CE1F32539163'.decode('hex') IV = '006CB6DBC0543B59DA48D90B00000001'.decode('hex') PLAINTEXT = '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F'.decode('hex') CIPHERTEXT = '5104A106168A72D9790D41EE8EDAD388EB2E1EFC46DA57C8FCE630DF9141BE28'.decode('hex') assert fernet._AES_CTR_encrypt(PLAINTEXT, IV, KEY) == CIPHERTEXT assert fernet._AES_CTR_decrypt(CIPHERTEXT, IV, KEY) == PLAINTEXT KEY = '7691BE035E5020A8AC6E618529F9A0DC'.decode('hex') IV = '00E0017B27777F3F4A1786F000000001'.decode('hex') PLAINTEXT = '000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F20212223'.decode('hex') CIPHERTEXT = 'C1CF48A89F2FFDD9CF4652E9EFDB72D74540A42BDE6D7836D59A5CEAAEF3105325B2072F'.decode('hex') assert fernet._AES_CTR_encrypt(PLAINTEXT, IV, KEY) == CIPHERTEXT assert fernet._AES_CTR_decrypt(CIPHERTEXT, IV, KEY) == PLAINTEXT
def test_CTR_functionality(self): # test CTR functionality (decrypt(encrypt(msg)) == msg) key = Fernet.generate_key() fernet = Fernet(key) # test encrypt than decrypt gives back original message for i in range(20): data = os.urandom(i) key = os.urandom(16) iv = os.urandom(16) assert fernet._AES_CTR_decrypt(fernet._AES_CTR_encrypt(data, iv, key), iv, key) == data
def test_CTR_decrypt(self): # test CTR decrypt function (encrypt with library API, decrypt with ours) key = Fernet.generate_key() fernet = Fernet(key) for i in range(20): data = os.urandom(i) key = os.urandom(16) iv = os.urandom(16) # encrypt with library APIs cipher = Cipher(algorithms.AES(key), modes.CTR(iv), backend=default_backend()) encryptor = cipher.encryptor() ct = encryptor.update(data) + encryptor.finalize() # decrypt with our APIs pt = fernet._AES_CTR_decrypt(ct, iv, key) assert data == pt