Quantcast

DES crash in 64 bits...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

DES crash in 64 bits...

yhirsch (Bugzilla)

I'm compilied OpenSSL in 64-bit mode (both on SunOS and HP-UX). For some reason whenever I use the DES algorithm (and

any other DES-based algorithms) – my code crashes. I simply don't know what's wrong and I couldn't find any

reference to a similar problem.

 

I would greatly appreciate any help. Below is the code that crashes:

 

#include <iostream>

#include <openssl/evp.h>

#include <openssl/bio.h>

#include <vector>

 

using namespace std;

 

static int all_encrypt_ciphers_init = 0;

 

#define CIPHER_BUFFER_TOO_SMALL -1

#define CIPHER_NO_SUCH_ALGORITHM -2

#define CIPHER_OPENSSL_ERROR -3

 

int DoEncrypt(const char* text, int textsize, char* output, int outsize, const char* key, const char* algorithm)

{

    const EVP_CIPHER* cipher = NULL;

    BIO *encryptor = NULL, *base64 = NULL, *memory = NULL;

    char* mdata;

    int msize;

    unsigned char the_key[EVP_MAX_KEY_LENGTH];

    unsigned char the_iv[EVP_MAX_IV_LENGTH];

 

    if (!all_encrypt_ciphers_init) {

        OpenSSL_add_all_ciphers();

        all_encrypt_ciphers_init = 1;

    }

 

    cipher = EVP_get_cipherbyname(algorithm);

    if (cipher == NULL)

        return CIPHER_NO_SUCH_ALGORITHM;

 

    encryptor = BIO_new(BIO_f_cipher());

    if (encryptor == NULL)

        return CIPHER_OPENSSL_ERROR;

 

      cout << "test1\n";     

      EVP_BytesToKey (cipher, EVP_md5(), NULL, (const unsigned char*) key, strlen(key), 1, the_key, the_iv);

      cout << "test2\n";     

    BIO_set_cipher(encryptor, cipher, the_key, the_iv, 1);

      cout << "test3\n";     

 

    base64 = BIO_new(BIO_f_base64());

    if (base64 == NULL)

        return CIPHER_OPENSSL_ERROR;

 

    /* We ask the base64 encoder to put all the encoded message in one line. */

 

    BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL);

 

      cout << "test4\n";     

    memory = BIO_new(BIO_s_mem());

    if (memory == NULL)

        return CIPHER_OPENSSL_ERROR;

 

    /* We ask the base64 encoder to put all the encoded message in one line. */

 

    BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL);

      cout << "test5\n";     

 

    memory = BIO_new(BIO_s_mem());

    if (memory == NULL)

        return CIPHER_OPENSSL_ERROR;

 

      cout << "test6\n";     

    BIO_push(encryptor, base64);

    BIO_push(base64, memory);

 

      cout << "test7\n";     

    if (BIO_write(encryptor, text, textsize) != textsize)

        return CIPHER_OPENSSL_ERROR;

      cout << "test11\n";    

 

    if (BIO_flush(encryptor) != 1)

       return CIPHER_OPENSSL_ERROR;

 

      cout << "testa\n";     

    msize = BIO_get_mem_data(memory, &mdata);

      cout << "test8\n";     

 

    if (msize > outsize)

    {

        BIO_free_all(encryptor);

        return CIPHER_BUFFER_TOO_SMALL;

    }

 

    memcpy(output, mdata, msize);

      cout << "test9\n";     

 

    BIO_free_all(encryptor);

 

    return msize;

}

 

string Encrypt(string pswd, string algo, const char* key)

{

    // prepare a buffer that is large enough to hold the

    // encrypted password.

 

    vector<char> buffer((pswd.size()*3)/2+32);

 

    int sz = DoEncrypt(&(*pswd.begin()), pswd.size(), &(*buffer.begin()), buffer.size(), key, algo.c_str());

 

    if (sz >= 0)

    {

        return string(buffer.begin(), buffer.begin()+sz);

    }

    else if (sz == CIPHER_BUFFER_TOO_SMALL) {

        throw "output buffer is too small to contain the ecnrypted text";

    }

    else if (sz == CIPHER_NO_SUCH_ALGORITHM) {

        throw "no such algorithm.";

    }

    else { 

        throw "internal openssl error.";

    }

}

 

int main(int argc, char** argv)

{

      if (argc < 3)

      {

            cerr << "Not enough arguments." << endl;

            return -1;

      }

 

      cout << Encrypt("colosus\n", argv[1], argv[2]) << endl;

      return 0;

}

 

 

Loading...