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