Help with error codes in RSA code

Oct 17, 2019 at 7:24pm
I am trying to make a code that tests the throughput performance of RSA with given parameters. I'm getting a handful of errors that I'm not sure how to fix. If you guys could help or point me in the right direction, that would be great.

Error Codes:
rsa1.cpp: In function ‘int main()’:
rsa1.cpp:25:68: warning: ‘RSA* RSA_generate_key(int, long unsigned int, void (*)(int, int, void*), void*)’ is deprecated [-Wdeprecated-declarations]
r = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
^
In file included from /usr/include/openssl/rsa.h:13:0,
from rsa1.cpp:1:
/usr/include/openssl/rsa.h:234:1: note: declared here
DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void
^
rsa1.cpp:33:21: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
pri_key = malloc(pri_len + 1);
~~~~~~^~~~~~~~~~~~~
rsa1.cpp:34:21: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
pub_key = malloc(pub_len + 1);
~~~~~~^~~~~~~~~~~~~
rsa1.cpp:48:21: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
encrypt = malloc(RSA_size(keypair));
~~~~~~^~~~~~~~~~~~~~~~~~~
rsa1.cpp:50:17: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
err = malloc(130);
~~~~~~^~~~~
rsa1.cpp:68:25: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
encrypt = malloc(RSA_size(keypair));
~~~~~~^~~~~~~~~~~~~~~~~~~
rsa1.cpp:75:21: error: invalid conversion from ‘void*’ to ‘char*’ [-fpermissive]
decrypt = malloc(encrypt_len);
~~~~~~^~~~~~~~~~~~~
rsa1.cpp:87:5: error: jump to label ‘free_stuff’ [-fpermissive]
free_stuff:
^~~~~~~~~~
rsa1.cpp:56:14: note: from here
goto free_stuff;
^~~~~~~~~~
rsa1.cpp:60:15: note: crosses initialization of ‘FILE* out’
FILE *out = fopen("out.bin", "w");
^~~


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <stdio.h>
#include <string.h>
#include <time.h>  
#define KEY_LENGTH 768 
#define PUB_EXP 65537
#define PRINT_KEYS
#define WRITE_TO_FILE
int main(void) {
    size_t pri_len;            // Length of private key
    size_t pub_len;            // Length of public key
    char   *pri_key;           // Private key
    char   *pub_key;           // Public key
    char   msg[KEY_LENGTH/8] = "hello";  // Message to encrypt
    char   *encrypt = NULL;    // Encrypted message
    char   *decrypt = NULL;    // Decrypted message
    char   *err;               // Buffer for any error messages
    clock_t begin, end; 
    double time_spent; begin = clock();  
    // Generate key pair
    printf("Generating RSA (%d bits) keypair...", KEY_LENGTH);
    fflush(stdout);
    RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
    // To get the C-string PEM form:
    BIO *pri = BIO_new(BIO_s_mem());
    BIO *pub = BIO_new(BIO_s_mem());
    PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
    PEM_write_bio_RSAPublicKey(pub, keypair);
    pri_len = BIO_pending(pri);
    pub_len = BIO_pending(pub);
    pri_key = malloc(pri_len + 1);
    pub_key = malloc(pub_len + 1);
    BIO_read(pri, pri_key, pri_len);
    BIO_read(pub, pub_key, pub_len);
    pri_key[pri_len] = '\0';
    pub_key[pub_len] = '\0';
    #ifdef PRINT_KEYS
        printf("\n%s\n%s\n", pri_key, pub_key);
    #endif
    printf("done.\n");
    // Get the message to encrypt
   // printf("Message to encrypt: ");
   // fgets(msg, KEY_LENGTH-1, stdin);
   // msg[strlen(msg)-1] = '\0';
    // Encrypt the message
    encrypt = malloc(RSA_size(keypair));
    int encrypt_len;
    err = malloc(130);
    if((encrypt_len = RSA_public_encrypt(strlen(msg)+1, (unsigned char*)msg, (unsigned char*)encrypt,
                                         keypair, RSA_PKCS1_OAEP_PADDING)) == -1) {
        ERR_load_crypto_strings();
        ERR_error_string(ERR_get_error(), err);
        fprintf(stderr, "Error encrypting message: %s\n", err);
        goto free_stuff;
    }
    #ifdef WRITE_TO_FILE
    // Write the encrypted message to a file
        FILE *out = fopen("out.bin", "w");
        fwrite(encrypt, sizeof(*encrypt),  RSA_size(keypair), out);
        fclose(out);
        printf("Encrypted message written to file.\n");
        free(encrypt);
        encrypt = NULL;
        // Read it back
        printf("Reading back encrypted message and attempting decryption...\n");
        encrypt = malloc(RSA_size(keypair));
        out = fopen("out.bin", "r");

        fread(encrypt, sizeof(*encrypt), RSA_size(keypair), out);
        fclose(out);
    #endif
    // Decrypt it
    decrypt = malloc(encrypt_len);
    if(RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt,
                           keypair, RSA_PKCS1_OAEP_PADDING) == -1) {
        ERR_load_crypto_strings();
        ERR_error_string(ERR_get_error(), err);
        fprintf(stderr, "Error decrypting message: %s\n", err);
        goto free_stuff;
    }
    printf("Decrypted message: %s\n", decrypt);
    end = clock(); 
    time_spent = (double)(end - begin) / CLOCKS_PER_SEC; 
    printf("time spent %f\n", time_spent); 
    free_stuff:
    RSA_free(keypair);
    BIO_free_all(pub);
    BIO_free_all(pri);
    free(pri_key);
    free(pub_key);
    free(encrypt);
    free(decrypt);
    free(err);
    return 0;
}
Oct 17, 2019 at 7:29pm
Have you considered using a C compiler instead of a C++ compiler?

