Lines 23-30
Link Here
|
23 |
pass |
23 |
pass |
24 |
const EVP_MD *EVP_sha256() |
24 |
const EVP_MD *EVP_sha256() |
25 |
const EVP_CIPHER *EVP_aes_256_ctr() |
25 |
const EVP_CIPHER *EVP_aes_256_ctr() |
26 |
void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a) |
26 |
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new() |
27 |
void EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a) |
27 |
const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *a) |
|
|
28 |
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *a) |
28 |
|
29 |
|
29 |
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, |
30 |
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, |
30 |
const unsigned char *key, const unsigned char *iv) |
31 |
const unsigned char *key, const unsigned char *iv) |
Lines 84-99
Link Here
|
84 |
cdef class AES: |
85 |
cdef class AES: |
85 |
"""A thin wrapper around the OpenSSL EVP cipher API |
86 |
"""A thin wrapper around the OpenSSL EVP cipher API |
86 |
""" |
87 |
""" |
87 |
cdef EVP_CIPHER_CTX ctx |
88 |
cdef EVP_CIPHER_CTX * ctx |
88 |
|
89 |
|
89 |
def __cinit__(self, key, iv=None): |
90 |
def __cinit__(self, key, iv=None): |
90 |
EVP_CIPHER_CTX_init(&self.ctx) |
91 |
self.ctx = EVP_CIPHER_CTX_new(); |
91 |
if not EVP_EncryptInit_ex(&self.ctx, EVP_aes_256_ctr(), NULL, NULL, NULL): |
92 |
if not EVP_EncryptInit_ex(self.ctx, EVP_aes_256_ctr(), NULL, NULL, NULL): |
92 |
raise Exception('EVP_EncryptInit_ex failed') |
93 |
raise Exception('EVP_EncryptInit_ex failed') |
93 |
self.reset(key, iv) |
94 |
self.reset(key, iv) |
94 |
|
95 |
|
95 |
def __dealloc__(self): |
96 |
def __dealloc__(self): |
96 |
EVP_CIPHER_CTX_cleanup(&self.ctx) |
97 |
EVP_CIPHER_CTX_free(self.ctx) |
97 |
|
98 |
|
98 |
def reset(self, key=None, iv=None): |
99 |
def reset(self, key=None, iv=None): |
99 |
cdef const unsigned char *key2 = NULL |
100 |
cdef const unsigned char *key2 = NULL |
Lines 102-113
Link Here
|
102 |
key2 = key |
103 |
key2 = key |
103 |
if iv: |
104 |
if iv: |
104 |
iv2 = iv |
105 |
iv2 = iv |
105 |
if not EVP_EncryptInit_ex(&self.ctx, NULL, NULL, key2, iv2): |
106 |
if not EVP_EncryptInit_ex(self.ctx, NULL, NULL, key2, iv2): |
106 |
raise Exception('EVP_EncryptInit_ex failed') |
107 |
raise Exception('EVP_EncryptInit_ex failed') |
107 |
|
108 |
|
108 |
@property |
109 |
@property |
109 |
def iv(self): |
110 |
def iv(self): |
110 |
return self.ctx.iv[:16] |
111 |
return EVP_CIPHER_CTX_iv(self.ctx)[:16] |
111 |
|
112 |
|
112 |
def encrypt(self, data): |
113 |
def encrypt(self, data): |
113 |
cdef int inl = len(data) |
114 |
cdef int inl = len(data) |
Lines 116-122
Link Here
|
116 |
if not out: |
117 |
if not out: |
117 |
raise MemoryError |
118 |
raise MemoryError |
118 |
try: |
119 |
try: |
119 |
if not EVP_EncryptUpdate(&self.ctx, out, &outl, data, inl): |
120 |
if not EVP_EncryptUpdate(self.ctx, out, &outl, data, inl): |
120 |
raise Exception('EVP_EncryptUpdate failed') |
121 |
raise Exception('EVP_EncryptUpdate failed') |
121 |
return out[:inl] |
122 |
return out[:inl] |
122 |
finally: |
123 |
finally: |