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;
}
|