LibTomCtypt中AES的描述子有两个,rijndael_desc和aes_desc,这二者除了name有区别外别的都一样,因此aes和rijndael是等效的。
注意:VC编译AES的时候记得将aes_table.c设置为“exclude file from build”,别的带_table的c文件类似。“exclude file from build”的设置方式为:右键点击aes_table.c,选择Settings → General → exclude file from build。
AES的测试例见aes.c的int ECB_TEST(void),记得在使用前需要先注册aes的描述子。
tomcrypt_cipher.h中对AES的描述为
//aes和rijndael是一个意思
#define aes_setup rijndael_setup
#define aes_ecb_encrypt rijndael_ecb_encrypt
#define aes_ecb_decrypt rijndael_ecb_decrypt
#define aes_test rijndael_test
#define aes_done rijndael_done
#define aes_keysize rijndael_keysize
#define aes_enc_setup rijndael_enc_setup
#define aes_enc_ecb_encrypt rijndael_enc_ecb_encrypt
#define aes_enc_keysize rijndael_enc_keysize
// aes和rijndael的相关函数有以下这些
int rijndael_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
int rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
int rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey);
int rijndael_test(void);
void rijndael_done(symmetric_key *skey);
int rijndael_keysize(int *keysize);
//带enc的这些函数是用于只需要加密而不需要解密的时候
int rijndael_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
int rijndael_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey);
void rijndael_enc_done(symmetric_key *skey);
int rijndael_enc_keysize(int *keysize);
// aes和rijndael的相关描述子以及仅用于加密的描述子
extern const struct ltc_cipher_descriptor rijndael_desc, aes_desc;
extern const struct ltc_cipher_descriptor rijndael_enc_desc, aes_enc_desc;
在aes.c里面对相关函数做了详细描述,以不使用单加密模式(即既需要加密又需要解密)为例
#define SETUP rijndael_setup
#define ECB_ENC rijndael_ecb_encrypt
#define ECB_DEC rijndael_ecb_decrypt
#define ECB_DONE rijndael_done
#define ECB_TEST rijndael_test
#define ECB_KS rijndael_keysize
rijndael_desc描述子的详情如下
const struct ltc_cipher_descriptor rijndael_desc =
{
"rijndael",
6,
16, 32, 16, 10,
SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
aes_desc描述子的详情如下
const struct ltc_cipher_descriptor aes_desc =
{
"aes",
6,
16, 32, 16, 10,
SETUP, ECB_ENC, ECB_DEC, ECB_TEST, ECB_DONE, ECB_KS,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
aes涉及的函数如下:
──────────────────────────────────────
int SETUP(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
// [功能] 初始化,主要做密钥扩展
──────────────────────────────────────
──────────────────────────────────────
int ECB_ENC(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
// [功能] 加密 (明文密文长度为一个分组大小)
──────────────────────────────────────
──────────────────────────────────────
int ECB_DEC(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
// [功能] 解密 (明文密文长度为一个分组大小)
──────────────────────────────────────
──────────────────────────────────────
int ECB_TEST(void)
// [功能] 测试函数
──────────────────────────────────────
──────────────────────────────────────
void ECB_DONE(symmetric_key *skey)
// [功能] 反初始化
//备注:建议将skey信息销毁
──────────────────────────────────────
──────────────────────────────────────
int ECB_KS(int *keysize)
// [功能] 根据输入的密钥大小,返回推荐的密钥大小
──────────────────────────────────────