Please reply me too, I don't have openssl-users subscription.
I want to create a crypto objects using RSA keys so that I can get cipher
> update and final kind of behavior.
> I have written this code and it works fine on one machine.
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> #include <openssl/evp.h>
> #include <openssl/pem.h>
> #include <openssl/rsa.h>
> #include <openssl/err.h>
>
>
> int main(int argc, char *argv[])
> {
>
> RSA *rsa=NULL,*rsaPvt=NULL,*rsaPub=NULL;
> EVP_PKEY *evpPvt=NULL;
> EVP_PKEY *evpPub=NULL;
>
>
>
> BIGNUM *e=NULL;
>
> rsa=RSA_new();
> e = BN_new();BN_set_word(e, 65537);
>
> RSA_generate_key_ex(rsa,2046,e,NULL);
>
> rsaPub=RSAPublicKey_dup(rsa);
> rsaPvt=RSAPrivateKey_dup(rsa);
>
>
> //evp object
> evpPvt = (EVP_PKEY*) EVP_PKEY_new();
> EVP_PKEY_assign_RSA( (EVP_PKEY*) evpPvt,rsaPvt);
>
> evpPub= (EVP_PKEY*) EVP_PKEY_new();
> EVP_PKEY_assign_RSA( (EVP_PKEY*)evpPub,rsaPub);
>
>
>
> unsigned char
> data[1024]="ASDsdasdsdsdasdsadadsadsadasdsadasddasdadasdasdasdasddasdasdsdasdsadasdsadasdsaddasddasdasdsadasdasdsadasdasdasdsadsadasdasdasdsaddasdasdasdasdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddasdsadasd";
> unsigned char *enc_out=(unsigned char*) malloc(1024 +
> EVP_MAX_IV_LENGTH);
> unsigned char *dec_out=(unsigned char*) malloc(1024 +
> EVP_MAX_IV_LENGTH);
>
> unsigned char *enc_out1=enc_out;
> unsigned char *dec_out1=dec_out;
>
> unsigned int enc_out_len=1024 + EVP_MAX_IV_LENGTH;
> unsigned int dec_out_len=1024 + EVP_MAX_IV_LENGTH;
> int temp=0;
> unsigned int total_out=0;
>
>
> unsigned char *ek;
> int eklen;
> unsigned char iv[EVP_MAX_IV_LENGTH];
>
> EVP_CIPHER_CTX ctx;
>
> EVP_CIPHER_CTX_init(&ctx);
> eklen=EVP_PKEY_size(evpPub);
> ek = (unsigned char*) malloc(eklen);
>
> if (!EVP_SealInit(&ctx, EVP_aes_128_ecb(), &ek, &eklen, iv, &evpPub,
> 1))
> {
> fprintf(stderr, "EVP_SealInit: failed.\n");
> }
>
> temp=1024 + EVP_MAX_IV_LENGTH;
>
> if (!EVP_SealUpdate(&ctx, enc_out, &temp, data, 1024))
> {
> fprintf(stderr, "EVP_SealUpdate: failed.\n");
>
> }
> total_out+=temp;
> enc_out=enc_out+temp;
> temp=enc_out_len-temp;
> if (!EVP_SealFinal(&ctx, enc_out, &temp))
> {
> fprintf(stderr, "EVP_SealFinal: failed.\n");
> }
> total_out+=temp;
>
> EVP_CIPHER_CTX_cleanup(&ctx);
>
> enc_out=enc_out1;
>
> EVP_CIPHER_CTX_init(&ctx);
>
> if (!EVP_OpenInit(&ctx, EVP_aes_128_ecb(), ek, eklen, iv,evpPvt))
> {
> fprintf(stderr, "EVP_SealInit: failed.\n");
> }
>
> temp=1024 + EVP_MAX_IV_LENGTH;
>
> if (!EVP_OpenUpdate(&ctx, dec_out, &temp, enc_out, total_out))
> {
> fprintf(stderr, "EVP_SealUpdate: failed.\n");
>
> }
> total_out=0;
> total_out+=temp;
> dec_out=dec_out+temp;
> temp=dec_out_len-temp;
>
> if (!EVP_OpenFinal(&ctx, dec_out, &temp))
> {
> fprintf(stderr, "EVP_SealFinal: failed.\n");
> }
> total_out+=temp;
>
> dec_out=dec_out1;
> EVP_CIPHER_CTX_cleanup(&ctx);
>
>
> }
>
> But now problem is if I transfer the encrypted data to other machine then
> how other machine knows about the key (ek) used in EVP_OpenInit to create
> the decryption context . Some how I need to transfer this (ek) to other
> side. But first place why does it need to specify the encryption algorithm
> internally it should use the appropriate
> RSA cipher algo.
>
> Other this in other crypto system like cryptopp, library internally uses
> its own crypto algorithm. But here we need to specify this. Why it is so?
>
> If it needs it which algorithm should I use for the RSA encyption. So that
> other RSA cipher implementation understands this.
>
> Also tell me Is it the correct way of doing it or some other way I should
> do it.
>
> I know about this implement ion but I don't want to use this.
>
> int RSA_public_encrypt(int flen, unsigned char *from,
> unsigned char *to, RSA *rsa, int padding);
> int RSA_private_decrypt(int flen, unsigned char *from,
> unsigned char *to, RSA *rsa, int padding);
> int RSA_private_encrypt(int flen, unsigned char *from,
> unsigned char *to, RSA *rsa,int padding);
> int RSA_public_decrypt(int flen, unsigned char *from,
> unsigned char *to, RSA *rsa,int padding);
>
>
> Please help.
>
> Thanks
> -Trilok
>
>
>
>