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