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 | ) |
| 41 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 42 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 43 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 44 | @parameterize_kat_encrypt("CBCGFSbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 45 | def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 46 | cipher = BlockCipher( |
| 47 | ciphers.AES(binascii.unhexlify(key)), |
| 48 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 49 | ) |
| 50 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 51 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 52 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 53 | @parameterize_kat_encrypt("CBCGFSbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 54 | def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 55 | cipher = BlockCipher( |
| 56 | ciphers.AES(binascii.unhexlify(key)), |
| 57 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 58 | ) |
| 59 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 60 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 61 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 62 | @parameterize_kat_encrypt("CBCKeySbox128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 63 | def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 64 | cipher = BlockCipher( |
| 65 | ciphers.AES(binascii.unhexlify(key)), |
| 66 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 67 | ) |
| 68 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 69 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 70 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 71 | @parameterize_kat_encrypt("CBCKeySbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 72 | def test_KAT_KeySbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 73 | cipher = BlockCipher( |
| 74 | ciphers.AES(binascii.unhexlify(key)), |
| 75 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 76 | ) |
| 77 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 78 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 79 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 80 | @parameterize_kat_encrypt("CBCKeySbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 81 | def test_KAT_KeySbox_256_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 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 89 | @parameterize_kat_encrypt("CBCVarKey128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 90 | def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 91 | cipher = BlockCipher( |
| 92 | ciphers.AES(binascii.unhexlify(key)), |
| 93 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 94 | ) |
| 95 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 96 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 97 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 98 | @parameterize_kat_encrypt("CBCVarKey192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 99 | def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 100 | cipher = BlockCipher( |
| 101 | ciphers.AES(binascii.unhexlify(key)), |
| 102 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 103 | ) |
| 104 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 105 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 106 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 107 | @parameterize_kat_encrypt("CBCVarKey256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 108 | def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 109 | cipher = BlockCipher( |
| 110 | ciphers.AES(binascii.unhexlify(key)), |
| 111 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 112 | ) |
| 113 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 114 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 115 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 116 | @parameterize_kat_encrypt("CBCVarTxt128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 117 | def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 118 | cipher = BlockCipher( |
| 119 | ciphers.AES(binascii.unhexlify(key)), |
| 120 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 121 | ) |
| 122 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 123 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 124 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 125 | @parameterize_kat_encrypt("CBCVarTxt192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 126 | def test_KAT_VarTxt_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 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 134 | @parameterize_kat_encrypt("CBCVarTxt256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 135 | def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 136 | cipher = BlockCipher( |
| 137 | ciphers.AES(binascii.unhexlify(key)), |
| 138 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 139 | ) |
| 140 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 141 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame^] | 142 | |
| 143 | @paramterize_mmt_encrypt("CBCMMT128.rsp") |
| 144 | def test_MMT_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 145 | cipher = BlockCipher( |
| 146 | ciphers.AES(binascii.unhexlify(key)), |
| 147 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 148 | ) |
| 149 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 150 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 151 | |
| 152 | @paramterize_mmt_encrypt("CBCMMT192.rsp") |
| 153 | def test_MMT_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 154 | cipher = BlockCipher( |
| 155 | ciphers.AES(binascii.unhexlify(key)), |
| 156 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 157 | ) |
| 158 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 159 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 160 | |
| 161 | @paramterize_mmt_encrypt("CBCMMT256.rsp") |
| 162 | def test_MMT_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 163 | cipher = BlockCipher( |
| 164 | ciphers.AES(binascii.unhexlify(key)), |
| 165 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 166 | ) |
| 167 | actual_ciphertext = cipher.encrypt(plaintext) + cipher.finalize() |
| 168 | assert binascii.hexlify(actual_ciphertext) == ciphertext |