简体   繁体   中英

Using python to send unsigned char

I am doing a project related to cryptography. I am building the code in python . But the crypto functions use a C library PBC for computations. Hence I have made a C program that contains just the right functions and I have compiled it as a shared library. I import the library using ctypes . All has been well so far.

The problem that I face now is that I need to take an unsigned char declared as a global variable in the library and I need to send it through the python socket to another receiving python socket, where it will be passed back as an unsigned char to the library.

Right now, when I try to retrieve the unsigned char from the library,

ibc = cdll.LoadLibrary('./libibc.so.1.0.1')
strin = 'some string'
ibc.init_pairing(1, 1, 1)# Initializing the lib
ibc.read_share()
id_str = (c_char * 40)()
id_str.value = strin
ibc.hash_id_s(id_str)    # Passing a char to a function

hsid = (c_ubyte * 1000)()
hsid = c_ubyte.in_dll(ibc, "hid")
ibc.gen_privatekey(hsid, 1, 3) #pass unsigned char to function

The last line, ends in a segmentation fault. So I called the gen_privatekey function from c itself, using the following code :

int genprkey(){
    unsigned char key[100];
    element_to_bytes(key, pks);
    gen_privatekey(key, 1, 2);
}

I then call this function from python using :

ibc.genprkey()

and the program succeeds.

PROBLEM

The above function generates an unsigned char which will also be stored in another global variable. I tried to retrieve the variable thus :

hsidp = (c_char_p)()
hsidp = c_char_p.in_dll(ibc, "hid")

I tried the above code on the python interpreter. When I type :

hsidp

output is :

c_char_p(10598250801977757708)

But when I type :

hsidp.value

I get a segmentation fault (core dumped) . I suspect that my way of handling the variable is not correct. Once I get the unsigned char as a variable in python, I need to send it as a message to another socket that will read it and pass it as an unsigned char to the same library.

I don't have an idea how this will be possible. Please help

Here's how to access an array in a shared library:

Dummy library:

>>> from ctypes import *
>>> import os

>>> open('lib.c', 'w').write(
...   'unsigned char data[] = {0, 1, 2, 3};\n')
>>> ret = os.system('gcc -shared -o lib.so lib.c')

Load the library and access a length 4 array of unsigned char

>>> lib = CDLL('./lib.so')
>>> data = (c_ubyte * 4).in_dll(lib, 'data')
>>> list(data)
[0, 1, 2, 3]

Also, c_char_p is a pointer, and getting that from the first 4 or 8 bytes of the array will point who knows where and thus likely segfault when you try to dereference it. Plus the first statement below instantiates a pointer that gets deallocated immediately when you reassign hsidp in the 2nd statement:

hsidp = (c_char_p)()   
hsidp = c_char_p.in_dll(ibc, "hid")

Likewise the first line below creates a length 1000 array and throws it away. I don't know why. Then you get a single byte from the library.

hsid = (c_ubyte * 1000)()
hsid = c_ubyte.in_dll(ibc, "hid")

Do you need a byte or an array of bytes? What length? Or does gen_privatekey want a pointer to a null-terminated string?


You can use string_at to get a Python str object:

>>> hid = (c_ubyte * 24)(*map(ord, "This is a test."))
>>> string_at(hid, 24)
'This is a test.\x00\x00\x00\x00\x00\x00\x00\x00\x00'

Or you can create a c_char array from the buffer (or just use that from the outset instead of a c_ubyte array):

>>> (c_char * 24).from_buffer(hid).value
'This is a test.'

A c_char array is more convenient for working with strings because of its value and raw descriptors that return Python str objects. These descriptors also allow you to write a string to the buffer:

>>> hid = (c_char * 24)()
>>> hid.value = 'This is a test.'
>>> hid.value
'This is a test.'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM