An MD5 algorithm optimization method and system based on embedded software
By employing a single-threaded optimization of the MD5 algorithm in embedded software and evenly distributing the computation count across multiple cycles, the timeout problem of the MD5 algorithm in embedded software is solved, thereby improving the reliability and portability of the software.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- TIANJIN JINHANG COMP TECH RES INST
- Filing Date
- 2023-10-31
- Publication Date
- 2026-05-26
AI Technical Summary
When the input data is large, the MD5 algorithm implemented in embedded software needs to perform nonlinear calculations repeatedly, which causes the calculation time to exceed the clock cycle and results in software calculation timeout.
A single-threaded optimization method is adopted. By defining the MD5 calculation main function and three interface functions, combined with data structures, the number of calculations is evenly distributed across multiple software cycles to avoid single-cycle timeouts.
It effectively reduces the burden of single-cycle runtime, avoids MD5 algorithm function call timeouts, and improves software reliability and portability.
Smart Images

Figure CN117348926B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of embedded software technology, and in particular relates to an MD5 algorithm optimization method and system based on embedded software. Background Technology
[0002] MD5, or Message-Digest Algorithm 5, is a widely used hash function in computer security to ensure the integrity and consistency of transmitted information, providing message integrity protection. Its core is the hash function. It calculates a fixed-length hash value from variable-length input data, and any slight change in the input data will result in a significant change in the output. This method achieves data integrity verification and message authentication. A known MD5 algorithm function written in C language includes data segmentation and padding, parameter initialization, main loop calculation, and generating the calculation result.
[0003] a) Data segmentation and padding primarily involves segmenting the input data into blocks of 512 bits / 64 bytes. The remaining blocks at the end of the segmentation are padded with 48 bytes, leaving the last 8 bytes as the total input data length, excluding the padded bytes. For example... Figure 1 .
[0004] b) Parameter initialization initializes various parameters used in the main loop calculations, such as link variables. There are four link variables, each 4 bytes long. These variables will be used to perform bitwise AND, OR, NOT, addition, and multiplication calculations during the main loop calculations.
[0005] c) The main loop calculation involves performing four rounds of non-linear calculations on each 512-bit / 64-byte block of data. Each round is performed on a 4-byte (32-bit) unit, for a total of 16 4-byte units, and the calculation is performed 16 times, traversing each byte in the 64-byte block. Although the process is relatively complex, it essentially involves performing bitwise AND, OR, NOT, addition, and multiplication operations on the 32-bit data.
[0006] d) After each data block is computed in the main loop, four intermediate variables, `state`, are generated. These variables are used to modify four link variables and participate in the main loop computation of the next data block. When the last data block is computed, a 128-bit / 16-byte computation result is generated using the last modified link variables. See Appendix for details. Figure 2 .
[0007] Embedded software is typically implemented in C and runs on processor chips based on ARM, DSP, PowerPC, or microcontrollers, requiring high real-time performance. Internally, it generally uses a fixed-period global clock timer (e.g., generating a timer interrupt every 1ms) as the minimum timing resolution, achieving timing with a certain resolution by counting periodic interrupts. When a timer expires, a valid timing flag is generated, triggering the periodic execution of various modules within the software.
[0008] For example, if the timing indicator is generated once every 10ms, the software will call each function block in sequence. All function calls must be completed within 10ms before the timing indicator of the next cycle is generated. Otherwise, due to the timeout of the function calls in this cycle, the functions cannot be called on time in the next cycle, which will cause a series of adverse effects.
[0009] Sometimes, based on design input requirements, software needs to call the MD5 algorithm function in a certain cycle when external input conditions are met. The function internally performs multiple iterations of the algorithm. However, due to the length of the input data and the processor's own clock speed limitations, it's impossible to complete all the iterations within 10ms, and a timeout will inevitably occur after all calculations are completed in the current cycle.
[0010] To solve the above problems, a multi-threaded approach is generally adopted, in which a low-priority thread is responsible for executing complex algorithm functions when a high-priority thread relinquishes the CPU. However, this requires the two threads to implement strict data synchronization and protection mechanisms, and it relies excessively on system call functions, which is not conducive to code portability.
[0011] Based on the above analysis, the problems and defects of the existing technology are as follows: When there is a large amount of input data, the MD5 algorithm implemented by embedded software needs to repeatedly perform nonlinear calculations, which consumes a lot of time. As a result, when the software calculates the MD5 checksum, the calculation time exceeds the clock cycle. Summary of the Invention
[0012] To overcome the problems existing in related technologies, this invention discloses an MD5 algorithm optimization method and system based on embedded software. The purpose of this invention is to propose a software method that can be implemented in a single thread without timeouts when calling the MD5 algorithm function. It fully utilizes the consistent nature of each iteration of the MD5 algorithm, distributing the total number of calculations evenly across several consecutive software cycles, effectively reducing the runtime burden of a single cycle and avoiding single-cycle timeouts.
[0013] The technical solution is as follows: an MD5 algorithm optimization method based on embedded software, applied to a C language byte data processing module, including:
[0014] The defined MD5 calculation main function, together with three interface functions and data structures, constitutes the MD5 calculation module, which provides MD5 periodic calculation services in byte data processing to the upper-level user module and determines whether each clock cycle has timed out.
[0015] The three interface functions include an initialization function, a single-loop calculation function, and a result generation function;
[0016] The data structure includes control variables and state variables. The control variables are used to control the start of the entire MD5 calculation module and the number of calculations per cycle. The state variables are used to indicate the execution stage of the MD5 calculation module and whether the calculation is complete.
[0017] Furthermore, the control variable includes MD5_s, which includes a calculation start flag (start), a step count (step), a completion status (done), the number of calculations per cycle (cacuTimesPerCycle), and the input data length (totalLen).
[0018] Furthermore, the state variables include:
[0019] The `start` variable is used by the parent user module to send a start calculation command to the MD5 calculation module. It is valid if it is set to 1 by the parent user module and cleared to 0 by the MD5 calculation module.
[0020] The step variable is used to indicate the current state of the MD5 calculation module. 0 represents the initial ready state or the completed state, and 1 represents the calculation state.
[0021] The `done` variable is used to indicate the completion status and is fed back to the parent user module.
[0022] The cacuTimesPerCycle variable specifies the maximum number of times the MD5 calculation module executes its main loop during each cycle.
[0023] The variable totalLen represents the total number of bytes of data that need to be processed in the MD5 calculation.
[0024] Furthermore, the initialization function is GetMD5Fingerprint1, which is used to padded the input data with 48 bytes and generate the last 8 bytes of data length.
[0025] Furthermore, the single-loop calculation function is GetMD5Fingerprint2, which performs a main loop calculation on the input data in units of 64 bytes.
[0026] Furthermore, the result generation function is GetMD5Fingerprint3, which is used to convert the link variable generated by the last main loop calculation into a 16-byte calculation result; the MD5 calculation main function calls this function to generate a 16-byte calculation result after determining that the calculation is complete.
[0027] Furthermore, the main MD5 calculation function is CacuMD5, which calls an initialization function, a single-loop calculation function, and a result generation function. When step is 0 and start is 1, GetMD5Fingerprint1 is called once, and step is set to 1. When step is 1, GetMD5Fingerprint2 is called the maximum number of times specified by cacuTimesPerCycle. If the calculation is complete, that is, all input data has been processed in the 64-byte main loop, then done is given as 1, step is 0, and GetMD5Fingerprint3 is called once.
[0028] Furthermore, the method specifically includes the following steps:
[0029] The first step is to define an object md5 using MD5_s, which is used to control the execution of the MD5 calculation module and the feedback of the calculation status;
[0030] The second step is to check whether the timer expiration flag is valid in the while(1) loop statement block in the main loop. If it is invalid, the check continues until it is valid and the code for the periodic loop call is entered, so as to realize the sequential call of each module function according to the period, including the user module function using MD5 and the MD5 calculation module function.
[0031] The third step is to determine whether the module is in its initial state and whether user.step is 0 in the user module function that uses MD5. If it is 0, it means that MD5 has not been executed. If new image data is received at this time, the command to start calculating MD5 is sent to the MD5 calculation module interface structure md5.
[0032] The fourth step, in the main function of the MD5 calculation module, first checks if `md5.start` is 1 and `md5.step` is 0. If so, sets `md5.step` to 1 and calls the MD5 calculation initialization function `GetMD5Fingerprint1`. If not, checks if `md5.step` is 1. If so, checks if the current cycle's main loop calculation count exceeds the maximum number of times per cycle. If it does, it exits the current clock cycle's calculation directly. If it doesn't exceed the maximum, it calls the main loop calculation function `GetMD5Fingerprint2` once. Then, it checks if all input data has been calculated. If completed, it sets `md5.done` to 1 and `md5.step` to 0, and calls the result generation function `GetMD5Fingerprint3`. If not completed, it exits the function, and returns to this point to continue execution in the next clock cycle. Before exiting the function, it clears `md5.start` to 0.
[0033] In the third step, set the total length of the input data md5.totalLen, set the number of MD5 main loop calculations per clock cycle md5.CacuTimesPerCycle, and set the start command to be valid md5.start=1; finally, set user.step to 1, so that if new image input data is received before the calculation is finished, the MD5 calculation will not be executed.
[0034] When the next clock cycle runs, if user.step is 1, it will wait for md5.done to become 1; after it becomes 1, the MD5 calculation is complete, the upper-level user module will clear md5.done to 0 and obtain the MD5 calculation result for subsequent processing; if md5.done is not 1, the judgment will continue to be repeated in the next clock cycle.
[0035] Another objective of this invention is to provide an MD5 algorithm system based on embedded software, which is applied to a C language byte data processing module, comprising:
[0036] The MD5 calculation main function module is used to call three interface functions;
[0037] Three interface function modules are used to pad the input data with 48 bytes and generate the last 8 bytes of data length, perform a main loop calculation on the input data in units of 64 bytes, convert the link variable generated by the last main loop calculation into a 16-byte calculation result, and call this function to generate a 16-byte calculation result after the MD5 calculation main function module determines that the calculation is complete.
[0038] The data structure module is used to control the start of the entire MD5 calculation module and the number of calculations per cycle; as well as to indicate the execution stage of the MD5 calculation module and whether the calculation is complete.
[0039] Combining all the above technical solutions, the advantages and positive effects of this invention are as follows: This invention proposes an optimization method for the MD5 algorithm in embedded software. Utilizing the consistent computational process in each loop of the algorithm, the total time consumed by the algorithm is evenly distributed across several adjacent software cycles, effectively preventing timeouts in individual cycles caused by calling the MD5 algorithm function. Furthermore, this method does not employ system calls related to multithreaded operations, is independent of the operating system platform, and facilitates software code portability and promotion.
[0040] This invention fully leverages the computing power of embedded platforms, rationally allocating the computationally intensive and time-consuming MD5 verification calculation within the embedded software. This eliminates software timeout issues caused by large computational loads, improving software reliability, stability, and portability while correctly implementing the calculation function. Attached Figure Description
[0041] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure;
[0042] Figure 1 The existing MD5 algorithm provided in this embodiment of the invention divides the input data into blocks with 512 bits / 64 bytes as the boundary;
[0043] Figure 2 This is a 128-bit / 16-byte calculation result diagram generated by the existing MD5 algorithm provided in this embodiment of the invention using the last changed link variable;
[0044] Figure 3 This is a diagram illustrating the data structure, interface functions, and MD5 calculation main function operation principle provided in this embodiment of the invention.
[0045] Figure 4 This is a flowchart illustrating the process of the upper-level user module using the MD5 calculation module provided in this embodiment of the invention; and a flowchart showing the order of their calls in the main loop;
[0046] Figure 5 This is a flowchart showing the call order of the upper-level user module function User MD5() and the MD5 calculation module main function Cacu MD5() in the entire main loop, provided in this embodiment of the invention. Detailed Implementation
[0047] To make the above-mentioned objects, features, and advantages of the present invention more apparent and understandable, specific embodiments of the present invention will be described in detail below with reference to the accompanying drawings. Many specific details are set forth in the following description to provide a thorough understanding of the present invention. However, the present invention can be practiced in many other ways different from those described herein, and those skilled in the art can make similar modifications without departing from the spirit of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0048] The innovation of the MD5 algorithm optimization method and system based on embedded software provided in this invention is that: the method allows embedded software to make full use of each clock cycle to execute computational tasks with a long total time consumption, and ensures that each clock cycle does not time out, while the code is easy to port.
[0049] Example 1: This embodiment of the invention provides an MD5 algorithm optimization method based on embedded software, applied to a C language byte data processing module. The method includes:
[0050] In byte data processing, the defined MD5 calculation main function, together with three interface functions and data structures, constitutes the MD5 calculation module, which provides MD5 periodic calculation services in byte data processing to the upper-level user module and determines whether each clock cycle has timed out.
[0051] The MD5 calculation main function calls three interface functions depending on the situation;
[0052] The three interface functions include an initialization function, a single-loop calculation function, and a result generation function;
[0053] The data structure includes control variables to control the start of the entire MD5 calculation module and the number of calculations per cycle; and status variables to indicate the execution stage of the MD5 calculation module and whether the calculation is complete.
[0054] Example 2, as another embodiment of the present invention, the initial MD5 algorithm function internal call process is divided into three parts:
[0055] Step 1. Organize the raw input data.
[0056] Step 2. Perform calculations on the sorted data in a loop of 64 bytes at a time until all 64 bytes have been calculated.
[0057] Step 3. Present the calculation results.
[0058] Steps 1 and 3 only involve data organization and transfer, and do not consume a significant amount of time. Step 2 uses the same algorithm for each iteration, only the input data differs. A new link variable is generated after each calculation for the next iteration. The total number of iterations is proportional to the length of the input data, with each iteration processing 64 bytes. If the data is too long, calculating all iterations within a single cycle will inevitably result in a timeout.
[0059] Although the total number of loops is large, the time occupied by the calculation function in each loop is very short. In addition, after the software executes all the necessary functions in each cycle, there is usually a lot of idle time. Based on these timing characteristics of the software, by controlling the number of loops in each cycle, the total number of loops is evenly distributed across multiple consecutive cycles. Within each clock cycle, the loop function is called only a limited number of times, ensuring that the total execution time of a single cycle does not exceed the timeout limit, while still successfully obtaining the calculation results.
[0060] Based on the above characteristics, this invention modifies the original MD5 algorithm function, decomposing it into three interface functions: an initialization function, a single-loop calculation function, and a result generation function. A new MD5 calculation main function is defined, responsible for calling the three interface functions according to different situations. For details on the calling timing, please refer to the MD5 calculation function cacu MD5 flowchart, such as... Figure 5 Additionally, a new data structure is defined: it contains control variables to control the start of the entire algorithm execution and the number of calculations per cycle; and it contains state variables to represent the algorithm execution stage and whether the calculation is complete.
[0061] An MD5 calculation module consists of a main MD5 calculation function, three interface functions, and a data structure, which provides MD5 periodic calculation services to the upper-level user module.
[0062] In this embodiment of the invention, the data structure, interface functions, and the main MD5 calculation function are detailed as follows:
[0063] The data structure MD5_s includes the calculation start flag (start), step count (step), completion status (done), number of calculations per cycle (cacuTimesPerCycle), and input data length (totalLen).
[0064] In the data structure, the start variable is used by the parent user module to issue a start calculation command to the MD5 calculation module. It is valid when the parent user module sets it to 1 and the MD5 calculation module sets it to 0.
[0065] In the data structure, the step variable is used to represent the current state of the MD5 calculation module. 0 represents the initial ready state or the completed state, and 1 represents the calculation state.
[0066] In the data structure, the 'done' variable is used to represent the completion status and is fed back to the parent user module.
[0067] In the data structure cacuTimesPerCycle, the maximum number of calculations that the MD5 calculation module can perform in its main loop during each cycle can be adjusted appropriately based on the actual software operation.
[0068] In the data structure, the variable totalLen represents the total number of bytes of data to be processed in the MD5 calculation.
[0069] The initialization function GetMD5Fingerprint1 is used to pad the input data with 48 bytes and generate a data length of 8 bytes at the end.
[0070] The function GetMD5Fingerprint2 performs a single loop calculation on the input data in 64-byte units.
[0071] The function `GetMD5Fingerprint3` generates the result by converting the linked variable generated in the last main loop calculation into a 16-byte result. The main MD5 calculation function calls this function to generate the 16-byte result after determining that the calculation is complete.
[0072] The main MD5 calculation function, CacuMD5, is responsible for calling the three interface functions mentioned above: When step is 0 and start is 1, it calls GetMD5Fingerprint1 once and sets step to 1. When step is 1, it calls GetMD5Fingerprint2 the maximum number of times specified by cacuTimesPerCycle. If the calculation is complete, i.e., all input data has been processed in the main loop (64 bytes), it outputs done as 1, step as 0, calls GetMD5Fingerprint3 once, and ends the current MD5 calculation operation. See the appendix for a detailed flowchart. Figure 3 .
[0073] Example 2, using C language code as an example, details the implementation methods of the user module functions and the main function of the MD5 calculation module in terms of software implementation, such as... Figure 4 The flow of the upper-level user module using the MD5 calculation module is shown; and their calling order in the main loop is as follows: Figure 5 The order in which the parent user module function User MD5() and the MD5 calculation module main function Cacu MD5() are called in the entire main loop is shown. Software code on different development platforms can refer to this method to meet their respective MD5 calculation needs.
[0074] Specifically, MD5 algorithm optimization methods based on embedded software include:
[0075] The first step is to define an object named md5 using MD5_s, which is used to control the execution of the MD5 calculation module and the feedback of the calculation status.
[0076] The second step is to check whether the timer expiration flag is valid in the while(1) loop statement block in the main loop. If it is invalid, the check continues until it is valid and the code for the periodic loop call is entered, so as to realize the sequential call of each module function according to the period, including the user module function using MD5 and the MD5 calculation module function.
[0077] The third step, within the user module function using MD5, first checks if the module is in its initial state, i.e., whether `user.step` is 0. A value of 0 indicates that MD5 calculation has not yet been performed. If new data (such as image data) is received at this time, a command to start MD5 calculation can be sent to the MD5 calculation module interface structure `md5`. Specifically, this involves: setting the total length of the input data `md5.totalLen` (in bytes), setting the number of main MD5 calculation loop iterations per clock cycle `md5.CacuTimesPerCycle`, and setting the start command to be valid `md5.start = 1`. Finally, `user.step` is set to 1, indicating that MD5 calculation has started. Before this calculation is completed, if new input data is received again, MD5 calculation will not be performed.
[0078] When the next clock cycle arrives, the software will run to this point again. If user.step is 1, it will wait for md5.done to become 1. Once it becomes 1, it means the MD5 calculation is complete. The user module will then clear md5.done to 0 and obtain the MD5 calculation result for subsequent processing. If md5.done is not 1, the process will repeat in the next clock cycle.
[0079] The fourth step, within the main function of the MD5 calculation module, first checks if `md5.start` is 1 and `md5.step` is 0. If so, it sets `md5.step` to 1 and calls the MD5 calculation initialization function `GetMD5Fingerprint1`. If not, it checks if `md5.step` is 1. If so, it checks if the current cycle's main loop calculation count exceeds the maximum number of counts per cycle. If it does, it exits the current clock cycle's calculation. If not, it calls the main loop calculation function `GetMD5Fingerprint2` once, then checks if all input data has been calculated. If completed, it sets `md5.done` to 1 and `md5.step` to 0, and calls the result generation function `GetMD5Fingerprint3`. If not completed, it exits the function, and execution returns to this point in the next clock cycle. Before exiting the function, `md5.start` is cleared to 0.
[0080] Through the above implementation method, the software executes a limited number of MD5 main loop calculations in each clock cycle, ensuring that it does not consume excessive time and cause cycle timeouts. Simultaneously, it fully utilizes the time spare in each cycle to improve CPU utilization. When the main clock cycle arrives, the software cyclically calls each module function, including the user module function that uses MD5 and the MD5 calculation module function. These two functions communicate commands and statuses through the MD5 data structure, making the entire MD5 calculation process clear in hierarchy, logically controllable, and with well-defined statuses, facilitating widespread use.
[0081] Example 3: This invention provides an MD5 algorithm system based on embedded software, comprising:
[0082] The MD5 calculation main function module is used to call three interface functions according to different situations;
[0083] Three interface function modules are used to pad the input data with 48 bytes and generate the last 8 bytes of data length, perform a main loop calculation on the input data in units of 64 bytes, convert the link variable generated by the last main loop calculation into a 16-byte calculation result, and call this function to generate a 16-byte calculation result after the MD5 calculation main function module determines that the calculation is complete.
[0084] The data structure module is used to control the start of the entire MD5 calculation module and the number of calculations per cycle; as well as to indicate the execution stage of the MD5 calculation module and whether the calculation is complete.
[0085] In the above embodiments, the descriptions of each embodiment have different focuses. For parts that are not described in detail or recorded in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0086] The information interaction and execution process between the above-mentioned devices / units are based on the same concept as the method embodiments of the present invention. For details on their specific functions and technical effects, please refer to the method embodiments section, and they will not be repeated here.
[0087] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the above-described division of functional units and modules is merely an example. In practical applications, the above functions can be assigned to different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiments can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit. Furthermore, the specific names of the functional units and modules are only for easy differentiation and are not intended to limit the scope of protection of this invention. The specific working process of the units and modules in the above system can be referred to the corresponding process in the foregoing method embodiments.
[0088] This invention also provides a computer device comprising: at least one processor, a memory, and a computer program stored in the memory and executable on the at least one processor, wherein the processor executes the computer program to implement the steps in any of the above method embodiments.
[0089] This invention also provides a computer-readable storage medium storing a computer program that, when executed by a processor, can implement the steps described in the various method embodiments above.
[0090] This invention also provides an information data processing terminal, which, when executed on an electronic device, provides a user input interface to implement the steps described in the above method embodiments. The information data processing terminal is not limited to mobile phones, computers, or switches.
[0091] This invention also provides a server that, when executed on an electronic device, provides a user input interface to implement the steps described in the above method embodiments.
[0092] This invention provides a computer program product that, when run on an electronic device, enables the electronic device to implement the steps described in the various method embodiments above.
[0093] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments of this application can be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when executed by a processor, it can implement the steps of the various method embodiments described above. The computer program includes computer program code, which can be in the form of source code, object code, executable files, or certain intermediate forms. The computer-readable medium can include at least: any entity or device capable of carrying the computer program code to a photographing device / terminal device, a recording medium, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electrical carrier signal, a telecommunication signal, and a software distribution medium. Examples include USB flash drives, portable hard drives, magnetic disks, or optical disks.
[0094] To further illustrate the effects of the embodiments of the present invention, the following experiment was conducted: On a certain embedded platform, when the software needs to perform MD5 checksum calculation on an input image data of size 65536 bytes, the cacuTimesPerCycle is set to 10 within each 25ms clock cycle, meaning the main loop calculation is called a maximum of 10 times. Each calculation calculates 64 bytes, requiring a total of 65536 / 64 = 1024 calculations. With 10 calculations per cycle, a total of 1024 / 10 = 103 cycles are needed, accumulating a total calculation time of 103 * 25ms = 2.575s for one image. Simultaneously, measurements show that the time used for calculating the 10 main loop calculations within each clock cycle does not exceed 5ms, preventing timeouts within a single clock cycle. Furthermore, the number of main loop calls (cacuTimesPerCycle) per cycle can be adaptively adjusted based on the remaining time in the clock cycle; if there is ample remaining time, the number of calculations can be increased, and vice versa.
[0095] The above description is only a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any modifications, equivalent substitutions and improvements made by those skilled in the art within the scope of the technology disclosed in the present invention and within the spirit and principles of the present invention should be covered within the scope of protection of the present invention.
Claims
1. A method for optimizing the MD5 algorithm based on embedded software, characterized in that, This method is applied to the C language byte data processing module, including: The defined MD5 calculation main function, together with three interface functions and data structures, constitutes the MD5 calculation module. It provides MD5 periodic calculation services in byte data processing to the upper-level user module and determines whether each clock cycle has timed out. The three interface functions include an initialization function, a single loop calculation function, and a result generation function. The data structure includes control variables and state variables. The control variables are used to control the start of the entire MD5 calculation module and the number of calculations per cycle. The state variables are used to indicate the execution stage of the MD5 calculation module and whether the calculation is completed. The method specifically includes the following steps: The first step is to define an object md5 using MD5_s, which is used to control the execution of the MD5 calculation module and the feedback of the calculation status; The second step is to check whether the timer expiration flag is valid in the while(1) loop statement block in the main loop. If it is invalid, the check continues until it is valid and the code for the periodic loop call is entered, so as to realize the sequential call of each module function according to the period, including the user module function using MD5 and the MD5 calculation module function. The third step is to determine whether the module is in its initial state and whether user.step is 0 in the user module function that uses MD5. If it is 0, it means that MD5 has not been executed. If new image data is received at this time, the command to start calculating MD5 is sent to the MD5 calculation module interface structure md5. The fourth step, in the main function of the MD5 calculation module, first checks if `md5.start` is 1 and `md5.step` is 0. If so, sets `md5.step` to 1 and calls the MD5 calculation initialization function `GetMD5Fingerprint1`. If not, checks if `md5.step` is 1. If so, checks if the current cycle's main loop calculation count exceeds the maximum number of times per cycle. If it does, it exits the current clock cycle's calculation directly. If it doesn't exceed the maximum, it calls the main loop calculation function `GetMD5Fingerprint2` once. Then, it checks if all input data has been calculated. If completed, it sets `md5.done` to 1 and `md5.step` to 0, and calls the result generation function `GetMD5Fingerprint3`. If not completed, it exits the function, and returns to this point to continue execution in the next clock cycle. Before exiting the function, it clears `md5.start` to 0.
2. The MD5 algorithm optimization method based on embedded software according to claim 1, characterized in that, The control variables include MD5_s, which includes a calculation start flag (start), a step count (step), a completion status (done), the number of calculations per cycle (cacuTimesPerCycle), and the input data length (totalLen).
3. The MD5 algorithm optimization method based on embedded software according to claim 1, characterized in that, The state variables include: The `start` variable is used by the parent user module to send a start calculation command to the MD5 calculation module. It is valid if it is set to 1 by the parent user module and cleared to 0 by the MD5 calculation module. The step variable is used to indicate the current state of the MD5 calculation module. 0 represents the initial ready state or the completed state, and 1 represents the calculation state. The `done` variable is used to indicate the completion status and is fed back to the parent user module. The cacuTimesPerCycle variable specifies the maximum number of times the MD5 calculation module executes its main loop during each cycle. The variable totalLen represents the total number of bytes of data that need to be processed in the MD5 calculation.
4. The MD5 algorithm optimization method based on embedded software according to claim 1, characterized in that, The initialization function is GetMD5Fingerprint1, which is used to padded the input data with 48 bytes and generate a data length of 8 bytes at the end.
5. The MD5 algorithm optimization method based on embedded software according to claim 1, characterized in that, The single-loop calculation function is GetMD5Fingerprint2, which performs a main loop calculation on the input data in units of 64 bytes.
6. The MD5 algorithm optimization method based on embedded software according to claim 1, characterized in that, The function that generates the result is GetMD5Fingerprint3, which is used to convert the link variable generated by the last main loop calculation into a 16-byte calculation result; the MD5 calculation main function calls this function to generate a 16-byte calculation result after determining that the calculation is complete.
7. The MD5 algorithm optimization method based on embedded software according to claim 6, characterized in that, The main MD5 calculation function is CacuMD5, which calls an initialization function, a single-loop calculation function, and a result generation function. When step is 0 and start is 1, GetMD5Fingerprint1 is called once, and step is set to 1. When step is 1, GetMD5Fingerprint2 is called the maximum number of times specified by cacuTimesPerCycle. If the calculation is complete, that is, all input data has been processed in the 64-byte main loop, then done is given as 1, step is 0, and GetMD5Fingerprint3 is called once.
8. The MD5 algorithm optimization method based on embedded software according to claim 1, characterized in that, In the third step, set the total length of the input data md5.totalLen, set the number of MD5 main loop calculations per clock cycle md5.CacuTimesPerCycle, and set the start command to be valid md5.start=1; finally, set user.step to 1, so that if new image input data is received before the calculation is finished, the MD5 calculation will not be executed. When the next clock cycle runs, if user.step is 1, it will wait for md5.done to become 1; after it becomes 1, the MD5 calculation is complete, the upper-level user module will clear md5.done to 0 and obtain the MD5 calculation result for subsequent processing; if md5.done is not 1, the judgment will continue to be repeated in the next clock cycle.
9. An MD5 algorithm system based on embedded software, characterized in that, This system, applied to a C language byte data processing module, includes: The MD5 calculation main function module is used to call three interface functions; Three interface function modules are used to pad the input data with 48 bytes and generate the last 8 bytes of data length, perform a main loop calculation on the input data in units of 64 bytes, convert the link variable generated by the last main loop calculation into a 16-byte calculation result, and call this function to generate a 16-byte calculation result after the MD5 calculation main function module determines that the calculation is complete. The data structure module is used to control the start of the entire MD5 calculation module and the number of calculations per cycle; as well as to indicate the execution stage of the MD5 calculation module and whether the calculation is complete.