blob: 9059a886047ffa3ffaf794d09faf5452d6eec93d [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
Alex Gaynor95b86202013-08-08 19:36:44 -070014import binascii
15
Donald Stufftbac187d2013-08-10 15:43:51 -040016import pretend
Alex Gaynor95b86202013-08-08 19:36:44 -070017import pytest
18
Donald Stufftea8e9fc2013-08-10 15:22:28 -040019from cryptography.primitives.block import BlockCipher, ciphers, modes
Donald Stufftbac187d2013-08-10 15:43:51 -040020from cryptography.primitives.block.base import _Operation
Alex Gaynor95b86202013-08-08 19:36:44 -070021
22
23class TestBlockCipher(object):
Donald Stufftadacdb82013-08-10 15:13:22 -040024 def test_cipher_name(self):
25 cipher = BlockCipher(
26 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040027 modes.CBC(binascii.unhexlify(b"0" * 32))
Donald Stufftadacdb82013-08-10 15:13:22 -040028 )
29 assert cipher.name == "AES-128-CBC"
30
Alex Gaynor250903a2013-08-09 12:12:30 -070031 def test_use_after_finalize(self):
Alex Gaynor95b86202013-08-08 19:36:44 -070032 cipher = BlockCipher(
Alex Gaynor250903a2013-08-09 12:12:30 -070033 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040034 modes.CBC(binascii.unhexlify(b"0" * 32))
Alex Gaynor95b86202013-08-08 19:36:44 -070035 )
Alex Gaynor250903a2013-08-09 12:12:30 -070036 cipher.encrypt(b"a" * 16)
37 cipher.finalize()
38 with pytest.raises(ValueError):
39 cipher.encrypt(b"b" * 16)
40 with pytest.raises(ValueError):
41 cipher.finalize()
Donald Stufftb42af172013-08-10 14:32:08 -040042
43 def test_encrypt_with_invalid_operation(self):
44 cipher = BlockCipher(
45 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040046 modes.CBC(binascii.unhexlify(b"0" * 32))
Donald Stufftb42af172013-08-10 14:32:08 -040047 )
Donald Stufftbac187d2013-08-10 15:43:51 -040048 cipher._operation = _Operation.decrypt
Donald Stufftb42af172013-08-10 14:32:08 -040049
50 with pytest.raises(ValueError):
51 cipher.encrypt(b"b" * 16)
52
53 def test_finalize_with_invalid_operation(self):
54 cipher = BlockCipher(
55 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Donald Stufftea8e9fc2013-08-10 15:22:28 -040056 modes.CBC(binascii.unhexlify(b"0" * 32))
Donald Stufftb42af172013-08-10 14:32:08 -040057 )
Donald Stufftbac187d2013-08-10 15:43:51 -040058 cipher._operation = pretend.stub(name="wat")
Donald Stufftb42af172013-08-10 14:32:08 -040059
60 with pytest.raises(ValueError):
61 cipher.encrypt(b"b" * 16)