📄 padlock_demo.c
字号:
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 plain AES %sbits key cipher test:\n",cipher_mode,cipher_key);
print_star();
// create the ace aes cipher context
ctx = padlock_aes_begin();
// set cipher key
res = padlock_aes_setkey( ctx, p_key, key_len);
if(res != AES_SUCCEEDED)
{
print_ace_error(res);
return -1;
}
// because the iv will be updated by the plain aes encryption and decryption API
// here we reserve it in the orig_iv
memcpy(temp_iv, orig_iv, 16);
// set cipher mode and iv
res = padlock_aes_setmodeiv( ctx, mode, temp_iv);
if(res != AES_SUCCEEDED)
{
print_ace_error(res);
return -1;
}
// call ACE plain aes encryption API
res = padlock_aes_encrypt( ctx, p_plain, scratch, nbytes);
if(res != AES_SUCCEEDED)
{
print_ace_error(res);
return -1;
}
// see whether the iv has been updated by ACE plain 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 plain AES-%s encryption test failed.\n",cipher_mode,cipher_key);
}
else
{
printf("%s mode plain AES-%s encryption test ok\n",cipher_mode,cipher_key);
}
print_star();
/////////////////////////////////////////////////////////////////////
// because the temp_iv has been updated
// we have to restore orig_iv to it here
memcpy(temp_iv, orig_iv, 16);
// reset iv for decrytion
res = padlock_aes_setmodeiv( ctx, mode, temp_iv);
if(res != AES_SUCCEEDED)
{
print_ace_error(res);
return -1;
}
// call ACE plain aes decryption API
res = padlock_aes_decrypt( ctx, p_cipher, scratch, nbytes);
if(res != AES_SUCCEEDED)
{
print_ace_error(res);
return -1;
}
// see whether the iv has been updated by ACE plain 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 plain AES-%s decryption test failed.\n",cipher_mode,cipher_key);
}
else
{
printf("%s mode plain AES-%s decryption test ok\n",cipher_mode,cipher_key);
}
// destroy the ace aes cipher context
padlock_aes_close( ctx);
print_star();
return 0;
}
// test VIA Padlock SDK ACE aligned aes API
static int
test_padlock_aligned_aes(ACE_AES_MODE mode, KEY_LENGTH key_len, int nbytes)
{
struct aligned_memory_context *buf_aligned_mctx;
char cipher_mode[10]= {0,};
char cipher_key[10] = {0,};
unsigned char *p_key;
unsigned char *p_plain;
unsigned char *p_cipher;
unsigned char temp_iv[16]={0,};
unsigned char orig_iv[16]={0,};
AES_RESULT res;
switch (mode)
{
case ACE_AES_ECB: memcpy(cipher_mode,"ECB",3);
p_plain = (unsigned char *)ecb_aes_plain;
switch(key_len)
{
case KEY_128BITS: memcpy(cipher_key,"128",3);
p_key = (unsigned char *)ecb_aes128_key;
p_cipher = (unsigned char *)ecb_aes128_cipher;
break;
case KEY_192BITS: memcpy(cipher_key,"192",3);
p_key = (unsigned char *)ecb_aes192_key;
p_cipher = (unsigned char *)ecb_aes192_cipher;
break;
case KEY_256BITS: memcpy(cipher_key,"256",3);
p_key = (unsigned char *)ecb_aes256_key;
p_cipher = (unsigned char *)ecb_aes256_cipher;
break;
default: printf("Invalid Key Length Error!\n");
return -1;
}
break;
case ACE_AES_CBC: memcpy(cipher_mode,"CBC",3);
p_plain = (unsigned char *)cbc_aes_plain;
memcpy(orig_iv,(unsigned char *)cbc_aes_iv,16);
switch(key_len)
{
case KEY_128BITS: memcpy(cipher_key,"128",3);
p_key = (unsigned char *)cbc_aes128_key;
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 aligned AES %sbits key cipher test:\n",cipher_mode,cipher_key);
print_star();
// create address aligned buffer context
buf_aligned_mctx = padlock_aligned_malloc(nbytes);
// copy data to be encrypted to aligned buffer
res = padlock_aligned_memcpy_to(buf_aligned_mctx, p_plain, nbytes);
if(res != AES_SUCCEEDED)
{
printf("aligned memory copy error!\n");
return -1;
}
// 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 aligned aes encryption API
res = padlock_aes_aligned_encrypt(p_key, key_len, mode, buf_aligned_mctx, temp_iv);
if(res != AES_SUCCEEDED)
{
print_ace_error(res);
return -1;
}
// see whether the iv has been updated by ACE aligned 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");
}
// copy the encrypted result from aligned buffer to scratch
res = padlock_aligned_memcpy_from(buf_aligned_mctx, scratch, nbytes);
if(res != AES_SUCCEEDED)
{
printf("aligned memory copy error!\n");
return -1;
}
// verify the result of encryption
if (memcmp (scratch, p_cipher, nbytes))
{
printf("%s mode aligned AES-%s encryption test failed.\n",cipher_mode,cipher_key);
}
else
{
printf("%s mode aligned AES-%s encryption test ok\n",cipher_mode,cipher_key);
}
print_star();
/////////////////////////////////////////////////////////////////////
// copy data to be decrypted to aligned buffer
res = padlock_aligned_memcpy_to(buf_aligned_mctx, p_cipher, nbytes);
if(res != AES_SUCCEEDED)
{
printf("aligned memory copy error!\n");
return -1;
}
// because the temp_iv has been updated
// we have to restore orig_iv to it here
memcpy(temp_iv, orig_iv, 16);
// call ACE aligned aes decryption API
res = padlock_aes_aligned_decrypt(p_key, key_len, mode, buf_aligned_mctx, temp_iv);
if(res != AES_SUCCEEDED)
{
print_ace_error(res);
return -1;
}
// see whether the iv has been updated by ACE aligned 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");
}
// copy the decrypted result from aligned buffer to scratch
res = padlock_aligned_memcpy_from(buf_aligned_mctx, scratch, nbytes);
if(res != AES_SUCCEEDED)
{
printf("aligned memory copy error!\n");
return -1;
}
// verify the result of decryption
if (memcmp (scratch, p_plain, nbytes))
{
printf("%s mode aligned AES-%s decryption test failed.\n",cipher_mode,cipher_key);
}
else
{
printf("%s mode aligned AES-%s decryption test ok\n",cipher_mode,cipher_key);
}
// destroy the aligned buffer context
padlock_aligned_mfree( buf_aligned_mctx);
print_star();
return 0;
}
// test VIA Padlock SDK ACE fast aes API
static int
test_padlock_fast_aes(ACE_AES_MODE mode, KEY_LENGTH key_len, int nbytes)
{
unsigned char *p_temp_buf;
unsigned char *p_aligned_buf;
char cipher_mode[10]= {0,};
char cipher_key[10] = {0,};
unsigned char *p_key;
unsigned char *p_plain;
unsigned char *p_cipher;
unsigned char temp_iv[16]={0,};
unsigned char orig_iv[16]={0,};
AES_RESULT res;
switch (mode)
{
case ACE_AES_ECB: memcpy(cipher_mode,"ECB",3);
p_plain = (unsigned char *)ecb_aes_plain;
switch(key_len)
{
case KEY_128BITS: memcpy(cipher_key,"128",3);
p_key = (unsigned char *)ecb_aes128_key;
p_cipher = (unsigned char *)ecb_aes128_cipher;
break;
case KEY_192BITS: memcpy(cipher_key,"192",3);
p_key = (unsigned char *)ecb_aes192_key;
p_cipher = (unsigned char *)ecb_aes192_cipher;
break;
case KEY_256BITS: memcpy(cipher_key,"256",3);
p_key = (unsigned char *)ecb_aes256_key;
p_cipher = (unsigned char *)ecb_aes256_cipher;
break;
default: printf("Invalid Key Length Error!\n");
return -1;
}
break;
case ACE_AES_CBC: memcpy(cipher_mode,"CBC",3);
p_plain = (unsigned char *)cbc_aes_plain;
memcpy(orig_iv,(unsigned char *)cbc_aes_iv,16);
switch(key_len)
{
case KEY_128BITS: memcpy(cipher_key,"128",3);
p_key = (unsigned char *)cbc_aes128_key;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -