Python OpenSSL C扩展:未定义符号:AES_set_encrypt_密钥

2024-05-03 16:38:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为Python编写一个opensslc扩展。已生成共享库(*.so文件),但导入模块时遇到未定义的符号错误。它抛出以下错误(未定义的符号:AES_set_encrypt_key)

>>> import openssl_python
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /home/rohith/python_c_extension/aes/openssl_python.cpython-35m-x86_64-linux-gnu.so: undefined symbol: AES_set_encrypt_key

下面是我的源代码

设置.py

^{pr2}$

openssl_python.c

#include <Python.h>
#include <stdio.h>
#include <openssl/des.h>
#include <openssl/aes.h>

static PyObject* openssl_module_aes_encrypt(PyObject *self, PyObject *args){
    char* sn;
    if (!PyArg_ParseTuple(args, "s", &sn))
        return NULL;

    AES_KEY       key;
    unsigned char ivec[AES_BLOCK_SIZE];

    unsigned char outBuf[16];

    memcpy(ivec, sqlcFirmwareIvec, sizeof(sqlcFirmwareIvec));

    AES_set_encrypt_key(sqlcFirmwareKey,
                            sizeof(sqlcFirmwareKey) * 8,
                            &key);
    int dataLen = 16;
    int requiredLen = (dataLen / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;

    if (dataLen % AES_BLOCK_SIZE) {
        requiredLen += AES_BLOCK_SIZE;
    }

    AES_cbc_encrypt(sn, outBuf, requiredLen, &key, ivec, AES_ENCRYPT);

    return 1;
}

static PyMethodDef openssl_module_methods[] = { //Can add more functions here
    {   
        "aes_encrypt",
        openssl_module_aes_encrypt,
        METH_VARARGS,
        "Method to encrypt data using Openssl's AES algorithm"
    },  
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef openssl_module_definition = { 
    PyModuleDef_HEAD_INIT,
    "hello_module",
    "A Python module that prints 'hello world' from C code.",
    -1, 
    openssl_module_methods
};

PyMODINIT_FUNC PyInit_openssl_python(void)
{
    Py_Initialize();
    return PyModule_Create(&openssl_module_definition);
}

我用CFLAGS="-lcrypto" python3 ./setup.py build_ext --inplace编译它

有谁能帮我修正这个错误吗?在

谢谢。我不是故意展示key和Ivec的价值观。在

编辑:

运行命令:python3 setup.py clean, CFLAGS="-Wl,-z,defs -lcrypto" python3 setup.py build_ext --inplace

这是输出

running build_ext
building 'openssl_python' extension
creating build
creating build/temp.linux-x86_64-3.5
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wl,-z,defs -lcrypto -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c openssl_python.c -o build/temp.linux-x86_64-3.5/openssl_python.o
openssl_python.c: In function ‘openssl_module_aes_encrypt’:
openssl_python.c:49:21: warning: pointer targets in passing argument 1 of ‘AES_cbc_encrypt’ differ in signedness [-Wpointer-sign]
     AES_cbc_encrypt(sn,
                     ^
In file included from openssl_python.c:4:0:
/usr/include/openssl/aes.h:107:6: note: expected ‘const unsigned char *’ but argument is of type ‘char *’
 void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
      ^
openssl_python.c:61:12: warning: return makes pointer from integer without a cast [-Wint-conversion]
     return 1;
            ^
openssl_python.c:20:23: warning: unused variable ‘sqlcFirmwarePadding’ [-Wunused-variable]
  static unsigned char sqlcFirmwarePadding[] = {
                       ^
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,defs -lcrypto -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.5/openssl_python.o -o /home/rohith/python_c_extension/aes/openssl_python.cpython-35m-x86_64-linux-gnu.so
build/temp.linux-x86_64-3.5/openssl_python.o: In function `openssl_module_aes_encrypt':
/home/rohith/python_c_extension/aes/openssl_python.c:27: undefined reference to `PyArg_ParseTuple'
/home/rohith/python_c_extension/aes/openssl_python.c:37: undefined reference to `AES_set_encrypt_key'
/home/rohith/python_c_extension/aes/openssl_python.c:49: undefined reference to `AES_cbc_encrypt'
build/temp.linux-x86_64-3.5/openssl_python.o: In function `PyInit_openssl_python':
/home/rohith/python_c_extension/aes/openssl_python.c:84: undefined reference to `Py_Initialize'
/home/rohith/python_c_extension/aes/openssl_python.c:86: undefined reference to `PyModule_Create2'
collect2: error: ld returned 1 exit status
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Tags: keybuildhomelinuxextensionx86encryptaes
2条回答

这里的关键问题是当setup.py链接扩展时,它会将-lcrypto放在命令行上,然后是中包含代码的对象文件。Unix链接器在命令行严格地从左到右处理对象和库:-lcrypto foo.o将不使用libcrypto来解析foo.o中的符号。这是因为历史原因,现在已经没有什么意义了,但我们还是坚持下去,因为它会破坏太多的makefile来修改它。另外,由于历史原因,不再有任何意义,如果不在命令行中放置-Wl,-z,defs,其中包含未定义符号的共享库(编译代码Python扩展在技术上是共享库)并不是链接时间错误,这就是构建似乎起作用的原因。

您的扩展本质上需要libcrypto。如果我正确地阅读了Distutils文档,这意味着您应该在libraries=关键字参数中将其指定给Extension(...),而不是将其放在CFLAGS中。像这样:

openssl_module = Extension('openssl_python',
                       sources = ['openssl_python.c'],
                       libraries = ['crypto'])

zwol的解决方案要好得多。但我只是为了完整起见把它包括在内。

发出以下命令:

x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.5m -c openssl_python.c -lcrypto -o build/temp.linux-x86_64-3.5/openssl_python.o

其次是

^{pr2}$

现在已生成共享库。

相关问题 更多 >