Skip to content

Commit

Permalink
Unittest with ECDHE-SM2-WITH-SM4-SM3 (#11)
Browse files Browse the repository at this point in the history
* readme: quick start

* refactor: put algo into independent package crypto

* unit test with ECDHE-SM2-WITH-SM4-SM3 client

* use new test certs

* fix gitignore

* fix go vet workflow
  • Loading branch information
itomsawyer committed Oct 9, 2023
1 parent 7ba1d11 commit b650229
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 153 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*.so
*.dylib

# Temp files for IDEs
*.exrc

# Test binary, built with `go test -c`
*.test

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# tongsuo-go-sdk
tongsuo bindings for Go

# quick start

```
git clone https://github.com/Tongsuo-Project/Tongsuo.git tongsuo
```

```
cd tongsuo && ./config --prefix=/opt/tongsuo -Wl,-rpath,/opt/tongsuo/lib enable-ssl-trace enable-ec_elgamal enable-ntls && make -j && make install
```

```
go test -exec "env LD_LIBRARY_PATH=/opt/tongsuo/lib" ./...
```
48 changes: 25 additions & 23 deletions md5.go → crypto/md5/md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,73 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tongsuogo
package md5

// #include "shim.h"
// #include "../../shim.h"
import "C"

import (
"errors"
"hash"
"runtime"
"unsafe"

tongsuogo "github.com/tongsuo-project/tongsuo-go-sdk"
)

const (
MD5_DIGEST_LENGTH = 16
MD5_CBLOCK = 64
)

var _ hash.Hash = new(MD5Hash)
var _ hash.Hash = new(MD5)

type MD5Hash struct {
type MD5 struct {
ctx *C.EVP_MD_CTX
engine *Engine
engine *tongsuogo.Engine
}

func NewMD5Hash() (*MD5Hash, error) { return NewMD5HashWithEngine(nil) }
func New() (*MD5, error) { return NewWithEngine(nil) }

func NewMD5HashWithEngine(e *Engine) (*MD5Hash, error) {
h, err := newMD5HashWithEngine(e)
func NewWithEngine(e *tongsuogo.Engine) (*MD5, error) {
h, err := newMD5WithEngine(e)
if err != nil {
return nil, err
}
h.Reset()
return h, nil
}

func newMD5HashWithEngine(e *Engine) (*MD5Hash, error) {
hash := &MD5Hash{engine: e}
func newMD5WithEngine(e *tongsuogo.Engine) (*MD5, error) {
hash := &MD5{engine: e}
hash.ctx = C.X_EVP_MD_CTX_new()
if hash.ctx == nil {
return nil, errors.New("openssl: md5: unable to allocate ctx")
}
runtime.SetFinalizer(hash, func(hash *MD5Hash) { hash.Close() })
runtime.SetFinalizer(hash, func(hash *MD5) { hash.Close() })
return hash, nil
}

func (s *MD5Hash) BlockSize() int {
func (s *MD5) BlockSize() int {
return MD5_CBLOCK
}

func (s *MD5Hash) Size() int {
func (s *MD5) Size() int {
return MD5_DIGEST_LENGTH
}

func (s *MD5Hash) Close() {
func (s *MD5) Close() {
if s.ctx != nil {
C.X_EVP_MD_CTX_free(s.ctx)
s.ctx = nil
}
}

func (s *MD5Hash) Reset() {
C.X_EVP_DigestInit_ex(s.ctx, C.X_EVP_md5(), engineRef(s.engine))
func (s *MD5) Reset() {
C.X_EVP_DigestInit_ex(s.ctx, C.X_EVP_md5(), (*C.ENGINE)(s.engine.Engine()))
}

func (s *MD5Hash) Write(p []byte) (n int, err error) {
func (s *MD5) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
Expand All @@ -86,26 +88,26 @@ func (s *MD5Hash) Write(p []byte) (n int, err error) {
return len(p), nil
}

func (s *MD5Hash) Sum(in []byte) []byte {
hash, err := NewMD5HashWithEngine(s.engine)
func (s *MD5) Sum(in []byte) []byte {
hash, err := NewWithEngine(s.engine)
if err != nil {
panic("NewMD5Hash fail " + err.Error())
panic("New fail " + err.Error())
}

if C.X_EVP_MD_CTX_copy_ex(hash.ctx, s.ctx) == 0 {
panic("NewMD5Hash X_EVP_MD_CTX_copy_ex fail")
panic("New X_EVP_MD_CTX_copy_ex fail")
}

result := hash.checkSum()
return append(in, result[:]...)
}

func (s *MD5Hash) checkSum() (result [MD5_DIGEST_LENGTH]byte) {
func (s *MD5) checkSum() (result [MD5_DIGEST_LENGTH]byte) {
C.X_EVP_DigestFinal_ex(s.ctx, (*C.uchar)(unsafe.Pointer(&result[0])), nil)
return result
}

func MD5Sum(data []byte) (result [MD5_DIGEST_LENGTH]byte) {
func Sum(data []byte) (result [MD5_DIGEST_LENGTH]byte) {
C.X_EVP_Digest(
unsafe.Pointer(&data[0]),
C.size_t(len(data)),
Expand Down
12 changes: 6 additions & 6 deletions md5_test.go → crypto/md5/md5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tongsuogo
package md5

import (
"crypto/md5"
Expand All @@ -31,7 +31,7 @@ func TestMD5(t *testing.T) {
var got, expected [MD5_DIGEST_LENGTH]byte

s := md5.Sum(buf)
got = MD5Sum(buf)
got = Sum(buf)
copy(expected[:], s[:MD5_DIGEST_LENGTH])

if expected != got {
Expand All @@ -41,7 +41,7 @@ func TestMD5(t *testing.T) {
}

func TestMD5Writer(t *testing.T) {
ohash, err := NewMD5Hash()
ohash, err := New()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -88,23 +88,23 @@ func benchmarkMD5(b *testing.B, length int64, fn md5func) {
}

func BenchmarkMD5Large_openssl(b *testing.B) {
benchmarkMD5(b, 1024*1024, func(buf []byte) { MD5Sum(buf) })
benchmarkMD5(b, 1024*1024, func(buf []byte) { Sum(buf) })
}

func BenchmarkMD5Large_stdlib(b *testing.B) {
benchmarkMD5(b, 1024*1024, func(buf []byte) { md5.Sum(buf) })
}

func BenchmarkMD5Normal_openssl(b *testing.B) {
benchmarkMD5(b, 1024, func(buf []byte) { MD5Sum(buf) })
benchmarkMD5(b, 1024, func(buf []byte) { Sum(buf) })
}

func BenchmarkMD5Normal_stdlib(b *testing.B) {
benchmarkMD5(b, 1024, func(buf []byte) { md5.Sum(buf) })
}

func BenchmarkMD5Small_openssl(b *testing.B) {
benchmarkMD5(b, 1, func(buf []byte) { MD5Sum(buf) })
benchmarkMD5(b, 1, func(buf []byte) { Sum(buf) })
}

func BenchmarkMD5Small_stdlib(b *testing.B) {
Expand Down
25 changes: 10 additions & 15 deletions sha1.go → crypto/sha1/sha1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,27 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tongsuogo
package sha1

// #include "shim.h"
// #include "../../shim.h"
import "C"

import (
"errors"
"runtime"
"unsafe"

tongsuogo "github.com/tongsuo-project/tongsuo-go-sdk"
)

type SHA1Hash struct {
ctx *C.EVP_MD_CTX
engine *Engine
engine *tongsuogo.Engine
}

func NewSHA1Hash() (*SHA1Hash, error) { return NewSHA1HashWithEngine(nil) }
func New() (*SHA1Hash, error) { return NewWithEngine(nil) }

func NewSHA1HashWithEngine(e *Engine) (*SHA1Hash, error) {
func NewWithEngine(e *tongsuogo.Engine) (*SHA1Hash, error) {
hash := &SHA1Hash{engine: e}
hash.ctx = C.X_EVP_MD_CTX_new()
if hash.ctx == nil {
Expand All @@ -50,15 +52,8 @@ func (s *SHA1Hash) Close() {
}
}

func engineRef(e *Engine) *C.ENGINE {
if e == nil {
return nil
}
return e.e
}

func (s *SHA1Hash) Reset() error {
if 1 != C.X_EVP_DigestInit_ex(s.ctx, C.X_EVP_sha1(), engineRef(s.engine)) {
if 1 != C.X_EVP_DigestInit_ex(s.ctx, C.X_EVP_sha1(), (*C.ENGINE)(s.engine.Engine())) {
return errors.New("openssl: sha1: cannot init digest ctx")
}
return nil
Expand All @@ -83,8 +78,8 @@ func (s *SHA1Hash) Sum() (result [20]byte, err error) {
return result, s.Reset()
}

func SHA1(data []byte) (result [20]byte, err error) {
hash, err := NewSHA1Hash()
func Sum(data []byte) (result [20]byte, err error) {
hash, err := New()
if err != nil {
return result, err
}
Expand Down
10 changes: 5 additions & 5 deletions sha1_test.go → crypto/sha1/sha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tongsuogo
package sha1

import (
"crypto/rand"
Expand All @@ -29,7 +29,7 @@ func TestSHA1(t *testing.T) {
}

expected := sha1.Sum(buf)
got, err := SHA1(buf)
got, err := Sum(buf)
if err != nil {
t.Fatal(err)
}
Expand All @@ -41,7 +41,7 @@ func TestSHA1(t *testing.T) {
}

func TestSHA1Writer(t *testing.T) {
ohash, err := NewSHA1Hash()
ohash, err := New()
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -93,15 +93,15 @@ func benchmarkSHA1(b *testing.B, length int64, fn shafunc) {
}

func BenchmarkSHA1Large_openssl(b *testing.B) {
benchmarkSHA1(b, 1024*1024, func(buf []byte) { SHA1(buf) })
benchmarkSHA1(b, 1024*1024, func(buf []byte) { Sum(buf) })
}

func BenchmarkSHA1Large_stdlib(b *testing.B) {
benchmarkSHA1(b, 1024*1024, func(buf []byte) { sha1.Sum(buf) })
}

func BenchmarkSHA1Small_openssl(b *testing.B) {
benchmarkSHA1(b, 1, func(buf []byte) { SHA1(buf) })
benchmarkSHA1(b, 1, func(buf []byte) { Sum(buf) })
}

func BenchmarkSHA1Small_stdlib(b *testing.B) {
Expand Down
32 changes: 17 additions & 15 deletions sha256.go → crypto/sha256/sha256.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,54 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tongsuogo
package sha256

// #include "shim.h"
// #include "../../shim.h"
import "C"

import (
"errors"
"runtime"
"unsafe"

tongsuogo "github.com/tongsuo-project/tongsuo-go-sdk"
)

type SHA256Hash struct {
type SHA256 struct {
ctx *C.EVP_MD_CTX
engine *Engine
engine *tongsuogo.Engine
}

func NewSHA256Hash() (*SHA256Hash, error) { return NewSHA256HashWithEngine(nil) }
func New() (*SHA256, error) { return NewWithEngine(nil) }

func NewSHA256HashWithEngine(e *Engine) (*SHA256Hash, error) {
hash := &SHA256Hash{engine: e}
func NewWithEngine(e *tongsuogo.Engine) (*SHA256, error) {
hash := &SHA256{engine: e}
hash.ctx = C.X_EVP_MD_CTX_new()
if hash.ctx == nil {
return nil, errors.New("openssl: sha256: unable to allocate ctx")
}
runtime.SetFinalizer(hash, func(hash *SHA256Hash) { hash.Close() })
runtime.SetFinalizer(hash, func(hash *SHA256) { hash.Close() })
if err := hash.Reset(); err != nil {
return nil, err
}
return hash, nil
}

func (s *SHA256Hash) Close() {
func (s *SHA256) Close() {
if s.ctx != nil {
C.X_EVP_MD_CTX_free(s.ctx)
s.ctx = nil
}
}

func (s *SHA256Hash) Reset() error {
if 1 != C.X_EVP_DigestInit_ex(s.ctx, C.X_EVP_sha256(), engineRef(s.engine)) {
func (s *SHA256) Reset() error {
if 1 != C.X_EVP_DigestInit_ex(s.ctx, C.X_EVP_sha256(), (*C.ENGINE)(s.engine.Engine())) {
return errors.New("openssl: sha256: cannot init digest ctx")
}
return nil
}

func (s *SHA256Hash) Write(p []byte) (n int, err error) {
func (s *SHA256) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
Expand All @@ -68,16 +70,16 @@ func (s *SHA256Hash) Write(p []byte) (n int, err error) {
return len(p), nil
}

func (s *SHA256Hash) Sum() (result [32]byte, err error) {
func (s *SHA256) Sum() (result [32]byte, err error) {
if 1 != C.X_EVP_DigestFinal_ex(s.ctx,
(*C.uchar)(unsafe.Pointer(&result[0])), nil) {
return result, errors.New("openssl: sha256: cannot finalize ctx")
}
return result, s.Reset()
}

func SHA256(data []byte) (result [32]byte, err error) {
hash, err := NewSHA256Hash()
func Sum(data []byte) (result [32]byte, err error) {
hash, err := New()
if err != nil {
return result, err
}
Expand Down
Loading

0 comments on commit b650229

Please sign in to comment.