mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-26 03:13:45 +03:00
Improve libssl.py's file formatting
This commit is contained in:
parent
274e16ac66
commit
88ec9c297e
|
@ -3,7 +3,7 @@ import pyaes
|
||||||
from . import libssl
|
from . import libssl
|
||||||
|
|
||||||
|
|
||||||
if libssl.libssl:
|
if libssl.AES is not None:
|
||||||
# Use libssl if available, since it will be faster
|
# Use libssl if available, since it will be faster
|
||||||
AES = libssl.AES
|
AES = libssl.AES
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -2,51 +2,47 @@ import os
|
||||||
import ctypes
|
import ctypes
|
||||||
from ctypes.util import find_library
|
from ctypes.util import find_library
|
||||||
|
|
||||||
# search and load libssl.so
|
|
||||||
lib = find_library('ssl')
|
lib = find_library('ssl')
|
||||||
if not lib:
|
if not lib:
|
||||||
libssl = None
|
AES = None
|
||||||
else:
|
else:
|
||||||
libssl = ctypes.cdll.LoadLibrary(lib)
|
""" <aes.h>
|
||||||
|
# define AES_ENCRYPT 1
|
||||||
""" <aes.h>
|
# define AES_DECRYPT 0
|
||||||
# define AES_ENCRYPT 1
|
# define AES_MAXNR 14
|
||||||
# define AES_DECRYPT 0
|
struct aes_key_st {
|
||||||
# define AES_MAXNR 14
|
# ifdef AES_LONG
|
||||||
struct aes_key_st {
|
|
||||||
# ifdef AES_LONG
|
|
||||||
unsigned long rd_key[4 * (AES_MAXNR + 1)];
|
unsigned long rd_key[4 * (AES_MAXNR + 1)];
|
||||||
# else
|
# else
|
||||||
unsigned int rd_key[4 * (AES_MAXNR + 1)];
|
unsigned int rd_key[4 * (AES_MAXNR + 1)];
|
||||||
# endif
|
# endif
|
||||||
int rounds;
|
int rounds;
|
||||||
};
|
};
|
||||||
typedef struct aes_key_st AES_KEY;
|
typedef struct aes_key_st AES_KEY;
|
||||||
|
|
||||||
int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
||||||
AES_KEY *key);
|
AES_KEY *key);
|
||||||
int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
||||||
AES_KEY *key);
|
AES_KEY *key);
|
||||||
void AES_ige_encrypt(const unsigned char *in, unsigned char *out,
|
void AES_ige_encrypt(const unsigned char *in, unsigned char *out,
|
||||||
size_t length, const AES_KEY *key,
|
size_t length, const AES_KEY *key,
|
||||||
unsigned char *ivec, const int enc);
|
unsigned char *ivec, const int enc);
|
||||||
"""
|
"""
|
||||||
|
_libssl = ctypes.cdll.LoadLibrary(lib)
|
||||||
|
|
||||||
AES_MAXNR = 14
|
AES_MAXNR = 14
|
||||||
AES_ENCRYPT = ctypes.c_int(1)
|
AES_ENCRYPT = ctypes.c_int(1)
|
||||||
AES_DECRYPT = ctypes.c_int(0)
|
AES_DECRYPT = ctypes.c_int(0)
|
||||||
|
|
||||||
class AES_KEY(ctypes.Structure):
|
class AES_KEY(ctypes.Structure):
|
||||||
_fields_ = [
|
_fields_ = [
|
||||||
('rd_key', ctypes.c_uint32 * (4*(AES_MAXNR + 1))),
|
('rd_key', ctypes.c_uint32 * (4*(AES_MAXNR + 1))),
|
||||||
('rounds', ctypes.c_uint),
|
('rounds', ctypes.c_uint),
|
||||||
]
|
]
|
||||||
|
|
||||||
class AES:
|
class AES:
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def decrypt_ige(cipher_text, key, iv):
|
def decrypt_ige(cipher_text, key, iv):
|
||||||
|
|
||||||
# declare types
|
|
||||||
aeskey = AES_KEY()
|
aeskey = AES_KEY()
|
||||||
ckey = (ctypes.c_ubyte * len(key))(*key)
|
ckey = (ctypes.c_ubyte * len(key))(*key)
|
||||||
cklen = ctypes.c_int(len(key)*8)
|
cklen = ctypes.c_int(len(key)*8)
|
||||||
|
@ -55,21 +51,25 @@ class AES:
|
||||||
cout = (ctypes.c_ubyte * len(cipher_text))()
|
cout = (ctypes.c_ubyte * len(cipher_text))()
|
||||||
civ = (ctypes.c_ubyte * len(iv))(*iv)
|
civ = (ctypes.c_ubyte * len(iv))(*iv)
|
||||||
|
|
||||||
# decrypt
|
_libssl.AES_set_decrypt_key(ckey, cklen, ctypes.byref(aeskey))
|
||||||
libssl.AES_set_decrypt_key(ckey, cklen, ctypes.byref(aeskey))
|
_libssl.AES_ige_encrypt(
|
||||||
libssl.AES_ige_encrypt(ctypes.byref(cin), ctypes.byref(cout), ctlen, ctypes.byref(aeskey), ctypes.byref(civ), AES_DECRYPT)
|
ctypes.byref(cin),
|
||||||
|
ctypes.byref(cout),
|
||||||
|
ctlen,
|
||||||
|
ctypes.byref(aeskey),
|
||||||
|
ctypes.byref(civ),
|
||||||
|
AES_DECRYPT
|
||||||
|
)
|
||||||
|
|
||||||
return bytes(cout)
|
return bytes(cout)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def encrypt_ige(plain_text, key, iv):
|
def encrypt_ige(plain_text, key, iv):
|
||||||
|
# Add random padding iff it's not evenly divisible by 16 already
|
||||||
# Add random padding if and only if it's not evenly divisible by 16 already
|
|
||||||
if len(plain_text) % 16 != 0:
|
if len(plain_text) % 16 != 0:
|
||||||
padding_count = 16 - len(plain_text) % 16
|
padding_count = 16 - len(plain_text) % 16
|
||||||
plain_text += os.urandom(padding_count)
|
plain_text += os.urandom(padding_count)
|
||||||
|
|
||||||
# declare types
|
|
||||||
aeskey = AES_KEY()
|
aeskey = AES_KEY()
|
||||||
ckey = (ctypes.c_ubyte * len(key))(*key)
|
ckey = (ctypes.c_ubyte * len(key))(*key)
|
||||||
cklen = ctypes.c_int(len(key)*8)
|
cklen = ctypes.c_int(len(key)*8)
|
||||||
|
@ -78,8 +78,14 @@ class AES:
|
||||||
cout = (ctypes.c_ubyte * len(plain_text))()
|
cout = (ctypes.c_ubyte * len(plain_text))()
|
||||||
civ = (ctypes.c_ubyte * len(iv))(*iv)
|
civ = (ctypes.c_ubyte * len(iv))(*iv)
|
||||||
|
|
||||||
# encrypt
|
_libssl.AES_set_encrypt_key(ckey, cklen, ctypes.byref(aeskey))
|
||||||
libssl.AES_set_encrypt_key(ckey, cklen, ctypes.byref(aeskey))
|
_libssl.AES_ige_encrypt(
|
||||||
libssl.AES_ige_encrypt(ctypes.byref(cin), ctypes.byref(cout), ctlen, ctypes.byref(aeskey), ctypes.byref(civ), AES_ENCRYPT)
|
ctypes.byref(cin),
|
||||||
|
ctypes.byref(cout),
|
||||||
|
ctlen,
|
||||||
|
ctypes.byref(aeskey),
|
||||||
|
ctypes.byref(civ),
|
||||||
|
AES_ENCRYPT
|
||||||
|
)
|
||||||
|
|
||||||
return bytes(cout)
|
return bytes(cout)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user