⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 padlock_demo.c

📁 A Brief Guide to User/Hacker of VIA Padlock SDK
💻 C
📖 第 1 页 / 共 3 页
字号:
													p_cipher = (unsigned char *)cbc_aes128_cipher;
													break;
							case KEY_192BITS:		memcpy(cipher_key,"192",3);
													p_key = (unsigned char *)cbc_aes192_key;
													p_cipher = (unsigned char *)cbc_aes192_cipher;
													break;
							case KEY_256BITS:		memcpy(cipher_key,"256",3);
													p_key = (unsigned char *)cbc_aes256_key;
													p_cipher = (unsigned char *)cbc_aes256_cipher;
													break;
							default:				printf("Invalid Key Length Error!\n");
													return -1;
							}
							break;
	case ACE_AES_CFB128:	memcpy(cipher_mode,"CFB128",6);
							p_plain = (unsigned char *)cfb128_aes_plain;
							memcpy(orig_iv,(unsigned char *)cfb128_aes_iv,16);
							switch(key_len)
							{
							case KEY_128BITS:		memcpy(cipher_key,"128",3);
													p_key = (unsigned char *)cfb128_aes128_key;
													p_cipher = (unsigned char *)cfb128_aes128_cipher;
													break;
							case KEY_192BITS:		memcpy(cipher_key,"192",3);
													p_key = (unsigned char *)cfb128_aes192_key;
													p_cipher = (unsigned char *)cfb128_aes192_cipher;
													break;
							case KEY_256BITS:		memcpy(cipher_key,"256",3);
													p_key = (unsigned char *)cfb128_aes256_key;
													p_cipher = (unsigned char *)cfb128_aes256_cipher;
													break;
							default:				printf("Invalid Key Length Error!\n");
													return -1;
							}
							break;
	case ACE_AES_OFB128:	memcpy(cipher_mode,"OFB128",6);
							p_plain = (unsigned char *)ofb_aes_plain;
							memcpy(orig_iv,(unsigned char *)ofb_aes_iv,16);
							switch(key_len)
							{
							case KEY_128BITS:		memcpy(cipher_key,"128",3);
													p_key = (unsigned char *)ofb_aes128_key;
													p_cipher = (unsigned char *)ofb_aes128_cipher;
													break;
							case KEY_192BITS:		memcpy(cipher_key,"192",3);
													p_key = (unsigned char *)ofb_aes192_key;
													p_cipher = (unsigned char *)ofb_aes192_cipher;
													break;
							case KEY_256BITS:		memcpy(cipher_key,"256",3);
													p_key = (unsigned char *)ofb_aes256_key;
													p_cipher = (unsigned char *)ofb_aes256_cipher;
													break;
							default:				printf("Invalid Key Length Error!\n");
													return -1;
							}
							break;
	default :				printf("Invalid Cipher Mode Error!\n");
							return -1;
	}
	
	print_star();
	printf("%s mode fast AES %sbits key cipher test:\n",cipher_mode,cipher_key);
	print_star();

	// malloc temporary buffer for later use
	p_temp_buf = (unsigned char *)malloc((nbytes + 16));
	if(p_temp_buf == NULL)
	{
		printf("Fail to malloc %d bytes memory!\n",(nbytes + 16));
		return -1;
	}
	
	// get address aligned buffer from temporary buffer
	p_aligned_buf = (unsigned char *)((((unsigned long)p_temp_buf) + 15 )&(~15UL));
	
	// copy the data to be encrypted to aligned buffer
	memcpy(p_aligned_buf, p_plain,nbytes);
	
	// because the temp_iv will be updated 
	// we have to reserve it to orig_iv and copy it to temp_iv here
	memcpy(temp_iv, orig_iv, 16);
	
	// call ACE fast aes encryption API 
	res = padlock_aes_fast_encrypt(p_key, key_len, mode, p_aligned_buf, p_aligned_buf, nbytes, temp_iv);
	if(res != AES_SUCCEEDED)
	{
		print_ace_error(res);
		return -1;
	}
	
	// copy the encryption result to scratch
	memcpy(scratch, p_aligned_buf, nbytes);
	
	// see whether the iv has been updated by ACE fast aes encryption API
	if(memcmp (temp_iv, orig_iv, 16))
	{
		printf("\niv has been changed after encryption!\n");
	}
	else
	{
		printf("\niv hasn't been changed after encryption!\n");
	}

	// verify the result of encryption
	if (memcmp (scratch, p_cipher, nbytes))
	{
        printf("%s mode fast AES-%s encryption test failed.\n",cipher_mode,cipher_key);
    }
	else 
    {
		printf("%s mode fast AES-%s encryption test ok\n",cipher_mode,cipher_key);
	}

	print_star();
	/////////////////////////////////////////////////////////////////////
	
	// copy the data to be decrypted to aligned buffer
	memcpy(p_aligned_buf, p_cipher, nbytes);
	
	// because the temp_iv has been updated 
	// we have to restore orig_iv to it here
	memcpy(temp_iv, orig_iv, 16);

	// call ACE fast aes decryption API 
	res = padlock_aes_fast_decrypt(p_key, key_len, mode, p_aligned_buf, p_aligned_buf, nbytes, temp_iv);
	if(res != AES_SUCCEEDED)
	{
		print_ace_error(res);
		return -1;
	}

	// copy the decryption result to scratch
	memcpy(scratch, p_aligned_buf, nbytes);

	// see whether the iv has been updated by ACE fast aes decryption API
	if(memcmp (temp_iv, orig_iv, 16))
	{
		printf("\niv has been changed after decryption!\n");
	}
	else
	{
		printf("\niv hasn't been changed after decryption!\n");
	}

	// verify the result of decryption
	if (memcmp (scratch, p_plain, nbytes))
	{
        printf("%s mode fast AES-%s decryption test failed.\n",cipher_mode,cipher_key);
    }
	else 
    {
		printf("%s mode fast AES-%s decryption test ok\n",cipher_mode,cipher_key);
	}

	// free temporary buffer
	free( p_temp_buf);
    
	print_star();

	return 0;
}

