mirror of
https://github.com/emmansun/gmsm.git
synced 2025-04-26 04:06:18 +08:00
[SM4] gcmSm4Enc & gcmSm4Dec
This commit is contained in:
parent
93df5651e9
commit
6dde984da4
1688
sm4/gcm_amd64.s
1688
sm4/gcm_amd64.s
File diff suppressed because it is too large
Load Diff
123
sm4/gcm_amd64_test.go
Normal file
123
sm4/gcm_amd64_test.go
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
//go:build amd64
|
||||||
|
// +build amd64
|
||||||
|
|
||||||
|
package sm4
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createGcm() *gcmAsm {
|
||||||
|
key := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
|
||||||
|
c := sm4CipherAsm{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}, 4, 64}
|
||||||
|
expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0])
|
||||||
|
c1 := &sm4CipherGCM{c}
|
||||||
|
g := &gcmAsm{}
|
||||||
|
g.cipher = &c1.sm4CipherAsm
|
||||||
|
g.tagSize = 16
|
||||||
|
gcmSm4Init(&g.bytesProductTable, g.cipher.enc)
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
|
||||||
|
var sm4GCMTests = []struct {
|
||||||
|
plaintext string
|
||||||
|
}{
|
||||||
|
{ // case 0: < 16
|
||||||
|
"abcdefg",
|
||||||
|
},
|
||||||
|
{ // case 1: = 16
|
||||||
|
"abcdefgabcdefghg",
|
||||||
|
},
|
||||||
|
{ // case 2: > 16 , < 64
|
||||||
|
"abcdefgabcdefghgabcdefgabcdefghgaaa",
|
||||||
|
},
|
||||||
|
{ // case 3: = 64
|
||||||
|
"abcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghg",
|
||||||
|
},
|
||||||
|
{ // case 4: > 64, < 128
|
||||||
|
"abcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgaaa",
|
||||||
|
},
|
||||||
|
{ // case 5: = 128
|
||||||
|
"abcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghg",
|
||||||
|
},
|
||||||
|
{ // case 6: 227 > 128, < 256, 128 + 64 + 35
|
||||||
|
"abcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgaaa",
|
||||||
|
},
|
||||||
|
{ // case 7: = 256
|
||||||
|
"abcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghg",
|
||||||
|
},
|
||||||
|
{ // case 8: > 256, = 355
|
||||||
|
"abcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgabcdefgabcdefghgaaa",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func initCounter(i byte, counter *[16]byte) {
|
||||||
|
copy(counter[:], []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
|
||||||
|
counter[gcmBlockSize-1] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
func resetTag(tag *[16]byte) {
|
||||||
|
for j := 0; j < 16; j++ {
|
||||||
|
tag[j] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGcmSm4Enc(t *testing.T) {
|
||||||
|
var counter1, counter2 [16]byte
|
||||||
|
gcm := createGcm()
|
||||||
|
var tagOut1, tagOut2 [gcmTagSize]byte
|
||||||
|
|
||||||
|
for i, test := range sm4GCMTests {
|
||||||
|
initCounter(2, &counter1)
|
||||||
|
initCounter(1, &counter2)
|
||||||
|
|
||||||
|
gcmSm4Data(&gcm.bytesProductTable, []byte("emmansun"), &tagOut1)
|
||||||
|
out1 := make([]byte, len(test.plaintext)+gcm.tagSize)
|
||||||
|
gcm.counterCrypt(out1, []byte(test.plaintext), &counter1)
|
||||||
|
gcmSm4Data(&gcm.bytesProductTable, out1[:len(test.plaintext)], &tagOut1)
|
||||||
|
|
||||||
|
out2 := make([]byte, len(test.plaintext)+gcm.tagSize)
|
||||||
|
gcmSm4Data(&gcm.bytesProductTable, []byte("emmansun"), &tagOut2)
|
||||||
|
gcmSm4Enc(&gcm.bytesProductTable, out2, []byte(test.plaintext), &counter2, &tagOut2, gcm.cipher.enc)
|
||||||
|
if hex.EncodeToString(out1) != hex.EncodeToString(out2) {
|
||||||
|
t.Errorf("#%d: out expected %s, got %s", i, hex.EncodeToString(out1), hex.EncodeToString(out2))
|
||||||
|
}
|
||||||
|
if hex.EncodeToString(tagOut1[:]) != hex.EncodeToString(tagOut2[:]) {
|
||||||
|
t.Errorf("#%d: tag expected %s, got %s", i, hex.EncodeToString(tagOut1[:]), hex.EncodeToString(tagOut2[:]))
|
||||||
|
}
|
||||||
|
resetTag(&tagOut1)
|
||||||
|
resetTag(&tagOut2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGcmSm4Dec(t *testing.T) {
|
||||||
|
var counter1, counter2 [16]byte
|
||||||
|
gcm := createGcm()
|
||||||
|
var tagOut1, tagOut2 [gcmTagSize]byte
|
||||||
|
|
||||||
|
for i, test := range sm4GCMTests {
|
||||||
|
initCounter(2, &counter1)
|
||||||
|
initCounter(1, &counter2)
|
||||||
|
|
||||||
|
gcmSm4Data(&gcm.bytesProductTable, []byte("emmansun"), &tagOut1)
|
||||||
|
out1 := make([]byte, len(test.plaintext)+gcm.tagSize)
|
||||||
|
gcm.counterCrypt(out1, []byte(test.plaintext), &counter1)
|
||||||
|
gcmSm4Data(&gcm.bytesProductTable, out1[:len(test.plaintext)], &tagOut1)
|
||||||
|
|
||||||
|
out1 = out1[:len(test.plaintext)]
|
||||||
|
|
||||||
|
out2 := make([]byte, len(test.plaintext)+gcm.tagSize)
|
||||||
|
gcmSm4Data(&gcm.bytesProductTable, []byte("emmansun"), &tagOut2)
|
||||||
|
gcmSm4Dec(&gcm.bytesProductTable, out2, out1, &counter2, &tagOut2, gcm.cipher.enc)
|
||||||
|
|
||||||
|
if hex.EncodeToString([]byte(test.plaintext)) != hex.EncodeToString(out2[:len(test.plaintext)]) {
|
||||||
|
t.Errorf("#%d: out expected %s, got %s", i, hex.EncodeToString([]byte(test.plaintext)), hex.EncodeToString(out2[:len(test.plaintext)]))
|
||||||
|
}
|
||||||
|
if hex.EncodeToString(tagOut1[:]) != hex.EncodeToString(tagOut2[:]) {
|
||||||
|
t.Errorf("#%d: tag expected %s, got %s", i, hex.EncodeToString(tagOut1[:]), hex.EncodeToString(tagOut2[:]))
|
||||||
|
}
|
||||||
|
resetTag(&tagOut1)
|
||||||
|
resetTag(&tagOut2)
|
||||||
|
}
|
||||||
|
}
|
@ -23,6 +23,12 @@ var _ gcmAble = (*sm4CipherGCM)(nil)
|
|||||||
//go:noescape
|
//go:noescape
|
||||||
func gcmSm4Init(productTable *[256]byte, rk []uint32)
|
func gcmSm4Init(productTable *[256]byte, rk []uint32)
|
||||||
|
|
||||||
|
//go:noescape
|
||||||
|
func gcmSm4Enc(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, rk []uint32)
|
||||||
|
|
||||||
|
//go:noescape
|
||||||
|
func gcmSm4Dec(productTable *[256]byte, dst, src []byte, ctr, T *[16]byte, rk []uint32)
|
||||||
|
|
||||||
//go:noescape
|
//go:noescape
|
||||||
func gcmSm4Data(productTable *[256]byte, data []byte, T *[16]byte)
|
func gcmSm4Data(productTable *[256]byte, data []byte, T *[16]byte)
|
||||||
|
|
||||||
@ -76,10 +82,8 @@ func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
g.cipher.Encrypt(tagMask[:], counter[:])
|
g.cipher.Encrypt(tagMask[:], counter[:])
|
||||||
gcmInc32(&counter)
|
|
||||||
|
|
||||||
var tagOut [gcmTagSize]byte
|
var tagOut [gcmTagSize]byte
|
||||||
|
|
||||||
gcmSm4Data(&g.bytesProductTable, data, &tagOut)
|
gcmSm4Data(&g.bytesProductTable, data, &tagOut)
|
||||||
|
|
||||||
ret, out := subtle.SliceForAppend(dst, len(plaintext)+g.tagSize)
|
ret, out := subtle.SliceForAppend(dst, len(plaintext)+g.tagSize)
|
||||||
@ -88,8 +92,7 @@ func (g *gcmAsm) Seal(dst, nonce, plaintext, data []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(plaintext) > 0 {
|
if len(plaintext) > 0 {
|
||||||
g.counterCrypt(out, plaintext, &counter)
|
gcmSm4Enc(&g.bytesProductTable, out, plaintext, &counter, &tagOut, g.cipher.enc)
|
||||||
gcmSm4Data(&g.bytesProductTable, out[:len(plaintext)], &tagOut)
|
|
||||||
}
|
}
|
||||||
gcmSm4Finish(&g.bytesProductTable, &tagMask, &tagOut, uint64(len(plaintext)), uint64(len(data)))
|
gcmSm4Finish(&g.bytesProductTable, &tagMask, &tagOut, uint64(len(plaintext)), uint64(len(data)))
|
||||||
copy(out[len(plaintext):], tagOut[:])
|
copy(out[len(plaintext):], tagOut[:])
|
||||||
@ -133,7 +136,6 @@ func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
g.cipher.Encrypt(tagMask[:], counter[:])
|
g.cipher.Encrypt(tagMask[:], counter[:])
|
||||||
gcmInc32(&counter)
|
|
||||||
|
|
||||||
var expectedTag [gcmTagSize]byte
|
var expectedTag [gcmTagSize]byte
|
||||||
gcmSm4Data(&g.bytesProductTable, data, &expectedTag)
|
gcmSm4Data(&g.bytesProductTable, data, &expectedTag)
|
||||||
@ -143,7 +145,7 @@ func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
|||||||
panic("cipher: invalid buffer overlap")
|
panic("cipher: invalid buffer overlap")
|
||||||
}
|
}
|
||||||
if len(ciphertext) > 0 {
|
if len(ciphertext) > 0 {
|
||||||
gcmSm4Data(&g.bytesProductTable, ciphertext, &expectedTag)
|
gcmSm4Dec(&g.bytesProductTable, out, ciphertext, &counter, &expectedTag, g.cipher.enc)
|
||||||
}
|
}
|
||||||
gcmSm4Finish(&g.bytesProductTable, &tagMask, &expectedTag, uint64(len(ciphertext)), uint64(len(data)))
|
gcmSm4Finish(&g.bytesProductTable, &tagMask, &expectedTag, uint64(len(ciphertext)), uint64(len(data)))
|
||||||
|
|
||||||
@ -153,8 +155,5 @@ func (g *gcmAsm) Open(dst, nonce, ciphertext, data []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
return nil, errOpen
|
return nil, errOpen
|
||||||
}
|
}
|
||||||
|
|
||||||
g.counterCrypt(out, ciphertext, &counter)
|
|
||||||
|
|
||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user