From 4a2cb8015e2b728a064a6c2309c0b0101dd92b14 Mon Sep 17 00:00:00 2001 From: syl543 Date: Thu, 17 Sep 2020 21:09:21 +0800 Subject: [PATCH] implement Decrypter interface --- CHANGELOG.md | 9 ++++++--- sm2/sm2enc.go | 5 +++++ tls/conn.go | 1 + tls/gm_handshake_client.go | 2 +- tls/gm_handshake_client_double.go | 2 +- tls/gm_handshake_server.go | 2 +- tls/gm_handshake_server_double.go | 2 +- 7 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83bf961..b5971a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ ## 更新日志 -### master 更新(Sep 11, 2020): +### master 更新(Sep 17, 2020) +- 私钥新增Decrypt函数,实现crypto.Decrypter接口 + +### master 更新(Sep 11, 2020) - 新增导入导出接口 | 接口名 | 接口功能 | @@ -28,13 +31,13 @@ - 新增CHANGLOG文件 -### master 更新(Aug 13, 2020): +### master 更新(Aug 13, 2020) - 优化国密tls库,支持tls双国密证书通信。 ### master 更新(Aug 7, 2020) - 将中国网安的两个底层密码库合并到一起。 -### master 更新(Jul 17, 2020): +### master 更新(Jul 17, 2020) - 上传中国网安国密密码库。 diff --git a/sm2/sm2enc.go b/sm2/sm2enc.go index 3d6bfca..52f2979 100644 --- a/sm2/sm2enc.go +++ b/sm2/sm2enc.go @@ -6,6 +6,7 @@ package sm2 import ( "bytes" + "crypto" "encoding/binary" "errors" "io" @@ -18,6 +19,10 @@ import ( var EncryptionErr = errors.New("sm2: encryption error") var DecryptionErr = errors.New("sm2: decryption error") +func (key *PrivateKey) Decrypt(rand io.Reader, msg []byte, opts crypto.DecrypterOpts) (plaintext []byte, err error) { + return Decrypt(msg, key) +} + func keyDerivation(Z []byte, klen int) []byte { var ct = 1 if klen%8 != 0 { diff --git a/tls/conn.go b/tls/conn.go index d3676f8..1b555f9 100644 --- a/tls/conn.go +++ b/tls/conn.go @@ -1288,6 +1288,7 @@ func (c *Conn) Handshake() error { // If an error occurred during the hadshake try to flush the // alert that might be left in the buffer. c.flush() + fmt.Println("handshake error :", c.handshakeErr) } if c.handshakeErr == nil && !c.handshakeComplete() { diff --git a/tls/gm_handshake_client.go b/tls/gm_handshake_client.go index 0cc3c48..d5b5312 100644 --- a/tls/gm_handshake_client.go +++ b/tls/gm_handshake_client.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build sigle_cert +// +build single_cert package tls diff --git a/tls/gm_handshake_client_double.go b/tls/gm_handshake_client_double.go index 5885fe8..55920fc 100644 --- a/tls/gm_handshake_client_double.go +++ b/tls/gm_handshake_client_double.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !sigle_cert +// +build !single_cert package tls diff --git a/tls/gm_handshake_server.go b/tls/gm_handshake_server.go index 6cd581d..808efac 100644 --- a/tls/gm_handshake_server.go +++ b/tls/gm_handshake_server.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build sigle_cert +// +build single_cert package tls diff --git a/tls/gm_handshake_server_double.go b/tls/gm_handshake_server_double.go index 04da1e7..7cd301a 100644 --- a/tls/gm_handshake_server_double.go +++ b/tls/gm_handshake_server_double.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !sigle_cert +// +build !single_cert package tls