Debian Codegraph @

lib/Crypto/Cipher/Blowfish.py

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