Skip to content

Instantly share code, notes, and snippets.

@emmansun
Last active October 10, 2023 07:55
Show Gist options
  • Save emmansun/ae4677d71c75ff8407d5f5b3a884f5d2 to your computer and use it in GitHub Desktop.
Save emmansun/ae4677d71c75ff8407d5f5b3a884f5d2 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/binary"
"fmt"
"math/bits"
)
var aes_sbox = [256]byte{
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 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16]byte
}
var shift_row = set64(0x0B06010C07020D08, 0x030E09040F0A0500)
var shift_row_inv = set64(0x0306090C0F020508, 0x0B0E0104070A0D00)
var const_0f = set64(0x0F0F0F0F0F0F0F0F, 0x0F0F0F0F0F0F0F0F)
var r08 = set64(0x0E0D0C0F0A09080B, 0x0605040702010003)
var r16 = set64(0x0D0C0F0E09080B0A, 0x0504070601000302)
var r24 = set64(0x0C0F0E0D080B0A09, 0x0407060500030201)
var flp = set64(0x0C0D0E0F08090A0B, 0x0405060700010203)
var m1l = set64(0xC7C1B4B222245157, 0x9197E2E474720701)
var m1h = set64(0xF052B91BF95BB012, 0xE240AB09EB49A200)
var m2l = set64(0xEDD14478172BBE82, 0x5B67F2CEA19D0834)
var m2h = set64(0x11CDBE62CC1063BF, 0xAE7201DD73AFDC00)
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
// gen_lookup_table([8]byte{0x14, 0x2e, 0x16, 0x8a, 0x60, 0x0d, 0x9b, 0x66}, 0x01)
var m1_box = [256]byte{
0x01, 0x07, 0x72, 0x74, 0xE4, 0xE2, 0x97, 0x91, 0x57, 0x51, 0x24, 0x22, 0xB2, 0xB4, 0xC1, 0xC7,
0xA3, 0xA5, 0xD0, 0xD6, 0x46, 0x40, 0x35, 0x33, 0xF5, 0xF3, 0x86, 0x80, 0x10, 0x16, 0x63, 0x65,
0x48, 0x4E, 0x3B, 0x3D, 0xAD, 0xAB, 0xDE, 0xD8, 0x1E, 0x18, 0x6D, 0x6B, 0xFB, 0xFD, 0x88, 0x8E,
0xEA, 0xEC, 0x99, 0x9F, 0x0F, 0x09, 0x7C, 0x7A, 0xBC, 0xBA, 0xCF,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xDD, 0x48, 0x74, 0x1B, 0x27, 0xB2, 0x8E,
0x24, 0x18, 0x8D, 0xB1, 0xDE, 0xE2, 0x77, 0x4B, 0x92, 0xAE, 0x3B, 0x07, 0x68, 0x54, 0xC1, 0xFD,
0xF8, 0xC4, 0x51, 0x6D, 0x02, 0x3E, 0xAB, 0x97, 0x4E, 0x72, 0xE7, 0xDB, 0xB4, 0x88, 0x1D, 0x21,
0x56, 0x6A, 0xFF, 0xC3, 0xAC, 0x90, 0x05, 0x39, 0xE0, 0xDC, 0x49, 0x75, 0x1A, 0x26, 0xB3, 0x8F,
0x8A, 0xB6, 0x23, 0x1F, 0x70, 0x4C, 0xD9, 0xE5, 0x3C, 0x00, 0x95, 0xA9, 0xC6, 0xFA, 0x6F, 0x53,
0xF9, 0xC5, 0x50, 0x6C, 0x03, 0x3F, 0xAA, 0x96, 0x4F, 0x73, 0xE6, 0xDA, 0xB5, 0x89, 0x1C, 0x20,
0x25, 0x19, 0x8C, 0xB0, 0xDF, 0xE3, 0x76, 0x4A, 0x93, 0xAF, 0x3A, 0x06, 0x69, 0x55, 0xC0, 0xFC,
}
// Generate lookup table based on M matrix and C
func gen_lookup_table(m [8]byte, c byte) {
for i := 0; i < 16; i++ {
for j := 0; j < 16; j++ {
x := ((byte(bits.OnesCount8(byte(i*16+j)&m[0])) & 1) << 7) ^
((byte(bits.OnesCount8(byte(i*16+j)&m[1])) & 1) << 6) ^
((byte(bits.OnesCount8(byte(i*16+j)&m[2])) & 1) << 5) ^
((byte(bits.OnesCount8(byte(i*16+j)&m[3])) & 1) << 4) ^
((byte(bits.OnesCount8(byte(i*16+j)&m[4])) & 1) << 3) ^
((byte(bits.OnesCount8(byte(i*16+j)&m[5])) & 1) << 2) ^
((byte(bits.OnesCount8(byte(i*16+j)&m[6])) & 1) << 1) ^
((byte(bits.OnesCount8(byte(i*16+j)&m[7])) & 1) << 0) ^ c
fmt.Printf("0x%02X, ", x)
}
fmt.Println()
}
}
// Generate matrix based on lookup table
func gen_matrix(lookup [256]byte) (m [8]byte) {
c := lookup[0]
m80 := lookup[0x80] ^ c
m40 := lookup[0x40] ^ c
m20 := lookup[0x20] ^ c
m10 := lookup[0x10] ^ c
m08 := lookup[0x08] ^ c
m04 := lookup[0x04] ^ c
m02 := lookup[0x02] ^ c
m01 := lookup[0x01] ^ c
m[0] = (m80 & 0x80) ^ ((m40 & 0x80) >> 1) ^ ((m20 & 0x80) >> 2) ^ ((m10 & 0x80) >> 3) ^ ((m08 & 0x80) >> 4) ^ ((m04 & 0x80) >> 5) ^ ((m02 & 0x80) >> 6) ^ ((m01 & 0x80) >> 7)
m[1] = ((m80 & 0x40) << 1) ^ (m40 & 0x40) ^ ((m20 & 0x40) >> 1) ^ ((m10 & 0x40) >> 2) ^ ((m08 & 0x40) >> 3) ^ ((m04 & 0x40) >> 4) ^ ((m02 & 0x40) >> 5) ^ ((m01 & 0x40) >> 6)
m[2] = ((m80 & 0x20) << 2) ^ ((m40 & 0x20) << 1) ^ (m20 & 0x20) ^ ((m10 & 0x20) >> 1) ^ ((m08 & 0x20) >> 2) ^ ((m04 & 0x20) >> 3) ^ ((m02 & 0x20) >> 4) ^ ((m01 & 0x20) >> 5)
m[3] = ((m80 & 0x10) << 3) ^ ((m40 & 0x10) << 2) ^ ((m20 & 0x10) << 1) ^ (m10 & 0x10) ^ ((m08 & 0x10) >> 1) ^ ((m04 & 0x10) >> 2) ^ ((m02 & 0x10) >> 3) ^ ((m01 & 0x10) >> 4)
m[4] = ((m80 & 0x08) << 4) ^ ((m40 & 0x08) << 3) ^ ((m20 & 0x08) << 2) ^ ((m10 & 0x08) << 1) ^ (m08 & 0x08) ^ ((m04 & 0x08) >> 1) ^ ((m02 & 0x08) >> 2) ^ ((m01 & 0x08) >> 3)
m[5] = ((m80 & 0x04) << 5) ^ ((m40 & 0x04) << 4) ^ ((m20 & 0x04) << 3) ^ ((m10 & 0x04) << 2) ^ ((m08 & 0x04) << 1) ^ (m04 & 0x04) ^ ((m02 & 0x04) >> 1) ^ ((m01 & 0x04) >> 2)
m[6] = ((m80 & 0x02) << 6) ^ ((m40 & 0x02) << 5) ^ ((m20 & 0x02) << 4) ^ ((m10 & 0x02) << 3) ^ ((m08 & 0x02) << 2) ^ ((m04 & 0x02) << 1) ^ (m02 & 0x02) ^ ((m01 & 0x02) >> 1)
m[7] = ((m80 & 0x01) << 7) ^ ((m40 & 0x01) << 6) ^ ((m20 & 0x01) << 5) ^ ((m10 & 0x01) << 4) ^ ((m08 & 0x01) << 2) ^ ((m04 & 0x01) << 2) ^ ((m02 & 0x01) << 1) ^ (m01 & 0x01)
return
}
// https://ppfocus.com/0/ed0cb869b.html
var m3l = set64(0x3045F98CEF9A2653, 0x0A7FC3B6D5A01C69)
var m3h = set64(0x68F05FC7049C33AB, 0xC35BF46CAF379800)
var m4l = set64(0x0E019E916A65FAF5, 0x9A950A05FEF16E61)
var m4h = set64(0x2C88CC68E14501A5, 0x892D69CD44E0A400)
// gen_lookup_table([8]byte{0x96, 0x47, 0xe9, 0x3d, 0xde, 0x65, 0xac, 0xa7}, 0x69)
var m3_box = [256]byte{
0x69, 0x1C, 0xA0, 0xD5, 0xB6, 0xC3, 0x7F, 0x0A, 0x53, 0x26, 0x9A, 0xEF, 0x8C, 0xF9, 0x45, 0x30,
0xF1, 0x84, 0x38, 0x4D, 0x2E, 0x5B, 0xE7, 0x92, 0xCB, 0xBE, 0x02, 0x77, 0x14, 0x61, 0xDD, 0xA8,
0x5E, 0x2B, 0x97, 0xE2, 0x81, 0xF4, 0x48, 0x3D, 0x64, 0x11, 0xAD, 0xD8, 0xBB, 0xCE, 0x72, 0x07,
0xC6, 0xB3, 0x0F, 0x7A, 0x19, 0x6C, 0xD0, 0xA5, 0xFC, 0x89, 0x35, 0x40, 0x23, 0x56, 0xEA, 0x9F,
0x05, 0x70, 0xCC, 0xB9, 0xDA, 0xAF, 0x13, 0x66, 0x3F, 0x4A, 0xF6, 0x83, 0xE0, 0x95, 0x29, 0x5C,
0x9D, 0xE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B,
0x80, 0x8F, 0x10, 0x1F, 0xE4, 0xEB, 0x74, 0x7B, 0x14, 0x1B, 0x84, 0x8B, 0x70, 0x7F, 0xE0, 0xEF,
0x09, 0x06, 0x99, 0x96, 0x6D, 0x62, 0xFD, 0xF2, 0x9D, 0x92, 0x0D, 0x02, 0xF9, 0xF6, 0x69, 0x66,
0xAD, 0xA2, 0x3D, 0x32, 0xC9, 0xC6, 0x59, 0x56, 0x39, 0x36, 0xA9, 0xA6, 0x5D, 0x52, 0xCD, 0xC2,
0xE9, 0xE6, 0x79, 0x76, 0x8D, 0x82, 0x1D, 0x12, 0x7D, 0x72, 0xED, 0xE2, 0x19, 0x16, 0x89, 0x86,
0x4D, 0x42, 0xDD, 0xD2, 0x29, 0x26, 0xB9, 0xB6, 0xD9, 0xD6, 0x49, 0x46, 0xBD, 0xB2, 0x2D, 0x22,
}
func set64(hi, lo uint64) (m __m128i) {
binary.LittleEndian.PutUint64(m.bytes[:], lo)
binary.LittleEndian.PutUint64(m.bytes[8:], hi)
return
}
func mm_set_epi32(e0, e1, e2, e3 uint32) (m __m128i) {
binary.LittleEndian.PutUint32(m.bytes[:], e0)
binary.LittleEndian.PutUint32(m.bytes[4:], e1)
binary.LittleEndian.PutUint32(m.bytes[8:], e2)
binary.LittleEndian.PutUint32(m.bytes[12:], e3)
return
}
func mm_and_si128(a, b __m128i) (m __m128i) {
for i := 0; i < 16; i++ {
m.bytes[i] = a.bytes[i] & b.bytes[i]
}
return
}
func mm_andnot_si128(a, b __m128i) (m __m128i) {
for i := 0; i < 16; i++ {
m.bytes[i] = (^a.bytes[i]) & b.bytes[i]
}
return
}
func mm_shuffle_epi8(a, b __m128i) (m __m128i) {
for i := 0; i < 16; i++ {
if b.bytes[i]&0x80 == 0x80 {
m.bytes[i] = 0
} else {
idx := b.bytes[i] & 0x0f
m.bytes[i] = a.bytes[idx]
}
}
return
}
func mm_srli_epi32(a __m128i, imm8 byte) (m __m128i) {
e0 := binary.LittleEndian.Uint32(a.bytes[:])
e1 := binary.LittleEndian.Uint32(a.bytes[4:])
e2 := binary.LittleEndian.Uint32(a.bytes[8:])
e3 := binary.LittleEndian.Uint32(a.bytes[12:])
if imm8 > 31 {
e0 = 0
e1 = 0
e2 = 0
e3 = 0
} else {
e0 = e0 >> imm8
e1 = e1 >> imm8
e2 = e2 >> imm8
e3 = e3 >> imm8
}
binary.LittleEndian.PutUint32(m.bytes[:], e0)
binary.LittleEndian.PutUint32(m.bytes[4:], e1)
binary.LittleEndian.PutUint32(m.bytes[8:], e2)
binary.LittleEndian.PutUint32(m.bytes[12:], e3)
return
}
func mm_slli_epi32(a __m128i, imm8 byte) (m __m128i) {
e0 := binary.LittleEndian.Uint32(a.bytes[:])
e1 := binary.LittleEndian.Uint32(a.bytes[4:])
e2 := binary.LittleEndian.Uint32(a.bytes[8:])
e3 := binary.LittleEndian.Uint32(a.bytes[12:])
if imm8 > 31 {
e0 = 0
e1 = 0
e2 = 0
e3 = 0
} else {
e0 = e0 << imm8
e1 = e1 << imm8
e2 = e2 << imm8
e3 = e3 << imm8
}
binary.LittleEndian.PutUint32(m.bytes[:], e0)
binary.LittleEndian.PutUint32(m.bytes[4:], e1)
binary.LittleEndian.PutUint32(m.bytes[8:], e2)
binary.LittleEndian.PutUint32(m.bytes[12:], e3)
return
}
func mm_srli_epi64(a __m128i, imm8 byte) (m __m128i) {
lo := binary.LittleEndian.Uint64(a.bytes[:])
hi := binary.LittleEndian.Uint64(a.bytes[8:])
if imm8 > 63 {
lo = 0
hi = 0
} else {
lo = lo >> imm8
hi = hi >> imm8
}
binary.LittleEndian.PutUint64(m.bytes[:], lo)
binary.LittleEndian.PutUint64(m.bytes[8:], hi)
return
}
func xor(a, b __m128i) (m __m128i) {
for i := 0; i < 16; i++ {
m.bytes[i] = a.bytes[i] ^ b.bytes[i]
}
return
}
func mm_aesenclast_si128(a, rk __m128i) (m __m128i) {
// ShiftRows
s := mm_shuffle_epi8(a, shift_row)
// SubBytes
for i := 0; i < 16; i++ {
s.bytes[i] = aes_sbox[s.bytes[i]]
}
// State XOR RoundKey
s = xor(s, rk)
m.bytes = s.bytes
return m
}
type sm4_sbox_fn func(uint32, __m128i, __m128i, __m128i, __m128i) __m128i
func sm4_box_ref(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
rk128 := mm_set_epi32(rk, rk, rk, rk)
x := xor(xor(t1, t2), t3)
x = xor(x, rk128)
for i := 0; i < 16; i++ {
x.bytes[i] = sm4_sbox[x.bytes[i]]
}
return x
}
func sm4_box_aesenclast(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, const_0f)
x = mm_shuffle_epi8(x, shift_row_inv)
y = mm_andnot_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
}
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_shuffle_epi8(x, intelmaskSrows)
x = mm_aesenclast_si128(x, intelenckey)
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
}
func sm4_box_aesenclast_3(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesenclast_intel(rk, t0, t1, t2, t3, intelm1l, intelm1h, intelm2l, intelm2h)
}
func sm4_box_aesenclast_1(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesenclast(rk, t0, t1, t2, t3, m1l, m1h, m2l, m2h)
}
func sm4_box_aesenclast_2(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesenclast(rk, t0, t1, t2, t3, m3l, m3h, m4l, m4h)
}
func sm4_box_aesbox_1(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)
var y __m128i
for i := 0; i < 16; i++ {
y.bytes[i] = a1l.bytes[x.bytes[i]&0xf] ^ a1h.bytes[x.bytes[i]>>4]
}
x = y
for i := 0; i < 16; i++ {
x.bytes[i] = aes_sbox[x.bytes[i]] ^ 0xf
}
for i := 0; i < 16; i++ {
y.bytes[i] = a2l.bytes[(^x.bytes[i])&0xf] ^ a2h.bytes[x.bytes[i]>>4]
}
return y
}
func sm4_box_aesbox_1_1(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_1(rk, t0, t1, t2, t3, m1l, m1h, m2l, m2h)
}
func sm4_box_aesbox_1_2(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_1(rk, t0, t1, t2, t3, m3l, m3h, m4l, m4h)
}
func sm4_box_aesbox_2(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)
for i := 0; i < 16; i++ {
v := x.bytes[i]
v = a1l.bytes[v&0xf] ^ a1h.bytes[v>>4]
v = aes_sbox[v] ^ 0xf
v = a2l.bytes[^v&0xf] ^ a2h.bytes[v>>4]
x.bytes[i] = v
}
return x
}
func sm4_box_aesbox_2_1(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_2(rk, t0, t1, t2, t3, m1l, m1h, m2l, m2h)
}
func sm4_box_aesbox_2_2(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_2(rk, t0, t1, t2, t3, m3l, m3h, m4l, m4h)
}
func sm4_box_aesbox_3(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)
for i := 0; i < 16; i++ {
v := x.bytes[i]
v = a1l.bytes[v&0xf] ^ a1h.bytes[v>>4]
v = aes_sbox[v]
v = a2l.bytes[v&0xf] ^ a2h.bytes[v>>4]
x.bytes[i] = v
}
return x
}
func sm4_box_aesbox_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)
for i := 0; i < 16; i++ {
v := x.bytes[i]
v = a1l.bytes[v&0xf] ^ a1h.bytes[v>>4]
v = aes_sbox[v] ^ 0x63
v = a2l.bytes[v&0xf] ^ a2h.bytes[v>>4]
x.bytes[i] = v
}
return x
}
func sm4_box_aesbox_3_1(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_3(rk, t0, t1, t2, t3, m1l, m1h, m2l, m2h)
}
func sm4_box_aesbox_3_2(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_3(rk, t0, t1, t2, t3, m3l, m3h, m4l, m4h)
}
func sm4_box_aesbox_3_intel(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_intel(rk, t0, t1, t2, t3, intelm1l, intelm1h, intelm2l, intelm2h)
}
func sm4_box_aesbox_4(rk uint32, t0, t1, t2, t3 __m128i, a1_table, a2_table [256]byte) __m128i {
rk128 := mm_set_epi32(rk, rk, rk, rk)
x := xor(xor(t1, t2), t3)
x = xor(x, rk128)
for i := 0; i < 16; i++ {
v := x.bytes[i]
v = a1_table[v]
v = aes_sbox[v]
v = a2_table[v]
x.bytes[i] = v
}
return x
}
func sm4_box_aesbox_4_1(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_4(rk, t0, t1, t2, t3, m1_box, m2_box)
}
func sm4_box_aesbox_4_2(rk uint32, t0, t1, t2, t3 __m128i) __m128i {
return sm4_box_aesbox_4(rk, t0, t1, t2, t3, m3_box, m4_box)
}
// Encrypt 4 blocks (64 bytes) in ECB mode
func sm4_enc(rk [32]uint32, in [64]byte, out *[64]byte, fn sm4_sbox_fn) {
t0 := mm_set_epi32(binary.BigEndian.Uint32(in[:]), binary.BigEndian.Uint32(in[16:]), binary.BigEndian.Uint32(in[32:]), binary.BigEndian.Uint32(in[48:]))
t1 := mm_set_epi32(binary.BigEndian.Uint32(in[4:]), binary.BigEndian.Uint32(in[20:]), binary.BigEndian.Uint32(in[36:]), binary.BigEndian.Uint32(in[52:]))
t2 := mm_set_epi32(binary.BigEndian.Uint32(in[8:]), binary.BigEndian.Uint32(in[24:]), binary.BigEndian.Uint32(in[40:]), binary.BigEndian.Uint32(in[56:]))
t3 := mm_set_epi32(binary.BigEndian.Uint32(in[12:]), binary.BigEndian.Uint32(in[28:]), binary.BigEndian.Uint32(in[44:]), binary.BigEndian.Uint32(in[60:]))
for i := 0; i < 32; i++ {
x := fn(rk[i], t0, t1, t2, t3)
// 4 parallel L1 linear transforms
y := xor(x, mm_shuffle_epi8(x, r08))
y = xor(y, mm_shuffle_epi8(x, r16))
y = xor(mm_slli_epi32(y, 2), mm_srli_epi32(y, 30))
x = xor(xor(x, y), mm_shuffle_epi8(x, r24))
// rotate registers
x = xor(x, t0)
t0 = t1
t1 = t2
t2 = t3
t3 = x
}
binary.LittleEndian.PutUint32(out[:], binary.BigEndian.Uint32(t3.bytes[:]))
binary.LittleEndian.PutUint32(out[16:], binary.BigEndian.Uint32(t3.bytes[4:]))
binary.LittleEndian.PutUint32(out[32:], binary.BigEndian.Uint32(t3.bytes[8:]))
binary.LittleEndian.PutUint32(out[48:], binary.BigEndian.Uint32(t3.bytes[12:]))
binary.LittleEndian.PutUint32(out[4:], binary.BigEndian.Uint32(t2.bytes[:]))
binary.LittleEndian.PutUint32(out[20:], binary.BigEndian.Uint32(t2.bytes[4:]))
binary.LittleEndian.PutUint32(out[36:], binary.BigEndian.Uint32(t2.bytes[8:]))
binary.LittleEndian.PutUint32(out[52:], binary.BigEndian.Uint32(t2.bytes[12:]))
binary.LittleEndian.PutUint32(out[8:], binary.BigEndian.Uint32(t1.bytes[:]))
binary.LittleEndian.PutUint32(out[24:], binary.BigEndian.Uint32(t1.bytes[4:]))
binary.LittleEndian.PutUint32(out[40:], binary.BigEndian.Uint32(t1.bytes[8:]))
binary.LittleEndian.PutUint32(out[56:], binary.BigEndian.Uint32(t1.bytes[12:]))
binary.LittleEndian.PutUint32(out[12:], binary.BigEndian.Uint32(t0.bytes[:]))
binary.LittleEndian.PutUint32(out[28:], binary.BigEndian.Uint32(t0.bytes[4:]))
binary.LittleEndian.PutUint32(out[44:], binary.BigEndian.Uint32(t0.bytes[8:]))
binary.LittleEndian.PutUint32(out[60:], binary.BigEndian.Uint32(t0.bytes[12:]))
}
func print_blocks(blocks *[64]byte) {
for i := 0; i < 64; i++ {
fmt.Printf("0x%02x, ", blocks[i])
if i > 0 && (i+1)%16 == 0 {
fmt.Println()
}
}
}
func ref_case_1(fn sm4_sbox_fn) {
var rk = [32]uint32{
0xF12186F9, 0x41662B61, 0x5A6AB19A, 0x7BA92077, 0x367360F4, 0x776A0C61, 0xB6BB89B3, 0x24763151,
0xA520307C, 0xB7584DBD, 0xC30753ED, 0x7EE55B57, 0x6988608C, 0x30D895B7, 0x44BA14AF, 0x104495A1,
0xD120B428, 0x73B55FA3, 0xCC874966, 0x92244439, 0xE89E641F, 0x98CA015A, 0xC7159060, 0x99E1FD2E,
0xB79BD80C, 0x1D2115B0, 0x0E228AEB, 0xF1780C81, 0x428D3654, 0x62293496, 0x01CF72E5, 0x9124A012,
}
var in, out [64]byte
in[0] = 0x01
in[1] = 0x23
in[2] = 0x45
in[3] = 0x67
in[4] = 0x89
in[5] = 0xAB
in[6] = 0xCD
in[7] = 0xEF
in[8] = 0xFE
in[9] = 0xDC
in[10] = 0xBA
in[11] = 0x98
in[12] = 0x76
in[13] = 0x54
in[14] = 0x32
in[15] = 0x10
copy(in[16:], in[:16])
copy(in[32:], in[:16])
copy(in[48:], in[:16])
sm4_enc(rk, in, &out, fn)
fmt.Println("input=")
print_blocks(&in)
fmt.Println("output=")
print_blocks(&out)
}
func main() {
fmt.Println("SM4 with AESENCLAST 1")
ref_case_1(sm4_box_aesenclast_1)
fmt.Println()
fmt.Println("SM4 with AESENCLAST 2")
ref_case_1(sm4_box_aesenclast_2)
fmt.Println()
fmt.Println("SM4 with AESENCLAST 3: Intel method")
ref_case_1(sm4_box_aesenclast_3)
fmt.Println()
fmt.Println("SM4 with AESBOX 1 1")
ref_case_1(sm4_box_aesbox_1_1)
fmt.Println()
fmt.Println("SM4 with AESBOX 1 2")
ref_case_1(sm4_box_aesbox_1_2)
fmt.Println()
fmt.Println("SM4 with AESBOX 2 1")
ref_case_1(sm4_box_aesbox_2_1)
fmt.Println()
fmt.Println("SM4 with AESBOX 2 2")
ref_case_1(sm4_box_aesbox_2_2)
fmt.Println()
fmt.Println("SM4 with AESBOX 3 1")
ref_case_1(sm4_box_aesbox_3_1)
fmt.Println()
fmt.Println("SM4 with AESBOX 3 2")
ref_case_1(sm4_box_aesbox_3_2)
fmt.Println()
fmt.Println("SM4 with AESBOX 3 Intel")
ref_case_1(sm4_box_aesbox_3_intel)
fmt.Println()
fmt.Println("SM4 with AESBOX 4 1")
ref_case_1(sm4_box_aesbox_4_1)
fmt.Println()
fmt.Println("SM4 with AESBOX 4 2")
ref_case_1(sm4_box_aesbox_4_2)
fmt.Println()
fmt.Println("SM4 STANDARD")
ref_case_1(sm4_box_ref)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment