diff options
author | Matěj Cepl <mcepl@cepl.eu> | 2020-01-29 20:16:11 +0100 |
---|---|---|
committer | Matěj Cepl <mcepl@cepl.eu> | 2020-01-29 22:21:11 +0100 |
commit | f6bb4413466aa2588bcb7d22b9e6bf97885f16d7 (patch) | |
tree | 106d8f5547897f1133829bca2ec46bc96e5fcd6a | |
parent | 7b04b43fe15ec95327a872f0ea83325fadf6840f (diff) | |
download | m2crypto-f6bb4413466aa2588bcb7d22b9e6bf97885f16d7.tar.gz |
NULL is legal argument for key and iv paramters of EVP_CipherInit(3)
Fixes #272
-rw-r--r-- | SWIG/_evp.i | 13 | ||||
-rw-r--r-- | tests/test_evp.py | 5 |
2 files changed, 16 insertions, 2 deletions
diff --git a/SWIG/_evp.i b/SWIG/_evp.i index d04e806..9a3b67b 100644 --- a/SWIG/_evp.i +++ b/SWIG/_evp.i @@ -430,8 +430,17 @@ PyObject *cipher_init(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, const void *kbuf, *ibuf; Py_ssize_t klen, ilen; - if ((PyObject_AsReadBuffer(key, &kbuf, &klen) == -1) - || (PyObject_AsReadBuffer(iv, &ibuf, &ilen) == -1)) + if (cipher == Py_None) + cipher = NULL; + + if (key == Py_None) + kbuf = NULL; + else if (PyObject_AsReadBuffer(key, &kbuf, &klen) == -1) + return NULL; + + if (iv == Py_None) + ibuf = NULL; + else if (PyObject_AsReadBuffer(iv, &ibuf, &ilen) == -1) return NULL; if (!EVP_CipherInit(ctx, cipher, (unsigned char *)kbuf, diff --git a/tests/test_evp.py b/tests/test_evp.py index d133ed0..fff7127 100644 --- a/tests/test_evp.py +++ b/tests/test_evp.py @@ -517,6 +517,11 @@ class CipherTestCase(unittest.TestCase): unhexlify('5cd148eeaf680d4ff933aed83009cad4110162f53ef89fd44fad09611b0524d4'), unhexlify('')) + def test_cipher_init_reinit(self): + ctx = m2.cipher_ctx_new() + m2.cipher_init(ctx, m2.aes_128_cbc(), b'\x01' * (128//8), b'\x02' * (128//8), 1) + m2.cipher_init(ctx, m2.aes_128_cbc(), None, None, 1) + class PBKDF2TestCase(unittest.TestCase): def test_rfc3211_test_vectors(self): |