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 | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 31 | def parameterize_encrypt_test(cipher, vector_type, fnames): |
Alex Gaynor | aef7ee8 | 2013-08-08 22:31:11 -0700 | [diff] [blame] | 32 | return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"), |
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", |
| 37 | ["key", "iv", "plaintext", "ciphertext"], |
| 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 | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 45 | @parameterize_encrypt_test("AES", "KAT", [ |
| 46 | "CBCGFSbox128.rsp", |
| 47 | "CBCGFSbox192.rsp", |
| 48 | "CBCGFSbox256.rsp", |
| 49 | "CBCKeySbox128.rsp", |
| 50 | "CBCKeySbox192.rsp", |
| 51 | "CBCKeySbox256.rsp", |
| 52 | "CBCVarKey128.rsp", |
| 53 | "CBCVarKey192.rsp", |
| 54 | "CBCVarKey256.rsp", |
| 55 | "CBCVarTxt128.rsp", |
| 56 | "CBCVarTxt192.rsp", |
| 57 | "CBCVarTxt256.rsp", |
| 58 | ]) |
| 59 | def test_KAT(self, key, iv, plaintext, ciphertext): |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 60 | cipher = BlockCipher( |
| 61 | ciphers.AES(binascii.unhexlify(key)), |
Donald Stufft | ea8e9fc | 2013-08-10 15:22:28 -0400 | [diff] [blame] | 62 | modes.CBC(binascii.unhexlify(iv)), |
Donald Stufft | ec672e8 | 2013-08-09 01:20:03 -0400 | [diff] [blame] | 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 | |
Alex Gaynor | 735df00 | 2013-09-09 16:46:03 -0700 | [diff] [blame] | 68 | @parameterize_encrypt_test("AES", "MMT", [ |
| 69 | "CBCMMT128.rsp", |
| 70 | "CBCMMT192.rsp", |
| 71 | "CBCMMT256.rsp", |
| 72 | ]) |
| 73 | def test_MMT(self, key, iv, plaintext, ciphertext): |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 74 | cipher = BlockCipher( |
| 75 | ciphers.AES(binascii.unhexlify(key)), |
Donald Stufft | ea8e9fc | 2013-08-10 15:22:28 -0400 | [diff] [blame] | 76 | modes.CBC(binascii.unhexlify(iv)), |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 77 | ) |
Donald Stufft | 5de0392 | 2013-08-09 07:28:31 -0400 | [diff] [blame] | 78 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 79 | actual_ciphertext += cipher.finalize() |
Donald Stufft | 3704a83 | 2013-08-09 06:01:41 -0400 | [diff] [blame] | 80 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
Alex Gaynor | 9f44cab | 2013-09-09 19:04:16 -0700 | [diff] [blame^] | 81 | |
| 82 | |
| 83 | class TestTripleDES_CBC(object): |
| 84 | @parameterize_encrypt_test("3DES", "KAT", [ |
| 85 | "TCBCIinvperm.rsp", |
| 86 | "TCBCIpermop.rsp", |
| 87 | "TCBCIsubtab.rsp", |
| 88 | "TCBCIvarkey.rsp", |
| 89 | "TCBCIvartext.rsp", |
| 90 | "TCBCinvperm.rsp", |
| 91 | "TCBCpermop.rsp", |
| 92 | "TCBCsubtab.rsp", |
| 93 | "TCBCvarkey.rsp", |
| 94 | "TCBCvartext.rsp", |
| 95 | ]) |
| 96 | def test_KAT(self, key, iv, plaintext, ciphertext): |
| 97 | cipher = BlockCipher( |
| 98 | ciphers.TripleDES(binascii.unhexlify(key)), |
| 99 | modes.CBC(binascii.unhexlify(iv)) |
| 100 | ) |
| 101 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 102 | actual_ciphertext += cipher.finalize() |
| 103 | assert binascii.hexlify(actual_ciphertext) == ciphertext |
| 104 | |
| 105 | @parameterize_encrypt_test("3DES", "MMT", [ |
| 106 | "TCBCIMMT1.rsp", |
| 107 | "TCBCIMMT2.rsp", |
| 108 | "TCBCIMMT3.rsp", |
| 109 | "TCBCMMT1.rsp", |
| 110 | "TCBCMMT2.rsp", |
| 111 | "TCBCMMT3.rsp", |
| 112 | ]) |
| 113 | def test_MMT(self, key, iv, plaintext, ciphertext): |
| 114 | cipher = BlockCipher( |
| 115 | ciphers.TripleDES(binascii.unhexlify(key)), |
| 116 | modes.CBC(binascii.unhexlify(iv)) |
| 117 | ) |
| 118 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 119 | actual_ciphertext += cipher.finalize() |
| 120 | assert binascii.hexlify(actual_ciphertext) == ciphertext |