github.com/Hyperledger-TWGC/tjfoc-gm@v1.4.0/sm4/sm4_gcm_test.go (about) 1 /* 2 Copyright Hyperledger-TWGC All Rights Reserved. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 15 writed by Zhiwei Yan, 2020 Oct 16 */ 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{ // the length of the A can be random 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 //Failed Test : if we input the different A , that will be a falied result. 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 }