Skip to content

Commit

Permalink
code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Aug 17, 2023
1 parent f8d3e82 commit 0ed28d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
11 changes: 5 additions & 6 deletions evp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

previous case

Check failure on line 88 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

previous case
if version1_1_1_or_above() {
return C.go_openssl_EVP_sha3_224()
}
case crypto.SHA3_256:

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch

Check failure on line 92 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

duplicate case crypto.SHA3_256 (constant 11 of type crypto.Hash) in expression switch
if sha3Defined {
if version1_1_1_or_above() {
return C.go_openssl_EVP_sha3_256()
}
case crypto.SHA3_384:

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch

Check failure on line 96 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

duplicate case crypto.SHA3_384 (constant 12 of type crypto.Hash) in expression switch
if sha3Defined {
if version1_1_1_or_above() {
return C.go_openssl_EVP_sha3_384()
}
case crypto.SHA3_512:

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.1)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.1.0)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 1.0.2)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.1)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch

Check failure on line 100 in evp.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, 3.0.9)

duplicate case crypto.SHA3_512 (constant 13 of type crypto.Hash) in expression switch
if sha3Defined {
if version1_1_1_or_above() {
return C.go_openssl_EVP_sha3_512()
}
}
Expand Down
4 changes: 1 addition & 3 deletions hkdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
22 changes: 11 additions & 11 deletions sha.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand Down

0 comments on commit 0ed28d5

Please sign in to comment.