fentec-project / CiFEr

Functional encryption library in C
Apache License 2.0
78 stars 24 forks source link

my question about ABE #21

Closed exmarine closed 4 years ago

exmarine commented 4 years ago

Please forgive me for my poor English.I am a student in Asian. How can I get character string from FP12_BN254 ? I use the function "extern void FP12_BN254_toOctet(octet S,FP12_BN254 x);". But I cannot get the character string what I use the function'extern void FP12_BN254_fromOctet(FP12_BN254 x,octet S);' to input.

tilenmarc commented 4 years ago

Hi, the function you mention and the structs octet and FP12_BN254 are part of AMCL library that we are using in our ABE schemes. The function FP12_BN254_fromOctet(FP12_BN254 *x,octet *S) is the right choice, since it saves an element x of FP12_BN254 to octet S. Now you have to understand how octet is defined: typedef struct { int len; /**< length in bytes */ int max; /**< max length allowed - enforce truncation */ char *val; /**< byte array */ } octet; This means that if you want a string out of octet S, you have to call S.val (this is a string of length S.len). Hope this helps. Kind regards, Tilen

tilenmarc commented 4 years ago

I am closing the issue since I believe it was answered and there is no additional questions from exmarine.

emmanuelthesis commented 4 years ago

please, can you give an example of how to use this: For example i want to encrypt a string: "helloworld" and get the string back after decryption.

tilenmarc commented 4 years ago

Hey, you will need some library for symmetric encryption (for example libgcrypt). Based on your previous questions you want to use it together with FAME. You can think of ABE scheme as a key management scheme. What you need to do is:

emmanuelthesis commented 4 years ago

Thanks for the reply. The ABE scheme is majorly for key management. It encrypt a message FP12_BN254 msg based on the example given. I tried to apply the serialization example given to change it to string but do not know how to do that. Actually what i what to do next is sending a string as a message and decrypt it to get the string back. i.e for example, the message to be encrypted will be char *msg = "I want to encrypt and decrypt you with the key management" and after decryption, i should be able to get the string back. please maybe one or two line of code explaining this will solve the problem like how you did for me when i was unable to rightly apply the serialization. Thanks

tilenmarc commented 4 years ago

As I said, to encrypt a char *msg = "I want to encrypt and decrypt you with the key management" you need a library for symmetric cryptography. I cannot give you a code example if I don't know which library will you be using. But for example if you wil be using libhydrogen, you can check this link. Instead of creating the secret key with their function, you should create it from FP12_BN254 in a way I wrote above.

emmanuelthesis commented 4 years ago

Thanks for your guidance. I appreciate all your effort so far. i am sorry for giving you much stress. I reviewed the two library you sent to me. I decided to use libhydrogen. I did implement is as follows but not getting expected result. my c programming knowledge not so good. cfe_ser buf4;
hydro_secretbox_keygen(key); char MESSAGE = "Helloworld"; size_t MESSAGE_LEN = strlen(MESSAGE); uint8_t ciphertext[CIPHERTEXT_LEN]; hydro_secretbox_keygen(key); hydro_secretbox_encrypt(ciphertext, MESSAGE, MESSAGE_LEN, 0, CONTEXT, key); cfe_FP12_BN254_ser((FP12_BN254 )ciphertext, &buf4);

and use cfe_fame_encrypt(&cipher, (FP12_BN254 *)buf4.ser, &msp, &pk2, &fame);

and at the receiver end:

error = cfe_fame_decrypt(&decryption, &cipher2, &keys, &fame);    
if (error==0)
{
    printf("Decryption successful \n");
    cfe_ser buf3;
    cfe_FP12_BN254_read(&decryption, &buf3);
    char decrypted[11];
    hydro_secretbox_decrypt(decrypted, buf3.ser, CIPHERTEXT_LEN, 0, CONTEXT, key);
    printf("%s", decrypted);

but not working.

tilenmarc commented 4 years ago

Hi, you are not following the instructions I wrote you. Your code has a lot of mistakes. At the encryptor:

At the decryptor:

emmanuelthesis commented 4 years ago

Hello, Thanks for your guidance, it works perfectly okay now.

tilenmarc commented 4 years ago

Great!