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