Hello all,
Hoping someone can help me here.
I have this function that I use to do DES and Base64
encryption/encoding/decryption/decoding.
When it links against libcrypto.0.9 it works fine.
However when I rebuild it against libcrypto.0.9.7 it doesn't work.
It encrypts but when I got to decrypt, it tells me the decrypt failed.
Can anyone see the problem. I'm desparate. I've spent hours
trying to figure this out.
Thank you for any and all help.
---------------------------------
typedef enum
{
myenc_DECRYPT = 0,
myenc_ENCRYPT = 1
} myenc_t;
//Caller must free the returned string.
//Returns null on error.
char* base64DESEncrypt(myenc_t enc, const char *pass, const char
*data, int *dataSize)
{
IFDEBUG(printf("base64DESEncrypt begin %s\n", pass));
static const char magic[]="Salted__";
char mbuf[8]; /* should be 1 smaller than magic */
unsigned char *buff=NULL;
int bsize=BSIZE;
char* ret=NULL;
int inl;
unsigned char key[24],iv[MD5_DIGEST_LENGTH];
unsigned char salt[PKCS5_SALT_LEN];
char *hkey=NULL,*hiv=NULL;
int base64=0;
int nosalt=0;
const EVP_CIPHER *cipher=NULL;
BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL;
IFDEBUG(
BIO *bio_err=NULL;
if ((bio_err=BIO_new(BIO_s_file())) != NULL)
BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
)
//nosalt = 1;
base64 = 1;
if(!MYENC_IS_INITED)
{ //only add the algorithms once
//SSLeay_add_all_algorithms();
SSLeay_add_all_ciphers();
MYENC_IS_INITED = 1;
}
cipher=EVP_get_cipherbyname("des");
buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
in=BIO_new(BIO_s_mem());
BIO_write(in, data, *dataSize);
out=BIO_new(BIO_s_mem());
if((in == NULL) || (out == NULL))
{
IFDEBUG(ERR_print_errors(bio_err);)
goto end;
}
rbio=in;
wbio=out;
if(base64)
{
if((b64=BIO_new(BIO_f_base64())) == NULL)
goto end;
if(enc == myenc_ENCRYPT)
wbio=BIO_push(b64,wbio);
else
rbio=BIO_push(b64,rbio);
}
if(cipher != NULL)
{
if(pass != NULL)
{
/* Salt handling: if encrypting generate a salt and
* write to output BIO. If decrypting read salt from
* input BIO.
*/
unsigned char *sptr;
if(nosalt)
sptr = NULL;
else
{
if(enc == myenc_ENCRYPT)
{
if(RAND_pseudo_bytes(salt, PKCS5_SALT_LEN)
< 0)
goto end;
/* If -P option then don't bother
writing */
if((BIO_write(wbio,magic,
sizeof magic-1) !=
sizeof magic-1
|| BIO_write(wbio,
(char *)salt,
PKCS5_SALT_LEN) !=
PKCS5_SALT_LEN))
{
IFDEBUG(BIO_printf(bio_err,"error writing
output file\n");)
goto end;
}
}
else if(BIO_read(rbio,mbuf,sizeof mbuf) !=
sizeof mbuf
|| BIO_read(rbio,
(unsigned char *)salt,
PKCS5_SALT_LEN) != PKCS5_SALT_LEN)
{
IFDEBUG(BIO_printf(bio_err,"error reading
input file\n");)
goto end;
}
else if(memcmp(mbuf,magic,sizeof magic-1))
{
IFDEBUG(BIO_printf(bio_err,"bad magic
number\n");)
goto end;
}
sptr = salt;
}
EVP_BytesToKey(cipher,EVP_md5(),sptr,
(unsigned char *)pass,
strlen(pass),1,key,iv);
}
if ((hiv != NULL) && !set_hex(hiv,iv,8))
{
IFDEBUG(BIO_printf(bio_err,"invalid hex iv value\n");)
goto end;
}
if ((hiv == NULL) && (pass == NULL))
{
/* No IV was explicitly set and no IV was generated
* during EVP_BytesToKey. Hence the IV is undefined,
* making correct decryption impossible. */
IFDEBUG(BIO_printf(bio_err, "iv undefined\n");)
goto end;
}
if ((hkey != NULL) && !set_hex(hkey,key,24))
{
IFDEBUG(BIO_printf(bio_err,"invalid hex key value\n");)
goto end;
}
if ((benc=BIO_new(BIO_f_cipher())) == NULL)
goto end;
BIO_set_cipher(benc,cipher,key,iv,enc);
}
/* Only encrypt/decrypt as we write the file */
if (benc != NULL)
wbio=BIO_push(benc,wbio);
for (;;)
{
inl=BIO_read(rbio,(char *)buff,bsize);
if (inl <= 0) break;
if (BIO_write(wbio,(char *)buff,inl) != inl)
{
IFDEBUG(BIO_printf(bio_err,"error writing output
file\n");)
goto end;
}
}
if (!BIO_flush(wbio))
{
IFDEBUG(BIO_printf(bio_err,"bad decrypt\n");)
goto end;
}
char *theBuf;
*dataSize = BIO_get_mem_data(wbio, &theBuf);
char *returnBuf = (char*)malloc(*dataSize);
memcpy(returnBuf, theBuf, *dataSize);
ret=returnBuf;
end:
IFDEBUG(ERR_print_errors(bio_err);)
if (buff != NULL) OPENSSL_free(buff);
if (in != NULL) BIO_free(in);
if (out != NULL) BIO_free_all(out);
if (benc != NULL) BIO_free(benc);
if (b64 != NULL) BIO_free(b64);
IFDEBUG(printf("base64DESEncrypt end\n"));
return ret;
}
int set_hex(char *in, unsigned char *out, int size)
{
int i,n;
unsigned char j;
IFDEBUG_ENCR(BIO *bio_err=NULL;)
n=strlen(in);
if (n > (size*2))
{
IFDEBUG_ENCR(BIO_printf(bio_err,"hex string is too long\n");)
return(0);
}
memset(out,0,size);
for (i=0; i<n; i++)
{
j=(unsigned char)*in;
*(in++)='\0';
if (j == 0) break;
if ((j >= '0') && (j <= '9'))
j-='0';
else if ((j >= 'A') && (j <= 'F'))
j=j-'A'+10;
else if ((j >= 'a') && (j <= 'f'))
j=j-'a'+10;
else
{
IFDEBUG_ENCR(BIO_printf(bio_err,"non-hex digit\n");)
return(0);
}
if (i&1)
out[i/2]|=j;
else
out[i/2]=(j<<4);
}
return(1);
}
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List openssl-users@openssl.org
Automated List Manager [EMAIL PROTECTED]