Linux Kernel (Android) encryption algorithm Summary (iii)-Application call Kernel cryptographic algorithm interface

Source: Internet
Author: User



In the Linux Kernel (Android) Cryptographic algorithm summary (cipher, Compress, Digest) article, it describes how to include three different types of kernel encryption algorithms in the kernel, and gives examples of how to invoke them in kernel modules.

This article will focus on how to invoke the cryptographic algorithm API provided by the kernel space (kernel space) cryptographic module in application space (user spaces).


Method one: By calling the Crypto:af_alg-user-space interface for Crypto API, Herbert Xu <[email protected]> 2010, to the kernel 2.6.X interface implementation

For specific information, please refer to Linux Kernel (Android) encryption algorithm Summary (ii)-A netlink-based user-space Crypto API


The following method implements the application call Kernel cryptographic Algorithm interface example:

This method can realize the hardware encryption in the way that the kernel layer realizes the interface with the CPU encryption module, or the hardware encryption card, and provides interfaces for the upper application.


Application Call kernel 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,);     for (i = 0; i < 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>

after the compilation is complete ,

ADB push testhash/system/bin/

ADB shell chmod A+x/system/bin/testhash

ADB Shell Testhash

Verify the output results.


Linux Kernel (Android) encryption algorithm Summary (iii)-Application call Kernel cryptographic algorithm interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.