| 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 sm4scip-go gomod github.com/tjfoc/gmsm . `github.com/tjfoc/gmsm/sm4`/ |
| 19 | |
| 20 | import ( |
| 21 | "bytesscip-go gomod github.com/golang/go/src go1.14 bytes/" |
| 22 | "fmtscip-go gomod github.com/golang/go/src go1.14 fmt/" |
| 23 | "testingscip-go gomod github.com/golang/go/src go1.14 testing/" |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | func TestSM4GCMscip-go gomod github.com/tjfoc/gmsm . `github.com/tjfoc/gmsm/sm4`/TestSM4GCM().(t *testingscip-go gomod github.com/golang/go/src go1.14 testing/.Tscip-go gomod github.com/golang/go/src go1.14 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,BlockSizescip-go gomod github.com/tjfoc/gmsm . `github.com/tjfoc/gmsm/sm4`/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:=Sm4GCMscip-go gomod github.com/tjfoc/gmsm . `github.com/tjfoc/gmsm/sm4`/Sm4GCM().(key,IV,data,A,true) |
| 38 | if err !=nil{ |
| 39 | t.Errorfscip-go gomod github.com/golang/go/src go1.14 testing/common#Errorf().("sm4 enc error:%s", err) |
| 40 | } |
| 41 | fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Printfscip-go gomod github.com/golang/go/src go1.14 fmt/Printf().("gcmMsg = %x\n", gcmMsg) |
| 42 | gcmDec,T_,err:=Sm4GCMscip-go gomod github.com/tjfoc/gmsm . `github.com/tjfoc/gmsm/sm4`/Sm4GCM().(key,IV,gcmMsg,A,false) |
| 43 | if err != nil{ |
| 44 | t.Errorfscip-go gomod github.com/golang/go/src go1.14 testing/common#Errorf().("sm4 dec error:%s", err) |
| 45 | } |
| 46 | fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Printfscip-go gomod github.com/golang/go/src go1.14 fmt/Printf().("gcmDec = %x\n", gcmDec) |
| 47 | if bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Comparescip-go gomod github.com/golang/go/src go1.14 bytes/Compare().(T,T_)==0{ |
| 48 | fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.14 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=Sm4GCMscip-go gomod github.com/tjfoc/gmsm . `github.com/tjfoc/gmsm/sm4`/Sm4GCM().(key,IV,gcmMsg,A ,false) |
| 53 | if err != nil{ |
| 54 | t.Errorfscip-go gomod github.com/golang/go/src go1.14 testing/common#Errorf().("sm4 dec error:%s", err) |
| 55 | } |
| 56 | if bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Comparescip-go gomod github.com/golang/go/src go1.14 bytes/Compare().(T,T_)!=0{ |
| 57 | fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.14 fmt/Println().("authentication failed") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | } |