diff --git a/evp.go b/evp.go index 3eac4057..eca12af7 100644 --- a/evp.go +++ b/evp.go @@ -70,7 +70,6 @@ func cryptoHashToMD(ch crypto.Hash) (md C.GO_EVP_MD_PTR) { return C.go_openssl_EVP_md5_sha1() } } - sha3Defined := vMajor > 1 || (vMajor >= 1 && vMinor > 1) || (vMajor >= 1 && vMinor >= 1 && vPatch >= 1) switch ch { case crypto.MD4: return C.go_openssl_EVP_md4() @@ -86,20 +85,20 @@ func cryptoHashToMD(ch crypto.Hash) (md C.GO_EVP_MD_PTR) { return C.go_openssl_EVP_sha384() case crypto.SHA512: return C.go_openssl_EVP_sha512() - case crypto.SHA3_224: - if sha3Defined { + case crypto.SHA3_224, crypto.SHA3_256, crypto.SHA3_384, crypto.SHA3_512: + if version1_1_1_or_above() { return C.go_openssl_EVP_sha3_224() } case crypto.SHA3_256: - if sha3Defined { + if version1_1_1_or_above() { return C.go_openssl_EVP_sha3_256() } case crypto.SHA3_384: - if sha3Defined { + if version1_1_1_or_above() { return C.go_openssl_EVP_sha3_384() } case crypto.SHA3_512: - if sha3Defined { + if version1_1_1_or_above() { return C.go_openssl_EVP_sha3_512() } } diff --git a/hkdf.go b/hkdf.go index 20a966a7..db39b451 100644 --- a/hkdf.go +++ b/hkdf.go @@ -13,9 +13,7 @@ import ( ) func SupportsHKDF() bool { - return vMajor > 1 || - (vMajor >= 1 && vMinor > 1) || - (vMajor >= 1 && vMinor >= 1 && vPatch >= 1) + return version1_1_1_or_above() } func newHKDF(h func() hash.Hash, mode C.int) (*hkdf, error) { diff --git a/openssl.go b/openssl.go index 659b7e12..fce41943 100644 --- a/openssl.go +++ b/openssl.go @@ -271,3 +271,7 @@ func bnToBig(bn C.GO_BIGNUM_PTR) BigInt { func CheckLeaks() { C.go_openssl_do_leak_check() } + +func version1_1_1_or_above() bool { + return vMajor > 1 || (vMajor >= 1 && vMinor > 1) || (vMajor >= 1 && vMinor >= 1 && vPatch >= 1) +} diff --git a/sha.go b/sha.go index 05dd1469..3cc87687 100644 --- a/sha.go +++ b/sha.go @@ -269,17 +269,17 @@ func (h *md5Hash) Sum(in []byte) []byte { } const ( - sha5Magic = "md5\x01" - sha5MarshaledSize = len(sha5Magic) + 4*4 + 64 + 8 + md5Magic = "md5\x01" + md5MarshaledSize = len(md5Magic) + 4*4 + 64 + 8 ) func (h *md5Hash) MarshalBinary() ([]byte, error) { d := (*md5State)(h.shaState()) if d == nil { - return nil, errors.New("crypto/sha1: can't retrieve hash state") + return nil, errors.New("crypto/md5: can't retrieve hash state") } - b := make([]byte, 0, sha5MarshaledSize) - b = append(b, sha5Magic...) + b := make([]byte, 0, md5MarshaledSize) + b = append(b, md5Magic...) b = appendUint32(b, d.h[0]) b = appendUint32(b, d.h[1]) b = appendUint32(b, d.h[2]) @@ -291,17 +291,17 @@ func (h *md5Hash) MarshalBinary() ([]byte, error) { } func (h *md5Hash) UnmarshalBinary(b []byte) error { - if len(b) < len(sha5Magic) || string(b[:len(sha5Magic)]) != sha5Magic { - return errors.New("crypto/sha1: invalid hash state identifier") + if len(b) < len(md5Magic) || string(b[:len(md5Magic)]) != md5Magic { + return errors.New("crypto/md5: invalid hash state identifier") } - if len(b) != sha5MarshaledSize { - return errors.New("crypto/sha1: invalid hash state size") + if len(b) != md5MarshaledSize { + return errors.New("crypto/md5: invalid hash state size") } d := (*md5State)(h.shaState()) if d == nil { - return errors.New("crypto/sha1: can't retrieve hash state") + return errors.New("crypto/md5: can't retrieve hash state") } - b = b[len(sha5Magic):] + b = b[len(md5Magic):] b, d.h[0] = consumeUint32(b) b, d.h[1] = consumeUint32(b) b, d.h[2] = consumeUint32(b)