Skip to content

Commit

Permalink
sm4: gcm purego optimize NewGCM
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Oct 30, 2024
1 parent ac075d8 commit 559da49
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
18 changes: 11 additions & 7 deletions sm4/gcm_amd64.s
Original file line number Diff line number Diff line change
Expand Up @@ -207,25 +207,29 @@ sm4InitEncLoop:
MOVQ $7, AX

initLoop:
// B0 * B2, Karatsuba Approach
MOVOU B2, T0
MOVOU B2, T1
MOVOU B3, T2
PCLMULQDQ $0x00, B0, T0
PCLMULQDQ $0x11, B0, T1
PCLMULQDQ $0x00, B1, T2
PCLMULQDQ $0x00, B0, T0 // B0[0] * B2[0]
PCLMULQDQ $0x11, B0, T1 // B0[1] * B2[1]
PCLMULQDQ $0x00, B1, T2 // (B0[0] + B0[1]) * (B2[0] + B2[1])

PXOR T0, T2
PXOR T1, T2
PXOR T0, T2 // (B0[0] + B0[1]) * (B2[0] + B2[1]) - B0[0] * B2[0]
PXOR T1, T2 // B0[0] * B2[1] + B0[1] * B2[0]
MOVOU T2, B4
PSLLDQ $8, B4
PSRLDQ $8, T2
PXOR B4, T0
PXOR T2, T1
PXOR T2, T1 // [T1, T0] = B0 * B2

// Fast reduction
// 1st reduction
MOVOU POLY, B2
PCLMULQDQ $0x01, T0, B2
PCLMULQDQ $0x01, T0, B2 // B2 = T0[0] * POLY[1]
PSHUFD $78, T0, T0
PXOR B2, T0
// 2nd reduction
MOVOU POLY, B2
PCLMULQDQ $0x01, T0, B2
PSHUFD $78, T0, T0
Expand Down
13 changes: 9 additions & 4 deletions sm4/gcm_cipher_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ func (c *sm4CipherAsm) NewGCM(nonceSize, tagSize int) (cipher.AEAD, error) {
binary.BigEndian.Uint64(key[:8]),
binary.BigEndian.Uint64(key[8:]),
}
g.productTable[reverseBits(1)] = x
g.productTable[8] = x // reverseBits(1) = 8

for i := 2; i < 16; i += 2 {
g.productTable[reverseBits(i)] = gcmDouble(&g.productTable[reverseBits(i/2)])
g.productTable[reverseBits(i+1)] = gcmAdd(&g.productTable[reverseBits(i)], &x)
for j := 4; j > 0; j /= 2 {
g.productTable[j] = gcmDouble(&g.productTable[j*2])
}

for j := 2; j < 16; j *= 2 {
for k := 1; k < j; k++ {
g.productTable[j+k] = gcmAdd(&g.productTable[j], &g.productTable[k])
}
}

return g, nil
Expand Down

0 comments on commit 559da49

Please sign in to comment.