Lightweight cryptographic algorithm integration methods, systems, and devices for RTOS
By acquiring and storing the time deterministic parameters of cryptographic algorithms on the RTOS platform, dynamically selecting the optimal algorithm and requesting an execution time budget, the problem of task deadline violations caused by algorithm execution uncertainty in embedded devices is solved, ensuring the stable operation of hard real-time tasks.
Patent Information
- Application Number
- CN202611379995.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-07
- Publication Date
- 2026-11-17
AI Technical Summary
In existing technologies, when embedded devices call cryptographic services for hard real-time tasks, the uncertainty of algorithm execution time makes it impossible for the scheduler to predict, which may cause the task to miss the deadline and cause abnormal system functions.
By acquiring the time deterministic parameters of each cryptographic algorithm to be integrated on the RTOS platform and storing them in association with the service interface, the algorithm with the best time deterministic performance is dynamically selected, and an execution time budget request is sent to the RTOS scheduler to ensure that hard real-time tasks meet the deadline constraints when calling cryptographic services.
This effectively avoids system malfunctions caused by occasional fluctuations in algorithm execution time, ensuring the normal execution of hard real-time tasks.
Smart Images

Figure CN123122818A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of lightweight cryptographic integration technology, and in particular to a lightweight cryptographic algorithm integration method, system and device for RTOS. Background Technology
[0002] Embedded devices widely run real-time operating systems to perform various real-time tasks, making the deployment of lightweight cryptographic algorithms to ensure data security a common requirement. Current technologies primarily obtain algorithm security and performance parameters through offline benchmarking and generate static evaluation results for developers to reference when selecting algorithms before deployment.
[0003] However, existing technologies suffer from the following technical problems: when hard real-time tasks need to call cryptographic services, the execution time of the same algorithm running multiple times under the same conditions is not fixed, and the fluctuation range may be several times the average time. The scheduler cannot predict this fluctuation, and the static evaluation results cannot reflect the uncertainty at runtime, which may cause the task to miss the deadline due to a random jump in the algorithm's execution time, leading to system malfunctions. Therefore, a cryptographic algorithm integration method is needed that can perceive the uncertainty of algorithm execution time at runtime and make decisions collaboratively with the scheduler. Summary of the Invention
[0004] Based on this, it is necessary to provide a lightweight cryptographic algorithm integration method, system, computer device, computer-readable storage medium, and computer program product for RTOS that can dynamically select time-deterministic matching cryptographic algorithms according to the real-time requirements of the task.
[0005] Firstly, this application provides a lightweight cryptographic algorithm integration method for RTOS, including:
[0006] Obtain the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform;
[0007] Register the service interfaces of each of the cryptographic algorithms to be integrated to the RTOS kernel of the target RTOS platform, and store the time deterministic parameters in association with the service interfaces;
[0008] In response to a cryptographic service request from an application task, obtain the real-time requirement level of the application task;
[0009] According to the real-time requirement level, the target cryptographic algorithm is obtained from the registered cryptographic algorithms; wherein, if the real-time requirement level is hard real-time, the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameters of the registered cryptographic algorithms.
[0010] The execution time budget request for the target cryptographic algorithm is sent to the RTOS scheduler of the target RTOS platform. If the execution time budget request is approved, the service interface of the target cryptographic algorithm is invoked to perform cryptographic services. If the execution time budget request is not approved, the target cryptographic algorithm is re-determined.
[0011] In one embodiment, obtaining the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform includes: creating a benchmark task on the target RTOS platform; the benchmark task has a lower priority than all application tasks to avoid interference with the scheduling of normal tasks during the benchmark testing process; through the benchmark task, for each cryptographic algorithm to be integrated, under fixed input data length and fixed key conditions, the cryptographic algorithm to be integrated is run N times consecutively, with a cache clearing instruction and a pipeline flushing instruction executed before each run to eliminate the cache influence of the previous run on the subsequent run, making the execution environment of each run independent of each other; using the hardware high-precision timer on the target RTOS platform, a first count value is read at the start time of each run of the cryptographic algorithm to be integrated, and a second count value is read at the end time, and the difference between the two count values is used as the execution time of that run, obtaining a sample set containing N execution time values; the difference between the maximum and minimum values of the N execution time values in the sample set is calculated as the time deterministic parameter.
[0012] In one embodiment, registering the service interfaces of each cryptographic algorithm to be integrated to the RTOS kernel of the target RTOS platform and storing the time deterministic parameters associated with the service interfaces includes: defining a unified service interface function signature for each cryptographic algorithm to be integrated, wherein the unified service interface function signature includes at least an initialization interface, an encryption interface, and a decryption interface, so that the interfaces of different cryptographic algorithms to be integrated are consistent in function name, number of parameters, parameter type, and return value type, and the upper-layer application calls different algorithms through the same set of interface names; for each cryptographic algorithm to be integrated, writing a corresponding encapsulation function according to the unified service interface function signature, and encapsulating the original functions of each cryptographic algorithm to be integrated. The heterogeneous calling methods are uniformly encapsulated into the same calling method; a contiguous storage space is allocated in the read-only data segment of the RTOS kernel, and an array of algorithm information structures is created in the storage space. The fields of the array of algorithm information structures include algorithm identifier, time determinism parameter, security strength parameter, initialization interface function pointer, encryption interface function pointer, and decryption interface function pointer; according to the preset enumeration order, the algorithm information of each cryptographic algorithm to be integrated is written into each entry of the array of algorithm information structures, so that each entry completely stores all the registration information of a cryptographic algorithm to be integrated, and an entry containing a null pointer is written at the end of the array of algorithm information structures as a traversal termination mark.
[0013] In one embodiment, when the application task initiates a cryptographic service request, the application task calls the message queue sending function of the target RTOS platform to send a cryptographic service request message containing the application task's task identifier to the message queue of the cryptographic service management task. Obtaining the real-time requirement level of the application task includes: reading the cryptographic service request message from the message queue, then calling the task control block query function provided by the RTOS kernel, using the task identifier as a parameter, to obtain the task control block pointer of the application task; reading the value of the priority field of the application task from the storage area pointed to by the task control block pointer, the value of the priority field being written by the RTOS kernel according to the parameters passed to the task creation function during task creation, and the value range being 0 to the maximum priority number configured by the system minus 1; comparing the value of the priority field with a preset first threshold and a second threshold, wherein the first threshold is greater than the second threshold; if the value of the priority field is greater than or equal to the first threshold, the application task is determined to be at a hard real-time level; if the value of the priority field is less than the second threshold, the application task is determined to be at a non-real-time level.
[0014] In one embodiment, obtaining the target cryptographic algorithm from the registered cryptographic algorithms according to the real-time requirement level includes: traversing each entry in the algorithm information structure array, reading the security strength parameter in each entry, and removing registered cryptographic algorithms whose security strength parameter is lower than a preset security threshold from the candidate set to obtain a valid candidate set; if the real-time requirement level is hard real-time, traversing the time deterministic parameter of each entry in the valid candidate set, and selecting the registered cryptographic algorithm with the smallest time deterministic parameter from the valid candidate set as the target cryptographic algorithm; if the real-time requirement level is soft real-time, traversing the time deterministic parameter and security strength parameter of each entry in the valid candidate set, and obtaining the target cryptographic algorithm from the registered cryptographic algorithms according to the time deterministic parameter and security strength parameter; if the real-time requirement level is non-real-time, traversing the security strength parameter of each entry in the valid candidate set, and selecting the registered cryptographic algorithm with the largest security strength parameter as the target cryptographic algorithm.
[0015] In one embodiment, sending the execution time budget request for the target cryptographic algorithm to the RTOS scheduler of the target RTOS platform includes: reading the maximum value of the execution time recorded in offline testing of the target cryptographic algorithm from the algorithm information structure array, as the worst-case execution time; adding the worst-case execution time to a preset protection margin to obtain the estimated execution time; calling the budget request function provided by the RTOS scheduler to send a budget request message containing the task identifier of the application task and the estimated execution time to the RTOS scheduler; after receiving the budget request message, the RTOS scheduler reads the deadline field and the executed time field of the application task from the task control block of the application task according to the task identifier, calculates whether the sum of the executed time and the estimated execution time is greater than the deadline; if the sum of the estimated execution times is less than or equal to the deadline, the approval status field in the budget request message is set to the approved status; if the sum of the estimated execution times is greater than the deadline, the approval status field is set to the rejected status, and the budget request message is returned to the cryptographic service management task.
[0016] In one embodiment, if the execution time budget request is not approved, the target cryptographic algorithm is re-determined, including: if the approval status field in the budget request message returned by the RTOS scheduler is in a rejected state, the target cryptographic algorithm is removed from the valid candidate set, and it is determined whether the valid candidate set is empty after the target cryptographic algorithm is removed; if the valid candidate set is not empty, the step of re-determining the target cryptographic algorithm from the valid candidate set is returned until the valid candidate set is empty or the execution time budget request is approved; if the valid candidate set is empty after removal, the data to be encrypted is split into multiple data blocks according to a preset data block size, and the steps of determining the target cryptographic algorithm and sending the budget request message are repeated for each data block, so that each data block is executed independently after obtaining approval from the RTOS scheduler.
[0017] Secondly, this application also provides a lightweight cryptographic algorithm integration system for RTOS, comprising:
[0018] The parameter acquisition module is used to acquire the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform;
[0019] An associated storage module is used to register the service interfaces of each of the cryptographic algorithms to be integrated to the RTOS kernel of the target RTOS platform, and to associate the time deterministic parameters with the service interfaces for storage.
[0020] The response level acquisition module is used to acquire the real-time requirement level of the application task in response to the password service request of the application task.
[0021] The target algorithm determination module is used to obtain a target cryptographic algorithm from the registered cryptographic algorithms according to the real-time requirement level; wherein, if the real-time requirement level is a hard real-time level, the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameters of the registered cryptographic algorithms.
[0022] The optimization module is used to send the execution time budget request of the target cryptographic algorithm to the RTOS scheduler of the target RTOS platform. If the execution time budget request is approved, the module calls the service interface of the target cryptographic algorithm to perform cryptographic services. If the execution time budget request is not approved, the module redetermines the target cryptographic algorithm.
[0023] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the method described in any embodiment of the first aspect.
[0024] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method described in any embodiment of the first aspect.
[0025] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the method described in any embodiment of the first aspect.
[0026] The aforementioned lightweight cryptographic algorithm integration method, system, computer device, computer-readable storage medium, and computer program product for RTOS obtains the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform; registers the service interfaces of each cryptographic algorithm to be integrated with the RTOS kernel of the target RTOS platform, and stores the time deterministic parameters associated with the service interfaces; responds to cryptographic service requests from application tasks and obtains the real-time requirement level of the application task; according to the real-time requirement level, obtains the target cryptographic algorithm from the registered cryptographic algorithms; wherein, if the real-time requirement level is hard real-time, the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameters of the registered cryptographic algorithms; sends an execution time budget request for the target cryptographic algorithm to the RTOS scheduler of the target RTOS platform; if the execution time budget request is approved, the service interface of the target cryptographic algorithm is invoked to execute the cryptographic service; if the execution time budget request is not approved, the target cryptographic algorithm is re-determined. This application obtains the time deterministic parameters of each algorithm in advance and associates them with the service interface. Therefore, it can evaluate whether the time cost of the cryptographic service will affect the task deadline before the task is executed, and dynamically select the algorithm with the best time determinism based on the evaluation results. This ensures that hard real-time tasks can still meet their deadline constraints when calling the cryptographic service, and avoids system malfunctions caused by occasional fluctuations in algorithm execution time. Attached Figure Description
[0027] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0028] Figure 1 This is a flowchart illustrating a lightweight cryptographic algorithm integration method for RTOS in one embodiment.
[0029] Figure 2 This is a flowchart illustrating the offline testing process for time deterministic parameters in one embodiment;
[0030] Figure 3 This is a flowchart illustrating the response to a cryptographic service request in one embodiment;
[0031] Figure 4 This is a block diagram of a lightweight cryptographic algorithm integration system for RTOS in one embodiment;
[0032] Figure 5 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0033] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0034] It should be noted that the terms "first," "second," etc., used in this application can be used to describe various elements, but these elements are not limited by these terms. These terms are only used to distinguish the first element from the second element. The terms "comprising" and "having," and any variations thereof, used in this application, are intended to cover non-exclusive inclusion. The term "multiple" used in this application refers to two or more. The term "and / or" used in this application refers to one of the embodiments, or any combination of multiple embodiments.
[0035] In one embodiment, such as Figure 1 As shown, a lightweight cryptographic algorithm integration method for RTOS is provided. This embodiment illustrates the application of this method to a terminal device deployed on an RTOS platform, such as a cryptographic service management task. In this embodiment, the method includes the following steps:
[0036] Step S101: Obtain the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform.
[0037] Among them, the cryptographic algorithms to be integrated refer to the cryptographic algorithms that need to be integrated and registered on the target RTOS platform, while the time determinism parameter is mainly used to characterize whether the execution time of each cryptographic algorithm to be integrated is stable and predictable. For example, the smaller the settable time determinism parameter, the more stable and predictable the algorithm execution time is.
[0038] Specifically, the cryptographic algorithms to be integrated can be tested by using terminal devices deployed on the target RTOS platform, such as cryptographic service management tasks deployed on the target RTOS platform, thereby obtaining the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform.
[0039] Step S102: Register the service interfaces of each cryptographic algorithm to be integrated to the RTOS kernel of the target RTOS platform, and store the time deterministic parameters associated with the service interfaces.
[0040] The RTOS kernel refers to the smallest software unit in the target RTOS platform that directly manages hardware resources, while the service interface refers to the unified and standardized call entry point exposed to the RTOS kernel and application layer after different cryptographic algorithms to be integrated are encapsulated.
[0041] Specifically, after obtaining the time deterministic parameters, the service interface of the cryptographic algorithm to be integrated can be registered to the RTOS kernel of the target RTOS platform, and the time deterministic parameters of each cryptographic algorithm to be integrated can be associated with the service interface and stored.
[0042] Step S103: In response to the application task's cryptographic service request, obtain the application task's real-time requirement level.
[0043] Real-time requirement levels can be used to describe the importance of the real-time performance of application tasks. Some application tasks must be completed on time, so they have the highest real-time requirement level, which is the hard real-time level. Some application tasks can be allowed to time out occasionally, so their corresponding real-time requirement level can be the soft real-time level. And some application tasks do not need to be completed on time, so their corresponding real-time requirement level can be the non-real-time level.
[0044] Specifically, when the cryptographic service management task receives a cryptographic service request from an application task, it can first determine the real-time requirement level of the application task.
[0045] Step S104: Obtain the target cryptographic algorithm from the registered cryptographic algorithms according to the real-time requirement level; wherein, if the real-time requirement level is hard real-time, the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameters of the registered cryptographic algorithms.
[0046] The target cryptographic algorithm refers to the target algorithm to be executed, while the registered cryptographic algorithm refers to the cryptographic algorithm integrated and registered on the target RTOS platform through steps S101 and S102. Specifically, after obtaining the real-time requirement level of the application task, the cryptographic service management task can obtain the target cryptographic algorithm from the cryptographic algorithms already registered on the target RTOS platform. If the real-time requirement level of the application task is a hard real-time level that must be completed on time, the target cryptographic algorithm can be obtained from the registered cryptographic algorithms by combining the time deterministic parameter to ensure that the application task can be completed on time.
[0047] Step S105: Send the execution time budget request for the target cryptographic algorithm to the RTOS scheduler of the target RTOS platform. If the execution time budget request is approved, call the service interface of the target cryptographic algorithm to execute the cryptographic service. If the execution time budget request is not approved, re-determine the target cryptographic algorithm.
[0048] The RTOS scheduler is used to approve execution time budget requests, determining whether a target cryptographic algorithm can be used to execute cryptographic services. Specifically, after selecting target cryptographic algorithms, the cryptographic service management task can send an execution time budget request for the target algorithm to the RTOS scheduler to request its use for cryptographic services. The RTOS scheduler then approves the execution time budget request and returns the approval result.
[0049] After receiving the approval result, if the approval result indicates that the cryptographic service can be executed through the target cryptographic algorithm, the cryptographic service management task will directly call the service interface of the target cryptographic algorithm to execute the cryptographic service. However, if the approval result is not received, such as a rejection result indicating that the cryptographic service cannot be executed through the target cryptographic algorithm, step S104 needs to be re-executed to determine the target cryptographic algorithm in order to obtain the approval result again.
[0050] In the aforementioned lightweight cryptographic algorithm integration method for RTOS, the following steps are taken: First, the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform are obtained. Then, the service interfaces of each cryptographic algorithm to be integrated are registered with the RTOS kernel of the target RTOS platform, and the time deterministic parameters are associated with and stored with the service interfaces. In response to cryptographic service requests from application tasks, the real-time requirement level of the application task is obtained. Based on the real-time requirement level, the target cryptographic algorithm is obtained from the registered cryptographic algorithms. Specifically, if the real-time requirement level is hard real-time, the target cryptographic algorithm is obtained from the registered cryptographic algorithms based on their time deterministic parameters. Finally, an execution time budget request for the target cryptographic algorithm is sent to the RTOS scheduler of the target RTOS platform. If the execution time budget request is approved, the service interface of the target cryptographic algorithm is invoked to execute the cryptographic service. If the execution time budget request is not approved, the target cryptographic algorithm is re-determined. This application obtains the time deterministic parameters of each algorithm in advance and associates them with the service interface. Therefore, it can evaluate whether the time cost of the cryptographic service will affect the task deadline before the task is executed, and dynamically select the algorithm with the best time determinism based on the evaluation results. This ensures that hard real-time tasks can still meet their deadline constraints when calling the cryptographic service, and avoids system malfunctions caused by occasional fluctuations in algorithm execution time.
[0051] In one embodiment, step S101 may further include: creating a benchmark task on the target RTOS platform; the benchmark task has a lower priority than all application tasks to avoid interference with the scheduling of normal tasks during the benchmark process; through the benchmark task, for each cryptographic algorithm to be integrated, under the conditions of fixed input data length and fixed key, the cryptographic algorithm to be integrated is run N times continuously, and a cache clearing instruction and a pipeline flushing instruction are executed before each run to eliminate the cache influence of the previous run on the subsequent run, so that the execution environment of each run is independent of each other; using the hardware high-precision timer on the target RTOS platform, a first count value is read at the start time of each cryptographic algorithm to be integrated run and a second count value is read at the end time, and the difference between the two count values is used as the execution time of that run, to obtain a sample set containing N execution time values; the difference between the maximum and minimum values of the N execution time values in the sample set is calculated as a time deterministic parameter.
[0052] Specifically, a benchmark task can be created on the target RTOS platform. This benchmark task is set to have a lower priority than all application tasks, thus avoiding interference with the scheduling and execution of normal tasks. The benchmark task runs automatically when the system is idle, without preempting the execution of any application tasks. Through this benchmark task, for each cryptographic algorithm to be integrated, the algorithm can be run continuously a preset number of times under fixed input data length and fixed key conditions. This preset number of times is set to at least one thousand.
[0053] Furthermore, a cache clearing operation and a pipeline flushing operation are performed before each run of the algorithm, ensuring that each run of the algorithm is in an independent initial state, eliminating the cache influence of the previous run on the subsequent run. Utilizing the hardware timer on the target RTOS platform, a first count value is read at the start of each run, and a second count value is read at the end of each run. The difference between the two count values is used as the execution time of that run, resulting in a set of execution time samples.
[0054] Meanwhile, the benchmark test task can insert a random idle waiting time after each run ends and before the next run begins. This idle waiting time is obtained by reading the hardware random number generator, so that the start time of each run is not uniformly distributed on the time axis, avoiding the introduction of measurement deviation due to the synchronization lock effect between external periodic events and test runs.
[0055] After obtaining the execution time sample set, the difference between the maximum and minimum values in the sample set can be calculated as a time deterministic parameter for the algorithm. The smaller the difference, the more stable and predictable the algorithm's execution time. For this sample set, outlier removal is also performed: the median and absolute median difference of the sample set are calculated, and samples deviating from the median by more than a preset multiple are removed. Then, the difference between the maximum and minimum values is recalculated based on the removed sample set as a time deterministic parameter for the algorithm, to avoid outlier samples contaminating the measurement results due to occasional system interference. Simultaneously, the arithmetic mean of the sample set is calculated as the average execution time parameter, and the maximum value in the sample set is used as the worst-case execution time parameter.
[0056] Furthermore, this embodiment also needs to obtain the security strength parameters of each algorithm, and the specific process is as follows:
[0057] For lightweight cryptographic algorithms that include S-boxes, the S-box table of the algorithm is read. An S-box is a non-linear mapping table from a fixed-length input to a fixed-length output. In lightweight cryptographic algorithms, the input length is typically 4 or 5 bits. It is stored as an array, with each entry corresponding to an input value and its corresponding output value. A difference distribution table for this S-box is constructed. The difference distribution table is a two-dimensional matrix, where the row and column indices correspond to the input difference and output difference values, respectively. The matrix element value is the number of input values that, when the input difference is XORed with the S-box, equals the output difference.
[0058] Iterate through all non-zero input difference values and all output difference values, find the maximum value in the matrix, and divide this maximum value by the total number of inputs to the S-box to obtain the maximum difference probability. Construct a linear approximation table for this S-box. The linear approximation table is a two-dimensional matrix, where the row and column indices correspond to the input mask value and the output mask value, respectively. The value of each matrix element is the number of input values that make the inner product of the input mask and the input equal to the inner product of the output mask and the output of the S-box minus half the total number of inputs.
[0059] Iterate through all non-zero input mask values and all non-zero output mask values, find the maximum value in the matrix, and divide this maximum value by the total number of inputs to the S-box to obtain the maximum linear deviation. Determine the security strength parameter based on the maximum difference probability and the maximum linear deviation. The smaller of the reciprocal of the maximum difference probability and the reciprocal of the maximum linear deviation can be taken as the security strength parameter; the larger this value, the stronger the algorithm's resistance to differential and linear attacks.
[0060] In this embodiment, the execution time of each cryptographic algorithm to be integrated can be tested by creating a benchmark test task. At the same time, the difference between the maximum and minimum values of multiple execution times can be used to obtain the time deterministic parameters that characterize the stability of the algorithm's execution time. This method can improve the accuracy of obtaining the time deterministic parameters.
[0061] In one embodiment, step S102 may further include: defining a unified service interface function signature for each cryptographic algorithm to be integrated, wherein the unified service interface function signature includes at least an initialization interface, an encryption interface, and a decryption interface, so that the interfaces of different cryptographic algorithms to be integrated are consistent in function name, number of parameters, parameter type, and return value type, and the upper-layer application calls different algorithms through the same set of interface names; for each cryptographic algorithm to be integrated, writing a corresponding encapsulation function according to the unified service interface function signature, and uniformly encapsulating the original heterogeneous calling methods of each cryptographic algorithm to be integrated into the same calling method; allocating a contiguous storage space in the read-only data segment of the RTOS kernel, creating an algorithm information structure array in the storage space, wherein the fields of the algorithm information structure array include algorithm identifier, time determinism parameter, security strength parameter, initialization interface function pointer, encryption interface function pointer, and decryption interface function pointer; writing the algorithm information of each cryptographic algorithm to be integrated into each entry of the algorithm information structure array according to a preset enumeration order, so that each entry completely stores all the registration information of a cryptographic algorithm to be integrated, and writing an entry containing a null pointer at the end of the algorithm information structure array as a traversal termination mark.
[0062] Specifically, a standardized service interface function signature is first defined for all cryptographic algorithms to be integrated. This signature includes an initialization interface, an encryption interface, and a decryption interface. The interfaces for each algorithm maintain consistency in function name, number of parameters, parameter types, and return value type.
[0063] Then, for each algorithm, a corresponding wrapper function is written according to a unified function signature, which encapsulates the original heterogeneous calling methods of each algorithm into a unified calling method, so that the upper-layer application can call different algorithms through the same set of interface names without being aware of the specific implementation differences of each algorithm.
[0064] Next, a contiguous block of memory is allocated in the read-only data segment of the RTOS kernel, and an array of algorithm information structures is created within this memory space. This structure must contain at least an algorithm identifier field, a timing deterministic parameter field, a security strength parameter field, an initialization interface function pointer, an encryption interface function pointer, and a decryption interface function pointer. In embedded systems, the read-only data segment resides in Flash memory and is not limited by stack or heap space.
[0065] Finally, the information for each algorithm is written into the respective table entries in a preset order, with each entry storing all the registration information for one algorithm. An entry containing a null pointer is written at the end of the array as a traversal termination marker; subsequent traversals stop when a null function pointer is encountered, thus avoiding access to illegal memory addresses.
[0066] Furthermore, a secondary index table is created in the read-only data segment of the RTOS kernel. This secondary index table uses the value of the time deterministic parameter as the index key and organizes the algorithm identifiers of each algorithm into a linked list structure in ascending order of the time deterministic parameter. This allows the search for the algorithm with the best time deterministic performance at the hard real-time level to directly obtain the target algorithm from the head of the secondary index table without traversing the entire algorithm information structure array, thus reducing the search time complexity from linear to constant.
[0067] In this embodiment, the time deterministic parameters can also be associated with and stored with the service interface through an array of algorithm information structures. This storage method can encapsulate the original heterogeneous calling methods of each algorithm into a unified calling method. Therefore, upper-layer applications can call different algorithms through the same set of interface names without needing to be aware of the specific implementation differences of each algorithm, thereby further improving the efficiency of algorithm calling.
[0068] Furthermore, when the application task initiates a cryptographic service request, the application task calls the message queue sending function of the target RTOS platform to send the cryptographic service request message containing the application task's task identifier to the message queue of the cryptographic service management task; step S103 may further include: after reading the cryptographic service request message from the message queue, calling the task control block query function provided by the RTOS kernel, using the task identifier as a parameter, to obtain the application task's task control block pointer; reading the value of the application task's priority field from the storage area pointed to by the task control block pointer, the value of the priority field is written by the RTOS kernel according to the parameters passed to the task creation function when the task is created, and the value range is from 0 to the maximum priority number configured by the system minus 1; comparing the value of the priority field with a preset first threshold and a second threshold, wherein the first threshold is greater than the second threshold, if the value of the priority field is greater than or equal to the first threshold, the application task is determined to be at the hard real-time level, and if the value of the priority field is less than the second threshold, the application task is determined to be at the non-real-time level.
[0069] Specifically, the cryptographic service management task is created and runs independently before the RTOS starts. After creation, it enters a waiting state, awaiting the arrival of cryptographic service request messages. When an application task needs cryptographic services, it sends a cryptographic service request message to the cryptographic service management task through the RTOS message queue. The message carries the application task's task identifier.
[0070] After reading the request message from the message queue, the cryptographic service management task calls the RTOS kernel's task control block query function with the task identifier as a parameter to obtain the task control block pointer for that task. The task control block is a data structure maintained by the RTOS kernel for each task, storing all management information for that task.
[0071] Read the priority field value of the task from the task control block. This field is written by the RTOS kernel when the task is created. The value ranges from 0 to the maximum priority number configured by the system minus 1. The larger the value, the higher the priority.
[0072] The priority value is compared with a preset first threshold and a second threshold, where the first threshold is greater than the second threshold. If the priority value is greater than or equal to the first threshold, it is determined to be a hard real-time level, meaning that the task must be completed before the deadline, otherwise it may cause system failure or serious consequences. If the priority value is between the first and second thresholds, it is determined to be a soft real-time level, meaning that occasional timeouts will not cause system failure. If the priority value is less than the second threshold, it is determined to be a non-real-time level, meaning that it is not subject to the deadline.
[0073] In this embodiment, the application task can send a password service request message through the RTOS message queue to trigger the password service request, and can obtain the real-time requirement level by retrieving the value of the priority field stored in the task control block. This method can improve the efficiency of obtaining the real-time requirement level.
[0074] In one embodiment, step S104 may further include: traversing each entry in the algorithm information structure array, reading the security strength parameter in each entry, removing registered cryptographic algorithms with security strength parameters lower than a preset security threshold from the candidate set to obtain a valid candidate set; if the real-time requirement level is hard real-time, traversing the time deterministic parameter of each entry in the valid candidate set, selecting the registered cryptographic algorithm with the smallest time deterministic parameter from the valid candidate set as the target cryptographic algorithm; if the real-time requirement level is soft real-time, traversing the time deterministic parameter and security strength parameter of each entry in the valid candidate set, obtaining the target cryptographic algorithm from the registered cryptographic algorithms based on the time deterministic parameter and security strength parameter; if the real-time requirement level is non-real-time, traversing the security strength parameter of each entry in the valid candidate set, selecting the registered cryptographic algorithm with the largest security strength parameter as the target cryptographic algorithm.
[0075] Specifically, the cryptographic service management task iterates through each entry in the algorithm information structure array from the beginning, reads the security strength parameter of each entry, and eliminates algorithms with security strength parameters lower than a preset security threshold to obtain a valid candidate set. This security threshold is determined based on the security compliance requirements of the target application scenario. For example, applications involving financial payments or critical infrastructure require a higher threshold, while applications for general IoT sensors can have a lower threshold.
[0076] If the real-time requirement is at the hard real-time level, then the time deterministic parameters of each entry in the valid candidate set are traversed, and the algorithm with the smallest value of the time deterministic parameter is selected as the target algorithm. That is, the algorithm with the smallest execution time fluctuation is selected rather than the algorithm with the shortest average execution time, because the predictability of execution time is more important than average speed for hard real-time tasks. If the time deterministic parameters of multiple algorithms are the same or the difference is within the preset tolerance range, then the algorithm with the highest safety strength parameter is further selected as the target algorithm.
[0077] If the real-time requirement level is soft real-time, then each entry in the valid candidate set is traversed, and the safety strength parameter and time determinism parameter of each algorithm are normalized to dimensionless values in the range of 0 to 1. The ratio of the normalized safety strength value to the time determinism value is used as a comprehensive score, and the algorithm with the highest comprehensive score is selected as the target algorithm, thus achieving a balance between safety and time determinism. Specifically, the time determinism parameter is first taken as its reciprocal and then normalized, so that a larger normalized value indicates better time determinism.
[0078] If the real-time requirement level is non-real-time, then iterate through the security strength parameters of each item in the valid candidate set and select the algorithm with the largest security strength parameter as the target algorithm, because non-real-time tasks are not subject to deadline constraints and can prioritize the highest security.
[0079] In this embodiment, the target cryptographic algorithm can be selected based on the real-time requirement level, as well as the time determinism parameters and security strength parameters of each registered cryptographic algorithm. This method can improve the accuracy of the target cryptographic algorithm selection.
[0080] In one embodiment, step S105 may further include: reading the maximum value of the execution time recorded in the offline test of the target cryptographic algorithm from the algorithm information structure array, as the worst-case execution time; adding the worst-case execution time to a preset protection margin to obtain the estimated execution time; calling the budget request function provided by the RTOS scheduler to send a budget request message containing the task identifier of the application task and the estimated execution time to the RTOS scheduler; after receiving the budget request message, the RTOS scheduler reads the deadline field and the executed time field of the application task from the task control block of the application task according to the task identifier, calculates whether the sum of the executed time and the estimated execution time is greater than the deadline; if the sum of the estimated execution times is less than or equal to the deadline, the approval status field in the budget request message is set to the approved status; if the sum of the estimated execution times is greater than the deadline, the approval status field is set to the rejected status, and the budget request message after setting is returned to the cryptographic service management task.
[0081] Specifically, the cryptographic service management task reads the worst-case execution time field corresponding to the target algorithm from the algorithm information structure array, adds this value to the preset protection margin, and obtains the estimated execution time. The protection margin is used to cover system overhead that cannot be simulated in offline testing, such as interrupt response, context switching, and bus arbitration latency, and can be dynamically configured according to system load.
[0082] The cryptographic service management task calls the budget request interface provided by the RTOS scheduler to send a budget request message containing the task identifier and the estimated execution time to the scheduler.
[0083] Upon receiving a budget request message, the scheduler reads the deadline and elapsed execution time fields from the task's task control block based on the task identifier. The deadline is set during task creation according to the task's cycle and expected computational load, while the elapsed execution time is cumulatively updated by the scheduler in the interrupt handler function at each clock tick. If the sum of the elapsed execution time and the estimated execution time is less than or equal to the deadline, the scheduler approves the budget and sets the approval status field in the budget request message to "approved." If the sum of the elapsed execution time and the estimated execution time is greater than the deadline, the scheduler rejects the budget and sets the approval status field to "rejected." The scheduler then returns the updated budget request message to the cryptographic service management task.
[0084] Before executing the above approval logic, the scheduler first checks whether there is an incomplete cryptographic service budget usage recorded in the task control block of the task. If so, the incomplete budget usage is deducted from the executed time before proceeding with this approval, so as to avoid duplicate billing caused by the previous budget application being approved but not actually executed.
[0085] In this embodiment, the estimated execution time can be obtained based on the maximum execution time of the target cryptographic algorithm and the preset protection margin. Then, based on the execution time of the application task and the estimated execution time, it is determined whether the sum of the execution time and the estimated execution time is less than or equal to the deadline. Only when this condition is met is execution approved. This method can ensure that the target cryptographic algorithm can be completed before the deadline of the application task.
[0086] Further, step S105 may include: if the approval status field in the budget request message returned by the RTOS scheduler is in a rejected state, then the target cryptographic algorithm is removed from the valid candidate set, and it is determined whether the valid candidate set is empty after the target cryptographic algorithm is removed; if the valid candidate set is not empty, then the step of re-determining the target cryptographic algorithm from the valid candidate set is returned until the valid candidate set is empty or the approval result of the execution time budget request is obtained; if the valid candidate set is empty after removal, then the data to be encrypted is split into multiple data blocks according to the preset data block size, and the steps of determining the target cryptographic algorithm and sending the budget request message are repeated for each data block, so that each data block is independently approved by the RTOS scheduler and then executed.
[0087] After the scheduler completes the budget request message settings and returns, the cryptographic service management task can read the approval status field in the budget request message. If it is approved, it locates the corresponding entry in the algorithm information structure array based on the algorithm identifier, reads the encryption interface function pointer, calls the encryption interface of the target algorithm through the function pointer to execute the cryptographic service, returns the encryption result to the application task through the message queue, and notifies the scheduler to release the budget through the budget release interface. The scheduler updates the execution time of the task accordingly.
[0088] If the status is rejected, the current target algorithm is removed from the valid candidate set, and it is determined whether the valid candidate set is empty after the removal. If it is not empty, the process returns to step S104 to redetermine the target algorithm, and the budget request message is resent with the newly determined target algorithm. If the status is empty, meaning all algorithms have been tried and all have been rejected by the scheduler, the data to be encrypted is split into multiple data blocks. Each data block executes the steps of determining the target algorithm and sending the budget request independently, and is executed sequentially after obtaining approval from the scheduler independently.
[0089] In this embodiment, the target cryptographic algorithm can be re-selected after the target cryptographic algorithm refuses to execute. This method can ensure the stability of the execution of cryptographic service requests for application tasks.
[0090] In one embodiment, a method for integrating lightweight cryptographic algorithms into an RTOS is also provided, the method comprising:
[0091] Step 1: Obtain the time deterministic parameters of each algorithm on the target RTOS platform, specifically including:
[0092] A benchmark task is created on the target RTOS platform. This benchmark task is set to a priority lower than all application tasks to avoid interfering with the scheduling and execution of normal tasks. The benchmark task runs automatically when the system is idle and will not preempt the execution of any application tasks. Figure 2The diagram shown is a flowchart of the offline testing process for the algorithm's time deterministic parameters in this embodiment.
[0093] This benchmark test task involves running each lightweight cryptographic algorithm to be integrated a predetermined number of times under fixed input data length and fixed key conditions. The predetermined number of times is at least one thousand.
[0094] Furthermore, before each run, a cache clearing operation and a pipeline flushing operation are performed to ensure that each run is in an independent initial state, eliminating the cache influence of the previous run on the subsequent run. Utilizing the hardware timer on the target RTOS platform, a first count value is read at the start of each run and a second count value is read at the end of each run. The difference between the two count values is used as the execution time of that run, resulting in an execution time sample set.
[0095] After each run of the benchmark test task ends and before the next run begins, a random idle waiting time is inserted. This idle waiting time is obtained by reading a hardware random number generator, so that the start time of each run is not uniformly distributed on the time axis, thus avoiding measurement deviations caused by the synchronization lock effect between external periodic events and the test run.
[0096] The difference between the maximum and minimum values in the sample set is calculated and used as the time deterministic parameter for the algorithm. A smaller difference indicates a more stable and predictable algorithm execution time. For this sample set, outlier removal is also performed: the median and absolute median difference of the sample set are calculated, and samples deviating from the median by more than a preset multiple are removed. The difference between the maximum and minimum values is then recalculated based on the removed sample set and used as the time deterministic parameter for the algorithm to avoid outlier contamination of measurement results due to occasional system interference. Simultaneously, the arithmetic mean of the sample set is calculated as the average execution time parameter, and the maximum value in the sample set is used as the worst-case execution time parameter.
[0097] Furthermore, embodiments of the present invention also require obtaining the security strength parameters of each algorithm.
[0098] For lightweight cryptographic algorithms that include S-boxes, the S-box table of the algorithm is read. An S-box is a non-linear mapping table from a fixed-length input to a fixed-length output. In lightweight cryptographic algorithms, the input length is typically 4 or 5 bits. It is stored as an array, with each entry corresponding to an input value and its corresponding output value. A difference distribution table for this S-box is constructed. The difference distribution table is a two-dimensional matrix, where the row and column indices correspond to the input difference and output difference values, respectively. The matrix element value is the number of input values that, when the input difference is XORed with the S-box, equals the output difference.
[0099] Iterate through all non-zero input difference values and all output difference values, find the maximum value in the matrix, and divide this maximum value by the total number of inputs to the S-box to obtain the maximum difference probability. Construct a linear approximation table for this S-box. The linear approximation table is a two-dimensional matrix, where the row and column indices correspond to the input mask value and the output mask value, respectively. The value of each matrix element is the number of input values that make the inner product of the input mask and the input equal to the inner product of the output mask and the output of the S-box minus half the total number of inputs.
[0100] Iterate through all non-zero input mask values and all non-zero output mask values, find the maximum value in the matrix, and divide this maximum value by the total number of inputs to the S-box to obtain the maximum linear deviation. Determine the security strength parameter based on the maximum difference probability and the maximum linear deviation. The smaller of the reciprocal of the maximum difference probability and the reciprocal of the maximum linear deviation can be taken as the security strength parameter; the larger this value, the stronger the algorithm's resistance to differential and linear attacks.
[0101] Step 2 involves registering the service interfaces of each algorithm to the RTOS kernel and associating and storing the time deterministic parameters with the corresponding service interfaces. This includes:
[0102] First, a standardized service interface function signature is defined for all lightweight cryptographic algorithms to be integrated. This signature includes an initialization interface, an encryption interface, and a decryption interface. The interfaces for each algorithm maintain consistency in function name, number of parameters, parameter types, and return value type.
[0103] For each algorithm, a corresponding wrapper function is written according to a unified function signature, which encapsulates the original heterogeneous calling methods of each algorithm into a unified calling method, so that upper-layer applications can call different algorithms through the same set of interface names without being aware of the specific implementation differences of each algorithm.
[0104] Allocate a contiguous block of memory in the read-only data segment of the RTOS kernel, and create an array of algorithm information structures within this memory space. This structure must contain at least an algorithm identifier field, a timing deterministic parameter field, a security strength parameter field, an initialization interface function pointer, an encryption interface function pointer, and a decryption interface function pointer. In embedded systems, the read-only data segment resides in Flash memory and is not limited by stack or heap space. The aforementioned information for each algorithm is written into its respective table entry in a preset order, with each entry storing all registration information for one algorithm. An entry containing a null pointer is written to the end of the array as a traversal termination marker; subsequent traversals stop when a null function pointer is encountered, preventing access to illegal memory addresses.
[0105] Furthermore, a secondary index table is created in the read-only data segment of the RTOS kernel. This secondary index table uses the value of the time deterministic parameter as the index key and organizes the algorithm identifiers of each algorithm into a linked list structure in ascending order of the time deterministic parameter. This allows the search for the algorithm with the best time deterministic performance at the hard real-time level to directly obtain the target algorithm from the head of the secondary index table without traversing the entire algorithm information structure array, thus reducing the search time complexity from linear to constant.
[0106] Step 3: In response to the cryptographic service request from the application task, obtain the real-time requirement level of the application task. Figure 3 This is a schematic diagram illustrating the response to a cryptographic service request in this embodiment.
[0107] Specifically, the cryptographic service management task is created and runs independently before the RTOS starts. After creation, it enters a waiting state, awaiting the arrival of cryptographic service request messages. When an application task needs cryptographic services, it sends a cryptographic service request message to the cryptographic service management task through the RTOS message queue. The message carries the application task's task identifier.
[0108] After reading the request message from the message queue, the cryptographic service management task calls the RTOS kernel's task control block query function with the task identifier as a parameter to obtain the task control block pointer for that task. The task control block is a data structure maintained by the RTOS kernel for each task, storing all management information for that task.
[0109] Read the priority field value of the task from the task control block. This field is written by the RTOS kernel when the task is created. The value ranges from 0 to the maximum priority number configured by the system minus 1. The larger the value, the higher the priority.
[0110] The priority value is compared with a preset first threshold and a second threshold, where the first threshold is greater than the second threshold. If the priority value is greater than or equal to the first threshold, it is determined to be a hard real-time level, meaning that the task must be completed before the deadline, otherwise it may cause system failure or serious consequences. If the priority value is between the first and second thresholds, it is determined to be a soft real-time level, meaning that occasional timeouts will not cause system failure. If the priority value is less than the second threshold, it is determined to be a non-real-time level, meaning that it is not subject to the deadline.
[0111] Step 4: Based on the real-time requirement level, determine the target algorithm from the registered algorithms, specifically including:
[0112] The cryptographic service management task iterates through each entry in the algorithm information structure array from the beginning, reading the security strength parameter of each entry one by one. Algorithms with security strength parameters lower than a preset security threshold are eliminated to obtain a valid candidate set. This security threshold is determined based on the security compliance requirements of the target application scenario. For example, applications involving financial payments or critical infrastructure require a higher threshold, while applications for general IoT sensors can have a lower threshold.
[0113] If the real-time requirement is at the hard real-time level, then the time deterministic parameters of each entry in the valid candidate set are traversed, and the algorithm with the smallest value of the time deterministic parameter is selected as the target algorithm. That is, the algorithm with the smallest execution time fluctuation is selected rather than the algorithm with the shortest average execution time, because the predictability of execution time is more important than average speed for hard real-time tasks. If the time deterministic parameters of multiple algorithms are the same or the difference is within the preset tolerance range, then the algorithm with the highest safety strength parameter is further selected as the target algorithm.
[0114] If the real-time requirement level is soft real-time, then each entry in the valid candidate set is traversed, and the safety strength parameter and time determinism parameter of each algorithm are normalized to dimensionless values in the range of 0 to 1. The ratio of the normalized safety strength value to the time determinism value is used as a comprehensive score, and the algorithm with the highest comprehensive score is selected as the target algorithm, thus achieving a balance between safety and time determinism. Specifically, the time determinism parameter is first taken as its reciprocal and then normalized, so that a larger normalized value indicates better time determinism.
[0115] If the real-time requirement level is non-real-time, then iterate through the security strength parameters of each item in the valid candidate set and select the algorithm with the largest security strength parameter as the target algorithm, because non-real-time tasks are not subject to deadline constraints and can prioritize the highest security.
[0116] Step 5: Send the execution time budget request for the target algorithm to the RTOS scheduler. If approved, call the service interface of the target algorithm to execute the cryptographic service. If not approved, re-determine the target algorithm.
[0117] Specifically, the cryptographic service management task reads the worst-case execution time field corresponding to the target algorithm from the algorithm information structure array, adds this value to the preset protection margin, and obtains the estimated execution time. The protection margin is used to cover system overhead that cannot be simulated in offline testing, such as interrupt response, context switching, and bus arbitration latency, and can be dynamically configured according to system load.
[0118] The cryptographic service management task calls the budget request interface provided by the RTOS scheduler to send a budget request message containing the task identifier and the estimated execution time to the scheduler.
[0119] Upon receiving a budget request message, the scheduler reads the deadline and elapsed execution time fields from the task's task control block based on the task identifier. The deadline is set during task creation according to the task's cycle and expected computational load, while the elapsed execution time is cumulatively updated by the scheduler in the interrupt handler function at each clock tick. If the sum of the elapsed execution time and the estimated execution time is less than or equal to the deadline, the scheduler approves the budget and sets the approval status field in the budget request message to "approved." If the sum of the elapsed execution time and the estimated execution time is greater than the deadline, the scheduler rejects the budget and sets the approval status field to "rejected." The scheduler then returns the updated budget request message to the cryptographic service management task.
[0120] Before executing the above approval logic, the scheduler first checks whether there is an incomplete cryptographic service budget usage recorded in the task control block of the task. If so, the incomplete budget usage is deducted from the executed time before proceeding with this approval, so as to avoid duplicate billing caused by the previous budget application being approved but not actually executed.
[0121] The cryptographic service management task reads the approval status field. If the status is approved, it locates the corresponding entry in the algorithm information structure array based on the algorithm identifier, reads the encryption interface function pointer, calls the encryption interface of the target algorithm through the function pointer to execute the cryptographic service, returns the encryption result to the application task through the message queue, and notifies the scheduler to release the current budget through the budget release interface. The scheduler then updates the execution time of the task accordingly.
[0122] If the status is rejected, the current target algorithm is removed from the valid candidate set, and it is determined whether the valid candidate set is empty after the removal. If it is not empty, the process returns to step S104 to redetermine the target algorithm, and the budget request message is resent with the newly determined target algorithm. If the status is empty, meaning all algorithms have been tried and all have been rejected by the scheduler, the data to be encrypted is split into multiple data blocks. Each data block executes the steps of determining the target algorithm and sending the budget request independently, and is executed sequentially after obtaining approval from the scheduler independently.
[0123] This embodiment achieves bidirectional communication and collaborative decision-making between the cryptographic service and the RTOS scheduler. The scheduler is no longer unaware of the execution time of the cryptographic service; instead, it proactively participates in the selection of cryptographic algorithms based on the remaining time margin of the task, ensuring that the task still meets the deadline constraint when calling the cryptographic service. This embodiment pre-obtains the time deterministic parameters of each algorithm and associates them with the task scheduler. This allows the scheduler to assess whether the time overhead of the cryptographic service will affect the task deadline before task execution and dynamically selects the algorithm with the optimal time determinism based on the assessment results. This ensures that hard real-time tasks still meet their deadline constraints when calling the cryptographic service, avoiding system malfunctions caused by occasional fluctuations in algorithm execution time.
[0124] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps. It is understood that the steps in different embodiments can be freely combined as needed, and all non-contradictory solutions formed by such combinations are within the scope of protection of this application.
[0125] Based on the same inventive concept, this application also provides a lightweight cryptographic algorithm integration system for RTOS, used to implement the aforementioned lightweight cryptographic algorithm integration method for RTOS. The solution provided by this system is similar to the implementation scheme described in the above method. Therefore, the specific limitations of one or more embodiments of the lightweight cryptographic algorithm integration system for RTOS provided below can be found in the limitations of the lightweight cryptographic algorithm integration method for RTOS described above, and will not be repeated here.
[0126] In one embodiment, such as Figure 4 As shown, a lightweight cryptographic algorithm integration system for RTOS is provided, including: a parameter acquisition module 401, an associated storage module 402, a response level acquisition module 403, a target algorithm determination module 404, and an optimization module 405, wherein:
[0127] The parameter acquisition module 401 is used to acquire the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform;
[0128] The associated storage module 402 is used to register the service interfaces of each cryptographic algorithm to be integrated to the RTOS kernel of the target RTOS platform, and to associate the time deterministic parameters with the service interfaces for storage.
[0129] The response level acquisition module 403 is used to respond to the password service request of the application task and obtain the real-time requirement level of the application task.
[0130] The target algorithm determination module 404 is used to obtain the target cryptographic algorithm from the registered cryptographic algorithms according to the real-time requirement level; wherein, if the real-time requirement level is hard real-time, the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameters of the registered cryptographic algorithms.
[0131] The optimization module 405 is used to send the execution time budget request of the target cryptographic algorithm to the RTOS scheduler of the target RTOS platform. If the execution time budget request is approved, the service interface of the target cryptographic algorithm is called to execute the cryptographic service. If the execution time budget request is not approved, the target cryptographic algorithm is re-determined.
[0132] In one embodiment, the parameter acquisition module 401 is further used to create a benchmark task on the target RTOS platform. The benchmark task has a lower priority than all application tasks to avoid interference with the scheduling of normal tasks during the benchmark process. Through the benchmark task, for each cryptographic algorithm to be integrated, the algorithm is run N times continuously under the conditions of fixed input data length and fixed key. Before each run, a cache clearing instruction and a pipeline flushing instruction are executed to eliminate the cache influence of the previous run on the subsequent run, making the execution environment of each run independent of each other. Using the high-precision hardware timer on the target RTOS platform, a first count value is read at the start time of each cryptographic algorithm run and a second count value is read at the end time. The difference between the two count values is used as the execution time of that run, obtaining a sample set containing N execution time values. The difference between the maximum and minimum values of the N execution time values in the sample set is calculated as a time deterministic parameter.
[0133] In one embodiment, the associated storage module 402 is further configured to define a unified service interface function signature for each cryptographic algorithm to be integrated. The unified service interface function signature includes at least an initialization interface, an encryption interface, and a decryption interface, ensuring that the interfaces of different cryptographic algorithms to be integrated are consistent in function name, number of parameters, parameter type, and return value type. The upper-layer application calls different algorithms through the same set of interface names. For each cryptographic algorithm to be integrated, a corresponding encapsulation function is written according to the unified service interface function signature to encapsulate the original heterogeneous calling methods of each cryptographic algorithm to be integrated into the same calling method. A contiguous storage space is allocated in the read-only data segment of the RTOS kernel, and an algorithm information structure array is created in the storage space. The fields of the algorithm information structure array include algorithm identifier, time determinism parameter, security strength parameter, initialization interface function pointer, encryption interface function pointer, and decryption interface function pointer. According to a preset enumeration order, the algorithm information of each cryptographic algorithm to be integrated is written into each entry of the algorithm information structure array, so that each entry completely stores all the registration information of a cryptographic algorithm to be integrated, and an entry containing a null pointer is written at the end of the algorithm information structure array as a traversal termination mark.
[0134] In one embodiment, when an application task initiates a cryptographic service request, the application task calls the message queue sending function of the target RTOS platform to send a cryptographic service request message containing the application task's task identifier to the message queue of the cryptographic service management task. The response level acquisition module 403 is further used to read the cryptographic service request message from the message queue, and then call the task control block query function provided by the RTOS kernel to obtain the application task's task control block pointer with the task identifier as a parameter. From the storage area pointed to by the task control block pointer, the value of the application task's priority field is read. The value of the priority field is written by the RTOS kernel according to the parameters passed to the task creation function when the task is created, and the value range is from 0 to the maximum priority number configured by the system minus 1. The value of the priority field is compared with a preset first threshold and a second threshold, where the first threshold is greater than the second threshold. If the priority value is greater than or equal to the first threshold, the application task is determined to be at the hard real-time level. If the priority field value is less than the second threshold, the application task is determined to be at the non-real-time level.
[0135] In one embodiment, the target algorithm determination module 404 is further configured to traverse each entry in the algorithm information structure array, read the security strength parameter in each entry, and remove registered cryptographic algorithms with security strength parameters lower than a preset security threshold from the candidate set to obtain a valid candidate set. If the real-time requirement level is hard real-time, the time deterministic parameter of each entry in the valid candidate set is traversed, and the registered cryptographic algorithm with the smallest time deterministic parameter is selected from the valid candidate set as the target cryptographic algorithm. If the real-time requirement level is soft real-time, the time deterministic parameter and security strength parameter of each entry in the valid candidate set are traversed, and the target cryptographic algorithm is obtained from the registered cryptographic algorithms based on the time deterministic parameter and security strength parameter. If the real-time requirement level is non-real-time, the security strength parameter of each entry in the valid candidate set is traversed, and the registered cryptographic algorithm with the largest security strength parameter is selected as the target cryptographic algorithm.
[0136] In one embodiment, the optimization module 405 is further configured to read the maximum value of the execution time recorded in the offline test of the target cryptographic algorithm from the algorithm information structure array, as the worst-case execution time; add the worst-case execution time to the preset protection margin to obtain the estimated execution time; call the budget request function provided by the RTOS scheduler to send a budget request message containing the task identifier of the application task and the estimated execution time to the RTOS scheduler; after receiving the budget request message, the RTOS scheduler reads the deadline field and the executed time field of the application task from the task control block of the application task according to the task identifier, calculates whether the sum of the executed time and the estimated execution time is greater than the deadline; if the sum of the estimated execution times is less than or equal to the deadline, the approval status field in the budget request message is set to the approved status; if the sum of the estimated execution times is greater than the deadline, the approval status field is set to the rejected status, and the budget request message after setting is returned to the cryptographic service management task.
[0137] In one embodiment, the optimization module 405 is further configured to, if the approval status field in the budget request message returned by the RTOS scheduler is in the rejection state, remove the target cryptographic algorithm from the valid candidate set and determine whether the valid candidate set is empty after removing the target cryptographic algorithm; if the valid candidate set is not empty, return to the step of re-determining the target cryptographic algorithm from the valid candidate set until the valid candidate set is empty or the approval result of the execution time budget request is obtained; if the valid candidate set is empty after removal, split the data to be encrypted into multiple data blocks according to the preset data block size, and repeat the steps of determining the target cryptographic algorithm and sending the budget request message for each data block, so that each data block is executed independently after obtaining the approval of the RTOS scheduler.
[0138] The modules in the aforementioned lightweight cryptographic algorithm integration system for RTOS can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware or independent of the processor in a computer device, or stored in software within the computer device's memory, allowing the processor to invoke and execute the corresponding operations of each module.
[0139] In one embodiment, a computer device is provided, the internal structure of which can be shown as follows: Figure 5 As shown, this computer device includes a processor, memory, input / output interfaces (I / O), and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides the environment for the operating system and computer programs stored in the non-volatile storage media. The database stores time-deterministic parameter data. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communicating with external terminals via a network connection. When executed by the processor, the computer program implements a lightweight cryptographic algorithm integration method for an RTOS.
[0140] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0141] In one embodiment, a computer device is also provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps in the above method embodiments.
[0142] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon that, when executed by a processor, implements the steps in the above method embodiments.
[0143] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps in the above method embodiments.
[0144] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.
[0145] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0146] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0147] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A method for integrating lightweight cryptographic algorithms for RTOS, characterized in that, The method includes: Obtain the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform; Register the service interfaces of each of the cryptographic algorithms to be integrated to the RTOS kernel of the target RTOS platform, and store the time deterministic parameters in association with the service interfaces; In response to a cryptographic service request from an application task, obtain the real-time requirement level of the application task; According to the real-time requirement level, the target cryptographic algorithm is obtained from the registered cryptographic algorithms; wherein, if the real-time requirement level is hard real-time, the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameters of the registered cryptographic algorithms. The execution time budget request for the target cryptographic algorithm is sent to the RTOS scheduler of the target RTOS platform. If the execution time budget request is approved, the service interface of the target cryptographic algorithm is invoked to perform cryptographic services. If the execution time budget request is not approved, the target cryptographic algorithm is re-determined.
2. The method according to claim 1, characterized in that, The acquisition of the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform includes: A benchmark task is created on the target RTOS platform; the priority of the benchmark task is lower than that of all application tasks to avoid interference with the scheduling of normal tasks during the benchmark process. Through the benchmark test task, for each of the cryptographic algorithms to be integrated, under the conditions of fixed input data length and fixed key, the cryptographic algorithm to be integrated is run N times continuously. Before each run, a cache clearing instruction and a pipeline flushing instruction are executed to eliminate the cache influence of the previous run on the subsequent run, so that the execution environment of each run is independent of each other. Using the high-precision hardware timer on the target RTOS platform, a first count value is read at the start time of each run of the cryptographic algorithm to be integrated, and a second count value is read at the end time. The difference between the two count values is taken as the execution time of the run, and a sample set containing N execution time values is obtained. The difference between the maximum and minimum values of the N execution time values in the sample set is calculated and used as the time deterministic parameter.
3. The method according to claim 1, characterized in that, The step of registering the service interfaces of each of the cryptographic algorithms to be integrated to the RTOS kernel of the target RTOS platform, and storing the time deterministic parameters associated with the service interfaces, includes: Define a unified service interface function signature for each of the cryptographic algorithms to be integrated. The unified service interface function signature includes at least an initialization interface, an encryption interface, and a decryption interface, so that the interfaces of different cryptographic algorithms to be integrated are consistent in function name, number of parameters, parameter type, and return value type. The upper-layer application calls different algorithms through the same set of interface names. For each of the cryptographic algorithms to be integrated, a corresponding encapsulation function is written according to the unified service interface function signature to encapsulate the original heterogeneous calling methods of each of the cryptographic algorithms to be integrated into the same calling method. Allocate a contiguous storage space in the read-only data segment of the RTOS kernel, and create an array of algorithm information structures in the storage space. The fields of the array of algorithm information structures include algorithm identifier, time determinism parameter, security strength parameter, initialization interface function pointer, encryption interface function pointer and decryption interface function pointer. According to the preset enumeration order, the algorithm information of each cryptographic algorithm to be integrated is written into each entry of the algorithm information structure array, so that each entry completely stores all the registration information of the cryptographic algorithm to be integrated, and an entry containing a null pointer is written at the end of the algorithm information structure array as a traversal termination mark.
4. The method according to claim 3, characterized in that, When the application task initiates a password service request, the application task calls the message queue sending function of the target RTOS platform to send a password service request message containing the application task's task identifier to the message queue of the password service management task. The process of obtaining the real-time requirement level of the application task includes: After reading the password service request message from the message queue, the task control block query function provided by the RTOS kernel is called to obtain the task control block pointer of the application task with the task identifier as the parameter. Read the value of the priority field of the application task from the storage area pointed to by the task control block pointer. The value of the priority field is written by the RTOS kernel according to the parameters passed to the task creation function when the task is created, and the value range is from 0 to the maximum priority number configured by the system minus 1. The value of the priority field is compared with a preset first threshold and a second threshold, wherein the first threshold is greater than the second threshold. If the priority value is greater than or equal to the first threshold, the application task is determined to be at the hard real-time level. If the value of the priority field is less than the second threshold, the application task is determined to be at the non-real-time level.
5. The method according to claim 4, characterized in that, The step of obtaining the target cryptographic algorithm from the registered cryptographic algorithms according to the real-time requirement level includes: Traverse each entry in the algorithm information structure array, read the security strength parameter in each entry, and remove registered cryptographic algorithms whose security strength parameter is lower than a preset security threshold from the candidate set to obtain a valid candidate set; If the real-time requirement level is hard real-time, then the time deterministic parameter of each entry in the valid candidate set is traversed, and the registered cryptographic algorithm with the smallest time deterministic parameter is selected from the valid candidate set as the target cryptographic algorithm. If the real-time requirement level is soft real-time, then the time deterministic parameter and security strength parameter of each entry in the valid candidate set are traversed, and the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameter and security strength parameter. If the real-time requirement level is non-real-time, then the security strength parameter of each entry in the valid candidate set is traversed, and the registered cryptographic algorithm with the largest security strength parameter is selected as the target cryptographic algorithm.
6. The method according to claim 5, characterized in that, Sending the execution time budget request for the target cryptographic algorithm to the RTOS scheduler of the target RTOS platform includes: The maximum value of the execution time recorded in the offline test of the target cryptographic algorithm is read from the algorithm information structure array and used as the worst-case execution time; The worst-case execution time is added to the preset protection margin to obtain the estimated execution time; Call the budget request function provided by the RTOS scheduler to send a budget request message containing the task identifier of the application task and the estimated execution time to the RTOS scheduler; Upon receiving the budget request message, the RTOS scheduler reads the deadline and execution time fields of the application task from the task control block of the application task based on the task identifier, calculates whether the sum of the execution time and the estimated execution time is greater than the deadline; if the sum of the estimated execution times is less than or equal to the deadline, the approval status field in the budget request message is set to approved; if the sum of the estimated execution times is greater than the deadline, the approval status field is set to rejected, and the budget request message with the set status is returned to the cryptographic service management task.
7. The method according to claim 6, characterized in that, If the execution time budget request is not approved, the target cryptographic algorithm will be redefined, including: If the approval status field in the budget request message returned by the RTOS scheduler is in the rejected state, the target cryptographic algorithm is removed from the valid candidate set, and it is determined whether the valid candidate set is empty after the target cryptographic algorithm is removed. If the set of valid candidates is not empty, then return to the step of re-determining the target cryptographic algorithm from the set of valid candidates until the set of valid candidates is empty or the execution time budget request is approved; If the set of valid candidates is empty after elimination, the data to be encrypted is split into multiple data blocks according to the preset data block size. The steps of determining the target cryptographic algorithm and sending the budget request message are repeated for each data block, so that each data block can be executed independently after obtaining approval from the RTOS scheduler.
8. A lightweight cryptographic algorithm integration system for RTOS, characterized in that, The system includes: The parameter acquisition module is used to acquire the time deterministic parameters of each cryptographic algorithm to be integrated on the target RTOS platform; An associated storage module is used to register the service interfaces of each of the cryptographic algorithms to be integrated to the RTOS kernel of the target RTOS platform, and to associate the time deterministic parameters with the service interfaces for storage. The response level acquisition module is used to acquire the real-time requirement level of the application task in response to the password service request of the application task. The target algorithm determination module is used to obtain a target cryptographic algorithm from the registered cryptographic algorithms according to the real-time requirement level; wherein, if the real-time requirement level is a hard real-time level, the target cryptographic algorithm is obtained from the registered cryptographic algorithms according to the time deterministic parameters of the registered cryptographic algorithms. The optimization module is used to send the execution time budget request of the target cryptographic algorithm to the RTOS scheduler of the target RTOS platform. If the execution time budget request is approved, the module calls the service interface of the target cryptographic algorithm to perform cryptographic services. If the execution time budget request is not approved, the module redetermines the target cryptographic algorithm.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 7.
