blob: 7cbadeb91fae4ff50678d50c09ac58dba93aa7d5 [file] [log] [blame]
Alex Gaynor5f3db272013-10-29 10:56:35 -07001.. danger::
2
3 This is a "Hazardous Materials" module. You should **ONLY** use it if
4 you're 100% absolutely sure that you know what you're doing because this
5 module is full of land mines, dragons, and dinosaurs with laser guns.
6
7
8Padding
9=======
10
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070011.. currentmodule:: cryptography.hazmat.primitives.padding
Alex Gaynor5f3db272013-10-29 10:56:35 -070012
13Padding is a way to take data that may or may not be be a multiple of the block
14size for a cipher and extend it out so that it is. This is required for many
15block cipher modes as they require the data to be encrypted to be an exact
16multiple of the block size.
17
18
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070019.. class:: PKCS7(block_size)
Alex Gaynor5f3db272013-10-29 10:56:35 -070020
21 PKCS7 padding is a generalization of PKCS5 padding (also known as standard
22 padding). PKCS7 padding works by appending ``N`` bytes with the value of
23 ``chr(N)``, where ``N`` is the number of bytes required to make the final
24 block of data the same size as the block size. A simple example of padding
25 is:
26
27 .. doctest::
28
Alex Gaynor07082782013-10-29 11:18:23 -070029 >>> from cryptography.hazmat.primitives import padding
Alex Gaynor5f3db272013-10-29 10:56:35 -070030 >>> padder = padding.PKCS7(128)
31 >>> padder.pad(b"1111111111")
32 '1111111111\x06\x06\x06\x06\x06\x06'
33
34 :param block_size: The size of the block in bits that the data is being
35 padded to.
36
37 .. method:: pad(data)
38
39 :param data: The data that should be padded.
40 :rtype bytes: The padded data.
41
42 .. method:: unpad(data)
43
44 :param data: The data that should be unpadded.
45 :rtype bytes: The unpadded data.
Alex Gaynorb2d5efd2013-10-29 11:15:30 -070046
47 .. method:: padder()
48
49 :returns: A padding
50 :class:`~cryptography.hazmat.primitives.interfaces.PaddingContext`
51 provider.
52
53 .. method:: unpadder()
54
55 :returns: An unpadding
56 :class:`~cryptography.hazmat.primitives.interfaces.PaddingContext`
57 provider.
58
59
60.. currentmodule:: cryptography.hazmat.primitives.interfaces
61
62.. class:: PaddingContext
63
64 When calling ``padder()`` or ``unpadder()`` you will receive an a return
65 object conforming to the ``PaddingContext`` interface. You can then call
66 ``update(data)`` with data until you have fed everything into the context.
67 Once that is done call ``finalize()`` to finish the operation and obtain
68 the remainder of the data.
69
70 .. method:: update(data)
71
72 :param bytes data: The data you wish to pass into the context.
73 :return bytes: Returns the data that was padded or unpadded.
74
75 .. method:: finalize()
76
77 :return bytes: Returns the remainder of the data.