Skip to content

Commit 0bf146c

Browse files
committed
simplify
1 parent 66cbbca commit 0bf146c

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

providersymcrypt.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ func symCryptUnmarshalBinary(d []byte, chain, buffer []byte) _UINT64 {
105105
return newUINT64(length)
106106
}
107107

108+
// swapEndianessInt32 swaps the endianness of the given byte slice
109+
// in place. It assumes the slice is a backup of a 32-bit integer array.
110+
func swapEndianessInt32(d []uint8) {
111+
for i := 0; i < len(d); i += 4 {
112+
d[i], d[i+3] = d[i+3], d[i]
113+
d[i+1], d[i+2] = d[i+2], d[i+1]
114+
}
115+
116+
}
117+
108118
type _SYMCRYPT_MD5_STATE_EXPORT_BLOB struct {
109119
header _SYMCRYPT_BLOB_HEADER
110120
chain [16]uint8 // little endian
@@ -117,21 +127,13 @@ type _SYMCRYPT_MD5_STATE_EXPORT_BLOB struct {
117127
func (b *_SYMCRYPT_MD5_STATE_EXPORT_BLOB) appendBinary(d []byte) ([]byte, error) {
118128
// b.chain is little endian, but Go expects big endian,
119129
// we need to swap the bytes.
120-
for i := 0; i < len(b.chain); i += 4 {
121-
b.chain[i], b.chain[i+3] = b.chain[i+3], b.chain[i]
122-
b.chain[i+1], b.chain[i+2] = b.chain[i+2], b.chain[i+1]
123-
}
130+
swapEndianessInt32(b.chain[:])
124131
return symCryptAppendBinary(d, b.chain[:], b.buffer[:], b.length), nil
125132
}
126133

127134
func (b *_SYMCRYPT_MD5_STATE_EXPORT_BLOB) unmarshalBinary(d []byte) {
128135
b.length = symCryptUnmarshalBinary(d, b.chain[:], b.buffer[:])
129-
// b.chain should be little endian, but Go uses big endian,
130-
// we need to swap the bytes.
131-
for i := 0; i < len(b.chain); i += 4 {
132-
b.chain[i], b.chain[i+3] = b.chain[i+3], b.chain[i]
133-
b.chain[i+1], b.chain[i+2] = b.chain[i+2], b.chain[i+1]
134-
}
136+
swapEndianessInt32(b.chain[:])
135137
}
136138

137139
type _SYMCRYPT_SHA1_STATE_EXPORT_BLOB struct {
@@ -283,12 +285,8 @@ func symCryptHashUnmarshalBinary(ctx ossl.EVP_MD_CTX_PTR, ch crypto.Hash, magic
283285
if err != nil {
284286
return err
285287
}
286-
if _, err := ossl.EVP_MD_CTX_set_params(ctx, params); err != nil {
287-
// Old versions of SCOSSL don't support SCOSSL_DIGEST_PARAM_STATE
288-
// nor _SCOSSL_DIGEST_PARAM_RECOMPUTE_CHECKSUM.
289-
return errHashNotMarshallable
290-
}
291-
return nil
288+
_, err = ossl.EVP_MD_CTX_set_params(ctx, params)
289+
return err
292290
}
293291

294292
func symCryptHashStateInfo(ch crypto.Hash) (size, typ uint32, serializable bool) {

0 commit comments

Comments
 (0)