1
17
18 package sm4
19
20 import (
21 "bytes"
22 "fmt"
23 "testing"
24 )
25
26
27 func TestSM4GCM(t *testing.T){
28 key := []byte("1234567890abcdef")
29 data := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
30 IV :=make([]byte,BlockSize)
31 testA:=[][]byte{
32 []byte{},
33 []byte{0x01, 0x23, 0x45, 0x67, 0x89},
34 []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10},
35 }
36 for _,A:=range testA{
37 gcmMsg,T,err:=Sm4GCM(key,IV,data,A,true)
38 if err !=nil{
39 t.Errorf("sm4 enc error:%s", err)
40 }
41 fmt.Printf("gcmMsg = %x\n", gcmMsg)
42 gcmDec,T_,err:=Sm4GCM(key,IV,gcmMsg,A,false)
43 if err != nil{
44 t.Errorf("sm4 dec error:%s", err)
45 }
46 fmt.Printf("gcmDec = %x\n", gcmDec)
47 if bytes.Compare(T,T_)==0{
48 fmt.Println("authentication successed")
49 }
50
51 A= []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd}
52 gcmDec,T_,err=Sm4GCM(key,IV,gcmMsg,A ,false)
53 if err != nil{
54 t.Errorf("sm4 dec error:%s", err)
55 }
56 if bytes.Compare(T,T_)!=0{
57 fmt.Println("authentication failed")
58 }
59 }
60
61 }
View as plain text