Linux内核加密算法,Linux Kernel(Android) 加密算法汇总(三)-应用程序调用内核加密算法接口...

本文将主要介绍,怎样在应用程序空间中(user space) 调用内核空间(kernel space)加密模块提供的加密算法API。

方法一:通过调用crypto: af_alg - User-space interface for Crypto API, Herbert Xu 2010年,给内核2.6.X 接口实现

以下依据以上方法实现应用程序调用内核加密算法接口演示样例:

该方法经过在内核层实现与CPU加密模块。或者硬件加密卡对接,并为上层应用程序提供接口的方式。能够实现硬件加密。

应用程序调用内核 hash

hash.c

#include

#include

#include

#ifndef AF_ALG

#define AF_ALG 38

#define SOL_ALG 279

#endif

int main(void)

{

int opfd;

int tfmfd;

struct sockaddr_alg sa = {

.salg_family = AF_ALG,

.salg_type = "hash",

.salg_name = "sha1"

};

char buf[20];

int i;

tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);

bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa));

opfd = accept(tfmfd, NULL, 0);

write(opfd, "abc", 3);

read(opfd, buf, 20);

for (i = 0; i < 20; i++) {

printf("%02x", (unsigned char)buf[i]);

}

printf("\n");

close(opfd);

close(tfmfd);

return 0;

}

Andrid.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := testhash

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := \

hash.c

include $(BUILD_EXECUTABLE)

编译完毕后在

adb push testhash /system/bin/

adb shell chmod a+x /system/bin/testhash

adb shell testhash

验证输出结果.

版权声明:本文博主原创文章。博客,未经同意不得转载。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值