关于Xen的AES库(转载注明出处,所有内容均为Superymk原创,版权归作者所有)
- 对应文件为xen\crypto\rijndael.h, 使用时要注意:
- 我暂时试验过256位,其它不明
- rijndael_ctx的enc_only设为0,除非只想加密
- rijndael_ctx的ek,dk都是有rijndaelKeySetupEnc和rijndaelKeySetupDec生成的
- key一定要是32字节长的
示例代码
- 仅用于示例,照copy肯定不能跑.
.c文件
case AES_TEST:
{
unsigned char key[] = "abcdefghijklmnopabcdefghijklmnop";
unsigned char in[16] = "abcdabcdabcdabcd";
unsigned char encrypted[32], decrypted[32];
uint32_t cipher_enckey[4*(KEY_LEN/32+7)], cipher_deckey[4*(KEY_LEN/32+7)], i;
rijndael_ctx ctx = {0};
aes_enckey_setup(key, &cipher_enckey);
aes_deckey_setup(key, &cipher_deckey);
for(i = 0;i < 4*(KEY_LEN/32+7) ; ++i)
{
ctx.ek[i] = cipher_enckey[i];
ctx.dk[i] = cipher_deckey[i];
}
ctx.Nr = (KEY_LEN/32)+6;
rijndael_encrypt(&ctx, in, encrypted);
encrypted[32]= '\0';
joan_dprintk("After encryption %s\n", encrypted);
rijndael_decrypt(&ctx, encrypted, decrypted);
decrypted[32]='\0';
joan_dprintk("After decryption %s\n", decrypted);
break;
}
.h文件
#include <crypto/rijndael.h>
#define KEY_LEN 256
//~ typedef u32 aes_int_key[4*(KEY_LEN/32+7)];
#define aes_enckey_setup(user_key,int_key) \
rijndaelKeySetupEnc((u32 *)(int_key), \
(u8 *)(user_key), \
KEY_LEN)
#define aes_deckey_setup(user_key,int_key) \
rijndaelKeySetupDec((u32 *)(int_key), \
(u8 *)(user_key), \
KEY_LEN)