テストベクトル#2を確認した

bytePrinter::print() は unsigned char * 配列か std::vector を入力して,16進数文字列を出力する.hexstring2bin::convert() は 16進数文字列(std::string)を入力して,std::vecotr を出力する.

暗号化後のデータも,復号化後のデータも文書 The AES-CBC Cipher Algorithm and Its Use with IPsec と一致した.暗号化したいデータは a,暗号化後のデータを b へ,これを復号化したデータは c.

// AES-CBC テストベクトル確認
// http://www.ipa.go.jp/security/rfc/RFC3602JA.html#4

#include <iostream>
#include <vector>
#include <string>
#include "../bytePrinter/bytePrinter.h"
#include "../hexstring2bin/hexstring2bin.h"
#include <aes.h>

using std::cout;
using std::endl;

void printInputData(
	const std::string &keyString,
	const std::string &ivString,
	const std::string &planetextString,
	const std::string &ciphertextString)
{
	std::cout << "key       : " << keyString << std::endl;
	std::cout << "iv        : " << ivString << std::endl;
	std::cout << "planetext : " << planetextString << std::endl;
	std::cout << "ciphertext: " << ciphertextString << std::endl;
}

void testcase2()
{
	std::string keyString("0xc286696d887c9aa0611bbb3e2025a45a");
	std::string ivString("0x562e17996d093d28ddb3ba695a2e6f58");
	std::string aString("0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
	std::string bString("0xd296cd94c2cccf8a3a863028b5e1dc0a7586602d253cfff91b8266bea6d61ab1");
	printInputData(keyString,ivString,aString,bString);
	std::vector<unsigned char> key;
	std::vector<unsigned char> iv;
	std::vector<unsigned char> a;
	hexstring2bin::convert( keyString, key );
	hexstring2bin::convert( ivString, iv );
	hexstring2bin::convert( aString, a );
	unsigned char b[32];
	unsigned char c[32];
	unsigned char workIv[32];
	::memcpy_s (workIv, 32, iv.data(), iv.size() );

	int result;
	aes_encrypt_ctx cxEnc;
	result = aes_encrypt_key128 ( key.data(), &cxEnc );
	::memcpy_s (workIv, 32, iv.data(), iv.size() );
	result = aes_cbc_encrypt( a.data(), b, 32, workIv, &cxEnc);
	cout << "encrypted :   "; bytePrinter::print( b, 32);

	aes_decrypt_ctx cxDec;
	result = aes_decrypt_key128 ( key.data(), &cxDec );
	::memcpy_s (workIv, 32, iv.data(), iv.size() );
	result = aes_cbc_decrypt( b, c, 32, workIv, &cxDec);
	cout << "decrypted :   "; bytePrinter::print( c, 32);
}

int main () 
{
	aes_init();
	testcase2();

	return 0;
}