blob: 54ae4d0ca14ea5b38e04ab10a4735fb1faa6e641 [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
26from cryptography.bindings.openssl.api import api
27from cryptography.primitives.block import BlockCipher, ciphers, modes
28
29from ..utils import load_cryptrec_vectors_from_file
30
31CAMELLIA_ECB_SUPPORTED = api.supports('camellia-128-ecb')
32
33
34def parameterize_encrypt_test(cipher, vector_type, params, fnames):
35 return pytest.mark.parametrize(params,
36 list(itertools.chain.from_iterable(
37 load_cryptrec_vectors_from_file(
38 os.path.join(cipher, vector_type, fname),
39 )
40 for fname in fnames
41 ))
42 )
43
44
45@pytest.mark.skipif("not CAMELLIA_ECB_SUPPORTED")
46class TestCamelliaECB(object):
47
48 @parameterize_encrypt_test(
49 "Camellia", "NTT",
50 ("key", "plaintext", "ciphertext"),
51 [
52 "camellia-128-ecb.txt",
53 "camellia-192-ecb.txt",
54 "camellia-256-ecb.txt",
55 ]
56 )
57 def test_NTT(self, key, plaintext, ciphertext):
58 cipher = BlockCipher(
59 ciphers.Camellia(binascii.unhexlify(key)),
60 modes.ECB(),
61 )
62 actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext))
63 actual_ciphertext += cipher.finalize()
64 assert binascii.hexlify(actual_ciphertext).upper() == ciphertext