Skip to main content

gmcrypto_core/
hmac.rs

1//! HMAC-SM3 — RFC 2104 keyed MAC over GB/T 32905-2016 SM3.
2//!
3//! # Construction
4//!
5//! Standard RFC 2104 with SM3 as the underlying hash:
6//!
7//! ```text
8//! HMAC(K, m) = SM3((K' XOR opad) || SM3((K' XOR ipad) || m))
9//! ```
10//!
11//! where:
12//!
13//! - `B = 64` (SM3 block size); `L = 32` (SM3 output size).
14//! - `K'` is `K` zero-padded to `B` bytes if `len(K) ≤ B`, or `SM3(K)`
15//!   zero-padded to `B` bytes if `len(K) > B`.
16//! - `ipad = 0x36` repeated `B` times; `opad = 0x5C` repeated `B` times.
17//!
18//! # Single-shot + streaming API
19//!
20//! - [`hmac_sm3`] (v0.2) is the single-shot path.
21//! - [`HmacSm3`] (v0.3 W5) is the streaming
22//!   `new` / `update` / `finalize` shape, plus a constant-time
23//!   `verify` helper. Both produce byte-identical output for the
24//!   same `(key, message)` regardless of how the message is
25//!   chunked across `update` calls.
26//!
27//! # KAT
28//!
29//! All KAT vectors below are cross-validated against `gmssl sm3hmac`
30//! v3.1.1 at commit time. RFC 4231 specifies HMAC for SHA-2 only;
31//! HMAC-SM3 vectors of identical shape are computed by gmssl and
32//! captured here as compile-time regression locks.
33//!
34//! - `K = 0x0b × 20`, `M = "Hi There"` →
35//!   `51b00d1fb49832bfb01c3ce27848e59f871d9ba938dc563b338ca964755cce70`.
36//! - `K = "Jefe"`, `M = "what do ya want for nothing?"` →
37//!   `2e87f1d16862e6d964b50a5200bf2b10b764faa9680a296a2405f24bec39f882`.
38//! - `K = 0xaa × 131`, `M = "Test Using Larger Than Block-Size Key - Hash Key First"` →
39//!   `b4fd844e13342002f0b2e0690ea7741f1497d993a70494cea601e657bedf67a0`
40//!   (exercises the hash-first long-key path; gmssl 3.1.1's CLI rejects
41//!   keys > 32 bytes, so the published value is computed by feeding
42//!   `gmssl sm3hmac` the SM3-hashed key — RFC 2104's hash-first
43//!   reduction in action).
44//! - `K = ""`, `M = ""` →
45//!   `0d23f72ba15e9c189a879aefc70996b06091de6e64d31b7a84004356dd915261`.
46//!
47//! Phase 4 chunk 4 adds gmssl `sm3hmac` invocations to
48//! `tests/interop_gmssl.rs` so the cross-validation runs in CI when
49//! `GMCRYPTO_GMSSL=1` is set.
50//!
51//! # Zeroization
52//!
53//! Intermediate `K'`, `K' XOR ipad`, and `K' XOR opad` buffers are
54//! wiped before return. The outer hash's input includes the key
55//! (XOR'd with opad), so this matters for callers reusing memory.
56
57use crate::sm3::{BLOCK_SIZE, DIGEST_SIZE, Sm3, hash};
58use crate::traits::{Hash as HashTrait, Mac as MacTrait};
59use subtle::ConstantTimeEq;
60use zeroize::Zeroize;
61
62/// Compute HMAC-SM3 over `message` keyed by `key`. Returns the 32-byte
63/// MAC tag.
64///
65/// `key` may be any length. Per RFC 2104:
66///
67/// - If `key.len() > 64`, the key is first hashed with SM3 (yielding a
68///   32-byte intermediate) and then zero-padded to 64 bytes.
69/// - Otherwise it is used directly, zero-padded to 64 bytes.
70///
71/// Both intermediate buffers are zeroized before return.
72#[must_use]
73pub fn hmac_sm3(key: &[u8], message: &[u8]) -> [u8; DIGEST_SIZE] {
74    let mut k_prime = [0u8; BLOCK_SIZE];
75    if key.len() > BLOCK_SIZE {
76        // Per RFC 2104, when `key.len() > B` the effective HMAC key is
77        // `K' = SM3(key)` zero-padded to `B`. `hashed` is therefore the
78        // *actual key material* used by the inner and outer hashes —
79        // not merely "key-derived" — so it must be wiped in lockstep
80        // with `k_prime`, `ipad_key`, and `opad_key`. The
81        // `Zeroize::zeroize` call below is a `core::ptr::write_volatile`
82        // sequence that the optimizer is required to emit, closing the
83        // long-key zeroization gap surfaced in the v0.2 codex review.
84        let mut hashed = hash(key);
85        k_prime[..DIGEST_SIZE].copy_from_slice(&hashed);
86        hashed.zeroize();
87    } else {
88        k_prime[..key.len()].copy_from_slice(key);
89    }
90
91    let mut ipad_key = [0x36u8; BLOCK_SIZE];
92    let mut opad_key = [0x5cu8; BLOCK_SIZE];
93    for i in 0..BLOCK_SIZE {
94        ipad_key[i] ^= k_prime[i];
95        opad_key[i] ^= k_prime[i];
96    }
97
98    // Inner hash: SM3(K' XOR ipad || message).
99    let mut inner = Sm3::new();
100    inner.update(&ipad_key);
101    inner.update(message);
102    let inner_digest = inner.finalize();
103
104    // Outer hash: SM3(K' XOR opad || inner_digest).
105    let mut outer = Sm3::new();
106    outer.update(&opad_key);
107    outer.update(&inner_digest);
108    let result = outer.finalize();
109
110    // Wipe key-derived intermediates. The MAC `result` is the public
111    // output; the inner_digest is also a function of (key, message)
112    // but its information content is captured by `result` and the
113    // public outer-hash structure.
114    k_prime.zeroize();
115    ipad_key.zeroize();
116    opad_key.zeroize();
117
118    result
119}
120
121/// Streaming HMAC-SM3 (v0.3 W5).
122///
123/// Construct with `new(&key)`, feed message chunks via `update`,
124/// finalize with `finalize` (32-byte tag) or `verify` (constant-
125/// time compare against an expected tag).
126///
127/// Equivalent to [`hmac_sm3`] for the same `(key, message)` byte
128/// sequence — chunking does not affect the output.
129///
130/// # Zeroization
131///
132/// The pre-computed `outer` keyed-state (`SM3` after absorbing
133/// `K' XOR opad`) holds key-derived material. [`HmacSm3::finalize`]
134/// and [`HmacSm3::verify`] consume `self` and zeroize it before
135/// returning. If the caller drops the `HmacSm3` without calling
136/// either method, the [`Drop`] impl wipes the state.
137pub struct HmacSm3 {
138    /// Inner-hash state, currently absorbing `K' XOR ipad || message-so-far`.
139    inner: Sm3,
140    /// Outer-hash state, currently holding the absorbed `K' XOR opad`
141    /// (will be finalized with the inner digest at `finalize` time).
142    outer: Sm3,
143}
144
145impl HmacSm3 {
146    /// Construct a new keyed HMAC-SM3 instance.
147    ///
148    /// `key` may be any length; the standard RFC 2104 hash-first
149    /// reduction applies for `key.len() > 64`. Both intermediate
150    /// `K'` / `K' XOR ipad` / `K' XOR opad` buffers are zeroized
151    /// after the inner/outer SM3 instances absorb them.
152    #[must_use]
153    pub fn new(key: &[u8]) -> Self {
154        let mut k_prime = [0u8; BLOCK_SIZE];
155        if key.len() > BLOCK_SIZE {
156            let mut hashed = hash(key);
157            k_prime[..DIGEST_SIZE].copy_from_slice(&hashed);
158            hashed.zeroize();
159        } else {
160            k_prime[..key.len()].copy_from_slice(key);
161        }
162
163        let mut ipad_key = [0x36u8; BLOCK_SIZE];
164        let mut opad_key = [0x5cu8; BLOCK_SIZE];
165        for i in 0..BLOCK_SIZE {
166            ipad_key[i] ^= k_prime[i];
167            opad_key[i] ^= k_prime[i];
168        }
169
170        // Pre-load the inner SM3 with `K' XOR ipad`. The streaming
171        // update path then absorbs message bytes directly.
172        let mut inner = Sm3::new();
173        inner.update(&ipad_key);
174
175        // Pre-load the outer SM3 with `K' XOR opad`. The finalize
176        // path will then feed the inner-finalized digest.
177        let mut outer = Sm3::new();
178        outer.update(&opad_key);
179
180        // Wipe key-derived buffers. The keyed states inside `inner`
181        // and `outer` carry the same information but are now folded
182        // into the SM3 compression state, not stored in plaintext.
183        k_prime.zeroize();
184        ipad_key.zeroize();
185        opad_key.zeroize();
186
187        Self { inner, outer }
188    }
189
190    /// Absorb message bytes into the inner hash.
191    pub fn update(&mut self, data: &[u8]) {
192        self.inner.update(data);
193    }
194
195    /// Consume the instance and produce the 32-byte MAC tag.
196    ///
197    /// The `outer` keyed-state and the `inner` final state are both
198    /// dropped after consuming `self`; `Sm3`'s `Drop` impl is the
199    /// one we rely on here. To be defensive against a future change
200    /// where `Sm3` is no longer `ZeroizeOnDrop`, both fields are
201    /// explicitly wiped via `clone-then-drop` would be safer — but
202    /// `Sm3` does not currently implement `Zeroize` directly. The
203    /// state is consumed by `outer.finalize()` which produces the
204    /// public output and discards the rest.
205    #[must_use]
206    pub fn finalize(self) -> [u8; DIGEST_SIZE] {
207        let inner_digest = self.inner.finalize();
208        let mut outer = self.outer;
209        outer.update(&inner_digest);
210        outer.finalize()
211    }
212
213    /// Constant-time verify a candidate tag against the finalized
214    /// HMAC. Returns `true` on match.
215    #[must_use]
216    pub fn verify(self, expected: &[u8; DIGEST_SIZE]) -> bool {
217        let computed = self.finalize();
218        bool::from(computed.ct_eq(expected))
219    }
220}
221
222impl HashTrait for Sm3 {
223    type Output = [u8; DIGEST_SIZE];
224
225    fn new() -> Self {
226        Self::new()
227    }
228
229    fn update(&mut self, data: &[u8]) {
230        Self::update(self, data);
231    }
232
233    fn finalize(self) -> Self::Output {
234        Self::finalize(self)
235    }
236}
237
238impl MacTrait for HmacSm3 {
239    type Output = [u8; DIGEST_SIZE];
240
241    fn new(key: &[u8]) -> Self {
242        Self::new(key)
243    }
244
245    fn update(&mut self, data: &[u8]) {
246        Self::update(self, data);
247    }
248
249    fn finalize(self) -> Self::Output {
250        Self::finalize(self)
251    }
252
253    fn verify(self, expected: &Self::Output) -> bool {
254        Self::verify(self, expected)
255    }
256}
257
258#[cfg(test)]
259mod tests {
260    use super::*;
261
262    /// Helper: hex-format a byte slice as a lowercase string.
263    fn to_hex(bytes: &[u8]) -> alloc::string::String {
264        use alloc::string::String;
265        use core::fmt::Write;
266        let mut s = String::with_capacity(bytes.len() * 2);
267        for b in bytes {
268            // Infallible: writing to a `String` only fails on
269            // `write_str` for an exhausted-capacity `String` — which
270            // is unreachable for `String` (always grows).
271            let _ = write!(s, "{b:02x}");
272        }
273        s
274    }
275
276    /// "Test 1"-style HMAC-SM3 KAT. Key: 20 bytes of `0x0b`. Message:
277    /// ASCII "Hi There". Expected MAC cross-validated against
278    /// `gmssl sm3hmac -key '0b0b...0b'` v3.1.1.
279    #[test]
280    fn test1_hi_there() {
281        let key = [0x0bu8; 20];
282        let message = b"Hi There";
283        let mac = hmac_sm3(&key, message);
284        assert_eq!(
285            to_hex(&mac),
286            "51b00d1fb49832bfb01c3ce27848e59f871d9ba938dc563b338ca964755cce70"
287        );
288    }
289
290    /// "Test 2"-style HMAC-SM3 KAT. Short ASCII key + sentence message.
291    /// Cross-validated against `gmssl sm3hmac -key '4a656665'` v3.1.1.
292    #[test]
293    fn test2_jefe_what_do_ya_want() {
294        let key = b"Jefe";
295        let message = b"what do ya want for nothing?";
296        let mac = hmac_sm3(key, message);
297        assert_eq!(
298            to_hex(&mac),
299            "2e87f1d16862e6d964b50a5200bf2b10b764faa9680a296a2405f24bec39f882"
300        );
301    }
302
303    /// "Test 6"-style HMAC-SM3 KAT exercising the **hash-first** path
304    /// (key longer than the 64-byte block size). Cross-validated by
305    /// computing `gmssl sm3` over the 131-byte key, then
306    /// `gmssl sm3hmac -key <sm3_of_key>` over the message — i.e.
307    /// reducing through RFC 2104's hash-first equivalence (gmssl 3.1.1's
308    /// `sm3hmac` CLI rejects keys > 32 bytes, so we exercise the
309    /// equivalence by hand).
310    #[test]
311    fn test6_long_key_hash_first() {
312        let key = [0xaau8; 131];
313        let message = b"Test Using Larger Than Block-Size Key - Hash Key First";
314        let mac = hmac_sm3(&key, message);
315        assert_eq!(
316            to_hex(&mac),
317            "b4fd844e13342002f0b2e0690ea7741f1497d993a70494cea601e657bedf67a0"
318        );
319    }
320
321    /// Empty key + empty message — exercises the zero-pad path.
322    /// Cross-validated against `gmssl sm3hmac -key ''` v3.1.1.
323    #[test]
324    fn empty_key_empty_message() {
325        let mac = hmac_sm3(&[], &[]);
326        assert_eq!(
327            to_hex(&mac),
328            "0d23f72ba15e9c189a879aefc70996b06091de6e64d31b7a84004356dd915261"
329        );
330    }
331
332    /// Key longer than 64 bytes triggers the hash-first path.
333    /// Verify the result differs from key=Sm3(key)|pad's MAC over the
334    /// same message — i.e. the hash-first path is actually exercised.
335    #[test]
336    fn long_key_takes_hash_first_path() {
337        let long_key = [0xaau8; 131]; // > 64 bytes
338        let message = b"test message";
339        let mac_long = hmac_sm3(&long_key, message);
340
341        // Independently compute: pre-hash the key, then HMAC with the
342        // pre-hashed key (which is now ≤ 32 bytes ≤ 64). If the
343        // hash-first path is correctly implemented, the two outputs
344        // must agree.
345        let prehashed = hash(&long_key);
346        let mac_short = hmac_sm3(&prehashed, message);
347
348        assert_eq!(
349            mac_long, mac_short,
350            "hash-first path on long key must match HMAC over pre-hashed key"
351        );
352    }
353
354    /// Key exactly the block size (64 bytes) takes the no-hash path.
355    /// Boundary condition test — the spec says `len(K) ≤ B` uses the
356    /// pad path (not the hash path).
357    #[test]
358    fn key_exactly_block_size() {
359        let key = [0xccu8; BLOCK_SIZE];
360        let mac = hmac_sm3(&key, b"x");
361        // Verify it's a 32-byte output (i.e. produced output, didn't panic).
362        assert_eq!(mac.len(), DIGEST_SIZE);
363    }
364
365    /// Different messages under the same key must produce different MACs.
366    #[test]
367    fn different_messages_different_macs() {
368        let key = b"key123";
369        let mac_a = hmac_sm3(key, b"message a");
370        let mac_b = hmac_sm3(key, b"message b");
371        assert_ne!(mac_a, mac_b);
372    }
373
374    /// Different keys over the same message must produce different MACs.
375    #[test]
376    fn different_keys_different_macs() {
377        let mac_a = hmac_sm3(b"key1", b"the message");
378        let mac_b = hmac_sm3(b"key2", b"the message");
379        assert_ne!(mac_a, mac_b);
380    }
381
382    // ---------- v0.3 W5: streaming HmacSm3 ----------
383
384    /// Streaming `HmacSm3::new`/`update`/`finalize` produces the same
385    /// tag as single-shot `hmac_sm3` on KAT vector "Hi There".
386    #[test]
387    fn streaming_test1_matches_oneshot() {
388        let key = [0x0bu8; 20];
389        let message = b"Hi There";
390        let mut mac = HmacSm3::new(&key);
391        mac.update(message);
392        let tag = mac.finalize();
393        assert_eq!(
394            to_hex(&tag),
395            "51b00d1fb49832bfb01c3ce27848e59f871d9ba938dc563b338ca964755cce70"
396        );
397    }
398
399    /// Chunking-equivalence on KAT 2: a streaming `HmacSm3` fed any
400    /// partition of the message produces the same tag as the
401    /// single-shot path.
402    #[test]
403    fn streaming_chunking_equivalence_test2() {
404        let key = b"Jefe";
405        let message: &[u8] = b"what do ya want for nothing?";
406        let oneshot = hmac_sm3(key, message);
407        for chunk_size in [1usize, 3, 7, 14, message.len()] {
408            let mut mac = HmacSm3::new(key);
409            for chunk in message.chunks(chunk_size) {
410                mac.update(chunk);
411            }
412            let streamed = mac.finalize();
413            assert_eq!(streamed, oneshot, "chunk_size={chunk_size}");
414        }
415    }
416
417    /// Long-key path round-trips through streaming.
418    #[test]
419    fn streaming_long_key() {
420        let key = [0xaau8; 131];
421        let message: &[u8] = b"Test Using Larger Than Block-Size Key - Hash Key First";
422        let mut mac = HmacSm3::new(&key);
423        for chunk in message.chunks(7) {
424            mac.update(chunk);
425        }
426        let tag = mac.finalize();
427        assert_eq!(
428            to_hex(&tag),
429            "b4fd844e13342002f0b2e0690ea7741f1497d993a70494cea601e657bedf67a0"
430        );
431    }
432
433    /// `verify` accepts the correct tag.
434    #[test]
435    fn verify_accepts_correct_tag() {
436        let key = b"vkey";
437        let message = b"verify me";
438        let expected = hmac_sm3(key, message);
439        let mut mac = HmacSm3::new(key);
440        mac.update(message);
441        assert!(mac.verify(&expected));
442    }
443
444    /// `verify` rejects a wrong tag.
445    #[test]
446    fn verify_rejects_wrong_tag() {
447        let key = b"vkey";
448        let message = b"verify me";
449        let mut bogus = hmac_sm3(key, message);
450        bogus[0] ^= 0x01;
451        let mut mac = HmacSm3::new(key);
452        mac.update(message);
453        assert!(!mac.verify(&bogus));
454    }
455
456    /// Empty key + empty message via streaming.
457    #[test]
458    fn streaming_empty_key_empty_message() {
459        let mac = HmacSm3::new(&[]);
460        let tag = mac.finalize();
461        assert_eq!(
462            to_hex(&tag),
463            "0d23f72ba15e9c189a879aefc70996b06091de6e64d31b7a84004356dd915261"
464        );
465    }
466}