blob: 774885faf5284c9ecf5eb70767de7544b3cf0971 [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
Donald Stufftbac187d2013-08-10 15:43:51 -040018import pretend
Alex Gaynor95b86202013-08-08 19:36:44 -070019import pytest
20
Donald Stufftea8e9fc2013-08-10 15:22:28 -040021from cryptography.primitives.block import BlockCipher, ciphers, modes
Donald Stufftbac187d2013-08-10 15:43:51 -040022from cryptography.primitives.block.base import _Operation
Alex Gaynor95b86202013-08-08 19:36:44 -070023
24
25class TestBlockCipher(object):
Alex Gaynor814efab2013-10-03 09:24:58 -070026 def test_cipher_name(self, api):
Donald Stufftadacdb82013-08-10 15:13:22 -040027 cipher = BlockCipher(
28 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Alex Gaynor814efab2013-10-03 09:24:58 -070029 modes.CBC(binascii.unhexlify(b"0" * 32)),
30 api
Donald Stufftadacdb82013-08-10 15:13:22 -040031 )
32 assert cipher.name == "AES-128-CBC"
33
Alex Gaynor81a52872013-10-03 09:58:45 -070034 def test_instantiate_without_api(self):
35 cipher = BlockCipher(
36 ciphers.AES(binascii.unhexlify(b"0" * 32)),
37 modes.CBC(binascii.unhexlify(b"0" * 32))
38 )
39 assert cipher.name == "AES-128-CBC"
40
Alex Gaynor814efab2013-10-03 09:24:58 -070041 def test_use_after_finalize(self, api):
Alex Gaynor95b86202013-08-08 19:36:44 -070042 cipher = BlockCipher(
Alex Gaynor250903a2013-08-09 12:12:30 -070043 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Alex Gaynor814efab2013-10-03 09:24:58 -070044 modes.CBC(binascii.unhexlify(b"0" * 32)),
45 api
Alex Gaynor95b86202013-08-08 19:36:44 -070046 )
Alex Gaynor250903a2013-08-09 12:12:30 -070047 cipher.encrypt(b"a" * 16)
48 cipher.finalize()
49 with pytest.raises(ValueError):
50 cipher.encrypt(b"b" * 16)
51 with pytest.raises(ValueError):
52 cipher.finalize()
Donald Stufftb42af172013-08-10 14:32:08 -040053
Alex Gaynor814efab2013-10-03 09:24:58 -070054 def test_encrypt_with_invalid_operation(self, api):
Donald Stufftb42af172013-08-10 14:32:08 -040055 cipher = BlockCipher(
56 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Alex Gaynor814efab2013-10-03 09:24:58 -070057 modes.CBC(binascii.unhexlify(b"0" * 32)),
58 api
Donald Stufftb42af172013-08-10 14:32:08 -040059 )
Donald Stufftbac187d2013-08-10 15:43:51 -040060 cipher._operation = _Operation.decrypt
Donald Stufftb42af172013-08-10 14:32:08 -040061
62 with pytest.raises(ValueError):
63 cipher.encrypt(b"b" * 16)
64
Alex Gaynor814efab2013-10-03 09:24:58 -070065 def test_finalize_with_invalid_operation(self, api):
Donald Stufftb42af172013-08-10 14:32:08 -040066 cipher = BlockCipher(
67 ciphers.AES(binascii.unhexlify(b"0" * 32)),
Alex Gaynor814efab2013-10-03 09:24:58 -070068 modes.CBC(binascii.unhexlify(b"0" * 32)),
69 api
Donald Stufftb42af172013-08-10 14:32:08 -040070 )
Donald Stufftbac187d2013-08-10 15:43:51 -040071 cipher._operation = pretend.stub(name="wat")
Donald Stufftb42af172013-08-10 14:32:08 -040072
73 with pytest.raises(ValueError):
Alex Gaynor497576c2013-08-10 16:03:42 -040074 cipher.finalize()