Forwarded to openssl-users for public discussion.

Best regards,
        Lutz

----- Forwarded message from User User <si.be.ar....@mail.ru> -----

From: User User <si.be.ar....@mail.ru>
To: r...@openssl.org
Subject: Build incorrect crypt/decrypt in Win32. x86. MSVC 2003. MinGW. 
Date: Sun, 18 Oct 2009 07:46:40 +0400
Reply-To: User User <si.be.ar....@mail.ru>

  Hello openssl Hackers.
I am beginner in openssl. Learn for wrote simple crypto_test.cpp  program. Test 
it under CentOS, FreeBSD and Win32.
Linux,BSD work fine, but windows builds uncorrect.
Build in MS VC .Net 2003 and MingGW, both not work in crypt/decrypt cycles 
correctly.
Send code in attach, and openssl makefile and simple description in README.TXT
I can't understand. is it a bug or my build problems.
Please, show right way.
Thank you.
Nick.
------------------------
RESULTS:

--[ FreeBSD ]--
./crypto_test -e plaintext encypt.bsd 123
./crypto_test -d encypt.bsd plaintext.out.bsd 123
./openssl dgst -rmd160 plaintext encypt.bsd plaintext.out.bsd

RIPEMD160(plaintext)            =       d7ca608f0430c3248527572662af5e50d93e87ca
RIPEMD160(encypt.bsd)   =        9a465d7a2304833f0d7a967ce5a04687b7350441
RIPEMD160(plaintext.out.bsd)    =        
d7ca608f0430c3248527572662af5e50d93e87ca

--[ WIN32 ]--
crypto_test.exe -e plaintext encrypt.w32 123
crypto_test.exe -d encrypt.w32 plaintext.out.w32 123
openssl.exe dgst -rmd160 plaintext encrypt.w32 plaintext.out.w32

RIPEMD160(plaintext)            =       d7ca608f0430c3248527572662af5e50d93e87ca
RIPEMD160(encrypt.w32)  =        e1ae968afaa4fa259a4f81012804769fe2d13dd6
RIPEMD160(plaintext.out.w32)    =        
753e049eb8c31e2116e575402db6f7dd7abdbfa1


------------------------] Code of crypto_test.cpp 
#include <stdio.h>
#include <string.h>

#include <openssl/err.h>
#include <openssl/bio.h>
#include <openssl/evp.h>

#define         BUF_SIZE        32767

int enc(char* fin, char* fout, unsigned char* key);
int dec(char* fin, char* fout, unsigned char* key);

int main(int argc, char* argv[])
{
        ERR_load_crypto_strings();
        if(argc<5){
                printf("Need more args:\n\t1 -  -e/-d\n\t2 - file_in\n\t3 - 
file_out\n\t4 - pass\n\t");
                exit(0);
        }

        int ret =0;
        if(!strcmp(argv[1], "-e")){
                ret = enc(argv[2], argv[3], (unsigned char*)argv[4]);
        }else if(!strcmp(argv[1], "-d")){
                ret = dec(argv[2], argv[3], (unsigned char*)argv[4]);
        }else{
                printf("Unknown direction: %s\n",argv[1]);
                exit(0);
        }

        if(!ret)
                printf("Completed\n");
        else
                printf("Error:%d",ret);

        ERR_free_strings();

        return ret;
}

int enc(char* fin, char* fout, unsigned char* key){
        int ret = 0;

        printf("Mode ENCRYPT\n");
        
        BIO *bin = BIO_new_file(fin, "r");

        if(!bin){
                ret = ERR_get_error();
                printf("Decryption failed, 
reason:%s\n",ERR_reason_error_string(ret));
                return ret;
        }

        BIO *bout = BIO_new_file(fout, "w");

        if(!bout)
                return ERR_get_error();

        BIO* cipher = BIO_new(BIO_f_cipher());

        BIO_set_cipher(cipher, EVP_bf_ecb(), key, 0, 1);
        BIO_push(cipher, bout);

        void* buff = malloc(BUF_SIZE+1);
        int rlen = 0, written =0;

        do{
                memset(buff, 0, BUF_SIZE+1);
                if(!(rlen=BIO_read(bin, buff, sizeof(buff))) ){
                        break;
                }

                if( (written = BIO_write(cipher, buff, 
(int)strlen((char*)buff)) ) <=0 ){
                        ret = ERR_get_error();
                        printf("Encryption failed, 
reason:%s\n",ERR_reason_error_string(ret));
                        break;
                }

        }while(1);

        free(buff);
        BIO_flush(cipher);
        BIO_free_all(cipher);

        return ret;
}

int dec(char* fin, char* fout, unsigned char* key){
        int ret = 0;

        printf("Mode DECRYPT\n");
        BIO *bin = BIO_new_file(fin, "r");

        if(!bin)
                return ERR_get_error();

        BIO *bout = BIO_new_file(fout, "w");

        if(!bout)
                return ERR_get_error();

        BIO* cipher = BIO_new(BIO_f_cipher());

        BIO_set_cipher(cipher, EVP_bf_ecb(), key, 0, 0);
        BIO_push(cipher, bin);

        void* buff = malloc(BUF_SIZE+1);
        int rlen = 0, written =0;

        do{
                memset(buff, 0, BUF_SIZE+1);

                if(!(rlen=BIO_read(cipher, buff, sizeof(buff))) ){
                        break;
                }

                if(!BIO_get_cipher_status(cipher)){
                        ret = ERR_get_error();
                        printf("Decryption failed, 
reason:%s\n",ERR_reason_error_string(ret));
                        break;
                }

                if( (written = BIO_write(bout, buff, (int)strlen((char*)buff)) 
) <=0 ){
                        ret = ERR_get_error();
                        break;
                }

        }while(1);

        free(buff);
        BIO_flush(bout);
        BIO_free_all(cipher);

        return ret;
}




----- End forwarded message -----
--
Lutz Jaenicke           jaeni...@openssl.org
OpenSSL Project         http://www.openssl.org/~jaenicke/
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to