Envelope problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Envelope problem

Robert Burner Schadek
Hello all,

I'm trying to follow the guide shown in
https://wiki.openssl.org/index.php/EVP_Asymmetric_Encryption_and_Decryption_of_an_Envelope

But for the life of me I can't figure out how to do the following:

```
struct Data {
    unsigned char* data;
    unsigned long length;
};

struct KeyArray {
    EVP_PKEY** keys;
    unsigned long numberKeys;
};

/* When Data is written to disk the openssl cli should be
 * able to decrypt the resulting file with something like
 * $ openssl rsautl -decrypt -in ENCRYPTED -out PLAINTEXT -inkey
keys/privkey.pem
 */
Data encryptWithMultiplePublicKeys(Data input, KeyArray keys) {
    // WHAT GOES HERE?
}

Data decryptWithSinglePublicKey(Data input, EVP_PKEY* key) {
    // WHAT GOES HERE?
}

int main() {
    unsigned char* dataToEncrypt = (unsigned char*)"whatever";
    Data data;
    data.data = dataToEncrypt;
    dtat.length = strlen(dataToEncrypt);

    KeyArray ka = fillKeyArrayFromSomewhere();
   
    Data encryptedData = encryptWithMultiplePublicKeys(data, kArray);

    // see the requirement for this file in the comment above
    FILE* f = fopen("ENCRYPTED", "w");
    fwrite(encryptedData, 1, encryptedData.length, f);
    fclose(f);

    Data decryptedData = decryptWithSinglePublicKey(encryptedData,
ka.keys[rand() % ka.numberKeys]);

    // this assert should be true
    assert(strcmp(dataToEncrypt, decryptedData.data));

    return 0;
}

```

The parts I can't figure out are the contents of the two empty functions
in the example.

I tried working out how the openssl cli does this, but I couldn't.

Help would be much appreciated.

Best regards
Robert
Reply | Threaded
Open this post in threaded view
|

Re: Envelope problem

Robert Burner Schadek
Well,

I used the openssl cli program, and cut everything out that wasn't required.
The results can be seen here.
https://gist.github.com/burner/4d87d1421e39627f84316bc2892e6103