Hello all, 

As someone new to openSSL I'm trying to write a simple
program to read a text file from disk, encrypt using
AES 128 and write to disk and reverse the process. 

I started out with a blowfish example program and am
attempting to change it to use AES. It works fine
during the encryption phase but am running into either
a EVP_DecryptFinal error or segmentation fault.

If anyone can provide some suggestion on how to find
more info to understand and fix this problem I would
appreciate it. I have searched the openssl-user list
with nothing has been directly applicable (to my small
amount of knowledge). 

Could this be a buffer management problem? Someone
said that I needed to provide an extra block (AES_128
= 16) for EVP_DecryptFinal to function properly?

Here's the relevant snippet:
---
#define IP_SIZE 1024        /// buffer size
#define OP_SIZE 1040        /// buffer size
#define AES_KEY_LENGTH 16
#define AES_IV_LENGTH 16


  // key and iv array
    unsigned char _key[AES_KEY_LENGTH];
    unsigned char _iv[AES_IV_LENGTH];
----

I read the generated key and iv from disk before
calling the following function.

int CryptoModule::decrypt(string infile, string
outfile)
{
    unsigned char outbuf[IP_SIZE];
    int olen, tlen, n;
    unsigned char inbuff[OP_SIZE];

    //open file descriptors
    if ((_infd = open (infile.c_str(), flags1, mode))
== -1)
        perror ("open output file error");

    if ((_outfd = open (outfile.c_str(), flags2,
mode)) == -1)
        perror ("open output file error");


    // init cipher context
    EVP_CIPHER_CTX_init (&_ctx);
    EVP_DecryptInit (&_ctx, EVP_aes_128_cbc (), _key,
_iv);

      for (;;)
      {
          bzero (&inbuff, OP_SIZE);
          if ((n = read (_infd, inbuff, OP_SIZE)) ==
-1)
            {
                perror ("read error");
                break;
            }
          else if (n == 0)
              break;

          bzero (&outbuf, IP_SIZE);

    if (EVP_DecryptUpdate (&_ctx, outbuf, &olen,
inbuff, n) != 1)
            {
                printf ("error in decrypt update\n");
                return 0;
            }

          if (EVP_DecryptFinal (&_ctx, outbuf + olen,
&tlen) != 1)
            {
                printf ("error in decrypt final\n");
                return 0;
            }
          olen += tlen;
          if ((n = write (_outfd, outbuf, olen)) ==
-1)
              perror ("write error");
      }

    EVP_CIPHER_CTX_cleanup (&_ctx);
    close (_infd);
    close (_outfd);
    return 1;
} // end decrypt

thank you!


                
__________________________________ 
Do you Yahoo!? 
All your favorites on one personal page – Try My Yahoo!
http://my.yahoo.com 
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to