以下代码实现了CBC的正确性测试(标准测试数据)
说明:
1. 代码里面使用了一个Str2Num函数,它将测试向量中的字符串转为十六进制字节数据,可自行实现。
2. 测试向量出处为NIST SP 800-38A (Recommendation for Block Cipher Modes of Operation:Methods and Techniques)
#include "tomcrypt.h"
#include "TestMode.h"
//lie test , use nist test vetor
typedef struct NistCBCTestVector_st{
char * name;//test vetor name
int keylen;
int msglen;
BYTE * key[1]; // 密钥
BYTE * IV[1]; // 初始化向量
BYTE * pt[4]; // 明文,暂时最多支持四段明文
BYTE * ct[4]; // 密文,暂时最多支持四段明文
BYTE * nonce[1];
BYTE * associated_data[1];
BYTE * mac[1];
} NistCBCTestVector;
int Test_CBC_AES(void)
{
// 数据来源
// NIST SP 800-38A (Recommendation for Block Cipher Modes of Operation:Methods and Techniques).pdf
NistCBCTestVector vect[] = {
{//vect[0]
/*name*/ "F.2.1 CBC-AES128.Encrypt",
/*keylen*/ 16,
/*msglen*/ 16*4,
/*Key*/ "2b7e151628aed2a6abf7158809cf4f3c",
/*IV */ "000102030405060708090a0b0c0d0e0f",
{// pt
"6bc1bee22e409f96e93d7e117393172a",
"ae2d8a571e03ac9c9eb76fac45af8e51",
"30c81c46a35ce411e5fbc1191a0a52ef",
"f69f2445df4f9b17ad2b417be66c3710"
},
{//ct
"7649abac8119b246cee98e9b12e9197d",
"5086cb9b507219ee95db113a917678b2",
"73bed6b8e3c1743b7116e69e22229516",
"3ff1caa1681fac09120eca307586e1a7"
}
},
//////////////////////////////////////////////////////////////////////////
{//vect[1]
/*name*/ "F.2.2 CBC-AES128.Decrypt",
/*keylen*/ 16,
/*msglen*/ 16*4,
/*Key*/ "2b7e151628aed2a6abf7158809cf4f3c",
/*IV */ "000102030405060708090a0b0c0d0e0f",
{//ct //解密时明文密文相反而已
"7649abac8119b246cee98e9b12e9197d",
"5086cb9b507219ee95db113a917678b2",
"73bed6b8e3c1743b7116e69e22229516",
"3ff1caa1681fac09120eca307586e1a7"
},
{// pt //解密时明文密文相反而已
"6bc1bee22e409f96e93d7e117393172a",
"ae2d8a571e03ac9c9eb76fac45af8e51",
"30c81c46a35ce411e5fbc1191a0a52ef",
"f69f2445df4f9b17ad2b417be66c3710"
}
},
//////////////////////////////////////////////////////////////////////////
{//vect[2]
/*name*/ "F.2.3 CBC-AES192.Encrypt",
/*keylen*/ 24,
/*msglen*/ 16*4,
/*Key*/ "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
/*IV */ "000102030405060708090a0b0c0d0e0f",
{// pt
"6bc1bee22e409f96e93d7e117393172a",
"ae2d8a571e03ac9c9eb76fac45af8e51",
"30c81c46a35ce411e5fbc1191a0a52ef",
"f69f2445df4f9b17ad2b417be66c3710"
},
{//ct
"4f021db243bc633d7178183a9fa071e8",
"b4d9ada9ad7dedf4e5e738763f69145a",
"571b242012fb7ae07fa9baac3df102e0",
"08b0e27988598881d920a9e64f5615cd"
}
},
//////////////////////////////////////////////////////////////////////////
{//vect[3]
/*name*/ "F.2.4 CBC-AES192.Decrypt",
/*keylen*/ 24,
/*msglen*/ 16*4,
/*Key*/ "8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b",
/*IV */ "000102030405060708090a0b0c0d0e0f",
{//ct //解密时明文密文相反而已
"4f021db243bc633d7178183a9fa071e8",
"b4d9ada9ad7dedf4e5e738763f69145a",
"571b242012fb7ae07fa9baac3df102e0",
"08b0e27988598881d920a9e64f5615cd"
},
{// pt //解密时明文密文相反而已
"6bc1bee22e409f96e93d7e117393172a",
"ae2d8a571e03ac9c9eb76fac45af8e51",
"30c81c46a35ce411e5fbc1191a0a52ef",
"f69f2445df4f9b17ad2b417be66c3710"
}
},
//////////////////////////////////////////////////////////////////////////
{//vect[4]
/*name*/ "F.2.5 CBC-AES256.Encrypt",
/*keylen*/ 32,
/*msglen*/ 16*4,
/*Key*/ "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
/*IV */ "000102030405060708090a0b0c0d0e0f",
{// pt
"6bc1bee22e409f96e93d7e117393172a",
"ae2d8a571e03ac9c9eb76fac45af8e51",
"30c81c46a35ce411e5fbc1191a0a52ef",
"f69f2445df4f9b17ad2b417be66c3710"
},
{//ct
"f58c4c04d6e5f1ba779eabfb5f7bfbd6",
"9cfc4e967edb808d679f777bc6702c7d",
"39f23369a9d9bacfa530e26304231461",
"b2eb05e2c39be9fcda6c19078c6a9d1b"
}
},
//////////////////////////////////////////////////////////////////////////
{//vect[5]
/*name*/ "F.2.6 CBC-AES256.Decrypt",
/*keylen*/ 32,
/*msglen*/ 16*4,
/*Key*/ "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4",
/*IV */ "000102030405060708090a0b0c0d0e0f",
{//ct //解密时明文密文相反而已
"f58c4c04d6e5f1ba779eabfb5f7bfbd6",
"9cfc4e967edb808d679f777bc6702c7d",
"39f23369a9d9bacfa530e26304231461",
"b2eb05e2c39be9fcda6c19078c6a9d1b"
},
{// pt //解密时明文密文相反而已
"6bc1bee22e409f96e93d7e117393172a",
"ae2d8a571e03ac9c9eb76fac45af8e51",
"30c81c46a35ce411e5fbc1191a0a52ef",
"f69f2445df4f9b17ad2b417be66c3710"
}
}
};
int idx, err, i, res;
BYTE buf[64];
symmetric_CBC ctr;
int keylen, msglen;
BYTE key[32], IV[16], pt[64], ct[64];
char *p_operate;
/* AES can be under rijndael or aes... try to find it */
if ( register_cipher (&aes_desc) != CRYPT_OK )
{
return CRYPT_INVALID_CIPHER;
}
if ((idx = find_cipher("aes")) == -1)
{
return CRYPT_NOP;
}
for ( i = 0; i < (int)(sizeof(vect)/sizeof(vect[0])); i++ )
{
keylen = vect[i].keylen;
msglen = vect[i].msglen;
Str2Num(vect[i].key, 1, key);
Str2Num(vect[i].IV, 1, IV);
Str2Num(vect[i].pt, 4, pt);
Str2Num(vect[i].ct, 4, ct);
if ((err = cbc_start(idx, IV, key, keylen, 0, &ctr)) != CRYPT_OK)
{
return err;
}
p_operate = vect[i].name + strlen(vect[i].name) - strlen("Encrypt");
if (strcmp( p_operate, "Encrypt") == 0 )
{
if ((err = cbc_encrypt( pt, buf, msglen, &ctr)) != CRYPT_OK)
{
return err;
}
}
else if (strcmp( p_operate, "Decrypt") == 0 )
{
if ((err = cbc_decrypt( pt, buf, msglen, &ctr)) != CRYPT_OK)
{
return err;
}
}
else
{
printf("Test Vetor : operate err! %s \n",vect[i].name );
}
cbc_done(&ctr);
res = XMEMCMP(buf, ct, msglen);
printf("Test Vetor : %s pass ? %s \n",vect[i].name, (res == 0)?"Yes":"No" );
}
printf("\nTest CBC AES Finish!\n" );
return CRYPT_OK;
}