A firmware encryption method, a storage medium, an electronic device and a mobile power supply
By dividing the microcontroller's flash memory into logical addresses, integrating key generation and data verification programs, generating basic firmware, and performing encryption verification upon power-on, the problems of high cost and resource consumption of external encryption chips are solved, achieving low-cost and reliable firmware protection.
Patent Information
- Application Number
- CN202611259029.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-18
- Publication Date
- 2026-11-20
AI Technical Summary
Existing technologies use external encryption chips, which are costly, difficult to wire, and consume microcontroller peripheral resources, posing risks of chip production stoppages and supply shortages.
By logically dividing the microcontroller's flash memory into three storage areas, a boot program and application program integrating key generation and data verification programs are generated to create basic firmware. Upon power-up, encryption verification codes are calculated and compared to achieve one-key-one-protection encryption.
No additional hardware chips are required, saving material costs, simplifying layout and wiring, adapting to situations with limited peripheral resources, achieving firmware anti-copy and anti-cloning protection, and adapting to low-cost microcontrollers.
Smart Images

Figure CN123139301A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of information security technology, and in particular to a firmware encryption method, storage medium, electronic device, and power bank. Background Technology
[0002] Microcontrollers are widely used in various electronic devices. The firmware stored in the microcontroller's internal flash memory is the core carrier for the device to implement hardware control and functional logic. Currently, the mainstream firmware security protection solution in the industry is an external independent encryption chip. The microcontroller interacts with the encryption chip through communication buses such as integrated circuit buses, universal asynchronous transceivers, and serial peripheral interfaces to complete the legitimacy verification, thereby restricting the illegal operation of the firmware. However, adding an encryption chip will increase the cost of hardware materials, increase the layout area of printed circuit boards, and increase the difficulty of wiring design. The communication of the encryption chip will occupy the microcontroller's peripheral resources, and the compatibility with low-cost main control chips with limited peripheral resource margins is poor. At the same time, the product supply chain is constrained by encryption chip manufacturers, and there is a potential risk of chip production stoppage and supply shortage.
[0003] Therefore, it is crucial for those skilled in the art to design a firmware encryption method, storage medium, electronic device, and power bank that does not require additional production steps and is low-cost. Summary of the Invention
[0004] This invention provides a firmware encryption method, storage medium, electronic device, and power bank that do not require additional production processes and are low-cost, thereby solving the problems of high cost, difficult wiring, and occupation of microcontroller peripheral resources of existing external encryption chips.
[0005] This invention discloses a firmware encryption method, which includes the following steps:
[0006] The microcontroller's flash memory is logically divided into three memory areas: the first memory area, the second memory area, and the third memory area. The bootloader, which has a pre-set key generation program and a data verification program, is filled into the address segment corresponding to the first storage area, and the application program is filled into the address segment corresponding to the third storage area. The two programs are then merged to generate the basic firmware. Initial verification data is entered into the address range corresponding to the second storage area, and the initial verification data is then embedded into the boot program. When the microcontroller is powered on, a key generation program is run based on the microcontroller serial number to calculate the encryption verification code, and a data verification program is run based on the boot program to read the data in the second storage area; The read data is compared with the initial verification data. If the read data matches the initial verification data, the encrypted verification code is overwritten with the initial verification data in the second storage area before the application is started. If the read data is inconsistent with the initial verification data, the read data will be compared with the encrypted verification code. If the read data is consistent with the encrypted verification code, the verification will be successful and the application will start running. If the read data is inconsistent with the encrypted verification code, the application will be blocked from starting.
[0007] The beneficial effects of the encryption estimation method provided in this invention are as follows: By dividing the microcontroller's flash memory into three logical storage areas, and integrating the boot program with the integrated key generation program and data verification program with the application program to generate basic firmware, the initial verification data is filled in and solidified to obtain complete firmware, and the burning is completed. No additional supporting hardware chips are required, which effectively saves material costs, simplifies PCB layout and wiring, and does not occupy communication peripheral resources, making it suitable for low-cost microcontrollers with limited peripheral resources. Furthermore, the microcontroller completes the encryption verification code calculation and the reading of data from the second storage area simultaneously upon power-up. By comparing two levels, it distinguishes between the initial power-up initialization and the regular power-up verification scenario. During the initial power-up, a unique encryption verification code is generated to overwrite the initial verification data to achieve one key per machine. During regular power-up, the stored data is compared with the real-time calculated encryption verification code. If the verification does not match, the application startup is blocked. The entire logic is embedded in the boot program and automatically executed upon power-up. Under the condition of no hardware increment and no additional production line process, firmware anti-copy and anti-cloning protection can be reliably achieved. Attached Figure Description
[0008] The technical solution of the present invention will be further described in detail below with reference to the accompanying drawings and embodiments. In the accompanying drawings: Figure 1 This is the flowchart of the firmware encryption method in the embodiments of the present invention. Figure 1 ; Figure 2 This is the flowchart of the firmware encryption method in the embodiments of the present invention. Figure 2 ; Figure 3 This is the flowchart of the firmware encryption method in the embodiments of the present invention. Figure 3 ; Figure 4 This is the flowchart of the firmware encryption method in the embodiments of the present invention. Figure 4 . Detailed Implementation
[0009] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. The preferred embodiments of the present invention will now be described in detail with reference to the accompanying drawings.
[0010] like Figures 1 to 4 As shown, the present invention provides a specific embodiment of a firmware encryption method.
[0011] A firmware encryption method, reference Figure 1 This includes the following steps: S1. Divide the microcontroller's flash memory into logical addresses to form a first memory area, a second memory area, and a third memory area; S2. Fill the boot program with the preset key generation program and data verification program into the address segment corresponding to the first storage area, fill the application program into the address segment corresponding to the third storage area, and merge to generate the basic firmware. S3. Fill the initial verification data into the address segment corresponding to the second storage area, and save the initial verification data into the boot program; S4. When the microcontroller is powered on, a key generation program is run based on the microcontroller serial number to calculate the encryption verification code, and a data verification program is run based on the boot program to read the data in the second storage area. S5. Compare the read data with the initial verification data. If the read data is consistent with the initial verification data, overwrite the initial verification data in the second storage area with the encrypted verification code and then start the application. S6. If the read data is inconsistent with the initial verification data, the read data is compared with the encrypted verification code. If the read data is consistent with the encrypted verification code, the verification is successful and the application is started. If the read data is inconsistent with the encrypted verification code, the application is blocked from starting.
[0012] Step 1: Using the microcontroller's supporting linker script file, the internal Flash memory of the microcontroller is logically divided into three independent storage areas without modifying the physical hardware structure of the flash memory: the first storage area, the second storage area, and the third storage area. The first storage area is allocated a dedicated address segment to store the boot program that executes first upon power-on. The second storage area is allocated an independent address segment specifically for storing various verification data, including initial verification data and encrypted verification codes. The third storage area is allocated all remaining flash memory address segments to store the application program that implements the device's business functions. The first, second, and third storage areas each have their own fixed read / write access permissions and address boundaries to ensure that cross-area data read / write conflicts do not occur during program execution.
[0013] In this design, when partitioning the microcontroller's flash memory logically, the capacity of the three storage areas is fixed. The first storage area is configured with 12KB of storage space, specifically for storing the bootloader; the second storage area is configured with 1KB of independent storage space, used only for storing initial verification data and encryption verification codes; the third storage area occupies all remaining space in the microcontroller's flash memory excluding the first two partitions, and is used to store the application programs corresponding to the device's business functions. By using fixed partition capacity, the maximum space occupied by the bootloader and verification data can be precisely limited, preventing the bootloader code and verification data from overflowing and occupying the application storage area. This ensures complete isolation between the bootloader, verification data, and business application programs, while also being compatible with the flash memory capacity specifications of common general-purpose microcontrollers such as the GD32F330CBT6. The partition parameters are standardized, facilitating unified project development, firmware compilation, and mass production burning, ensuring the stable operation of the entire firmware encryption logic and preventing cross-partition read / write conflicts.
[0014] This step achieves layered storage of programs and data through partitioning and isolation. The bootloader is stored separately in the first storage area to ensure that the security verification logic is started first after the device is powered on, and the firmware legitimacy verification is completed before the application runs. The verification data is separately allocated to the second storage area to avoid accidental modification or overwriting of the encryption verification mark by the business application, ensuring the stable operation of the entire encryption logic. The three-zone storage architecture provides a unified hardware storage carrier for the subsequent firmware merging, initial verification data writing, and power-on partition reading and comparison of this solution. It is the underlying storage foundation for the implementation of the entire hardware-free encryption chip protection solution.
[0015] Step 2: In advance, complete the integrated development of key encoding operation logic and data verification judgment logic within the bootloader source code, compile and generate a bootloader binary file, and accurately fill the binary file into the address segment corresponding to the pre-divided first storage area; at the same time, compile and generate an application binary file that implements the device functions, and fill it into the dedicated address segment corresponding to the third storage area; then, use an automated compilation script to read the two program images, merge the two program files according to the partition address rules, and generate a basic firmware image file that only contains the bootloader and application and has not written verification data.
[0016] Specifically, the length of the initial verification data written to the second storage area is less than the total storage space size of the second storage area. During the process of generating the initial verification data and writing it to the second storage area, the total number of bytes of the randomly generated initial verification data is controlled to be less than 1K Bytes of the total capacity of the second storage area. Some free storage space is reserved in the second storage area, which can not only completely store the initial verification data and avoid data overflow causing storage address disorder and data loss, but also reserve the spare space to make it convenient to add verification auxiliary identifiers, encrypted cache data and other extended content in the future. This ensures the stable storage of the initial verification data, improves the reliability of the verification and recognition process, and prevents the recognition logic from failing on the first power-on due to the data length exceeding the limit.
[0017] This step embeds the core encryption and verification algorithms within the bootloader. The entire encryption operation relies solely on the microcontroller's internal computing resources, eliminating the need for external communication peripherals such as I2C, SPI, and UART to interact with external encryption chips. This avoids consuming the microcontroller's limited peripheral resources, making it suitable for low-cost microcontrollers with limited peripheral resources. The bootloader and application program are merged into a single basic firmware, requiring only one firmware file to be processed during mass production. This simplifies the firmware compilation and production burning process by eliminating the need to separately burn the bootloader and application program. The encryption logic is pre-integrated and embedded, allowing the device to directly call the algorithm for verification upon power-on without dynamically loading external computing programs, significantly reducing the power-on security verification time.
[0018] Step 3: Generate a fixed-length random number as initial verification data. Write this set of initial verification data into the independent address segment corresponding to the second storage area. At the same time, embed the identical initial verification data encoding into the boot program code segment for permanent storage. Merge and integrate the storage segment image after writing the verification data with the basic firmware generated in Step 2 to obtain a complete firmware image containing the partition program and factory verification mark. Finally, use a mass production burning tool to burn the complete firmware into the entire flash memory space of the microcontroller in one go.
[0019] This step uses the initial verification data written to the second storage area as a device factory status identifier, which can distinguish whether the microcontroller has completed the first power-on encryption initialization, i.e. whether the microcontroller has been activated, and provides a basis for judgment for subsequent power-on branch logic. The complete firmware is burned to the microcontroller at once, without the need for additional independent processes such as key burning, chip pairing, and data writing on the production line, which simplifies the mass production process. At the same time, it saves the cost of purchasing external encryption chips and soldering materials, and avoids the supply chain risks caused by encryption chip discontinuation and supply shortage.
[0020] Step 4: After the microcontroller is powered on and reset, it automatically jumps to the starting address of the first storage area to execute the boot program. The boot program performs two operations in parallel: calling the built-in key generation program to read the microcontroller's unique hardware serial number and calculate the encryption verification code; and calling the data verification program to read the data stored in the address segment of the second storage area through the flash memory read instruction to obtain the data to be compared, thus preparing all the input data for the subsequent two-level comparison.
[0021] This step simultaneously completes the real-time calculation of the encryption verification code and the reading of data from the second storage area after power-on, acquiring two sets of comparison materials at once to provide a data foundation for subsequent branch judgments. Verification-related calculations and flash memory reading operations are all executed by the boot program first, requiring no manual intervention throughout the process. The encryption protection logic runs silently in the background without interfering with the basic device startup process, establishing a firmware security protection barrier from the source of device startup and intercepting illegal tampering and copying of firmware in advance.
[0022] Step 5: Compare the data in the second storage area read in Step 4 with the initial verification data in the boot program's internal hard-coded backup. If they match, the device is determined to be in the initial power-on state. Call the flash memory erase / write command and use the encrypted verification code calculated in Step 4 to completely overwrite the original initial verification data stored in the second storage area. After the overwrite operation is completed, the boot program jumps to the starting address of the third storage area and starts running the application.
[0023] This step generates a unique encryption verification code based on the unique hardware serial number of each microcontroller, achieving a one-machine-one-key encryption effect. Different microcontrollers cannot generate the same verification code. By overwriting the initial verification data in the second storage area, the device is marked as having completed encryption initialization, allowing the device to enter the normal verification branch upon the next power-on. After completing encryption initialization, the application is automatically allowed to run, achieving firmware encryption protection without affecting the normal business functions of the device, thus balancing security and user experience.
[0024] Step 6: If the data read in Step 5 is inconsistent with the initial verification data, the device is determined to enter the normal power-on verification branch. The data read from the second storage area is compared with the encrypted verification code calculated in real time in Step 4. When the two sets of data match, the firmware is determined to be valid and the verification is successful. The boot program jumps to the third storage area to start the application. When the two sets of data do not match, the application startup is blocked and a firmware security error message is output.
[0025] In scenarios other than the first power-on, this step relies on the encrypted verification code calculated in real time using the device's serial number and the verification code already stored in the second storage area to complete the legitimacy verification. If the firmware is copied and ported to other microcontrollers, the encrypted verification code calculated in real time cannot match the stored data because the target chip serial number is different, thus reliably blocking the operation of the business program and achieving anti-copying and anti-firmware cloning. The entire comparison and blocking logic is embedded in the boot program and does not require external hardware intervention, continuously ensuring the legitimacy of firmware power-on operation.
[0026] When the data read during verification is inconsistent with the encrypted verification code, and it is necessary to block the application from starting, the bootloader can use at least one of the following methods to achieve the blocking: Method 1: The bootloader directly cancels the jump instruction to the application entry point in the third storage area, without triggering the application startup jump; Method 2: The bootloader enters a preset infinite loop, continuously executes an empty loop, and no longer executes subsequent program jump logic. Within the infinite loop, it can simultaneously drive the indicator light via GPIO and print firmware security error messages via the serial port; Method 3: The bootloader stops feeding the watchdog and waits for the hardware watchdog to overflow and trigger a system reset, causing the device to repeatedly restart and unable to stably enter the application; Method 4: The bootloader sets an invalid application flag and writes it to the second storage area. After the flag takes effect, the bootloader directly refuses to start the application on each subsequent power-on; Method 5: The bootloader disables the peripheral clock and blocks global interrupts, causing the microcontroller to enter a low-power shutdown mode, and the device becomes unresponsive. The combination of not executing the application jump and entering an infinite loop, and synchronously outputting firmware security error messages, can be used first. This method is concise in code implementation, has low resource overhead, and is easy to implement in products; other blocking methods can also be used individually or in combination depending on the product's security level.
[0027] In this embodiment, by dividing the microcontroller's flash memory into three independent logical storage zones, the boot program integrating the key generation program and data verification program is merged with the application program to obtain the basic firmware. Initial verification data is written to the second storage zone and simultaneously solidified until the boot program generates complete firmware, which is then burned into the microcontroller in one go. No external encryption chip is required throughout the process, which saves on encryption chip material costs, simplifies PCB layout and wiring design, and does not occupy communication peripheral resources such as I2C, UART, and SPI. It can be adapted to various low-cost microcontrollers with limited peripheral resources. At the same time, there is no need to add extra processes such as key burning and chip pairing during mass production, simplifying the overall production process.
[0028] Upon initial power-up of the microcontroller, the bootloader automatically identifies the initial factory verification data in the second storage area, reads the unique hardware serial number, and generates a unique encrypted verification code through built-in encoding logic to overwrite the original identification data, achieving a differentiated encryption effect of one code per device. Each subsequent power-up will recalculate the encrypted verification code based on the device's serial number and compare it with the stored data. If the firmware is copied to another microcontroller, the verification will fail due to serial number mismatch, and the application will not be able to start. The entire encryption verification logic is embedded in the bootloader and executed automatically during the power-up phase. Without increasing hardware costs or adding production processes, a long-term protection mechanism is built from the device startup source, effectively preventing unauthorized firmware reading, copying, and device cloning and counterfeiting.
[0029] like Figures 2 to 4 This invention provides three schemes for calculating and generating encryption verification codes.
[0030] Option 1, for reference Figure 2 The process of "running a key generation program based on the microcontroller serial number and calculating the encryption verification code" includes the following steps: S51. Using the microcontroller serial number as the input data, perform a hash algorithm on the input data, and use the hash algorithm output to form an encrypted verification code.
[0031] Step 51: After the microcontroller identifies the initial verification data in the second storage area upon its first power-on, the bootloader retrieves the unique hardware serial number that is factory-fixed and cannot be modified. It directly uses the original byte stream of this complete serial number as the sole input data source for the hash operation, without adding any additional operation parameters or introducing other values for preprocessing. Then, it calls the built-in hash algorithm operation logic to perform hash compression operation on the complete serial number byte by byte. After the hash operation is completed, it outputs a fixed-length hash digest result, which is directly used as the local exclusive encrypted verification code to overwrite the initial verification data stored in the second storage area.
[0032] This solution boasts a concise and efficient operational flow, eliminating the serial number preprocessing step, reducing the microcontroller's internal computing power consumption, and lowering the runtime of the power-on encryption initialization phase. It generates the verification code solely based on the chip's native unique serial number, ensuring the uniqueness of the input source and naturally guaranteeing that each microcontroller outputs a distinct hash digest, achieving a basic encryption effect of one code per device. The operational logic is simple, the code occupies minimal storage space, and it is compatible with a 12KB first-storage boot program without code overflow issues. The absence of intermediate computation steps reduces program logic vulnerabilities, improves the stability of the encryption verification code generation process, and avoids the computational anomaly risks associated with multi-step operations.
[0033] Option 2, for reference Figure 3The process of "running a key generation program based on the microcontroller serial number and calculating the encryption verification code" includes the following steps: S52. Perform an AND-OR operation on the microcontroller serial number and the hexadecimal number, use the result of the AND-OR operation as new input data to perform a hash algorithm calculation, and use the output of the hash algorithm to form an encrypted verification code.
[0034] Scheme 52: When the microcontroller is powered on for the first time and recognizes the initial verification data in the second storage area, the bootloader reads the unique, unchangeable hardware serial number of the microcontroller, selects a set of fixed hexadecimal constants, and performs bitwise AND-OR operations on each byte of the serial number with the fixed hexadecimal number. The original serial number byte stream is obfuscated and replaced to obtain a new byte stream after encryption and obfuscation. This obfuscated byte stream is used as the input data for the hash algorithm. Then, the built-in hash algorithm is called to perform a complete hash operation on the preprocessed new input data. The fixed-length hash digest output by the operation is directly used as the microcontroller's exclusive encryption verification code to cover the initial verification data in the second storage area.
[0035] This solution performs an AND-OR obfuscation operation on the original serial number using a fixed hexadecimal number, destroying the original plaintext characteristics of the serial number. Even if someone intercepts the hash input data stream, they cannot directly reverse engineer the microcontroller's true hardware serial number, increasing the difficulty of firmware reverse engineering. It only adds a single simple bit operation, consuming very little computing power to the microcontroller and not significantly extending the device's power-on initialization time. The obfuscation logic code is small and can be contained within the first 12KB storage area, eliminating the code overflow issue. The input data after obfuscation has stronger randomness, and the encrypted checksum output by hash has higher distinguishability, further strengthening the one-device-one-key binding effect and reducing the probability of different devices having the same checksum.
[0036] Option 3, for reference Figure 4 The process of "running a key generation program based on the microcontroller serial number and calculating the encryption verification code" includes the following steps: S531. Perform an AND-OR operation between the microcontroller serial number and multiple different hexadecimal numbers to obtain multiple sets of independent AND-OR operation results; S532. Each set of AND-OR operation results is used as new input data to perform a hash algorithm calculation separately, and the output results of multiple sets of hash algorithms are concatenated to form an encrypted verification code.
[0037] Step 531: After the microcontroller is powered on for the first time and recognizes the initial verification data stored in the second storage area, the bootloader reads the unique and unchangeable hardware serial number of the microcontroller and pre-configures multiple sets of different fixed hexadecimal constants. The complete microcontroller serial number is then sequentially ANDed or ORed with each set of independent hexadecimal constants byte by byte. Each set of hexadecimal constants generates an independent obfuscated byte stream, resulting in multiple independent operation results that are different and have undergone permutation and obfuscation processing. All operation results are cached separately as the raw input material for subsequent hash calculations.
[0038] Specifically, 15 different fixed hexadecimal constants with equal increments from 0x11 to 0xFF are selected. The unique hardware serial number of the microcontroller is then sequentially subjected to bitwise AND-OR operations with each of the 15 hexadecimal constants, generating 15 sets of serial number operation results after obfuscation and permutation. At the same time, the original microcontroller serial number without processing is retained as the first set of original input data, resulting in 16 sets of independent input datasets containing the original serial number and the 15 sets of obfuscated data.
[0039] This step employs multiple sets of different numerical values to obfuscate the serial number, disrupting the data arrangement of the original serial number in multiple layers. Compared to a single set of hexadecimal number obfuscation methods, this significantly increases the difficulty of reverse engineering, as extracting only a single segment of computational data is insufficient to reconstruct the complete serial number. Multiple operations are executed step-by-step, with each step performing only lightweight bit operations, resulting in low computational load on the microcontroller and minimizing the time required for device power-on initialization. Multiple independent results are cached separately, with clear data logic division. This prevents data from overlapping or becoming disordered during program execution, ensuring the integrity and reliability of subsequent hash calculation input data.
[0040] Step 532: For each group of independent AND-OR operation obfuscation results obtained in step S531, a single obfuscated byte stream is used as the input source for the hash algorithm, and a complete round of hash algorithm operation is performed independently. Each round of hash operation outputs a hash digest of a fixed length. After all group hash operations are completed, all group hash digests are concatenated in a preset fixed order. The concatenated complete long string is used as the local exclusive encryption verification code. Finally, the encryption verification code is used to overwrite the original initial verification data inside the second storage area.
[0041] Specifically, for the 16 sets of independent input data generated above, a hash algorithm is called separately to perform a complete hash operation on each set of data. Each operation outputs a fixed 32-byte hash digest, and the 16 hash operations output a total of 512 bytes of hash digest data. According to the preset fixed sorting rule of "the hash result corresponding to the original sequence number first, and the hash results corresponding to the incrementing constants from 0x11 to 0xFF in sequence last", the 16 sets of 32-byte hash digests are sequentially and orderly concatenated to form a 512-byte encrypted verification code. Finally, the 512-byte encrypted verification code is written to the second storage area to replace the original initial verification data.
[0042] Before completing the encryption verification code overwrite operation and jumping to the application execution, the bootloader configures the MCU's built-in Flash control register to enable hardware write protection for the address range of the second storage area storing the verification data. This locks the read and write permissions of the second storage area and prohibits subsequent programs from performing erase, programming, or data modification operations on this storage area. After this hardware write protection mechanism takes effect, only the bootloader during the device's power-on initialization phase can remove the protection and modify the verification data. The application in the running state has no permission to tamper with or erase the encryption verification code stored in the second storage area. This prevents malicious programs and reverse engineering tools from bypassing the firmware verification mechanism by tampering with the verification data. It strengthens the security and uniqueness of the verification data at the hardware storage level and ensures the stable and reliable operation of the verification logic on each subsequent power-on of the device.
[0043] This step involves independently hashing each obfuscation result, concatenating multiple hash digests to form a complete checksum. The encrypted checksum data is longer, has a higher entropy value, and the probability of duplication is almost zero, achieving optimal binding effect for each device with a unique key. The composite encryption logic combining multi-layer obfuscation and multi-segment hashing significantly increases the cost of firmware reverse engineering and brute-force attacks, effectively resisting firmware decompilation, tampering, and copying attacks. The step-by-step operation logic is meticulously broken down, with small code size for each operation segment. The entire key generation program can be completely stored in the 12K Bytes boot program of the first storage area.
[0044] This scheme performs AND-OR obfuscation operations on the microcontroller serial number using multiple sets of different hexadecimal numbers, resulting in multiple sets of independent obfuscated data. These data are then hashed separately, and the hash results are concatenated to form a complete encrypted verification code. The multi-layer obfuscation combined with segmented hashing significantly enhances the obfuscation level of the serial number data, making it significantly more difficult to reverse engineer or brute-force crack the firmware. All operations use only lightweight bitwise and hash operations, resulting in controllable computing power consumption and no significant increase in device power-on startup time. The entire key generation program code occupies a small space, suitable for a 12KB first storage area. The encrypted verification code obtained by concatenating multiple hash segments has stronger randomness and uniqueness, enabling it to distinguish between different hardware microcontrollers. Once the firmware is copied and ported to other chips, it cannot generate a matching verification code due to the different hardware serial numbers, directly blocking the application from running. This effectively prevents board copying and firmware cloning piracy. The entire encryption process relies on the internal hardware resources of the microcontroller, eliminating the need for an additional external encryption chip. This approach combines the advantages of low cost, no occupation of peripheral resources, and no additional production processes.
[0045] Among them, the hash algorithm used in Schemes 1 to 3 is preferably the HMAC-SHA256 algorithm. This algorithm introduces a built-in private key to participate in the hash operation, which can effectively avoid the risk of hash collisions and resist replay and forgery attacks compared with ordinary hash algorithms. The 256-bit fixed-length output digest has extremely high distinguishability, further strengthening the uniqueness of the check code of different microcontrollers. The entire HMAC-SHA256 operation code is small in size and can be compatible with a 12K Bytes boot program storage partition. It has low computing power consumption and will not prolong the device power-on initialization time. Combined with three different serial number preprocessing obfuscation logics, it can form a tiered security protection system to adapt to the needs of products with different security levels. Attackers cannot forge a legitimate encrypted check code without obtaining the built-in private key of the program, which greatly increases the cost of firmware reverse engineering and cloning. The entire algorithm operation relies on the internal resources of the microcontroller and does not require an external encryption chip, maintaining the technical advantages of low cost, no peripheral occupation, and simple mass production process.
[0046] It should be understood that the above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit them. Those skilled in the art can modify the technical solutions described in the above embodiments, or make equivalent substitutions for some of the technical features; and all such modifications and substitutions should fall within the protection scope of the appended claims of the present invention.
Claims
1. A firmware encryption method, characterized in that, Includes the following steps: The microcontroller's flash memory is logically divided into three memory areas: the first memory area, the second memory area, and the third memory area. The bootloader, which has a pre-set key generation program and a data verification program, is filled into the address segment corresponding to the first storage area, and the application program is filled into the address segment corresponding to the third storage area. The two programs are then merged to generate the basic firmware. Initial verification data is entered into the address range corresponding to the second storage area, and the initial verification data is then embedded into the boot program. When the microcontroller is powered on, a key generation program is run based on the microcontroller serial number to calculate the encryption verification code, and a data verification program is run based on the boot program to read the data in the second storage area; The read data is compared with the initial verification data. If the read data matches the initial verification data, the encrypted verification code is overwritten with the initial verification data in the second storage area before the application is started. If the read data is inconsistent with the initial verification data, the read data will be compared with the encrypted verification code. If the read data is consistent with the encrypted verification code, the verification will be successful and the application will start running. If the read data is inconsistent with the encrypted verification code, the application will be blocked from starting.
2. The firmware encryption method according to claim 1, characterized in that, The step of "filling in the initial verification data in the address segment corresponding to the second storage area" includes the following steps: Initial verification data is generated using random numbers and then filled into the address range corresponding to the second storage area.
3. The firmware encryption method according to claim 1, characterized in that, The step of "running a key generation program based on the microcontroller serial number and calculating the encryption verification code" includes the following steps: The microcontroller serial number is used as the input data for the operation. A hash algorithm is performed on the input data, and the output of the hash algorithm is used to form an encrypted verification code.
4. The firmware encryption method according to claim 1, characterized in that, The step of "running a key generation program based on the microcontroller serial number and calculating the encryption verification code" includes the following steps: The microcontroller serial number and the hexadecimal number are subjected to an AND-OR operation. The result of the AND-OR operation is used as new input data to perform a hash algorithm calculation. The output of the hash algorithm is used to form an encrypted verification code.
5. The firmware encryption method according to claim 1, characterized in that, The step of "running a key generation program based on the microcontroller serial number and calculating the encryption verification code" includes the following steps: Perform an AND-OR operation between the microcontroller serial number and multiple different hexadecimal numbers to obtain multiple sets of independent AND-OR operation results; Each set of AND-OR operation results is used as new input data to perform a hash algorithm calculation separately, and the output results of multiple sets of hash algorithms are concatenated to form an encrypted verification code.
6. The firmware encryption method according to any one of claims 3-5, characterized in that, The hash algorithm used is HMAC-SHA256.
7. The firmware encryption method according to claim 1, characterized in that, It also includes the following steps: If the read data does not match the encryption verification code, the bootloader will enter an infinite loop and output a firmware security error message.
8. An electronic device, characterized in that, It includes a memory and a processor, the memory storing a computer program that, when executed by the processor, causes the processor to perform the steps of the method as described in any one of claims 1-7.
9. A computer-readable storage medium, characterized in that, The device stores a computer program that, when executed by a processor, causes the processor to perform the steps of the method as described in any one of claims 1-7.
10. A portable power bank, characterized in that, It has an MCU chip, wherein the MCU chip has a boot program, a data verification program, an application program and initial verification data as described in any one of claims 1-7, and is used to implement the firmware encryption method as described in any one of claims 1-7.
