如http://csrc.nist.gov/groups/STM/cavp/documents/aes/AESAVS.pdf蒙特卡洛試驗上AES 128 CBC
對於第一次迭代的預期矢量結果說明我上AES 128 CBC執行MCT是
KEY = 9dc2c84a37850c11699818605f47958c
IV = 256953b2feab2a04ae0180d8335bbed6
PLAINTEXT = 2e586692e647f5028ec6fa47a55a2aab
密文= 1b1ebd1fc45ec4 3037fd4844241a437f
下面功能用來產生第一迭代輸出,
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);
}
}
它打印的結果作爲, CT: - C1 B7 7E D5 25 21 52 5F 0A 4B A3 41 BD AF 51 D9
與預期結果不符。 我懷疑上述功能中有些東西不對,但是AESAVS pdf中給出的僞代碼也有點混淆。 請澄清它出錯的地方..
哎呀..我的壞,它需要1000迭代產生一個輸出..我只做999 ..改變循環爲(INT我= 0;我<1000;我++),它的工作 – akhilbaai