2017-08-20 02:21:11 +03:00
|
|
|
import os
|
|
|
|
import ctypes
|
|
|
|
from ctypes.util import find_library
|
|
|
|
|
|
|
|
lib = find_library('ssl')
|
|
|
|
if not lib:
|
2017-08-20 02:27:04 +03:00
|
|
|
AES = None
|
2017-08-20 02:21:11 +03:00
|
|
|
else:
|
2017-08-20 02:27:04 +03:00
|
|
|
""" <aes.h>
|
|
|
|
# define AES_ENCRYPT 1
|
|
|
|
# define AES_DECRYPT 0
|
|
|
|
# define AES_MAXNR 14
|
|
|
|
struct aes_key_st {
|
|
|
|
# ifdef AES_LONG
|
|
|
|
unsigned long rd_key[4 * (AES_MAXNR + 1)];
|
|
|
|
# else
|
|
|
|
unsigned int rd_key[4 * (AES_MAXNR + 1)];
|
|
|
|
# endif
|
|
|
|
int rounds;
|
|
|
|
};
|
|
|
|
typedef struct aes_key_st AES_KEY;
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
|
|
|
AES_KEY *key);
|
|
|
|
int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
|
|
|
AES_KEY *key);
|
|
|
|
void AES_ige_encrypt(const unsigned char *in, unsigned char *out,
|
|
|
|
size_t length, const AES_KEY *key,
|
|
|
|
unsigned char *ivec, const int enc);
|
|
|
|
"""
|
|
|
|
_libssl = ctypes.cdll.LoadLibrary(lib)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
AES_MAXNR = 14
|
|
|
|
AES_ENCRYPT = ctypes.c_int(1)
|
|
|
|
AES_DECRYPT = ctypes.c_int(0)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
class AES_KEY(ctypes.Structure):
|
|
|
|
_fields_ = [
|
|
|
|
('rd_key', ctypes.c_uint32 * (4*(AES_MAXNR + 1))),
|
|
|
|
('rounds', ctypes.c_uint),
|
|
|
|
]
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
class AES:
|
|
|
|
@staticmethod
|
|
|
|
def decrypt_ige(cipher_text, key, iv):
|
|
|
|
aeskey = AES_KEY()
|
|
|
|
ckey = (ctypes.c_ubyte * len(key))(*key)
|
|
|
|
cklen = ctypes.c_int(len(key)*8)
|
|
|
|
cin = (ctypes.c_ubyte * len(cipher_text))(*cipher_text)
|
|
|
|
ctlen = ctypes.c_size_t(len(cipher_text))
|
|
|
|
cout = (ctypes.c_ubyte * len(cipher_text))()
|
|
|
|
civ = (ctypes.c_ubyte * len(iv))(*iv)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
_libssl.AES_set_decrypt_key(ckey, cklen, ctypes.byref(aeskey))
|
|
|
|
_libssl.AES_ige_encrypt(
|
|
|
|
ctypes.byref(cin),
|
|
|
|
ctypes.byref(cout),
|
|
|
|
ctlen,
|
|
|
|
ctypes.byref(aeskey),
|
|
|
|
ctypes.byref(civ),
|
|
|
|
AES_DECRYPT
|
|
|
|
)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
return bytes(cout)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
@staticmethod
|
|
|
|
def encrypt_ige(plain_text, key, iv):
|
|
|
|
# Add random padding iff it's not evenly divisible by 16 already
|
|
|
|
if len(plain_text) % 16 != 0:
|
|
|
|
padding_count = 16 - len(plain_text) % 16
|
|
|
|
plain_text += os.urandom(padding_count)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
aeskey = AES_KEY()
|
|
|
|
ckey = (ctypes.c_ubyte * len(key))(*key)
|
|
|
|
cklen = ctypes.c_int(len(key)*8)
|
|
|
|
cin = (ctypes.c_ubyte * len(plain_text))(*plain_text)
|
|
|
|
ctlen = ctypes.c_size_t(len(plain_text))
|
|
|
|
cout = (ctypes.c_ubyte * len(plain_text))()
|
|
|
|
civ = (ctypes.c_ubyte * len(iv))(*iv)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
_libssl.AES_set_encrypt_key(ckey, cklen, ctypes.byref(aeskey))
|
|
|
|
_libssl.AES_ige_encrypt(
|
|
|
|
ctypes.byref(cin),
|
|
|
|
ctypes.byref(cout),
|
|
|
|
ctlen,
|
|
|
|
ctypes.byref(aeskey),
|
|
|
|
ctypes.byref(civ),
|
|
|
|
AES_ENCRYPT
|
|
|
|
)
|
2017-08-20 02:21:11 +03:00
|
|
|
|
2017-08-20 02:27:04 +03:00
|
|
|
return bytes(cout)
|