Newbie: Password based encrypting using cryptlib

From: Ajay (abdudani_at_unity.ncsu.edu)
Date: 10/30/03


Date: 30 Oct 2003 10:54:21 -0800

Hello,

I am having problems using cryptlib library under Linux. I am trying
to write a simple program using cryptlib that takes plaintext and
encrypts it using a password.

I am unable to figure out why this is happening. I have gone through
the Enveloping and Password-based Encryption Enveloping in cryptlib
manual and am following the things said in there, but still cannot
figure out the issue.

I would appreciate if someone with cryptlib experience can help me
debug this.

Thanks,

Ajay

-- <Code> --

#include <limits.h> /* To determine max.buffer size encrypt
*/
#include "cryptlib.h"
#include "test/test.h"

#define ENVELOPE_TESTDATA ( ( BYTE * ) "Some test data" )
#define ENVELOPE_PGP_TESTDATA ( ( BYTE * ) "Some test data." )
#define ENVELOPE_COMPRESSEDDATA "/* This is a lowest-"

#define ENVELOPE_TESTDATA_SIZE 15
#define ENVELOPE_COMPRESSEDDATA_SIZE 20

int keyReadOK, doubleCertOK;

BYTE globalBuffer[ BUFFER_SIZE ];

/* Test raw data enveloping */
static int envelopeData( const CRYPT_FORMAT_TYPE formatType)
{
    CRYPT_ENVELOPE cryptEnvelope;
    CRYPT_ENVELOPE decryptEnvelope;
    CRYPT_CONTEXT cryptContext;
    BYTE *inBufPtr = ENVELOPE_TESTDATA, *outBufPtr = globalBuffer;
    const int length = ENVELOPE_TESTDATA_SIZE;
    const int bufSize = length + 128;
    int count;
    int bytes;
    int status;

    printf( "Testing encrypted data enveloping...\n");

    /* Create the envelope, push in the data, pop the enveloped
         * result, and destroy the envelope */

    status = cryptCreateEnvelope (&cryptEnvelope, CRYPT_UNUSED,
                        formatType);

        if( cryptStatusError( status ) )
        {
            printf( "cryptCreateEnvelope() failed with error code %d,
line %d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptSetAttributeString( cryptEnvelope,
CRYPT_ENVINFO_PASSWORD, "password", 8);

    status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_DATASIZE,
length );

    if (cryptStatusError(status))
        {
            printf( "cryptSetAttribute() failed with error code %d,
line %d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptPushData (cryptEnvelope, inBufPtr, length, NULL);
    if (cryptStatusError(status))
        {
            printf( "cryptPushData() failed with error code %d, line
%d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptFlushData (cryptEnvelope);
    if (cryptStatusError(status))
        {
            printf( "cryptFlushData() failed with error code %d, line
%d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptPopData (cryptEnvelope, outBufPtr, bufSize, &count);
    if (cryptStatusError(status))
        {
            printf( "cryptPopData() failed with error code %d, line
%d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptDestroyEnvelope (cryptEnvelope);

    /* Tell user what happened */
    printf( "Enveloped data has size %d bytes.\n", count);

    /* Encryption complete, now decrypt the encrypted data */

    /* Create the envelope, push in the encrypted data, pop the
result, and
       destroy the envelope */
    status = cryptCreateEnvelope (&decryptEnvelope, CRYPT_UNUSED,
CRYPT_FORMAT_AUTO);

        if( cryptStatusError( status ) )
        {
            printf( "cryptCreateEnvelope() failed with error code %d,
line %d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptPushData (decryptEnvelope, outBufPtr, count,
&bytes);
    if (cryptStatusError(status))
        {
            printf( "cryptPushData() failed with error code %d, line
%d. Bytes=%d\n",
                            status, __LINE__, bytes );
            return( FALSE );
        }

    status = cryptSetAttributeString( decryptEnvelope,
CRYPT_ENVINFO_PASSWORD, "password", 8);

    if (cryptStatusError(status))
        {
            printf( "cryptSetAttribute() failed with error code %d,
line %d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptFlushData (decryptEnvelope);
    if (cryptStatusError(status))
        {
            printf( "cryptFlushData() failed with error code %d, line
%d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptPopData (decryptEnvelope, outBufPtr, bufSize,
&count);
    if (cryptStatusError(status))
        {
            printf( "cryptPopData() failed with error code %d, line
%d.\n",
                            status, __LINE__ );
            return( FALSE );
        }

    status = cryptDestroyEnvelope (decryptEnvelope);

    /* Make sure the result matches what we pushed */
    if( count != length )
    {
        printf( "De-enveloped data length(%d) != original
length(%d).\n", count, length );
        return( FALSE );
    }

    if( memcmp( outBufPtr, ENVELOPE_TESTDATA, length ) )
    {
        puts( "De-enveloped data != original data." );
        return( FALSE );
    }

    puts( "Enveloping of encrypted data succeeded.\n" );
    return( TRUE );
}

int main()
{
    cryptInit();
    envelopeData(CRYPT_FORMAT_CRYPTLIB);
    cryptEnd();
}

-- </Code> --