Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 1 | """ |
| 2 | Test using the NIST Test Vectors |
| 3 | """ |
| 4 | import binascii |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 5 | import os |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from cryptography.primitives.block import BlockCipher, ciphers, modes, padding |
| 10 | |
| 11 | from ..utils import load_nist_vectors_from_file |
| 12 | |
| 13 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 14 | def parameterize_encrypt(fname): |
| 15 | return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 16 | load_nist_vectors_from_file( |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 17 | os.path.join("AES/KAT/", fname), |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 18 | "ENCRYPT", |
| 19 | ["key", "iv", "plaintext", "ciphertext"], |
| 20 | ), |
| 21 | ) |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 22 | |
| 23 | |
| 24 | class TestAES_CBC(object): |
| 25 | @parameterize_encrypt("CBCGFSbox128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 26 | def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 27 | cipher = BlockCipher( |
| 28 | ciphers.AES(binascii.unhexlify(key)), |
| 29 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 30 | ) |
| 31 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 32 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 33 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 34 | @parameterize_encrypt("CBCGFSbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 35 | def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 36 | cipher = BlockCipher( |
| 37 | ciphers.AES(binascii.unhexlify(key)), |
| 38 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 39 | ) |
| 40 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 41 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 42 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 43 | @parameterize_encrypt("CBCGFSbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 44 | def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 45 | cipher = BlockCipher( |
| 46 | ciphers.AES(binascii.unhexlify(key)), |
| 47 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 48 | ) |
| 49 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 50 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 51 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 52 | @parameterize_encrypt("CBCKeySbox128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 53 | def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 54 | cipher = BlockCipher( |
| 55 | ciphers.AES(binascii.unhexlify(key)), |
| 56 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 57 | ) |
| 58 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 59 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 60 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 61 | @parameterize_encrypt("CBCKeySbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 62 | def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 63 | cipher = BlockCipher( |
| 64 | ciphers.AES(binascii.unhexlify(key)), |
| 65 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 66 | ) |
| 67 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 68 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 69 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 70 | @parameterize_encrypt("CBCKeySbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 71 | def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 72 | cipher = BlockCipher( |
| 73 | ciphers.AES(binascii.unhexlify(key)), |
| 74 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 75 | ) |
| 76 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 77 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 78 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 79 | @parameterize_encrypt("CBCVarKey128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 80 | def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 81 | cipher = BlockCipher( |
| 82 | ciphers.AES(binascii.unhexlify(key)), |
| 83 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 84 | ) |
| 85 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 86 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 87 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 88 | @parameterize_encrypt("CBCVarKey192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 89 | def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 90 | cipher = BlockCipher( |
| 91 | ciphers.AES(binascii.unhexlify(key)), |
| 92 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 93 | ) |
| 94 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 95 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 96 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 97 | @parameterize_encrypt("CBCVarKey256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 98 | def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 99 | cipher = BlockCipher( |
| 100 | ciphers.AES(binascii.unhexlify(key)), |
| 101 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 102 | ) |
| 103 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 104 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 105 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 106 | @parameterize_encrypt("CBCVarTxt128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 107 | def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 108 | cipher = BlockCipher( |
| 109 | ciphers.AES(binascii.unhexlify(key)), |
| 110 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 111 | ) |
| 112 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 113 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 114 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 115 | @parameterize_encrypt("CBCVarTxt192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 116 | def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 117 | cipher = BlockCipher( |
| 118 | ciphers.AES(binascii.unhexlify(key)), |
| 119 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 120 | ) |
| 121 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 122 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 123 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame^] | 124 | @parameterize_encrypt("CBCVarTxt256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 125 | def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 126 | cipher = BlockCipher( |
| 127 | ciphers.AES(binascii.unhexlify(key)), |
| 128 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 129 | ) |
| 130 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 131 | assert binascii.hexlify(actual_ciphertext) == ciphertext |