| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 129 |
block_size = 8 |
| 130 |
|
| 131 |
key_size = range(0, 72 + 1) |