> -----Original Message-----
> From: Richard Levitte [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, March 12, 2008 7:21 PM
> To: openssl-users@openssl.org; Xu, Qiang (FXSGSC)
> Subject: Re: crypto library in openssl
>
> You're doing three mistakes:
>
> 1. you're assuming the EVP routines treat your data as character
>    strings.  That's incorrect, it treats them as binary blobs.  Any of
>    the bytes in that blob can be zero, and apparently, you're getting
>    a result that starts with a zero byte.
> 2. you're not using the resulting length from the EVP routines.  Doing
>    so will give you correct answer, strlen() is not guaranteed to do
>    that (it will only give you the correct answer if you have no zero
>    byte anywhere in the result).
> 3. you're also not checking the returned result from the EVP routines.

Now i am adding code to check the resulting lenght from EVP routines:
=====================================================
#define ESS_CRYPTO_CRYPT_BUFFER_SIZE   (56)
......
bool_t esscrypto_decryptString(unsigned char *toDecrypt,
                               unsigned char *passPhrase,
                               int  sizeOfStrToDecrypt,
                               int  maxDecryptedStringSize,
                               int  *sizeOfDecryptedString,
                               unsigned char **decryptedString)
{
    EVP_CIPHER_CTX openSSLDecryptionStructure;
    int tempOutputLength = 0;
    int i = 0;
    int tmpOutputBufferPosition = 0;
    int tmpOffset = 0;
    char *tempPassPhrase = NULL;
    int currentDecryptedStrLength = 0;
    char *tempPtr = NULL;
    ......
    for (i = 0; i < sizeOfStrToDecrypt/ ESS_CRYPTO_CRYPT_BUFFER_SIZE; i++)
    {
        /* decrypt the contents of the buffer */
        EVP_DecryptUpdate(&openSSLDecryptionStructure,
                         &((*decryptedString)[tmpOutputBufferPosition]),
                         &tmpOffset,
                         &toDecrypt[tmpOutputBufferPosition],
                         ESS_CRYPTO_CRYPT_BUFFER_SIZE);

        /* Increment the position we are at in decrypting the string */
        tmpOutputBufferPosition = tmpOutputBufferPosition + tmpOffset;
        LOGFORCE("EVP_DecryptUpdate1: tmpOffset is %d", tmpOffset); //xq
    }
    ......
    /*
     * if there is data left to decrypt that did not fit exactly within
     *  the buffer, decrypt that remaining bit.
     ******/
    if ( sizeOfStrToDecrypt % ESS_CRYPTO_CRYPT_BUFFER_SIZE)
    {
        /* decrypt the contents of the buffer */
         EVP_DecryptUpdate(&openSSLDecryptionStructure,
                         &((*decryptedString)[tmpOutputBufferPosition]),
                         &tmpOffset,
                         &toDecrypt[tmpOutputBufferPosition],
                         sizeOfStrToDecrypt % ESS_CRYPTO_CRYPT_BUFFER_SIZE);

        /* Increment the position we are at in decrypting the string */
        tmpOutputBufferPosition = tmpOutputBufferPosition + tmpOffset;
        LOGFORCE("EVP_DecryptUpdate2: tmpOffset is %d", tmpOffset); //xq
    }
    ......
    /* finalize the results from decryption (check CRC) */
    EVP_DecryptFinal(&openSSLDecryptionStructure,
                    &((*decryptedString)[tmpOutputBufferPosition]),
                    &tmpOffset);
    LOGFORCE("EVP_DecryptFinal: tmpOffset is %d", tmpOffset); //xq

   /* set the final length of the decrypted string */
   *sizeOfDecryptedString = tmpOutputBufferPosition + tmpOffset;

   LOGFORCE("*sizeOfDecryptedString is %d", *sizeOfDecryptedString);
   for (i = 0; i < *sizeOfDecryptedString; i++)
   {
        LOGFORCE("char in final result is %c", (*decryptedString)[i]);
   }
   ......
=====================================================
The lenght of the password "$elkins02" is only 9 characters, far less than 
ESS_CRYPTO_CRYPT_BUFFER_SIZE (56), so only the later two LOGFORCE are recorded. 
But still, the error seems to come from EVP APIs.

For user with password "#elkins02", the log is:
=====================================================
<smbipc> (Thu Mar 13 2008 02:50:39.354) <p17565,t16384,essCrypto.c,563>
     INFO>> Decrypting string (in hex) [AAC893A997A6E21FD2A2D2D4F205133B]
<smbipc> (Thu Mar 13 2008 02:50:39.364) <p17565,t16384,essCrypto.c,682>
     INFO>> EVP_DecryptUpdate2: tmpOffset is 8
<smbipc> (Thu Mar 13 2008 02:50:39.364) <p17565,t16384,essCrypto.c,690>
     INFO>> EVP_DecryptFinal: tmpOffset is 1
<smbipc> (Thu Mar 13 2008 02:50:39.364) <p17565,t16384,essCrypto.c,695>
     INFO>> *sizeOfDecryptedString is 9
=====================================================
This is correct.

For user with password "$elkins02", the log is:
=====================================================
<smbipc> (Thu Mar 13 2008 02:51:22.509) <p17565,t16384,essCrypto.c,563>
     INFO>> Decrypting string (in hex) [7208148B96ABDC]
<smbipc> (Thu Mar 13 2008 02:51:22.509) <p17565,t16384,essCrypto.c,682>
     INFO>> EVP_DecryptUpdate2: tmpOffset is 0
<smbipc> (Thu Mar 13 2008 02:51:22.517) <p17565,t16384,essCrypto.c,690>
     INFO>> EVP_DecryptFinal: tmpOffset is 0
<smbipc> (Thu Mar 13 2008 02:51:22.517) <p17565,t16384,essCrypto.c,695>
     INFO>> *sizeOfDecryptedString is 0
=====================================================
The prototype of EVP_DecryptUpdate() is "int EVP_DecryptUpdate(EVP_CIPHER_CTX 
*ctx, unsigned char *out, int *outl, unsigned char *in, int inl)", so this 
tmpOffset is 0 means something wrong with the output length.

Is this the evidence of a defect of crypto library?
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to