Debian Codegraph @

lib/Crypto/Cipher/_EKSBlowfish.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2019, Legrandin <[email protected]>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 # ===================================================================
30
31 import sys
32
33 from Crypto.Cipher import _create_cipher
34 from Crypto.Util._raw_api import (load_pycryptodome_raw_lib,
35 VoidPointer, SmartPointer, c_size_t,
36 c_uint8_ptr, c_uint)
37
38 _raw_blowfish_lib = load_pycryptodome_raw_lib(
39 "Crypto.Cipher._raw_eksblowfish",
40 """
41 int EKSBlowfish_start_operation(const uint8_t key[],
42 size_t key_len,
43 const uint8_t salt[16],
44 size_t salt_len,
45 unsigned cost,
46 unsigned invert,
47 void **pResult);
48 int EKSBlowfish_encrypt(const void *state,
49 const uint8_t *in,
50 uint8_t *out,
51 size_t data_len);
52 int EKSBlowfish_decrypt(const void *state,
53 const uint8_t *in,
54 uint8_t *out,
55 size_t data_len);
56 int EKSBlowfish_stop_operation(void *state);
57 """
58 )
59
60
61 def _create_base_cipher(dict_parameters):
62 """This method instantiates and returns a smart pointer to
63 a low-level base cipher. It will absorb named parameters in
64 the process."""
65
66 try:
67 key = dict_parameters.pop("key")
68 salt = dict_parameters.pop("salt")
69 cost = dict_parameters.pop("cost")
70 except KeyError as e:
71 raise TypeError("Missing EKSBlowfish parameter: " + str(e))
72 invert = dict_parameters.pop("invert", True)
73
74 if len(key) not in key_size:
75 raise ValueError("Incorrect EKSBlowfish key length (%d bytes)" % len(key))
76
77 start_operation = _raw_blowfish_lib.EKSBlowfish_start_operation
78 stop_operation = _raw_blowfish_lib.EKSBlowfish_stop_operation
79
80 void_p = VoidPointer()
81 result = start_operation(c_uint8_ptr(key),
82 c_size_t(len(key)),
83 c_uint8_ptr(salt),
84 c_size_t(len(salt)),
85 c_uint(cost),
86 c_uint(int(invert)),
87 void_p.address_of())
88 if result:
89 raise ValueError("Error %X while instantiating the EKSBlowfish cipher"
90 % result)
91 return SmartPointer(void_p.get(), stop_operation)
92
93
94 def new(key, mode, salt, cost, invert):
95 """Create a new EKSBlowfish cipher
96
97 Args:
98
99 key (bytes, bytearray, memoryview):
100 The secret key to use in the symmetric cipher.
101 Its length can vary from 0 to 72 bytes.
102
103 mode (one of the supported ``MODE_*`` constants):
104 The chaining mode to use for encryption or decryption.
105
106 salt (bytes, bytearray, memoryview):
107 The salt that bcrypt uses to thwart rainbow table attacks
108
109 cost (integer):
110 The complexity factor in bcrypt
111
112 invert (bool):
113 If ``False``, in the inner loop use ``ExpandKey`` first over the salt
114 and then over the key, as defined in
115 the `original bcrypt specification <https://www.usenix.org/legacy/events/usenix99/provos/provos_html/node4.html>`_.
116 If ``True``, reverse the order, as in the first implementation of
117 `bcrypt` in OpenBSD.
118
119 :Return: an EKSBlowfish object
120 """
121
122 kwargs = { 'salt':salt, 'cost':cost, 'invert':invert }
123 return _create_cipher(sys.modules[__name__], key, mode, **kwargs)
124
125
126 MODE_ECB = 1
127
128 # Size of a data block (in bytes)
129 block_size = 8
130 # Size of a key (in bytes)
131 key_size = range(0, 72 + 1)