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 | |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [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", |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [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): |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [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 | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 63 | def test_KAT(self, key, iv, plaintext, ciphertext): |
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)), |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 67 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 68 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 69 | actual_ciphertext += cipher.finalize() |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 70 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 71 | |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [diff] [blame^] | 72 | @parameterize_encrypt_test( |
| 73 | "AES", "MMT", |
| 74 | ["key", "iv", "plaintext", "ciphertext"], |
| 75 | [ |
| 76 | "CBCMMT128.rsp", |
| 77 | "CBCMMT192.rsp", |
| 78 | "CBCMMT256.rsp", |
| 79 | ] |
| 80 | ) |
Alex Gaynor | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 81 | def test_MMT(self, key, iv, plaintext, ciphertext): |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 82 | cipher = BlockCipher( |
| 83 | ciphers.AES(binascii.unhexlify(key)), |
Donald Stufft | ea8e9fc | 2013-08-10 15:22:28 -0400 | [diff] [blame] | 84 | modes.CBC(binascii.unhexlify(iv)), |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 85 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 86 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 87 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 88 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Alex Gaynor | 9f44cab | 2013-09-09 19:04:16 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 91 | class TestTripleDES_CBC(object): |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [diff] [blame^] | 92 | @parameterize_encrypt_test( |
| 93 | "3DES", "KAT", |
| 94 | ["keys", "iv", "plaintext", "ciphertext"], |
| 95 | [ |
| 96 | "TCBCinvperm.rsp", |
| 97 | "TCBCpermop.rsp", |
| 98 | "TCBCsubtab.rsp", |
| 99 | "TCBCvarkey.rsp", |
| 100 | "TCBCvartext.rsp", |
| 101 | ] |
| 102 | ) |
| 103 | def test_KAT_1(self, keys, iv, plaintext, ciphertext): |
Alex Gaynor | 9f44cab | 2013-09-09 19:04:16 -0700 | [diff] [blame] | 104 | cipher = BlockCipher( |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [diff] [blame^] | 105 | ciphers.TripleDES(binascii.unhexlify(keys)), |
| 106 | modes.CBC(binascii.unhexlify(iv)), |
Alex Gaynor | 9f44cab | 2013-09-09 19:04:16 -0700 | [diff] [blame] | 107 | ) |
| 108 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 109 | actual_ciphertext += cipher.finalize() |
| 110 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 111 | |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [diff] [blame^] | 112 | @parameterize_encrypt_test( |
| 113 | "3DES", "KAT", |
| 114 | ["keys", "iv1", "iv2", "iv3", "plaintext", "ciphertext3"], |
| 115 | [ |
| 116 | "TCBCIpermop.rsp", |
| 117 | "TCBCIsubtab.rsp", |
| 118 | "TCBCIvarkey.rsp", |
| 119 | "TCBCIvartext.rsp", |
| 120 | ] |
| 121 | ) |
| 122 | def test_KAT_2(self, keys, iv1, iv2, iv3, plaintext, ciphertext3): |
Alex Gaynor | 9f44cab | 2013-09-09 19:04:16 -0700 | [diff] [blame] | 123 | cipher = BlockCipher( |
Alex Gaynor | 920e330 | 2013-09-10 16:55:52 -0700 | [diff] [blame^] | 124 | ciphers.TripleDES(binascii.unhexlify(keys)), |
| 125 | modes.CBC(binascii.unhexlify(iv1 + iv2 + iv3)), |
| 126 | ) |
| 127 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 128 | actual_ciphertext += cipher.finalize() |
| 129 | assert binascii.hexlify(actual_ciphertext) == ciphertext3 |
| 130 | |
| 131 | @parameterize_encrypt_test( |
| 132 | "3DES", "KAT", |
| 133 | ["keys", "iv1", "iv2", "iv3", "plaintext1", "ciphertext3"], |
| 134 | [ |
| 135 | "TCBCIinvperm.rsp", |
| 136 | ] |
| 137 | ) |
| 138 | def test_KAT_3(self, keys, iv1, iv2, iv3, plaintext1, ciphertext3): |
| 139 | cipher = BlockCipher( |
| 140 | ciphers.TripleDES(binascii.unhexlify(keys)), |
| 141 | modes.CBC(binascii.unhexlify(iv1 + iv2 + iv3)), |
| 142 | ) |
| 143 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext1)) |
| 144 | actual_ciphertext += cipher.finalize() |
| 145 | assert binascii.hexlify(actual_ciphertext) == ciphertext3 |
| 146 | |
| 147 | @parameterize_encrypt_test( |
| 148 | "3DES", "MMT", |
| 149 | ["key1", "key2", "key3", "iv1", "iv2", "iv3", "plaintext", "ciphertext"], |
| 150 | [ |
| 151 | "TCBCIMMT1.rsp", |
| 152 | "TCBCIMMT2.rsp", |
| 153 | "TCBCIMMT3.rsp", |
| 154 | ] |
| 155 | ) |
| 156 | def test_MMT_1(self, key1, key2, key3, iv1, iv2, iv3, plaintext, ciphertext): |
| 157 | cipher = BlockCipher( |
| 158 | ciphers.TripleDES(binascii.unhexlify(key1 + key2 + key3)), |
| 159 | modes.CBC(binascii.unhexlify(iv1 + iv2 + iv3)), |
| 160 | ) |
| 161 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 162 | actual_ciphertext += cipher.finalize() |
| 163 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 164 | |
| 165 | @parameterize_encrypt_test( |
| 166 | "3DES", "MMT", |
| 167 | ["key1", "key2", "key3", "iv", "plaintext", "ciphertext"], |
| 168 | [ |
| 169 | "TCBCMMT1.rsp", |
| 170 | "TCBCMMT2.rsp", |
| 171 | "TCBCMMT3.rsp", |
| 172 | ] |
| 173 | ) |
| 174 | def test_MMT_2(self, key1, key2, key3, iv, plaintext, ciphertext): |
| 175 | cipher = BlockCipher( |
| 176 | ciphers.TripleDES(binascii.unhexlify(key1 + key2 + key3)), |
| 177 | modes.CBC(binascii.unhexlify(iv)), |
Alex Gaynor | 9f44cab | 2013-09-09 19:04:16 -0700 | [diff] [blame] | 178 | ) |
| 179 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 180 | actual_ciphertext += cipher.finalize() |
| 181 | assert binascii.hexlify(actual_ciphertext) == ciphertext |