栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

使用Netlink AF

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Netlink AF

检查当前socket是否支持AF_ALG协议

af_alg_check.c

#include 
#include 
#include 
#include 
#include 
#include 

int main()
{
    int sockfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
    if(sockfd == -1)
    {
        if(errno == EAFNOSUPPORT)
        {
            printf("#define AF_ALG_UNAVAILABLEn");
        } else {
            printf("socket api occurs other errorsn");
        }
    } else {
        printf("#define AF_ALG_AVAILABLEn");
    }

    close(sockfd);
    return 0;
}

 编译并运行

# gcc af_alg_check.c -o af_alg_check
# ./af_alg_check
 #define AF_ALG_AVAILABLE

打印“#define AF_ALG_AVAILABLE” 说明socket支持AF_ALG协议;

打印“#define AF_ALG_UNAVAILABLE” 说明socket不支持AF_ALG协议。

实现自己的内核态密码模块接口

使用github上示例代码完成内核态密码模块

# git clone https://github.com/Ed-Yang/crypto-examples.git
# cd crypto-examples/
# cd aes-example
# make
# sudo dmesg -C
# sudo insmod aes-example.ko
# sudo rmmod aes-example.ko
# sudo dmesg
实现Netlink AF_ALG协议调用内核态密码模块里的对称加密

 使用github上示例代码完成加密算法的调用

# git clone https://github.com/nibrunie/af_alg-examples.git
# cd af_alg-examples
# make
cc -Wall -Werror  -o basic_cipher examples/basic_cipher.c -lcrypto
# ./basic_cipher
e353779c1079aeb82708942dbe77181a

使用 # cat /proc/crypto  查看内核态密码模块支持的算法名称

使用libkcapi库完成内核态密码模块的调用

libkcapi - Linux Kernel Crypto API User Space Interface Library. The Linux kernel exports a Netlink interface of type AF_ALG to allow user space to utilize the kernel crypto API. libkcapi uses this Netlink interface and exports easy to use APIs so that a developer does not need to consider the low-level Netlink interface handling.

# git clone https://github.com/smuellerDD/libkcapi.git
# cd libkcapi
# ./configure
# make
# make install

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323456.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号