Debian Codegraph @

lib/Crypto/Cipher/ARC2.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # Cipher/ARC2.py : ARC2.py
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 ARC2:
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_arc2_lib = load_pycryptodome_raw_lib(
43 "Crypto.Cipher._raw_arc2",
44 """
45 int ARC2_start_operation(const uint8_t key[],
46 size_t key_len,
47 size_t effective_key_len,
48 void **pResult);
49 int ARC2_encrypt(const void *state,
50 const uint8_t *in,
51 uint8_t *out,
52 size_t data_len);
53 int ARC2_decrypt(const void *state,
54 const uint8_t *in,
55 uint8_t *out,
56 size_t data_len);
57 int ARC2_stop_operation(void *state);
58 """
59 )
60
61
62 def _create_base_cipher(dict_parameters):
63 """This method instantiates and returns a handle to a low-level
64 base cipher. It will absorb named parameters in the process."""
65
66 try:
67 key = dict_parameters.pop("key")
68 except KeyError:
69 raise TypeError("Missing 'key' parameter")
70
71 effective_keylen = dict_parameters.pop("effective_keylen", 1024)
72
73 if len(key) not in key_size:
74 raise ValueError("Incorrect ARC2 key length (%d bytes)" % len(key))
75
76 if not (40 <= effective_keylen <= 1024):
77 raise ValueError("'effective_key_len' must be at least 40 and no larger than 1024 "
78 "(not %d)" % effective_keylen)
79
80 start_operation = _raw_arc2_lib.ARC2_start_operation
81 stop_operation = _raw_arc2_lib.ARC2_stop_operation
82
83 cipher = VoidPointer()
84 result = start_operation(c_uint8_ptr(key),
85 c_size_t(len(key)),
86 c_size_t(effective_keylen),
87 cipher.address_of())
88 if result:
89 raise ValueError("Error %X while instantiating the ARC2 cipher"
90 % result)
91
92 return SmartPointer(cipher.get(), stop_operation)
93
94
95 def new(key, mode, *args, **kwargs):
96 """Create a new RC2 cipher.
97
98 :param key:
99 The secret key to use in the symmetric cipher.
100 Its length can vary from 5 to 128 bytes; the actual search space
101 (and the cipher strength) can be reduced with the ``effective_keylen`` parameter.
102 :type key: bytes, bytearray, memoryview
103
104 :param mode:
105 The chaining mode to use for encryption or decryption.
106 :type mode: One of the supported ``MODE_*`` constants
107
108 :Keyword Arguments:
109 * **iv** (*bytes*, *bytearray*, *memoryview*) --
110 (Only applicable for ``MODE_CBC``, ``MODE_CFB``, ``MODE_OFB``,
111 and ``MODE_OPENPGP`` modes).
112
113 The initialization vector to use for encryption or decryption.
114
115 For ``MODE_CBC``, ``MODE_CFB``, and ``MODE_OFB`` it must be 8 bytes long.
116
117 For ``MODE_OPENPGP`` mode only,
118 it must be 8 bytes long for encryption
119 and 10 bytes for decryption (in the latter case, it is
120 actually the *encrypted* IV which was prefixed to the ciphertext).
121
122 If not provided, a random byte string is generated (you must then
123 read its value with the :attr:`iv` attribute).
124
125 * **nonce** (*bytes*, *bytearray*, *memoryview*) --
126 (Only applicable for ``MODE_EAX`` and ``MODE_CTR``).
127
128 A value that must never be reused for any other encryption done
129 with this key.
130
131 For ``MODE_EAX`` there are no
132 restrictions on its length (recommended: **16** bytes).
133
134 For ``MODE_CTR``, its length must be in the range **[0..7]**.
135
136 If not provided for ``MODE_EAX``, a random byte string is generated (you
137 can read it back via the ``nonce`` attribute).
138
139 * **effective_keylen** (*integer*) --
140 Optional. Maximum strength in bits of the actual key used by the ARC2 algorithm.
141 If the supplied ``key`` parameter is longer (in bits) of the value specified
142 here, it will be weakened to match it.
143 If not specified, no limitation is applied.
144
145 * **segment_size** (*integer*) --
146 (Only ``MODE_CFB``).The number of **bits** the plaintext and ciphertext
147 are segmented in. It must be a multiple of 8.
148 If not specified, it will be assumed to be 8.
149
150 * **mac_len** : (*integer*) --
151 (Only ``MODE_EAX``)
152 Length of the authentication tag, in bytes.
153 It must be no longer than 8 (default).
154
155 * **initial_value** : (*integer*) --
156 (Only ``MODE_CTR``). The initial value for the counter within
157 the counter block. By default it is **0**.
158
159 :Return: an ARC2 object, of the applicable mode.
160 """
161
162 return _create_cipher(sys.modules[__name__], key, mode, *args, **kwargs)
163
164 MODE_ECB = 1
165 MODE_CBC = 2
166 MODE_CFB = 3
167 MODE_OFB = 5
168 MODE_CTR = 6
169 MODE_OPENPGP = 7
170 MODE_EAX = 9
171
172 # Size of a data block (in bytes)
173 block_size = 8
174 # Size of a key (in bytes)
175 key_size = range(5, 128 + 1)