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 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 14 | def parameterize_kat_encrypt(fname): |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 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 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 24 | def paramterize_mmt_encrypt(fname): |
| 25 | return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 26 | load_nist_vectors_from_file( |
| 27 | os.path.join("AES/MMT", fname), |
| 28 | "ENCRYPT", |
| 29 | ["key", "iv", "plaintext", "ciphertext"], |
| 30 | ) |
| 31 | ) |
| 32 | |
| 33 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 34 | class TestAES_CBC(object): |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 35 | @parameterize_kat_encrypt("CBCGFSbox128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 36 | def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 37 | cipher = BlockCipher( |
| 38 | ciphers.AES(binascii.unhexlify(key)), |
| 39 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 40 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 41 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 42 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 43 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 44 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 45 | @parameterize_kat_encrypt("CBCGFSbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 46 | def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 47 | cipher = BlockCipher( |
| 48 | ciphers.AES(binascii.unhexlify(key)), |
| 49 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 50 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 51 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 52 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 53 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 54 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 55 | @parameterize_kat_encrypt("CBCGFSbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 56 | def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 57 | cipher = BlockCipher( |
| 58 | ciphers.AES(binascii.unhexlify(key)), |
| 59 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 60 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 61 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 62 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 63 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 64 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 65 | @parameterize_kat_encrypt("CBCKeySbox128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 66 | def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 67 | cipher = BlockCipher( |
| 68 | ciphers.AES(binascii.unhexlify(key)), |
| 69 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 70 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 71 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 72 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 73 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 74 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 75 | @parameterize_kat_encrypt("CBCKeySbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 76 | def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 77 | cipher = BlockCipher( |
| 78 | ciphers.AES(binascii.unhexlify(key)), |
| 79 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 80 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 81 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 82 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 83 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 84 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 85 | @parameterize_kat_encrypt("CBCKeySbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 86 | def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 87 | cipher = BlockCipher( |
| 88 | ciphers.AES(binascii.unhexlify(key)), |
| 89 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 90 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 91 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 92 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 93 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 94 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 95 | @parameterize_kat_encrypt("CBCVarKey128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 96 | def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 97 | cipher = BlockCipher( |
| 98 | ciphers.AES(binascii.unhexlify(key)), |
| 99 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 100 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 101 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 102 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 103 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 104 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 105 | @parameterize_kat_encrypt("CBCVarKey192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 106 | def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 107 | cipher = BlockCipher( |
| 108 | ciphers.AES(binascii.unhexlify(key)), |
| 109 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 110 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 111 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 112 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 113 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 114 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 115 | @parameterize_kat_encrypt("CBCVarKey256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 116 | def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 117 | cipher = BlockCipher( |
| 118 | ciphers.AES(binascii.unhexlify(key)), |
| 119 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 120 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 121 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 122 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 123 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 124 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 125 | @parameterize_kat_encrypt("CBCVarTxt128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 126 | def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 127 | cipher = BlockCipher( |
| 128 | ciphers.AES(binascii.unhexlify(key)), |
| 129 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 130 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 131 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 132 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 133 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 134 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 135 | @parameterize_kat_encrypt("CBCVarTxt192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 136 | def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 137 | cipher = BlockCipher( |
| 138 | ciphers.AES(binascii.unhexlify(key)), |
| 139 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 140 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 141 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 142 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 143 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 144 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 145 | @parameterize_kat_encrypt("CBCVarTxt256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 146 | def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 147 | cipher = BlockCipher( |
| 148 | ciphers.AES(binascii.unhexlify(key)), |
| 149 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 150 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 151 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 152 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 153 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 154 | |
| 155 | @paramterize_mmt_encrypt("CBCMMT128.rsp") |
| 156 | def test_MMT_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 157 | cipher = BlockCipher( |
| 158 | ciphers.AES(binascii.unhexlify(key)), |
| 159 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 160 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 161 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 162 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 163 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 164 | |
| 165 | @paramterize_mmt_encrypt("CBCMMT192.rsp") |
| 166 | def test_MMT_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 167 | cipher = BlockCipher( |
| 168 | ciphers.AES(binascii.unhexlify(key)), |
| 169 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 170 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 171 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 172 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 173 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 174 | |
| 175 | @paramterize_mmt_encrypt("CBCMMT256.rsp") |
| 176 | def test_MMT_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 177 | cipher = BlockCipher( |
| 178 | ciphers.AES(binascii.unhexlify(key)), |
| 179 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 180 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 181 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 182 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 183 | assert binascii.hexlify(actual_ciphertext) == ciphertext |