Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [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 | |
| 14 | """ |
| 15 | Test using the CRYPTREC (Camellia) Test Vectors |
| 16 | """ |
| 17 | |
| 18 | from __future__ import absolute_import, division, print_function |
| 19 | |
| 20 | import binascii |
| 21 | import itertools |
| 22 | import os |
| 23 | |
| 24 | import pytest |
| 25 | |
| 26 | from cryptography.bindings.openssl.api import api |
| 27 | from cryptography.primitives.block import BlockCipher, ciphers, modes |
| 28 | |
| 29 | from ..utils import load_cryptrec_vectors_from_file |
| 30 | |
| 31 | CAMELLIA_ECB_SUPPORTED = api.supports('camellia-128-ecb') |
| 32 | |
| 33 | |
| 34 | def 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") |
| 46 | class 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 |