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

origin: http://blog.csdn.net/winceos/article/details/38035113

Linux Kernel(Android) 加密算法总结(cipher、compress、digest)文章中,介绍了如何在内核中加入三种不同类型的内核加密算法, 并给出了在内核模块中如何调用他们的实例。 

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


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

具体情况请参考Linux Kernel(Android) 加密算法总结(二)- A netlink-based user-space crypto API


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

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


应用程序调用内核 hash

hash.c

[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">#include <stdio.h>  
  2. #include <sys/socket.h>  
  3. #include <linux/if_alg.h>  
  4.    
  5. #ifndef AF_ALG  
  6. #define AF_ALG 38  
  7. #define SOL_ALG 279  
  8. #endif  
  9.    
  10. int main(void)  
  11. {  
  12.     int opfd;  
  13.     int tfmfd;  
  14.     struct sockaddr_alg sa = {  
  15.         .salg_family = AF_ALG,  
  16.         .salg_type = "hash",  
  17.         .salg_name = "sha1"  
  18.     };  
  19.     char buf[20];  
  20.     int i;  
  21.    
  22.     tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0);  
  23.    
  24.     bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa));  
  25.    
  26.     opfd = accept(tfmfd, NULL, 0);  
  27.    
  28.     write(opfd, "abc", 3);  
  29.     read(opfd, buf, 20);  
  30.    
  31.     for (i = 0; i < 20; i++) {  
  32.         printf("%02x", (unsigned char)buf[i]);  
  33.     }  
  34.     printf("\n");  
  35.    
  36.     close(opfd);  
  37.     close(tfmfd);  
  38.    
  39.     return 0;  
  40. }</span>  

Andrid.mk

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;">LOCAL_PATH := $(call my-dir)  
  2. include $(CLEAR_VARS)  
  3.   
  4. LOCAL_MODULE :testhash  
  5.   
  6. LOCAL_MODULE_TAGS :optional  
  7.   
  8. LOCAL_SRC_FILES := \  
  9.     hash.c  
  10.   
  11. include $(BUILD_EXECUTABLE)</span>  

编译完成后在

adb push testhash /system/bin/ 

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

adb shell testhash

验证输出结果.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值