blob: aa670be390b2d232e61c693cf91209a8c19144ed [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
16import pytest
17
18from cryptography.primitives.block import BlockCipher, ciphers, modes, padding
19
20
21class TestBlockCipher(object):
Donald Stufftadacdb82013-08-10 15:13:22 -040022 def test_cipher_name(self):
23 cipher = BlockCipher(
24 ciphers.AES(binascii.unhexlify(b"0" * 32)),
25 modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding())
26 )
27 assert cipher.name == "AES-128-CBC"
28
Alex Gaynor250903a2013-08-09 12:12:30 -070029 def test_use_after_finalize(self):
Alex Gaynor95b86202013-08-08 19:36:44 -070030 cipher = BlockCipher(
Alex Gaynor250903a2013-08-09 12:12:30 -070031 ciphers.AES(binascii.unhexlify(b"0" * 32)),
32 modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding())
Alex Gaynor95b86202013-08-08 19:36:44 -070033 )
Alex Gaynor250903a2013-08-09 12:12:30 -070034 cipher.encrypt(b"a" * 16)
35 cipher.finalize()
36 with pytest.raises(ValueError):
37 cipher.encrypt(b"b" * 16)
38 with pytest.raises(ValueError):
39 cipher.finalize()
Donald Stufftb42af172013-08-10 14:32:08 -040040
41 def test_encrypt_with_invalid_operation(self):
42 cipher = BlockCipher(
43 ciphers.AES(binascii.unhexlify(b"0" * 32)),
44 modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding())
45 )
46 cipher._operation = "decrypt"
47
48 with pytest.raises(ValueError):
49 cipher.encrypt(b"b" * 16)
50
51 def test_finalize_with_invalid_operation(self):
52 cipher = BlockCipher(
53 ciphers.AES(binascii.unhexlify(b"0" * 32)),
54 modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding())
55 )
56 cipher._operation = "wat"
57
58 with pytest.raises(ValueError):
59 cipher.encrypt(b"b" * 16)