diff --git a/CMakeLists.txt b/CMakeLists.txt index bfceb387b4773c304c1c11d3cdeaa27dedfa33ac..66fad940efe55226e7e32568dfcfba7593af2c43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,13 +4,21 @@ cmake_minimum_required(VERSION 2.8) set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_COLOR_MAKEFILE ON) set(CMAKE_C_STANDARD 11) -set(SUBMODULES_NO_BUILD ON) - -add_subdirectory(libdap) +#set(SUBMODULES_NO_BUILD ON) + +if(NOT (${SUBMODULES_NO_BUILD} MATCHES ON)) + if (NOT (TARGET dap_core)) + add_subdirectory(libdap) + target_compile_options( + dap_core PRIVATE + "-fpic" + ) + endif() +endif() add_subdirectory(libdap-crypto) -file(GLOB CRYPTO_SRCS src/*.c) -file(GLOB CRYPTO_HEADERS include/*.h) +file(GLOB CRYPTO_PYTHON_SRCS src/*.c) +file(GLOB CRYPTO_PYTHON_HEADERS include/*.h) set(Python_ADDITIONAL_VERSIONS 3.7) find_package (PythonLibs REQUIRED) @@ -18,7 +26,7 @@ find_package (PythonLibs REQUIRED) #pkg_check_modules(PC_JSON-C REQUIRED json-c) include_directories(${PYTHON_INCLUDE_DIR} include/) -add_library(${PROJECT_NAME} SHARED ${CRYPTO_SRCS} ${CRYPTO_HEADERS}) +add_library(${PROJECT_NAME} SHARED ${CRYPTO_PYTHON_SRCS} ${CRYPTO_PYTHON_HEADERS}) target_link_libraries(${PROJECT_NAME}) @@ -34,6 +42,8 @@ target_compile_options( target_link_libraries(${PROJECT_NAME} dap_core dap_crypto) +target_include_directories(${PROJECT_NAME} PUBLIC include/ ) + #if(BUILD_DAP_PYTHON_TESTS) # add_subdirectory(test) # enable_testing() diff --git a/include/libdap-crypto-python.h b/include/libdap-crypto-python.h index d08e40119a00b1061d1787c4566849207c02d3b8..3d576303c4c2b07416ea09c0c8dacb07538e7ddf 100644 --- a/include/libdap-crypto-python.h +++ b/include/libdap-crypto-python.h @@ -16,57 +16,95 @@ extern "C" { #undef LOG_TAG #define LOG_TAG "libdap-python-crypto" -static PyObject *dap_crypto_init(PyObject *self, PyObject *args); +typedef struct PyCrypto{ + PyObject_HEAD +} PyCryptoObject; -static PyObject *dap_crypto_deinit(); - -static PyObject *dap_log_it_debug(PyObject *self, PyObject *args); - -static PyObject *dap_log_it_info(PyObject *self, PyObject *args); +int dap_crypto_init(void); +void dap_crypto_deinit(void); static PyMethodDef DapCryptoMethods[] = { - {"init", dap_crypto_init, METH_VARARGS, "Initialization of the DAP (Deus Applicaions Prototypes) crypto library"}, - {"deinit", dap_crypto_deinit, METH_NOARGS, "Deinitialization of the DAP (Deus Applicaions Prototypes) crypto library"}, - {"encodeBase58", dap_encode_base58_py, METH_VARARGS, "Encrypts information using the base58 algorithm from the DAP crypto library"}, - {"decodeBase58", dap_decode_base58_py, METH_VARARGS, "Dencrypts information using the base58 algorithm from the DAP crypto library"}, - {"encodeBase64", dap_encode_base64_py, METH_VARARGS, "Encrypts information using the base64 algorithm from the DAP crypto library"}, - {"decodeBase64", dap_decode_base64_py, METH_VARARGS, "Dencrypts information using the base64 algorithm from the DAP crypto library"}, - {"logItDebug", dap_log_it_debug, METH_VARARGS, ""}, - {"logItInfo", dap_log_it_info, METH_VARARGS, ""}, - {"newKey", dap_enc_key_new_py, METH_VARARGS, "The function creates a new key, and returns it with PyObject."}, - {"delKey", dap_enc_key_delete_py, METH_VARARGS, ""}, - {"generateNewKey", dap_enc_key_new_generate_py, METH_VARARGS, ""}, - {"getEncSizeKey", dap_enc_key_get_enc_size_py, METH_VARARGS, ""}, - {"getDecSizeKey", dap_enc_key_get_dec_size_py, METH_VARARGS, ""}, + {"encodeBase58", (PyCFunction)dap_encode_base58_py, METH_VARARGS | METH_STATIC, "Encrypts information using the base58 algorithm from the DAP crypto library"}, + {"decodeBase58", (PyCFunction)dap_decode_base58_py, METH_VARARGS | METH_STATIC, "Dencrypts information using the base58 algorithm from the DAP crypto library"}, + {"encodeBase64", (PyCFunction)dap_encode_base64_py, METH_VARARGS | METH_STATIC, "Encrypts information using the base64 algorithm from the DAP crypto library"}, + {"decodeBase64", (PyCFunction)dap_decode_base64_py, METH_VARARGS | METH_STATIC, "Dencrypts information using the base64 algorithm from the DAP crypto library"}, + {"newKey", (PyCFunction)dap_enc_key_new_py, METH_VARARGS | METH_STATIC, "The function creates a new key, and returns it with PyObject."}, + {"delKey", (PyCFunction)dap_enc_key_delete_py, METH_VARARGS | METH_STATIC, ""}, + {"generateNewKey", (PyCFunction)dap_enc_key_new_generate_py, METH_VARARGS | METH_STATIC, ""}, + {"getEncSizeKey", (PyCFunction)dap_enc_key_get_enc_size_py, METH_VARARGS | METH_STATIC, ""}, + {"getDecSizeKey", (PyCFunction)dap_enc_key_get_dec_size_py, METH_VARARGS | METH_STATIC, ""}, /*IAES256*/ - {"newKeyIAES", dap_enc_iaes_key_new_py, METH_VARARGS, ""}, - {"deleteKeyIAES", dap_enc_iaes_key_delete_py, METH_VARARGS, ""}, - {"generateKeyIAES", dap_enc_iaes_key_generate_py, METH_VARARGS, ""}, - {"encodeSizeIAES256", dap_enc_iaes256_calc_encode_size_py, METH_VARARGS, ""}, - {"decodeSizeIAES256", dap_enc_iaes256_calc_decode_size_py, METH_VARARGS, ""}, - {"encryptIAES256CBCFast", dap_enc_iaes256_cbc_encrypt_fast_py, METH_VARARGS, ""}, - {"decryptIAES256CBCFast", dap_enc_iaes256_cbc_decrypt_fast_py, METH_VARARGS, ""}, + {"newKeyIAES", (PyCFunction)dap_enc_iaes_key_new_py, METH_VARARGS | METH_STATIC, ""}, + {"deleteKeyIAES", (PyCFunction)dap_enc_iaes_key_delete_py, METH_VARARGS | METH_STATIC, ""}, + {"generateKeyIAES", (PyCFunction)dap_enc_iaes_key_generate_py, METH_VARARGS | METH_STATIC, ""}, + {"encodeSizeIAES256", (PyCFunction)dap_enc_iaes256_calc_encode_size_py, METH_VARARGS | METH_STATIC, ""}, + {"decodeSizeIAES256", (PyCFunction)dap_enc_iaes256_calc_decode_size_py, METH_VARARGS | METH_STATIC, ""}, + {"encryptIAES256CBCFast", (PyCFunction)dap_enc_iaes256_cbc_encrypt_fast_py, METH_VARARGS | METH_STATIC, ""}, + {"decryptIAES256CBCFast", (PyCFunction)dap_enc_iaes256_cbc_decrypt_fast_py, METH_VARARGS | METH_STATIC, ""}, /*OAES*/ - {"newKeyOAES", dap_enc_oaes_key_new_py, METH_VARARGS, ""}, - {"deleteKeyOAES", dap_enc_oaes_key_delete_py, METH_VARARGS, ""}, - {"generateKeyOAES", dap_enc_oaes_key_generate_py, METH_VARARGS, ""}, - {"encodeSizeOAES", dap_enc_oaes_calc_encode_size_py, METH_VARARGS, ""}, - {"decodeSizeOAES", dap_enc_oaes_calc_decode_size_py, METH_VARARGS, ""}, - {"encryptOAESFast", dap_enc_oaes_encrypt_fast_py, METH_VARARGS, ""}, - {"decryptOAESFast", dap_enc_oaes_decrypt_fast_py, METH_VARARGS, ""}, + {"newKeyOAES", (PyCFunction)dap_enc_oaes_key_new_py, METH_VARARGS | METH_STATIC, ""}, + {"deleteKeyOAES", (PyCFunction)dap_enc_oaes_key_delete_py, METH_VARARGS | METH_STATIC, ""}, + {"generateKeyOAES", (PyCFunction)dap_enc_oaes_key_generate_py, METH_VARARGS | METH_STATIC, ""}, + {"encodeSizeOAES", (PyCFunction)dap_enc_oaes_calc_encode_size_py, METH_VARARGS | METH_STATIC, ""}, + {"decodeSizeOAES", (PyCFunction)dap_enc_oaes_calc_decode_size_py, METH_VARARGS | METH_STATIC, ""}, + {"encryptOAESFast", (PyCFunction)dap_enc_oaes_encrypt_fast_py, METH_VARARGS | METH_VARARGS | METH_STATIC, ""}, + {"decryptOAESFast", (PyCFunction)dap_enc_oaes_decrypt_fast_py, METH_VARARGS | METH_STATIC, ""}, {NULL, NULL, 0, NULL} }; -static struct PyModuleDef dapcryptomodule = { - PyModuleDef_HEAD_INIT, - "libdap_crypto_python_module", /* name of module */ - NULL, /* module documentation, may be NULL */ - -1, /* size of per-interpreter state of the module, - or -1 if the module keeps state in global variables. */ - DapCryptoMethods +PyTypeObject dapCrypto_dapCryptoType = { + PyVarObject_HEAD_INIT(NULL, 0) + "libCellFrame.crypto", /* tp_name */ + sizeof(PyCryptoObject), /* tp_basicsize */ + 0, /* tp_itemsize */ + 0,//(destructor)Noddy_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_reserved */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_BASETYPE, /* tp_flags */ + "Dap crypto objects", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + DapCryptoMethods,//Noddy_methods, /* tp_methods */ + 0,//Noddy_members, /* tp_members */ + 0,//Noddy_getseters, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0,//(initproc)PyDapEventsObject_init,//(initproc)Noddy_init, /* tp_init */ + 0, /* tp_alloc */ + PyType_GenericNew,//Noddy_new, /* tp_new */ }; -PyMODINIT_FUNC PyInit_libdap_crypto_python_module(void); + +//static struct PyModuleDef dapcryptomodule = { +// PyModuleDef_HEAD_INIT, +// "libdap_crypto_python_module", /* name of module */ +// NULL, /* module documentation, may be NULL */ +// -1, /* size of per-interpreter state of the module, +// or -1 if the module keeps state in global variables. */ +// DapCryptoMethods +//}; + +//PyMODINIT_FUNC PyInit_libdap_crypto_python_module(void); #ifdef __cplusplus } diff --git a/src/libdap-crypto-python.c b/src/libdap-crypto-python.c index 74303fd76fa03f4806cbf52fcd17f3fc1abfb182..cff1369c0ed64610d5a3fb0a829cfe5515cf8f37 100644 --- a/src/libdap-crypto-python.c +++ b/src/libdap-crypto-python.c @@ -1,68 +1,52 @@ #include "libdap-crypto-python.h" -static PyObject* dap_crypto_init(PyObject *self, PyObject *args){ - dap_common_init("libdap-crypto","libdap-crypto-python-module.txt"); +int dap_crypto_init(void){ if(dap_enc_init()!=0){ log_it(L_CRITICAL,"Can't init encryption module"); - return PyLong_FromLong(-1); + return -1; } if(dap_enc_key_init()!=0){ log_it(L_CRITICAL,"Can't init encryption key module"); - return PyLong_FromLong(-2); + return -2; } keys = key_list_init(); keys_iaes = keys; keys_oaes = keys; - return PyLong_FromLong(0); + return 0; } -static PyObject* dap_crypto_deinit(){ +void dap_crypto_deinit(void){ dap_enc_key_deinit(); dap_enc_deinit(); key_list_free(keys); - return PyLong_FromLong(0); } -/* Information */ -static PyObject *dap_log_it_debug(PyObject *self, PyObject *args){ - const char *data; - if (!PyArg_ParseTuple(args,"s", &data)){ - return NULL; - } - log_it(L_DEBUG, data); - return PyLong_FromLong(0); -} -static PyObject *dap_log_it_info(PyObject *self, PyObject *args){ - return PyLong_FromLong(0); -} -/*==========================================*/ +//PyMODINIT_FUNC PyInit_libdap_crypto_python_module(void){ +// return PyModule_Create(&dapcryptomodule); +//} -PyMODINIT_FUNC PyInit_libdap_crypto_python_module(void){ - return PyModule_Create(&dapcryptomodule); -} +//int main(int argc, char **argv) { +// wchar_t *program = Py_DecodeLocale(argv[0], NULL); +// if (program == NULL) { +// fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); +// exit(1); +// } -int main(int argc, char **argv) { - wchar_t *program = Py_DecodeLocale(argv[0], NULL); - if (program == NULL) { - fprintf(stderr, "Fatal error: cannot decode argv[0]\n"); - exit(1); - } - - /* Add a built-in module, before Py_Initialize */ - PyImport_AppendInittab("libdap_crypto_python_module", PyInit_libdap_crypto_python_module); +// /* Add a built-in module, before Py_Initialize */ +// PyImport_AppendInittab("libdap_crypto_python_module", PyInit_libdap_crypto_python_module); - /* Pass argv[0] to the Python interpreter */ - Py_SetProgramName(program); +// /* Pass argv[0] to the Python interpreter */ +// Py_SetProgramName(program); - /* Initialize the Python interpreter. Required. */ - Py_Initialize(); +// /* Initialize the Python interpreter. Required. */ +// Py_Initialize(); - /* Optionally import the module; alternatively, - import can be deferred until the embedded script - imports it. */ - PyImport_ImportModule("libdap_crypto_python_module"); +// /* Optionally import the module; alternatively, +// import can be deferred until the embedded script +// imports it. */ +// PyImport_ImportModule("libdap_crypto_python_module"); - PyMem_RawFree(program); - return 0; -} +// PyMem_RawFree(program); +// return 0; +//}