Linux Kernel (Android) encryption algorithm Summary (iii)-Application call Kernel cryptographic algorithm interface
The method of how to invoke interfaces in the kernel is described.
This section is mainly about how to invoke the AES encryption algorithm of the Android C + + application calling OpenSSL.
Crypt_ssl.c
#include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include < aes.h> #include <sys/socket.h> #include <linux/if_alg.h> #include <evp.h> #include <stdio.h > #include <stdlib.h> #ifndef af_alg #define AF_ALG #define SOL_ALG 279#endif#define buf_size 16static voi D Crypt_ssl (char *in, int inlen, char *out,const char *key, char *iv) {Aes_key akey; Aes_set_encrypt_key (Key, N, &akey); Aes_cbc_encrypt (in, Out, Inlen, &akey, IV, 1);} int main (int argc, char **argv) {int i; Char Out[buf_size] = {0}; Char in[buf_size] = "Single Block MSG"; const char KEY[16] = "x06xa9x21x40x36xb8xa1x5b" "x51x2ex03xd5x34x12x00x06"; Char iv[16] = "x3dxafxbax42x9dx9exb4x30" "xb4x22xdax80x2cx9fxacx41"; if (argc! = 2) {printf ("Usage:compare openssl/compare kernel \ n"); } if (strncmp (argv[1], "OpenSSL", 7) = = 0) {printf ("Encrypt by OPENSSL...N"); Crypt_ssL (in, buf_size, out, key, iv); } for (i = 0; i < buf_size; i) printf ("0x", (unsigned char) out[i]); printf ("n"); return 0;}
compile files in Android source code:
Android.mk
Local_path: = $ (call My-dir) include $ (clear_vars) Local_module: = myencryptlocal_module_tags: = Optionallocal_src_files : = Crypt_ssl.clocal_shared_libraries: = libdl liblog libcryptolocal_c_includes: = external/ Openssl/include/openssl External/openssl/includeinclude $ (build_executable)
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 (iv)-application calls OpenSSL encryption algorithm