blob: c30bda48527b7277e7b8209885841cf297ccc2cd [file] [log] [blame]
Paul Kehrerdff22d42013-09-27 13:43:06 -05001# 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
14"""
15Test using the CRYPTREC (Camellia) Test Vectors
16"""
17
18from __future__ import absolute_import, division, print_function
19
20import binascii
21import itertools
22import os
23
24import pytest
25
Paul Kehrerdff22d42013-09-27 13:43:06 -050026from cryptography.primitives.block import BlockCipher, ciphers, modes
27
28from ..utils import load_cryptrec_vectors_from_file
29
Paul Kehrerdff22d42013-09-27 13:43:06 -050030
31def parameterize_encrypt_test(cipher, vector_type, params, fnames):
32 return pytest.mark.parametrize(params,
33 list(itertools.chain.from_iterable(
34 load_cryptrec_vectors_from_file(
35 os.path.join(cipher, vector_type, fname),
36 )
37 for fname in fnames
38 ))
39 )
40
41
Paul Kehrerdff22d42013-09-27 13:43:06 -050042class TestCamelliaECB(object):
Paul Kehrerdff22d42013-09-27 13:43:06 -050043 @parameterize_encrypt_test(
44 "Camellia", "NTT",
45 ("key", "plaintext", "ciphertext"),
46 [
47 "camellia-128-ecb.txt",
48 "camellia-192-ecb.txt",
49 "camellia-256-ecb.txt",
50 ]
51 )
Paul Kehrerf2ce1ae2013-10-03 21:54:05 -050052 def test_NTT(self, key, plaintext, ciphertext, api):
Paul Kehrerf5427782013-10-15 20:02:10 -050053 if not api.supports_cipher("camellia-128-ecb"):
54 pytest.skip("Does not support Camellia ECB") # pragma: no cover
Paul Kehrerdff22d42013-09-27 13:43:06 -050055 cipher = BlockCipher(
56 ciphers.Camellia(binascii.unhexlify(key)),
57 modes.ECB(),
Paul Kehrerf2ce1ae2013-10-03 21:54:05 -050058 api
Paul Kehrerdff22d42013-09-27 13:43:06 -050059 )
60 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
61 actual_ciphertext += cipher.finalize()
62 assert binascii.hexlify(actual_ciphertext).upper() == ciphertext