標籤:加密 hash 核心加密 sha1 aes
在Linux Kernel(Android) 密碼編譯演算法總結(cipher、compress、digest)文章中,介紹了如何在核心中加入三種不同類型的核心密碼編譯演算法, 並給出了在核心模組中如何調用他們的執行個體。
本文將主要介紹,如何在應用程式空間中(user space) 調用核心空間(kernel space)加密模組提供的密碼編譯演算法API。
方法一:通過調用crypto: af_alg - User-space interface for Crypto API, Herbert Xu <[email protected]> 2010年,給核心2.6.X 介面實現
具體情況請參考Linux Kernel(Android) 密碼編譯演算法總結(二)- A netlink-based user-space crypto API
下面根據以上方法實現應用程式調用核心密碼編譯演算法介面樣本:
該方法經過在核心層實現與CPU加密模組,或者硬體加密卡對接,並為上層應用程式提供介面的方式,可以實現硬體加密。
應用程式調用核心 hash
hash.c
<span style="font-size:18px;">#include <stdio.h>#include <sys/socket.h>#include <linux/if_alg.h> #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;}</span>
Andrid.mk
<span style="font-size:18px;">LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := testhashLOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := hash.cinclude $(BUILD_EXECUTABLE)</span>
編譯完成後在
adb push testhash /system/bin/
adb shell chmod a+x /system/bin/testhash
adb shell testhash
驗證輸出結果.
Linux Kernel(Android) 密碼編譯演算法總結(三)-應用程式調用核心密碼編譯演算法介面