-
Notifications
You must be signed in to change notification settings - Fork 8
/
sm9_test.go
68 lines (50 loc) · 1.8 KB
/
sm9_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package gmssl
import (
"bytes"
"testing"
)
func TestSm9Enc(t *testing.T) {
master, _ := GenerateSm9EncMasterKey()
master.ExportEncryptedMasterKeyInfoPem("sm9enc.pem", "password")
master.ExportMasterPublicKeyPem("sm9encpub.pem")
master, _ = ImportEncryptedSm9EncMasterKeyInfoPem("sm9enc.pem", "password")
master_pub, _ := ImportSm9EncMasterPublicKeyPem("sm9encpub.pem")
id := "Alice"
key, _ := master.ExtractKey(id)
key.ExportEncryptedPrivateKeyInfoPem("sm9encpri.pem", "password")
key, _ = ImportEncryptedSm9EncPrivateKeyInfoPem("sm9encpri.pem", "password", id)
plaintext := []byte("plaintext")
ciphertext, _ := master_pub.Encrypt(plaintext, id)
decrypted, _ := key.Decrypt(ciphertext)
if bytes.Equal(decrypted, plaintext) != true {
t.Error("Test failure")
}
}
func TestSm9Sign(t *testing.T) {
master, _ := GenerateSm9SignMasterKey()
master.ExportEncryptedMasterKeyInfoPem("sm9sign.pem", "password")
master.ExportMasterPublicKeyPem("sm9signpub.pem")
master, _ = ImportEncryptedSm9SignMasterKeyInfoPem("sm9sign.pem", "password")
master_pub, _ := ImportSm9SignMasterPublicKeyPem("sm9signpub.pem")
id := "Alice"
key, _ := master.ExtractKey(id)
key.ExportEncryptedPrivateKeyInfoPem("sm9signpri.pem", "password")
key, _ = ImportEncryptedSm9SignPrivateKeyInfoPem("sm9signpri.pem", "password", id)
sign, _ := NewSm9Signature(true)
sign.Update([]byte("abc"))
signature, _ := sign.Sign(key)
sign, _ = NewSm9Signature(false)
sign.Update([]byte("abc"))
ret := sign.Verify(signature, master_pub, id)
if ret != true {
t.Error("Test failure")
}
}