1# This file is dual licensed under the terms of the Apache License, Version 2# 2.0, and the BSD License. See the LICENSE file in the root of this repository 3# for complete details. 4 5from __future__ import absolute_import, division, print_function 6 7import pytest 8 9import six 10 11from cryptography.exceptions import AlreadyFinalized 12from cryptography.hazmat.primitives import padding 13 14 15class TestPKCS7(object): 16 @pytest.mark.parametrize("size", [127, 4096, -2]) 17 def test_invalid_block_size(self, size): 18 with pytest.raises(ValueError): 19 padding.PKCS7(size) 20 21 @pytest.mark.parametrize(("size", "padded"), [ 22 (128, b"1111"), 23 (128, b"1111111111111111"), 24 (128, b"111111111111111\x06"), 25 (128, b""), 26 (128, b"\x06" * 6), 27 (128, b"\x00" * 16), 28 ]) 29 def test_invalid_padding(self, size, padded): 30 unpadder = padding.PKCS7(size).unpadder() 31 with pytest.raises(ValueError): 32 unpadder.update(padded) 33 unpadder.finalize() 34 35 def test_non_bytes(self): 36 padder = padding.PKCS7(128).padder() 37 with pytest.raises(TypeError): 38 padder.update(u"abc") 39 unpadder = padding.PKCS7(128).unpadder() 40 with pytest.raises(TypeError): 41 unpadder.update(u"abc") 42 43 @pytest.mark.parametrize(("size", "unpadded", "padded"), [ 44 ( 45 128, 46 b"1111111111", 47 b"1111111111\x06\x06\x06\x06\x06\x06", 48 ), 49 ( 50 128, 51 b"111111111111111122222222222222", 52 b"111111111111111122222222222222\x02\x02", 53 ), 54 ( 55 128, 56 b"1" * 16, 57 b"1" * 16 + b"\x10" * 16, 58 ), 59 ( 60 128, 61 b"1" * 17, 62 b"1" * 17 + b"\x0F" * 15, 63 ) 64 ]) 65 def test_pad(self, size, unpadded, padded): 66 padder = padding.PKCS7(size).padder() 67 result = padder.update(unpadded) 68 result += padder.finalize() 69 assert result == padded 70 71 @pytest.mark.parametrize(("size", "unpadded", "padded"), [ 72 ( 73 128, 74 b"1111111111", 75 b"1111111111\x06\x06\x06\x06\x06\x06", 76 ), 77 ( 78 128, 79 b"111111111111111122222222222222", 80 b"111111111111111122222222222222\x02\x02", 81 ), 82 ]) 83 def test_unpad(self, size, unpadded, padded): 84 unpadder = padding.PKCS7(size).unpadder() 85 result = unpadder.update(padded) 86 result += unpadder.finalize() 87 assert result == unpadded 88 89 def test_use_after_finalize(self): 90 padder = padding.PKCS7(128).padder() 91 b = padder.finalize() 92 with pytest.raises(AlreadyFinalized): 93 padder.update(b"") 94 with pytest.raises(AlreadyFinalized): 95 padder.finalize() 96 97 unpadder = padding.PKCS7(128).unpadder() 98 unpadder.update(b) 99 unpadder.finalize() 100 with pytest.raises(AlreadyFinalized): 101 unpadder.update(b"") 102 with pytest.raises(AlreadyFinalized): 103 unpadder.finalize() 104 105 def test_large_padding(self): 106 padder = padding.PKCS7(2040).padder() 107 padded_data = padder.update(b"") 108 padded_data += padder.finalize() 109 110 for i in six.iterbytes(padded_data): 111 assert i == 255 112 113 unpadder = padding.PKCS7(2040).unpadder() 114 data = unpadder.update(padded_data) 115 data += unpadder.finalize() 116 117 assert data == b"" 118 119 120class TestANSIX923(object): 121 @pytest.mark.parametrize("size", [127, 4096, -2]) 122 def test_invalid_block_size(self, size): 123 with pytest.raises(ValueError): 124 padding.ANSIX923(size) 125 126 @pytest.mark.parametrize(("size", "padded"), [ 127 (128, b"1111"), 128 (128, b"1111111111111111"), 129 (128, b"111111111111111\x06"), 130 (128, b"1111111111\x06\x06\x06\x06\x06\x06"), 131 (128, b""), 132 (128, b"\x06" * 6), 133 (128, b"\x00" * 16), 134 ]) 135 def test_invalid_padding(self, size, padded): 136 unpadder = padding.ANSIX923(size).unpadder() 137 with pytest.raises(ValueError): 138 unpadder.update(padded) 139 unpadder.finalize() 140 141 def test_non_bytes(self): 142 padder = padding.ANSIX923(128).padder() 143 with pytest.raises(TypeError): 144 padder.update(u"abc") 145 unpadder = padding.ANSIX923(128).unpadder() 146 with pytest.raises(TypeError): 147 unpadder.update(u"abc") 148 149 @pytest.mark.parametrize(("size", "unpadded", "padded"), [ 150 ( 151 128, 152 b"1111111111", 153 b"1111111111\x00\x00\x00\x00\x00\x06", 154 ), 155 ( 156 128, 157 b"111111111111111122222222222222", 158 b"111111111111111122222222222222\x00\x02", 159 ), 160 ( 161 128, 162 b"1" * 16, 163 b"1" * 16 + b"\x00" * 15 + b"\x10", 164 ), 165 ( 166 128, 167 b"1" * 17, 168 b"1" * 17 + b"\x00" * 14 + b"\x0F", 169 ) 170 ]) 171 def test_pad(self, size, unpadded, padded): 172 padder = padding.ANSIX923(size).padder() 173 result = padder.update(unpadded) 174 result += padder.finalize() 175 assert result == padded 176 177 @pytest.mark.parametrize(("size", "unpadded", "padded"), [ 178 ( 179 128, 180 b"1111111111", 181 b"1111111111\x00\x00\x00\x00\x00\x06", 182 ), 183 ( 184 128, 185 b"111111111111111122222222222222", 186 b"111111111111111122222222222222\x00\x02", 187 ), 188 ]) 189 def test_unpad(self, size, unpadded, padded): 190 unpadder = padding.ANSIX923(size).unpadder() 191 result = unpadder.update(padded) 192 result += unpadder.finalize() 193 assert result == unpadded 194 195 def test_use_after_finalize(self): 196 padder = padding.ANSIX923(128).padder() 197 b = padder.finalize() 198 with pytest.raises(AlreadyFinalized): 199 padder.update(b"") 200 with pytest.raises(AlreadyFinalized): 201 padder.finalize() 202 203 unpadder = padding.ANSIX923(128).unpadder() 204 unpadder.update(b) 205 unpadder.finalize() 206 with pytest.raises(AlreadyFinalized): 207 unpadder.update(b"") 208 with pytest.raises(AlreadyFinalized): 209 unpadder.finalize() 210