blob: 65d130adfc81a9d1da5b68e4c470635a9d8e1e4c [file] [log] [blame]
Alex Gaynora2e1f542013-08-10 08:59:11 -04001# 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 Stufftec672e82013-08-09 01:20:03 -040014"""
15Test using the NIST Test Vectors
16"""
Hynek Schlawack425f5842013-08-11 09:54:59 +020017
18from __future__ import absolute_import, division, print_function
19
Donald Stufftec672e82013-08-09 01:20:03 -040020import binascii
Alex Gaynor735df002013-09-09 16:46:03 -070021import itertools
Alex Gaynoraef7ee82013-08-08 22:31:11 -070022import os
Donald Stufftec672e82013-08-09 01:20:03 -040023
24import pytest
25
Donald Stufftea8e9fc2013-08-10 15:22:28 -040026from cryptography.primitives.block import BlockCipher, ciphers, modes
Donald Stufftec672e82013-08-09 01:20:03 -040027
28from ..utils import load_nist_vectors_from_file
29
30
Alex Gaynor735df002013-09-09 16:46:03 -070031def parameterize_encrypt_test(cipher, vector_type, fnames):
Alex Gaynoraef7ee82013-08-08 22:31:11 -070032 return pytest.mark.parametrize(("key", "iv", "plaintext", "ciphertext"),
Alex Gaynor735df002013-09-09 16:46:03 -070033 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 Stufft3704a832013-08-09 06:01:41 -040041 )
42
43
Alex Gaynoraef7ee82013-08-08 22:31:11 -070044class TestAES_CBC(object):
Alex Gaynor735df002013-09-09 16:46:03 -070045 @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 Stufftec672e82013-08-09 01:20:03 -040060 cipher = BlockCipher(
61 ciphers.AES(binascii.unhexlify(key)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040062 modes.CBC(binascii.unhexlify(iv)),
Donald Stufftec672e82013-08-09 01:20:03 -040063 )
Donald Stufft5de03922013-08-09 07:28:31 -040064 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
65 actual_ciphertext += cipher.finalize()
Donald Stufftec672e82013-08-09 01:20:03 -040066 assert binascii.hexlify(actual_ciphertext) == ciphertext
67
Alex Gaynor735df002013-09-09 16:46:03 -070068 @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 Stufft3704a832013-08-09 06:01:41 -040074 cipher = BlockCipher(
75 ciphers.AES(binascii.unhexlify(key)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040076 modes.CBC(binascii.unhexlify(iv)),
Donald Stufft3704a832013-08-09 06:01:41 -040077 )
Donald Stufft5de03922013-08-09 07:28:31 -040078 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
79 actual_ciphertext += cipher.finalize()
Donald Stufft3704a832013-08-09 06:01:41 -040080 assert binascii.hexlify(actual_ciphertext) == ciphertext