Alex Gaynor | a2e1f54 | 2013-08-10 08:59:11 -0400 | [diff] [blame] | 1 | # 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 Schlawack | 425f584 | 2013-08-11 09:54:59 +0200 | [diff] [blame] | 14 | from __future__ import absolute_import, division, print_function |
| 15 | |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 16 | import binascii |
| 17 | |
| 18 | import pytest |
| 19 | |
Paul Kehrer | b2c94fd | 2013-10-22 12:45:07 -0500 | [diff] [blame] | 20 | from cryptography.primitives import interfaces |
Donald Stufft | ea8e9fc | 2013-08-10 15:22:28 -0400 | [diff] [blame] | 21 | from cryptography.primitives.block import BlockCipher, ciphers, modes |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class TestBlockCipher(object): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame^] | 25 | def test_instantiate_without_backend(self): |
Alex Gaynor | 3bdc6b6 | 2013-10-17 18:53:12 -0700 | [diff] [blame] | 26 | BlockCipher( |
Alex Gaynor | 81a5287 | 2013-10-03 09:58:45 -0700 | [diff] [blame] | 27 | ciphers.AES(binascii.unhexlify(b"0" * 32)), |
| 28 | modes.CBC(binascii.unhexlify(b"0" * 32)) |
| 29 | ) |
Alex Gaynor | 81a5287 | 2013-10-03 09:58:45 -0700 | [diff] [blame] | 30 | |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 31 | 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 Kehrer | b2c94fd | 2013-10-22 12:45:07 -0500 | [diff] [blame] | 36 | assert isinstance(cipher.encryptor(), interfaces.CipherContext) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 37 | |
| 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 Kehrer | b2c94fd | 2013-10-22 12:45:07 -0500 | [diff] [blame] | 43 | assert isinstance(cipher.decryptor(), interfaces.CipherContext) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 44 | |
| 45 | |
| 46 | class TestBlockCipherContext(object): |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame^] | 47 | def test_use_after_finalize(self, backend): |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 48 | cipher = BlockCipher( |
Alex Gaynor | 250903a | 2013-08-09 12:12:30 -0700 | [diff] [blame] | 49 | ciphers.AES(binascii.unhexlify(b"0" * 32)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame] | 50 | modes.CBC(binascii.unhexlify(b"0" * 32)), |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame^] | 51 | backend |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 52 | ) |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 53 | encryptor = cipher.encryptor() |
| 54 | encryptor.update(b"a" * 16) |
| 55 | encryptor.finalize() |
Alex Gaynor | 250903a | 2013-08-09 12:12:30 -0700 | [diff] [blame] | 56 | with pytest.raises(ValueError): |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 57 | encryptor.update(b"b" * 16) |
Alex Gaynor | 250903a | 2013-08-09 12:12:30 -0700 | [diff] [blame] | 58 | with pytest.raises(ValueError): |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 59 | encryptor.finalize() |
| 60 | decryptor = cipher.decryptor() |
| 61 | decryptor.update(b"a" * 16) |
| 62 | decryptor.finalize() |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 63 | with pytest.raises(ValueError): |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 64 | decryptor.update(b"b" * 16) |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 65 | with pytest.raises(ValueError): |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 66 | decryptor.finalize() |
Paul Kehrer | c3a2b42 | 2013-10-19 16:10:25 -0500 | [diff] [blame] | 67 | |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame^] | 68 | def test_unaligned_block_encryption(self, backend): |
Paul Kehrer | c3a2b42 | 2013-10-19 16:10:25 -0500 | [diff] [blame] | 69 | cipher = BlockCipher( |
| 70 | ciphers.AES(binascii.unhexlify(b"0" * 32)), |
| 71 | modes.ECB(), |
Paul Kehrer | db37d0e | 2013-10-22 20:13:06 -0500 | [diff] [blame^] | 72 | backend |
Paul Kehrer | c3a2b42 | 2013-10-19 16:10:25 -0500 | [diff] [blame] | 73 | ) |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 74 | encryptor = cipher.encryptor() |
| 75 | ct = encryptor.update(b"a" * 15) |
Paul Kehrer | c3a2b42 | 2013-10-19 16:10:25 -0500 | [diff] [blame] | 76 | assert ct == b"" |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 77 | ct += encryptor.update(b"a" * 65) |
Paul Kehrer | c3a2b42 | 2013-10-19 16:10:25 -0500 | [diff] [blame] | 78 | assert len(ct) == 80 |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 79 | ct += encryptor.finalize() |
| 80 | decryptor = cipher.decryptor() |
| 81 | pt = decryptor.update(ct[:3]) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 82 | assert pt == b"" |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 83 | pt += decryptor.update(ct[3:]) |
Paul Kehrer | 620c2ae | 2013-10-19 14:12:04 -0500 | [diff] [blame] | 84 | assert len(pt) == 80 |
Paul Kehrer | 653463f | 2013-10-21 17:55:01 -0500 | [diff] [blame] | 85 | assert pt == b"a" * 80 |
| 86 | decryptor.finalize() |