github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/sm4/sm4_test.go (about) 1 // Copyright (c) 2022 zhaochun 2 // gmgo is licensed under Mulan PSL v2. 3 // You can use this software according to the terms and conditions of the Mulan PSL v2. 4 // You may obtain a copy of Mulan PSL v2 at: 5 // http://license.coscl.org.cn/MulanPSL2 6 // THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 7 // See the Mulan PSL v2 for more details. 8 9 package sm4 10 11 import ( 12 "crypto/aes" 13 "crypto/cipher" 14 "encoding/hex" 15 "fmt" 16 "runtime" 17 "testing" 18 19 "gitee.com/zhaochuninhefei/zcgolog/zclog" 20 "golang.org/x/sys/cpu" 21 ) 22 23 func TestSm4(t *testing.T) { 24 key := []byte("1234567890abcdef") 25 data := []byte("天行健君子以自强不息") 26 27 fmt.Println("---------------- testCBC ----------------") 28 err := testCBC(key, data) 29 if err != nil { 30 t.Fatal(err) 31 } 32 33 fmt.Println("---------------- testCFB ----------------") 34 err = testCFB(key, data) 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 fmt.Println("---------------- testOFB ----------------") 40 err = testOFB(key, data) 41 if err != nil { 42 t.Fatal(err) 43 } 44 45 fmt.Println("---------------- testGCM ----------------") 46 err = testGCM(key, data) 47 if err != nil { 48 t.Fatal(err) 49 } 50 } 51 52 func TestAesGCM1(t *testing.T) { 53 key, _ := hex.DecodeString("c64b7140c02e9cbe38626ea772794f57") 54 iv, _ := hex.DecodeString("11b413b9f5757aa64a803152") 55 56 ciphertext, _ := hex.DecodeString("863286881f10c94e642c7694ac605aa3427a14a84f6a681c056b21770f1b9abe241a2ecee0b8c369ffe16ad42b50ced2abd0bc90a161979b0f793371ebd53e97") 57 58 block, err := aes.NewCipher(key) 59 if err != nil { 60 t.Error(err) 61 } 62 63 aesgcm, err := cipher.NewGCM(block) 64 if err != nil { 65 t.Error(err) 66 } 67 68 plaintext, err := aesgcm.Open(nil, iv, ciphertext, nil) 69 if err != nil { 70 t.Error(err) 71 } 72 t.Logf("plaintext: %s\n", plaintext) 73 } 74 75 func TestGcmAsmWithNonce(t *testing.T) { 76 zclog.Level = zclog.LOG_LEVEL_DEBUG 77 zclog.Debug("supportSM4:", supportSM4) 78 zclog.Debug("supportsAES:", supportsAES) 79 zclog.Debug("supportsGFMUL:", supportsGFMUL) 80 zclog.Debug("useAVX2:", useAVX2) 81 82 key := []byte{251, 160, 47, 88, 53, 110, 220, 7, 229, 174, 145, 250, 40, 34, 188, 237} 83 nonce := []byte{182, 244, 44, 22, 113, 249, 246, 127, 114, 94, 115, 60} 84 dst := []byte{23, 3, 3, 2, 191} 85 data := []byte{11, 0, 2, 170, 0, 0, 2, 166, 0, 2, 161, 48, 130, 2, 157, 48, 130, 2, 67, 160, 3, 2, 1, 2, 2, 17, 0, 179, 19, 43, 244, 221, 102, 20, 101, 125, 96, 139, 186, 249, 198, 195, 128, 48, 10, 6, 8, 42, 129, 28, 207, 85, 1, 131, 117, 48, 74, 49, 15, 48, 13, 6, 3, 85, 4, 10, 19, 6, 99, 97, 116, 101, 115, 116, 49, 20, 48, 18, 6, 3, 85, 4, 3, 19, 11, 99, 97, 46, 116, 101, 115, 116, 46, 99, 111, 109, 49, 11, 48, 9, 6, 3, 85, 4, 6, 19, 2, 67, 78, 49, 20, 48, 18, 6, 3, 85, 4, 8, 19, 11, 65, 110, 104, 117, 105, 32, 72, 101, 102, 101, 105, 48, 30, 23, 13, 50, 50, 48, 52, 49, 50, 48, 56, 53, 50, 48, 51, 90, 23, 13, 51, 50, 48, 52, 48, 57, 48, 57, 53, 50, 48, 51, 90, 48, 83, 49, 20, 48, 18, 6, 3, 85, 4, 10, 12, 11, 115, 101, 114, 118, 101, 114, 95, 116, 101, 115, 116, 49, 24, 48, 22, 6, 3, 85, 4, 3, 19, 15, 115, 101, 114, 118, 101, 114, 46, 116, 101, 115, 116, 46, 99, 111, 109, 49, 11, 48, 9, 6, 3, 85, 4, 6, 19, 2, 67, 78, 49, 20, 48, 18, 6, 3, 85, 4, 8, 19, 11, 65, 110, 104, 117, 105, 32, 72, 101, 102, 101, 105, 48, 90, 48, 20, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 6, 8, 42, 129, 28, 207, 85, 1, 130, 45, 3, 66, 0, 4, 208, 246, 86, 87, 22, 133, 125, 168, 54, 91, 20, 197, 65, 195, 72, 121, 155, 195, 153, 47, 205, 174, 4, 237, 184, 164, 199, 171, 193, 125, 196, 244, 152, 160, 152, 212, 105, 20, 101, 74, 231, 154, 254, 71, 47, 116, 38, 82, 17, 16, 177, 44, 237, 56, 187, 48, 26, 125, 243, 220, 27, 128, 205, 173, 163, 129, 255, 48, 129, 252, 48, 14, 6, 3, 85, 29, 15, 1, 1, 255, 4, 4, 3, 2, 6, 192, 48, 29, 6, 3, 85, 29, 37, 4, 22, 48, 20, 6, 8, 43, 6, 1, 5, 5, 7, 3, 1, 6, 8, 43, 6, 1, 5, 5, 7, 3, 2, 48, 41, 6, 3, 85, 29, 14, 4, 34, 4, 32, 211, 20, 37, 161, 114, 121, 43, 88, 162, 253, 161, 74, 105, 189, 203, 192, 67, 227, 69, 174, 129, 131, 172, 208, 91, 24, 210, 108, 207, 72, 20, 121, 48, 43, 6, 3, 85, 29, 35, 4, 36, 48, 34, 128, 32, 72, 47, 170, 202, 171, 110, 250, 70, 1, 121, 23, 136, 94, 115, 82, 88, 94, 97, 91, 98, 5, 106, 154, 74, 111, 55, 129, 6, 143, 58, 220, 191, 48, 115, 6, 3, 85, 29, 17, 4, 108, 48, 106, 130, 15, 115, 101, 114, 118, 101, 114, 46, 116, 101, 115, 116, 46, 99, 111, 109, 130, 16, 116, 101, 115, 116, 46, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109, 129, 17, 103, 111, 112, 104, 101, 114, 64, 103, 111, 108, 97, 110, 103, 46, 111, 114, 103, 135, 4, 127, 0, 0, 1, 135, 16, 32, 1, 72, 96, 0, 0, 32, 1, 0, 0, 0, 0, 0, 0, 0, 104, 134, 26, 104, 116, 116, 112, 115, 58, 47, 47, 102, 111, 111, 46, 99, 111, 109, 47, 119, 105, 98, 98, 108, 101, 35, 102, 111, 111, 48, 10, 6, 8, 42, 129, 28, 207, 85, 1, 131, 117, 3, 72, 0, 48, 69, 2, 32, 118, 163, 224, 17, 60, 183, 70, 62, 5, 158, 223, 251, 62, 186, 40, 120, 53, 145, 196, 225, 9, 235, 5, 251, 224, 133, 172, 205, 181, 237, 2, 51, 2, 33, 0, 215, 113, 160, 193, 183, 1, 187, 104, 101, 175, 88, 66, 195, 191, 53, 200, 235, 175, 0, 33, 224, 189, 75, 215, 130, 219, 162, 54, 11, 183, 170, 216, 0, 0, 22} 86 err := testGCMWithNonce(key, data, nonce, dst) 87 if err != nil { 88 t.Fatal(err) 89 } 90 } 91 92 func testGCMWithNonce(key, data, nonce, dst []byte) error { 93 encryptData, err := Sm4EncryptGcmWithNonce(data, key, nonce, dst) 94 if err != nil { 95 return err 96 } 97 fmt.Printf("GCM encryptData : %v\n", encryptData) 98 99 plainData, err := Sm4DecryptGcmWithNonce(encryptData, key, nonce, dst) 100 if err != nil { 101 return err 102 } 103 fmt.Printf("GCM plainData : %v\n", plainData) 104 return nil 105 } 106 107 func testCBC(key, data []byte) error { 108 iv, encryptData, err := Sm4EncryptCbc(data, key) 109 if err != nil { 110 return err 111 } 112 fmt.Printf("CBC iv 16进制 : %x\n", iv) 113 fmt.Printf("CBC encryptData 16进制 : %x\n", encryptData) 114 115 plainData, err := Sm4DecryptCbc(encryptData, key, iv) 116 if err != nil { 117 return err 118 } 119 fmt.Printf("CBC plainData : %s\n", plainData) 120 return nil 121 } 122 123 func testCFB(key, data []byte) error { 124 iv, encryptData, err := Sm4EncryptCfb(data, key) 125 if err != nil { 126 return err 127 } 128 fmt.Printf("CFB iv 16进制 : %x\n", iv) 129 fmt.Printf("CFB encryptData 16进制 : %x\n", encryptData) 130 131 plainData, err := Sm4DecryptCfb(encryptData, key, iv) 132 if err != nil { 133 return err 134 } 135 fmt.Printf("CFB plainData : %s\n", plainData) 136 return nil 137 } 138 139 func testOFB(key, data []byte) error { 140 iv, encryptData, err := Sm4EncryptOfb(data, key) 141 if err != nil { 142 return err 143 } 144 fmt.Printf("OFB iv 16进制 : %x\n", iv) 145 fmt.Printf("OFB encryptData 16进制 : %x\n", encryptData) 146 147 plainData, err := Sm4DecryptOfb(encryptData, key, iv) 148 if err != nil { 149 return err 150 } 151 fmt.Printf("OFB plainData : %s\n", plainData) 152 return nil 153 } 154 155 func testGCM(key, data []byte) error { 156 nonce, encryptData, err := Sm4EncryptGcm(data, key) 157 if err != nil { 158 return err 159 } 160 fmt.Printf("GCM nonce 16进制 : %x\n", nonce) 161 fmt.Printf("GCM encryptData 16进制 : %x\n", encryptData) 162 163 plainData, err := Sm4DecryptGcm(encryptData, key, nonce) 164 if err != nil { 165 return err 166 } 167 fmt.Printf("GCM plainData : %s\n", plainData) 168 return nil 169 } 170 171 func BenchmarkSm4(t *testing.B) { 172 key := []byte("1234567890abcdef") 173 data := []byte("天行健君子以自强不息") 174 t.ReportAllocs() 175 for i := 0; i < t.N; i++ { 176 nonce, encryptData, _ := Sm4EncryptGcm(data, key) 177 _, err := Sm4DecryptGcm(encryptData, key, nonce) 178 if err != nil { 179 t.Fatal(err) 180 } 181 } 182 } 183 184 func TestCheckArch(t *testing.T) { 185 zclog.Level = zclog.LOG_LEVEL_DEBUG 186 zclog.Debug("supportSM4:", supportSM4) 187 zclog.Debug("supportsAES:", supportsAES) 188 zclog.Debug("supportsGFMUL:", supportsGFMUL) 189 zclog.Debug("useAVX2:", useAVX2) 190 zclog.Debug("arch:", runtime.GOARCH) 191 zclog.Debug("cpu.X86.HasAVX2:", cpu.X86.HasAVX2) 192 zclog.Debug("cpu.X86.HasBMI2:", cpu.X86.HasBMI2) 193 }