2
votes

I'm performing MCT on AES 128 CBC as described in http://csrc.nist.gov/groups/STM/cavp/documents/aes/AESAVS.pdf

The expected vector result for first iteration is

KEY = 9dc2c84a37850c11699818605f47958c

IV = 256953b2feab2a04ae0180d8335bbed6

PLAINTEXT = 2e586692e647f5028ec6fa47a55a2aab

CIPHERTEXT = 1b1ebd1fc45ec43037fd4844241a437f

Below function is used to generate first iteration output,

void
do_mct()
{
    EVP_CIPHER_CTX *ctx = NULL;
    unsigned char key[16] =
    { 0x9d, 0xc2, 0xc8, 0x4a, 0x37, 0x85, 0x0c, 0x11, 0x69, 0x98, 0x18, 0x60, 0x5f, 0x47, 0x95, 0x8c };
    unsigned char iv[16] =
    { 0x25, 0x69, 0x53, 0xb2, 0xfe, 0xab, 0x2a, 0x04, 0xae, 0x01, 0x80, 0xd8, 0x33, 0x5b, 0xbe, 0xd6 };
    unsigned char pt[16] =
    { 0x2e, 0x58, 0x66, 0x92, 0xe6, 0x47, 0xf5, 0x02, 0x8e, 0xc6, 0xfa, 0x47, 0xa5, 0x5a, 0x2a, 0xab };
    unsigned char ct_current[16] = { };
    unsigned char ct_previous[16] = { };
    int ptlen = 16, ctlen = 0;

    ctx = EVP_CIPHER_CTX_new();

    if ( ctx )
    {
        for ( int i = 0; i < 999 ; i++ )
        {
            if ( 0 == i )
            {
                if ( 1 == EVP_EncryptInit(ctx, EVP_aes_128_cbc(), &key[0], &iv[0] ) )
                {
                    EVP_CIPHER_CTX_set_padding(ctx, 0);

                    if ( 1 ==  EVP_EncryptUpdate(ctx, &ct_current[0] , &ctlen, &pt[0], ptlen) )
                    {
                        printf("\nctlen= %d\n", ctlen);
                        memcpy(&pt[0],&iv[0],16);
                    }
                    else
                    {
                        printf( " error");
                    }
                }
                else
                {
                    printf( " error");
                }
            }
            else
            {
                memcpy(&ct_previous[0],&ct_current[0],ctlen);

                if ( 1 ==  EVP_EncryptUpdate(ctx, &ct_current[0] , &ctlen, &pt[0], ptlen) )
                {
                    memcpy(&pt[0],&ct_previous[0],ctlen);
                }
                else
                {
                    printf( " error");
                }
            }
        }

        printf("\nCT :- ");
        for(int i=0;i<ctlen;i++)
        {
            printf("%02x ", ct_current[i]);
        }

        EVP_CIPHER_CTX_free(ctx);
    }
}

It prints result as, CT :- c1 b7 7e d5 25 21 52 5f 0a 4b a3 41 bd af 51 d9

which doesn't match the expected result. I'm suspecting something is not right in the above function, but the pseudo code given in AESAVS pdf was a bit confusing as well.. Please shed some lights on where it had gone wrong..

1
Oops.. My bad, it required 1000 iteration for producing one output.. I was doing only 999.. change for loop to for ( int i = 0; i < 1000 ; i++ ) and it workedakhilbaai

1 Answers

1
votes

Oops.. My bad, it required 1000 iteration for producing one output.. I was doing only 999.. change for loop to for ( int i = 0; i < 1000 ; i++ ) and it worked