blob: 4a67002f6f03411d3d66d080f8cd0e2da31799f9 [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 Kehrer620c2ae2013-10-19 14:12:04 -050052 context = cipher.encryptor()
53 context.update(b"a" * 16)
54 context.finalize()
Alex Gaynor250903a2013-08-09 12:12:30 -070055 with pytest.raises(ValueError):
Paul Kehrer620c2ae2013-10-19 14:12:04 -050056 context.update(b"b" * 16)
Alex Gaynor250903a2013-08-09 12:12:30 -070057 with pytest.raises(ValueError):
Paul Kehrer620c2ae2013-10-19 14:12:04 -050058 context.finalize()
59 context = cipher.decryptor()
60 context.update(b"a" * 16)
61 context.finalize()
Donald Stufftb42af172013-08-10 14:32:08 -040062 with pytest.raises(ValueError):
Paul Kehrer620c2ae2013-10-19 14:12:04 -050063 context.update(b"b" * 16)
Donald Stufftb42af172013-08-10 14:32:08 -040064 with pytest.raises(ValueError):
Paul Kehrer620c2ae2013-10-19 14:12:04 -050065 context.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 Kehrer620c2ae2013-10-19 14:12:04 -050073 context = cipher.encryptor()
74 ct = context.update(b"a" * 15)
Paul Kehrerc3a2b422013-10-19 16:10:25 -050075 assert ct == b""
Paul Kehrer620c2ae2013-10-19 14:12:04 -050076 ct += context.update(b"a" * 65)
Paul Kehrerc3a2b422013-10-19 16:10:25 -050077 assert len(ct) == 80
Paul Kehrer620c2ae2013-10-19 14:12:04 -050078 ct += context.finalize()
79 context = cipher.decryptor()
80 pt = context.update(ct[:3])
81 assert pt == b""
82 pt += context.update(ct[3:])
83 assert len(pt) == 80
84 context.finalize()