From 88c6ed81771b312be0507176a0406571d9264ccd Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Tue, 10 Oct 2023 15:28:40 +0800 Subject: [PATCH] Updated SM4 with AESENCLAST (markdown) --- SM4-with-AESENCLAST.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/SM4-with-AESENCLAST.md b/SM4-with-AESENCLAST.md index 55232b0..f37e1c1 100644 --- a/SM4-with-AESENCLAST.md +++ b/SM4-with-AESENCLAST.md @@ -141,6 +141,41 @@ sm4_box_aesbox_4 x.bytes[i] = v } ``` +## Intel的方法 +[pcpsms4_l9cn](https://github.com/intel/ipp-crypto/blob/develop/sources/ippcp/pcpsms4_l9cn.h) +```go +var shift_row_inv = set64(0x0306090C0F020508, 0x0B0E0104070A0D00) +var intelm1l = set64(0xdcf84460b3972b0f, 0xb6922e0ad9fd4165) +var intelm1h = set64(0x64ad03cae42d834a, 0x2ee74980ae67c900) +var intelm2l = set64(0x48c2a32957ddbc36, 0xad2746ccb23859d3) +var intelm2h = set64(0x134307579aca8ede, 0xcd9dd98944145000) +var intelenckey = set64(0x6363636363636363, 0x6363636363636363) +var intelmaskSrows = shift_row_inv + +func sm4_box_aesenclast_intel(rk uint32, t0, t1, t2, t3, a1l, a1h, a2l, a2h __m128i) __m128i { + rk128 := mm_set_epi32(rk, rk, rk, rk) + x := xor(xor(t1, t2), t3) + x = xor(x, rk128) + + y := mm_and_si128(x, const_0f) + y = mm_shuffle_epi8(a1l, y) + x = mm_srli_epi64(x, 4) + x = mm_and_si128(x, const_0f) + x = xor(mm_shuffle_epi8(a1h, x), y) + + x = mm_aesenclast_si128(x, intelenckey) + x = mm_shuffle_epi8(x, intelmaskSrows) + + y = mm_and_si128(x, const_0f) + y = mm_shuffle_epi8(a2l, y) + x = mm_srli_epi64(x, 4) + x = mm_and_si128(x, const_0f) + x = xor(mm_shuffle_epi8(a2h, x), y) + + return x +} +``` + ## How to calculate lookup table from M, C? $\{ M\times i + C \mid i \in [0,255] \}$