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 | |
Donald Stufft | bac187d | 2013-08-10 15:43:51 -0400 | [diff] [blame] | 18 | import pretend |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 19 | import pytest |
| 20 | |
Donald Stufft | ea8e9fc | 2013-08-10 15:22:28 -0400 | [diff] [blame] | 21 | from cryptography.primitives.block import BlockCipher, ciphers, modes |
Donald Stufft | bac187d | 2013-08-10 15:43:51 -0400 | [diff] [blame] | 22 | from cryptography.primitives.block.base import _Operation |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 23 | |
| 24 | |
| 25 | class TestBlockCipher(object): |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 26 | def test_cipher_name(self, api): |
Donald Stufft | adacdb8 | 2013-08-10 15:13:22 -0400 | [diff] [blame] | 27 | cipher = BlockCipher( |
| 28 | ciphers.AES(binascii.unhexlify(b"0" * 32)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 29 | modes.CBC(binascii.unhexlify(b"0" * 32)), |
| 30 | api |
Donald Stufft | adacdb8 | 2013-08-10 15:13:22 -0400 | [diff] [blame] | 31 | ) |
| 32 | assert cipher.name == "AES-128-CBC" |
| 33 | |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 34 | def test_use_after_finalize(self, api): |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 35 | cipher = BlockCipher( |
Alex Gaynor | 250903a | 2013-08-09 12:12:30 -0700 | [diff] [blame] | 36 | ciphers.AES(binascii.unhexlify(b"0" * 32)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 37 | modes.CBC(binascii.unhexlify(b"0" * 32)), |
| 38 | api |
Alex Gaynor | 95b8620 | 2013-08-08 19:36:44 -0700 | [diff] [blame] | 39 | ) |
Alex Gaynor | 250903a | 2013-08-09 12:12:30 -0700 | [diff] [blame] | 40 | cipher.encrypt(b"a" * 16) |
| 41 | cipher.finalize() |
| 42 | with pytest.raises(ValueError): |
| 43 | cipher.encrypt(b"b" * 16) |
| 44 | with pytest.raises(ValueError): |
| 45 | cipher.finalize() |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 46 | |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 47 | def test_encrypt_with_invalid_operation(self, api): |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 48 | cipher = BlockCipher( |
| 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)), |
| 51 | api |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 52 | ) |
Donald Stufft | bac187d | 2013-08-10 15:43:51 -0400 | [diff] [blame] | 53 | cipher._operation = _Operation.decrypt |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 54 | |
| 55 | with pytest.raises(ValueError): |
| 56 | cipher.encrypt(b"b" * 16) |
| 57 | |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 58 | def test_finalize_with_invalid_operation(self, api): |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 59 | cipher = BlockCipher( |
| 60 | ciphers.AES(binascii.unhexlify(b"0" * 32)), |
Alex Gaynor | 814efab | 2013-10-03 09:24:58 -0700 | [diff] [blame^] | 61 | modes.CBC(binascii.unhexlify(b"0" * 32)), |
| 62 | api |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 63 | ) |
Donald Stufft | bac187d | 2013-08-10 15:43:51 -0400 | [diff] [blame] | 64 | cipher._operation = pretend.stub(name="wat") |
Donald Stufft | b42af17 | 2013-08-10 14:32:08 -0400 | [diff] [blame] | 65 | |
| 66 | with pytest.raises(ValueError): |
Alex Gaynor | 497576c | 2013-08-10 16:03:42 -0400 | [diff] [blame] | 67 | cipher.finalize() |