登录
首页 >  Golang >  Go问答

如何验证我的 golang 应用程序是否使用了 Boringcrypto 而非本机的 golang crypto?

来源:stackoverflow

时间:2024-02-07 17:54:22 120浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《如何验证我的 golang 应用程序是否使用了 Boringcrypto 而非本机的 golang crypto?》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

上下文:我正在阅读多篇有关使我的 golang 应用程序符合 fips 规范的文章(换句话说,使我的应用程序使用 boringcrypto 而不是本机 golang 加密):

  • https://kupczynski.info/posts/fips-golang/
  • https://developers.redhat.com/articles/2022/05/31/your-go-application-fips-company#

简而言之,他们都说要跑

# build a binary and assert that it uses boringcrypto instead of the native golang crypto
run goexperiment=boringcrypto go build . && \
    go tool nm fips-echo-server > tags.txt && \
    grep '_cfunc__goboringcrypto_' tags.txt 1> /dev/null

然后期望以下输出:

e70fa0 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_aes_cbc_encrypt
  e70fc0 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_aes_ctr128_encrypt
  e70ff0 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_aes_decrypt
  e71000 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_aes_encrypt
  e71010 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_aes_set_decrypt_key
  e71050 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_aes_set_encrypt_key
  e71200 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_bn_bn2le_padded
  e71240 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_bn_free
  e71250 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_bn_le2bn
  e71300 t _cgo_d3bdb93f8e25_cfunc__goboringcrypto_bn_new

这将包含 _goboringcrypto_ (这意味着应用程序使用boringcrypto而不是本机golang加密)或空输出(这意味着应用程序使用本机golang加密而不是boringcrypto)。

但是,添加 goexperiment=boringcrypto 时我的应用程序的输出是:

➜  goexperiment=boringcrypto cgo_enabled=0 go build -o ./bin/app
➜  go tool nm bin/dapp | grep -i boring | cat 
 11230a0 t crypto/internal/boring.(*privatekeyecdh).publickey
 1123060 t crypto/internal/boring.(*publickeyecdh).bytes
 243fba0 d crypto/internal/boring..inittask
 243eda0 d crypto/internal/boring/bbig..inittask
 1060bc0 t crypto/internal/boring/bcache.registercache
 24efe14 b crypto/internal/boring/fipstls.required
 1123040 t crypto/internal/boring/sig.standardcrypto.abi0
 1219c00 t crypto/x509.boringallowcert
 24bfae0 b runtime.boringcaches
➜  go version bin/app                                                     
bin/app: go1.20.1 x:boringcrypto

_goboringcrypto_ 有 0 条匹配,但有 crypto/internal/boring

当我打开 crypto/internal/boring 的文档时,我可以看到:

package boring provides access to boringcrypto implementation functions. check the constant enabled to find out whether boringcrypto is available. if boringcrypto is not available, the functions in this package all panic.

基于此,我有两个简单的问题:

  1. 即使没有 __goboringcrypto_ 的匹配项,我的输出在语义上是否等效?换句话说,它是否确认我的应用程序使用boringcrypto而不是本机golang加密货币?
  2. 我正在搜索有关 fips 合规性的其他文章,其中一些确实提到使用 cgo_enabled=1 (但我正在使用 cgo_enabled=0)。使用 1.20.1 版本的 golang 还需要吗?
go 1.19 and higher:
starting with go 1.19, you can simply add [build_goexperiment=boringcrypto][18] and some related arguments to enable integrating boringcrypto for standard go.
make container \
  build_goexperiment=boringcrypto \
  build_cgo_enabled=1 \
  build_extra_go_ldflags="-linkmode=external -extldflags=-static"

也就是说,这篇文章说

since go 1.19 ... pass goexperiment=boringcrypto to the go tool during build time. as simple as that.

并且根本没有提及 cgo_enabled 标志。

我只能在 go 1.18 及更早版本部分看到 cgo 的提及:

go 1.18 and earlier
the build must have cgo enabled.

更新

  1. 我发现另一篇文章暗示即使对于 golang 1.19 版本,cgo_enabled=1 仍然是必要的。

  2. 上一条引用的文章,指向goversion

Also, you can use the program rsc.io/goversion. It will report the crypto implementation used by a given binary when invoked with the -crypto flag.

有一个有趣的 pr,暗示 _cfunc__goboringcrypto_ 相当于 crypto/internal/boring/sig.boringcrypto

另请参阅 goversion/version/read.go 文件:

也就是说,我的输出有 crypto/internal/boring/sig.standardcrypto 而不是 crypto/internal/boring/sig.boringcrypto

我的结论:

总而言之,如果应用程序具有以下任何一项

  • _cfunc__goboringcrypto_
  • crypto/internal/boring/sig.boringcrypto

go 工具 nm 命令的输出中,这意味着应用程序使用 boringcrypto,如果有

  • crypto/internal/boring/sig.standardcrypto

在其输出中,这意味着应用程序使用本机 golang 加密。


正确答案


总的来说,

如果应用程序具有以下任何一项

  • _Cfunc__goboringcrypto_

  • crypto/internal/boring/sig.BoringCrypto

go 工具 nm 命令的输出中,这意味着应用程序使用 boringcrypto 并且如果有

  • crypto/internal/boring/sig.StandardCrypto

在其输出中,这意味着应用程序使用原生 golang 加密

具体回答 2 个问题,

即使没有 _goboringcrypto 的匹配项,我的输出在语义上是否等效?换句话说,它是否确认我的应用程序使用boringcrypto而不是本机golang加密货币?

它不是,因为它包含 crypto/internal/boring/sig.StandardCrypto 而不是 _Cfunc__goboringcrypto_ / crypto/internal/boring/sig.BoringCrypto

我正在搜索有关 FIPS 合规性的其他文章,其中一些确实提到使用 CGO_ENABLED=1(但我正在使用 CGO_ENABLED=0)。使用 1.20.1 版本的 golang 还需要这样吗?

是的。

感谢大家的帮助!

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《如何验证我的 golang 应用程序是否使用了 Boringcrypto 而非本机的 golang crypto?》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>