microsoft / go-crypto-openssl

Go crypto backend for Linux using OpenSSL
MIT License
61 stars 15 forks source link

Panic when using the library #59

Closed thanhpp closed 1 year ago

thanhpp commented 1 year ago

My code panic when I try to use the library but I don't know how to fix it. Can someone help?

Go ENV

$  go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/thanhpp/go/bin"
GOCACHE="/home/thanhpp/.cache/go-build"
GOENV="/home/thanhpp/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/thanhpp/go/pkg/mod"
GONOPROXY="github.com/KyberNetwork,github.com/nri4nudge"
GONOSUMDB="github.com/KyberNetwork,github.com/nri4nudge"
GOOS="linux"
GOPATH="/home/thanhpp/go"
GOPROXY="proxy.golang.org,sum.golang.org,index.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build820668886=/tmp/go-build -gno-record-gcc-switches"

OpenSSL Version

 $  openssl version
OpenSSL 3.1.1 30 May 2023 (Library: OpenSSL 3.1.1 30 May 2023)

Example code

package main

import (
    "fmt"

    "github.com/microsoft/go-crypto-openssl/openssl"
)

func main() {
    fmt.Println(openssl.SHA1([]byte("abc")))
}

Error log

$  go run .
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x0]

runtime stack:
runtime.throw({0x4ad78a?, 0xffffffffffffffb8?})
    /usr/local/go/src/runtime/panic.go:1047 +0x5d fp=0x7ffdd49b7500 sp=0x7ffdd49b74d0 pc=0x437f7d
runtime.sigpanic()
    /usr/local/go/src/runtime/signal_unix.go:825 +0x3e9 fp=0x7ffdd49b7560 sp=0x7ffdd49b7500 pc=0x44ca69

goroutine 1 [syscall]:
runtime.cgocall(0x48a910, 0xc000060ed0)
    /usr/local/go/src/runtime/cgocall.go:157 +0x5c fp=0xc000060ea8 sp=0xc000060e70 pc=0x40a15c
github.com/microsoft/go-crypto-openssl/openssl._Cfunc_go_openssl_EVP_sha1()
    _cgo_gotypes.go:1519 +0x49 fp=0xc000060ed0 sp=0xc000060ea8 pc=0x4891c9
github.com/microsoft/go-crypto-openssl/openssl.SHA1({0xc000060f49, 0x3, 0x3})
    /home/thanhpp/go/pkg/mod/github.com/microsoft/go-crypto-openssl@v0.2.8/openssl/sha.go:36 +0x38 fp=0xc000060f18 sp=0xc000060ed0 pc=0x489598
main.main()
    /home/thanhpp/go/src/test-microsoft-openssl/main.go:10 +0x36 fp=0xc000060f80 sp=0xc000060f18 pc=0x489836
runtime.main()
    /usr/local/go/src/runtime/proc.go:250 +0x207 fp=0xc000060fe0 sp=0xc000060f80 pc=0x43a8a7
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000060fe8 sp=0xc000060fe0 pc=0x465141

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
    /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000052fb0 sp=0xc000052f90 pc=0x43acd6
runtime.goparkunlock(...)
    /usr/local/go/src/runtime/proc.go:387
runtime.forcegchelper()
    /usr/local/go/src/runtime/proc.go:305 +0xb0 fp=0xc000052fe0 sp=0xc000052fb0 pc=0x43ab10
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000052fe8 sp=0xc000052fe0 pc=0x465141
created by runtime.init.6
    /usr/local/go/src/runtime/proc.go:293 +0x25

goroutine 18 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
    /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc00004e780 sp=0xc00004e760 pc=0x43acd6
runtime.goparkunlock(...)
    /usr/local/go/src/runtime/proc.go:387
runtime.bgsweep(0x0?)
    /usr/local/go/src/runtime/mgcsweep.go:278 +0x8e fp=0xc00004e7c8 sp=0xc00004e780 pc=0x42796e
