String literal processing method, device, electronic device and storage medium
By encrypting and decrypting string literals in the intermediate representation layer of LLVM, the problem of string literal storage security is solved, and the security improvement is achieved that the original string literals cannot be searched in the file but can be used normally when the program is running.
Patent Information
- Application Number
- CN202111288189.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-02
- Publication Date
- 2025-04-08
- Estimated Expiration
- 2041-11-02
AI Technical Summary
How to improve the security of string literal storage and prevent sensitive information from being acquired by attackers.
The intermediate representation layer of LLVM uses a preset encryption algorithm to encrypt each string literal, and records the encryption information in the cache area, and calls the decryption function for decryption when needed.
Through the encryption and decryption process, the stored string literals cannot be directly searched, which improves storage security. At the same time, it can be decrypted and used normally when the program is running, and the entire process does not require manual participation.
Smart Images

Figure CN114003932B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and more specifically, to a method, device, electronic device and storage medium for processing string literals. Background Art
[0002] Strings embedded in source code (i.e., character sequences between two double quotes) are called string literals, such as "string" and "literal". LLVM is a complete compiler framework that can parse string literals in source code and place the parsed results in the appropriate location in the compiled results. In the C / C++ programming language international standard, string literals need to have static storage areas. The so-called static storage area means that the objects stored in it will always exist during the execution of the program and will not be discarded. Therefore, LLVM usually places string literals in the read-only storage area of the file. Anyone only needs to perform a simple scan and parse of the read-only storage area to extract all the string literals in the program.
[0003] Since string literals may contain sensitive information, such as customer phone numbers, file storage paths, contract signing dates, etc., if string literals are obtained by attackers, not only will the program logic be easily compromised, but it may also cause significant economic losses.
[0004] Therefore, how to improve the security of string literal storage has become a technical problem that technical personnel in this field need to solve urgently. Summary of the invention
[0005] In view of this, the present invention discloses a method, device, electronic device and storage medium for processing string literals, so as to improve the security of string literal storage without human intervention.
[0006] A method for processing a string literal, comprising:
[0007] Encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each encrypted string literal and the corresponding encryption information in a buffer, wherein the byte length of each encrypted string literal is not less than the byte length of the corresponding string literal;
[0008] Replacing each of the string literals in the original intermediate representation with the corresponding encrypted string literals to obtain a target intermediate representation;
[0009] When all the encrypted string literals in the target intermediate representation are about to be accessed, a decryption function corresponding to the preset encryption algorithm is called, and the decryption function decrypts each of the encrypted string literals based on the encryption information to obtain the corresponding string literal, until all the encrypted string literals are completely decrypted.
[0010] Optionally, the step of encrypting each of the string literals using a preset encryption algorithm to generate an encrypted string literal, and recording each of the encrypted string literals and corresponding encryption information in a buffer area specifically includes:
[0011] Calling the LLVM optimization module to optimize the original intermediate representation to obtain an optimized intermediate representation;
[0012] Encrypting each of the string literals in the optimized intermediate representation using the preset encryption algorithm to generate the encrypted string literal;
[0013] Record each of the encrypted string literals and the corresponding encryption information.
[0014] Optionally, when all the encrypted string literals in the target intermediate representation are about to be accessed, calling a decryption function corresponding to the preset encryption algorithm specifically includes:
[0015] Before calling the main function, calling the decryption function corresponding to the preset encryption algorithm;
[0016] or,
[0017] Before the dynamic library is loaded into the program, the decryption function corresponding to the preset encryption algorithm is called.
[0018] Optionally, the preset encryption algorithm is any one of the AES encryption algorithm, the SM3 algorithm and the SM4 algorithm.
[0019] Optionally, when the preset encryption algorithm is the AES encryption algorithm, the encryption information includes: a key and an initial vector corresponding to the string literal.
[0020] A device for processing string literals, comprising:
[0021] An encryption unit, used to encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each of the encrypted string literals and corresponding encryption information in a buffer, wherein the byte length of each of the encrypted string literals is not less than the byte length of the corresponding string literal;
[0022] A replacement unit, used for replacing each of the string literals in the original intermediate representation with the corresponding encrypted string literals to obtain a target intermediate representation;
[0023] A decryption unit is used to call a decryption function corresponding to the preset encryption algorithm when all the encrypted string literals in the target intermediate representation are about to be accessed, and the decryption function decrypts each of the encrypted string literals based on the encryption information to obtain the corresponding string literal until all the encrypted string literals are completely decrypted.
[0024] Optionally, the encryption unit specifically includes:
[0025] An optimization subunit, used for calling an LLVM optimization module to optimize the original intermediate representation to obtain an optimized intermediate representation;
[0026] An encryption subunit, configured to encrypt each of the string literals in the optimized intermediate representation using the preset encryption algorithm to generate the encrypted string literal;
[0027] The recording subunit is used to record each of the encrypted string literals and the corresponding encryption information.
[0028] Optionally, the decryption unit is specifically used for:
[0029] Before calling the main function, calling the decryption function corresponding to the preset encryption algorithm;
[0030] or,
[0031] Before the dynamic library is loaded into the program, the decryption function corresponding to the preset encryption algorithm is called.
[0032] An electronic device comprising a memory and a processor;
[0033] The memory is used to store at least one instruction;
[0034] The processor is used to execute the at least one instruction to implement the string literal processing method described above.
[0035] A computer-readable storage medium stores at least one instruction, and when the at least one instruction is executed by a processor, the above-mentioned string literal processing method is implemented.
[0036] From the above technical solution, it can be known that the present invention discloses a method, device, electronic device and storage medium for processing string literals, encrypts each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and records each encrypted string literal and the corresponding encryption information in the cache area, replaces each string literal in the original intermediate representation with the corresponding encrypted string literal to obtain a target intermediate representation, and when all the encrypted string literals in the target intermediate representation are about to be accessed, calls a decryption function corresponding to the preset encryption algorithm, and the encryption function decrypts each encrypted string literal based on the encryption information to obtain the corresponding string literal. The present invention encrypts each string literal in the intermediate representation layer of LLVM, and decrypts when all the encrypted string literals are about to be accessed, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, but when the program is running, each encrypted string literal has been decrypted, thereby greatly improving the security of string literal storage, and the whole process does not require manual participation, saving time and effort. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the disclosed drawings without paying creative work.
[0038] Figure 1 A flow chart of a method for processing string literals disclosed in an embodiment of the present invention;
[0039] Figure 2 A flow chart of a method for generating an encrypted string literal disclosed in an embodiment of the present invention;
[0040] Figure 3 A flow chart of another method for processing string literals disclosed in an embodiment of the present invention;
[0041] Figure 4 A schematic diagram of the structure of another device for processing string literals disclosed in an embodiment of the present invention;
[0042] Figure 5 It is a schematic diagram of the structure of an electronic device disclosed in an embodiment of the present invention. DETAILED DESCRIPTION
[0043] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0044] The embodiment of the present invention discloses a method, device, electronic device and storage medium for processing string literals. Each string literal in an original intermediate representation based on LLVM is encrypted using a preset encryption algorithm to generate an encrypted string literal, and each encrypted string literal and the corresponding encryption information are recorded in a buffer area. Each string literal in the original intermediate representation is replaced with a corresponding encrypted string literal to obtain a target intermediate representation. When all encrypted string literals in the target intermediate representation are about to be accessed, a decryption function corresponding to the preset encryption algorithm is called, and each encrypted string literal is decrypted based on the encryption information to obtain the corresponding string literal, until all encrypted string literals are completely decrypted. The present invention encrypts each string literal in the intermediate representation layer of LLVM, and decrypts when all encrypted string literals are about to be accessed, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, but when the program is running, each encrypted string literal has been decrypted, thereby greatly improving the security of string literal storage, and the whole process does not require manual participation, saving time and labor.
[0045] See also Figure 1 , a flow chart of a method for processing a string literal disclosed in an embodiment of the present invention, the method comprising:
[0046] Step S101: encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each encrypted string literal and corresponding encryption information in a buffer area;
[0047] The byte length of each encrypted string literal is not less than the byte length of the corresponding string literal.
[0048] The working principle of each layer of LLVM is as follows:
[0049] 1. Compiler front end: The written source code is preprocessed, lexically analyzed, parsed, and semantically analyzed to finally generate an intermediate representation IR. Preprocessing, lexically analyzed, parsed, and semantically analyzed are the processes of parsing the source code. The specific working principles of preprocessing, lexically analyzed, parsed, and semantically analyzed can be found in existing mature solutions and will not be elaborated here.
[0050] 2. Intermediate representation IR: After fully parsing the information carried by the source code, use another way of expression to more efficiently express information that is completely equivalent to the original code.
[0051] 3. Compiler backend: Generates computer executable files based on the intermediate representation IR.
[0052] Based on the above principles, the inventors of the present invention have found after research that the process of string literal encryption and insertion decryption algorithm is most suitable at the IR layer of LLVM. The reasons are: 1. The front end of the compiler will remove unnecessary string literals and merge repeated string literals; 2. The back end of the compiler can directly generate various platform architecture files.
[0053] Therefore, the present invention uses a preset encryption algorithm to encrypt each string literal in the original intermediate representation to generate an encrypted string literal. Assuming that the i-th string literal is STRi and the byte length is Li, the corresponding encrypted string literal obtained after encrypting the i-th string literal STRi is STRii and the byte length is Lii, wherein Lii≥Li needs to be ensured, otherwise useless bytes need to be filled to satisfy Lii≥Li.
[0054] Among them, encryption information refers to the necessary parameters used when encrypting string literals. For example, XOR encryption needs to record the values involved in the XOR operation, and AES needs to record the initial vector iv and the key key.
[0055] Step S102: Replace each string literal in the original intermediate representation with the corresponding encrypted string literal to obtain a target intermediate representation;
[0056] For example, the i-th string literal in the original intermediate representation is STRRi, and the corresponding encrypted string literal obtained after encrypting the i-th string literal STRRi is STRii. Then, all STRi in the original intermediate representation are replaced with the corresponding STRii to obtain the target intermediate representation.
[0057] Step S103: When all the encrypted string literals in the target intermediate representation are about to be accessed, a decryption function corresponding to a preset encryption algorithm is called, and the decryption function decrypts each encrypted string literal based on the encryption information to obtain a corresponding string literal, until all the encrypted string literals are decrypted.
[0058] A decryption function is a piece of code that can decrypt an encrypted string literal. A decryption function can be understood as a set of codes that complete a certain task.
[0059] When encrypting the original string literal, the compiler records the original string literal's serial number, encryption algorithm, and encryption-related information. When the compiler places the decryption function, it determines the decryption function corresponding to the encryption algorithm based on the serial number of the original string literal, and passes the encryption-related information to the decryption function.
[0060] It should be noted that different preset encryption algorithms correspond to different decryption functions. When encrypting each string literal, the same preset encryption algorithm can be used for all string literals, or different preset encryption algorithms can be used for different string literals. It is only necessary to ensure that the decryption function corresponding to the preset encryption algorithm is used when decrypting the encrypted string literal.
[0061] In this embodiment, when decrypting all encrypted string literals in the target intermediate representation, it is necessary to call the decryption function again for decryption when decrypting each encrypted string literal.
[0062] Assuming that there are k encrypted string literals in the target intermediate representation, when all the encrypted string literals in the target intermediate representation are about to be accessed, for each encrypted string literal, it is necessary to call the decryption function corresponding to the preset encryption algorithm to decrypt it to obtain the corresponding string literal. When all the encrypted string literals are decrypted, the subsequent access operations on each decrypted string literal are performed.
[0063] In actual applications, the decryption function records the correspondence between each encrypted string literal and the encryption information, finds the encryption information of the encrypted string literal corresponding to the unique identifier of the encrypted string literal to be decrypted, and decrypts the encrypted string literal based on the encryption information to obtain the corresponding string literal.
[0064] In summary, the present invention discloses a method for processing string literals, wherein each string literal in the original intermediate representation based on LLVM is encrypted using a preset encryption algorithm to generate an encrypted string literal, and each encrypted string literal and the corresponding encryption information are recorded in a buffer area, each string literal in the original intermediate representation is replaced with a corresponding encrypted string literal to obtain a target intermediate representation, and when all encrypted string literals in the target intermediate representation are about to be accessed, a decryption function corresponding to the preset encryption algorithm is called, and each encrypted string literal is decrypted based on the encryption information to obtain a corresponding string literal, until all encrypted string literals are completely decrypted. The present invention encrypts each string literal in the intermediate representation layer of LLVM, and decrypts when all encrypted string literals are about to be accessed, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, but when the program is running, each encrypted string literal has been decrypted, thereby greatly improving the security of string literal storage, and the whole process does not require manual participation, saving time and effort.
[0065] In addition, encrypting and decrypting each string literal in the intermediate representation layer of LLVM will not affect any semantics of the developer's source code, and this solution is compatible with any platform architecture supported by the compiler backend.
[0066] In order to prevent simple encryption algorithms from being exhausted and deciphered by computer automation, the preset encryption algorithm in the present invention adopts a highly secure encryption algorithm. For example, the preset encryption algorithm is any one of the AES (Advanced Encryption Standard) encryption algorithm, the SM3 (domestic hash algorithm) algorithm and the SM4 algorithm. Of course, the preset encryption algorithm can also adopt other highly secure encryption algorithms other than these three algorithms, which are not listed here.
[0067] Assume that when the preset encryption algorithm is the AES encryption algorithm, the encryption information includes: the key KEYi and the initial vector IVi corresponding to the i-th string literal STRi.
[0068] It should be noted that AES is a block encryption standard adopted by the US government and one of the most popular algorithms in symmetric key encryption, with a solid theoretical foundation in terms of security. The initial vector and key are specified by the AES algorithm itself.
[0069] To further optimize the above embodiment, the present invention also provides a process for generating an original intermediate representation, which is as follows:
[0070] The LLVM compiler front end is called to parse the original C / C++ source code to generate the original intermediate representation.
[0071] Among them, the process of parsing the original C / C++ source code by the LLVM compiler front end includes: preprocessing, lexical analysis, syntax analysis and semantic analysis.
[0072] In the above embodiment, there are mainly two situations when all encrypted string literals in the target intermediate representation are about to be accessed: the first one is before the main function is called; the second one is before the dynamic library is loaded into the program.
[0073] That is, before the main function is called, the decryption function corresponding to the preset encryption algorithm is called, or before the dynamic library is loaded into the program, the decryption function corresponding to the preset encryption algorithm is called.
[0074] LLVM is a compiler that can convert source code written by programmers into programs that can actually be executed by computers. When the program is stored, a certain compilation structure is required, and dynamic library is one of the compilation structures.
[0075] A dynamic library is a file that stores code snippets. It can import code snippets that did not originally exist in the program into the program. When a code snippet is imported, a string literal may be imported.
[0076] When a C / C++ program is running, the code written by the developer is usually executed after calling the main function, or when the dynamic library is loaded, after the dynamic library is loaded into the program. Therefore, the present invention calls the decryption function to decrypt each encrypted string literal before calling the main function, or calls the decryption function to decrypt each encrypted string literal before the dynamic library is loaded into the program, so as to ensure that when the string literal is accessed, the encrypted string literal has been restored to the string literal in the original code. In this way, when the program is running, after the decryption function completes the decryption of each encrypted string literal, the data stored in the memory area originally representing the encrypted string literal STRii is completely consistent with the original string literal STRi, so that STRi cannot be searched in the file where STRii is placed, but when the program is running, STRii has been decrypted and restored to STRi.
[0077] In order to improve the accuracy of each string literal in the original intermediate representation, the present invention optimizes the original intermediate representation before encrypting each string literal.
[0078] Therefore, to further optimize the above embodiment, see Figure 2 , a flow chart of a method for generating an encrypted string literal disclosed in an embodiment of the present invention, that is, Figure 1 Step S101 in the illustrated embodiment may specifically include:
[0079] Step S201, calling the LLVM optimization module to optimize the original intermediate representation to obtain an optimized intermediate representation;
[0080] Among them, the specific working principle of the LLVM optimization module to optimize the original intermediate representation can be found in the existing mature solutions, which will not be repeated here.
[0081] Step S202: encrypt each string literal in the optimized intermediate representation using a preset encryption algorithm to generate an encrypted string literal;
[0082] In order to prevent simple encryption algorithms from being exhausted and deciphered by computer automation, the preset encryption algorithm in the present invention adopts a highly secure encryption algorithm. For example, the preset encryption algorithm is any one of the AES (Advanced Encryption Standard) encryption algorithm, the SM3 (domestic hash algorithm) algorithm and the SM4 algorithm. Of course, the preset encryption algorithm can also adopt other highly secure encryption algorithms other than these three algorithms, which are not listed here.
[0083] Step S203: record each encrypted string literal and the corresponding encryption information.
[0084] In summary, the present invention optimizes the original intermediate representation by calling the LLVM optimization module, and encrypts each string literal in the optimized intermediate representation using a preset encryption algorithm, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, thereby improving the security of string literal storage. By recording each encrypted string and the corresponding encryption information, it is convenient to decrypt each encrypted string literal based on the encryption information to obtain the corresponding string literal when the encrypted string literal is about to be accessed, thereby facilitating subsequent program operation.
[0085] To facilitate understanding of the entire processing process of string literals disclosed in the present invention, a detailed processing scheme for string literals is provided below, as follows:
[0086] See also Figure 3 , a flow chart of another method for processing string literals disclosed in an embodiment of the present invention, the method comprising:
[0087] Step S301, calling the LLVM optimization module to optimize the original intermediate representation to obtain an optimized intermediate representation;
[0088] Step S302: encrypt each string literal in the optimized intermediate representation using a preset encryption algorithm to generate an encrypted string literal, and record each encrypted string literal and corresponding encryption information in a buffer area;
[0089] The byte length of each encrypted string literal is not less than the byte length of the corresponding string literal.
[0090] In order to prevent simple encryption algorithms from being exhausted and deciphered by computer automation, the preset encryption algorithm in the present invention adopts a highly secure encryption algorithm. For example, the preset encryption algorithm is any one of the AES (Advanced Encryption Standard) encryption algorithm, the SM3 (domestic hash algorithm) algorithm and the SM4 algorithm. Of course, the preset encryption algorithm can also adopt other highly secure encryption algorithms other than these three algorithms, which are not listed here.
[0091] Step S303: Replace each string literal in the original intermediate representation with the corresponding encrypted string literal to obtain a target intermediate representation;
[0092] Step S304: before the main function is called or before the dynamic library is loaded into the program, a decryption function corresponding to the preset encryption algorithm is called, and the decryption function decrypts the current encrypted string literal based on the encryption information to obtain the corresponding string literal;
[0093] Step S305: determine whether there is any undecrypted encrypted string literal at the current moment, if yes, return to step S304, if no, execute step S306;
[0094] Step S306: call the main function or load the dynamic library into the program.
[0095] In summary, the present invention optimizes the original intermediate representation by calling the LLVM optimization module, and encrypts each string literal in the optimized intermediate representation using a preset encryption algorithm, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, thereby improving the security of string literal storage. By recording each encrypted string and the corresponding encryption information, it is convenient to decrypt each encrypted string literal based on the encryption information to obtain the corresponding string literal when the encrypted string literal is about to be accessed. By encrypting each string literal in the intermediate representation layer of LLVM and decrypting all encrypted string literals when they are about to be accessed, the original string literal cannot be searched in the file where the encrypted string literal is placed, but when the program is running, each encrypted string literal has been decrypted, thereby greatly improving the security of string literal storage, and the whole process does not require manual participation, saving time and effort.
[0096] Corresponding to the above method embodiment, the present invention also discloses a device for processing string literals.
[0097] See also Figure 4 , a schematic diagram of a structure of a device for processing string literals disclosed in an embodiment of the present invention, the device comprising:
[0098] The encryption unit 401 is used to encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each encrypted string literal and corresponding encryption information in a buffer area;
[0099] The byte length of each encrypted string literal is not less than the byte length of the corresponding string literal.
[0100] The byte length of each encrypted string literal is not less than the byte length of the corresponding string literal.
[0101] The working principle of each layer of LLVM is as follows:
[0102] 1. Compiler front end: The written source code is preprocessed, lexically analyzed, parsed, and semantically analyzed to finally generate an intermediate representation IR. Preprocessing, lexically analyzed, parsed, and semantically analyzed are the processes of parsing the source code. The specific working principles of preprocessing, lexically analyzed, parsed, and semantically analyzed can be found in existing mature solutions and will not be elaborated here.
[0103] 2. Intermediate representation IR: After fully parsing the information carried by the source code, use another way of expression to more efficiently express information that is completely equivalent to the original code.
[0104] 3. Compiler backend: Generates computer executable files based on the intermediate representation IR.
[0105] Based on the above principles, the inventors of the present invention have found after research that the process of string literal encryption and insertion decryption algorithm is most suitable at the IR layer of LLVM. The reasons are: 1. The front end of the compiler will remove unnecessary string literals and merge repeated string literals; 2. The back end of the compiler can directly generate various platform architecture files.
[0106] Therefore, the present invention uses a preset encryption algorithm to encrypt each string literal in the original intermediate representation to generate an encrypted string literal. Assuming that the i-th string literal is STRi and the byte length is Li, the corresponding encrypted string literal obtained after encrypting the i-th string literal STRi is STRii and the byte length is Lii, wherein Lii≥Li needs to be ensured, otherwise useless bytes need to be filled to satisfy Lii≥Li.
[0107] Among them, encryption information refers to the necessary parameters used when encrypting string literals. For example, XOR encryption needs to record the values involved in the XOR operation, and AES needs to record the initial vector iv and the key key.
[0108] A replacement unit 402, configured to replace each of the string literals in the original intermediate representation with the corresponding encrypted string literals to obtain a target intermediate representation;
[0109] For example, the i-th string literal in the original intermediate representation is STRRi, and the corresponding encrypted string literal obtained after encrypting the i-th string literal STRRi is STRii. Then, all STRi in the original intermediate representation are replaced with the corresponding STRii to obtain the target intermediate representation.
[0110] The decryption unit 403 is used to call the decryption function corresponding to the preset encryption algorithm when all the encrypted string literals in the target intermediate representation are about to be accessed, and the decryption function decrypts each of the encrypted string literals based on the encryption information to obtain the corresponding string literal until all the encrypted string literals are completely decrypted.
[0111] A decryption function is a piece of code that can decrypt an encrypted string literal. A decryption function can be understood as a set of codes that complete a certain task.
[0112] When encrypting the original string literal, the compiler records the original string literal's serial number, encryption algorithm, and encryption-related information. When the compiler places the decryption function, it determines the decryption function corresponding to the encryption algorithm based on the serial number of the original string literal, and passes the encryption-related information to the decryption function.
[0113] It should be noted that different preset encryption algorithms correspond to different decryption functions. When encrypting each string literal, the same preset encryption algorithm can be used for all string literals, or different preset encryption algorithms can be used for different string literals. It is only necessary to ensure that the decryption function corresponding to the preset encryption algorithm is used when decrypting the encrypted string literal.
[0114] In this embodiment, when decrypting all encrypted string literals in the target intermediate representation, it is necessary to call the decryption function again for decryption when decrypting each encrypted string literal.
[0115] In summary, the present invention discloses a device for processing string literals, which uses a preset encryption algorithm to encrypt each string literal in the original intermediate representation based on LLVM to generate an encrypted string literal, and records each encrypted string literal and the corresponding encryption information in the buffer area, replaces each string literal in the original intermediate representation with the corresponding encrypted string literal to obtain a target intermediate representation, and when all the encrypted string literals in the target intermediate representation are about to be accessed, calls a decryption function corresponding to the preset encryption algorithm, and decrypts each encrypted string literal based on the encryption information to obtain the corresponding string literal, until all the encrypted string literals are completely decrypted. The present invention encrypts each string literal in the intermediate representation layer of LLVM, and decrypts all the encrypted string literals when they are about to be accessed, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, but when the program is running, each encrypted string literal has been decrypted, thereby greatly improving the security of string literal storage, and the whole process does not require manual participation, saving time and effort.
[0116] In addition, encrypting and decrypting each string literal in the intermediate representation layer of LLVM will not affect any semantics of the developer's source code, and this solution is compatible with any platform architecture supported by the compiler backend.
[0117] In order to improve the accuracy of each string literal in the original intermediate representation, the present invention optimizes the original intermediate representation before encrypting each string literal.
[0118] Therefore, the encryption unit 401 may specifically include:
[0119] An optimization subunit, used for calling an LLVM optimization module to optimize the original intermediate representation to obtain an optimized intermediate representation;
[0120] An encryption subunit, configured to encrypt each of the string literals in the optimized intermediate representation using the preset encryption algorithm to generate the encrypted string literal;
[0121] The recording subunit is used to record each of the encrypted string literals and the corresponding encryption information.
[0122] In summary, the present invention optimizes the original intermediate representation by calling the LLVM optimization module, and encrypts each string literal in the optimized intermediate representation using a preset encryption algorithm, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, thereby improving the security of string literal storage. By recording each encrypted string and the corresponding encryption information, it is convenient to decrypt each encrypted string literal based on the encryption information to obtain the corresponding string literal when the encrypted string literal is about to be accessed, thereby facilitating subsequent program operation.
[0123] In the above embodiment, there are two main situations when all encrypted string literals in the target intermediate representation are about to be accessed: the first one is before the main function is called; the second one is before the dynamic library is loaded into the program. Therefore, the decryption unit 403 can be specifically used for:
[0124] Before calling the main function, calling the decryption function corresponding to the preset encryption algorithm;
[0125] Alternatively, before the dynamic library is loaded into the program, the decryption function corresponding to the preset encryption algorithm is called.
[0126] It should be noted that for the specific working principles of each component in the device embodiment, please refer to the corresponding part of the method embodiment, which will not be repeated here.
[0127] Corresponding to the above embodiment, Figure 5 As shown, the present invention also provides an electronic device, which may include: a processor 1 and a memory 2;
[0128] The processor 1 and the memory 2 communicate with each other via a communication bus 3;
[0129] Processor 1, configured to execute at least one instruction;
[0130] Memory 2, used to store at least one instruction;
[0131] The processor 1 may be a central processing unit (CPU), or an application-specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the present invention.
[0132] The memory 2 may include a high-speed RAM memory, and may also include a non-volatile memory (non-volatile memory), such as at least one disk memory.
[0133] The processor executes at least one instruction to implement the following functions:
[0134] Encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each encrypted string literal and the corresponding encryption information in a buffer, wherein the byte length of each encrypted string literal is not less than the byte length of the corresponding string literal;
[0135] Replacing each of the string literals in the original intermediate representation with the corresponding encrypted string literals to obtain a target intermediate representation;
[0136] When all the encrypted string literals in the target intermediate representation are about to be accessed, a decryption function corresponding to the preset encryption algorithm is called, and the decryption function decrypts each of the encrypted string literals based on the encryption information to obtain the corresponding string literal, until all the encrypted string literals are completely decrypted.
[0137] In summary, in the electronic device disclosed in the present invention, each string literal is encrypted in the intermediate representation layer of LLVM, and all encrypted string literals are decrypted when they are about to be accessed, so that the original string literals cannot be searched in the file where the encrypted string literals are placed, but when the program is running, each encrypted string literal has been decrypted, thereby greatly improving the security of string literal storage, and the entire process does not require human participation, saving time and effort.
[0138] Corresponding to the above embodiment, the present invention further discloses a computer-readable storage medium, which stores at least one instruction, and when the at least one instruction is executed by a processor, the following functions are realized:
[0139] Encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each encrypted string literal and the corresponding encryption information in a buffer, wherein the byte length of each encrypted string literal is not less than the byte length of the corresponding string literal;
[0140] Replacing each of the string literals in the original intermediate representation with the corresponding encrypted string literals to obtain a target intermediate representation;
[0141] When all the encrypted string literals in the target intermediate representation are about to be accessed, a decryption function corresponding to the preset encryption algorithm is called, and the decryption function decrypts each of the encrypted string literals based on the encryption information to obtain the corresponding string literal, until all the encrypted string literals are completely decrypted.
[0142] In summary, in the computer-readable storage medium disclosed in the present invention, each string literal is encrypted in the intermediate representation layer of LLVM, and all encrypted string literals are decrypted when they are about to be accessed, so that the original string literal cannot be searched in the file where the encrypted string literal is placed, but when the program is running, each encrypted string literal has been decrypted, thereby greatly improving the security of string literal storage, and the entire process does not require human participation, saving time and effort.
[0143] It should be noted that the specific working principles of the electronic device and the computer-readable storage medium can be found in the corresponding part of the method embodiment and will not be repeated here.
[0144] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0145] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0146] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for processing string literals, characterized in that: include: Encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each encrypted string literal and corresponding encryption information in a buffer, wherein different string literals use different preset encryption algorithms, and the byte length of each encrypted string literal is not less than the byte length of the corresponding string literal; Replacing each of the string literals in the original intermediate representation with the corresponding encrypted string literals to obtain a target intermediate representation; Before the main function is called or before the dynamic library is loaded into the program, the decryption function corresponding to the preset encryption algorithm is called, and the decryption function decrypts each of the encrypted string literals based on the encryption information to obtain the corresponding string literal until all the encrypted string literals are completely decrypted.
2. The processing method according to claim 1, characterized in that: The step of encrypting each of the string literals using a preset encryption algorithm to generate an encrypted string literal, and recording each of the encrypted string literals and corresponding encryption information in a buffer area, specifically includes: Calling the LLVM optimization module to optimize the original intermediate representation to obtain an optimized intermediate representation; Encrypting each of the string literals in the optimized intermediate representation using the preset encryption algorithm to generate the encrypted string literal; Record each of the encrypted string literals and the corresponding encryption information.
3. The processing method according to claim 1, characterized in that: The preset encryption algorithm is any one of the AES encryption algorithm, the SM3 algorithm and the SM4 algorithm.
4. The processing method according to claim 1, characterized in that: When the preset encryption algorithm is the AES encryption algorithm, the encryption information includes: a key and an initial vector corresponding to the string literal.
5. A device for processing string literals, characterized in that: include: An encryption unit, used to encrypt each string literal in the original intermediate representation based on LLVM using a preset encryption algorithm to generate an encrypted string literal, and record each of the encrypted string literals and corresponding encryption information in a buffer, wherein different string literals use different preset encryption algorithms, and the byte length of each of the encrypted string literals is not less than the byte length of the corresponding string literal; A replacement unit, used for replacing each of the string literals in the original intermediate representation with the corresponding encrypted string literals to obtain a target intermediate representation; The decryption unit is used to call the decryption function corresponding to the preset encryption algorithm before the main function is called or before the dynamic library is loaded into the program, and the decryption function decrypts each of the encrypted string literals based on the encryption information to obtain the corresponding string literal until all the encrypted string literals are completely decrypted.
6. The processing device according to claim 5, characterized in that The encryption unit specifically includes: An optimization subunit, used for calling an LLVM optimization module to optimize the original intermediate representation to obtain an optimized intermediate representation; An encryption subunit, configured to encrypt each of the string literals in the optimized intermediate representation using the preset encryption algorithm to generate the encrypted string literal; The recording subunit is used to record each of the encrypted string literals and the corresponding encryption information.
7. An electronic device, characterized in that: The electronic device comprises a memory and a processor; The memory is used to store at least one instruction; The processor is used to execute the at least one instruction to implement the string literal processing method according to any one of claims 1 to 4.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores at least one instruction, and when the at least one instruction is executed by a processor, the method for processing string literals according to any one of claims 1 to 4 is implemented.
Citation Information
Patent Citations
Software libraries for heterogeneous parallel processing platforms
CN104011679A