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 | |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 26 | from cryptography.primitives.block import BlockCipher, ciphers, modes |
| 27 | |
| 28 | from ..utils import load_cryptrec_vectors_from_file |
| 29 | |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 30 | |
| 31 | def 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 Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 42 | class TestCamelliaECB(object): |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 43 | @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 Kehrer | f2ce1ae | 2013-10-03 21:54:05 -0500 | [diff] [blame] | 52 | def test_NTT(self, key, plaintext, ciphertext, api): |
Paul Kehrer | f542778 | 2013-10-15 20:02:10 -0500 | [diff] [blame] | 53 | if not api.supports_cipher("camellia-128-ecb"): |
| 54 | pytest.skip("Does not support Camellia ECB") # pragma: no cover |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 55 | cipher = BlockCipher( |
| 56 | ciphers.Camellia(binascii.unhexlify(key)), |
| 57 | modes.ECB(), |
Paul Kehrer | f2ce1ae | 2013-10-03 21:54:05 -0500 | [diff] [blame] | 58 | api |
Paul Kehrer | dff22d4 | 2013-09-27 13:43:06 -0500 | [diff] [blame] | 59 | ) |
| 60 | actual_ciphertext = cipher.encrypt(binascii.unhexlify(plaintext)) |
| 61 | actual_ciphertext += cipher.finalize() |
| 62 | assert binascii.hexlify(actual_ciphertext).upper() == ciphertext |