A message authentication code generation and verification method and system
By pre-compiling intermediate state value pairs through the HMAC key management system and combining iterative compression operations of prefix differences, the security and performance bottlenecks of HMAC keys in cloud computing systems are solved, achieving high-security and high-performance message authentication code generation and verification.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- UNIV OF SCI & TECH OF CHINA
- Filing Date
- 2026-02-26
- Publication Date
- 2026-04-28
AI Technical Summary
Existing HMAC keys in cloud computing systems pose security risks due to key-resident memory and hardware performance bottlenecks, making it difficult to achieve high-security and high-performance message authentication code generation and verification in a microservice architecture.
The HMAC key management system pre-computes intermediate state value pairs. The HMAC generator and verifier perform iterative compression operations based on prefix differences to generate and verify message authentication codes, ensuring that the key does not end up in the network and reducing network dependence.
It achieves end-to-end non-local protection of HMAC keys, improves the performance of message authentication code calculation and the flexibility of access control, reduces the risk of memory leaks and network latency, and supports cross-level legitimacy verification.
Smart Images

Figure CN121727754B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of network security and cloud computing technology, and in particular to a method and system for generating and verifying message authentication codes. Background Technology
[0002] HMAC (Hash-based Message Authentication Code) is a message authentication mechanism based on a cryptographic hash function and a shared key. It is widely used to ensure message integrity and verify that the message indeed originates from a party holding the same shared key. The Internet Engineering Task Force (IETF) has standardized the HMAC algorithm in RFC 2104 (the official specification document for the HMAC algorithm), and its calculation process is as follows:
[0003] ;
[0004] Where H represents the underlying cryptographic hash function; K represents the shared key; text represents the message to be authenticated; opad (Outer Padding) and ipad (Inner Padding) are fixed constant strings defined by RFC 2104, referred to as outer padding constant and inner padding constant, respectively. This represents a bitwise XOR operation; This indicates sequence concatenation.
[0005] Current mainstream cryptographic hash algorithms are mainly divided into two types of structures:
[0006] (1) Merkle-Damgård (MD) structure: This is the most widely used structure. It is an iterative construction method that extends the compression function into a complete cryptographic hash function. Typical algorithms include SM3 and the Secure Hash Algorithm (SHA) series, such as SHA-256, SHA-384, and SHA-512 (SHA is an abbreviation for the algorithm's English name, and the following value is the bit length of the message digest output by the algorithm). Its core working principle is to pad the input message and then divide it into message blocks of fixed length. Then, the compression function (CF) is used to process each message block one by one and perform iterative calculations: each round of operation takes the state output by the previous round and the current message block as input and output the updated state. The output of the final round is the message digest (also known as the hash value). For example, for the SM3 algorithm, the length of the message block is 512 bits and the length of the state is 256 bits.
[0007] (2) Sponge Construction: This is the structure used in next-generation cryptographic hash algorithms, with the SHA-3 series being a typical example. In the "Absorb" phase, message blocks are injected into the internal state. During the absorption phase, the internal state is gradually updated based on each message block using a permutation function. Finally, in the "Squeeze" phase, the internal state is used to generate the output message digest. For example, in the SHA-3-512 algorithm, the message block length is 576 bits, and the state length is 1600 bits.
[0008] In the microservice architecture of cloud computing systems, service API (Application Programming Interface) calls typically employ HMAC message authentication mechanisms (such as AWS Signature V4 authentication protocols) to verify identity and prevent tampering. This process generally includes the following two stages:
[0009] (1) Key Derivation Stage: To limit the risk of key exposure, the system usually does not directly use the long-term key (Secret Access Key). Instead, it generates a dedicated "Derived Key" through multiple rounds of recursive HMAC operations based on scope information such as date, region, and service name. For example: Derived Key = HMAC(HMAC(HMAC("AWS4"||SK, Date), Region), Service, "aws4_request"); where AWS4 is a fixed string prefix, SK (Secret Key) is the secret key, "AWS4"||SK forms the initial key, and "aws4_request" is the terminator of the multiple rounds of key derivation.
[0010] (2) Message authentication code generation stage: Use the Derived Key generated above to perform HMAC operation on the specific request content (e.g., Canonical Request) to obtain the final message authentication code.
[0011] However, existing solutions have significant shortcomings in balancing the requirements of high security and high performance:
[0012] (1) Security risks of key residing in memory: Traditional software implementations require the business server (HMAC generator and HMAC verifier) to hold the plaintext Secret Key (SK) or its derived Derived Key in memory so that message authentication code generation and verification operations can be performed on the content of high-concurrency requests at any time. Once the business server is subjected to network intrusion (for example, software vulnerabilities leading to memory information leakage or dumping, key configuration file leakage, or being attacked by side channels), the attacker can obtain the key and then forge any legitimate request.
[0013] (2) Performance bottleneck of hardware solutions: Although hardware security modules (HSM) or key management services (KMS) can be used to store keys and perform HMAC operations to ensure that keys do not reside in the memory of business servers, in the high-concurrency scenario of microservice architecture, every API call needs to access HSM or KMS through the network. This not only brings huge network latency, but also causes performance bottlenecks due to the I / O throughput limitations of HSM or KMS, and the hardware deployment cost is high.
[0014] Therefore, the industry urgently needs a message authentication code generation and verification technology solution that can ensure that the HMAC key is "not stored in the memory" throughout the entire process (does not appear in the memory of the business server), eliminate the real-time dependence on external hardware devices, and meet the high-performance and low-latency computing requirements of microservice architecture.
[0015] In view of this, the present invention is hereby proposed. Summary of the Invention
[0016] The purpose of this invention is to provide a method and system for generating and verifying message authentication codes, which can achieve a balance between security and performance.
[0017] The objective of this invention is achieved through the following technical solution:
[0018] A method for generating and verifying message authentication codes, comprising:
[0019] The HMAC key management system uses a cryptographic hash algorithm to generate a first intermediate state value pair by combining the HMAC key with the prefix of the HMAC generator and transmits it to the HMAC generator; and uses a cryptographic hash algorithm to generate a second intermediate state value pair by combining the stored HMAC key with the prefix of the HMAC verifier and transmits it to the HMAC verifier; wherein, HMAC is a message authentication code based on cryptographic hash.
[0020] The HMAC generator determines the initial state of the iterative compression operation based on the first intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier. It then combines the input sequence constructed based on the message content to be authenticated and performs the iterative compression operation of the cryptographic hash algorithm to obtain the first message authentication code. The first message authentication code and the message content to be authenticated are then transmitted to the HMAC verifier.
[0021] The HMAC verifier determines the initial state of its iterative compression operation based on the second intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier. It then combines the input sequence constructed based on the message content to be authenticated and performs iterative compression operation of the cryptographic hash algorithm to obtain the second message authentication code. Based on whether the second message authentication code matches the first message authentication code, the verification result is output.
[0022] A message authentication code generation and verification system, used to implement the aforementioned method, includes: an HMAC key management system, an HMAC generator, and an HMAC verifier; wherein:
[0023] HMAC Key Management System: Based on a cryptographic hash algorithm, it generates a first intermediate state value pair by combining the HMAC key with the prefix of the HMAC generator and transmits it to the HMAC generator; and based on a cryptographic hash algorithm, it generates a second intermediate state value pair by combining the stored HMAC key with the prefix of the HMAC verifier and transmits it to the HMAC verifier; wherein, HMAC is a message authentication code based on a cryptographic hash function.
[0024] HMAC Generator: Based on the first intermediate state value pair and the difference between the prefix of the HMAC Generator and the prefix of the HMAC Verifier, determine the initial state of the iterative compression operation of the HMAC Generator, combine it with the input sequence constructed based on the message content to be authenticated, perform iterative compression operation of the cryptographic hash algorithm to obtain the first message authentication code, and transmit the first message authentication code and the message content to be authenticated to the HMAC Verifier.
[0025] HMAC verifier: Based on the second intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, the initial state of the HMAC verifier's iterative compression operation is determined. Combined with the input sequence constructed based on the message content to be authenticated, the iterative compression operation of the cryptographic hash algorithm is performed to obtain the second message authentication code. The verification result is output according to whether the second message authentication code is consistent with the first message authentication code.
[0026] As can be seen from the technical solution provided by the present invention, the HMAC algorithm is decomposed into the following steps: the HMAC key management system uses the HMAC key to pre-calculate intermediate state value pairs that do not contain plaintext key information; the HMAC generator uses the received intermediate state value pairs and, based on prefix differences, performs iterative compression operations of the cryptographic hash algorithm on only the message content to be authenticated (such as API request messages) to efficiently synthesize the final message authentication code; the HMAC verifier uses the received intermediate state value pairs and, based on prefix differences, calculates the message authentication code using similar operational logic, and verifies the message authentication code sent by the HMAC generator, thereby achieving cross-level legitimacy verification and realizing "non-landing" protection of the HMAC key throughout the entire process, significantly improving the performance of message authentication code calculation and the flexibility of access control. Attached Figure Description
[0027] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0028] Figure 1 This is a flowchart of a message authentication code generation and verification method provided in an embodiment of the present invention.
[0029] Figure 2 This is a timing diagram showing the interaction between the components in the message authentication code generation and verification method provided in the embodiments of the present invention.
[0030] Figure 3 This is a flowchart illustrating the generation of intermediate state value pairs in the HMAC key management system provided in this embodiment of the invention.
[0031] Figure 4 This is a flowchart illustrating the process of generating the first message authentication code for the HMAC generator, as provided in an embodiment of the present invention.
[0032] Figure 5 This is a schematic diagram of a message authentication code generation and verification system provided in an embodiment of the present invention. Detailed Implementation
[0033] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the protection scope of the present invention.
[0034] First, the following explanations are provided for the terms that may be used in this article:
[0035] The term "and / or" means that either or both can be achieved simultaneously. For example, X and / or Y means that it includes both "X" or "Y" as well as the three cases of "X and Y".
[0036] The terms "comprising," "including," "containing," "having," or other similar semantic descriptions should be interpreted as non-exclusive inclusion. For example, including a technical feature element (such as raw material, component, ingredient, carrier, dosage form, material, size, part, component, mechanism, device, step, process, method, reaction conditions, processing conditions, parameter, algorithm, signal, data, product or article of manufacture, etc.) should be interpreted as including not only the expressly listed technical feature element, but also other technical feature elements that are not expressly listed and are well-known in the art.
[0037] The following is a detailed description of a message authentication code generation and verification method and system provided by the present invention. Contents not described in detail in the embodiments of the present invention are prior art known to those skilled in the art. Where specific conditions are not specified in the embodiments of the present invention, conventional conditions in the art or conditions recommended by the manufacturer shall apply. Reagents or instruments used in the embodiments of the present invention whose manufacturers are not specified are all conventional products that can be purchased commercially.
[0038] Example 1
[0039] This invention provides a method for generating and verifying message authentication codes, such as... Figure 1 As shown, it mainly includes the following steps:
[0040] Step S1: The HMAC key management system generates intermediate state value pairs.
[0041] In this step: the HMAC key management system generates a first intermediate state value pair based on a cryptographic hash algorithm, combining the HMAC key with the prefix of the HMAC generator, and transmits it to the HMAC generator; and generates a second intermediate state value pair based on a cryptographic hash algorithm, combining the stored HMAC key with the prefix of the HMAC verifier, and transmits it to the HMAC verifier.
[0042] In this embodiment of the invention, the first intermediate state value pair is an intermediate state value pair used to generate a message authentication code, denoted as... The second intermediate state value pair is an intermediate state value pair used to verify the message authentication code, denoted as... Where U is the first intermediate state value. This is the second intermediate state value. This is the third intermediate state value.
[0043] The three intermediate state values are generated in the following ways:
[0044] (1) The first intermediate state value U is generated based on the stored HMAC key: the preprocessed key is calculated based on the cryptographic hash algorithm to obtain the HMAC key K. Using the initial value IV defined by the cryptographic hash algorithm as the state, and the preprocessed key... The XOR value of the input sequence with the external padding constant opad defined by the HMAC algorithm is used to perform the compression operation of the cryptographic hash algorithm (that is, it only needs to be run once), and the result of the operation is the first intermediate state value U.
[0045] This process can be represented as:
[0046] ;
[0047] in, This represents the compression function of a cryptographic hash algorithm. This represents the XOR operation.
[0048] (2) The generation process of the second intermediate state value and the third intermediate state value is as follows: using the preprocessed key The XOR value of the value with the internal padding constant ipad defined by the HMAC algorithm is used as the starting sequence; for the prefix of the HMAC generator... prefix of HMAC validator Each scope parameter (ScopeParameter) within the algorithm is independently padded with block boundaries to ensure its length is an integer multiple of the block length of the cryptographic hash algorithm. This yields the processed prefixes for the HMAC generator and the HMAC verifier. These prefixes are then concatenated with the initial sequence to obtain two concatenated sequences: the first concatenated sequence for the HMAC generator and the second concatenated sequence for the HMAC verifier. Using the initial value IV defined by the cryptographic hash algorithm as the state, and the first and second concatenated sequences as input sequences, the iterative compression operation of the cryptographic hash algorithm is performed on each sequence. The corresponding results are the second intermediate state value. With the third intermediate state value .
[0049] The above process can be uniformly represented as:
[0050] ;
[0051] in, This represents the iterative compression operation of a cryptographic hash algorithm (i.e., dividing the input sequence into blocks and then performing the operation multiple times). Each time, one block is processed and its state is updated. This indicates sequence concatenation. For the HMAC generator, in the above formula... for , for For the HMAC validator, in the above formula... for , for .
[0052] In this embodiment of the invention, the block boundary alignment padding process includes: padding the end of the scope parameter with a bit sequence generated by a preset rule (for example, padding with several zero bits), so that the total length of the padded sequence is an integer multiple of the block length of the cryptographic hash algorithm.
[0053] In this embodiment of the invention, the prefix of the HMAC generator and the prefix of the HMAC verifier are each composed of zero or one or more ordered scope parameters.
[0054] Step S2: The HMAC generator generates the first message authentication code.
[0055] In this step: The HMAC generator determines the initial state of the iterative compression operation based on the first intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier. Combined with the input sequence constructed based on the message content to be authenticated, the iterative compression operation of the cryptographic hash algorithm is performed to obtain the first message authentication code. The first message authentication code and the message content to be authenticated are then transmitted to the HMAC verifier.
[0056] The preferred implementation method for this step is as follows:
[0057] (1) Based on the second intermediate state value Based on the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, the first initial state of the iterative compression operation of the HMAC generator is determined. The first input sequence, constructed based on the message content to be authenticated, is combined with an iterative compression operation of a cryptographic hash algorithm. The output state is then used as the internal hash value of the HMAC generator. .
[0058] In this embodiment of the invention, based on the second intermediate state value Based on the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, the first initial state of the iterative compression operation of the HMAC generator is determined. include:
[0059] If the prefix of the HMAC generator prefix of HMAC validator Same, or the prefix of the HMAC validator. It is the prefix of the HMAC generator. A proper prefix, that is, one that satisfies the relation: , The first initial state of the HMAC generator iterative compression operation is the difference between the two prefixes. ;
[0060] If the prefix of the HMAC generator It is the prefix of the HMAC validator. A proper prefix, that is, one that satisfies the relation: Then, regarding the differences Each scope parameter contained therein undergoes independent block boundary alignment and padding (in the same way as in step S1 above), ensuring that the length of each scope parameter is an integer multiple of the block length of the cryptographic hash algorithm, thus obtaining the processed difference sequence. Combined with the second intermediate state value Perform iterative compression operations using a cryptographic hash algorithm, and output the resulting state as the first initial state. This process can be represented as:
[0061] .
[0062] In this embodiment of the invention, an iterative compression operation of a cryptographic hash algorithm is performed on a first input sequence constructed based on the message content to be authenticated, and the output state is used as the internal hash value of the HMAC generator. , is represented as:
[0063] ;
[0064] in, This indicates the message content to be identified. This indicates that padding data is generated according to the padding rules defined by the cryptographic hash algorithm.
[0065] (2) The first intermediate state value U is used as the second initial state of the HMAC generator's iterative compression operation, combined with the internal hash value generated by the HMAC generator. The constructed second input sequence is subjected to iterative compression operations using a cryptographic hash algorithm, and the output state is used as the first message authentication code.
[0066] The above process can be represented as follows:
[0067] ;
[0068] HMAC is based on a cryptographic hash algorithm. The message authentication code is the first message authentication code generated by the HMAC generator.
[0069] Preferably, the present invention can directly construct the input sequence using the message to be authenticated; alternatively, it can concatenate the authorization code, which is pre-shared between the HMAC generator and the HMAC verifier and is unknown to the HMAC key management system, with the message to be authenticated in a preset order to form an enhanced message sequence, and then use the enhanced message sequence to construct the input sequence. If the latter is used, then the above-mentioned method... It will be replaced with an enhanced message sequence.
[0070] In this embodiment of the invention, the message to be identified can be an API call request message constructed by the HMAC generator according to its own API call requirements.
[0071] Step S3: The HMAC verifier verifies the first message authentication code.
[0072] In this step: the HMAC verifier determines the initial state of the iterative compression operation based on the second intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier. Combined with the input sequence constructed based on the message content to be authenticated, the iterative compression operation of the cryptographic hash algorithm is performed to obtain the second message authentication code. The verification result is output based on whether the second message authentication code is consistent with the first message authentication code.
[0073] Those skilled in the art will understand that the first and second messages primarily serve as identifiers to distinguish between the two message authentication codes. Specifically, the first message authentication code represents the message authentication code generated by the HMAC generator, while the second message authentication code represents the message authentication code generated by the HMAC verifier.
[0074] The overall operational logic of this step is similar to that of step S2 mentioned above. Specifically, it is based on the third intermediate state value. The first initial state of the iterative compression operation of the HMAC verifier is determined by using the operational logic corresponding to that of the HMAC generator. The internal hash value of the HMAC verifier is calculated; and the second message authentication code is calculated by using the first intermediate state value U as the second initial state of the HMAC verifier's iterative compression operation, adopting the operation logic corresponding to that of the HMAC generator.
[0075] Specifically: HMAC verifiers based on and The inclusion relationship determines the first initial state of the HMAC verification iterative compression operation. (a) If and The contents are completely equal, or yes The true prefix, HMAC verifier directly sets (b) If yes A proper prefix (i.e., the generator has restricted permissions) satisfies the relation. (in (Indicating prefix difference sequences), the HMAC validator needs to target... Each scope parameter contained therein undergoes the same block boundary alignment and padding process as step S1 independently, resulting in the padded prefix difference sequence (denoted as...). ); then with As a state, the padded prefix difference sequence As the input sequence, perform iterative compression operations using a cryptographic hash algorithm, and use the output state as the initial state. The process is represented as follows:
[0076] .
[0077] The subsequent calculation of the hash value and the method of constructing the corresponding input sequence based on the hash value are the same as those in step S2 above, so they will not be described in detail.
[0078] Finally, the HMAC verifier obtains the second message authentication code by performing iterative compression operations of the cryptographic hash algorithm; the second message authentication code is compared with the first message authentication code, and if they match, the verification is successful.
[0079] Compared with the prior art, the above-mentioned solution provided by the embodiments of the present invention has the following main advantages:
[0080] 1. High Security: The HMAC key is physically isolated within the security boundary of the HMAC key management system. The HMAC generator and HMAC verifier only hold intermediate state value pairs without plaintext key information. Even if a memory leak occurs on the server, the attacker can only obtain the intermediate state under a specific prefix and cannot reverse-engineer the key. Furthermore, this state is strictly limited to a specific range defined by the scope parameter (e.g., valid only at a specific time or in a specific region), greatly reducing the risk radiating after the intermediate state is leaked.
[0081] 2. Flexible Hierarchical Access Control: This invention innovatively employs a block boundary alignment and padding technique based on scope parameters to construct an intermediate state with cascadable characteristics, thereby supporting inclusion relationship verification based on the true prefix. The party holding the shorter message prefix (i.e., a wider permission range), whether it's the generator or the verifier, only needs to perform the same block boundary alignment and padding and iterative compression operations on the scope parameters of the differing parts to dynamically update its local intermediate state to adapt to the party holding the longer message prefix (i.e., a narrower permission range). This makes cross-level, cross-service microservice API call authentication possible, eliminating the need to distribute independent keys for each sub-service and significantly reducing the complexity of key management.
[0082] 3. High Performance and High Availability: The HMAC generator utilizes pre-computed intermediate state value pairs, eliminating the complex key derivation process frequently performed in traditional schemes and simplifying it to a low-overhead intermediate state copy. When generating the message authentication code, only the padding and iterative compression operations defined by the cryptographic hash algorithm need to be performed on the message content to be authenticated. There is no need to access HSM or KMS over the network, nor is it necessary to repeatedly calculate the hash value of the prefix. This eliminates the network latency and single point of failure risks caused by external dependencies, and can meet the extremely high concurrency API call requirements of microservice architectures.
[0083] To more clearly demonstrate the technical solution and its effects provided by the present invention, the method provided by the embodiments of the present invention will be described in detail below with reference to specific examples.
[0084] I. Overall Overview of the Plan
[0085] The method provided in this invention is a message authentication code generation and verification scheme based on HMAC intermediate state pre-computation, utilizing the HMAC algorithm. The iterative structure of the HMAC key management system can be decomposed into two stages: 1. Intermediate State Calculation Stage: The HMAC key management system holds the HMAC key and pre-calculates intermediate state value pairs (U, V) that do not contain plaintext key information but solidify the key state. The calculation of V may or may not include a specific message prefix (such as date or region) that has undergone block boundary alignment and padding to support the transmission of the intermediate state. 2. Message Authentication Code Calculation Stage: The HMAC generator receives U and V as the initial state and uses the message content to be authenticated as the input sequence. It performs iterative compression operations using a cryptographic hash algorithm to calculate the final message authentication code. The HMAC verifier not only supports message authentication code verification for cases with the same prefix, but also, based on the property prefix inclusion relationship, allows the party holding the shorter prefix to update its local intermediate state by performing the same padding and iterative compression operations on the scope parameters of the differing parts, thereby achieving cross-level legitimacy verification. This invention supports flexible message prefix configuration, and the HMAC key never leaves the security boundary of the key management system throughout the entire scheme, realizing the non-local protection of the HMAC key throughout the process, significantly improving the performance of message authentication code calculation and the flexibility of access control, and achieving a balance between security and performance.
[0086] The present invention involves three entities: an HMAC key management system, an HMAC generator, and an HMAC verifier. The overall process includes: the HMAC key management system calculating intermediate state value pairs based on the HMAC key; the HMAC generator calculating the first message authentication code based on the intermediate state value pairs; and the HMAC verifier verifying the first message authentication code based on the intermediate state value pairs.
[0087] The HMAC key management system stores a root key; the HMAC key is calculated by the HMAC key management system based on the root key, combined with a set of ordered derivation parameters, using a preset key derivation algorithm.
[0088] The preset key derivation algorithm can adopt a recursive HMAC operation method, and the main steps include: (a) initialization: using the root key as the initial derivation key; (b) recursive calculation: performing HMAC operation on each derivation parameter in turn according to the order of the derivation parameters; in each round of operation, the result of the previous round of operation is used as the key of the current round of HMAC operation, and the derivation parameter corresponding to the current round is used as the message input of the current round of HMAC operation; (c) output: using the output result of the last round of HMAC operation as the HMAC key.
[0089] In this embodiment of the invention, the cryptographic hash algorithm can be selected from SM3, SHA-256, SHA-384, or SHA-512, etc.; for example, in this embodiment, the cryptographic hash algorithm can be selected from SM3 (block length 64 bytes). Logically, a complete piece of data to be authenticated includes: a message prefix and a message content. The message prefix is optional, and when present, it consists of one or more scope parameters in an ordered manner. Scope parameters refer to discrete information units used to limit the scope of the key, such as date, region identifier, service name, etc. For the prefix "20251208 / cn-north-1 / s3", it contains three scope parameters: parameter 1 (date) "20251208", parameter 2 (region identifier) "cn-north-1", and parameter 3 (service name) "s3". The message content corresponds to the actual API request. For example, " / order / create?id=123" means that the client requests to create an order on the server and specifies that the order ID is 123.
[0090] II. Overview of the overall process of the plan.
[0091] like Figure 2 The diagram shows the interaction timing between the components in the present invention.
[0092] 1. Initialization and intermediate state value pair generation.
[0093] Before business operations begin or during the first invocation, the HMAC generator and HMAC verifier each request and obtain their respective intermediate state value pairs from the HMAC key management system. Assuming this is the case in this embodiment:
[0094] 1.1~1.2 The HMAC generator holds the prefix for specific business operations. (e.g., "20251208 / RegionA"), obtain the first intermediate state value pair from the HMAC key management system. .
[0095] 1.3~1.4, HMAC validators hold positions targeting broader prefixes. (e.g., "20251208"), obtain the second intermediate state value pair from the HMAC key management system. .
[0096] 2. The HMAC generator generates the first message authentication code.
[0097] 2.1 Determine the differences between the message content, its own prefix, and the prefix of the HMAC verifier.
[0098] 2.2 Determine your initial state based on the prefix differences between yourself and the HMAC verifier.
[0099] 2.3 Calculate the first message authentication code.
[0100] The HMAC generator appends the generated first message authentication code to the API request header and sends it along with the API request message to the HMAC verifier. The prefix of the HMAC generator... They can be sent together, or they can be omitted if the prefix difference between the HMAC generator and the verifier is known to the HMAC verifier.
[0101] 3. The HMAC verifier verifies the first message authentication code.
[0102] 3.1 Receive requests from the HMAC generator.
[0103] 3.2 The HMAC verifier uses the second intermediate state value held locally as a basis. Perform verification. Assume the prefix of the HMAC verifier. ("20251208") is the prefix of the HMAC generator. The proper prefix of ("20251208 / RegionA") is defined. The verification logic includes the following handling of the proper prefix inclusion relationship:
[0104] (3.2.1) Difference identification: At this time, the difference between the two is the complete scope parameter "RegionA".
[0105] (3.2.2) Status Update: Since the HMAC key management system has already processed "20251208" in the aforementioned steps, However, "RegionA" has not yet been processed. This is to restore an intermediate state consistent with the generator (i.e.,...). The verifier needs to perform a state update on the difference portion "RegionA", that is, to perform block boundary alignment padding on "RegionA" (using the same rules as the previous steps, such as using bit zero padding to 64 bytes), and then... As a state, the scope parameter of the padded difference portion is used as the input sequence to perform an iterative compression operation of the cryptographic hash algorithm. The result of this operation is the updated intermediate state, which will serve as the first initial state for the HMAC verifier's iterative compression operation. This is equivalent to the HMAC generator's method. .
[0106] 3.3 Local Recalculation: The verifier uses the updated initial state. The second message authentication code is calculated using the same operational logic as the HMAC generator in generating the first message authentication code.
[0107] Note: If and Equal or yes If it is a proper prefix, then the verifier directly uses it. Calculate the message authentication code; the above state update steps are unnecessary.
[0108] 3.4 Response processing.
[0109] The HMAC authenticator compares the second message authentication code with the first message authentication code in the request header. If they match, the authentication passes, the business logic is executed, and the business response data is returned; otherwise, an error message indicating authentication failure is returned.
[0110] III. Detailed introduction of the plan.
[0111] This section provides a detailed introduction to the working processes of the HMAC key management system, the HMAC generator, and the HMAC verifier.
[0112] 1. The HMAC key management system generates intermediate state value pairs.
[0113] Upon receiving an intermediate state retrieval request, the HMAC key management system performs calculations within a secure environment. For example... Figure 3 As shown, the HMAC key management system calculates intermediate state values U and V (including those used for HMAC generation) through two independent paths. and used for verification ).
[0114] (1) Calculation scheme for the first intermediate state value U.
[0115] The HMAC key management system first preprocesses the HMAC key K. If the length of K exceeds the block size of the cryptographic hash algorithm (for example, the block size of the SM3 algorithm is 64 bytes), then the hash algorithm is used first. Calculate the preprocessed key of K. ;otherwise .like If the length is insufficient for the block length of the cryptographic hash algorithm, zero bits are added until the length equals the block length of the cryptographic hash algorithm.
[0116] Then, calculate The XOR value of the outer message block with the external padding constant opad constitutes the outer message block. Subsequently, the HMAC key management system uses the initial value IV defined by the cryptographic hash algorithm as the state, takes this outer message block as the input sequence, performs the compression operation of the cryptographic hash algorithm, and outputs the resulting state as the first intermediate state value U.
[0117] (2) Calculation scheme for intermediate state value V.
[0118] Intermediate state value V includes and The calculation process is the same for both. The HMAC key management system calculates the preprocessed key. The XOR value of this value with the internal padding constant `ipad` forms the first message block of the inner iterative compression operation, serving as the starting sequence. Next, the HMAC key management system constructs the aligned prefix sequence. The specific process is as follows: For each scope parameter contained in the message prefix (such as "20251208", "cn-north-1", etc.), block alignment padding is performed to make the length of each scope parameter pad to an integer multiple of the cryptographic hash algorithm block length (64 bytes).
[0119] The specific construction rules for the block boundary alignment padding include, but are not limited to, the following:
[0120] (a) Zero-Padding.
[0121] This method aims to extend the sequence length to an integer multiple of the block length of the cryptographic hash algorithm (e.g., 64 bytes). Specifically, it involves continuously padding the end of the original sequence with binary "0" bits until the total length after padding reaches a minimum block length that is greater than or equal to an integer multiple of the original sequence length. Here, the original sequence refers to the sequence of scope parameters before padding.
[0122] (b) Standard Padding.
[0123] This method follows the standard specifications of the selected cryptographic hash algorithm (such as SM3, SHA-256), and the padding data includes a field indicating the length of the original sequence.
[0124] Taking the SHA-256 algorithm as an example, the specific method includes the following steps in sequence:
[0125] Step b1: Append a binary bit "1" to the end of the original sequence.
[0126] Step b2: Then append k binary bits "0", where k is the smallest non-negative integer that satisfies the following modulo congruence: (L+1+k)=448 (mod 512), where mod is the modulo operation and L is the length of the original sequence.
[0127] Step b3: Append a fixed 64-bit length (i.e., block length) field to store the original sequence length in big-endian.
[0128] Even if the original sequence length already meets the grouping multiple alignment condition, the above steps still need to be performed completely.
[0129] The HMAC key management system concatenates the padded sequences of scope parameters in order to obtain the complete aligned prefix sequence. Finally, the HMAC key management system will process the first message packet ( ) and the aligned prefix sequence The concatenation process uses the initial values defined by the cryptographic hash algorithm as the initial state, and the concatenated complete sequence as the input sequence to perform iterative compression operations of the cryptographic hash algorithm.
[0130] This section processes the prefixes of the HMAC generator and the HMAC verifier separately. For the HMAC generator, the output state after iterative compression is: For the HMAC verifier, the output state after iterative compression is: Afterwards, the HMAC key management system will... , The data is then transmitted to the HMAC generator and the HMAC verifier.
[0131] 2. The HMAC generator generates the first message authentication code.
[0132] HMAC generator receives Then, the remaining part of the HMAC operation is completed in memory, such as... Figure 4 As shown.
[0133] The HMAC generator first determines its initial state for iterative compression operations. Assume the prefix of the HMAC generator in this case... If the prefix of the HMAC validator is "20251208 / RegionA", then... and Consistent, or It is already If it is a proper prefix, then directly let If the HMA generator holds a shorter prefix, such as "20251208", while the HMAC verifier holds a different prefix... If the value is "20251208 / RegionA", then the generator needs to first process the difference in "RegionA" (i.e., Perform the same block boundary alignment and padding as described above, then combine... And the sequence after padding the difference (i.e. Using the input sequence as input, perform iterative compression operations of the cryptographic hash algorithm, output the updated intermediate state, and use it as the first initial state. .
[0134] Subsequently, the HMAC generator combines the first initial state. It uses the input message content (the message content to be authenticated) MessageContent and the padding data generated according to the padding rules defined by the cryptographic hash algorithm. The concatenated result is used as the first input sequence, and the iterative compression operation of the cryptographic hash algorithm is performed. The output state is used as the internal hash value. At this point, the HMAC generator has essentially calculated: .
[0135] Preferably, if the HMAC generator and the HMAC verifier share an authorization code unknown to the HMAC key management system, then in this scenario, the HMAC generator concatenates the authorization code and the message content in a preset order to form an enhanced message sequence; in the above operation, MessageContent is replaced by the enhanced message sequence.
[0136] Finally, the HMAC generator uses the received U as the second initial state. It then uses the internal hash value obtained in the previous step... and the padding data generated according to the padding rules defined by the cryptographic hash algorithm. The concatenation result is used as the second input sequence. The iterative compression operation of the cryptographic hash algorithm is performed, and the output state is the first message authentication code.
[0137] 3. The HMAC verifier verifies the first message authentication code.
[0138] HMAC verifiers base their data on the intermediate state values they hold. Generate a second message authentication code. The technical process involved in this part is the same as that in the HMAC generator, so it will not be described in detail. Output the verification result by comparing whether the two message authentication codes are consistent.
[0139] Through the above description of the embodiments, those skilled in the art can clearly understand that the above embodiments can be implemented by software, or by using software plus necessary general-purpose hardware platforms. Based on this understanding, the technical solutions of the above embodiments can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, mobile hard drive, etc.), including several instructions to cause a computer device (such as a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0140] Example 2
[0141] This invention also provides a message authentication code generation and verification system, which is mainly used to implement the methods provided in the foregoing embodiments, such as... Figure 5 As shown, the system mainly includes: an HMAC key management system, an HMAC generator, and an HMAC verifier; wherein:
[0142] HMAC Key Management System: Based on a cryptographic hash algorithm, it generates a first intermediate state value pair by combining the HMAC key with the prefix of the HMAC generator and transmits it to the HMAC generator; and based on a cryptographic hash algorithm, it generates a second intermediate state value pair by combining the stored HMAC key with the prefix of the HMAC verifier and transmits it to the HMAC verifier; wherein, HMAC is a message authentication code based on a cryptographic hash algorithm.
[0143] HMAC Generator: Based on the first intermediate state value pair and the difference between the prefix of the HMAC Generator and the prefix of the HMAC Verifier, determine the initial state of the iterative compression operation of the HMAC Generator, combine it with the input sequence constructed based on the message content to be authenticated, perform iterative compression operation of the cryptographic hash algorithm to obtain the first message authentication code, and transmit the first message authentication code and the message content to be authenticated to the HMAC Verifier.
[0144] HMAC verifier: Based on the second intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, the initial state of the HMAC verifier's iterative compression operation is determined. Combined with the input sequence constructed based on the message content to be authenticated, the iterative compression operation of the cryptographic hash algorithm is performed to obtain the second message authentication code. The verification result is output according to whether the second message authentication code is consistent with the first message authentication code.
[0145] Since the main technical details of this system have been described in detail in previous embodiments, they will not be repeated here.
[0146] Those skilled in the art will understand that, for the sake of convenience and brevity, the above-described division of functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the system can be divided into different functional modules to complete all or part of the functions described above.
[0147] The above description is merely a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims. The information disclosed in the background section is intended only to enhance the understanding of the overall background technology of the present invention and should not be construed as an admission or implication in any way that such information constitutes prior art known to those skilled in the art.
Claims
1. A method for generating and verifying message authentication codes, characterized in that, include: The HMAC key management system is based on a cryptographic hash algorithm. It combines the HMAC key with the prefix of the HMAC generator to generate a first intermediate state value pair, which is then transmitted to the HMAC generator. And based on a cryptographic hash algorithm, a second intermediate state value pair is generated by combining the stored HMAC key with the prefix of the HMAC verifier, and then transmitted to the HMAC verifier; wherein, HMAC is a message authentication code based on a cryptographic hash; the first intermediate state value pair is an intermediate state value pair used to generate the message authentication code, denoted as . The second intermediate state value pair is an intermediate state value pair used to verify the message authentication code, denoted as... Where U is the first intermediate state value. This is the second intermediate state value. The third intermediate state value; the first intermediate state value U is generated based on the stored HMAC key: the preprocessed key is calculated based on the cryptographic hash algorithm to obtain the HMAC key K. Using the initial value IV defined by the cryptographic hash algorithm as the state, and the preprocessed key... The XOR value of the input sequence with the external padding constant opad defined in the HMAC algorithm is used to perform a compression operation of the cryptographic hash algorithm. The result of the operation is the first intermediate state value U. The generation process of the second and third intermediate state values is as follows: using the preprocessed key... The XOR value of the internal padding constant `ipad` defined by the HMAC algorithm is used as the starting sequence. For the prefixes of the HMAC generator and the HMAC verifier, block boundary alignment padding is performed independently on each scope parameter to ensure that the length of each scope parameter is an integer multiple of the block length of the cryptographic hash algorithm. This yields the processed prefixes of the HMAC generator and the HMAC verifier, which are then concatenated with the starting sequence to obtain two concatenated sequences. The concatenated sequence corresponding to the HMAC generator is called the first concatenated sequence, and the concatenated sequence corresponding to the HMAC verifier is called the second concatenated sequence. Using the initial value `IV` defined by the cryptographic hash algorithm as the state, and the first and second concatenated sequences as input sequences, the iterative compression operation of the cryptographic hash algorithm is performed on each. The corresponding operation result is the second intermediate state value. With the third intermediate state value ; The HMAC generator determines the initial state of the iterative compression operation based on the first intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier. It then combines the input sequence constructed based on the message content to be authenticated and performs the iterative compression operation of the cryptographic hash algorithm to obtain the first message authentication code. The first message authentication code and the message content to be authenticated are then transmitted to the HMAC verifier. The HMAC verifier determines the initial state of its iterative compression operation based on the second intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier. It then combines the input sequence constructed based on the message content to be authenticated and performs iterative compression operation of the cryptographic hash algorithm to obtain the second message authentication code. Based on whether the second message authentication code matches the first message authentication code, the verification result is output.
2. The message authentication code generation and verification method according to claim 1, characterized in that, The block boundary alignment padding process includes: padding the end of the scope parameter with a bit sequence generated by a preset rule, so that the total length of the padded sequence is an integer multiple of the block length of the cryptographic hash algorithm.
3. The message authentication code generation and verification method according to claim 1, characterized in that, The prefix of the HMAC generator and the prefix of the HMAC verifier are each composed of zero or one or more ordered scope parameters.
4. The message authentication code generation and verification method according to claim 1, characterized in that, The HMAC generator, based on the first intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, determines the initial state of the iterative compression operation of the HMAC generator. Combining this with the input sequence constructed based on the message content to be authenticated, it performs iterative compression operation of the cryptographic hash algorithm to obtain the first message authentication code, which includes: The first intermediate state value pair is Where U is the first intermediate state value. This is the second intermediate state value; Based on the second intermediate state value Based on the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, the first initial state of the iterative compression operation of the HMAC generator is determined. The first input sequence, constructed based on the message content to be authenticated, is combined with an iterative compression operation of a cryptographic hash algorithm. The output state is then used as the internal hash value of the HMAC generator. ; Using the first intermediate state value U as the second initial state for the iterative compression operation of the HMAC generator, combined with the internal hash value generated by the HMAC generator... The constructed second input sequence is subjected to iterative compression operations using a cryptographic hash algorithm, and the output state is used as the first message authentication code.
5. The message authentication code generation and verification method according to claim 4, characterized in that, The second intermediate state value Based on the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, the first initial state of the iterative compression operation of the HMAC generator is determined. include: If the prefix of the HMAC generator prefix of HMAC validator Same, or the prefix of the HMAC validator. It is the prefix of the HMAC generator. The proper prefix, i.e.: , The first initial state of the HMAC generator iterative compression operation is the difference between the two prefixes. ; If the prefix of the HMAC generator It is the prefix of the HMAC validator. The proper prefix, i.e.: , For the difference between two prefixes, then for the difference Each scope parameter within the algorithm is independently padded with block boundaries to ensure that the length of each scope parameter is an integer multiple of the block length of the cryptographic hash algorithm, thus obtaining the processed difference sequence. Combined with the second intermediate state value Perform iterative compression operations using a cryptographic hash algorithm, and output the resulting state as the first initial state. .
6. The message authentication code generation and verification method according to claim 4, characterized in that, Also includes: Fill data is generated according to the filling rules defined by the cryptographic hash algorithm. ; Based on the message content to be identified and the populated data Construct the first input sequence based on the internal hash value of the HMAC generator. With filling data Construct the second input sequence.
7. The message authentication code generation and verification method according to claim 4, characterized in that, The HMAC verifier, based on the second intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, determines the initial state of the iterative compression operation of the HMAC verifier. Combining this with the input sequence constructed based on the message content to be authenticated, it performs iterative compression operation of the cryptographic hash algorithm to obtain the second message authentication code, which includes: The second intermediate state value pair is Where U is the first intermediate state value. This is the third intermediate state value; Based on the third intermediate state value The first initial state of the iterative compression operation of the HMAC verifier is determined by using the operational logic corresponding to that of the HMAC generator. And calculate the internal hash value of the HMAC verifier; Furthermore, the first intermediate state value U is used as the second initial state for the HMAC verifier's iterative compression operation. The operation logic corresponding to that of the HMAC generator is adopted, and the second message authentication code is obtained through the operation.
8. A method for generating and verifying a message authentication code according to any one of claims 1 to 7, characterized in that, Constructing the input sequence based on the message content to be identified includes: The HMAC generator constructs an API call request message based on its own API call requirements and uses it as the message to be authenticated; where API stands for Application Programming Interface. The input sequence can be constructed directly using the message to be authenticated; or, the authorization code, which is shared in advance by the HMAC generator and the HMAC verifier and is unknown to the HMAC key management system, can be concatenated with the message to be authenticated in a preset order to form an enhanced message sequence, and then the input sequence can be constructed using the enhanced message sequence.
9. A message authentication code generation and verification system, characterized in that, The method for implementing any one of claims 1 to 8 comprises: an HMAC key management system, an HMAC generator, and an HMAC verifier; wherein: HMAC Key Management System: Based on a cryptographic hash algorithm, it generates a first intermediate state value pair by combining the HMAC key with the prefix of the HMAC generator and transmits it to the HMAC generator; and based on a cryptographic hash algorithm, it generates a second intermediate state value pair by combining the stored HMAC key with the prefix of the HMAC verifier and transmits it to the HMAC verifier; wherein, HMAC is a message authentication code based on a cryptographic hash function. HMAC Generator: Based on the first intermediate state value pair and the difference between the prefix of the HMAC Generator and the prefix of the HMAC Verifier, determine the initial state of the iterative compression operation of the HMAC Generator, combine it with the input sequence constructed based on the message content to be authenticated, perform iterative compression operation of the cryptographic hash algorithm to obtain the first message authentication code, and transmit the first message authentication code and the message content to be authenticated to the HMAC Verifier. HMAC verifier: Based on the second intermediate state value pair and the difference between the prefix of the HMAC generator and the prefix of the HMAC verifier, the initial state of the HMAC verifier's iterative compression operation is determined. Combined with the input sequence constructed based on the message content to be authenticated, the iterative compression operation of the cryptographic hash algorithm is performed to obtain the second message authentication code. The verification result is output according to whether the second message authentication code is consistent with the first message authentication code.
Citation Information
Patent Citations
Implementation method and device of hash algorithm
CN112084534A
Method and device for improving message authentication code algorithm, equipment and medium
CN114513311A