Skip to content

[ms-go1.24-support] Support serializing SymCrypt hash objects #280

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

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions evp.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,22 @@ func hashFuncToMD(fn func() hash.Hash) (C.GO_EVP_MD_PTR, error) {
return md, nil
}

// provider is an identifier for a known provider.
type provider uint8

const (
providerNone provider = iota
providerOSSLDefault
providerOSSLFIPS
providerSymCrypt
)

type hashAlgorithm struct {
md C.GO_EVP_MD_PTR
ch crypto.Hash
size int
blockSize int
marshallable bool
magic string
marshalledSize int
md C.GO_EVP_MD_PTR
ch crypto.Hash
size int
blockSize int
provider provider
}

// loadHash converts a crypto.Hash to a EVP_MD.
Expand All @@ -92,8 +100,6 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
hash.md = C.go_openssl_EVP_md4()
case crypto.MD5:
hash.md = C.go_openssl_EVP_md5()
hash.magic = md5Magic
hash.marshalledSize = md5MarshaledSize
case crypto.MD5SHA1:
if vMajor == 1 && vMinor == 0 {
// OpenSSL 1.0.2 does not support MD5SHA1.
Expand All @@ -103,35 +109,21 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
}
case crypto.SHA1:
hash.md = C.go_openssl_EVP_sha1()
hash.magic = sha1Magic
hash.marshalledSize = sha1MarshaledSize
case crypto.SHA224:
hash.md = C.go_openssl_EVP_sha224()
hash.magic = magic224
hash.marshalledSize = marshaledSize256
case crypto.SHA256:
hash.md = C.go_openssl_EVP_sha256()
hash.magic = magic256
hash.marshalledSize = marshaledSize256
case crypto.SHA384:
hash.md = C.go_openssl_EVP_sha384()
hash.magic = magic384
hash.marshalledSize = marshaledSize512
case crypto.SHA512:
hash.md = C.go_openssl_EVP_sha512()
hash.magic = magic512
hash.marshalledSize = marshaledSize512
case crypto.SHA512_224:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha512_224()
hash.magic = magic512_224
hash.marshalledSize = marshaledSize512
}
case crypto.SHA512_256:
if versionAtOrAbove(1, 1, 1) {
hash.md = C.go_openssl_EVP_sha512_256()
hash.magic = magic512_256
hash.marshalledSize = marshaledSize512
}
case crypto.SHA3_224:
if versionAtOrAbove(1, 1, 1) {
Expand Down Expand Up @@ -169,7 +161,25 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
hash.md = md
}
}
hash.marshallable = hash.magic != "" && isHashMarshallable(hash.md)

switch vMajor {
case 1:
hash.provider = providerOSSLDefault
case 3:
if prov := C.go_openssl_EVP_MD_get0_provider(hash.md); prov != nil {
switch C.GoString(C.go_openssl_OSSL_PROVIDER_get0_name(prov)) {
case "default":
hash.provider = providerOSSLDefault
case "fips":
hash.provider = providerOSSLFIPS
case "symcryptprovider":
hash.provider = providerSymCrypt
}
}
default:
panic(errUnsupportedVersion())
}

cacheMD.Store(ch, &hash)
return &hash
}
Expand Down
Loading
Loading