Skip to content
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
Prev Previous commit
Next Next commit
pass magic as parameter
  • Loading branch information
qmuntal committed May 23, 2025
commit 66cbbca11f8863303454cefe1b62ba74be4a8f3f
11 changes: 6 additions & 5 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,15 @@ func (d *evpHash) MarshalBinary() ([]byte, error) {
func (d *evpHash) AppendBinary(buf []byte) ([]byte, error) {
defer runtime.KeepAlive(d)
d.init()
if magic, _ := cryptoHashEncodingInfo(d.alg.ch); magic == "" {
magic, _ := cryptoHashEncodingInfo(d.alg.ch)
if magic == "" {
return nil, errHashNotMarshallable
}
switch d.alg.provider {
case providerOSSLDefault, providerOSSLFIPS:
return osslHashAppendBinary(d.ctx, d.alg.ch, buf)
return osslHashAppendBinary(d.ctx, d.alg.ch, magic, buf)
case providerSymCrypt:
return symCryptHashAppendBinary(d.ctx, d.alg.ch, buf)
return symCryptHashAppendBinary(d.ctx, d.alg.ch, magic, buf)
default:
return nil, errHashNotMarshallable
}
Expand All @@ -420,9 +421,9 @@ func (d *evpHash) UnmarshalBinary(b []byte) error {
}
switch d.alg.provider {
case providerOSSLDefault, providerOSSLFIPS:
return osslHashUnmarshalBinary(d.ctx, d.alg.ch, b)
return osslHashUnmarshalBinary(d.ctx, d.alg.ch, magic, b)
case providerSymCrypt:
return symCryptHashUnmarshalBinary(d.ctx, d.alg.ch, b)
return symCryptHashUnmarshalBinary(d.ctx, d.alg.ch, magic, b)
default:
return errHashNotMarshallable
}
Expand Down
6 changes: 2 additions & 4 deletions provideropenssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,11 @@ func getOSSLDigetsContext(ctx ossl.EVP_MD_CTX_PTR) unsafe.Pointer {

var errHashStateInvalid = errors.New("openssl: can't retrieve hash state")

func osslHashAppendBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, buf []byte) ([]byte, error) {
func osslHashAppendBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, magic string, buf []byte) ([]byte, error) {
algctx := getOSSLDigetsContext(ctx)
if algctx == nil {
return nil, errHashStateInvalid
}
magic, _ := cryptoHashEncodingInfo(ch)
buf = append(buf, magic...)
switch ch {
case crypto.MD5:
Expand All @@ -215,12 +214,11 @@ func osslHashAppendBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, buf []byte) (
}
}

func osslHashUnmarshalBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, b []byte) error {
func osslHashUnmarshalBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, magic string, b []byte) error {
algctx := getOSSLDigetsContext(ctx)
if algctx == nil {
return errHashStateInvalid
}
magic, _ := cryptoHashEncodingInfo(ch)
b = b[len(magic):]
switch ch {
case crypto.MD5:
Expand Down
6 changes: 2 additions & 4 deletions providersymcrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (b *_SYMCRYPT_SHA512_STATE_EXPORT_BLOB) unmarshalBinary(d []byte) {
b.lengthL = symCryptUnmarshalBinary(d, b.chain[:], b.buffer[:])
}

func symCryptHashAppendBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, buf []byte) ([]byte, error) {
func symCryptHashAppendBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, magic string, buf []byte) ([]byte, error) {
size, typ, serializable := symCryptHashStateInfo(ch)
if !serializable {
return nil, errHashNotMarshallable
Expand Down Expand Up @@ -217,7 +217,6 @@ func symCryptHashAppendBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, buf []byt
return nil, errors.New("invalid blob type")
}

magic, _ := cryptoHashEncodingInfo(ch)
buf = append(buf, magic...)
switch ch {
case crypto.MD5:
Expand All @@ -237,7 +236,7 @@ func symCryptHashAppendBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, buf []byt
}
}

func symCryptHashUnmarshalBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, b []byte) error {
func symCryptHashUnmarshalBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, magic string, b []byte) error {
size, typ, serializable := symCryptHashStateInfo(ch)
if !serializable {
return errHashNotMarshallable
Expand All @@ -248,7 +247,6 @@ func symCryptHashUnmarshalBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, b []by
_type: typ,
}
var blobPtr unsafe.Pointer
magic, _ := cryptoHashEncodingInfo(ch)
b = b[len(magic):]
switch ch {
case crypto.MD5:
Expand Down
Loading