Alex Gaynor | a2e1f54 | 2013-08-10 08:59:11 -0400 | [diff] [blame^] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | # you may not use this file except in compliance with the License. |
| 3 | # You may obtain a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 10 | # implied. |
| 11 | # See the License for the specific language governing permissions and |
| 12 | # limitations under the License. |
| 13 | |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 14 | """ |
| 15 | Test using the NIST Test Vectors |
| 16 | """ |
| 17 | import binascii |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 18 | import os |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 19 | |
| 20 | import pytest |
| 21 | |
| 22 | from cryptography.primitives.block import BlockCipher, ciphers, modes, padding |
| 23 | |
| 24 | from ..utils import load_nist_vectors_from_file |
| 25 | |
| 26 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 27 | def parameterize_kat_encrypt(fname): |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 28 | return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 29 | load_nist_vectors_from_file( |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 30 | os.path.join("AES/KAT/", fname), |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 31 | "ENCRYPT", |
| 32 | ["key", "iv", "plaintext", "ciphertext"], |
| 33 | ), |
| 34 | ) |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 35 | |
| 36 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 37 | def paramterize_mmt_encrypt(fname): |
| 38 | return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
| 39 | load_nist_vectors_from_file( |
| 40 | os.path.join("AES/MMT", fname), |
| 41 | "ENCRYPT", |
| 42 | ["key", "iv", "plaintext", "ciphertext"], |
| 43 | ) |
| 44 | ) |
| 45 | |
| 46 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 47 | class TestAES_CBC(object): |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 48 | @parameterize_kat_encrypt("CBCGFSbox128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 49 | def test_KAT_GFSbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 50 | cipher = BlockCipher( |
| 51 | ciphers.AES(binascii.unhexlify(key)), |
| 52 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 53 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 54 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 55 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 56 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 57 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 58 | @parameterize_kat_encrypt("CBCGFSbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 59 | def test_KAT_GFSbox_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 60 | cipher = BlockCipher( |
| 61 | ciphers.AES(binascii.unhexlify(key)), |
| 62 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 63 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 64 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 65 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 66 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 67 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 68 | @parameterize_kat_encrypt("CBCGFSbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 69 | def test_KAT_GFSbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 70 | cipher = BlockCipher( |
| 71 | ciphers.AES(binascii.unhexlify(key)), |
| 72 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 73 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 74 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 75 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 76 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 77 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 78 | @parameterize_kat_encrypt("CBCKeySbox128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 79 | def test_KAT_KeySbox_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 80 | cipher = BlockCipher( |
| 81 | ciphers.AES(binascii.unhexlify(key)), |
| 82 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 83 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 84 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 85 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 86 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 87 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 88 | @parameterize_kat_encrypt("CBCKeySbox192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 89 | def test_KAT_KeySbox_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 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 94 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 95 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 96 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 97 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 98 | @parameterize_kat_encrypt("CBCKeySbox256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 99 | def test_KAT_KeySbox_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 100 | cipher = BlockCipher( |
| 101 | ciphers.AES(binascii.unhexlify(key)), |
| 102 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 103 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 104 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 105 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 106 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 107 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 108 | @parameterize_kat_encrypt("CBCVarKey128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 109 | def test_KAT_VarKey_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 110 | cipher = BlockCipher( |
| 111 | ciphers.AES(binascii.unhexlify(key)), |
| 112 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 113 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 114 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 115 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 116 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 117 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 118 | @parameterize_kat_encrypt("CBCVarKey192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 119 | def test_KAT_VarKey_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 120 | cipher = BlockCipher( |
| 121 | ciphers.AES(binascii.unhexlify(key)), |
| 122 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 123 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 124 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 125 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 126 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 127 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 128 | @parameterize_kat_encrypt("CBCVarKey256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 129 | def test_KAT_VarKey_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 130 | cipher = BlockCipher( |
| 131 | ciphers.AES(binascii.unhexlify(key)), |
| 132 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 133 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 134 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 135 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 136 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 137 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 138 | @parameterize_kat_encrypt("CBCVarTxt128.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 139 | def test_KAT_VarTxt_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 140 | cipher = BlockCipher( |
| 141 | ciphers.AES(binascii.unhexlify(key)), |
| 142 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 143 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 144 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 145 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 146 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 147 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 148 | @parameterize_kat_encrypt("CBCVarTxt192.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 149 | def test_KAT_VarTxt_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 150 | cipher = BlockCipher( |
| 151 | ciphers.AES(binascii.unhexlify(key)), |
| 152 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 153 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 154 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 155 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 156 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 157 | |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 158 | @parameterize_kat_encrypt("CBCVarTxt256.rsp") |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 159 | def test_KAT_VarTxt_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 160 | cipher = BlockCipher( |
| 161 | ciphers.AES(binascii.unhexlify(key)), |
| 162 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 163 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 164 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 165 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 166 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 167 | |
| 168 | @paramterize_mmt_encrypt("CBCMMT128.rsp") |
| 169 | def test_MMT_128_encrypt(self, key, iv, plaintext, ciphertext): |
| 170 | cipher = BlockCipher( |
| 171 | ciphers.AES(binascii.unhexlify(key)), |
| 172 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 173 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 174 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 175 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 176 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 177 | |
| 178 | @paramterize_mmt_encrypt("CBCMMT192.rsp") |
| 179 | def test_MMT_192_encrypt(self, key, iv, plaintext, ciphertext): |
| 180 | cipher = BlockCipher( |
| 181 | ciphers.AES(binascii.unhexlify(key)), |
| 182 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 183 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 184 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 185 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 186 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 187 | |
| 188 | @paramterize_mmt_encrypt("CBCMMT256.rsp") |
| 189 | def test_MMT_256_encrypt(self, key, iv, plaintext, ciphertext): |
| 190 | cipher = BlockCipher( |
| 191 | ciphers.AES(binascii.unhexlify(key)), |
| 192 | modes.CBC(binascii.unhexlify(iv), padding.NoPadding()) |
| 193 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 194 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 195 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 196 | assert binascii.hexlify(actual_ciphertext) == ciphertext |