1#[rustfmt::skip]
20const SBOX: [u8; 256] = [
21 0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7, 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05,
22 0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3, 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99,
23 0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a, 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62,
24 0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95, 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6,
25 0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba, 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8,
26 0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b, 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35,
27 0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2, 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87,
28 0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52, 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e,
29 0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5, 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1,
30 0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55, 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3,
31 0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60, 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f,
32 0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f, 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51,
33 0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f, 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8,
34 0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd, 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0,
35 0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e, 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84,
36 0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20, 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48,
37];
38
39const fn l_const(x: u32) -> u32 {
57 let r2 = x.rotate_left(2);
60 let r10 = x.rotate_left(10);
61 let r18 = x.rotate_left(18);
62 let r24 = x.rotate_left(24);
63 x ^ r2 ^ r10 ^ r18 ^ r24
64}
65
66const fn build_t_table(byte_pos: usize) -> [u32; 256] {
67 let mut tbl = [0u32; 256];
68 let mut b = 0usize;
69 while b < 256 {
70 let sval = SBOX[b] as u32;
71 let shifted = sval << (24 - 8 * byte_pos);
72 tbl[b] = l_const(shifted);
73 b += 1;
74 }
75 tbl
76}
77
78static T0: [u32; 256] = build_t_table(0);
79static T1: [u32; 256] = build_t_table(1);
80static T2: [u32; 256] = build_t_table(2);
81static T3: [u32; 256] = build_t_table(3);
82
83const SBOX_ANF: [[u128; 2]; 8] = crate::ct::build_byte_sbox_anf(&SBOX);
89
90const FK: [u32; 4] = [0xa3b1_bac6, 0x56aa_3350, 0x677d_9197, 0xb270_22dc];
92
93const CK: [u32; 32] = [
94 0x0007_0e15,
95 0x1c23_2a31,
96 0x383f_464d,
97 0x545b_6269,
98 0x7077_7e85,
99 0x8c93_9aa1,
100 0xa8af_b6bd,
101 0xc4cb_d2d9,
102 0xe0e7_eef5,
103 0xfc03_0a11,
104 0x181f_262d,
105 0x343b_4249,
106 0x5057_5e65,
107 0x6c73_7a81,
108 0x888f_969d,
109 0xa4ab_b2b9,
110 0xc0c7_ced5,
111 0xdce3_eaf1,
112 0xf8ff_060d,
113 0x141b_2229,
114 0x3037_3e45,
115 0x4c53_5a61,
116 0x686f_767d,
117 0x848b_9299,
118 0xa0a7_aeb5,
119 0xbcc3_cad1,
120 0xd8df_e6ed,
121 0xf4fb_0209,
122 0x1017_1e25,
123 0x2c33_3a41,
124 0x484f_565d,
125 0x646b_7279,
126];
127
128#[inline]
129fn tau(x: u32) -> u32 {
130 (u32::from(SBOX[(x >> 24) as usize]) << 24)
131 | (u32::from(SBOX[((x >> 16) & 0xff) as usize]) << 16)
132 | (u32::from(SBOX[((x >> 8) & 0xff) as usize]) << 8)
133 | u32::from(SBOX[(x & 0xff) as usize])
134}
135
136#[inline]
137fn sbox_ct_byte(input: u8) -> u8 {
138 crate::ct::eval_byte_sbox(&SBOX_ANF, input)
139}
140
141#[inline]
142fn tau_ct(x: u32) -> u32 {
143 (u32::from(sbox_ct_byte((x >> 24) as u8)) << 24)
144 | (u32::from(sbox_ct_byte(((x >> 16) & 0xff) as u8)) << 16)
145 | (u32::from(sbox_ct_byte(((x >> 8) & 0xff) as u8)) << 8)
146 | u32::from(sbox_ct_byte((x & 0xff) as u8))
147}
148
149#[inline]
150fn l(x: u32) -> u32 {
151 x ^ x.rotate_left(2) ^ x.rotate_left(10) ^ x.rotate_left(18) ^ x.rotate_left(24)
152}
153
154#[inline]
155fn l_prime(x: u32) -> u32 {
156 x ^ x.rotate_left(13) ^ x.rotate_left(23)
157}
158
159#[inline]
160fn t(x: u32) -> u32 {
161 T0[(x >> 24) as usize]
162 ^ T1[((x >> 16) & 0xff) as usize]
163 ^ T2[((x >> 8) & 0xff) as usize]
164 ^ T3[(x & 0xff) as usize]
165}
166
167#[inline]
168fn t_prime(x: u32) -> u32 {
169 l_prime(tau(x))
170}
171
172#[inline]
173fn t_ct(x: u32) -> u32 {
174 l(tau_ct(x))
175}
176
177#[inline]
178fn t_prime_ct(x: u32) -> u32 {
179 l_prime(tau_ct(x))
180}
181
182fn expand_round_keys(key: &[u8; 16]) -> ([u32; 32], [u32; 32]) {
183 let mut k = [0u32; 36];
184 for i in 0..4 {
185 let mk = u32::from_be_bytes(key[4 * i..4 * i + 4].try_into().unwrap());
186 k[i] = mk ^ FK[i];
187 }
188
189 let mut enc = [0u32; 32];
190 for i in 0..32 {
191 k[i + 4] = k[i] ^ t_prime(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i]);
192 enc[i] = k[i + 4];
193 }
194
195 let mut dec = enc;
196 dec.reverse();
197 (enc, dec)
198}
199
200fn expand_round_keys_ct(key: &[u8; 16]) -> ([u32; 32], [u32; 32]) {
201 let mut k = [0u32; 36];
202 for i in 0..4 {
203 let mk = u32::from_be_bytes(key[4 * i..4 * i + 4].try_into().unwrap());
204 k[i] = mk ^ FK[i];
205 }
206
207 let mut enc = [0u32; 32];
208 for i in 0..32 {
209 k[i + 4] = k[i] ^ t_prime_ct(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i]);
210 enc[i] = k[i + 4];
211 }
212
213 let mut dec = enc;
214 dec.reverse();
215 (enc, dec)
216}
217
218#[inline]
219fn sm4_core(block: &[u8; 16], rk: &[u32; 32]) -> [u8; 16] {
220 let mut x0 = u32::from_be_bytes(block[0..4].try_into().unwrap());
221 let mut x1 = u32::from_be_bytes(block[4..8].try_into().unwrap());
222 let mut x2 = u32::from_be_bytes(block[8..12].try_into().unwrap());
223 let mut x3 = u32::from_be_bytes(block[12..16].try_into().unwrap());
224
225 for &rki in rk {
226 let x4 = x0 ^ t(x1 ^ x2 ^ x3 ^ rki);
227 x0 = x1;
228 x1 = x2;
229 x2 = x3;
230 x3 = x4;
231 }
232
233 let mut out = [0u8; 16];
234 out[0..4].copy_from_slice(&x3.to_be_bytes());
235 out[4..8].copy_from_slice(&x2.to_be_bytes());
236 out[8..12].copy_from_slice(&x1.to_be_bytes());
237 out[12..16].copy_from_slice(&x0.to_be_bytes());
238 out
239}
240
241#[inline]
242fn sm4_core_ct(block: &[u8; 16], rk: &[u32; 32]) -> [u8; 16] {
243 let mut x0 = u32::from_be_bytes(block[0..4].try_into().unwrap());
244 let mut x1 = u32::from_be_bytes(block[4..8].try_into().unwrap());
245 let mut x2 = u32::from_be_bytes(block[8..12].try_into().unwrap());
246 let mut x3 = u32::from_be_bytes(block[12..16].try_into().unwrap());
247
248 for &rki in rk {
249 let x4 = x0 ^ t_ct(x1 ^ x2 ^ x3 ^ rki);
250 x0 = x1;
251 x1 = x2;
252 x2 = x3;
253 x3 = x4;
254 }
255
256 let mut out = [0u8; 16];
257 out[0..4].copy_from_slice(&x3.to_be_bytes());
258 out[4..8].copy_from_slice(&x2.to_be_bytes());
259 out[8..12].copy_from_slice(&x1.to_be_bytes());
260 out[12..16].copy_from_slice(&x0.to_be_bytes());
261 out
262}
263
264pub struct Sm4 {
266 enc_rk: [u32; 32],
267 dec_rk: [u32; 32],
268}
269
270pub struct Sm4Ct {
272 enc_rk: [u32; 32],
273 dec_rk: [u32; 32],
274}
275
276pub type Sms4 = Sm4;
278pub type Sms4Ct = Sm4Ct;
280
281impl Sm4 {
282 #[must_use]
284 pub fn new(key: &[u8; 16]) -> Self {
285 let (enc_rk, dec_rk) = expand_round_keys(key);
286 Self { enc_rk, dec_rk }
287 }
288
289 pub fn new_wiping(key: &mut [u8; 16]) -> Self {
291 let out = Self::new(key);
292 crate::ct::zeroize_slice(key.as_mut_slice());
293 out
294 }
295
296 #[must_use]
298 pub fn encrypt_block(&self, block: &[u8; 16]) -> [u8; 16] {
299 sm4_core(block, &self.enc_rk)
300 }
301
302 #[must_use]
304 pub fn decrypt_block(&self, block: &[u8; 16]) -> [u8; 16] {
305 sm4_core(block, &self.dec_rk)
306 }
307}
308
309impl Sm4Ct {
310 #[must_use]
312 pub fn new(key: &[u8; 16]) -> Self {
313 let (enc_rk, dec_rk) = expand_round_keys_ct(key);
314 Self { enc_rk, dec_rk }
315 }
316
317 pub fn new_wiping(key: &mut [u8; 16]) -> Self {
319 let out = Self::new(key);
320 crate::ct::zeroize_slice(key.as_mut_slice());
321 out
322 }
323
324 #[must_use]
326 pub fn encrypt_block(&self, block: &[u8; 16]) -> [u8; 16] {
327 sm4_core_ct(block, &self.enc_rk)
328 }
329
330 #[must_use]
332 pub fn decrypt_block(&self, block: &[u8; 16]) -> [u8; 16] {
333 sm4_core_ct(block, &self.dec_rk)
334 }
335}
336
337impl crate::BlockCipher for Sm4 {
338 const BLOCK_LEN: usize = 16;
339
340 fn encrypt(&self, block: &mut [u8]) {
341 assert_eq!(block.len(), Self::BLOCK_LEN);
342 let mut tmp = [0u8; 16];
343 tmp.copy_from_slice(block);
344 block.copy_from_slice(&self.encrypt_block(&tmp));
345 }
346
347 fn decrypt(&self, block: &mut [u8]) {
348 assert_eq!(block.len(), Self::BLOCK_LEN);
349 let mut tmp = [0u8; 16];
350 tmp.copy_from_slice(block);
351 block.copy_from_slice(&self.decrypt_block(&tmp));
352 }
353}
354
355impl crate::BlockCipher for Sm4Ct {
356 const BLOCK_LEN: usize = 16;
357
358 fn encrypt(&self, block: &mut [u8]) {
359 assert_eq!(block.len(), Self::BLOCK_LEN);
360 let mut tmp = [0u8; 16];
361 tmp.copy_from_slice(block);
362 block.copy_from_slice(&self.encrypt_block(&tmp));
363 }
364
365 fn decrypt(&self, block: &mut [u8]) {
366 assert_eq!(block.len(), Self::BLOCK_LEN);
367 let mut tmp = [0u8; 16];
368 tmp.copy_from_slice(block);
369 block.copy_from_slice(&self.decrypt_block(&tmp));
370 }
371}
372
373impl Drop for Sm4 {
374 fn drop(&mut self) {
375 crate::ct::zeroize_slice(self.enc_rk.as_mut_slice());
376 crate::ct::zeroize_slice(self.dec_rk.as_mut_slice());
377 }
378}
379
380impl Drop for Sm4Ct {
381 fn drop(&mut self) {
382 crate::ct::zeroize_slice(self.enc_rk.as_mut_slice());
383 crate::ct::zeroize_slice(self.dec_rk.as_mut_slice());
384 }
385}
386
387#[cfg(test)]
388mod tests {
389 use super::*;
390
391 fn xorshift64(state: &mut u64) -> u64 {
392 let mut x = *state;
393 x ^= x << 13;
394 x ^= x >> 7;
395 x ^= x << 17;
396 *state = x;
397 x
398 }
399
400 fn fill_bytes(state: &mut u64, out: &mut [u8]) {
401 for chunk in out.chunks_mut(8) {
402 let bytes = xorshift64(state).to_le_bytes();
403 let n = chunk.len();
404 chunk.copy_from_slice(&bytes[..n]);
405 }
406 }
407
408 fn parse<const N: usize>(s: &str) -> [u8; N] {
409 let v: Vec<u8> = (0..s.len())
410 .step_by(2)
411 .map(|i| u8::from_str_radix(&s[i..i + 2], 16).unwrap())
412 .collect();
413 v.try_into().unwrap()
414 }
415
416 #[test]
417 fn example_1_encrypt_decrypt() {
418 let key: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
419 let pt: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
420 let ct: [u8; 16] = parse("681edf34d206965e86b3e94f536e4246");
421
422 let sm4 = Sm4::new(&key);
423 assert_eq!(sm4.encrypt_block(&pt), ct, "encrypt");
424 assert_eq!(sm4.decrypt_block(&ct), pt, "decrypt");
425 }
426
427 #[test]
428 fn example_1_encrypt_decrypt_ct() {
429 let key: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
430 let pt: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
431 let ct: [u8; 16] = parse("681edf34d206965e86b3e94f536e4246");
432
433 let sm4 = Sm4Ct::new(&key);
434 assert_eq!(sm4.encrypt_block(&pt), ct, "encrypt");
435 assert_eq!(sm4.decrypt_block(&ct), pt, "decrypt");
436 }
437
438 #[test]
439 fn example_2_million_encryptions() {
440 let key: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
441 let mut block: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
442 let expected: [u8; 16] = parse("595298c7c6fd271f0402f804c33d3f66");
443
444 let sm4 = Sm4::new(&key);
445 for _ in 0..1_000_000 {
446 block = sm4.encrypt_block(&block);
447 }
448
449 assert_eq!(block, expected);
450 }
451
452 #[test]
453 fn example_2_million_encryptions_ct() {
454 let key: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
455 let mut block: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
456 let expected: [u8; 16] = parse("595298c7c6fd271f0402f804c33d3f66");
457
458 let sm4 = Sm4Ct::new(&key);
459 for _ in 0..1_000_000 {
460 block = sm4.encrypt_block(&block);
461 }
462
463 assert_eq!(block, expected);
464 }
465
466 #[test]
467 fn sms4_alias_matches_sm4() {
468 let key: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
469 let pt: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
470 let a = Sm4::new(&key);
471 let b = Sms4::new(&key);
472 assert_eq!(a.encrypt_block(&pt), b.encrypt_block(&pt));
473 }
474
475 #[test]
476 fn sms4_ct_alias_matches_sm4_ct() {
477 let key: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
478 let pt: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
479 let a = Sm4Ct::new(&key);
480 let b = Sms4Ct::new(&key);
481 assert_eq!(a.encrypt_block(&pt), b.encrypt_block(&pt));
482 }
483
484 #[test]
485 fn ct_sbox_matches_table() {
486 for x in 0u16..=255 {
487 let b = u8::try_from(x).expect("table index fits in u8");
488 assert_eq!(sbox_ct_byte(b), SBOX[x as usize], "sbox {x:02x}");
489 }
490 }
491
492 #[test]
493 fn sm4_and_sm4ct_match() {
494 let key: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
495 let pt: [u8; 16] = parse("0123456789abcdeffedcba9876543210");
496 let fast = Sm4::new(&key);
497 let slow = Sm4Ct::new(&key);
498 assert_eq!(fast.encrypt_block(&pt), slow.encrypt_block(&pt));
499 }
500
501 #[test]
502 fn sm4_and_sm4ct_match_random_vectors() {
503 let mut seed_rng = 0x0ddc_0ffe_ecad_beefu64;
504 for _ in 0..256 {
505 let mut key = [0u8; 16];
506 let mut block = [0u8; 16];
507 fill_bytes(&mut seed_rng, &mut key);
508 fill_bytes(&mut seed_rng, &mut block);
509
510 let fast = Sm4::new(&key);
511 let ct = Sm4Ct::new(&key);
512 let fast_ct = fast.encrypt_block(&block);
513 let ct_ct = ct.encrypt_block(&block);
514 assert_eq!(fast_ct, ct_ct);
515 assert_eq!(block, fast.decrypt_block(&fast_ct));
516 assert_eq!(block, ct.decrypt_block(&ct_ct));
517 }
518 }
519
520 #[test]
521 fn sm4_matches_openssl_ecb() {
522 let key_hex = "0123456789abcdeffedcba9876543210";
523 let pt_hex = "0123456789abcdeffedcba9876543210";
524 let Some(expected) =
525 crate::test_utils::run_openssl_enc("-sm4-ecb", key_hex, None, &parse::<16>(pt_hex))
526 else {
527 return;
528 };
529
530 let cipher = Sm4::new(&parse(key_hex));
531 assert_eq!(
532 cipher.encrypt_block(&parse(pt_hex)).as_slice(),
533 expected.as_slice()
534 );
535 }
536}