blob: 7dccda4b5e245083c1c2b780d54ac6ec451c1dfd [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):
Alex Gaynor250903a2013-08-09 12:12:30 -070022 def test_use_after_finalize(self):
Alex Gaynor95b86202013-08-08 19:36:44 -070023 cipher = BlockCipher(
Alex Gaynor250903a2013-08-09 12:12:30 -070024 ciphers.AES(binascii.unhexlify(b"0" * 32)),
25 modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding())
Alex Gaynor95b86202013-08-08 19:36:44 -070026 )
Alex Gaynor250903a2013-08-09 12:12:30 -070027 cipher.encrypt(b"a" * 16)
28 cipher.finalize()
29 with pytest.raises(ValueError):
30 cipher.encrypt(b"b" * 16)
31 with pytest.raises(ValueError):
32 cipher.finalize()
Donald Stufftb42af172013-08-10 14:32:08 -040033
34 def test_encrypt_with_invalid_operation(self):
35 cipher = BlockCipher(
36 ciphers.AES(binascii.unhexlify(b"0" * 32)),
37 modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding())
38 )
39 cipher._operation = "decrypt"
40
41 with pytest.raises(ValueError):
42 cipher.encrypt(b"b" * 16)
43
44 def test_finalize_with_invalid_operation(self):
45 cipher = BlockCipher(
46 ciphers.AES(binascii.unhexlify(b"0" * 32)),
47 modes.CBC(binascii.unhexlify(b"0" * 32), padding.NoPadding())
48 )
49 cipher._operation = "wat"
50
51 with pytest.raises(ValueError):
52 cipher.encrypt(b"b" * 16)