Linux Kernel(Android) 密碼編譯演算法總結(三)-應用程式調用核心密碼編譯演算法介面

來源:互聯網
上載者:User

標籤:加密   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) 密碼編譯演算法總結(三)-應用程式調用核心密碼編譯演算法介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.