A protection method for MCU application program based on HMAC algorithm
By generating signature information using the HMAC algorithm and performing comparison verification, the protection problem of MCU applications is solved, achieving a combination of security and flexibility, and preventing unauthorized copying.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-27
- Publication Date
- 2026-03-20
AI Technical Summary
Existing methods for protecting microcontroller (MCU) applications suffer from difficulties in chip decryption and insufficient flexibility in software protection.
The HMAC algorithm is used to generate an input key using the MCU's unique device ID, chip model, and custom data. The signature information is generated through two iterations and stored in the last page of the MCU's flash memory. The application performs a comparison and verification during execution.
It implements security protection for MCU applications, preventing unauthorized copying, and allows users to freely update applications.
Abstract
Description
TECHNICAL FIELD
[0001] The application relates to the field of application program protection, in particular to a protection method for a single-chip microcomputer (MCU) application program based on an HMAC algorithm. BACKGROUND
[0002] Currently, there are two methods for protecting the application program in the MCU, namely software and hardware.
[0003] Hardware: the program in the chip cannot be read out by using the encryption bits of the internal hardware of the chip, and the chip itself is dependent on the encryption technology. However, with the update of technology, the decryption of the chip becomes easier, and the protection of the application program becomes more difficult.
[0004] Software: the protection method is flexible and does not depend on the encryption technology of the chip itself. The encryption level is determined by the selection of the algorithm, the length of the key and the random number and the iteration number of the algorithm. SUMMARY
[0005] The application aims at solving the above technical problems and provides a protection method for the MCU application program based on the HMAC algorithm.
[0006] To solve the above technical problems, the application adopts the technical scheme that the protection method for the MCU application program based on the HMAC algorithm comprises the following steps.
[0007] Step 1: using the device unique ID in the information block of the flash memory of the MCU, the MCU chip model and the self-defined data to form a 20-byte data string as the input key K for calculation, using the software to generate a 64-byte random number as the input message text, and using the hash algorithm to generate the corresponding number of byte messages as the signature information after twice iterations, and then storing the signature information in the last page of the flash of the MCU;
[0008] Step 2: the user burns the application program into the flash of the MCU through a tool, reads the device unique ID in the information block of the flash memory of the MCU, and then forms a 20-byte data string as the input key K for calculation together with the MCU chip model and the self-defined data stored in the application program, uses the 64-byte random number stored in the application program as the input message text, and calculates the data obtained after twice iterations of the hash algorithm, and then compares the calculated data with the signature information stored in the last page of the flash of the MCU, if the comparison is correct, the program is executed, and if the comparison is incorrect, the execution of the application program is stopped.
[0009] The application has the advantages and positive effects that the protection method based on the HMAC algorithm protects the safety of the application program in the chip, the user can update the application program at will, and the application program cannot run when being copied to the MCU without the signature information, thereby protecting the user program from being copied without permission. DETAILED DESCRIPTION
[0010] The specific embodiments of the application are described in detail below.
[0011] The protection method based on the HMAC algorithm for the MCU application program includes the following steps.
[0012] Step one, using the device unique ID in the information block of the flash memory of the MCU, the MCU chip model and the custom data to form a 20-byte data string as the input key K for calculation, using the software to generate a 64-byte random number as the input message text, generating the corresponding number of byte messages as the signature information through the hash algorithm for twice iteration, and storing the signature information in the last page of the flash of the MCU.
[0013] Step two, the user burns the application program into the flash of the MCU through the tool, when the application program is executed, the device unique ID in the information block of the flash memory of the MCU is read first, and then a 20-byte data string composed of the MCU chip model and the custom data stored in the application program is used as the input key K for calculation, a 64-byte random number stored in the application program is used as the input message text, the data calculated through the hash algorithm for twice iteration is compared with the signature information stored in the last page of the flash of the MCU, if the comparison is correct, the program is executed, and if the comparison is incorrect, the execution of the application program is stopped.
[0014] The best implementation of the protection method based on the HMAC algorithm for the MCU application program has the following function implementation process.
[0015] 1. The writing process of the signature information is realized through the running of the application program A, the application program A is burned into the flash of the MCU by using the burning tool of the MCU chip, the MCU containing the program is run, and the signature information is written.
[0016] 2. The application program B written by the user actually contains the functions to be realized, needs to contain the signature information comparison program, and needs to contain the MCU chip model D1, the custom data D2 and the 64-byte random number S1.
[0017] 3. The application program B is burned into the flash of the MCU by using the burning tool of the MCU or the OTA mode.
[0018] Algorithm implementation steps:
[0019] 1. Application A:
[0020] ① Read the device unique ID: D0 in the information block of the MCU's flash memory;
[0021] MCU chip model: D1;
[0022] Custom data: D2;
[0023] Construct a 20-byte data string as an input key K for calculation:
[0024] K = D0 + D1 + D2 + … + … (make up 0 if necessary)
[0025] ② Generate a 64-byte random number: M using specific software
[0026] Calculate the input text: text = M;
[0027] ③ Use the HMAC iteration algorithm, HMAC = H (key xor opad, H (key xor ipad, text))
[0028] H (X, Y) represents a Hash operation on X + Y, such as SM3, SHA1, SHA256, MD5;
[0029] ipad represents a single-byte hexadecimal constant 0x36 repeated 64 times;
[0030] opad represents a single-byte hexadecimal constant 0x5c repeated 64 times;
[0031] key represents a 64-byte string composed of a key, and 0 is added if necessary;
[0032] text represents an arbitrary length text.
[0033] ④ Write the generated result as signature information into the last page of the MCU's flash memory.
[0034] ⑤ Erase all of the MCU chip except the last page of the flash memory.
[0035] 2. Verification part in application B:
[0036] ① Read the device unique ID: D0 in the information block of the MCU's flash memory;
[0037] MCU chip model fixed in the program: D1;
[0038] Custom data fixed in the program: D2;
[0039] Construct a 20 byte data string as an input key K for calculation:
[0040] K = D0 + D1 + D2 + … + … (not enough to fill 0)
[0041] 2. Read the 64 byte random number M fixed in the program;
[0042] Calculate the input text text = M;
[0043] 3. Use the HMAC iteration algorithm, HMAC = H (key xor opad, H (key xor ipad, text)) H (X, Y) represents a Hash operation on X + Y message,
[0044] Such as SM3, SHA1, SHA256, MD5;
[0045] ipad represents a single byte hexadecimal constant 0x36 repeated 64 times;
[0046] opad represents a single byte hexadecimal constant 0x5c repeated 64 times;
[0047] key represents a 64 byte string composed of a key, and the rest is filled with 0;
[0048] text represents any length of text.
[0049] 4. Compare the result generated by the second iteration calculation with the signature information stored in the last page of the flash of the single-chip microcomputer MCU.
[0050] A protection method for the application program of a single-chip microcomputer MCU based on the HMAC algorithm protects the security of the application program in the chip, and the user can update the application program at will, but when the application program is copied to an MCU that does not contain signature information, it cannot run. Thus, the user program is protected from unauthorized copying.
[0051] The embodiments of the application are described in detail above, but the content described is only the preferred embodiments of the application, and cannot be considered as limiting the scope of the implementation of the application. Any equivalent changes and improvements made within the scope of the application should still be included in the scope of the patent.
Claims
1. A method for protecting a microcontroller (MCU) application program based on the HMAC algorithm, characterized in that: include: Step 1: Use the device unique ID, MCU chip model and custom data in the information block of the MCU's flash memory to form a 20-byte data string as the input key K for calculation. Use software to generate a 64-byte random number as the input message text. After a second iteration through a hash algorithm, generate the corresponding number of bytes as the signature information and store it in the last page of the MCU's flash memory. Step two: The user uses a tool to burn their application into the MCU's flash memory. When the application is executed, the device's unique ID is read from the information block of the MCU's flash memory. This ID, along with the MCU chip model and custom data stored in the application, forms a 20-byte data string as the input key K for calculation. A 64-byte random number stored in the application is used as the input message text. The data calculated through a hash algorithm after two iterations is compared with the signature information stored in the last page of the MCU's flash memory. If the comparison is correct, the program starts executing; otherwise, the application stops executing.
Citation Information
Patent Citations
MCU program encryption method, intelligent terminal and storage medium
CN111062007A
KR20190074042A