diff options
Diffstat (limited to 'SWIG/_aes.i')
-rw-r--r-- | SWIG/_aes.i | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/SWIG/_aes.i b/SWIG/_aes.i index be0af87..ea36167 100644 --- a/SWIG/_aes.i +++ b/SWIG/_aes.i @@ -1,5 +1,5 @@ /* Copyright (c) 1999-2004 Ng Pheng Siong. All rights reserved. */ -/* $Id: _aes.i 721 2010-02-13 06:30:33Z heikki $ */ +/* $Id$ */ %{ #include <openssl/evp.h> @@ -28,8 +28,11 @@ extern EVP_CIPHER const *EVP_aes_256_ctr(void); AES_KEY *aes_new(void) { AES_KEY *key; - if (!(key = (AES_KEY *)PyMem_Malloc(sizeof(AES_KEY)))) - PyErr_SetString(PyExc_MemoryError, "aes_new"); + if (!(key = (AES_KEY *)PyMem_Malloc(sizeof(AES_KEY)))) { + PyErr_SetString(PyExc_MemoryError, + "Insufficient memory for AES key."); + return NULL; + } return key; } @@ -38,34 +41,34 @@ void AES_free(AES_KEY *key) { } /* -// op == 0: decrypt -// otherwise: encrypt (Python code will supply the value 1.) +// op == 0: encrypt +// otherwise: decrypt (Python code will supply the value 1.) */ PyObject *AES_set_key(AES_KEY *key, PyObject *value, int bits, int op) { - const void *vbuf; + char *vbuf; Py_ssize_t vlen; - if (PyObject_AsReadBuffer(value, &vbuf, &vlen) == -1) + if (PyBytes_AsStringAndSize(value, &vbuf, &vlen) == -1) return NULL; if (op == 0) - AES_set_encrypt_key(vbuf, bits, key); + AES_set_encrypt_key((const unsigned char *)vbuf, bits, key); else - AES_set_decrypt_key(vbuf, bits, key); - Py_INCREF(Py_None); - return Py_None; + AES_set_decrypt_key((const unsigned char *)vbuf, bits, key); + Py_RETURN_NONE; } /* -// op == 0: decrypt -// otherwise: encrypt (Python code will supply the value 1.) +// op == 0: encrypt +// otherwise: decrypt (Python code will supply the value 1.) */ PyObject *AES_crypt(const AES_KEY *key, PyObject *in, int outlen, int op) { - const void *buf; + char *buf; Py_ssize_t len; unsigned char *out; + PyObject *res; - if (PyObject_AsReadBuffer(in, &buf, &len) == -1) + if (PyBytes_AsStringAndSize(in, &buf, &len) == -1) return NULL; if (!(out=(unsigned char *)PyMem_Malloc(outlen))) { @@ -73,10 +76,12 @@ PyObject *AES_crypt(const AES_KEY *key, PyObject *in, int outlen, int op) { return NULL; } if (op == 0) - AES_encrypt((const unsigned char *)in, out, key); + AES_encrypt((const unsigned char *)buf, out, key); else - AES_decrypt((const unsigned char *)in, out, key); - return PyString_FromStringAndSize((char*)out, outlen); + AES_decrypt((const unsigned char *)buf, out, key); + res = PyBytes_FromStringAndSize((char*)out, outlen); + PyMem_Free(out); + return res; } int AES_type_check(AES_KEY *key) { |