blob: 5e147a790916936ab054fdeb98d0d96d29707a77 [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
Paul Kehrerb2c94fd2013-10-22 12:45:07 -050020from cryptography.primitives import interfaces
Donald Stufftea8e9fc2013-08-10 15:22:28 -040021from cryptography.primitives.block import BlockCipher, ciphers, modes
Alex Gaynor95b86202013-08-08 19:36:44 -070022
23
24class TestBlockCipher(object):
Alex Gaynor81a52872013-10-03 09:58:45 -070025 def test_instantiate_without_api(self):
Alex Gaynor3bdc6b62013-10-17 18:53:12 -070026 BlockCipher(
Alex Gaynor81a52872013-10-03 09:58:45 -070027 ciphers.AES(binascii.unhexlify(b"0" * 32)),
28 modes.CBC(binascii.unhexlify(b"0" * 32))
29 )
Alex Gaynor81a52872013-10-03 09:58:45 -070030
Paul Kehrer620c2ae2013-10-19 14:12:04 -050031 def test_creates_encryptor(self):
32 cipher = BlockCipher(
33 ciphers.AES(binascii.unhexlify(b"0" * 32)),
34 modes.CBC(binascii.unhexlify(b"0" * 32))
35 )
Paul Kehrerb2c94fd2013-10-22 12:45:07 -050036 assert isinstance(cipher.encryptor(), interfaces.CipherContext)
Paul Kehrer620c2ae2013-10-19 14:12:04 -050037
38 def test_creates_decryptor(self):
39 cipher = BlockCipher(
40 ciphers.AES(binascii.unhexlify(b"0" * 32)),
41 modes.CBC(binascii.unhexlify(b"0" * 32))
42 )
Paul Kehrerb2c94fd2013-10-22 12:45:07 -050043 assert isinstance(cipher.decryptor(), interfaces.CipherContext)
Paul Kehrer620c2ae2013-10-19 14:12:04 -050044
45
46class TestBlockCipherContext(object):
Alex Gaynor814efab2013-10-03 09:24:58 -070047 def test_use_after_finalize(self, api):
Alex Gaynor95b86202013-08-08 19:36:44 -070048 cipher = BlockCipher(
Alex Gaynor250903a2013-08-09 12:12:30 -070049 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Alex Gaynor814efab2013-10-03 09:24:58 -070050 modes.CBC(binascii.unhexlify(b"0" * 32)),
51 api
Alex Gaynor95b86202013-08-08 19:36:44 -070052 )
Paul Kehrer653463f2013-10-21 17:55:01 -050053 encryptor = cipher.encryptor()
54 encryptor.update(b"a" * 16)
55 encryptor.finalize()
Alex Gaynor250903a2013-08-09 12:12:30 -070056 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050057 encryptor.update(b"b" * 16)
Alex Gaynor250903a2013-08-09 12:12:30 -070058 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050059 encryptor.finalize()
60 decryptor = cipher.decryptor()
61 decryptor.update(b"a" * 16)
62 decryptor.finalize()
Donald Stufftb42af172013-08-10 14:32:08 -040063 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050064 decryptor.update(b"b" * 16)
Donald Stufftb42af172013-08-10 14:32:08 -040065 with pytest.raises(ValueError):
Paul Kehrer653463f2013-10-21 17:55:01 -050066 decryptor.finalize()
Paul Kehrerc3a2b422013-10-19 16:10:25 -050067
68 def test_unaligned_block_encryption(self, api):
69 cipher = BlockCipher(
70 ciphers.AES(binascii.unhexlify(b"0" * 32)),
71 modes.ECB(),
72 api
73 )
Paul Kehrer653463f2013-10-21 17:55:01 -050074 encryptor = cipher.encryptor()
75 ct = encryptor.update(b"a" * 15)
Paul Kehrerc3a2b422013-10-19 16:10:25 -050076 assert ct == b""
Paul Kehrer653463f2013-10-21 17:55:01 -050077 ct += encryptor.update(b"a" * 65)
Paul Kehrerc3a2b422013-10-19 16:10:25 -050078 assert len(ct) == 80
Paul Kehrer653463f2013-10-21 17:55:01 -050079 ct += encryptor.finalize()
80 decryptor = cipher.decryptor()
81 pt = decryptor.update(ct[:3])
Paul Kehrer620c2ae2013-10-19 14:12:04 -050082 assert pt == b""
Paul Kehrer653463f2013-10-21 17:55:01 -050083 pt += decryptor.update(ct[3:])
Paul Kehrer620c2ae2013-10-19 14:12:04 -050084 assert len(pt) == 80
Paul Kehrer653463f2013-10-21 17:55:01 -050085 assert pt == b"a" * 80
86 decryptor.finalize()