OpenEuler basic experiment
20191331 lyx
Based on Kunpeng -- openeuer 20.03 64bit -- arm (Huawei cloud server)
Experimental environment: Kunpeng -- openeuer 20.03 64bit -- arm
Connect using Xshell
PS: Xshell is a very easy-to-use remote connection tool. Its built-in file management system can be very intuitive for file management. It is in line with our usual Windows system interaction logic and can carry out two-way file transfer very quickly.
OpenSSL compilation, installation and programming practice based on openEuler
- Check openssl version
Currently, the OpenSSL version installed in openEuler system is 1.1.1d, and now (2021.04) the latest version of OpenSSL is 1.1.1l
- There is no header file required for development in openEuler. We need to download the source code of OpenSSL, compile and install it ourselves.
1. First, go to the OpenSSL official website to download the latest version of OpenSSL source code.
Link: https://www.openssl.org/source/
2. Create two new folders to place the source code and installation path of OpenSSL respectively.
[root@arm ~]# mkdir lyx-opensslsrc lyx-openssl [root@arm ~]# cd lyx-openssl [root@arm lyx-openssl]# pwd /root/lyx-openssl
3. Upload and unzip the source code to lyx opensslsrc folder:
tar -zxvf openssl-1.1.1l.tar.gz -C ./
4. Configure the compilation and installation path (/ root / lyx OpenSSL), which is different from the default path of openEuler system.
cd rocopensslsrc/openssl-1.1.1k ./config --prefix=/root/rocopenssl
5. Compile (make takes a little longer and wait patiently), test and install. The test steps are optional:
make make test make install
6. Check the installed version and confirm that it is the latest installed version 1.1.1l:
cd ~/rocopenssl ./bin/openssl version
Installation succeeded:
Use of OpenSSL commands
View the help document through openssl help:
OpenSSL programming
The cryptographic algorithm library is very powerful and is the basis of OpenSSL. It implements most mainstream cryptographic algorithms and standards in modern cryptography, mainly including public key (asymmetric) algorithm, symmetric encryption algorithm, information digest algorithm, X509 digital certificate standard, PKCS12 personal information exchange Syntax Standard, PKCS7 encrypted message Syntax Standard OCSP online certificate status query protocol, CRL certificate revocation list and other standards. At the same time, OpenSSL also provides engine mechanism; Engine can seamlessly integrate external hardware algorithm modules such as encryption card and encryption machine into OpenSSL.
What is SSL
-
In order to make the network communication more secure, authentication and encryption are needed. Authentication means that you are the person you are looking for. Encryption is to make the third party who intercepts the intermediate message unable to get the message content.
-
To this end, someone designed SSL, that is, the security layer on the socket. In short, it is to make a secure communication layer on TCP. HTTP on SSL is HTTPS. Now almost all bank websites are accessed based on HTTPS protocol.
-
Authentication is solved by certificate + asymmetric encryption algorithm
So there is OpenSSL
-
SSL interaction process is still very complex, involving asymmetric encryption and symmetric encryption, as well as complex interaction process. Therefore, someone wrote openssl library, which is widely used. This paper explains how to install and basically use this library.
-
With this openssl library, you can directly write code for interaction with HTTPS.
OpenSSL features
openssl can realize: secret key certificate management, symmetric encryption and asymmetric encryption.
openssl: Multipurpose command line tools, packages openssl,You can execute interactive or batch commands. libcrypto: Encryption algorithm library, package openssl-libs. libssl: Encryption module application library, which realizes ssl and tls,package nss.
- As a security development package based on cryptography, OpenSSL provides quite powerful and comprehensive functions, including main cryptographic algorithms, common key and certificate encapsulation management functions and SSL protocol, and provides rich applications for testing or other purposes.
A simple test
#include <stdio.h> #include <openssl/evp.h> int main(){ OpenSSL_add_all_algorithms(); return 0; }
compile:
gcc -o to test_openssl.c -I /root/lyx-openssl/include -L /root/lyx-openssl/lib -lcrypto -lpthread
Execute. / to;echo $?:
BASE64 algorithm
Write a BASE64 test code testbase64.c:
#include <stdio.h> #include <string.h> #include <openssl/evp.h> #include <openssl/x509.h> //Base64 encoding void tEVP_Encode() { EVP_ENCODE_CTX *ctx; ctx = EVP_ENCODE_CTX_new(); //EVP coding structure unsigned char in[1024]; //Input data buffer int inl; //Input data length char out[2048]={0}; //Output data buffer int outl; //Output data length FILE *infp; //Input file handle FILE *outfp; //Output file handle infp = fopen("test.dat","rb");//Open the file to be encoded if(infp == NULL) { printf("Open File \"Test.dat\" for Read Err.\n"); return; } outfp = fopen("test.txt","w");//Open the file saved after encoding if(outfp == NULL) { printf("Open File \"test.txt\" For Write Err.\n"); return; } EVP_EncodeInit(ctx);//Base64 encoding initialization printf("file\"Test.dat\" Base64 After coding:\n"); //Loop to read the original text and call EVP_EncodeUpdate calculates Base64 encoding while(1) { inl = fread(in,1,1024,infp); if(inl <= 0) break; EVP_EncodeUpdate(ctx,out,&outl,in,inl);//code fwrite(out,1,outl,outfp);//Output encoding results to file printf("%s",out); } EVP_EncodeFinal(ctx,out,&outl);//Finish coding and output the final data. fwrite(out,1,outl,outfp); printf("%s",out); fclose(infp); fclose(outfp); printf("On file\"Test.dat\" Base64 Coding completed, save to\"test.txt\"file.\n\n\n"); } //Base64 decoding void tEVP_Decode() { EVP_ENCODE_CTX *ctx; ctx = EVP_ENCODE_CTX_new(); //EVP coding structure char in[1024]; //Input data buffer int inl; //Input data length unsigned char out[1024]; //Output data buffer int outl; //Output data length FILE *infp; //Input file handle FILE *outfp; //Output file handle infp = fopen("test.txt","r");//Open the file to be decoded if(infp == NULL) { printf("Open File \"Test.txt\" for Read Err.\n"); return; } outfp = fopen("test-1.dat","wb");//Open the file saved after decoding if(outfp == NULL) { printf("Open File \"test-1.txt\" For Write Err.\n"); return; } EVP_DecodeInit(ctx);//Base64 decoding initialization printf("Start editing files\"Test.txt\" Base64 decode...\n\n"); //Loop to read the original text and call EVP_DecodeUpdate Base64 decoding while(1) { inl = fread(in,1,1024,infp); if(inl <= 0) break; EVP_DecodeUpdate(ctx,out,&outl,in,inl);//Base64 decoding fwrite(out,1,outl,outfp);//output to a file } EVP_DecodeFinal(ctx,out,&outl);//Complete decoding and output the last data. fwrite(out,1,outl,outfp); fclose(infp); fclose(outfp); printf("On file\"Test.txt\" Base64 Decoding completed, save as\"test-1.dat\"\n\n\n"); } int main() { tEVP_Encode(); tEVP_Decode(); return 0; }
Compile through GCC - O testbase64 testbase64. C - I / root / lyx OpenSSL / include - L / root / lyx OpenSSL / lib - lcrypto - lpthread - put charset = GBK - fexec charset = UTF-8:
An error occurred:
The reason is the lack of Chinese font library
Install the necessary font library:
yum groupinstall -y fonts
Recompile successfully:
Execute. / testbase64:
test.dat is a binary file with the contents of 20191331 lyx hello BASE64
Assembly language practice
3.2 assembly language practice - finding the maximum number
- 1. Experimental purpose
Through this experiment, understand and be familiar with ARM64 assembly language.
-
2. Experimental requirements
-
ARM64 assembly language is used to realize the following functions: find the maximum number in a given set of numbers.
-
The program can be compiled using GCC (Aarch64 version) tool, and can be run on raspberry pie Linux system or QEMU + ARM64 experimental platform.
-
.section .data .align 3 my_data: .quad 1 .quad 2 .quad 5 .quad 8 .quad 10 .quad 12 my_data_count: .quad 6 .align 3 print_data: .string "big data: %d\n" .section .text .globl main main: stp x29, x30, [sp, -16]! ldr x0, =my_data ldr x1, my_data_count add x4, x0, #40 mov x3, xzr 1: ldr x2, [x0], #8 cmp x2, x3 csel x3, x2, x3, hi cmp x0, x4 b.ls 1b ldr x0, =print_data mov x1, x3 bl printf ldp x29, x30, [sp], 16 ret
Compile run:
A mistake was made at compile time: forget that the assembly code suffix is. s
Commissioning:
3.3 assembly language practice - calling assembly functions through C language
- 1. Experimental purpose
Through this experiment, understand and be familiar with how to call assembly functions in C language.
-
2. Experimental requirements
-
ARM64 assembly language is used to realize the following functions: find the maximum number in a given set of numbers.
-
The program can be compiled using GCC (Aarch64 version) tool, and can be run on raspberry pie Linux system or QEMU + ARM64 experimental platform.
-
*****************compare.S****************** .section .text .globl compare_data compare_data: cmp x0, x1 csel x0, x0, x1, hi ret
*******************main.c******************** #include <stdio.h> extern int compare_data(int a, int b); int main() { int val; val = compare_data(5, 6); printf("big data: %d\n", val); }
Compile run:
3.4 assembly language practice - calling C functions through assembly language
- 1. Experimental purpose
Through this experiment, understand and be familiar with how to call assembly functions in C language.
-
2. Experimental requirements
-
ARM64 assembly language is used to realize the following functions: find the maximum number in a given set of numbers.
-
The program can be compiled using GCC (Aarch64 version) tool, and can be run on raspberry pie Linux system or QEMU + ARM64 experimental platform.
-
*************compare.c************ int compare_data(int a, int b) { return (a >= b) ? a : b; }
***************main.s************** .section .data .align 3 print_data: .string "big data: %d\n" .section .text .globl main main: stp x29, x30, [sp, -16]! mov x0, #6 mov x1, #5 bl compare_data mov x1, x0 ldr x0, =print_data bl printf ldp x29, x30, [sp], 16 ret
Compile run:
3.5 assembly language practice - GCC inline assembly
#include <stdio.h> static int compare_data(int a, int b) { int val; asm volatile ( "cmp %1, %2\n" "csel %0, %1, %2, hi\n" : "+r" (val) : "r" (a), "r" (b) : "memory"); return val; } int main() { int val; val = compare_data(5, 6); printf("big data: %d\n", val); val = compare_data(6, 4); printf("big data: %d\n", val); }
Compile run:
reference material
OpenEuler system installation https://www.cnblogs.com/rocedu/p/14615565.html
OpenSLL compilation, installation and programming foundation https://www.cnblogs.com/rocedu/p/14617763.html
Introduction and use of OpenSSL https://blog.csdn.net/zhaoem82/article/details/102544993