OpenWrt Forum Archive

Topic: i want to cross compile c code(using openssl) for openwrt

The content of this topic has been archived on 13 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

i want to cross compile c code(using openssl) for openwrt
after i get openwrt code by SVN
i run "make manuconfig"  and selected  libopenssl library
i run "make V=99"

after that execute for compile and get output

lokios@lokios-pc:~/Desktop/ssl$ mipsel-openwrt-linux-gcc rsa_encryp.c 
rsa_encryp.c:8:25: fatal error: openssl/rsa.h: No such file or directory
compilation terminated.
lokios@lokios-pc:~/Desktop/ssl$ mipsel-openwrt-linux-gcc rsa_encryp.c -lcrypto
rsa_encryp.c:8:25: fatal error: openssl/rsa.h: No such file or directory
compilation terminated.

i want to compile this code for run in openwrt(router wrt54gl)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <netinet/in.h>

#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
unsigned char* encrypt (char *message,  RSA *cli_pub_key, long *buffer_size)
{
    unsigned char *encrypted =  (unsigned char *)malloc(RSA_size(cli_pub_key));
    *buffer_size = RSA_public_encrypt(strlen(message), (unsigned char *) message, encrypted, cli_pub_key, RSA_PKCS1_PADDING);
    if(*buffer_size == -1)
    {
        fputs("\nFailed to encrypt data. \n",stderr);
    }
    return encrypted;
}

unsigned char* decrypt (unsigned char *encrypted, RSA *priv_key, long enc_message_length, long original_length)
{
    unsigned char *decrypted = (unsigned char *)malloc(original_length);
  
  int n = RSA_private_decrypt(enc_message_length, encrypted, decrypted, priv_key, RSA_PKCS1_PADDING);
  *(decrypted+7) = '\0';
  printf("N = %d\n",n);
    if(n == -1)
    {
        fputs("\nFailed to decrypt data.\n",stderr);
    }
    return decrypted;
}

RSA* load_pubkey(char *key_file_name)
{
    FILE *keyfile;
    keyfile = fopen(key_file_name, "r");
    if(keyfile == NULL)
    {
        fputs("Failed to load public key file!\n",stderr);
    }

    //Retrieve the size of the file
    struct stat file_status;
    char *file_buffer = NULL;

    stat(key_file_name, &file_status);
    long file_size = file_status.st_size;
    file_buffer = (char *)malloc(file_size); //Allocate memory for the buffer using the size of the file
    fread(file_buffer,1,file_size,keyfile);
    fclose(keyfile);

        BIO *bp = BIO_new_mem_buf(file_buffer,-1);
        RSA *pub_key = PEM_read_bio_RSA_PUBKEY(bp,0,0,0);
        BIO_free(bp);

    free(file_buffer);
    return pub_key;
}

RSA* load_privkey(char *key_file_name)
{
        FILE *priv_key = fopen(key_file_name,"r");
        RSA *rsa_priv_key = PEM_read_RSAPrivateKey(priv_key,NULL,NULL,NULL);
    return rsa_priv_key;
}

int main ()
{

    RSA *pub_key  = load_pubkey("public.key");    /*Load the public key*/
    RSA *rsa_priv_key = load_privkey("private.key"); /*Load the private key*/

    char *message = "ABCDEFG";
    printf("Original Data : %s\n", message);
        long enc_message_length = 0;

    /* Encrypt the message! */
    unsigned char *encrypted = encrypt(message, pub_key, &enc_message_length); //The length of the encrypted buffer is stored in enc_message_length;
    printf("Encrypted Message: %s\n", encrypted);

    /* Decrypt the message &  print ! */
    unsigned char *decrypted = decrypt(encrypted, rsa_priv_key, enc_message_length, strlen(message));
    printf("Decrypted message : %s\n",decrypted);
}

thank you

You need to pass -I and -L flags to specify the location of header files and shared libraries.

BUILDROOT=/home/lokios/openwrt/trunk
mipsel-openwrt-linux-gcc rsa_encryp.c \
    -I$BUILDROOT/staging_dir/target-*/usr/include \
    -L$BUILDROOT/staging_dir/target-*/usr/lib \
    -Wl,-rpath-link=$BUILDROOT/staging_dir/target-*/usr/lib \
    -lcrypto -o encrypt

it's work for cross compile with openssl
thank you

(Last edited by napat1412 on 15 Nov 2011, 08:40)

The discussion might have continued from here.