📄 padlock_aes.c
字号:
AES_RESULT res = AES_SUCCEEDED;
if(ctx == NULL)
{
printf("Fatal error : ace_aes_ctx NULL pointer error!\n");
res = AES_FAILED;
return res;
}
if(key == NULL)
{
printf("Fatal error : key NULL pointer error!\n");
res = AES_FAILED;
return res;
}
switch(key_len)
{
case KEY_128BITS: real_key_len = 16;
rounds = 10;
break;
case KEY_192BITS: real_key_len = 24;
rounds = 12;
break;
case KEY_256BITS: real_key_len = 32;
rounds = 14;
break;
default: printf("Invalid Key Length!\n");
return AES_KEY_NOT_SUPPORTED;
}
ctx->key_length = key_len;
E_KEY[0] = (unsigned int)(*(const unsigned int *)(key));
E_KEY[1] = (unsigned int)(*(const unsigned int *)(key + 4));
E_KEY[2] = (unsigned int)(*(const unsigned int *)(key + 8));
E_KEY[3] = (unsigned int)(*(const unsigned int *)(key + 12));
switch (key_len)
{
case KEY_128BITS:
t = E_KEY[3];
for (i = 0; i < 10; ++i)
{
t = rotr(t, 8);
t = ls_box(t) ^ rco_tab[i];
t ^= E_KEY[4 * i];
E_KEY[4 * i + 4] = t;
t ^= E_KEY[4 * i + 1];
E_KEY[4 * i + 5] = t;
t ^= E_KEY[4 * i + 2];
E_KEY[4 * i + 6] = t;
t ^= E_KEY[4 * i + 3];
E_KEY[4 * i + 7] = t;
}
break;
case KEY_192BITS:
E_KEY[4] = (unsigned int)(*(const unsigned int *)(key + 16));
t = E_KEY[5] = (unsigned int)(*(const unsigned int *)(key + 20));
for (i = 0; i < 8; ++i)
{
t = rotr(t, 8);
t = ls_box(t) ^ rco_tab[i];
t ^= E_KEY[6 * i];
E_KEY[6 * i + 6] = t;
t ^= E_KEY[6 * i + 1];
E_KEY[6 * i + 7] = t;
t ^= E_KEY[6 * i + 2];
E_KEY[6 * i + 8] = t;
t ^= E_KEY[6 * i + 3];
E_KEY[6 * i + 9] = t;
t ^= E_KEY[6 * i + 4];
E_KEY[6 * i + 10] = t;
t ^= E_KEY[6 * i + 5];
E_KEY[6 * i + 11] = t;
}
break;
case KEY_256BITS:
E_KEY[4] = (unsigned int)(*(const unsigned int *)(key + 16));
E_KEY[5] = (unsigned int)(*(const unsigned int *)(key + 20));
E_KEY[6] = (unsigned int)(*(const unsigned int *)(key + 24));
t = E_KEY[7] = (unsigned int)(*(const unsigned int *)(key + 28));
for (i = 0; i < 7; ++i)
{
t = rotr(t, 8);
t = ls_box(t) ^ rco_tab[i];
t ^= E_KEY[8 * i];
E_KEY[8 * i + 8] = t;
t ^= E_KEY[8 * i + 1];
E_KEY[8 * i + 9] = t;
t ^= E_KEY[8 * i + 2];
E_KEY[8 * i + 10] = t;
t ^= E_KEY[8 * i + 3];
E_KEY[8 * i + 11] = t;
t = E_KEY[8 * i + 4] ^ ls_box(t);
E_KEY[8 * i + 12] = t;
t ^= E_KEY[8 * i + 5];
E_KEY[8 * i + 13] = t;
t ^= E_KEY[8 * i + 6];
E_KEY[8 * i + 14] = t;
t ^= E_KEY[8 * i + 7];
E_KEY[8 * i + 15] = t;
}
break;
}
D_KEY[0] = E_KEY[0];
D_KEY[1] = E_KEY[1];
D_KEY[2] = E_KEY[2];
D_KEY[3] = E_KEY[3];
for (i = 4; i < real_key_len + 24; ++i)
{
imix_col (D_KEY[i], E_KEY[i]);
}
for (i = real_key_len + 24; i < real_key_len + 28; ++i)
{
D_KEY[i] = E_KEY[i];
}
/* Reverse the extended keys for decryption. */
for (m = 0; m < rounds / 2; m++)
{
for (n = 0; n < 4; n++)
{
exch = D_KEY[4 * m + n];
D_KEY[4 * m + n] = D_KEY[4 * (rounds - m) + n];
D_KEY[4 * (rounds - m) + n] = exch;
}
}
return AES_SUCCEEDED;
}
AES_RESULT
ace_aes_atomic_crypt(struct ace_aes_context *ctx,
int enc,
unsigned char *src,
unsigned char *dst,
int nbytes)
{
AES_RESULT res = AES_SUCCEEDED;
int count = nbytes / 16;
size_t bsize = 16;
unsigned char buf[512];
unsigned char *p;
unsigned char *pkey;
unsigned char *pctrl;
unsigned char *aligned_iv;
p = ALIGN16(buf);
// Modified by Danial Miao 2004-06-08
// Note there is no difference between encryption and decrytion excepting the process of IV
// for cfb mode ciphergraphy See the original flow of cfb mode ciphergraphy in functions:
// do_cfb_encrypt() and do_cfb_decrypt() they all call *c->encrypt() to process the data
switch (ctx->key_length)
{
case KEY_128BITS:
memcpy(p, E_KEY, 16);
pkey = p;
p += 256; //Note: VIA ACE hardare requires 256bytes for storage of cipher key
// regardless the actual size of cipher key.
// Please refer to VIA Nehemiah Advanced Cryptography Engine Programming Guide
// for more details about VIA ACE
if (enc)
memcpy(p, &NEH_GENKEY_ENCRYPT_128, 4);
else
memcpy(p, &NEH_GENKEY_DECRYPT_128, 4);
pctrl = p;
p += 16;
break;
case KEY_192BITS:
if (enc )
{
memcpy(p, (unsigned char *) (E_KEY), 208);
pkey = p;
p += 256; //Note: VIA ACE hardare requires 256bytes for storage of cipher key
// regardless the actual size of cipher key.
// Please refer to VIA Nehemiah Advanced Cryptography Engine Programming Guide
// for more details about VIA ACE
memcpy(p, &NEH_LDKEY_ENCRYPT_192, 4);
pctrl = p;
p += 16;
}
else
{
if((ctx->mode == ACE_AES_CFB128) || (ctx->mode == ACE_AES_OFB128))
{
// Though we decrypt n blocks data,
// we load encryption extended key for
// via c5p ace hardware CFB mode 192 bits key aes algo
memcpy(p, E_KEY, 208);
pkey = p;
p += 256; //Note: VIA ACE hardare requires 256bytes for storage of cipher key
// regardless the actual size of cipher key.
// Please refer to VIA Nehemiah Advanced Cryptography Engine Programming Guide
// for more details about VIA ACE
memcpy(p, &NEH_LDKEY_DECRYPT_192, 4);
pctrl = p;
p += 16;
}
else
{
memcpy(p, D_KEY, 208);
pkey = p;
p += 256; //Note: VIA ACE hardare requires 256bytes for storage of cipher key
// regardless the actual size of cipher key.
// Please refer to VIA Nehemiah Advanced Cryptography Engine Programming Guide
// for more details about VIA ACE
memcpy(p, &NEH_LDKEY_DECRYPT_192, 4);
pctrl = p;
p += 16;
}
}
break;
case KEY_256BITS:
if (enc)
{
memcpy(p, (unsigned char *) (E_KEY), 240);
pkey = p;
p += 256; //Note: VIA ACE hardare requires 256bytes for storage of cipher key
// regardless the actual size of cipher key.
// Please refer to VIA Nehemiah Advanced Cryptography Engine Programming Guide
// for more details about VIA ACE
memcpy(p, &NEH_LDKEY_ENCRYPT_256, 4);
pctrl = p;
p += 16;
}
else
{
if((ctx->mode == ACE_AES_CFB128) || (ctx->mode == ACE_AES_OFB128))
{
// Though we decrypt n blocks data,
// we load encryption extended key for
// via c5p ace hardware CFB mode 256 bits key aes algo
memcpy(p, E_KEY, 240);
pkey = p;
p += 256; //Note: VIA ACE hardare requires 256bytes for storage of cipher key
// regardless the actual size of cipher key.
// Please refer to VIA Nehemiah Advanced Cryptography Engine Programming Guide
// for more details about VIA ACE
memcpy(p, &NEH_LDKEY_DECRYPT_256, 4);
pctrl = p;
p += 16;
}
else
{
memcpy(p, D_KEY, 240);
pkey = p;
p += 256; //Note: VIA ACE hardare requires 256bytes for storage of cipher key
// regardless the actual size of cipher key.
// Please refer to VIA Nehemiah Advanced Cryptography Engine Programming Guide
// for more details about VIA ACE
memcpy(p, &NEH_LDKEY_DECRYPT_256, 4);
pctrl = p;
p += 16;
}
}
break;
default:
printf("\nInvalid Key Length!\n");
return AES_KEY_NOT_SUPPORTED;
}
if(ctx->iv != NULL)
{
aligned_iv = p;
memcpy(aligned_iv, ctx->iv, bsize);
}
switch(ctx->mode)
{
case ACE_AES_ECB: ace_ecb_op5(pkey, pctrl, src, dst, count);
return res;
case ACE_AES_CBC: ace_cbc_op6(pkey, pctrl, src, dst, count, aligned_iv);
return res;
case ACE_AES_CFB128: ace_cfb_op6(pkey, pctrl, src, dst, count, aligned_iv);
return res;
case ACE_AES_OFB128: ace_ofb_op6(pkey, pctrl, src, dst, count, aligned_iv);
return res;
}
res = AES_FAILED;
return res;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -