blob: 8e42908566f8f6a7152075b27a3af3aecb9dab5e [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
Hynek Schlawack425f5842013-08-11 09:54:59 +020014from __future__ import absolute_import, division, print_function
15
Alex Gaynor95b86202013-08-08 19:36:44 -070016import binascii
17
18import pytest
19
Donald Stufftea8e9fc2013-08-10 15:22:28 -040020from cryptography.primitives.block import BlockCipher, ciphers, modes
Alex Gaynor95b86202013-08-08 19:36:44 -070021
22
23class TestBlockCipher(object):
Alex Gaynor81a52872013-10-03 09:58:45 -070024 def test_instantiate_without_api(self):
Alex Gaynor3bdc6b62013-10-17 18:53:12 -070025 BlockCipher(
Alex Gaynor81a52872013-10-03 09:58:45 -070026 ciphers.AES(binascii.unhexlify(b"0" * 32)),
27 modes.CBC(binascii.unhexlify(b"0" * 32))
28 )
Alex Gaynor81a52872013-10-03 09:58:45 -070029
Paul Kehrer620c2ae2013-10-19 14:12:04 -050030 def test_creates_encryptor(self):
31 cipher = BlockCipher(
32 ciphers.AES(binascii.unhexlify(b"0" * 32)),
33 modes.CBC(binascii.unhexlify(b"0" * 32))
34 )
35 assert cipher.encryptor() is not None
36
37 def test_creates_decryptor(self):
38 cipher = BlockCipher(
39 ciphers.AES(binascii.unhexlify(b"0" * 32)),
40 modes.CBC(binascii.unhexlify(b"0" * 32))
41 )
42 assert cipher.decryptor() is not None
43
44
45class TestBlockCipherContext(object):
Alex Gaynor814efab2013-10-03 09:24:58 -070046 def test_use_after_finalize(self, api):
Alex Gaynor95b86202013-08-08 19:36:44 -070047 cipher = BlockCipher(
Alex Gaynor250903a2013-08-09 12:12:30 -070048 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Alex Gaynor814efab2013-10-03 09:24:58 -070049 modes.CBC(binascii.unhexlify(b"0" * 32)),
50 api
Alex Gaynor95b86202013-08-08 19:36:44 -070051 )
Paul Kehrer653463f2013-10-21 17:55:01 -050052 encryptor = cipher.encryptor()
53 encryptor.update(b"a" * 16)
54 encryptor.finalize()
Alex Gaynor250903a2013-08-09 12:12:30 -070055 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050056 encryptor.update(b"b" * 16)
Alex Gaynor250903a2013-08-09 12:12:30 -070057 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050058 encryptor.finalize()
59 decryptor = cipher.decryptor()
60 decryptor.update(b"a" * 16)
61 decryptor.finalize()
Donald Stufftb42af172013-08-10 14:32:08 -040062 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050063 decryptor.update(b"b" * 16)
Donald Stufftb42af172013-08-10 14:32:08 -040064 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050065 decryptor.finalize()
Paul Kehrerc3a2b422013-10-19 16:10:25 -050066
67 def test_unaligned_block_encryption(self, api):
68 cipher = BlockCipher(
69 ciphers.AES(binascii.unhexlify(b"0" * 32)),
70 modes.ECB(),
71 api
72 )
Paul Kehrer653463f2013-10-21 17:55:01 -050073 encryptor = cipher.encryptor()
74 ct = encryptor.update(b"a" * 15)
Paul Kehrerc3a2b422013-10-19 16:10:25 -050075 assert ct == b""
Paul Kehrer653463f2013-10-21 17:55:01 -050076 ct += encryptor.update(b"a" * 65)
Paul Kehrerc3a2b422013-10-19 16:10:25 -050077 assert len(ct) == 80
Paul Kehrer653463f2013-10-21 17:55:01 -050078 ct += encryptor.finalize()
79 decryptor = cipher.decryptor()
80 pt = decryptor.update(ct[:3])
Paul Kehrer620c2ae2013-10-19 14:12:04 -050081 assert pt == b""
Paul Kehrer653463f2013-10-21 17:55:01 -050082 pt += decryptor.update(ct[3:])
Paul Kehrer620c2ae2013-10-19 14:12:04 -050083 assert len(pt) == 80
Paul Kehrer653463f2013-10-21 17:55:01 -050084 assert pt == b"a" * 80
85 decryptor.finalize()