github.com/emmansun/gmsm@v0.29.1/sm9/sm9_key_test.go (about) 1 package sm9 2 3 import ( 4 "crypto/rand" 5 "encoding/hex" 6 "encoding/pem" 7 "testing" 8 9 "golang.org/x/crypto/cryptobyte" 10 cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1" 11 ) 12 13 func TestSignMasterPrivateKeyMarshalASN1(t *testing.T) { 14 masterKey, err := GenerateSignMasterKey(rand.Reader) 15 if err != nil { 16 t.Fatal(err) 17 } 18 der, err := masterKey.MarshalASN1() 19 if err != nil { 20 t.Fatal(err) 21 } 22 masterKey2 := new(SignMasterPrivateKey) 23 err = masterKey2.UnmarshalASN1(der) 24 if err != nil { 25 t.Fatal(err) 26 } 27 if masterKey.D.Cmp(masterKey2.D) != 0 { 28 t.Errorf("expected %v, got %v", hex.EncodeToString(masterKey.D.Bytes()), hex.EncodeToString(masterKey2.D.Bytes())) 29 } 30 } 31 32 func TestSignMasterPublicKeyMarshalASN1(t *testing.T) { 33 masterKey, err := GenerateSignMasterKey(rand.Reader) 34 if err != nil { 35 t.Fatal(err) 36 } 37 der, err := masterKey.Public().MarshalASN1() 38 if err != nil { 39 t.Fatal(err) 40 } 41 pub2 := new(SignMasterPublicKey) 42 err = pub2.UnmarshalASN1(der) 43 if err != nil { 44 t.Fatal(err) 45 } 46 if !masterKey.MasterPublicKey.Equal(pub2.MasterPublicKey) { 47 t.Errorf("not same") 48 } 49 } 50 51 func TestSignMasterPublicKeyMarshalCompressedASN1(t *testing.T) { 52 masterKey, err := GenerateSignMasterKey(rand.Reader) 53 if err != nil { 54 t.Fatal(err) 55 } 56 der, err := masterKey.Public().MarshalCompressedASN1() 57 if err != nil { 58 t.Fatal(err) 59 } 60 pub2 := new(SignMasterPublicKey) 61 err = pub2.UnmarshalASN1(der) 62 if err != nil { 63 t.Fatal(err) 64 } 65 if !masterKey.MasterPublicKey.Equal(pub2.MasterPublicKey) { 66 t.Errorf("not same") 67 } 68 } 69 70 func TestSignUserPrivateKeyMarshalASN1(t *testing.T) { 71 masterKey, err := GenerateSignMasterKey(rand.Reader) 72 uid := []byte("emmansun") 73 hid := byte(0x01) 74 if err != nil { 75 t.Fatal(err) 76 } 77 userKey, err := masterKey.GenerateUserKey(uid, hid) 78 if err != nil { 79 t.Fatal(err) 80 } 81 der, err := userKey.MarshalASN1() 82 if err != nil { 83 t.Fatal(err) 84 } 85 userKey2 := new(SignPrivateKey) 86 err = userKey2.UnmarshalASN1(der) 87 if err != nil { 88 t.Fatal(err) 89 } 90 if !userKey.PrivateKey.Equal(userKey2.PrivateKey) { 91 t.Errorf("not same") 92 } 93 } 94 95 func TestSignUserPrivateKeyMarshalCompressedASN1(t *testing.T) { 96 masterKey, err := GenerateSignMasterKey(rand.Reader) 97 uid := []byte("emmansun") 98 hid := byte(0x01) 99 if err != nil { 100 t.Fatal(err) 101 } 102 userKey, err := masterKey.GenerateUserKey(uid, hid) 103 if err != nil { 104 t.Fatal(err) 105 } 106 der, err := userKey.MarshalCompressedASN1() 107 if err != nil { 108 t.Fatal(err) 109 } 110 userKey2 := new(SignPrivateKey) 111 err = userKey2.UnmarshalASN1(der) 112 if err != nil { 113 t.Fatal(err) 114 } 115 if !userKey.PrivateKey.Equal(userKey2.PrivateKey) { 116 t.Errorf("not same") 117 } 118 } 119 120 func TestEncryptMasterPrivateKeyMarshalASN1(t *testing.T) { 121 masterKey, err := GenerateEncryptMasterKey(rand.Reader) 122 if err != nil { 123 t.Fatal(err) 124 } 125 der, err := masterKey.MarshalASN1() 126 if err != nil { 127 t.Fatal(err) 128 } 129 masterKey2 := new(EncryptMasterPrivateKey) 130 err = masterKey2.UnmarshalASN1(der) 131 if err != nil { 132 t.Fatal(err) 133 } 134 if masterKey.D.Cmp(masterKey2.D) != 0 { 135 t.Errorf("expected %v, got %v", hex.EncodeToString(masterKey.D.Bytes()), hex.EncodeToString(masterKey2.D.Bytes())) 136 } 137 } 138 139 func TestEncryptMasterPublicKeyMarshalASN1(t *testing.T) { 140 masterKey, err := GenerateEncryptMasterKey(rand.Reader) 141 if err != nil { 142 t.Fatal(err) 143 } 144 der, err := masterKey.Public().MarshalASN1() 145 if err != nil { 146 t.Fatal(err) 147 } 148 pub2 := new(EncryptMasterPublicKey) 149 err = pub2.UnmarshalASN1(der) 150 if err != nil { 151 t.Fatal(err) 152 } 153 if !masterKey.MasterPublicKey.Equal(pub2.MasterPublicKey) { 154 t.Errorf("not same") 155 } 156 } 157 158 func TestEncryptMasterPublicKeyMarshalCompressedASN1(t *testing.T) { 159 masterKey, err := GenerateEncryptMasterKey(rand.Reader) 160 if err != nil { 161 t.Fatal(err) 162 } 163 der, err := masterKey.Public().MarshalCompressedASN1() 164 if err != nil { 165 t.Fatal(err) 166 } 167 pub2 := new(EncryptMasterPublicKey) 168 err = pub2.UnmarshalASN1(der) 169 if err != nil { 170 t.Fatal(err) 171 } 172 if !masterKey.MasterPublicKey.Equal(pub2.MasterPublicKey) { 173 t.Errorf("not same") 174 } 175 } 176 177 func TestEncryptUserPrivateKeyMarshalASN1(t *testing.T) { 178 masterKey, err := GenerateEncryptMasterKey(rand.Reader) 179 uid := []byte("emmansun") 180 hid := byte(0x01) 181 if err != nil { 182 t.Fatal(err) 183 } 184 userKey, err := masterKey.GenerateUserKey(uid, hid) 185 if err != nil { 186 t.Fatal(err) 187 } 188 der, err := userKey.MarshalASN1() 189 if err != nil { 190 t.Fatal(err) 191 } 192 userKey2 := new(EncryptPrivateKey) 193 err = userKey2.UnmarshalASN1(der) 194 if err != nil { 195 t.Fatal(err) 196 } 197 if !userKey.PrivateKey.Equal(userKey2.PrivateKey) { 198 t.Errorf("not same") 199 } 200 } 201 202 func TestEncryptUserPrivateKeyMarshalCompressedASN1(t *testing.T) { 203 masterKey, err := GenerateEncryptMasterKey(rand.Reader) 204 uid := []byte("emmansun") 205 hid := byte(0x01) 206 if err != nil { 207 t.Fatal(err) 208 } 209 userKey, err := masterKey.GenerateUserKey(uid, hid) 210 if err != nil { 211 t.Fatal(err) 212 } 213 der, err := userKey.MarshalCompressedASN1() 214 if err != nil { 215 t.Fatal(err) 216 } 217 userKey2 := new(EncryptPrivateKey) 218 err = userKey2.UnmarshalASN1(der) 219 if err != nil { 220 t.Fatal(err) 221 } 222 if !userKey.PrivateKey.Equal(userKey2.PrivateKey) { 223 t.Errorf("not same") 224 } 225 } 226 227 func BenchmarkGenerateSignPrivKey(b *testing.B) { 228 masterKey, err := GenerateSignMasterKey(rand.Reader) 229 uid := []byte("emmansun") 230 hid := byte(0x01) 231 if err != nil { 232 b.Fatal(err) 233 } 234 b.ReportAllocs() 235 b.ResetTimer() 236 237 for i := 0; i < b.N; i++ { 238 if _, err := masterKey.GenerateUserKey(uid, hid); err != nil { 239 b.Fatal(err) 240 } 241 } 242 } 243 244 func BenchmarkGenerateEncryptPrivKey(b *testing.B) { 245 masterKey, err := GenerateEncryptMasterKey(rand.Reader) 246 uid := []byte("emmansun") 247 hid := byte(0x01) 248 if err != nil { 249 b.Fatal(err) 250 } 251 b.ReportAllocs() 252 b.ResetTimer() 253 254 for i := 0; i < b.N; i++ { 255 if _, err := masterKey.GenerateUserKey(uid, hid); err != nil { 256 b.Fatal(err) 257 } 258 } 259 } 260 261 const sm9SignMasterPublicKeyFromGMSSL = `-----BEGIN SM9 SIGN MASTER PUBLIC KEY----- 262 MIGFA4GCAARvTUvk1ztAlmjlUK0kP3zdFEVHHr8HUL4sUbcnFoQPukP0AjurnySy 263 f1MY0Plzt4lZ5u0/6GC4zUjYEcjWiYV+bV9YCnOGVQAYfPr/a+4/alewf43qBJuX 264 Ri1gDhueE6gkoeZ4HHUu1wfhRbKRF8okwSO933f/ZSpLlYu1P7/ckw== 265 -----END SM9 SIGN MASTER PUBLIC KEY----- 266 ` 267 268 func TestParseSM9SignMasterPublicKey(t *testing.T) { 269 key := new(SignMasterPublicKey) 270 err := key.ParseFromPEM([]byte(sm9SignMasterPublicKeyFromGMSSL)) 271 if err != nil { 272 t.Fatal(err) 273 } 274 if key.MasterPublicKey == nil { 275 t.Errorf("not expected nil") 276 } 277 278 // create sign master public key PEM with cryptobyte 279 var b cryptobyte.Builder 280 bytes, _ := key.MarshalASN1() 281 b.AddASN1(cryptobyte_asn1.SEQUENCE, func(b *cryptobyte.Builder) { 282 b.AddBytes(bytes) 283 }) 284 data, err := b.Bytes() 285 if err != nil { 286 t.Fatal(err) 287 } 288 block := &pem.Block{Bytes: data, Type: "SM9 SIGN MASTER PUBLIC KEY"} 289 pemContent := string(pem.EncodeToMemory(block)) 290 291 if pemContent != sm9SignMasterPublicKeyFromGMSSL { 292 t.Fatalf("failed %s\n", pemContent) 293 } 294 } 295 296 const sm9EncMasterPublicKeyFromGMSSL = `-----BEGIN SM9 ENC MASTER PUBLIC KEY----- 297 MEQDQgAEUWC+GS/3JrpMJqH/ZBItUDROFg62fmY4HuU0kHlnK/trA/GBX/P+MH0P 298 tYwoUdCETdYJwxiKXlI1jytVTuuT2Q== 299 -----END SM9 ENC MASTER PUBLIC KEY----- 300 ` 301 302 func TestParseSM9EncryptMasterPublicKey(t *testing.T) { 303 key := new(EncryptMasterPublicKey) 304 err := key.ParseFromPEM([]byte(sm9EncMasterPublicKeyFromGMSSL)) 305 if err != nil { 306 t.Fatal(err) 307 } 308 if key.MasterPublicKey == nil { 309 t.Errorf("not expected nil") 310 } 311 312 // create encrypt master public key PEM with asn1 313 var b cryptobyte.Builder 314 315 b.AddASN1(cryptobyte_asn1.SEQUENCE, func(b *cryptobyte.Builder) { 316 b.AddASN1BitString(key.MasterPublicKey.MarshalUncompressed()) 317 }) 318 data, err := b.Bytes() 319 if err != nil { 320 t.Fatal(err) 321 } 322 if err != nil { 323 t.Fatal(err) 324 } 325 block := &pem.Block{Bytes: data, Type: "SM9 ENC MASTER PUBLIC KEY"} 326 pemContent := string(pem.EncodeToMemory(block)) 327 328 if pemContent != sm9EncMasterPublicKeyFromGMSSL { 329 t.Fatalf("failed %s\n", pemContent) 330 } 331 }