基于openswan klips的IPsec实现分析(九)加密算法维护

基于openswan klips的IPsec实现分析(九)加密算法维护

转载请注明出处:http://blog.csdn.net/rosetta

    本节将介绍klips加密算法的维护,并介绍如何增加新加密算法的支持,下一节将讲认证算法维护和增加。

    这里说的加密算法是指对称加密算法,是在通信过程中对传送的信息加解密时使用的,比如:AES,3DES,sm4等。

    加密算法对应的数据结构tructipsec_alg,其信息也是放在哈希表中:

    static structlist_head ipsec_alg_hash_table[IPSEC_ALG_HASHSZ];

相关数据结构

#define IPSEC_ALG_ST_SUPP   0x01

#define IPSEC_ALG_ST_REGISTERED 0x02

#define IPSEC_ALG_ST_EXCL   0x04

 

#define IPSEC_ALG_TYPE_AUTH14

#define IPSEC_ALG_TYPE_ENCRYPT  15

 

struct ipsec_alg_supported// pfkey_supported_list[]数据的每一个成员为此结构体。

{

         uint16_tias_exttype;//算法类型:加密或认证,IPSEC_ALG_TYPE_*

         uint8_t  ias_id;//算法ID,比如3des为3,aes为12。

         uint8_t  ias_ivlen;//IV长度,单位是bit,比如SM4的IV长度为128bit

         uint16_tias_keyminbits; //密钥支持最小位数。

         uint16_tias_keymaxbits;  //密钥支持最大位数。

       char    *ias_name;

};

 

struct ipsec_alg {

         unsignedixt_version;       /* only allow thisversion (or 'near')*/\  //IPSEC_ALG_VERSION

         structlist_head ixt_list;  /* dlinked list */ \  //内核标志的链表结构

         structmodule *ixt_module;    /* THIS_MODULE */ \ //THIS_MODULE

         unsignedixt_state;          /* state flags */ \ // IPSEC_ALG_ST_*三种

         atomic_tixt_refcnt;        /* ref. count whenpointed from ipsec_sa */ \ //被sa引用的次数

         charixt_name[16];  /* descriptive short name,eg. "3des" */ \ //算法名字,比如“3des”

         void*ixt_data;                   /* privatefor algo implementation */ \

         uint8_t  ixt_blocksize;   /* blocksize in bytes */ \//CBC模式分块大小,比如3des为8;SM4为16。

         structipsec_alg_supported ixt_support;

};

/*

 *   Note the const in cbc_encrypt IV arg:

 *   some ciphers like to toast passed IV (eg.3DES): make a local IV copy

 */

struct ipsec_alg_enc {

         structipsec_alg ixt_common;

         unsignedixt_e_keylen;             /* raw keylength in bytes */原始密钥长度,单位为字节,比如SM4为16.

         unsignedixt_e_ctx_size;          /*sa_p->key_e_size *///这个不知道什么用,SM4为16.

         int(*ixt_e_set_key)(struct ipsec_alg_enc *alg, __u8 *key_e, const __u8 *key,size_t keysize);

         //设置密钥函数指针

         __u8*(*ixt_e_new_key)(struct ipsec_alg_enc *alg, const __u8 *key, size_t keysize);

         void(*ixt_e_destroy_key)(struct ipsec_alg_enc *alg, __u8 *key_e);

         //销毁密钥函数指针

         int(*ixt_e_cbc_encrypt)(struct ipsec_alg_enc *alg, __u8 *key_e, __u8 *in, intilen, const __u8 *iv, int encrypt);

         //加密函数指针

};

相关函数接口

函数调用关系:

ipsec_klips_init()

->ipsec_alg_init()

->ipsec_3des_init()

  -> register_ipsec_alg_enc()

 

//内核增加加密算法支持

static inline int register_ipsec_alg_enc(structipsec_alg_enc *ixt) {

   return register_ipsec_alg((struct ipsec_alg*)ixt);

}          

//内核撤销此加密算法支持

static inline intunregister_ipsec_alg_enc(struct ipsec_alg_enc *ixt) {

   return unregister_ipsec_alg((struct ipsec_alg*)ixt);

}

register_ipsec_alg()函数

/*

 *Generic (enc, auth) registration entry point

 */

int register_ipsec_alg(structipsec_alg *ixt)

{

         intret=

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值