runtime.gcenable.func1()
    /usr/local/go/src/runtime/mgc.go:178 +0x26 fp=0xc00004e7e0 sp=0xc00004e7c8 pc=0x41ce26
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc00004e7e8 sp=0xc00004e7e0 pc=0x465141
created by runtime.gcenable
    /usr/local/go/src/runtime/mgc.go:178 +0x6b

goroutine 19 [GC scavenge wait]:
runtime.gopark(0xc000096000?, 0x4c3fd0?, 0x1?, 0x0?, 0x0?)
    /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc00004ef70 sp=0xc00004ef50 pc=0x43acd6
runtime.goparkunlock(...)
    /usr/local/go/src/runtime/proc.go:387
runtime.(*scavengerState).park(0x542bc0)
    /usr/local/go/src/runtime/mgcscavenge.go:400 +0x53 fp=0xc00004efa0 sp=0xc00004ef70 pc=0x425893
runtime.bgscavenge(0x0?)
    /usr/local/go/src/runtime/mgcscavenge.go:628 +0x45 fp=0xc00004efc8 sp=0xc00004efa0 pc=0x425e65
runtime.gcenable.func2()
    /usr/local/go/src/runtime/mgc.go:179 +0x26 fp=0xc00004efe0 sp=0xc00004efc8 pc=0x41cdc6
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc00004efe8 sp=0xc00004efe0 pc=0x465141
created by runtime.gcenable
    /usr/local/go/src/runtime/mgc.go:179 +0xaa

goroutine 20 [finalizer wait]:
runtime.gopark(0x1a0?, 0x543000?, 0x20?, 0xa8?, 0xc000052770?)
    /usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000052628 sp=0xc000052608 pc=0x43acd6
runtime.runfinq()
    /usr/local/go/src/runtime/mfinal.go:193 +0x107 fp=0xc0000527e0 sp=0xc000052628 pc=0x41be67
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000527e8 sp=0xc0000527e0 pc=0x465141
created by runtime.createfing
    /usr/local/go/src/runtime/mfinal.go:163 +0x45
exit status 2
dagood commented 1 year ago

You need to call openssl.Init() first to let the package find/load OpenSSL: https://pkg.go.dev/github.com/microsoft/go-crypto-openssl/openssl#Init

The repo doesn't have an example of how to use the module standalone (other than the test code) and it requires manual initialization because it's primarily designed to be used in a fork of the Go toolset as an alternative for standard library crypto.

If you don't mind me asking, what are your plans with this module, rather than using the Go standard library crypto? Just want to get a better sense what people might be using the module for and if it's worth making improvements to usability in standalone situations. 🙂

(For future reference: we're planning to migrate to https://github.com/golang-fips/openssl, which will behave mostly the same as this repo once it's ready. In the future, we'll be helping maintain the project over there as a shared resource rather than here in microsoft/go-crypto-openssl.)

thanhpp commented 1 year ago

You need to call openssl.Init() first to let the package find/load OpenSSL: https://pkg.go.dev/github.com/microsoft/go-crypto-openssl/openssl#Init

The repo doesn't have an example of how to use the module standalone (other than the test code) and it requires manual initialization because it's primarily designed to be used in a fork of the Go toolset as an alternative for standard library crypto.

If you don't mind me asking, what are your plans with this module, rather than using the Go standard library crypto? Just want to get a better sense what people might be using the module for and if it's worth making improvements to usability in standalone situations. slightly_smiling_face

(For future reference: we're planning to migrate to https://github.com/golang-fips/openssl, which will behave mostly the same as this repo once it's ready. In the future, we'll be helping maintain the project over there as a shared resource rather than here in microsoft/go-crypto-openssl.)

Thank you for getting back to me. I found benchmarks of the openssl library written in C with the standard crypto package in the spacemonkeygo/openssl repository. But it is no longer maintained and not compatible with openssl version 3.x. Then I found this library and want to try it if I can improve my application's performance.