Skip to content

Commit 6200bfa

Browse files
authored
Support serializing SymCrypt hash objects (#278)
* support serializing SymCrypt hash objects * fix function signatures * fix tests * fix tests * fix cgoless build * fix headers * fix sha512 buffer length * return err * pass magic as parameter * simplify * reduce diffs
1 parent 0d4c1bf commit 6200bfa

File tree

11 files changed

+837
-310
lines changed

11 files changed

+837
-310
lines changed

const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const ( //checkheader:ignore
5050
// KDF names
5151
_OSSL_KDF_NAME_HKDF cString = "HKDF\x00"
5252
_OSSL_KDF_NAME_PBKDF2 cString = "PBKDF2\x00"
53-
_OSSL_KDF_NAME_TLS1_PRF cString = "TLS1-PRF\x00"
53+
_OSSL_KDF_NAME_TLS1_PRF cString = "TLS1-PRF\x00"
5454
_OSSL_KDF_NAME_TLS13_KDF cString = "TLS13-KDF\x00"
5555
_OSSL_MAC_NAME_HMAC cString = "HMAC\x00"
5656

evp.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,22 @@ func hashFuncToMD(fn func() hash.Hash) (ossl.EVP_MD_PTR, error) {
6363
return md, nil
6464
}
6565

66+
// provider is an identifier for a known provider.
67+
type provider uint8
68+
69+
const (
70+
providerNone provider = iota
71+
providerOSSLDefault
72+
providerOSSLFIPS
73+
providerSymCrypt
74+
)
75+
6676
type hashAlgorithm struct {
6777
md ossl.EVP_MD_PTR
6878
ch crypto.Hash
6979
size int
7080
blockSize int
81+
provider provider
7182
marshallable bool
7283
magic string
7384
marshalledSize int
@@ -87,14 +98,14 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
8798
hash.md = ossl.EVP_md4()
8899
case crypto.MD5:
89100
hash.md = ossl.EVP_md5()
90-
hash.magic = md5Magic
91-
hash.marshalledSize = md5MarshaledSize
101+
hash.magic = magicMD5
102+
hash.marshalledSize = marshaledSizeMD5
92103
case crypto.MD5SHA1:
93104
hash.md = ossl.EVP_md5_sha1()
94105
case crypto.SHA1:
95106
hash.md = ossl.EVP_sha1()
96-
hash.magic = sha1Magic
97-
hash.marshalledSize = sha1MarshaledSize
107+
hash.magic = magic1
108+
hash.marshalledSize = marshaledSize1
98109
case crypto.SHA224:
99110
hash.md = ossl.EVP_sha224()
100111
hash.magic = magic224
@@ -159,7 +170,34 @@ func loadHash(ch crypto.Hash) *hashAlgorithm {
159170
hash.md = md
160171
}
161172
}
162-
hash.marshallable = hash.magic != "" && isHashMarshallable(hash.md)
173+
if hash.magic != "" {
174+
if hash.marshalledSize == 0 {
175+
panic("marshalledSize must be set for " + hash.magic)
176+
}
177+
}
178+
179+
switch vMajor {
180+
case 1:
181+
hash.provider = providerOSSLDefault
182+
case 3:
183+
if prov := ossl.EVP_MD_get0_provider(hash.md); prov != nil {
184+
cname := ossl.OSSL_PROVIDER_get0_name(prov)
185+
switch C.GoString((*C.char)(unsafe.Pointer(cname))) {
186+
case "default":
187+
hash.provider = providerOSSLDefault
188+
hash.marshallable = hash.magic != ""
189+
case "fips":
190+
hash.provider = providerOSSLFIPS
191+
hash.marshallable = hash.magic != ""
192+
case "symcryptprovider":
193+
hash.provider = providerSymCrypt
194+
hash.marshallable = hash.magic != "" && isSymCryptHashStateSerializable(hash.md)
195+
}
196+
}
197+
default:
198+
panic(errUnsupportedVersion())
199+
}
200+
163201
cacheMD.Store(ch, &hash)
164202
return &hash
165203
}

0 commit comments

Comments
 (0)