Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 1 | """ |
| 2 | Test using the NIST Test Vectors |
| 3 | """ |
| 4 | import binascii |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from cryptography.primitives.block import BlockCipher, ciphers, modes, padding |
| 9 | |
| 10 | from ..utils import load_nist_vectors_from_file |
| 11 | |
| 12 | |
Alex Gaynor | 2fbbe13 | 2013-08-08 22:22:58 -0700 | [diff] [blame^] | 13 | class TestAES_CBC(object): |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 14 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 15 | load_nist_vectors_from_file( |
| 16 | "AES/KAT/CBCGFSbox128.rsp", |
| 17 | "ENCRYPT", |
| 18 | ["key", "iv", "plaintext", "ciphertext"], |
| 19 | ), |
| 20 | ) |
| 21 | def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 22 | cipher = BlockCipher( |
| 23 | ciphers.AES(binascii.unhexlify(key)), |
| 24 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 25 | ) |
| 26 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 27 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 28 | |
| 29 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 30 | load_nist_vectors_from_file( |
| 31 | "AES/KAT/CBCGFSbox192.rsp", |
| 32 | "ENCRYPT", |
| 33 | ["key", "iv", "plaintext", "ciphertext"], |
| 34 | ), |
| 35 | ) |
| 36 | def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 37 | cipher = BlockCipher( |
| 38 | ciphers.AES(binascii.unhexlify(key)), |
| 39 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 40 | ) |
| 41 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 42 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 43 | |
| 44 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 45 | load_nist_vectors_from_file( |
| 46 | "AES/KAT/CBCGFSbox256.rsp", |
| 47 | "ENCRYPT", |
| 48 | ["key", "iv", "plaintext", "ciphertext"], |
| 49 | ), |
| 50 | ) |
| 51 | def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 52 | cipher = BlockCipher( |
| 53 | ciphers.AES(binascii.unhexlify(key)), |
| 54 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 55 | ) |
| 56 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 57 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 58 | |
| 59 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 60 | load_nist_vectors_from_file( |
| 61 | "AES/KAT/CBCKeySbox128.rsp", |
| 62 | "ENCRYPT", |
| 63 | ["key", "iv", "plaintext", "ciphertext"], |
| 64 | ), |
| 65 | ) |
| 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 | ) |
| 71 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 72 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 73 | |
| 74 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 75 | load_nist_vectors_from_file( |
| 76 | "AES/KAT/CBCKeySbox192.rsp", |
| 77 | "ENCRYPT", |
| 78 | ["key", "iv", "plaintext", "ciphertext"], |
| 79 | ), |
| 80 | ) |
| 81 | def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 82 | cipher = BlockCipher( |
| 83 | ciphers.AES(binascii.unhexlify(key)), |
| 84 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 85 | ) |
| 86 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 87 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 88 | |
| 89 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 90 | load_nist_vectors_from_file( |
| 91 | "AES/KAT/CBCKeySbox256.rsp", |
| 92 | "ENCRYPT", |
| 93 | ["key", "iv", "plaintext", "ciphertext"], |
| 94 | ), |
| 95 | ) |
| 96 | def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 97 | cipher = BlockCipher( |
| 98 | ciphers.AES(binascii.unhexlify(key)), |
| 99 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 100 | ) |
| 101 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 102 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 103 | |
| 104 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 105 | load_nist_vectors_from_file( |
| 106 | "AES/KAT/CBCVarKey128.rsp", |
| 107 | "ENCRYPT", |
| 108 | ["key", "iv", "plaintext", "ciphertext"], |
| 109 | ), |
| 110 | ) |
| 111 | def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 112 | cipher = BlockCipher( |
| 113 | ciphers.AES(binascii.unhexlify(key)), |
| 114 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 115 | ) |
| 116 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 117 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 118 | |
| 119 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 120 | load_nist_vectors_from_file( |
| 121 | "AES/KAT/CBCVarKey192.rsp", |
| 122 | "ENCRYPT", |
| 123 | ["key", "iv", "plaintext", "ciphertext"], |
| 124 | ), |
| 125 | ) |
| 126 | def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 127 | cipher = BlockCipher( |
| 128 | ciphers.AES(binascii.unhexlify(key)), |
| 129 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 130 | ) |
| 131 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 132 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 133 | |
| 134 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 135 | load_nist_vectors_from_file( |
| 136 | "AES/KAT/CBCVarKey256.rsp", |
| 137 | "ENCRYPT", |
| 138 | ["key", "iv", "plaintext", "ciphertext"], |
| 139 | ), |
| 140 | ) |
| 141 | def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 142 | cipher = BlockCipher( |
| 143 | ciphers.AES(binascii.unhexlify(key)), |
| 144 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 145 | ) |
| 146 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 147 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 148 | |
| 149 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 150 | load_nist_vectors_from_file( |
| 151 | "AES/KAT/CBCVarTxt128.rsp", |
| 152 | "ENCRYPT", |
| 153 | ["key", "iv", "plaintext", "ciphertext"], |
| 154 | ), |
| 155 | ) |
| 156 | def test_KAT_VarTxt_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 | ) |
| 161 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 162 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 163 | |
| 164 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 165 | load_nist_vectors_from_file( |
| 166 | "AES/KAT/CBCVarTxt192.rsp", |
| 167 | "ENCRYPT", |
| 168 | ["key", "iv", "plaintext", "ciphertext"], |
| 169 | ), |
| 170 | ) |
| 171 | def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 172 | cipher = BlockCipher( |
| 173 | ciphers.AES(binascii.unhexlify(key)), |
| 174 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 175 | ) |
| 176 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 177 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 178 | |
| 179 | @pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 180 | load_nist_vectors_from_file( |
| 181 | "AES/KAT/CBCVarTxt256.rsp", |
| 182 | "ENCRYPT", |
| 183 | ["key", "iv", "plaintext", "ciphertext"], |
| 184 | ), |
| 185 | ) |
| 186 | def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 187 | cipher = BlockCipher( |
| 188 | ciphers.AES(binascii.unhexlify(key)), |
| 189 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 190 | ) |
| 191 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 192 | assert binascii.hexlify(actual_ciphertext) == ciphertext |