int main(int argc, char* argv[])
{
    int rng_available;
	unsigned char random_num1[1024] = {0,};
	unsigned char random_num2[1024] = {0,};

	int ace_available;
	ACE_AES_MODE mode;
	KEY_LENGTH key_len;
	int nbytes = 64;

	///////////////////////////////////////////////////////////////////////////

	print_star();
	printf("Begin to test Padlock SDK RNG API functions:\n");
	print_star();


	// Padlock SDK RNG API
	rng_available = padlock_rng_available();
	if(rng_available)
	{
		printf("VIA RNG hardware is available!\n");
	}
	else
	{
		printf("VIA RNG hardware isn't available!\n");
		return -1;
	}
		
	print_star();
	padlock_rng_rand( random_num1, 1024);
	printf("VIA RNG generates random number 1 :\n");
	dump_rand(random_num1, 1024);	
	print_star();

	print_star();
	padlock_rng_rand( random_num2, 1024);
	printf("VIA RNG generates random number 2 :\n");
	dump_rand(random_num2, 1024);	
	print_star();

	if((strcmp(random_num1, random_num2)))
	{
		printf("\nVIA RNG random number generation function test ok!\n");
	}
	else
	{
		printf("\nVIA RNG random number generation function test failed!\n");
	}
	
	print_star();

	////////////////////////////////////////////////////////////////////////////////////

	print_star();
	printf("Begin to test Padlock SDK ACE AES API functions:\n");
	print_star();

	// Padlock SDK ACE AES API
	ace_available = padlock_ace_available();
	if(ace_available)
	{
		printf("VIA ACE hardware is available!\n");
	}
	else
	{
		printf("VIA ACE hardware isn't available!\n");
		return -1;
	}
	print_star();

	mode = ACE_AES_ECB;
	key_len = KEY_128BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_ECB;
	key_len = KEY_192BITS;
	test_padlock_aes(mode,key_len,nbytes);
	
	mode = ACE_AES_ECB;
	key_len = KEY_256BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_CBC;
	key_len = KEY_128BITS;
	test_padlock_aes(mode,key_len,nbytes);
	
	mode = ACE_AES_CBC;
	key_len = KEY_192BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_CBC;
	key_len = KEY_256BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_128BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_192BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_256BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_128BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_192BITS;
	test_padlock_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_256BITS;
	test_padlock_aes(mode,key_len,nbytes);

	//////////////////////////////////////////////////////////////////

	print_star();
	printf("Begin to test Padlock SDK aligned aes API functions:\n");

	mode = ACE_AES_ECB;
	key_len = KEY_128BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_ECB;
	key_len = KEY_192BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);
	
	mode = ACE_AES_ECB;
	key_len = KEY_256BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_CBC;
	key_len = KEY_128BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);
	
	mode = ACE_AES_CBC;
	key_len = KEY_192BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_CBC;
	key_len = KEY_256BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_128BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_192BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_256BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_128BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_192BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_256BITS;
	test_padlock_aligned_aes(mode,key_len,nbytes);

	///////////////////////////////////////////////////////////////////////////////////////

	print_star();
	printf("Begin to test Padlock SDK fast aes API functions:\n");

	mode = ACE_AES_ECB;
	key_len = KEY_128BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_ECB;
	key_len = KEY_192BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);
	
	mode = ACE_AES_ECB;
	key_len = KEY_256BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_CBC;
	key_len = KEY_128BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);
	
	mode = ACE_AES_CBC;
	key_len = KEY_192BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_CBC;
	key_len = KEY_256BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_128BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_192BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_CFB128;
	key_len = KEY_256BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_128BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);

	mode = ACE_AES_OFB128;
	key_len = KEY_192BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);
	
	mode = ACE_AES_OFB128;
	key_len = KEY_256BITS;
	test_padlock_fast_aes(mode,key_len,nbytes);
	
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -