Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only implement encoding.BinaryMarshaler for hashers provided by built-in providers #161

Merged
merged 6 commits into from
Sep 5, 2024

Conversation

qmuntal
Copy link
Collaborator

@qmuntal qmuntal commented Sep 3, 2024

We can only marshal/unmarshal the internal state of hashers provided by built-in providers, which are the only ones from which we know the memory layout.

This PR does some struct embedding tricks so the hashers returned byNewMD5, NewSHA1, NewSHA224, NewSHA256, NewSHA384, and NewSHA512, only implement the encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interface if the hashers are provided by the fips or the default provider.

Fixes #156.

Copy link
Contributor

@derekparker derekparker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

hash.go Outdated Show resolved Hide resolved
hash.go Outdated Show resolved Hide resolved
hash.go Outdated Show resolved Hide resolved
hash.go Outdated Show resolved Hide resolved
hash_test.go Outdated Show resolved Hide resolved
qmuntal and others added 3 commits September 4, 2024 09:17
@qmuntal qmuntal requested a review from dagood September 4, 2024 07:52
@@ -232,6 +235,9 @@ func (h *evpHash) sum(out []byte) {
// The EVP_MD_CTX memory layout has changed in OpenSSL 3
// and the property holding the internal structure is no longer md_data but algctx.
func (h *evpHash) hashState() unsafe.Pointer {
if !h.marshallable {
panic("openssl: hash state is not marshallable")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this ever come up from user code or OpenSSL oddity, or would there have to be a typo+missed test in golang-fips/openssl?

Wondering if it might make more sense to return this as a second value from newEvpHash rather than store it in the struct (at the cost of not having this runtime check).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There would have to be a typo+missed test, aka it is a programmer error, not something that should happen at runtime.

I initially returned that property as second value from newEvpHash, but that made all NewSHAX a bit less cleaner, and having the runtime check is a nice bonus. Also, with the current implementation, NewSHAX functions are inlinable, which can help escape analysis take better decisions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MarshalBinary and UnmarshalBinary can't be used with custom providers
3 participants