Since you're using a C++ compiler you need to cast the return value from malloc() or perhaps change the malloc calls to new/delete.



Oct 17, 2019 at 7:53pm
there is likely a c++ version of it somewhere on the web too, given what it is.
Oct 18, 2019 at 3:01am
I tried using a C compiler instead and I now receive this error code:

rsa2.c: In function ‘main’:
rsa2.c:29:5: warning: ‘RSA_generate_key’ is deprecated [-Wdeprecated-declarations]
RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
^~~
In file included from /usr/include/openssl/rsa.h:13:0,
from rsa2.c:1:

and I have absolutely no idea how to fix that. In addition, I get a handful of undefined reference errors as well.
Oct 18, 2019 at 3:05am
That's a warning not an error. Search the documentation of whatever library that is to see why it's deprecated. Someone might be able to help more if you post the actual error messages.
Last edited on Oct 18, 2019 at 3:06am
Oct 18, 2019 at 3:07am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
rsa3.c: In function ‘main’:
rsa3.c:25:5: warning: ‘RSA_generate_key’ is deprecated [-Wdeprecated-declarations]
     RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);
     ^~~
In file included from /usr/include/openssl/rsa.h:13:0,
                 from rsa3.c:1:
/usr/include/openssl/rsa.h:234:1: note: declared here
 DEPRECATEDIN_0_9_8(RSA *RSA_generate_key(int bits, unsigned long e, void
 ^
/tmp/cchcqBUZ.o: In function `main':
rsa3.c:(.text+0x6a): undefined reference to `RSA_generate_key'
rsa3.c:(.text+0x76): undefined reference to `BIO_s_mem'
rsa3.c:(.text+0x7e): undefined reference to `BIO_new'
rsa3.c:(.text+0x8a): undefined reference to `BIO_s_mem'
rsa3.c:(.text+0x92): undefined reference to `BIO_new'
rsa3.c:(.text+0xcb): undefined reference to `PEM_write_bio_RSAPrivateKey'
rsa3.c:(.text+0xe8): undefined reference to `PEM_write_bio_RSAPublicKey'
rsa3.c:(.text+0x106): undefined reference to `BIO_ctrl'
rsa3.c:(.text+0x12d): undefined reference to `BIO_ctrl'
rsa3.c:(.text+0x18c): undefined reference to `BIO_read'
rsa3.c:(.text+0x1ae): undefined reference to `BIO_read'
rsa3.c:(.text+0x25a): undefined reference to `RSA_size'
rsa3.c:(.text+0x2b3): undefined reference to `RSA_public_encrypt'
rsa3.c:(.text+0x2d1): undefined reference to `OPENSSL_init_crypto'
rsa3.c:(.text+0x2d6): undefined reference to `ERR_get_error'
rsa3.c:(.text+0x2eb): undefined reference to `ERR_error_string'
rsa3.c:(.text+0x33b): undefined reference to `RSA_size'
rsa3.c:(.text+0x3a9): undefined reference to `RSA_size'
rsa3.c:(.text+0x3e3): undefined reference to `RSA_size'
rsa3.c:(.text+0x44f): undefined reference to `RSA_private_decrypt'
rsa3.c:(.text+0x463): undefined reference to `OPENSSL_init_crypto'
rsa3.c:(.text+0x468): undefined reference to `ERR_get_error'
rsa3.c:(.text+0x47d): undefined reference to `ERR_error_string'
rsa3.c:(.text+0x4cb): undefined reference to `RSA_free'
rsa3.c:(.text+0x4da): undefined reference to `BIO_free_all'
rsa3.c:(.text+0x4e9): undefined reference to `BIO_free_all'
collect2: error: ld returned 1 exit status
 
Last edited on Oct 18, 2019 at 3:07am
Oct 18, 2019 at 3:18am
What are you linking with? A cursory search shows you need to link with -lssl and maybe -lcrypto
Topic archived. No new replies allowed.