📄 padlock.c
字号:
}
else
{
res = ace_aes_aligned_crypt(ctx, enc, ciphertxt, plaintxt, nbytes);
}
return res;
}
void
padlock_aes_close(struct ace_aes_context *ctx)
{
if(ctx != NULL)
{
free(ctx);
ctx = NULL;
}
}
/////////////////////////////////////////////////////////////////
// VIA Padlock SDK aligned ACE AES API
struct aligned_memory_context *padlock_aligned_malloc(int size)
{
struct aligned_memory_context *aligned_mctx;
aligned_mctx = (struct aligned_memory_context *)malloc(sizeof(struct aligned_memory_context));
if(aligned_mctx == NULL)
{
printf("Fatal error: Fail to create struct aligned_memory!\n");
exit;
}
aligned_mctx->temp_buf = (unsigned char *)malloc(size + 16);
if(aligned_mctx->temp_buf == NULL)
{
printf("Fatal error: Fail to malloc %d bytes buffer!\n",size);
exit;
}
aligned_mctx->p_alignedbuf = ALIGN16(aligned_mctx->temp_buf);
aligned_mctx->size = size;
aligned_mctx->offset = 0;
return aligned_mctx;
}
void padlock_aligned_mfree(struct aligned_memory_context *aligned_mctx)
{
if(aligned_mctx != NULL)
{
free(aligned_mctx->temp_buf);
free(aligned_mctx);
aligned_mctx = NULL;
}
}
AES_RESULT
padlock_aligned_memcpy_to(struct aligned_memory_context *aligned_mctx, unsigned char *src, int nbytes)
{
AES_RESULT res = AES_SUCCEEDED;
if(nbytes == 0)
{
printf("no data need to be processed!\n");
res = AES_FAILED;
return res;
}
if(nbytes % 16)
{
printf("Fatal error : the length of plaintxt/ciphertxt must be multiples of 16bytes\n");
res = AES_NOT_BLOCKED;
return res;
}
if( nbytes > aligned_mctx->size)
{
printf("aligned memcpy error!\n");
res = AES_FAILED;
return res;
}
memcpy(aligned_mctx->p_alignedbuf, src, nbytes);
aligned_mctx->offset = nbytes;
return res;
}
AES_RESULT
padlock_aligned_memcpy_from(struct aligned_memory_context *aligned_mctx, unsigned char *dst, int nbytes)
{
AES_RESULT res = AES_SUCCEEDED;
if( nbytes > aligned_mctx->size)
{
printf("aligned memcpy error!\n");
res = AES_FAILED;
return res;
}
memcpy( dst, aligned_mctx->p_alignedbuf, nbytes);
aligned_mctx->offset = 0;
return res;
}
AES_RESULT
padlock_aes_aligned_encrypt(unsigned char *key, KEY_LENGTH key_len, ACE_AES_MODE mode,
struct aligned_memory_context *buf_aligned_mctx, unsigned char *iv)
{
struct ace_aes_context *ctx;
unsigned char *aligned_ciphertxt;
unsigned char *aligned_plaintxt;
int nbytes;
AES_RESULT res = AES_SUCCEEDED;
if((key == NULL)||(buf_aligned_mctx == NULL))
{
printf("Fatal error : key/plaintxt/ciphertxt NULL pointer error!\n");
res = AES_FAILED;
return res;
}
switch(key_len)
{
case KEY_128BITS :
case KEY_192BITS :
case KEY_256BITS : break;
default: printf("Fatal error : invalid key length!\n");
res = AES_KEY_NOT_SUPPORTED;
return res;
}
switch(mode)
{
case ACE_AES_ECB: break;
case ACE_AES_CBC:
case ACE_AES_CFB128:
case ACE_AES_OFB128: if(iv == NULL)
{
printf("Fatal error : iv NULL pointer error!\n");
res = AES_FAILED;
return res;
}
break;
default: printf("Fatal error : invalid cipher mode!\n");
res = AES_MODE_NOT_SUPPORTED;
return res;
}
nbytes = buf_aligned_mctx->offset;
aligned_ciphertxt = buf_aligned_mctx->p_alignedbuf;
aligned_plaintxt = buf_aligned_mctx->p_alignedbuf;
if(UNALIGNED(aligned_plaintxt))
{
printf("plaintxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
if(UNALIGNED(aligned_ciphertxt))
{
printf("ciphertxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
////////////////////////////////////////////////////////////////////////////
ctx = padlock_aes_begin();
res = padlock_aes_setkey( ctx, key, key_len);
if( res != AES_SUCCEEDED)
{
return res;
}
res = padlock_aes_setmodeiv( ctx, mode, iv);
if( res != AES_SUCCEEDED)
{
return res;
}
res = ace_aes_aligned_crypt( ctx, 1, aligned_plaintxt, aligned_ciphertxt, nbytes);
if( res != AES_SUCCEEDED)
{
return res;
}
padlock_aes_close( ctx);
return res;
}
AES_RESULT
padlock_aes_aligned_decrypt(unsigned char *key, KEY_LENGTH key_len, ACE_AES_MODE mode,
struct aligned_memory_context *buf_aligned_mctx, unsigned char *iv)
{
struct ace_aes_context *ctx;
unsigned char *aligned_ciphertxt;
unsigned char *aligned_plaintxt;
int nbytes;
AES_RESULT res = AES_SUCCEEDED;
if((key == NULL)||(buf_aligned_mctx == NULL))
{
printf("Fatal error : key/plaintxt/ciphertxt NULL pointer error!\n");
res = AES_FAILED;
return res;
}
switch(key_len)
{
case KEY_128BITS :
case KEY_192BITS :
case KEY_256BITS : break;
default: printf("Fatal error : invalid key length!\n");
res = AES_KEY_NOT_SUPPORTED;
return res;
}
switch(mode)
{
case ACE_AES_ECB: break;
case ACE_AES_CBC:
case ACE_AES_CFB128:
case ACE_AES_OFB128: if(iv == NULL)
{
printf("Fatal error : iv NULL pointer error!\n");
res = AES_FAILED;
return res;
}
break;
default: printf("Fatal error : invalid cipher mode!\n");
res = AES_MODE_NOT_SUPPORTED;
return res;
}
nbytes = buf_aligned_mctx->offset;
aligned_ciphertxt = buf_aligned_mctx->p_alignedbuf;
aligned_plaintxt = buf_aligned_mctx->p_alignedbuf;
if(UNALIGNED(aligned_plaintxt))
{
printf("plaintxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
if(UNALIGNED(aligned_ciphertxt))
{
printf("ciphertxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
////////////////////////////////////////////////////////////////////////////////////////////
ctx = padlock_aes_begin();
res = padlock_aes_setkey( ctx, key, key_len);
if( res != AES_SUCCEEDED)
{
return res;
}
res = padlock_aes_setmodeiv( ctx, mode, iv);
if( res != AES_SUCCEEDED)
{
return res;
}
res = ace_aes_aligned_crypt( ctx, 0, aligned_ciphertxt, aligned_plaintxt, nbytes);
if( res != AES_SUCCEEDED)
{
return res;
}
padlock_aes_close( ctx);
return res;
}
/////////////////////////////////////////////////////////////////////////
// VIA Padlock SDK fast ACE AES API for aligned data cryptography
AES_RESULT
padlock_aes_fast_encrypt(unsigned char *key, KEY_LENGTH key_len, ACE_AES_MODE mode,
unsigned char *aligned_plaintxt, unsigned char *aligned_ciphertxt,
int nbytes, unsigned char *iv)
{
struct ace_aes_context *ctx;
AES_RESULT res = AES_SUCCEEDED;
if((key == NULL)||(aligned_ciphertxt == NULL)||(aligned_plaintxt == NULL))
{
printf("Fatal error : key/plaintxt/ciphertxt NULL pointer error!\n");
res = AES_FAILED;
return res;
}
switch(key_len)
{
case KEY_128BITS :
case KEY_192BITS :
case KEY_256BITS : break;
default: printf("Fatal error : invalid key length!\n");
res = AES_KEY_NOT_SUPPORTED;
return res;
}
switch(mode)
{
case ACE_AES_ECB: break;
case ACE_AES_CBC:
case ACE_AES_CFB128:
case ACE_AES_OFB128: if(iv == NULL)
{
printf("Fatal error : iv NULL pointer error!\n");
res = AES_FAILED;
return res;
}
break;
default: printf("Fatal error : invalid cipher mode!\n");
res = AES_MODE_NOT_SUPPORTED;
return res;
}
if(nbytes == 0)
{
printf("no data need to be processed!\n");
res = AES_SUCCEEDED;
return res;
}
if(nbytes % 16)
{
printf("Fatal error : the length of plaintxt/ciphertxt must be multiples of 16bytes\n");
res = AES_NOT_BLOCKED;
return res;
}
if(UNALIGNED(aligned_plaintxt))
{
printf("plaintxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
if(UNALIGNED(aligned_ciphertxt))
{
printf("ciphertxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
////////////////////////////////////////////////////////////////////////////
ctx = padlock_aes_begin();
res = padlock_aes_setkey( ctx, key, key_len);
if( res != AES_SUCCEEDED)
{
return res;
}
res = padlock_aes_setmodeiv( ctx, mode, iv);
if( res != AES_SUCCEEDED)
{
return res;
}
res = ace_aes_aligned_crypt( ctx, 1, aligned_plaintxt, aligned_ciphertxt, nbytes);
if( res != AES_SUCCEEDED)
{
return res;
}
padlock_aes_close( ctx);
return res;
}
AES_RESULT
padlock_aes_fast_decrypt(unsigned char *key, KEY_LENGTH key_len, ACE_AES_MODE mode,
unsigned char *aligned_ciphertxt, unsigned char *aligned_plaintxt,
int nbytes, unsigned char *iv)
{
struct ace_aes_context *ctx;
AES_RESULT res = AES_SUCCEEDED;
if((key == NULL)||(aligned_ciphertxt == NULL)||(aligned_plaintxt == NULL))
{
printf("Fatal error : key/plaintxt/ciphertxt NULL pointer error!\n");
res = AES_FAILED;
return res;
}
switch(key_len)
{
case KEY_128BITS :
case KEY_192BITS :
case KEY_256BITS : break;
default: printf("Fatal error : invalid key length!\n");
res = AES_KEY_NOT_SUPPORTED;
return res;
}
switch(mode)
{
case ACE_AES_ECB: break;
case ACE_AES_CBC:
case ACE_AES_CFB128:
case ACE_AES_OFB128: if(iv == NULL)
{
printf("Fatal error : iv NULL pointer error!\n");
res = AES_FAILED;
return res;
}
break;
default: printf("Fatal error : invalid cipher mode!\n");
res = AES_MODE_NOT_SUPPORTED;
return res;
}
if(nbytes == 0)
{
printf("no data need to be processed!\n");
res = AES_SUCCEEDED;
return res;
}
if(nbytes % 16)
{
printf("Fatal error : the length of plaintxt/ciphertxt must be multiples of 16bytes\n");
res = AES_NOT_BLOCKED;
return res;
}
if(UNALIGNED(aligned_plaintxt))
{
printf("plaintxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
if(UNALIGNED(aligned_ciphertxt))
{
printf("ciphertxt is not aligned with 16bytes!\n");
res = AES_ADDRESS_NOT_ALIGNED;
return res;
}
////////////////////////////////////////////////////////////////////////////////////////////
ctx = padlock_aes_begin();
res = padlock_aes_setkey( ctx, key, key_len);
if( res != AES_SUCCEEDED)
{
return res;
}
res = padlock_aes_setmodeiv( ctx, mode, iv);
if( res != AES_SUCCEEDED)
{
return res;
}
res = ace_aes_aligned_crypt( ctx, 0, aligned_ciphertxt, aligned_plaintxt, nbytes);
if( res != AES_SUCCEEDED)
{
return res;
}
padlock_aes_close( ctx);
return res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -