Debian Codegraph @

lib/Crypto/Cipher/DES.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # Cipher/DES.py : DES
4 #
5 # ===================================================================
6 # The contents of this file are dedicated to the public domain. To
7 # the extent that dedication to the public domain is not available,
8 # everyone is granted a worldwide, perpetual, royalty-free,
9 # non-exclusive license to exercise all rights associated with the
10 # contents of this file for any purpose whatsoever.
11 # No rights are reserved.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 # SOFTWARE.
21 # ===================================================================
22 """
23 Module's constants for the modes of operation supported with Single DES:
24
25 :var MODE_ECB: :ref:`Electronic Code Book (ECB) <ecb_mode>`
26 :var MODE_CBC: :ref:`Cipher-Block Chaining (CBC) <cbc_mode>`
27 :var MODE_CFB: :ref:`Cipher FeedBack (CFB) <cfb_mode>`
28 :var MODE_OFB: :ref:`Output FeedBack (OFB) <ofb_mode>`
29 :var MODE_CTR: :ref:`CounTer Mode (CTR) <ctr_mode>`
30 :var MODE_OPENPGP: :ref:`OpenPGP Mode <openpgp_mode>`
31 :var MODE_EAX: :ref:`EAX Mode <eax_mode>`
32 """
33
34 import sys
35
36 from Crypto.Cipher import _create_cipher
37 from Crypto.Util.py3compat import byte_string
38 from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
39 VoidPointer, SmartPointer,
40 c_size_t, c_uint8_ptr)
41
42 _raw_des_lib = load_pycryptodome_raw_lib(
43 "Crypto.Cipher._raw_des",
44 """
45 int DES_start_operation(const uint8_t key[],
46 size_t key_len,
47 void **pResult);
48 int DES_encrypt(const void *state,
49 const uint8_t *in,
50 uint8_t *out,
51 size_t data_len);
52 int DES_decrypt(const void *state,
53 const uint8_t *in,
54 uint8_t *out,
55 size_t data_len);
56 int DES_stop_operation(void *state);
57 """)
58
59
60 def _create_base_cipher(dict_parameters):
61 """This method instantiates and returns a handle to a low-level
62 base cipher. It will absorb named parameters in the process."""
63
64 try:
65 key = dict_parameters.pop("key")
66 except KeyError:
67 raise TypeError("Missing 'key' parameter")
68
69 if len(key) != key_size:
70 raise ValueError("Incorrect DES key length (%d bytes)" % len(key))
71
72 start_operation = _raw_des_lib.DES_start_operation
73 stop_operation = _raw_des_lib.DES_stop_operation
74
75 cipher = VoidPointer()
76 result = start_operation(c_uint8_ptr(key),
77 c_size_t(len(key)),
78 cipher.address_of())
79 if result:
80 raise ValueError("Error %X while instantiating the DES cipher"
81 % result)
82 return SmartPointer(cipher.get(), stop_operation)
83
84
85 def new(key, mode, *args, **kwargs):
86 """Create a new DES cipher.
87
88 :param key:
89 The secret key to use in the symmetric cipher.
90 It must be 8 byte long. The parity bits will be ignored.
91 :type key: bytes/bytearray/memoryview
92
93 :param mode:
94 The chaining mode to use for encryption or decryption.
95 :type mode: One of the supported ``MODE_*`` constants
96
97 :Keyword Arguments:
98 * **iv** (*byte string*) --
99 (Only applicable for ``MODE_CBC``, ``MODE_CFB``, ``MODE_OFB``,
100 and ``MODE_OPENPGP`` modes).
101
102 The initialization vector to use for encryption or decryption.
103
104 For ``MODE_CBC``, ``MODE_CFB``, and ``MODE_OFB`` it must be 8 bytes long.
105
106 For ``MODE_OPENPGP`` mode only,
107 it must be 8 bytes long for encryption
108 and 10 bytes for decryption (in the latter case, it is
109 actually the *encrypted* IV which was prefixed to the ciphertext).
110
111 If not provided, a random byte string is generated (you must then
112 read its value with the :attr:`iv` attribute).
113
114 * **nonce** (*byte string*) --
115 (Only applicable for ``MODE_EAX`` and ``MODE_CTR``).
116
117 A value that must never be reused for any other encryption done
118 with this key.
119
120 For ``MODE_EAX`` there are no
121 restrictions on its length (recommended: **16** bytes).
122
123 For ``MODE_CTR``, its length must be in the range **[0..7]**.
124
125 If not provided for ``MODE_EAX``, a random byte string is generated (you
126 can read it back via the ``nonce`` attribute).
127
128 * **segment_size** (*integer*) --
129 (Only ``MODE_CFB``).The number of **bits** the plaintext and ciphertext
130 are segmented in. It must be a multiple of 8.
131 If not specified, it will be assumed to be 8.
132
133 * **mac_len** : (*integer*) --
134 (Only ``MODE_EAX``)
135 Length of the authentication tag, in bytes.
136 It must be no longer than 8 (default).
137
138 * **initial_value** : (*integer*) --
139 (Only ``MODE_CTR``). The initial value for the counter within
140 the counter block. By default it is **0**.
141
142 :Return: a DES object, of the applicable mode.
143 """
144
145 return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
146
147 MODE_ECB = 1
148 MODE_CBC = 2
149 MODE_CFB = 3
150 MODE_OFB = 5
151 MODE_CTR = 6
152 MODE_OPENPGP = 7
153 MODE_EAX = 9
154
155 # Size of a data block (in bytes)
156 block_size = 8
157 # Size of a key (in bytes)
158 key_size = 8