blob: aebb4d4d3ed0891496c1ffb53f6713bbca1c8039 [file] [log] [blame]
Alex Gaynoraf82d5e2013-10-29 17:07:24 -07001.. hazmat::
Alex Gaynor5f3db272013-10-29 10:56:35 -07002
3Padding
4=======
5
Alex Gaynorb2d5efd2013-10-29 11:15:30 -07006.. currentmodule:: cryptography.hazmat.primitives.padding
Alex Gaynor5f3db272013-10-29 10:56:35 -07007
8Padding is a way to take data that may or may not be be a multiple of the block
9size for a cipher and extend it out so that it is. This is required for many
10block cipher modes as they require the data to be encrypted to be an exact
11multiple of the block size.
12
13
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070014.. class:: PKCS7(block_size)
Alex Gaynor5f3db272013-10-29 10:56:35 -070015
16 PKCS7 padding is a generalization of PKCS5 padding (also known as standard
17 padding). PKCS7 padding works by appending ``N`` bytes with the value of
18 ``chr(N)``, where ``N`` is the number of bytes required to make the final
19 block of data the same size as the block size. A simple example of padding
20 is:
21
22 .. doctest::
23
Alex Gaynor07082782013-10-29 11:18:23 -070024 >>> from cryptography.hazmat.primitives import padding
Alex Gaynor60ad3e12013-10-29 14:26:11 -070025 >>> padder = padding.PKCS7(128).padder()
Alex Gaynor22e2eae2013-10-29 11:42:14 -070026 >>> padder.update(b"1111111111")
27 ''
28 >>> padder.finalize()
Alex Gaynor5f3db272013-10-29 10:56:35 -070029 '1111111111\x06\x06\x06\x06\x06\x06'
30
31 :param block_size: The size of the block in bits that the data is being
32 padded to.
33
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070034 .. method:: padder()
35
36 :returns: A padding
37 :class:`~cryptography.hazmat.primitives.interfaces.PaddingContext`
38 provider.
39
40 .. method:: unpadder()
41
42 :returns: An unpadding
43 :class:`~cryptography.hazmat.primitives.interfaces.PaddingContext`
44 provider.
45
46
47.. currentmodule:: cryptography.hazmat.primitives.interfaces
48
49.. class:: PaddingContext
50
51 When calling ``padder()`` or ``unpadder()`` you will receive an a return
52 object conforming to the ``PaddingContext`` interface. You can then call
53 ``update(data)`` with data until you have fed everything into the context.
54 Once that is done call ``finalize()`` to finish the operation and obtain
55 the remainder of the data.
56
57 .. method:: update(data)
58
59 :param bytes data: The data you wish to pass into the context.
60 :return bytes: Returns the data that was padded or unpadded.
61
62 .. method:: finalize()
63
64 :return bytes: Returns the remainder of the data.