blob: 0322f9d2aa89560a1cb82a2952174bc1842bccea [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
Alex Gaynor462bd602014-04-25 07:49:08 -07008Padding is a way to take data that may or may not be a multiple of the block
Alex Gaynor5f3db272013-10-29 10:56:35 -07009size 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()
David Reiddb616be2014-02-12 10:51:56 -080026 >>> padded_data = padder.update(b"11111111111111112222222222")
Alex Gaynorbae899a2013-11-22 16:54:55 -080027 >>> padded_data
David Reiddb616be2014-02-12 10:51:56 -080028 '1111111111111111'
David Reid50309fb2014-02-12 11:16:27 -080029 >>> padded_data += padder.finalize()
30 >>> padded_data
David Reiddb616be2014-02-12 10:51:56 -080031 '11111111111111112222222222\x06\x06\x06\x06\x06\x06'
Alex Gaynorbae899a2013-11-22 16:54:55 -080032 >>> unpadder = padding.PKCS7(128).unpadder()
David Reiddb616be2014-02-12 10:51:56 -080033 >>> data = unpadder.update(padded_data)
34 >>> data
35 '1111111111111111'
36 >>> data + unpadder.finalize()
37 '11111111111111112222222222'
Alex Gaynor5f3db272013-10-29 10:56:35 -070038
39 :param block_size: The size of the block in bits that the data is being
40 padded to.
Paul Kehrerc6f19562014-04-27 09:54:11 -050041 :raises ValueError: Raised if block size is not a multiple of 8 or is not
42 between 0 and 256.
Alex Gaynor5f3db272013-10-29 10:56:35 -070043
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070044 .. method:: padder()
45
46 :returns: A padding
47 :class:`~cryptography.hazmat.primitives.interfaces.PaddingContext`
48 provider.
49
50 .. method:: unpadder()
51
52 :returns: An unpadding
53 :class:`~cryptography.hazmat.primitives.interfaces.PaddingContext`
54 provider.
55
56
57.. currentmodule:: cryptography.hazmat.primitives.interfaces
58
59.. class:: PaddingContext
60
Alex Gaynorb4818892014-02-06 10:58:50 -080061 When calling ``padder()`` or ``unpadder()`` the result will conform to the
62 ``PaddingContext`` interface. You can then call ``update(data)`` with data
63 until you have fed everything into the context. Once that is done call
64 ``finalize()`` to finish the operation and obtain the remainder of the
65 data.
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070066
67 .. method:: update(data)
68
69 :param bytes data: The data you wish to pass into the context.
70 :return bytes: Returns the data that was padded or unpadded.
Paul Kehrerc6f19562014-04-27 09:54:11 -050071 :raises TypeError: Raised if data is not bytes.
Alex Stapleton425fc042014-04-25 22:32:01 +010072 :raises cryptography.exceptions.AlreadyFinalized: See :meth:`finalize`.
Ayrx00eff9c2014-05-17 19:47:09 +080073 :raises TypeError: This exception is raised if ``data`` is not ``bytes``.
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070074
75 .. method:: finalize()
76
Alex Stapleton425fc042014-04-25 22:32:01 +010077 Finalize the current context and return the rest of the data.
78
79 After ``finalize`` has been called this object can no longer be used;
80 :meth:`update` and :meth:`finalize` will raise an
81 :class:`~cryptography.exceptions.AlreadyFinalized` exception.
82
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070083 :return bytes: Returns the remainder of the data.
Paul Kehrerc6f19562014-04-27 09:54:11 -050084 :raises TypeError: Raised if data is not bytes.
Alex Stapleton425fc042014-04-25 22:32:01 +010085 :raises ValueError: When trying to remove padding from incorrectly
86 padded data.