diff options
author | yan11.meng <yan11.meng@samsung.com> | 2020-02-26 12:47:07 +0900 |
---|---|---|
committer | yan11.meng <yan11.meng@samsung.com> | 2020-02-26 12:47:07 +0900 |
commit | 13ec6f74c73b377d6233de42cc942846e976541b (patch) | |
tree | 894efb8d5e53dd68dd463f7b222406439f4f494d /SWIG/_aes.i | |
parent | 5015799de7fb1dc08b584c0044d8393e4062ff34 (diff) | |
download | python-M2Crypto-13ec6f74c73b377d6233de42cc942846e976541b.tar.gz python-M2Crypto-13ec6f74c73b377d6233de42cc942846e976541b.tar.bz2 python-M2Crypto-13ec6f74c73b377d6233de42cc942846e976541b.zip |
Imported Upstream version 0.35.1
Change-Id: Id4a444b8c1d11fee1026b433a47ca5aabcf11622
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) { |