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 | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 21 | import itertools |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 22 | import os |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 23 | |
| 24 | import pytest |
| 25 | |
Donald Stufft | ea8e9fc | 2013-08-10 15:22:28 -0400 | [diff] [blame] | 26 | from cryptography.primitives.block import BlockCipher, ciphers, modes |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 27 | |
| 28 | from ..utils import load_nist_vectors_from_file |
| 29 | |
| 30 | |
Paul Kehrer | 2354fcd | 2013-09-10 19:15:36 -0500 | [diff] [blame] | 31 | def parameterize_encrypt_test(cipher, vector_type, params, fnames): |
| 32 | return pytest.mark.parametrize(params, |
Alex Gaynor | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 33 | list(itertools.chain.from_iterable( |
| 34 | load_nist_vectors_from_file( |
| 35 | os.path.join(cipher, vector_type, fname), |
| 36 | "ENCRYPT", |
Paul Kehrer | 2354fcd | 2013-09-10 19:15:36 -0500 | [diff] [blame] | 37 | params |
Alex Gaynor | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 38 | ) |
| 39 | for fname in fnames |
| 40 | )) |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 44 | class TestAES_CBC(object): |
Paul Kehrer | 2354fcd | 2013-09-10 19:15:36 -0500 | [diff] [blame] | 45 | @parameterize_encrypt_test( |
| 46 | "AES", "KAT", |
| 47 | ("key", "iv", "plaintext", "ciphertext"), |
| 48 | [ |
| 49 | "CBCGFSbox128.rsp", |
| 50 | "CBCGFSbox192.rsp", |
| 51 | "CBCGFSbox256.rsp", |
| 52 | "CBCKeySbox128.rsp", |
| 53 | "CBCKeySbox192.rsp", |
| 54 | "CBCKeySbox256.rsp", |
| 55 | "CBCVarKey128.rsp", |
| 56 | "CBCVarKey192.rsp", |
| 57 | "CBCVarKey256.rsp", |
| 58 | "CBCVarTxt128.rsp", |
| 59 | "CBCVarTxt192.rsp", |
| 60 | "CBCVarTxt256.rsp", |
| 61 | ] |
| 62 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 63 | def test_KAT(self, key, iv, plaintext, ciphertext, api): |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 64 | cipher = BlockCipher( |
| 65 | ciphers.AES(binascii.unhexlify(key)), |
Donald Stufft | ea8e9fc | 2013-08-10 15:22:28 -0400 | [diff] [blame] | 66 | modes.CBC(binascii.unhexlify(iv)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 67 | api |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 68 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 69 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 70 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 71 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 72 | |
Paul Kehrer | 2354fcd | 2013-09-10 19:15:36 -0500 | [diff] [blame] | 73 | @parameterize_encrypt_test( |
| 74 | "AES", "MMT", |
| 75 | ("key", "iv", "plaintext", "ciphertext"), |
| 76 | [ |
| 77 | "CBCMMT128.rsp", |
| 78 | "CBCMMT192.rsp", |
| 79 | "CBCMMT256.rsp", |
| 80 | ] |
| 81 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 82 | def test_MMT(self, key, iv, plaintext, ciphertext, api): |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 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)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 86 | api |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 87 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 88 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 89 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 90 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 91 | |
| 92 | |
| 93 | class TestAES_ECB(object): |
| 94 | @parameterize_encrypt_test( |
| 95 | "AES", "KAT", |
| 96 | ("key", "plaintext", "ciphertext"), |
| 97 | [ |
| 98 | "ECBGFSbox128.rsp", |
| 99 | "ECBGFSbox192.rsp", |
| 100 | "ECBGFSbox256.rsp", |
| 101 | "ECBKeySbox128.rsp", |
| 102 | "ECBKeySbox192.rsp", |
| 103 | "ECBKeySbox256.rsp", |
| 104 | "ECBVarKey128.rsp", |
| 105 | "ECBVarKey192.rsp", |
| 106 | "ECBVarKey256.rsp", |
| 107 | "ECBVarTxt128.rsp", |
| 108 | "ECBVarTxt192.rsp", |
| 109 | "ECBVarTxt256.rsp", |
| 110 | ] |
| 111 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 112 | def test_KAT(self, key, plaintext, ciphertext, api): |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 113 | cipher = BlockCipher( |
| 114 | ciphers.AES(binascii.unhexlify(key)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 115 | modes.ECB(), |
| 116 | api |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 117 | ) |
| 118 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 119 | actual_ciphertext += cipher.finalize() |
| 120 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 121 | |
| 122 | @parameterize_encrypt_test( |
| 123 | "AES", "MMT", |
| 124 | ("key", "plaintext", "ciphertext"), |
| 125 | [ |
| 126 | "ECBMMT128.rsp", |
| 127 | "ECBMMT192.rsp", |
| 128 | "ECBMMT256.rsp", |
| 129 | ] |
| 130 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 131 | def test_MMT(self, key, plaintext, ciphertext, api): |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 132 | cipher = BlockCipher( |
| 133 | ciphers.AES(binascii.unhexlify(key)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 134 | modes.ECB(), |
| 135 | api |
Paul Kehrer | 13f108f | 2013-09-09 21:41:03 -0500 | [diff] [blame] | 136 | ) |
| 137 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 138 | actual_ciphertext += cipher.finalize() |
| 139 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 140 | |
| 141 | |
| 142 | class TestAES_OFB(object): |
| 143 | @parameterize_encrypt_test( |
| 144 | "AES", "KAT", |
| 145 | ("key", "iv", "plaintext", "ciphertext"), |
| 146 | [ |
| 147 | "OFBGFSbox128.rsp", |
| 148 | "OFBGFSbox192.rsp", |
| 149 | "OFBGFSbox256.rsp", |
| 150 | "OFBKeySbox128.rsp", |
| 151 | "OFBKeySbox192.rsp", |
| 152 | "OFBKeySbox256.rsp", |
| 153 | "OFBVarKey128.rsp", |
| 154 | "OFBVarKey192.rsp", |
| 155 | "OFBVarKey256.rsp", |
| 156 | "OFBVarTxt128.rsp", |
| 157 | "OFBVarTxt192.rsp", |
| 158 | "OFBVarTxt256.rsp", |
| 159 | ] |
| 160 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 161 | def test_KAT(self, key, iv, plaintext, ciphertext, api): |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 162 | cipher = BlockCipher( |
| 163 | ciphers.AES(binascii.unhexlify(key)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 164 | modes.OFB(binascii.unhexlify(iv)), |
| 165 | api |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 166 | ) |
| 167 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 168 | actual_ciphertext += cipher.finalize() |
| 169 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 170 | |
| 171 | @parameterize_encrypt_test( |
| 172 | "AES", "MMT", |
| 173 | ("key", "iv", "plaintext", "ciphertext"), |
| 174 | [ |
| 175 | "OFBMMT128.rsp", |
| 176 | "OFBMMT192.rsp", |
| 177 | "OFBMMT256.rsp", |
| 178 | ] |
| 179 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 180 | def test_MMT(self, key, iv, plaintext, ciphertext, api): |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 181 | cipher = BlockCipher( |
| 182 | ciphers.AES(binascii.unhexlify(key)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 183 | modes.OFB(binascii.unhexlify(iv)), |
| 184 | api |
Paul Kehrer | 6f412a0 | 2013-09-10 21:30:50 -0500 | [diff] [blame] | 185 | ) |
| 186 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 187 | actual_ciphertext += cipher.finalize() |
| 188 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Paul Kehrer | a1ec262 | 2013-09-11 09:38:45 -0500 | [diff] [blame] | 189 | |
| 190 | |
| 191 | class TestAES_CFB(object): |
| 192 | @parameterize_encrypt_test( |
| 193 | "AES", "KAT", |
| 194 | ("key", "iv", "plaintext", "ciphertext"), |
| 195 | [ |
| 196 | "CFB128GFSbox128.rsp", |
| 197 | "CFB128GFSbox192.rsp", |
| 198 | "CFB128GFSbox256.rsp", |
| 199 | "CFB128KeySbox128.rsp", |
| 200 | "CFB128KeySbox192.rsp", |
| 201 | "CFB128KeySbox256.rsp", |
| 202 | "CFB128VarKey128.rsp", |
| 203 | "CFB128VarKey192.rsp", |
| 204 | "CFB128VarKey256.rsp", |
| 205 | "CFB128VarTxt128.rsp", |
| 206 | "CFB128VarTxt192.rsp", |
| 207 | "CFB128VarTxt256.rsp", |
| 208 | ] |
| 209 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 210 | def test_KAT(self, key, iv, plaintext, ciphertext, api): |
Paul Kehrer | a1ec262 | 2013-09-11 09:38:45 -0500 | [diff] [blame] | 211 | cipher = BlockCipher( |
| 212 | ciphers.AES(binascii.unhexlify(key)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 213 | modes.CFB(binascii.unhexlify(iv)), |
| 214 | api |
Paul Kehrer | a1ec262 | 2013-09-11 09:38:45 -0500 | [diff] [blame] | 215 | ) |
| 216 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 217 | actual_ciphertext += cipher.finalize() |
| 218 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 219 | |
| 220 | @parameterize_encrypt_test( |
| 221 | "AES", "MMT", |
| 222 | ("key", "iv", "plaintext", "ciphertext"), |
| 223 | [ |
| 224 | "CFB128MMT128.rsp", |
| 225 | "CFB128MMT192.rsp", |
| 226 | "CFB128MMT256.rsp", |
| 227 | ] |
| 228 | ) |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 229 | def test_MMT(self, key, iv, plaintext, ciphertext, api): |
Paul Kehrer | a1ec262 | 2013-09-11 09:38:45 -0500 | [diff] [blame] | 230 | cipher = BlockCipher( |
| 231 | ciphers.AES(binascii.unhexlify(key)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 232 | modes.CFB(binascii.unhexlify(iv)), |
| 233 | api |
Paul Kehrer | a1ec262 | 2013-09-11 09:38:45 -0500 | [diff] [blame] | 234 | ) |
| 235 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 236 | actual_ciphertext += cipher.finalize() |
| 237 | assert binascii.hexlify(actual_ciphertext) == ciphertext |