Debian Codegraph @

Doc/src/util/util.rst

raw
1 :mod:`Crypto.Util` package
2 ==========================
3
4 Useful modules that don't belong in any other package.
5
6 .. toctree::
7 :hidden:
8
9 asn1
10
11 :mod:`Crypto.Util.Padding` module
12 ---------------------------------
13
14 This module provides minimal support for adding and removing standard padding
15 from data. Example::
16
17 >>> from Crypto.Util.Padding import pad, unpad
18 >>> from Crypto.Cipher import AES
19 >>> from Crypto.Random import get_random_bytes
20 >>>
21 >>> data = b'Unaligned' # 9 bytes
22 >>> key = get_random_bytes(32)
23 >>> iv = get_random_bytes(16)
24 >>>
25 >>> cipher1 = AES.new(key, AES.MODE_CBC, iv)
26 >>> ct = cipher1.encrypt(pad(data, 16))
27 >>>
28 >>> cipher2 = AES.new(key, AES.MODE_CBC, iv)
29 >>> pt = unpad(cipher2.decrypt(ct), 16)
30 >>> assert(data == pt)
31
32 .. automodule:: Crypto.Util.Padding
33 :members:
34
35 :mod:`Crypto.Util.RFC1751` module
36 ---------------------------------
37
38 .. automodule:: Crypto.Util.RFC1751
39 :members:
40
41 :mod:`Crypto.Util.strxor` module
42 --------------------------------
43
44 Fast XOR for byte strings.
45
46 .. automodule:: Crypto.Util.strxor
47 :members:
48
49 :mod:`Crypto.Util.Counter` module
50 ---------------------------------
51
52 The :mod:`Crypto.Util.Counter` module provides the functionality
53 to create more complex counter blocks for the CTR cipher mode.
54
55 Introduction to counter blocks
56 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
57
58 :ref:`CTR <ctr_mode>` (Counter) is a mode of operation for block ciphers.
59 In CTR mode, the ciphertext is generated by dividing the plaintext into blocks
60 and then XOR-ing each block with a unique *keystream*.
61
62 The *keystream* is produced by encrypting a sequence of *counter blocks*,
63 which must all be distinct to prevent repetitions in the keystream.
64 Counter blocks themselves do not need to be kept secret.
65 Encryption is done using :ref:`ECB <ecb_mode>`.
66
67 The most straightforward approach to create counter blocks is
68 to include a counter field, and increment it by one within each subsequent counter block.
69
70 Creating counter blocks without Counter
71 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
73 The :func:`new` function, at the module level under ``Crypto.Cipher``,
74 allows you to create a new CTR cipher object for a specific base algorithm.
75 It provides parameters for defining the structure of the counter block:
76
77 * an optional, fixed prefix
78 * the counter field encoded in big endian mode
79
80 The combined length of these two components must match the block size of the algorithm being used.
81 For example, in the case of AES, the block size is typically 16 bytes.
82
83 Creating counter blocks with Counter
84 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85
86 If you need a more complex structure for the counter block,
87 you can define it in advance with the function :func:`Crypto.Util.Counter.new()`,
88 and then pass it to :func:`new` of the cipher with the ``counter`` parameter.
89 The counter block can then have:
90
91 * an optional, fixed prefix
92 * the counter field, encoded in big endian or little endian mode
93 * an optional, fixed suffix
94
95 As before, the total length must match the block size.
96
97 The counter blocks with a big endian counter will look like this:
98
99 .. figure:: counter_be.png
100 :align: center
101
102 The counter blocks with a little endian counter will look like this:
103
104 .. figure:: counter_le.png
105 :align: center
106
107 This is an example of AES-CTR encryption with a custom counter::
108
109 from Crypto.Cipher import AES
110 from Crypto.Util import Counter
111 from Crypto import Random
112
113 # The counter block must be
114 #
115 # +--------+----------------+--------+
116 # | nonce + counter + EFGH +
117 # +--------+----------------+--------+
118 # 4 bytes 8 bytes 4 bytes
119 nonce = Random.get_random_bytes(4)
120 ctr = Counter.new(64, prefix=nonce, suffix=b'EFGH', little_endian=True, initial_value=10)
121 key = b'AES-128 symm key'
122 plaintext = b'X'*1000000
123 cipher = AES.new(key, AES.MODE_CTR, counter=ctr)
124 ciphertext = cipher.encrypt(plaintext)
125
126 .. automodule:: Crypto.Util.Counter
127 :members:
128
129 :mod:`Crypto.Util.number` module
130 --------------------------------
131
132 .. automodule:: Crypto.Util.number
133 :members: