Skip to content

Commit 76269a7

Browse files
authored
[ms-go1.24-support] Support serializing SymCrypt hash objects (#280)
* support serializing SymCrypt hash objects * reduce diffs
1 parent 6020143 commit 76269a7

File tree

7 files changed

+751
-308
lines changed

7 files changed

+751
-308
lines changed

evp.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,22 @@ func hashFuncToMD(fn func() hash.Hash) (C.GO_EVP_MD_PTR, error) {
6868
return md, nil
6969
}
7070

71+
// provider is an identifier for a known provider.
72+
type provider uint8
73+
74+
const (
75+
providerNone provider = iota
76+
providerOSSLDefault
77+
providerOSSLFIPS
78+
providerSymCrypt
79+
)
80+
7181
type hashAlgorithm struct {
7282
md C.GO_EVP_MD_PTR
7383
ch crypto.Hash
7484
size int
7585
blockSize int
86+
provider provider
7687
marshallable bool
7788
magic string
7889
marshalledSize int
@@ -92,8 +103,8 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
92103
hash.md = C.go_openssl_EVP_md4()
93104
case crypto.MD5:
94105
hash.md = C.go_openssl_EVP_md5()
95-
hash.magic = md5Magic
96-
hash.marshalledSize = md5MarshaledSize
106+
hash.magic = magicMD5
107+
hash.marshalledSize = marshaledSizeMD5
97108
case crypto.MD5SHA1:
98109
if vMajor == 1 && vMinor == 0 {
99110
// OpenSSL 1.0.2 does not support MD5SHA1.
@@ -103,8 +114,8 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
103114
}
104115
case crypto.SHA1:
105116
hash.md = C.go_openssl_EVP_sha1()
106-
hash.magic = sha1Magic
107-
hash.marshalledSize = sha1MarshaledSize
117+
hash.magic = magic1
118+
hash.marshalledSize = marshaledSize1
108119
case crypto.SHA224:
109120
hash.md = C.go_openssl_EVP_sha224()
110121
hash.magic = magic224
@@ -169,7 +180,33 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
169180
hash.md = md
170181
}
171182
}
172-
hash.marshallable = hash.magic != "" && isHashMarshallable(hash.md)
183+
if hash.magic != "" {
184+
if hash.marshalledSize == 0 {
185+
panic("marshalledSize must be set for " + hash.magic)
186+
}
187+
}
188+
189+
switch vMajor {
190+
case 1:
191+
hash.provider = providerOSSLDefault
192+
case 3:
193+
if prov := C.go_openssl_EVP_MD_get0_provider(hash.md); prov != nil {
194+
switch C.GoString(C.go_openssl_OSSL_PROVIDER_get0_name(prov)) {
195+
case "default":
196+
hash.provider = providerOSSLDefault
197+
hash.marshallable = hash.magic != ""
198+
case "fips":
199+
hash.provider = providerOSSLFIPS
200+
hash.marshallable = hash.magic != ""
201+
case "symcryptprovider":
202+
hash.provider = providerSymCrypt
203+
hash.marshallable = hash.magic != "" && isSymCryptHashStateSerializable(hash.md)
204+
}
205+
}
206+
default:
207+
panic(errUnsupportedVersion())
208+
}
209+
173210
cacheMD.Store(ch, &hash)
174211
return &hash
175212
}

0 commit comments

Comments
 (0)