|
Line 0
Link Here
|
|
|
1 |
--- attic/crypto.pyx.orig 2015-04-27 20:15:50 UTC |
| 2 |
+++ attic/crypto.pyx |
| 3 |
@@ -23,8 +23,9 @@ cdef extern from "openssl/evp.h": |
| 4 |
pass |
| 5 |
const EVP_MD *EVP_sha256() |
| 6 |
const EVP_CIPHER *EVP_aes_256_ctr() |
| 7 |
- void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *a) |
| 8 |
- void EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *a) |
| 9 |
+ EVP_CIPHER_CTX *EVP_CIPHER_CTX_new() |
| 10 |
+ const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *a) |
| 11 |
+ void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *a) |
| 12 |
|
| 13 |
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl, |
| 14 |
const unsigned char *key, const unsigned char *iv) |
| 15 |
@@ -84,16 +85,16 @@ def get_random_bytes(n): |
| 16 |
cdef class AES: |
| 17 |
"""A thin wrapper around the OpenSSL EVP cipher API |
| 18 |
""" |
| 19 |
- cdef EVP_CIPHER_CTX ctx |
| 20 |
+ cdef EVP_CIPHER_CTX * ctx |
| 21 |
|
| 22 |
def __cinit__(self, key, iv=None): |
| 23 |
- EVP_CIPHER_CTX_init(&self.ctx) |
| 24 |
- if not EVP_EncryptInit_ex(&self.ctx, EVP_aes_256_ctr(), NULL, NULL, NULL): |
| 25 |
+ self.ctx = EVP_CIPHER_CTX_new(); |
| 26 |
+ if not EVP_EncryptInit_ex(self.ctx, EVP_aes_256_ctr(), NULL, NULL, NULL): |
| 27 |
raise Exception('EVP_EncryptInit_ex failed') |
| 28 |
self.reset(key, iv) |
| 29 |
|
| 30 |
def __dealloc__(self): |
| 31 |
- EVP_CIPHER_CTX_cleanup(&self.ctx) |
| 32 |
+ EVP_CIPHER_CTX_free(self.ctx) |
| 33 |
|
| 34 |
def reset(self, key=None, iv=None): |
| 35 |
cdef const unsigned char *key2 = NULL |
| 36 |
@@ -102,12 +103,12 @@ cdef class AES: |
| 37 |
key2 = key |
| 38 |
if iv: |
| 39 |
iv2 = iv |
| 40 |
- if not EVP_EncryptInit_ex(&self.ctx, NULL, NULL, key2, iv2): |
| 41 |
+ if not EVP_EncryptInit_ex(self.ctx, NULL, NULL, key2, iv2): |
| 42 |
raise Exception('EVP_EncryptInit_ex failed') |
| 43 |
|
| 44 |
@property |
| 45 |
def iv(self): |
| 46 |
- return self.ctx.iv[:16] |
| 47 |
+ return EVP_CIPHER_CTX_iv(self.ctx)[:16] |
| 48 |
|
| 49 |
def encrypt(self, data): |
| 50 |
cdef int inl = len(data) |
| 51 |
@@ -116,7 +117,7 @@ cdef class AES: |
| 52 |
if not out: |
| 53 |
raise MemoryError |
| 54 |
try: |
| 55 |
- if not EVP_EncryptUpdate(&self.ctx, out, &outl, data, inl): |
| 56 |
+ if not EVP_EncryptUpdate(self.ctx, out, &outl, data, inl): |
| 57 |
raise Exception('EVP_EncryptUpdate failed') |
| 58 |
return out[:inl] |
| 59 |
finally: |